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

Proposed by Kyle Fazzari
Status: Merged
Approved by: dobey
Approved revision: 4
Merged at revision: 4
Proposed branch: lp:~kyrofa/go-trust-store/add_request_parameters
Merge into: lp:go-trust-store
Prerequisite: lp:~kyrofa/go-trust-store/add_request
Diff against target: 308 lines (+289/-0)
4 files modified
trust/request_parameters.go (+80/-0)
trust/request_parameters_shim.h (+51/-0)
trust/request_parameters_test.go (+32/-0)
trust/test_request_parameters.go (+126/-0)
To merge this branch: bzr merge lp:~kyrofa/go-trust-store/add_request_parameters
Reviewer Review Type Date Requested Status
dobey (community) Approve
Review via email: mp+273122@code.launchpad.net

Commit message

Add RequestParameters type and shim, for trust prompting.

Description of the change

Add RequestParameters type and shim, for trust prompting.

To post a comment you must log in.
4. By Kyle Fazzari

Add RequestParameters type and shim, for trust prompting.

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 file 'trust/request_parameters.go'
2--- trust/request_parameters.go 1970-01-01 00:00:00 +0000
3+++ trust/request_parameters.go 2015-10-01 18:48:45 +0000
4@@ -0,0 +1,80 @@
5+/* Copyright (C) 2015 Canonical Ltd.
6+ *
7+ * This file is part of go-trust-store.
8+ *
9+ * go-trust-store is free software: you can redistribute it and/or modify it
10+ * under the terms of the GNU Lesser General Public License version 3, as
11+ * published by the Free Software Foundation.
12+ *
13+ * go-trust-store is distributed in the hope that it will be useful, but WITHOUT
14+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16+ * details.
17+ *
18+ * You should have received a copy of the GNU Lesser General Public License
19+ * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>.
20+ *
21+ * Authored by: Kyle Fazzari <kyle@canonical.com>
22+ */
23+
24+package trust
25+
26+// #include <stdlib.h>
27+// #include "request_parameters_shim.h"
28+import "C"
29+
30+import (
31+ "runtime"
32+ "unsafe"
33+)
34+
35+// Application represents an application that is requesting access to a feature
36+// via the trust store.
37+type Application struct {
38+ Uid int // The user under which the application is running
39+ Pid int // The PID of the application
40+ Id string // The appid of the application
41+}
42+
43+// RequestParameters contains the information necessary to prompt the user
44+// for permission via the trust store.
45+type RequestParameters struct {
46+ Application Application // The application requesting access
47+ Feature Feature // The feature the application wants to access
48+ Description string // The description to use for prompting the user
49+}
50+
51+// FromShim converts from shim request parameters to RequestParameters.
52+func (parameters *RequestParameters) FromShim(shimPointer unsafe.Pointer) {
53+ shim := (*C.RequestParameters)(shimPointer)
54+
55+ parameters.Application.Uid = int(shim.application.uid)
56+ parameters.Application.Pid = int(shim.application.pid)
57+ parameters.Application.Id = C.GoString(shim.application.id)
58+
59+ parameters.Feature = Feature(shim.feature)
60+ parameters.Description = C.GoString(shim.description)
61+}
62+
63+// ToShim converts RequestParameters to shim request parameters.
64+func (parameters RequestParameters) ToShim() unsafe.Pointer {
65+ shim := new(C.RequestParameters)
66+
67+ shim.application.uid = C.int(parameters.Application.Uid)
68+ shim.application.pid = C.int(parameters.Application.Pid)
69+ shim.application.id = C.CString(parameters.Application.Id)
70+
71+ shim.feature = C.uint64_t(parameters.Feature)
72+ shim.description = C.CString(parameters.Description)
73+
74+ runtime.SetFinalizer(shim, destroyRequestParametersShim)
75+
76+ return unsafe.Pointer(shim)
77+}
78+
79+// destroyRequestParametersShim frees the resources of shim request
80+// parameters.
81+func destroyRequestParametersShim(shim *C.RequestParameters) {
82+ C.free(unsafe.Pointer(shim.application.id))
83+ C.free(unsafe.Pointer(shim.description))
84+}
85
86=== added file 'trust/request_parameters_shim.h'
87--- trust/request_parameters_shim.h 1970-01-01 00:00:00 +0000
88+++ trust/request_parameters_shim.h 2015-10-01 18:48:45 +0000
89@@ -0,0 +1,51 @@
90+/* Copyright (C) 2015 Canonical Ltd.
91+ *
92+ * This file is part of go-trust-store.
93+ *
94+ * go-trust-store is free software: you can redistribute it and/or modify it
95+ * under the terms of the GNU Lesser General Public License version 3, as
96+ * published by the Free Software Foundation.
97+ *
98+ * go-trust-store is distributed in the hope that it will be useful, but WITHOUT
99+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
100+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
101+ * details.
102+ *
103+ * You should have received a copy of the GNU Lesser General Public License
104+ * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>.
105+ *
106+ * Authored by: Kyle Fazzari <kyle@canonical.com>
107+ */
108+
109+#ifndef GO_TRUST_STORE_REQUEST_PARAMETERS_SHIM_H
110+#define GO_TRUST_STORE_REQUEST_PARAMETERS_SHIM_H
111+
112+#include <inttypes.h>
113+
114+#ifdef __cplusplus
115+extern "C" {
116+#endif
117+
118+// Represents an application that is requesting access to a feature via the
119+// trust store.
120+typedef struct
121+{
122+ int uid;
123+ int pid;
124+ char *id;
125+} Application;
126+
127+// Contains the information necessary to prompt the user for permission via the
128+// trust store.
129+typedef struct
130+{
131+ Application application;
132+ uint64_t feature;
133+ char *description;
134+} RequestParameters;
135+
136+#ifdef __cplusplus
137+} // extern "C"
138+#endif
139+
140+#endif // GO_TRUST_STORE_REQUEST_PARAMETERS_SHIM_H
141
142=== added file 'trust/request_parameters_test.go'
143--- trust/request_parameters_test.go 1970-01-01 00:00:00 +0000
144+++ trust/request_parameters_test.go 2015-10-01 18:48:45 +0000
145@@ -0,0 +1,32 @@
146+/* Copyright (C) 2015 Canonical Ltd.
147+ *
148+ * This file is part of go-trust-store.
149+ *
150+ * go-trust-store is free software: you can redistribute it and/or modify it
151+ * under the terms of the GNU Lesser General Public License version 3, as
152+ * published by the Free Software Foundation.
153+ *
154+ * go-trust-store is distributed in the hope that it will be useful, but WITHOUT
155+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
156+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
157+ * details.
158+ *
159+ * You should have received a copy of the GNU Lesser General Public License
160+ * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>.
161+ *
162+ * Authored by: Kyle Fazzari <kyle@canonical.com>
163+ */
164+
165+package trust
166+
167+import (
168+ "testing"
169+)
170+
171+func TestRequestParameters_FromShim(t *testing.T) {
172+ testRequestParameters_FromShim(t)
173+}
174+
175+func TestRequestParameters_ToShim(t *testing.T) {
176+ testRequestParameters_ToShim(t)
177+}
178
179=== added file 'trust/test_request_parameters.go'
180--- trust/test_request_parameters.go 1970-01-01 00:00:00 +0000
181+++ trust/test_request_parameters.go 2015-10-01 18:48:45 +0000
182@@ -0,0 +1,126 @@
183+/* Copyright (C) 2015 Canonical Ltd.
184+ *
185+ * This file is part of go-trust-store.
186+ *
187+ * go-trust-store is free software: you can redistribute it and/or modify it
188+ * under the terms of the GNU Lesser General Public License version 3, as
189+ * published by the Free Software Foundation.
190+ *
191+ * go-trust-store is distributed in the hope that it will be useful, but WITHOUT
192+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
193+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
194+ * details.
195+ *
196+ * You should have received a copy of the GNU Lesser General Public License
197+ * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>.
198+ *
199+ * Authored by: Kyle Fazzari <kyle@canonical.com>
200+ */
201+
202+// Since cgo cannot be used within Go test files, this file holds the actual
203+// tests and the corresponding test file calls them (i.e. the tests contained
204+// within this file are not run directly, but are called from other tests).
205+package trust
206+
207+// #include <stdlib.h>
208+// #include "request_parameters_shim.h"
209+import "C"
210+
211+import (
212+ "testing"
213+ "unsafe"
214+)
215+
216+// Test typical ToShim usage.
217+func testRequestParameters_ToShim(t *testing.T) {
218+ // First, create a trust.RequestParameters with known values
219+ parameters := RequestParameters{
220+ Application: Application{
221+ Uid: 1,
222+ Pid: 2,
223+ Id: "foo",
224+ },
225+ Feature: 3,
226+ Description: "bar",
227+ }
228+
229+ // Now convert it to a shim instance
230+ shim := (*C.RequestParameters)(parameters.ToShim())
231+ if shim == nil {
232+ t.Fatal("Shim was unexpectedly nil")
233+ }
234+
235+ id := C.GoString(shim.application.id)
236+ description := C.GoString(shim.description)
237+
238+ // Verify that the values are still the known values
239+ if shim.application.uid != 1 {
240+ t.Errorf("Application Uid was %d, expected 1",
241+ shim.application.uid)
242+ }
243+
244+ if shim.application.pid != 2 {
245+ t.Errorf("Application Uid was %d, expected 2",
246+ shim.application.pid)
247+ }
248+
249+ if id != "foo" {
250+ t.Errorf(`Application Id was "%s", expected "foo"`, id)
251+ }
252+
253+ if shim.feature != 3 {
254+ t.Errorf("Feature was %d, expected 3",
255+ shim.feature)
256+ }
257+
258+ if description != "bar" {
259+ t.Errorf(`Description was "%s", expected "bar"`, description)
260+ }
261+}
262+
263+// Test typical FromShim usage.
264+func testRequestParameters_FromShim(t *testing.T) {
265+ fooCstring := C.CString("foo")
266+ defer C.free(unsafe.Pointer(fooCstring))
267+
268+ barCstring := C.CString("bar")
269+ defer C.free(unsafe.Pointer(barCstring))
270+
271+ // First, create shim with known values
272+ shim := new(C.RequestParameters)
273+ shim.application.uid = 1
274+ shim.application.pid = 2
275+ shim.application.id = fooCstring
276+ shim.feature = 3
277+ shim.description = barCstring
278+
279+ // Now convert it to a trust.RequestParameters instance
280+ var parameters RequestParameters
281+ parameters.FromShim(unsafe.Pointer(shim))
282+
283+ // Verify that the values are still the known values
284+ if parameters.Application.Uid != 1 {
285+ t.Errorf("Application Uid was %d, expected 1",
286+ parameters.Application.Uid)
287+ }
288+
289+ if parameters.Application.Pid != 2 {
290+ t.Errorf("Application Pid was %d, expected 2",
291+ parameters.Application.Pid)
292+ }
293+
294+ if parameters.Application.Id != "foo" {
295+ t.Errorf(`Application Id was "%s", expected "foo"`,
296+ parameters.Application.Id)
297+ }
298+
299+ if parameters.Feature != 3 {
300+ t.Errorf("Feature was %d, expected 3",
301+ parameters.Feature)
302+ }
303+
304+ if parameters.Description != "bar" {
305+ t.Errorf(`Description was "%s", expected "bar"`,
306+ parameters.Description)
307+ }
308+}

Subscribers

People subscribed via source and target branches

to all changes: