Merge lp:~kyrofa/go-trust-store/add_request into lp:go-trust-store
- add_request
- Merge into trunk
Proposed by Kyle Fazzari
| Status: | Merged |
|---|---|
| Approved by: | Kyle Fazzari |
| Approved revision: | 3 |
| Merged at revision: | 3 |
| Proposed branch: | lp:~kyrofa/go-trust-store/add_request |
| Merge into: | lp:go-trust-store |
| Prerequisite: | lp:~kyrofa/go-trust-store/add_answer |
| Diff against target: | 309 lines (+290/-0) 4 files modified trust/request.go (+75/-0) trust/request_shim.h (+51/-0) trust/request_test.go (+32/-0) trust/test_request.go (+132/-0) |
| To merge this branch: | bzr merge lp:~kyrofa/go-trust-store/add_request |
| Related bugs: |
| Reviewer | Review Type | Date Requested | Status |
|---|---|---|---|
| dobey (community) | Approve | ||
| Review via email: | |||
Commit message
Add Request type and shim, representing a trust request and answer.
Description of the change
Add Request type and shim, representing a trust request and answer.
To post a comment you must log in.
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.go' |
| 2 | --- trust/request.go 1970-01-01 00:00:00 +0000 |
| 3 | +++ trust/request.go 2015-10-01 18:31:25 +0000 |
| 4 | @@ -0,0 +1,75 @@ |
| 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 <sys/types.h> |
| 28 | +// #include "request_shim.h" |
| 29 | +import "C" |
| 30 | + |
| 31 | +import ( |
| 32 | + "runtime" |
| 33 | + "time" |
| 34 | + "unsafe" |
| 35 | +) |
| 36 | + |
| 37 | +// Feature represents a trusted-helper-specific feature to which access is |
| 38 | +// requested via the trust store. |
| 39 | +type Feature uint64 |
| 40 | + |
| 41 | +// Request represents a timestamped permission request and answer to be stored |
| 42 | +// in the trust store. |
| 43 | +type Request struct { |
| 44 | + Answer Answer |
| 45 | + From string |
| 46 | + Feature Feature |
| 47 | + When time.Time |
| 48 | +} |
| 49 | + |
| 50 | +// FromShim converts a shim request into a Request. |
| 51 | +func (request *Request) FromShim(shimPointer unsafe.Pointer) { |
| 52 | + shim := (*C.Request)(shimPointer) |
| 53 | + |
| 54 | + request.Answer = AnswerFromShim(int(shim.answer)) |
| 55 | + request.From = C.GoString(shim.from) |
| 56 | + request.Feature = Feature(shim.feature) |
| 57 | + request.When = time.Unix(int64(shim.when.seconds), |
| 58 | + int64(shim.when.nanoseconds)) |
| 59 | +} |
| 60 | + |
| 61 | +// ToShim converts a Request into a shim request. |
| 62 | +func (request Request) ToShim() unsafe.Pointer { |
| 63 | + shim := new(C.Request) |
| 64 | + |
| 65 | + shim.answer = C.Answer(request.Answer.ToShim()) |
| 66 | + shim.from = C.CString(request.From) |
| 67 | + shim.feature = C.uint64_t(request.Feature) |
| 68 | + shim.when.seconds = C.int64_t(request.When.Unix()) |
| 69 | + shim.when.nanoseconds = C.int32_t(request.When.Nanosecond()) |
| 70 | + |
| 71 | + runtime.SetFinalizer(shim, destroyRequestShim) |
| 72 | + |
| 73 | + return unsafe.Pointer(shim) |
| 74 | +} |
| 75 | + |
| 76 | +// destroyRequestShim frees the resources of a shim request. |
| 77 | +func destroyRequestShim(shim *C.Request) { |
| 78 | + C.free(unsafe.Pointer(shim.from)) |
| 79 | +} |
| 80 | |
| 81 | === added file 'trust/request_shim.h' |
| 82 | --- trust/request_shim.h 1970-01-01 00:00:00 +0000 |
| 83 | +++ trust/request_shim.h 2015-10-01 18:31:25 +0000 |
| 84 | @@ -0,0 +1,51 @@ |
| 85 | +/* Copyright (C) 2015 Canonical Ltd. |
| 86 | + * |
| 87 | + * This file is part of go-trust-store. |
| 88 | + * |
| 89 | + * go-trust-store is free software: you can redistribute it and/or modify it |
| 90 | + * under the terms of the GNU Lesser General Public License version 3, as |
| 91 | + * published by the Free Software Foundation. |
| 92 | + * |
| 93 | + * go-trust-store is distributed in the hope that it will be useful, but WITHOUT |
| 94 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 95 | + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 96 | + * details. |
| 97 | + * |
| 98 | + * You should have received a copy of the GNU Lesser General Public License |
| 99 | + * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>. |
| 100 | + * |
| 101 | + * Authored by: Kyle Fazzari <kyle@canonical.com> |
| 102 | + */ |
| 103 | + |
| 104 | +#ifndef GO_TRUST_STORE_AGENT_REQUEST_SHIM_H |
| 105 | +#define GO_TRUST_STORE_AGENT_REQUEST_SHIM_H |
| 106 | + |
| 107 | +#include <inttypes.h> |
| 108 | + |
| 109 | +#include "answer_shim.h" |
| 110 | + |
| 111 | +#ifdef __cplusplus |
| 112 | +extern "C" { |
| 113 | +#endif |
| 114 | + |
| 115 | +// Not using timespec so we're not limited to 32 bits for the seconds. |
| 116 | +typedef struct |
| 117 | +{ |
| 118 | + int64_t seconds; |
| 119 | + int32_t nanoseconds; |
| 120 | +} Timestamp; |
| 121 | + |
| 122 | +// A shim request. |
| 123 | +typedef struct |
| 124 | +{ |
| 125 | + Answer answer; |
| 126 | + char *from; |
| 127 | + uint64_t feature; |
| 128 | + Timestamp when; |
| 129 | +} Request; |
| 130 | + |
| 131 | +#ifdef __cplusplus |
| 132 | +} // extern "C" |
| 133 | +#endif |
| 134 | + |
| 135 | +#endif // GO_TRUST_STORE_AGENT_REQUEST_SHIM_H |
| 136 | |
| 137 | === added file 'trust/request_test.go' |
| 138 | --- trust/request_test.go 1970-01-01 00:00:00 +0000 |
| 139 | +++ trust/request_test.go 2015-10-01 18:31:25 +0000 |
| 140 | @@ -0,0 +1,32 @@ |
| 141 | +/* Copyright (C) 2015 Canonical Ltd. |
| 142 | + * |
| 143 | + * This file is part of go-trust-store. |
| 144 | + * |
| 145 | + * go-trust-store is free software: you can redistribute it and/or modify it |
| 146 | + * under the terms of the GNU Lesser General Public License version 3, as |
| 147 | + * published by the Free Software Foundation. |
| 148 | + * |
| 149 | + * go-trust-store is distributed in the hope that it will be useful, but WITHOUT |
| 150 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 151 | + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 152 | + * details. |
| 153 | + * |
| 154 | + * You should have received a copy of the GNU Lesser General Public License |
| 155 | + * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>. |
| 156 | + * |
| 157 | + * Authored by: Kyle Fazzari <kyle@canonical.com> |
| 158 | + */ |
| 159 | + |
| 160 | +package trust |
| 161 | + |
| 162 | +import ( |
| 163 | + "testing" |
| 164 | +) |
| 165 | + |
| 166 | +func TestRequest_FromShim(t *testing.T) { |
| 167 | + testRequest_FromShim(t) |
| 168 | +} |
| 169 | + |
| 170 | +func TestRequest_ToShim(t *testing.T) { |
| 171 | + testRequest_ToShim(t) |
| 172 | +} |
| 173 | |
| 174 | === added file 'trust/test_request.go' |
| 175 | --- trust/test_request.go 1970-01-01 00:00:00 +0000 |
| 176 | +++ trust/test_request.go 2015-10-01 18:31:25 +0000 |
| 177 | @@ -0,0 +1,132 @@ |
| 178 | +/* Copyright (C) 2015 Canonical Ltd. |
| 179 | + * |
| 180 | + * This file is part of go-trust-store. |
| 181 | + * |
| 182 | + * go-trust-store is free software: you can redistribute it and/or modify it |
| 183 | + * under the terms of the GNU Lesser General Public License version 3, as |
| 184 | + * published by the Free Software Foundation. |
| 185 | + * |
| 186 | + * go-trust-store is distributed in the hope that it will be useful, but WITHOUT |
| 187 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 188 | + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 189 | + * details. |
| 190 | + * |
| 191 | + * You should have received a copy of the GNU Lesser General Public License |
| 192 | + * along with go-trust-store. If not, see <http://www.gnu.org/licenses/>. |
| 193 | + * |
| 194 | + * Authored by: Kyle Fazzari <kyle@canonical.com> |
| 195 | + */ |
| 196 | + |
| 197 | +// Since cgo cannot be used within Go test files, this file holds the actual |
| 198 | +// tests and the corresponding test file calls them (i.e. the tests contained |
| 199 | +// within this file are not run directly, but are called from other tests). |
| 200 | +package trust |
| 201 | + |
| 202 | +// #include <stdlib.h> |
| 203 | +// #include "request_shim.h" |
| 204 | +import "C" |
| 205 | + |
| 206 | +import ( |
| 207 | + "testing" |
| 208 | + "time" |
| 209 | + "unsafe" |
| 210 | +) |
| 211 | + |
| 212 | +// Test typical ToShim usage. |
| 213 | +func testRequest_ToShim(t *testing.T) { |
| 214 | + // First, create a trust.Request with known values |
| 215 | + now := time.Now() |
| 216 | + nowUnixSeconds := now.Unix() |
| 217 | + nowUnixNanoseconds := now.Nanosecond() |
| 218 | + |
| 219 | + request := Request{ |
| 220 | + Answer: AnswerGranted, |
| 221 | + From: "foo", |
| 222 | + Feature: 1, |
| 223 | + When: now, |
| 224 | + } |
| 225 | + |
| 226 | + // Now convert it to a shim instance |
| 227 | + shim := (*C.Request)(request.ToShim()) |
| 228 | + if shim == nil { |
| 229 | + t.Fatal("Shim was unexpectedly nil") |
| 230 | + } |
| 231 | + |
| 232 | + from := C.GoString(shim.from) |
| 233 | + whenSeconds := int64(shim.when.seconds) |
| 234 | + whenNanoseconds := int(shim.when.nanoseconds) |
| 235 | + |
| 236 | + // Verify that the values are still the known values |
| 237 | + if shim.answer != C.GRANTED { |
| 238 | + t.Errorf("Answer was %d, expected %d", shim.answer, C.GRANTED) |
| 239 | + } |
| 240 | + |
| 241 | + if from != "foo" { |
| 242 | + t.Errorf(`From was "%s", expected "foo"`, from) |
| 243 | + } |
| 244 | + |
| 245 | + if shim.feature != 1 { |
| 246 | + t.Errorf("Feature was %d, expected 1", shim.feature) |
| 247 | + } |
| 248 | + |
| 249 | + if whenSeconds != nowUnixSeconds { |
| 250 | + t.Errorf("When's seconds were %d, expected %d", whenSeconds, |
| 251 | + nowUnixSeconds) |
| 252 | + } |
| 253 | + |
| 254 | + if whenNanoseconds != nowUnixNanoseconds { |
| 255 | + t.Errorf("When's nanoseconds were %d, expected %d", whenNanoseconds, |
| 256 | + nowUnixNanoseconds) |
| 257 | + } |
| 258 | +} |
| 259 | + |
| 260 | +// Test typical FromShim usage. |
| 261 | +func testRequest_FromShim(t *testing.T) { |
| 262 | + fooCstring := C.CString("foo") |
| 263 | + defer C.free(unsafe.Pointer(fooCstring)) |
| 264 | + |
| 265 | + // First, create shim with known values |
| 266 | + shim := new(C.Request) |
| 267 | + shim.answer = C.GRANTED |
| 268 | + shim.from = fooCstring |
| 269 | + shim.feature = 1 |
| 270 | + |
| 271 | + // Getting time from Go so we don't have to worry about 32-bit times |
| 272 | + now := time.Now() |
| 273 | + |
| 274 | + shim.when.seconds = C.int64_t(now.Unix()) |
| 275 | + shim.when.nanoseconds = C.int32_t(now.Nanosecond()) |
| 276 | + |
| 277 | + shimSeconds := int64(shim.when.seconds) |
| 278 | + shimNanoseconds := int(shim.when.nanoseconds) |
| 279 | + |
| 280 | + // Now convert it to a trust.Request instance |
| 281 | + var request Request |
| 282 | + request.FromShim(unsafe.Pointer(shim)) |
| 283 | + |
| 284 | + whenUnixSeconds := request.When.Unix() |
| 285 | + whenUnixNanoseconds := request.When.Nanosecond() |
| 286 | + |
| 287 | + // Verify that the values are still the known values |
| 288 | + if request.Answer != AnswerGranted { |
| 289 | + t.Errorf("Answer was %s, expected %s", request.Answer, AnswerGranted) |
| 290 | + } |
| 291 | + |
| 292 | + if request.From != "foo" { |
| 293 | + t.Errorf(`From was "%s", expected "foo"`, request.From) |
| 294 | + } |
| 295 | + |
| 296 | + if request.Feature != 1 { |
| 297 | + t.Errorf("Feature was %d, expected 1", request.Feature) |
| 298 | + } |
| 299 | + |
| 300 | + if whenUnixSeconds != shimSeconds { |
| 301 | + t.Errorf("When's seconds were %d, expected %d", whenUnixSeconds, |
| 302 | + shimSeconds) |
| 303 | + } |
| 304 | + |
| 305 | + if whenUnixNanoseconds != shimNanoseconds { |
| 306 | + t.Errorf("When's nanoseconds were %d, expected %d", |
| 307 | + whenUnixNanoseconds, shimNanoseconds) |
| 308 | + } |
| 309 | +} |