~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/gopkg.in/macaroon-bakery.v1/bakery/keys_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package bakery_test
 
2
 
 
3
import (
 
4
        "encoding/base64"
 
5
        "encoding/json"
 
6
 
 
7
        jc "github.com/juju/testing/checkers"
 
8
        gc "gopkg.in/check.v1"
 
9
 
 
10
        "gopkg.in/macaroon-bakery.v1/bakery"
 
11
)
 
12
 
 
13
type KeysSuite struct{}
 
14
 
 
15
var _ = gc.Suite(&KeysSuite{})
 
16
 
 
17
var testKey = newTestKey(0)
 
18
 
 
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[:]))
 
23
 
 
24
        var key1 bakery.Key
 
25
        err = key1.UnmarshalBinary(data)
 
26
        c.Assert(err, gc.IsNil)
 
27
        c.Assert(key1, gc.DeepEquals, testKey)
 
28
}
 
29
 
 
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[:])))
 
34
 
 
35
        var key1 bakery.Key
 
36
        err = key1.UnmarshalText(data)
 
37
        c.Assert(err, gc.IsNil)
 
38
        c.Assert(key1, gc.Equals, testKey)
 
39
}
 
40
 
 
41
func (*KeysSuite) TestKeyPairMarshalJSON(c *gc.C) {
 
42
        kp := bakery.KeyPair{
 
43
                Public:  bakery.PublicKey{testKey},
 
44
                Private: bakery.PrivateKey{testKey},
 
45
        }
 
46
        kp.Private.Key[0] = 99
 
47
        data, err := json.Marshal(kp)
 
48
        c.Assert(err, gc.IsNil)
 
49
        var x interface{}
 
50
        err = json.Unmarshal(data, &x)
 
51
        c.Assert(err, gc.IsNil)
 
52
 
 
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, "")
 
56
 
 
57
        var kp1 bakery.KeyPair
 
58
        err = json.Unmarshal(data, &kp1)
 
59
        c.Assert(err, gc.IsNil)
 
60
        c.Assert(kp1, jc.DeepEquals, kp)
 
61
}
 
62
 
 
63
func newTestKey(n byte) bakery.Key {
 
64
        var k bakery.Key
 
65
        for i := range k {
 
66
                k[i] = n + byte(i)
 
67
        }
 
68
        return k
 
69
}
 
70
 
 
71
type addPublicKeyArgs struct {
 
72
        loc    string
 
73
        prefix bool
 
74
        key    bakery.Key
 
75
}
 
