7
jc "github.com/juju/testing/checkers"
10
"gopkg.in/macaroon-bakery.v1/bakery"
13
type KeysSuite struct{}
15
var _ = gc.Suite(&KeysSuite{})
17
var testKey = newTestKey(0)
19
func (*KeysSuite) TestMarshalBinary(c *gc.C) {
20
data, err := testKey.MarshalBinary()
21
c.Assert(err, gc.IsNil)
22
c.Assert(data, jc.DeepEquals, []byte(testKey[:]))
25
err = key1.UnmarshalBinary(data)
26
c.Assert(err, gc.IsNil)
27
c.Assert(key1, gc.DeepEquals, testKey)
30
func (*KeysSuite) TestMarshalText(c *gc.C) {
31
data, err := testKey.MarshalText()
32
c.Assert(err, gc.IsNil)
33
c.Assert(string(data), gc.Equals, base64.StdEncoding.EncodeToString([]byte(testKey[:])))
36
err = key1.UnmarshalText(data)
37
c.Assert(err, gc.IsNil)
38
c.Assert(key1, gc.Equals, testKey)
41
func (*KeysSuite) TestKeyPairMarshalJSON(c *gc.C) {
43
Public: bakery.PublicKey{testKey},
44
Private: bakery.PrivateKey{testKey},
46
kp.Private.Key[0] = 99
47
data, err := json.Marshal(kp)
48
c.Assert(err, gc.IsNil)
50
err = json.Unmarshal(data, &x)
51
c.Assert(err, gc.IsNil)
53
// Check that the fields have marshaled as strings.
54
c.Assert(x.(map[string]interface{})["private"], gc.FitsTypeOf, "")
55
c.Assert(x.(map[string]interface{})["public"], gc.FitsTypeOf, "")
57
var kp1 bakery.KeyPair
58
err = json.Unmarshal(data, &kp1)
59
c.Assert(err, gc.IsNil)
60
c.Assert(kp1, jc.DeepEquals, kp)
63
func newTestKey(n byte) bakery.Key {
71
type addPublicKeyArgs struct {
77
var publicKeyRingTests = []struct {
79
add []addPublicKeyArgs
84
about: "empty keyring",
85
add: []addPublicKeyArgs{},
89
about: "single non-prefix key",
90
add: []addPublicKeyArgs{{
91
loc: "http://foo.com/x",
94
loc: "http://foo.com/x",
97
about: "single prefix key",
98
add: []addPublicKeyArgs{{
99
loc: "http://foo.com/x",
103
loc: "http://foo.com/x",
106
about: "pattern longer than url",
107
add: []addPublicKeyArgs{{
108
loc: "http://foo.com/x",
112
loc: "http://foo.com/",
113
expectNotFound: true,
115
about: "pattern not ending in /",
116
add: []addPublicKeyArgs{{
117
loc: "http://foo.com/x",
121
loc: "http://foo.com/x/y",
122
expectNotFound: true,
124
about: "mismatched host",
125
add: []addPublicKeyArgs{{
126
loc: "http://foo.com/x",
130
loc: "http://bar.com/x/y",
131
expectNotFound: true,
133
about: "http vs https",
134
add: []addPublicKeyArgs{{
135
loc: "http://foo.com/x",
138
loc: "https://foo.com/x",
141
about: "naked pattern url with prefix",
142
add: []addPublicKeyArgs{{
143
loc: "http://foo.com",
147
loc: "http://foo.com/arble",
150
about: "naked pattern url with prefix with naked match url",
151
add: []addPublicKeyArgs{{
152
loc: "http://foo.com",
156
loc: "http://foo.com",
159
about: "naked pattern url, no prefix",
160
add: []addPublicKeyArgs{{
161
loc: "http://foo.com",
164
loc: "http://foo.com",
167
about: "naked pattern url, no prefix, match with no slash",
168
add: []addPublicKeyArgs{{
169
loc: "http://foo.com",
172
loc: "http://foo.com/",
175
about: "port mismatch",
176
add: []addPublicKeyArgs{{
177
loc: "http://foo.com:8080/x",
180
loc: "https://foo.com/x",
181
expectNotFound: true,
183
about: "url longer than pattern",
184
add: []addPublicKeyArgs{{
185
loc: "http://foo.com/x/",
189
loc: "https://foo.com/x/y/z",
192
about: "longer match preferred",
193
add: []addPublicKeyArgs{{
194
loc: "http://foo.com/x/",
198
loc: "http://foo.com/x/y/",
202
loc: "https://foo.com/x/y/z",
203
expectKey: newTestKey(1),
205
about: "longer match preferred, with other matches",
206
add: []addPublicKeyArgs{{
207
loc: "http://foo.com/foo/arble",
211
loc: "http://foo.com/foo/arble/blah/",
215
loc: "http://foo.com/foo/",
219
loc: "http://foo.com/foobieblahbletcharbl",
223
loc: "https://foo.com/foo/arble/blah/x",
224
expectKey: newTestKey(1),
227
func (*KeysSuite) TestPublicKeyRing(c *gc.C) {
228
for i, test := range publicKeyRingTests {
229
c.Logf("test %d: %s", i, test.about)
230
kr := bakery.NewPublicKeyRing()
231
for _, add := range test.add {
232
err := kr.AddPublicKeyForLocation(add.loc, add.prefix, &bakery.PublicKey{add.key})
233
c.Assert(err, gc.IsNil)
235
key, err := kr.PublicKeyForLocation(test.loc)
236
if test.expectNotFound {
237
c.Assert(err, gc.Equals, bakery.ErrNotFound)
238
c.Assert(key, gc.IsNil)
241
c.Assert(err, gc.IsNil)
242
c.Assert(*key, gc.Equals, bakery.PublicKey{test.expectKey})