-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkey_test.go
780 lines (703 loc) · 22.9 KB
/
key_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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//go:build !integration
// +build !integration
package uuidkey
import (
"bytes"
"reflect"
"strings"
"testing"
// test-only dependencies
gofrsUUID "github.com/gofrs/uuid"
googleUUID "github.com/google/uuid"
)
// test-only dependency
// TestValid tests the IsValid method for both with and without hyphens
func TestValid(t *testing.T) {
validKeys := []Key{
// with hyphens
"38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X",
"0000000-0000000-0000000-0000000",
"ZZZZZZZ-ZZZZZZZ-ZZZZZZZ-ZZZZZZZ",
// no hyphens
"38QARV01ET0G6Z2CJD9VA2ZZAR0X",
"0000000000000000000000000000",
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
}
invalidKeys := []Key{
// with hyphens
"38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0", // Too short
"38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0XX", // Too long
"38qarv0-1ET0G6Z-2CJD9VA-2ZZAR0X", // Lowercase
"38QARV0 1ET0G6Z 2CJD9VA 2ZZAR0X", // Spaces instead of hyphens
"38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0!", // Invalid character
"38QARV0-1ET0G6-2CJD9VA-2ZZAR0X", // Part too short
"38QARV0-1ET0G6Z-2CJD9VAA-2ZZAR0", // Third part too long
"38QARV0-LET0G6Z-2CJD9VA-2ZZAROX", // Contains non-crockford base32 characters
// no hyphens
"38QARV01ET0G6Z2CJD9VA2ZZAR0", // Too short
"38QARV01ET0G6Z2CJD9VA2ZZAR0XL", // Too long
"38qarv01et0g6z2cjd9va2zzar0", // Lowercase
"38QARV01ET0G6Z2CJD9VA2ZZAR0!", // Invalid character
"38QARV01ET0G6Z2CJD9VA2ZZAR0XL", // Too long
"38QARV0LET0G6Z2CJD9VA2ZZAR0X", // Contains non-crockford base32 characters
// Additional hyphen cases
"38QARV0-1ET0G6Z2CJD9VA2ZZAR0X", // Only first hyphen
// Additional non-hyphen cases
"38QARV01ET0G6Z-2CJD9VA2ZZAR0X", // Unexpected hyphen in middle
"38QARV01ET0G6Z2CJD9VA-2ZZAR0X", // Unexpected hyphen near end
"38QARV01ET0G6Z2CJD9VA2ZZAR0X-", // Unexpected hyphen at end
// Additional length validation cases
"38QARV0-1ET0G6-2CJD9VA-2ZZAR0X", // Second part too short (6 chars)
"38QARV0-1ET0G6ZZ-2CJD9VA-2ZZAR0X", // Second part too long (8 chars)
"38QAR-1ET0G6Z-2CJD9VA-2ZZAR0X", // First part too short (5 chars)
"38QARV0Z-1ET0G6Z-2CJD9VA-2ZZAR0X", // First part too long (8 chars)
// Without hyphens length validation
"38QAR01ET0G6Z2CJD9VA2ZZAR0X", // First part too short (5 chars)
"38QARV0Z1ET0G6Z2CJD9VA2ZZAR0X", // First part too long (8 chars)
"38QARV01ET0G2CJD9VA2ZZAR0X", // Second part too short (5 chars)
"38QARV01ET0G6ZZ2CJD9VA2ZZAR0X", // Second part too long (8 chars)
}
for _, k := range validKeys {
if !k.IsValid() {
t.Errorf("Validate() incorrectly reported valid key as invalid: %s", k)
}
}
for _, k := range invalidKeys {
if k.IsValid() {
t.Errorf("Validate() incorrectly reported invalid key as valid: %s", k)
}
}
}
// TestParse tests the Parse method for both with and without hyphens
func TestParse(t *testing.T) {
validKeyWithHyphens := "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X"
k, err := Parse(validKeyWithHyphens)
if err != nil {
t.Errorf("Parse() returned an error for valid key: %v", err)
}
if k != Key(validKeyWithHyphens) {
t.Errorf("Parse() returned incorrect key. Got %s, want %s", k, validKeyWithHyphens)
}
validKeyWithoutHyphens := "38QARV01ET0G6Z2CJD9VA2ZZAR0X"
k, err = Parse(validKeyWithoutHyphens)
if err != nil {
t.Errorf("Parse() returned an error for valid key: %v", err)
}
if k != Key(validKeyWithoutHyphens) {
t.Errorf("Parse() returned incorrect key. Got %s, want %s", k, validKeyWithoutHyphens)
}
invalidKey := "invalid-key"
_, err = Parse(invalidKey)
if err == nil {
t.Errorf("Parse() did not return an error for invalid key")
}
}
// TestEncodeDecode tests the Encode and Decode methods for both with and without hyphens
func TestEncodeDecode(t *testing.T) {
uuidStr := "d1756360-5da0-40df-9926-a76abff5601d"
key, err := Encode(uuidStr)
if err != nil {
t.Fatalf("Encode() returned an unexpected error: %v", err)
}
decodedUUID, err := key.Decode()
if err != nil {
t.Fatalf("Decode() returned an unexpected error: %v", err)
}
if decodedUUID != uuidStr {
t.Errorf("Encode/Decode roundtrip failed. Got %s, want %s", decodedUUID, uuidStr)
}
key, err = Encode(uuidStr, WithoutHyphens)
if err != nil {
t.Fatalf("Encode() returned an unexpected error: %v", err)
}
decodedUUID, err = key.Decode()
if err != nil {
t.Fatalf("Decode() returned an unexpected error: %v", err)
}
if decodedUUID != uuidStr {
t.Errorf("Encode/Decode roundtrip failed. Got %s, want %s", decodedUUID, uuidStr)
}
// Test invalid UUID length
invalidUUID := "invalid-uuid"
_, err = Encode(invalidUUID)
if err == nil {
t.Errorf("Encode() did not return an error for invalid UUID length")
}
}
// TestUUIDString tests the UUID method for both with and without hyphens
func TestUUIDString(t *testing.T) {
validKeyWithHyphens := Key("38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X")
validKeyWithoutHyphens := Key("38QARV01ET0G6Z2CJD9VA2ZZAR0X")
expectedUUID := "d1756360-5da0-40df-9926-a76abff5601d"
uuidStr, err := validKeyWithHyphens.UUID()
if err != nil {
t.Errorf("UUID() returned an error for valid key: %v", err)
}
if uuidStr != expectedUUID {
t.Errorf("UUID() returned incorrect UUID string. Got %s, want %s", uuidStr, expectedUUID)
}
uuidStr, err = validKeyWithoutHyphens.UUID()
if err != nil {
t.Errorf("UUID() returned an error for valid key: %v", err)
}
if uuidStr != expectedUUID {
t.Errorf("UUID() returned incorrect UUID string. Got %s, want %s", uuidStr, expectedUUID)
}
invalidKey := Key("invalid-key")
_, err = invalidKey.UUID()
if err == nil {
t.Errorf("UUID() did not return an error for invalid key")
}
}
// TestGoogleUUIDRoundtrip tests the roundtrip from Google's UUID library to our custom key format and back
func TestGoogleUUIDRoundtrip(t *testing.T) {
for i := 0; i < 1000; i++ { // Test with 1000 random UUIDs
// Generate a random UUID using Google's library
originalUUID := googleUUID.New()
uuidString := originalUUID.String()
// Encode the UUID to our custom key format
key, err := Encode(uuidString)
if err != nil {
t.Errorf("Error encoding UUID %s: %v", uuidString, err)
continue
}
// Ensure the key is valid
if !key.IsValid() {
t.Errorf("Generated key is not valid: %s", key)
continue
}
// Decode the key back to a UUID string
decodedUUIDString, err := key.UUID()
if err != nil {
t.Errorf("Error decoding key %s: %v", key, err)
continue
}
// Parse the decoded UUID string back into a UUID object
decodedUUID, err := googleUUID.Parse(decodedUUIDString)
if err != nil {
t.Errorf("Error parsing decoded UUID string %s: %v", decodedUUIDString, err)
continue
}
// Compare the original and decoded UUIDs
if originalUUID != decodedUUID {
t.Errorf("UUID mismatch. Original: %s, Decoded: %s", originalUUID, decodedUUID)
}
}
}
// TestGoogleUUIDRoundtripWithoutHyphens tests the roundtrip from Google's UUID library to our custom key format and back without hyphens
func TestGoogleUUIDRoundtripWithoutHyphens(t *testing.T) {
for i := 0; i < 1000; i++ { // Test with 1000 random UUIDs
// Generate a random UUID using Google's library
originalUUID := googleUUID.New()
uuidString := originalUUID.String()
// Encode the UUID to our custom key format
key, err := Encode(uuidString, WithoutHyphens)
if err != nil {
t.Errorf("Error encoding UUID %s: %v", uuidString, err)
continue
}
// Ensure the key is valid
if !key.IsValid() {
t.Errorf("Generated key is not valid: %s", key)
continue
}
// Decode the key back to a UUID string
decodedUUIDString, err := key.UUID()
if err != nil {
t.Errorf("Error decoding key %s: %v", key, err)
continue
}
// Parse the decoded UUID string back into a UUID object
decodedUUID, err := googleUUID.Parse(decodedUUIDString)
if err != nil {
t.Errorf("Error parsing decoded UUID string %s: %v", decodedUUIDString, err)
continue
}
// Compare the original and decoded UUIDs
if originalUUID != decodedUUID {
t.Errorf("UUID mismatch. Original: %s, Decoded: %s", originalUUID, decodedUUID)
}
}
}
// TestGofrsUUIDRoundtrip tests the roundtrip from gofrs/uuid library to our custom key format and back
func TestGofrsUUIDRoundtrip(t *testing.T) {
for i := 0; i < 1000; i++ { // Test with 1000 random UUIDs
// Generate a random UUID using gofrs/uuid library
originalUUID, err := gofrsUUID.NewV4()
if err != nil {
t.Fatalf("Failed to generate UUID: %v", err)
}
uuidString := originalUUID.String()
// Encode the UUID to our custom key format
key, err := Encode(uuidString)
if err != nil {
t.Errorf("Error encoding UUID %s: %v", uuidString, err)
continue
}
// Ensure the key is valid
if !key.IsValid() {
t.Errorf("Generated key is not valid: %s", key)
continue
}
// Decode the key back to a UUID string
decodedUUIDString, err := key.UUID()
if err != nil {
t.Errorf("Error decoding key %s: %v", key, err)
continue
}
// Parse the decoded UUID string back into a UUID object
decodedUUID, err := gofrsUUID.FromString(decodedUUIDString)
if err != nil {
t.Errorf("Error parsing decoded UUID string %s: %v", decodedUUIDString, err)
continue
}
// Compare the original and decoded UUIDs
if originalUUID != decodedUUID {
t.Errorf("UUID mismatch. Original: %s, Decoded: %s", originalUUID, decodedUUID)
}
}
}
// TestGofrsUUIDRoundtripWithoutHyphens tests the roundtrip from gofrs/uuid library to our custom key format and back without hyphens
func TestGofrsUUIDRoundtripWithoutHyphens(t *testing.T) {
for i := 0; i < 1000; i++ { // Test with 1000 random UUIDs
// Generate a random UUID using gofrs/uuid library
originalUUID, err := gofrsUUID.NewV4()
if err != nil {
t.Fatalf("Failed to generate UUID: %v", err)
}
uuidString := originalUUID.String()
// Encode the UUID to our custom key format
key, err := Encode(uuidString, WithoutHyphens)
if err != nil {
t.Errorf("Error encoding UUID %s: %v", uuidString, err)
continue
}
// Ensure the key is valid
if !key.IsValid() {
t.Errorf("Generated key is not valid: %s", key)
continue
}
// Decode the key back to a UUID string
decodedUUIDString, err := key.UUID()
if err != nil {
t.Errorf("Error decoding key %s: %v", key, err)
continue
}
// Parse the decoded UUID string back into a UUID object
decodedUUID, err := gofrsUUID.FromString(decodedUUIDString)
if err != nil {
t.Errorf("Error parsing decoded UUID string %s: %v", decodedUUIDString, err)
continue
}
// Compare the original and decoded UUIDs
if originalUUID != decodedUUID {
t.Errorf("UUID mismatch. Original: %s, Decoded: %s", originalUUID, decodedUUID)
}
}
}
// TestKeyString tests the String method for both with and without hyphens
func TestKeyString(t *testing.T) {
keyWithHyphens := Key("38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X")
keyWithoutHyphens := Key("38QARV01ET0G6Z2CJD9VA2ZZAR0X")
expectedWithHyphens := "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X"
expectedWithoutHyphens := "38QARV01ET0G6Z2CJD9VA2ZZAR0X"
result := keyWithHyphens.String()
if result != expectedWithHyphens {
t.Errorf("Key.String() returned incorrect value. Got %s, want %s", result, expectedWithHyphens)
}
result = keyWithoutHyphens.String()
if result != expectedWithoutHyphens {
t.Errorf("Key.String() returned incorrect value. Got %s, want %s", result, expectedWithoutHyphens)
}
}
// TestEncodeBytes tests the EncodeBytes method for both with and without hyphens
func TestEncodeBytes(t *testing.T) {
tests := []struct {
name string
input [16]byte
want Key
wantErr bool
}{
{
name: "Valid UUID with hyphens",
input: [16]byte{0xd1, 0x75, 0x63, 0x60, 0x5d, 0xa0, 0x40, 0xdf, 0x99, 0x26, 0xa7, 0x6a, 0xbf, 0xf5, 0x60, 0x1d},
want: "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X",
wantErr: false,
},
{
name: "All zeros with hyphens",
input: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
want: "0000000-0000000-0000000-0000000",
wantErr: false,
},
{
name: "All ones with hyphens",
input: [16]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
want: "3ZZZZZZ-3ZZZZZZ-3ZZZZZZ-3ZZZZZZ",
wantErr: false,
},
{
name: "Valid UUID without hyphens",
input: [16]byte{0xd1, 0x75, 0x63, 0x60, 0x5d, 0xa0, 0x40, 0xdf, 0x99, 0x26, 0xa7, 0x6a, 0xbf, 0xf5, 0x60, 0x1d},
want: "38QARV01ET0G6Z2CJD9VA2ZZAR0X",
wantErr: false,
},
{
name: "All zeros without hyphens",
input: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
want: "0000000000000000000000000000",
wantErr: false,
},
{
name: "All ones without hyphens",
input: [16]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
want: "3ZZZZZZ3ZZZZZZ3ZZZZZZ3ZZZZZZ",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var opts []Option
if strings.Contains(tt.name, "without hyphens") {
opts = append(opts, WithoutHyphens)
}
got, err := EncodeBytes(tt.input, opts...)
if (err != nil) != tt.wantErr {
t.Errorf("EncodeBytes() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("EncodeBytes() = %v, want %v", got, tt.want)
}
})
}
}
// TestEncodeBytesRoundTripGoogle tests the roundtrip from Google's UUID library to our custom key format and back
func TestEncodeBytesRoundTripGoogle(t *testing.T) {
for i := 0; i < 1000; i++ { // Test with 1000 random UUIDs
// Generate a random UUID using Google's library
originalUUID := googleUUID.New()
var uuidBytes [16]byte
copy(uuidBytes[:], originalUUID[:])
// Encode the UUID bytes to our custom key format
key, err := EncodeBytes(uuidBytes)
if err != nil {
t.Errorf("Error encoding UUID bytes %v: %v", uuidBytes, err)
continue
}
// Ensure the key is valid
if !key.IsValid() {
t.Errorf("Generated key is not valid: %s", key)
continue
}
// Convert the key back to bytes
decodedBytes, err := key.Bytes()
if err != nil {
t.Errorf("Error converting key %s to bytes: %v", key, err)
continue
}
// Compare the original and decoded UUID bytes
if !bytes.Equal(uuidBytes[:], decodedBytes[:]) {
t.Errorf("UUID bytes mismatch. Original: %v, Decoded: %v", uuidBytes, decodedBytes)
}
}
}
// TestEncodeBytesRoundTripGoogleWithoutHyphens tests the roundtrip from Google's UUID library to our custom key format and back without hyphens
func TestEncodeBytesRoundTripGoogleWithoutHyphens(t *testing.T) {
for i := 0; i < 1000; i++ { // Test with 1000 random UUIDs
// Generate a random UUID using Google's library
originalUUID := googleUUID.New()
var uuidBytes [16]byte
copy(uuidBytes[:], originalUUID[:])
// Encode the UUID bytes to our custom key format
key, err := EncodeBytes(uuidBytes, WithoutHyphens)
if err != nil {
t.Errorf("Error encoding UUID bytes %v: %v", uuidBytes, err)
continue
}
// Ensure the key is valid
if !key.IsValid() {
t.Errorf("Generated key is not valid: %s", key)
continue
}
// Convert the key back to bytes
decodedBytes, err := key.Bytes()
if err != nil {
t.Errorf("Error converting key %s to bytes: %v", key, err)
continue
}
// Compare the original and decoded UUID bytes
if !bytes.Equal(uuidBytes[:], decodedBytes[:]) {
t.Errorf("UUID bytes mismatch. Original: %v, Decoded: %v", uuidBytes, decodedBytes)
}
}
}
// TestEncodeBytesRoundTripGofrs tests the roundtrip from gofrs/uuid library to our custom key format and back
func TestEncodeBytesRoundTripGofrs(t *testing.T) {
for i := 0; i < 1000; i++ { // Test with 1000 random UUIDs
// Generate a random UUID using gofrs/uuid library
originalUUID, err := gofrsUUID.NewV4()
if err != nil {
t.Fatalf("Failed to generate UUID: %v", err)
}
var uuidBytes [16]byte
copy(uuidBytes[:], originalUUID[:])
// Encode the UUID bytes to our custom key format
key, err := EncodeBytes(uuidBytes)
if err != nil {
t.Errorf("Error encoding UUID bytes %v: %v", uuidBytes, err)
continue
}
// Ensure the key is valid
if !key.IsValid() {
t.Errorf("Generated key is not valid: %s", key)
continue
}
// Convert the key back to bytes
decodedBytes, err := key.Bytes()
if err != nil {
t.Errorf("Error converting key %s to bytes: %v", key, err)
continue
}
// Compare the original and decoded UUID bytes
if !bytes.Equal(uuidBytes[:], decodedBytes[:]) {
t.Errorf("UUID bytes mismatch. Original: %v, Decoded: %v", uuidBytes, decodedBytes)
}
}
}
// TestEncodeBytesRoundTripGofrsWithoutHyphens tests the roundtrip from gofrs/uuid library to our custom key format and back without hyphens
func TestEncodeBytesRoundTripGofrsWithoutHyphens(t *testing.T) {
for i := 0; i < 1000; i++ { // Test with 1000 random UUIDs
// Generate a random UUID using gofrs/uuid library
originalUUID, err := gofrsUUID.NewV4()
if err != nil {
t.Fatalf("Failed to generate UUID: %v", err)
}
var uuidBytes [16]byte
copy(uuidBytes[:], originalUUID[:])
// Encode the UUID bytes to our custom key format
key, err := EncodeBytes(uuidBytes, WithoutHyphens)
if err != nil {
t.Errorf("Error encoding UUID bytes %v: %v", uuidBytes, err)
continue
}
// Ensure the key is valid
if !key.IsValid() {
t.Errorf("Generated key is not valid: %s", key)
continue
}
// Convert the key back to bytes
decodedBytes, err := key.Bytes()
if err != nil {
t.Errorf("Error converting key %s to bytes: %v", key, err)
continue
}
// Compare the original and decoded UUID bytes
if !bytes.Equal(uuidBytes[:], decodedBytes[:]) {
t.Errorf("UUID bytes mismatch. Original: %v, Decoded: %v", uuidBytes, decodedBytes)
}
}
}
// TestKeyBytes tests the Bytes method for both with and without hyphens
func TestKeyBytes(t *testing.T) {
tests := []struct {
name string
key Key
want [16]byte
wantErr bool
}{
{
name: "Valid Key with hyphens",
key: "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X",
want: [16]byte{0xd1, 0x75, 0x63, 0x60, 0x5d, 0xa0, 0x40, 0xdf, 0x99, 0x26, 0xa7, 0x6a, 0xbf, 0xf5, 0x60, 0x1d},
wantErr: false,
},
{
name: "All Zeros with hyphens",
key: "0000000-0000000-0000000-0000000",
want: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
wantErr: false,
},
{
name: "Invalid Key (Too Short) with hyphens",
key: "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0",
want: [16]byte{},
wantErr: true,
},
{
name: "Valid Key without hyphens",
key: "38QARV01ET0G6Z2CJD9VA2ZZAR0X",
want: [16]byte{0xd1, 0x75, 0x63, 0x60, 0x5d, 0xa0, 0x40, 0xdf, 0x99, 0x26, 0xa7, 0x6a, 0xbf, 0xf5, 0x60, 0x1d},
wantErr: false,
},
{
name: "All Zeros without hyphens",
key: "0000000000000000000000000000",
want: [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
wantErr: false,
},
{
name: "Invalid Key (Too Short) without hyphens",
key: "38QARV01ET0G6Z2CJD9VA2ZZAR0",
want: [16]byte{},
wantErr: true,
},
{
name: "Invalid Key with hyphens",
key: "INVALID-KEY",
want: [16]byte{},
wantErr: true,
},
{
name: "Invalid Base32 characters with hyphens",
key: "######-1111111-1111111-1111111", // '#' is invalid in Base32
want: [16]byte{},
wantErr: true,
},
{
name: "Invalid Base32 characters without hyphens",
key: "#######1111111111111111111111",
want: [16]byte{},
wantErr: true,
},
{
name: "Invalid Base32 characters in second group with hyphens",
key: "0000000-#######-1111111-1111111",
want: [16]byte{},
wantErr: true,
},
{
name: "Invalid Base32 characters in third group with hyphens",
key: "0000000-1111111-#######-1111111",
want: [16]byte{},
wantErr: true,
},
{
name: "Invalid Base32 characters in fourth group with hyphens",
key: "0000000-1111111-1111111-#######",
want: [16]byte{},
wantErr: true,
},
{
name: "Invalid Base32 characters in second group without hyphens",
key: "0000000#######1111111111111",
want: [16]byte{},
wantErr: true,
},
{
name: "Invalid Base32 characters in third group without hyphens",
key: "00000001111111#######111111",
want: [16]byte{},
wantErr: true,
},
{
name: "Invalid Base32 characters in fourth group without hyphens",
key: "0000000111111111111111######",
want: [16]byte{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.key.Bytes()
if (err != nil) != tt.wantErr {
t.Errorf("Key.Bytes() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Key.Bytes() = %v, want %v", got, tt.want)
}
if err != nil && !tt.wantErr {
t.Errorf("Key.Bytes() unexpected error: %v", err)
}
if tt.wantErr && err == nil {
t.Errorf("Key.Bytes() expected error, got nil")
}
})
}
}
func TestDecodeErrorMessage(t *testing.T) {
tests := []struct {
name string
key Key
expectedError string
}{
{
name: "Too short key",
key: "ABC",
expectedError: "invalid Key length: expected 28 or 31 characters, got 3",
},
{
name: "Too long key",
key: "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0XX",
expectedError: "invalid Key length: expected 28 or 31 characters, got 32",
},
{
name: "Empty key",
key: "",
expectedError: "invalid Key length: expected 28 or 31 characters, got 0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.key.Decode()
if err == nil {
t.Error("Expected error, got nil")
return
}
if err.Error() != tt.expectedError {
t.Errorf("Expected error message %q, got %q", tt.expectedError, err.Error())
}
})
}
}
func TestKey_Bytes_Errors(t *testing.T) {
tests := []struct {
name string
key Key
wantErr string
}{
{
name: "invalid length - too short",
key: Key("ABC"),
wantErr: "invalid Key length: expected 28 or 31 characters, got 3",
},
{
name: "invalid characters in first group",
key: Key("@#$%^&*-1111111-2222222-3333333"),
wantErr: "failed to decode Key part: crock32.Decode: invalid character @",
},
{
name: "invalid characters in second group",
key: Key("1111111-@#$%^&*-2222222-3333333"),
wantErr: "failed to decode Key part: crock32.Decode: invalid character @",
},
{
name: "invalid characters in third group",
key: Key("1111111-2222222-@#$%^&*-3333333"),
wantErr: "failed to decode Key part: crock32.Decode: invalid character @",
},
{
name: "invalid characters in fourth group",
key: Key("1111111-2222222-3333333-@#$%^&*"),
wantErr: "failed to decode Key part: crock32.Decode: invalid character @",
},
{
name: "invalid characters without hyphens",
key: Key("1111111222222233333333@#$%^&"),
wantErr: "failed to decode Key part: crock32.Decode: invalid character @",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.key.Bytes()
if err == nil {
t.Error("expected error, got nil")
return
}
if err.Error() != tt.wantErr {
t.Errorf("expected error %q, got %q", tt.wantErr, err.Error())
}
})
}
}