Merge lp:~kyrofa/go-trust-store/add_answer into lp:go-trust-store

Proposed by Kyle Fazzari
Status: Merged
Approved by: dobey
Approved revision: 2
Merged at revision: 2
Proposed branch: lp:~kyrofa/go-trust-store/add_answer
Merge into: lp:go-trust-store
Diff against target: 227 lines (+207/-0)
4 files modified
trust/answer.go (+72/-0)
trust/answer_shim.h (+40/-0)
trust/answer_test.go (+32/-0)
trust/test_answer.go (+63/-0)
To merge this branch: bzr merge lp:~kyrofa/go-trust-store/add_answer
Reviewer Review Type Date Requested Status
dobey (community) Approve
Review via email: mp+273116@code.launchpad.net

Commit message

Add Answer type and shim, representing a response from the user.

Description of the change

Add Answer type and shim, representing a response from the user.

To post a comment you must log in.
lp:~kyrofa/go-trust-store/add_answer updated
2. By Kyle Fazzari

Add Answer type and shim, representing a response from the user.

Signed-off-by: Kyle Fazzari <email address hidden>

Revision history for this message
dobey (dobey) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'trust'
2=== added file 'trust/answer.go'
3--- trust/answer.go 1970-01-01 00:00:00 +0000
4+++ trust/answer.go 2015-10-01 18:03:29 +0000
5@@ -0,0 +1,72 @@
6+/* Copyright (C) 2015 Canonical Ltd.
7+ *
8+ * This file is part of go-trust-store.
9+ *
10+ * go-trust-store is free software: you can redistribute it and/or modify it
11+ * under the terms of the GNU Lesser General Public License version 3, as
12+ * published by the Free Software Foundation.
13+ *
14+ * go-trust-store is distributed in the hope that it will be useful, but WITHOUT
15+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17+ * details.
18+ *
19+ * You should have received a copy of the GNU Lesser General Public License
20+ * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>.
21+ *
22+ * Authored by: Kyle Fazzari <kyle@canonical.com>
23+ */
24+
25+package trust
26+
27+// #include <stdlib.h>
28+// #include "answer_shim.h"
29+import "C"
30+
31+// Answer represents a response from the user: yes or no, granted or denied.
32+type Answer int
33+
34+const (
35+ AnswerDenied Answer = iota
36+ AnswerGranted
37+)
38+
39+type shimMapping struct {
40+ shim C.Answer
41+ name string
42+}
43+
44+// answerShimMappings maps the answers to their string and shim representations.
45+var answerShimMappings = map[Answer]shimMapping{
46+ AnswerDenied: shimMapping{
47+ shim: C.DENIED,
48+ name: "DENIED",
49+ },
50+ AnswerGranted: shimMapping{
51+ shim: C.GRANTED,
52+ name: "GRANTED",
53+ },
54+}
55+
56+// AnswerFromShim converts a shim answer to an Answer.
57+func AnswerFromShim(shimInt int) Answer {
58+ shim := (C.Answer)(shimInt)
59+
60+ for key, value := range answerShimMappings {
61+ if value.shim == shim {
62+ return key
63+ }
64+ }
65+
66+ return AnswerDenied
67+}
68+
69+// ToShim converts an Answer to a shim answer.
70+func (answer Answer) ToShim() int {
71+ return int(answerShimMappings[answer].shim)
72+}
73+
74+// String returns the string representation of the answer.
75+func (answer Answer) String() string {
76+ return answerShimMappings[answer].name
77+}
78
79=== added file 'trust/answer_shim.h'
80--- trust/answer_shim.h 1970-01-01 00:00:00 +0000
81+++ trust/answer_shim.h 2015-10-01 18:03:29 +0000
82@@ -0,0 +1,40 @@
83+/* Copyright (C) 2015 Canonical Ltd.
84+ *
85+ * This file is part of go-trust-store.
86+ *
87+ * go-trust-store is free software: you can redistribute it and/or modify it
88+ * under the terms of the GNU Lesser General Public License version 3, as
89+ * published by the Free Software Foundation.
90+ *
91+ * go-trust-store is distributed in the hope that it will be useful, but WITHOUT
92+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
93+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
94+ * details.
95+ *
96+ * You should have received a copy of the GNU Lesser General Public License
97+ * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>.
98+ *
99+ * Authored by: Kyle Fazzari <kyle@canonical.com>
100+ */
101+
102+#ifndef GO_TRUST_STORE_ANSWER_SHIM_H
103+#define GO_TRUST_STORE_ANSWER_SHIM_H
104+
105+#include <time.h>
106+#include <inttypes.h>
107+
108+#ifdef __cplusplus
109+extern "C" {
110+#endif
111+
112+typedef enum
113+{
114+ DENIED,
115+ GRANTED
116+} Answer;
117+
118+#ifdef __cplusplus
119+} // extern "C"
120+#endif
121+
122+#endif // GO_TRUST_STORE_ANSWER_SHIM_H
123
124=== added file 'trust/answer_test.go'
125--- trust/answer_test.go 1970-01-01 00:00:00 +0000
126+++ trust/answer_test.go 2015-10-01 18:03:29 +0000
127@@ -0,0 +1,32 @@
128+/* Copyright (C) 2015 Canonical Ltd.
129+ *
130+ * This file is part of go-trust-store.
131+ *
132+ * go-trust-store is free software: you can redistribute it and/or modify it
133+ * under the terms of the GNU Lesser General Public License version 3, as
134+ * published by the Free Software Foundation.
135+ *
136+ * go-trust-store is distributed in the hope that it will be useful, but WITHOUT
137+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
138+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
139+ * details.
140+ *
141+ * You should have received a copy of the GNU Lesser General Public License
142+ * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>.
143+ *
144+ * Authored by: Kyle Fazzari <kyle@canonical.com>
145+ */
146+
147+package trust
148+
149+import (
150+ "testing"
151+)
152+
153+func TestAnswer_FromShim(t *testing.T) {
154+ testAnswer_FromShim(t)
155+}
156+
157+func TestAnswer_ToShim(t *testing.T) {
158+ testAnswer_ToShim(t)
159+}
160
161=== added file 'trust/test_answer.go'
162--- trust/test_answer.go 1970-01-01 00:00:00 +0000
163+++ trust/test_answer.go 2015-10-01 18:03:29 +0000
164@@ -0,0 +1,63 @@
165+/* Copyright (C) 2015 Canonical Ltd.
166+ *
167+ * This file is part of go-trust-store.
168+ *
169+ * go-trust-store is free software: you can redistribute it and/or modify it
170+ * under the terms of the GNU Lesser General Public License version 3, as
171+ * published by the Free Software Foundation.
172+ *
173+ * go-trust-store is distributed in the hope that it will be useful, but WITHOUT
174+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
175+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
176+ * details.
177+ *
178+ * You should have received a copy of the GNU Lesser General Public License
179+ * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>.
180+ *
181+ * Authored by: Kyle Fazzari <kyle@canonical.com>
182+ */
183+
184+// Since cgo cannot be used within Go test files, this file holds the actual
185+// tests and the corresponding test file calls them (i.e. the tests contained
186+// within this file are not run directly, but are called from other tests).
187+package trust
188+
189+// #include <stdlib.h>
190+// #include "answer_shim.h"
191+import "C"
192+
193+import (
194+ "testing"
195+)
196+
197+var answerTests = []struct {
198+ answer Answer
199+ shim C.Answer
200+}{
201+ {AnswerDenied, C.DENIED},
202+ {AnswerGranted, C.GRANTED},
203+}
204+
205+// Test typical ToShim usage.
206+func testAnswer_ToShim(t *testing.T) {
207+ for i, test := range answerTests {
208+ shim := C.Answer(test.answer.ToShim())
209+
210+ if shim != test.shim {
211+ t.Errorf("Test case %d: Shim was %d, expected %d", i, shim,
212+ test.shim)
213+ }
214+ }
215+}
216+
217+// Test typical AnswerFromShim usage.
218+func testAnswer_FromShim(t *testing.T) {
219+ for i, test := range answerTests {
220+ answer := AnswerFromShim(int(test.shim))
221+
222+ if answer != test.answer {
223+ t.Errorf("Test case %d: Answer was %s, expected %s", i, answer,
224+ test.answer)
225+ }
226+ }
227+}

Subscribers

People subscribed via source and target branches

to all changes: