-
Notifications
You must be signed in to change notification settings - Fork 21
/
did_test.go
764 lines (649 loc) · 23.4 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
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 Params", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Params: []Param{{Name: "foo", Value: "bar"}}}
assert(t, true, 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:456", 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("returns empty string if Param Name does not exist", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Params: []Param{{Name: "", Value: "agent"}}}
assert(t, "", d.String())
})
t.Run("returns name string if Param Value does not exist", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Params: []Param{{Name: "service", Value: ""}}}
assert(t, "did:example:123;service", d.String())
})
t.Run("returns param string with name and value", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Params: []Param{{Name: "service", Value: "agent"}}}
assert(t, "did:example:123;service=agent", d.String())
})
t.Run("includes Param generic", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Params: []Param{{Name: "service", Value: "agent"}}}
assert(t, "did:example:123;service=agent", d.String())
})
t.Run("includes Param method", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Params: []Param{{Name: "foo:bar", Value: "high"}}}
assert(t, "did:example:123;foo:bar=high", d.String())
})
t.Run("includes Param generic and method", func(t *testing.T) {
d := &DID{Method: "example", ID: "123",
Params: []Param{{Name: "service", Value: "agent"}, {Name: "foo:bar", Value: "high"}}}
assert(t, "did:example:123;service=agent;foo:bar=high", 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 Path after Param", func(t *testing.T) {
d := &DID{Method: "example", ID: "123",
Params: []Param{{Name: "service", Value: "agent"}}, Path: "a/b"}
assert(t, "did:example:123;service=agent/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("include Query after Param", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Query: "abc",
Params: []Param{{Name: "service", Value: "agent"}}}
assert(t, "did:example:123;service=agent?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 Param and Path", func(t *testing.T) {
d := &DID{Method: "example", ID: "123", Path: "x/y", Query: "abc",
Params: []Param{{Name: "service", Value: "agent"}}}
assert(t, "did:example:123;service=agent/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 idstring is empty", func(t *testing.T) {
dids := []string{
"did:a::123:456",
"did:a:123::456",
"did:a:123:456:",
"did:a:123:/abc",
"did:a:123:#abc",
}
for _, did := range dids {
_, err := Parse(did)
assert(t, false, err == nil, "Input: %s", did)
}
})
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("returns error if param name is empty", func(t *testing.T) {
_, err := Parse("did:a:123:456;")
assert(t, false, err == nil)
})
t.Run("returns error if Param name has an invalid char", func(t *testing.T) {
_, err := Parse("did:a:123:456;serv&ce")
assert(t, false, err == nil)
})
t.Run("returns error if Param value has an invalid char", func(t *testing.T) {
_, err := Parse("did:a:123:456;service=ag&nt")
assert(t, false, err == nil)
})
t.Run("returns error if Param name has an invalid percent encoded", func(t *testing.T) {
_, err := Parse("did:a:123:456;ser%2ge")
assert(t, false, err == nil)
})
t.Run("returns error if Param does not exist for value", func(t *testing.T) {
_, err := Parse("did:a:123:456;=value")
assert(t, false, err == nil)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract generic param with name and value", func(t *testing.T) {
d, err := Parse("did:a:123:456;service==agent")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "service=agent", d.Params[0].String())
assert(t, "service", d.Params[0].Name)
assert(t, "agent", d.Params[0].Value)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract generic param with name only", func(t *testing.T) {
d, err := Parse("did:a:123:456;service")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "service", d.Params[0].String())
assert(t, "service", d.Params[0].Name)
assert(t, "", d.Params[0].Value)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract generic param with name only and empty param", func(t *testing.T) {
d, err := Parse("did:a:123:456;service=")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "service", d.Params[0].String())
assert(t, "service", d.Params[0].Name)
assert(t, "", d.Params[0].Value)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract method param with name and value", func(t *testing.T) {
d, err := Parse("did:a:123:456;foo:bar=baz")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "foo:bar=baz", d.Params[0].String())
assert(t, "foo:bar", d.Params[0].Name)
assert(t, "baz", d.Params[0].Value)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract method param with name only", func(t *testing.T) {
d, err := Parse("did:a:123:456;foo:bar")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "foo:bar", d.Params[0].String())
assert(t, "foo:bar", d.Params[0].Name)
assert(t, "", d.Params[0].Value)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds with percent encoded chars in param name and value", func(t *testing.T) {
d, err := Parse("did:a:123:456;serv%20ice=val%20ue")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "serv%20ice=val%20ue", d.Params[0].String())
assert(t, "serv%20ice", d.Params[0].Name)
assert(t, "val%20ue", d.Params[0].Value)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract multiple generic params with name only", func(t *testing.T) {
d, err := Parse("did:a:123:456;foo;bar")
assert(t, nil, err)
assert(t, 2, len(d.Params))
assert(t, "foo", d.Params[0].Name)
assert(t, "", d.Params[0].Value)
assert(t, "bar", d.Params[1].Name)
assert(t, "", d.Params[1].Value)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract multiple params with names and values", func(t *testing.T) {
d, err := Parse("did:a:123:456;service=agent;foo:bar=baz")
assert(t, nil, err)
assert(t, 2, len(d.Params))
assert(t, "service", d.Params[0].Name)
assert(t, "agent", d.Params[0].Value)
assert(t, "foo:bar", d.Params[1].Name)
assert(t, "baz", d.Params[1].Value)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract path after generic param", func(t *testing.T) {
d, err := Parse("did:a:123:456;service==value/a/b")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "service=value", d.Params[0].String())
assert(t, "service", d.Params[0].Name)
assert(t, "value", d.Params[0].Value)
segments := d.PathSegments
assert(t, "a", segments[0])
assert(t, "b", segments[1])
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract path after generic param name and no value", func(t *testing.T) {
d, err := Parse("did:a:123:456;service=/a/b")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "service", d.Params[0].String())
assert(t, "service", d.Params[0].Name)
assert(t, "", d.Params[0].Value)
segments := d.PathSegments
assert(t, "a", segments[0])
assert(t, "b", segments[1])
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract query after generic param", func(t *testing.T) {
d, err := Parse("did:a:123:456;service=value?abc")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "service=value", d.Params[0].String())
assert(t, "service", d.Params[0].Name)
assert(t, "value", d.Params[0].Value)
assert(t, "abc", d.Query)
})
// nolint: dupl
// test for params look similar to linter
t.Run("succeeds to extract fragment after generic param", func(t *testing.T) {
d, err := Parse("did:a:123:456;service=value#xyz")
assert(t, nil, err)
assert(t, 1, len(d.Params))
assert(t, "service=value", d.Params[0].String())
assert(t, "service", d.Params[0].Name)
assert(t, "value", d.Params[0].Value)
assert(t, "xyz", d.Fragment)
})
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("returns error if path is empty but there is a slash", func(t *testing.T) {
_, err := Parse("did:a:123:456/")
assert(t, false, err == nil)
})
t.Run("returns error if first path segment is empty", func(t *testing.T) {
_, err := Parse("did:a:123:456//abc")
assert(t, false, err == nil)
})
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 Test_errorf(t *testing.T) {
p := &parser{}
p.errorf(10, "%s,%s", "a", "b")
if p.currentIndex != 10 {
t.Errorf("did not set currentIndex")
}
e := p.err.Error()
if e != "a,b" {
t.Errorf("err message is: '%s' expected: 'a,b'", e)
}
}
func Test_isNotValidParamChar(t *testing.T) {
a := []byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'.', '-', '_', ':'}
for _, c := range a {
assert(t, false, isNotValidParamChar(c), "Input: '%c'", c)
}
a = []byte{'%', '^', '#', ' ', '~', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', '@', '/', '?'}
for _, c := range a {
assert(t, true, isNotValidParamChar(c), "Input: '%c'", c)
}
}
func Test_isNotValidIDChar(t *testing.T) {
a := []byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'.', '-'}
for _, c := range a {
assert(t, false, isNotValidIDChar(c), "Input: '%c'", c)
}
a = []byte{'%', '^', '#', ' ', '_', '~', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '@', '/', '?'}
for _, c := range a {
assert(t, true, isNotValidIDChar(c), "Input: '%c'", c)
}
}
func Test_isNotValidQueryOrFragmentChar(t *testing.T) {
a := []byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'-', '.', '_', '~', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=',
':', '@',
'/', '?'}
for _, c := range a {
assert(t, false, isNotValidQueryOrFragmentChar(c), "Input: '%c'", c)
}
a = []byte{'%', '^', '#', ' '}
for _, c := range a {
assert(t, true, isNotValidQueryOrFragmentChar(c), "Input: '%c'", c)
}
}
func Test_isNotValidPathChar(t *testing.T) {
a := []byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'-', '.', '_', '~', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=',
':', '@'}
for _, c := range a {
assert(t, false, isNotValidPathChar(c), "Input: '%c'", c)
}
a = []byte{'%', '/', '?'}
for _, c := range a {
assert(t, true, isNotValidPathChar(c), "Input: '%c'", c)
}
}
func Test_isNotUnreservedOrSubdelim(t *testing.T) {
a := []byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'-', '.', '_', '~', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '='}
for _, c := range a {
assert(t, false, isNotUnreservedOrSubdelim(c), "Input: '%c'", c)
}
a = []byte{'%', ':', '@', '/', '?'}
for _, c := range a {
assert(t, true, isNotUnreservedOrSubdelim(c), "Input: '%c'", c)
}
}
func Test_isNotHexDigit(t *testing.T) {
a := []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f'}
for _, c := range a {
assert(t, false, isNotHexDigit(c), "Input: '%c'", c)
}
a = []byte{'G', 'g', '%', '\x40', '\x47', '\x60', '\x67'}
for _, c := range a {
assert(t, true, isNotHexDigit(c), "Input: '%c'", c)
}
}
func Test_isNotDigit(t *testing.T) {
a := []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
for _, c := range a {
assert(t, false, isNotDigit(c), "Input: '%c'", c)
}
a = []byte{'A', 'a', '\x29', '\x40', '/'}
for _, c := range a {
assert(t, true, isNotDigit(c), "Input: '%c'", c)
}
}
func Test_isNotAlpha(t *testing.T) {
a := []byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
for _, c := range a {
assert(t, false, isNotAlpha(c), "Input: '%c'", c)
}
a = []byte{'\x40', '\x5B', '\x60', '\x7B', '0', '9', '-', '%'}
for _, c := range a {
assert(t, true, isNotAlpha(c), "Input: '%c'", c)
}
}
// nolint: dupl
// Test_isNotSmallLetter and Test_isNotBigLetter look too similar to the dupl linter, ignore it
func Test_isNotBigLetter(t *testing.T) {
a := []byte{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
for _, c := range a {
assert(t, false, isNotBigLetter(c), "Input: '%c'", c)
}
a = []byte{'\x40', '\x5B', 'a', 'z', '1', '9', '-', '%'}
for _, c := range a {
assert(t, true, isNotBigLetter(c), "Input: '%c'", c)
}
}
// nolint: dupl
// Test_isNotSmallLetter and Test_isNotBigLetter look too similar to the dupl linter, ignore it
func Test_isNotSmallLetter(t *testing.T) {
a := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
for _, c := range a {
assert(t, false, isNotSmallLetter(c), "Input: '%c'", c)
}
a = []byte{'\x60', '\x7B', 'A', 'Z', '1', '9', '-', '%'}
for _, c := range a {
assert(t, true, isNotSmallLetter(c), "Input: '%c'", c)
}
}
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()
}
}