76
 
 
77
var publicKeyRingTests = []struct {
 
78
        about          string
 
79
        add            []addPublicKeyArgs
 
80
        loc            string
 
81
        expectKey      bakery.Key
 
82
        expectNotFound bool
 
83
}{{
 
84
        about:          "empty keyring",
 
85
        add:            []addPublicKeyArgs{},
 
86
        loc:            "something",
 
87
        expectNotFound: true,
 
88
}, {
 
89
        about: "single non-prefix key",
 
90
        add: []addPublicKeyArgs{{
 
91
                loc: "http://foo.com/x",
 
92
                key: testKey,
 
93
        }},
 
94
        loc:       "http://foo.com/x",
 
95
        expectKey: testKey,
 
96
}, {
 
97
        about: "single prefix key",
 
98
        add: []addPublicKeyArgs{{
 
99
                loc:    "http://foo.com/x",
 
100
                key:    testKey,
 
101
                prefix: true,
 
102
        }},
 
103
        loc:       "http://foo.com/x",
 
104
        expectKey: testKey,
 
105
}, {
 
106
        about: "pattern longer than url",
 
107
        add: []addPublicKeyArgs{{
 
108
                loc:    "http://foo.com/x",
 
109
                key:    testKey,
 
110
                prefix: true,
 
111
        }},
 
112
        loc:            "http://foo.com/",
 
113
        expectNotFound: true,
 
114
}, {
 
115
        about: "pattern not ending in /",
 
116
        add: []addPublicKeyArgs{{
 
117
                loc:    "http://foo.com/x",
 
118
                key:    testKey,
 
119
                prefix: true,
 
120
        }},
 
121
        loc:            "http://foo.com/x/y",
 
122
        expectNotFound: true,
 
123
}, {
 
124
        about: "mismatched host",
 
125
        add: []addPublicKeyArgs{{
 
126
                loc:    "http://foo.com/x",
 
127
                key:    testKey,
 
128
                prefix: true,
 
129
        }},
 
130
        loc:            "http://bar.com/x/y",
 
131
        expectNotFound: true,
 
132
}, {
 
133
        about: "http vs https",
 
134
        add: []addPublicKeyArgs{{
 
135
                loc: "http://foo.com/x",
 
136
                key: testKey,
 
137
        }},
 
138
        loc:       "https://foo.com/x",
 
139
        expectKey: testKey,
 
140
}, {
 
141
        about: "naked pattern url with prefix",
 
142
        add: []addPublicKeyArgs{{
 
143
                loc:    "http://foo.com",
 
144
                key:    testKey,
 
145
                prefix: true,
 
146
        }},
 
147
        loc:       "http://foo.com/arble",
 
148
        expectKey: testKey,
 
149
}, {
 
150
        about: "naked pattern url with prefix with naked match url",
 
151
        add: []addPublicKeyArgs{{
 
152
                loc:    "http://foo.com",
 
153
                key:    testKey,
 
154
                prefix: true,
 
155
        }},
 
156
        loc:       "http://foo.com",
 
157
        expectKey: testKey,
 
158
}, {
 
159
        about: "naked pattern url, no prefix",
 
160
        add: []addPublicKeyArgs{{
 
161
                loc: "http://foo.com",
 
162
                key: testKey,
 
163
        }},
 
164
        loc:       "http://foo.com",
 
165
        expectKey: testKey,
 
166
}, {
 
167
        about: "naked pattern url, no prefix, match with no slash",
 
168
        add: []addPublicKeyArgs{{
 
169
                loc: "http://foo.com",
 
170
                key: testKey,
 
171
        }},
 
172
        loc:       "http://foo.com/",
 
173
        expectKey: testKey,
 
174
}, {
 
175
        about: "port mismatch",
 
176
        add: []addPublicKeyArgs{{
 
177
                loc: "http://foo.com:8080/x",
 
178
                key: testKey,
 
179
        }},
 
180
        loc:            "https://foo.com/x",
 
181
        expectNotFound: true,
 
182
}, {
 
183
        about: "url longer than pattern",
 
184
        add: []addPublicKeyArgs{{
 
185
                loc:    "http://foo.com/x/",
 
186
                key:    testKey,
 
187
                prefix: true,
 
188
        }},
 
189
        loc:       "https://foo.com/x/y/z",
 
190
        expectKey: testKey,
 
191
}, {
 
192
        about: "longer match preferred",
 
193
        add: []addPublicKeyArgs{{
 
194
                loc:    "http://foo.com/x/",
 
195
                key:    newTestKey(0),
 
196
                prefix: true,
 
197
        }, {
 
198
                loc:    "http://foo.com/x/y/",
 
199
                key:    newTestKey(1),
 
200
                prefix: true,
 
201
        }},
 
202
        loc:       "https://foo.com/x/y/z",
 
203
        expectKey: newTestKey(1),
 
204
}, {
 
205
        about: "longer match preferred, with other matches",
 
206
        add: []addPublicKeyArgs{{
 
207
                loc:    "http://foo.com/foo/arble",
 
208
                key:    newTestKey(0),
 
209
                prefix: true,
 
210
        }, {
 
211
                loc:    "http://foo.com/foo/arble/blah/",
 
212
                key:    newTestKey(1),
 
213
                prefix: true,
 
214
        }, {
 
215
                loc:    "http://foo.com/foo/",
 
216
                key:    newTestKey(2),
 
217
                prefix: true,
 
218
        }, {
 
219
                loc:    "http://foo.com/foobieblahbletcharbl",
 
220
                key:    newTestKey(3),
 
221
                prefix: true,
 
222
        }},
 
223
        loc:       "https://foo.com/foo/arble/blah/x",
 
224
        expectKey: newTestKey(1),
 
225
}}
 
226
 
 
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)
 
234
                }
 
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)
 
239
                        continue
 
240
                }
 
241
                c.Assert(err, gc.IsNil)
 
242
                c.Assert(*key, gc.Equals, bakery.PublicKey{test.expectKey})
 
243
        }
 
244
}