forked from build-trust/did
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdid_test.go
345 lines (285 loc) · 9.29 KB
/
did_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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package did
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"testing"
)
func TestIsURL(t *testing.T) {
t.Run("returns false if no Path or Fragment", func(t *testing.T) {
d := &DID{Method: "example", ID: "123"}
assert(t, false, d.IsURL())
})
t.Run("returns true if Path", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Path: "a/b"}
assert(t, true, d.IsURL())
})
t.Run("returns true if PathSegements", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", PathSegments: []string{"a", "b"}}
assert(t, true, d.IsURL())
})
t.Run("returns true if Query", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Query: "abc"}
assert(t, true, d.IsURL())
})
t.Run("returns true if Fragment", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Fragment: "00000"}
assert(t, true, d.IsURL())
})
t.Run("returns true if Path and Fragment", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Path: "a/b", Fragment: "00000"}
assert(t, true, d.IsURL())
})
}
func TestString(t *testing.T) {
t.Run("assembles a DID", func(t *testing.T) {
d := &DID{Method: "example", ID: "123"}
assert(t, "did:example:123", d.String())
})
t.Run("assembles a DID from IDStrings", func(t *testing.T) {
d := &DID{Method: "example", IDStrings: []string{"123", "456"}}
assert(t, "did:example:123%3A456", d.String())
})
t.Run("returns empty string if no method", func(t *testing.T) {
d := &DID{ID: "123"}
assert(t, "", d.String())
})
t.Run("returns empty string in no ID or IDStrings", func(t *testing.T) {
d := &DID{Method: "example"}
assert(t, "", d.String())
})
t.Run("includes Path", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Path: "a/b"}
assert(t, "did:example:123/a/b", d.String())
})
t.Run("includes Path assembled from PathSegements", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", PathSegments: []string{"a", "b"}}
assert(t, "did:example:123/a/b", d.String())
})
t.Run("includes Query after IDString", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Query: "abc"}
assert(t, "did:example:123?abc", d.String())
})
t.Run("includes Query after Path", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Path: "x/y", Query: "abc"}
assert(t, "did:example:123/x/y?abc", d.String())
})
t.Run("includes Query after before Fragment", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Fragment: "zyx", Query: "abc"}
assert(t, "did:example:123?abc#zyx", d.String())
})
t.Run("includes Query", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Query: "abc"}
assert(t, "did:example:123?abc", d.String())
})
t.Run("includes Fragment", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Fragment: "00000"}
assert(t, "did:example:123#00000", d.String())
})
t.Run("includes Fragment after Param", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Fragment: "00000"}
assert(t, "did:example:123#00000", d.String())
})
}
func TestParse(t *testing.T) {
t.Run("returns error if input is empty", func(t *testing.T) {
_, err := Parse("")
assert(t, false, err == nil)
})
t.Run("returns error if input length is less than length 7", func(t *testing.T) {
_, err := Parse("did:")
assert(t, false, err == nil)
_, err = Parse("did:a")
assert(t, false, err == nil)
_, err = Parse("did:a:")
assert(t, false, err == nil)
})
t.Run("returns error if input does not have a second : to mark end of method", func(t *testing.T) {
_, err := Parse("did:aaaaaaaaaaa")
assert(t, false, err == nil)
})
t.Run("returns error if method is empty", func(t *testing.T) {
_, err := Parse("did::aaaaaaaaaaa")
assert(t, false, err == nil)
})
t.Run("returns error if input does not begin with did: scheme", func(t *testing.T) {
_, err := Parse("a:12345")
assert(t, false, err == nil)
})
t.Run("returned value is nil if input does not begin with did: scheme", func(t *testing.T) {
d, _ := Parse("a:12345")
assert(t, true, d == nil)
})
t.Run("succeeds if it has did prefix and length is greater than 7", func(t *testing.T) {
d, err := Parse("did:a:1")
assert(t, nil, err)
assert(t, true, d != nil)
})
t.Run("succeeds to extract method", func(t *testing.T) {
d, err := Parse("did:a:1")
assert(t, nil, err)
assert(t, "a", d.Method)
d, err = Parse("did:abcdef:11111")
assert(t, nil, err)
assert(t, "abcdef", d.Method)
})
t.Run("returns error if method has any other char than 0-9 or a-z", func(t *testing.T) {
_, err := Parse("did:aA:1")
assert(t, false, err == nil)
_, err = Parse("did:aa-aa:1")
assert(t, false, err == nil)
})
t.Run("succeeds to extract id", func(t *testing.T) {
d, err := Parse("did:a:1")
assert(t, nil, err)
assert(t, "1", d.ID)
})
t.Run("succeeds to extract id parts", func(t *testing.T) {
d, err := Parse("did:a:123:456")
assert(t, nil, err)
parts := d.IDStrings
assert(t, "123", parts[0])
assert(t, "456", parts[1])
})
t.Run("returns error if ID has an invalid char", func(t *testing.T) {
_, err := Parse("did:a:1&&111")
assert(t, false, err == nil)
})
t.Run("succeeds to extract path", func(t *testing.T) {
d, err := Parse("did:a:123:456/someService")
assert(t, nil, err)
assert(t, "someService", d.Path)
})
t.Run("succeeds to extract path segements", func(t *testing.T) {
d, err := Parse("did:a:123:456/a/b")
assert(t, nil, err)
segments := d.PathSegments
assert(t, "a", segments[0])
assert(t, "b", segments[1])
})
t.Run("succeeds with percent encoded chars in path", func(t *testing.T) {
d, err := Parse("did:a:123:456/a/%20a")
assert(t, nil, err)
assert(t, "a/%20a", d.Path)
})
t.Run("returns error if % in path is not followed by 2 hex chars", func(t *testing.T) {
dids := []string{
"did:a:123:456/%",
"did:a:123:456/%a",
"did:a:123:456/%!*",
"did:a:123:456/%A!",
"did:xyz:pqr#%A!",
"did:a:123:456/%A%",
}
for _, did := range dids {
_, err := Parse(did)
assert(t, false, err == nil, "Input: %s", did)
}
})
t.Run("does not fail if second path segment is empty", func(t *testing.T) {
_, err := Parse("did:a:123:456/abc//pqr")
assert(t, nil, err)
})
t.Run("returns error if path has invalid char", func(t *testing.T) {
_, err := Parse("did:a:123:456/ssss^sss")
assert(t, false, err == nil)
})
t.Run("does not fail if path has atleast one segment and a trailing slash", func(t *testing.T) {
_, err := Parse("did:a:123:456/a/b/")
assert(t, nil, err)
})
t.Run("succeeds to extract query after idstring", func(t *testing.T) {
d, err := Parse("did:a:123?abc")
assert(t, nil, err)
assert(t, "a", d.Method)
assert(t, "123", d.ID)
assert(t, "abc", d.Query)
})
t.Run("succeeds to extract query after path", func(t *testing.T) {
d, err := Parse("did:a:123/a/b/c?abc")
assert(t, nil, err)
assert(t, "a", d.Method)
assert(t, "123", d.ID)
assert(t, "a/b/c", d.Path)
assert(t, "abc", d.Query)
})
t.Run("succeeds to extract fragment after query", func(t *testing.T) {
d, err := Parse("did:a:123?abc#xyz")
assert(t, nil, err)
assert(t, "abc", d.Query)
assert(t, "xyz", d.Fragment)
})
t.Run("succeeds with percent encoded chars in query", func(t *testing.T) {
d, err := Parse("did:a:123?ab%20c")
assert(t, nil, err)
assert(t, "ab%20c", d.Query)
})
t.Run("returns error if % in query is not followed by 2 hex chars", func(t *testing.T) {
dids := []string{
"did:a:123:456?%",
"did:a:123:456?%a",
"did:a:123:456?%!*",
"did:a:123:456?%A!",
"did:xyz:pqr?%A!",
"did:a:123:456?%A%",
}
for _, did := range dids {
_, err := Parse(did)
assert(t, false, err == nil, "Input: %s", did)
}
})
t.Run("returns error if query has invalid char", func(t *testing.T) {
_, err := Parse("did:a:123:456?ssss^sss")
assert(t, false, err == nil)
})
t.Run("succeeds to extract fragment", func(t *testing.T) {
d, err := Parse("did:a:123:456#keys-1")
assert(t, nil, err)
assert(t, "keys-1", d.Fragment)
})
t.Run("succeeds with percent encoded chars in fragment", func(t *testing.T) {
d, err := Parse("did:a:123:456#aaaaaa%20a")
assert(t, nil, err)
assert(t, "aaaaaa%20a", d.Fragment)
})
t.Run("returns error if % in fragment is not followed by 2 hex chars", func(t *testing.T) {
dids := []string{
"did:xyz:pqr#%",
"did:xyz:pqr#%a",
"did:xyz:pqr#%!*",
"did:xyz:pqr#%!A",
"did:xyz:pqr#%A!",
"did:xyz:pqr#%A%",
}
for _, did := range dids {
_, err := Parse(did)
assert(t, false, err == nil, "Input: %s", did)
}
})
t.Run("fails if fragment has invalid char", func(t *testing.T) {
_, err := Parse("did:a:123:456#ssss^sss")
assert(t, false, err == nil)
})
}
func assert(t *testing.T, expected interface{}, actual interface{}, args ...interface{}) {
if !reflect.DeepEqual(expected, actual) {
argsLength := len(args)
var message string
// if only one arg is present, treat it as the message
if argsLength == 1 {
message = args[0].(string)
}
// if more than one arg is present, treat it as format, args (like Printf)
if argsLength > 1 {
message = fmt.Sprintf(args[0].(string), args[1:]...)
}
// is message is not empty add some spacing
if message != "" {
message = "\t" + message + "\n\n"
}
_, file, line, _ := runtime.Caller(1)
fmt.Printf("%s:%d:\n\tExpected: %#v\n\tActual: %#v\n%s", filepath.Base(file), line, expected, actual, message)
t.FailNow()
}
}