-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmacaroon_test.go
284 lines (263 loc) · 10.4 KB
/
macaroon_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package l402
import (
"bytes"
"encoding/base64"
"errors"
"reflect"
"testing"
macaroon "gopkg.in/macaroon.v2"
)
func TestMarshalMacaroons(t *testing.T) {
mac1, _ := macaroon.New([]byte{1}, []byte{
0, 0, // Version
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Payment Hash
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}, "", macaroon.V2)
mac2, _ := macaroon.New([]byte{1}, []byte{
0, 0, // Version
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Payment Hash
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}, "", macaroon.V2)
tests := map[string]struct {
macaroons macaroon.Slice
expectedMacaroonBase64 string
expectedError error
}{
"zero": {
expectedError: nil,
},
"one": {
macaroons: macaroon.Slice{mac1},
expectedMacaroonBase64: "AgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAGIHqWvcIDGguzG0xeNz7kxTr4IrPg64b0EjRonYD3zkVe",
},
"many": {
macaroons: macaroon.Slice{mac1, mac2},
expectedMacaroonBase64: "AgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAGIHqWvcIDGguzG0xeNz7kxTr4IrPg64b0EjRonYD3zkVeAgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAGIJL//w3j0KDNo5jUh+g47BAyhvsP7eiNYFHlPDw4Od/Z",
},
"defective": {
macaroons: macaroon.Slice{mac1, {}, mac2},
expectedError: errors.New(`failed to marshal macaroon "": bad macaroon version v0`),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
macaroonBase64, err := MarshalMacaroons(test.macaroons...)
if !((err == nil) == (test.expectedError == nil)) || !(errors.Is(err, test.expectedError) || err.Error() == test.expectedError.Error()) {
t.Fatalf("expected: %v but got: %v", test.expectedError, err)
}
if macaroonBase64 != test.expectedMacaroonBase64 {
t.Errorf("expected: %v but got: %v", test.expectedMacaroonBase64, macaroonBase64)
}
})
}
}
func TestUnmarshalMacaroons(t *testing.T) {
mac1, _ := macaroon.New([]byte{1}, []byte{
0, 0, // Version
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Payment Hash
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}, "", macaroon.V2)
mac2, _ := macaroon.New([]byte{1}, []byte{
0, 0, // Version
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Payment Hash
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5}, "", macaroon.V2)
tests := map[string]struct {
macaroonsBase64 string
expectedMacaroons map[Identifier]*macaroon.Macaroon
expectedError error
}{
"no macaroons": { // macaroonsBase64 is guaranteed by authorizationMatcher to be a non empty string
macaroonsBase64: "",
expectedMacaroons: map[Identifier]*macaroon.Macaroon{},
expectedError: nil,
},
"defective macaroon": {
macaroonsBase64: "AGIAJEemVQUTEyNCR0exk7ek90Cg==",
expectedError: base64.CorruptInputError(28),
},
"one invalid macaroon": {
macaroonsBase64: "MDAwZWxvY2F0aW9uIAowMDEwaWRlbnRpZmllciAKMDAyZnNpZ25hdHVyZSCPtT9UwdGWx8khvYJlWY9BhJu6JUG3in2Ef49M+/Oukgo=",
expectedError: ErrUnknownVersion(-1),
},
"one macaroon": {
macaroonsBase64: "AgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAGIHqWvcIDGguzG0xeNz7kxTr4IrPg64b0EjRonYD3zkVe",
expectedMacaroons: map[Identifier]*macaroon.Macaroon{
{
Version: 0,
PaymentHash: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
ID: [32]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4},
}: mac1,
},
},
"many defective macaroons": {
macaroonsBase64: "AgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAGIHqWvcIDGguzG0xeNz7kxTr4IrPg64b0EjRonYD3zkVeAgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAA",
expectedError: errors.New("illegal base64 data at input byte 172"),
},
"many macaroons with comma": {
macaroonsBase64: "AgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAGIHqWvcIDGguzG0xeNz7kxTr4IrPg64b0EjRonYD3zkVe,AgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAGIJL//w3j0KDNo5jUh+g47BAyhvsP7eiNYFHlPDw4Od/Z",
expectedMacaroons: map[Identifier]*macaroon.Macaroon{
{
Version: 0,
PaymentHash: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
ID: [32]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4},
}: mac1,
{
Version: 0,
PaymentHash: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
ID: [32]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
}: mac2,
},
},
"many macaroons": {
macaroonsBase64: "AgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAGIHqWvcIDGguzG0xeNz7kxTr4IrPg64b0EjRonYD3zkVeAgJCAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAGIJL//w3j0KDNo5jUh+g47BAyhvsP7eiNYFHlPDw4Od/Z",
expectedMacaroons: map[Identifier]*macaroon.Macaroon{
{
Version: 0,
PaymentHash: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
ID: [32]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4},
}: mac1,
{
Version: 0,
PaymentHash: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2},
ID: [32]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
}: mac2,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
macaroons, err := UnmarshalMacaroons(test.macaroonsBase64)
if !((err == nil) == (test.expectedError == nil)) || !(errors.Is(err, test.expectedError) || err.Error() == test.expectedError.Error()) {
t.Fatalf("expected: %v but got: %v", test.expectedError, err)
}
if !reflect.DeepEqual(macaroons, test.expectedMacaroons) {
t.Errorf("expected: %v but got: %v", test.expectedMacaroons, macaroons)
}
})
}
}
func TestMarchalIdentifier(t *testing.T) {
tests := map[string]struct {
version uint16
paymentHash Hash
id ID
expectedMacaroonId []byte
expectedErr error
}{
"invalid version": {
version: 1,
expectedErr: ErrUnknownVersion(1),
},
"success": {
paymentHash: [BlockSize]byte{
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
},
id: [BlockSize]byte{
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
},
expectedMacaroonId: []byte{
0, 0, // Version
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Payment Hash
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
identifier := Identifier{
Version: test.version,
PaymentHash: test.paymentHash,
ID: test.id,
}
macaroonID, err := MarchalIdentifier(identifier)
if !errors.Is(err, test.expectedErr) {
t.Fatalf("expected: %v but got: %v", test.expectedErr, err)
}
if !bytes.Equal(macaroonID, test.expectedMacaroonId) {
t.Errorf("expected: %s but got: %s", test.expectedMacaroonId, macaroonID)
}
})
}
}
func TestUnmarshalIdentifier(t *testing.T) {
tests := map[string]struct {
macaroonID []byte
expectedPaymentHash Hash
expectedId ID
expectedErr error
}{
"empty value": {
macaroonID: []byte{},
expectedErr: ErrUnknownVersion(-1),
},
"malformed truncated value": {
macaroonID: []byte{
0, 0, // Version
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Payment Hash
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
},
expectedErr: ErrUnknownVersion(-1),
},
"malformed extended value": {
macaroonID: []byte{
0, 0, // Version
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Payment Hash
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
0, 5,
},
expectedErr: ErrUnknownVersion(-1),
},
"wrong version": {
macaroonID: []byte{
0, 2, // Version
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Payment Hash
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
},
expectedErr: ErrUnknownVersion(2),
},
"success": {
macaroonID: []byte{
0, 0, // Version
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Payment Hash
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Id
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
},
expectedPaymentHash: [BlockSize]byte{
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
},
expectedId: [BlockSize]byte{
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
identifier, err := UnmarshalIdentifier(test.macaroonID)
if !errors.Is(err, test.expectedErr) {
t.Fatalf("expected: %v but got: %v", test.expectedErr, err)
}
if identifier.PaymentHash != test.expectedPaymentHash {
t.Errorf("expected: %s but got: %s", test.expectedPaymentHash, identifier.PaymentHash)
}
if identifier.ID != test.expectedId {
t.Errorf("expected:: %s but got: %s", test.expectedId, identifier.ID)
}
})
}
}