-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuidkey_test.go
364 lines (320 loc) · 9.26 KB
/
uuidkey_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
//go:build !integration
// +build !integration
package uuidkey
import (
"bytes"
"reflect"
"testing"
// test-only dependencies
gofrsUUID "github.com/gofrs/uuid"
googleUUID "github.com/google/uuid"
)
// test-only dependency
func TestValid(t *testing.T) {
validKeys := []Key{
"38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X",
"0000000-0000000-0000000-0000000",
"ZZZZZZZ-ZZZZZZZ-ZZZZZZZ-ZZZZZZZ",
}
invalidKeys := []Key{
"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
}
for _, k := range validKeys {
if !k.Valid() {
t.Errorf("Validate() incorrectly reported valid key as invalid: %s", k)
}
}
for _, k := range invalidKeys {
if k.Valid() {
t.Errorf("Validate() incorrectly reported invalid key as valid: %s", k)
}
}
}
func TestParse(t *testing.T) {
validKey := "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X"
k, err := Parse(validKey)
if err != nil {
t.Errorf("Parse() returned an error for valid key: %v", err)
}
if k != Key(validKey) {
t.Errorf("Parse() returned incorrect key. Got %s, want %s", k, validKey)
}
invalidKey := "invalid-key"
_, err = Parse(invalidKey)
if err == nil {
t.Errorf("Parse() did not return an error for invalid key")
}
}
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)
}
// 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")
}
}
func TestUUIDString(t *testing.T) {
validKey := Key("38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X")
expectedUUID := "d1756360-5da0-40df-9926-a76abff5601d"
uuidStr, err := validKey.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")
}
}
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.Valid() {
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)
}
}
}
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.Valid() {
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)
}
}
}
func TestKeyString(t *testing.T) {
key := Key("38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X")
expected := "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0X"
result := key.String()
if result != expected {
t.Errorf("Key.String() returned incorrect value. Got %s, want %s", result, expected)
}
}
func TestEncodeBytes(t *testing.T) {
tests := []struct {
name string
input [16]byte
want Key
wantErr bool
}{
{
name: "Valid UUID",
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",
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",
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,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := EncodeBytes(tt.input)
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)
}
})
}
}
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.Valid() {
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)
}
}
}
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.Valid() {
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)
}
}
}
func TestKeyBytes(t *testing.T) {
tests := []struct {
name string
key Key
want [16]byte
wantErr bool
}{
{
name: "Valid Key",
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",
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",
key: "INVALID-KEY",
want: [16]byte{},
wantErr: true,
},
// Add this new test case
{
name: "Invalid Key (Too Short)",
key: "38QARV0-1ET0G6Z-2CJD9VA-2ZZAR0",
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")
}
})
}
}