This repository has been archived by the owner on Dec 6, 2024. It is now read-only.
forked from ashwingopalsamy/uuidcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuuidcheck_test.go
290 lines (267 loc) · 6.9 KB
/
uuidcheck_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
package uuidcheck_test
import (
"github.com/ashwingopalsamy/uuidcheck"
"strings"
"testing"
"time"
)
func TestIsValidUUID(t *testing.T) {
tests := []struct {
name string
input string
expect bool
}{
// Valid Cases
{
name: "Lowercase valid UUID",
input: "f47ac10b-58cc-0372-8567-0e02b2c3d479",
expect: true,
},
{
name: "Uppercase valid UUID",
input: "F47AC10B-58CC-0372-8567-0E02B2C3D479",
expect: true,
},
{
name: "Mixed case valid UUID",
input: "f47Ac10B-58Cc-0372-8567-0E02b2C3D479",
expect: true,
},
// Invalid Length
{
name: "Empty string",
input: "",
expect: false,
},
{
name: "Shorter than 36 chars",
input: "f47ac10b-58cc-0372-8567-0e02b2c3d47",
expect: false,
},
{
name: "Longer than 36 chars",
input: "f47ac10b-58cc-0372-8567-0e02b2c3d479abc",
expect: false,
},
// Invalid Hyphens
{
name: "No hyphens at all",
input: "f47ac10b58cc037285670e02b2c3d479",
expect: false,
},
{
name: "Hyphens in wrong places",
input: "f47ac10b-58cc0-372-8567-0e02b2c3d479",
expect: false,
},
{
name: "Only hyphens",
input: "------------------------------------",
expect: false,
},
// Invalid characters
{
name: "Invalid character (g) in UUID",
input: "f47ac10b-58cc-0372-8567-0e02b2c3d47g",
expect: false,
},
{
name: "Invalid character (Z) in UUID",
input: "Z47ac10b-58cc-0372-8567-0e02b2c3d479",
expect: false,
},
// Edge Cases
{
name: "All zeros but valid format",
input: "00000000-0000-0000-0000-000000000000",
expect: true,
},
{
name: "All hyphens in correct positions but invalid",
input: "------------------------------------",
expect: false,
},
{
name: "Almost valid but last char not hex",
input: "f47ac10b-58cc-0372-8567-0e02b2c3d47x",
expect: false,
},
{
name: "Non-hex character in hex position",
input: "f47ac10b-58cc-0372-8567-0e02b2c3d47%",
expect: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := uuidcheck.IsValidUUID(tt.input)
if res != tt.expect {
t.Errorf("IsValidUUID(%q) = %v; want %v", tt.input, res, tt.expect)
}
})
}
}
func TestIsUUIDv7(t *testing.T) {
tests := []struct {
name string
input string
expectV7 bool
expectPanic bool
}{
{
name: "Short string",
input: "abcd",
expectV7: false,
expectPanic: true, // accessing uuid[14] should panic
},
{
name: "Non-UUID string but long enough",
input: "abcdefghijklmnopqrstuvxyz0123456789abcd", // 36 chars but no hyphens
expectV7: false,
expectPanic: false,
},
{
name: "Version 7 UUID all lowercase",
input: "00000000-0000-7000-0000-000000000000",
expectV7: true,
expectPanic: false,
},
{
name: "Version 7 UUID mixed case",
input: "00000000-0000-7FFF-0000-000000000000",
expectV7: true,
expectPanic: false,
},
{
name: "Version 4 UUID",
input: "f47ac10b-58cc-4372-8567-0e02b2c3d479",
expectV7: false,
expectPanic: false,
},
{
name: "Version 1 UUID",
input: "f47ac10b-58cc-1372-8567-0e02b2c3d479",
expectV7: false,
expectPanic: false,
},
{
name: "All zeros but version nibble not '7'",
input: "00000000-0000-4000-0000-000000000000", // version nibble = '4'
expectV7: false,
expectPanic: false,
},
{
name: "Check upper nibble when version = '7'",
input: "00000000-0000-7abc-0000-000000000000",
expectV7: true,
expectPanic: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var got bool
var didPanic bool
func() {
defer func() {
if r := recover(); r != nil {
didPanic = true
}
}()
got = uuidcheck.IsUUIDv7(tt.input)
}()
if tt.expectPanic && !didPanic {
t.Errorf("Expected panic but got none for input: %q", tt.input)
}
if !tt.expectPanic && didPanic {
t.Errorf("Did not expect panic, but got one for input: %q", tt.input)
}
if !tt.expectPanic && got != tt.expectV7 {
t.Errorf("IsUUIDv7(%q) = %v; want %v", tt.input, got, tt.expectV7)
}
})
}
}
func TestUUIDv7ToTimestamp(t *testing.T) {
tests := []struct {
name string
input string
expectErr bool
}{
{
name: "Too short string",
input: "abcd",
expectErr: true,
},
{
name: "Invalid character in first segment (time_low portion)",
input: "zzzzzzzz-0000-7000-0000-000000000000",
expectErr: true,
},
{
name: "Invalid character in second segment",
input: "00000000-gggg-7000-0000-000000000000",
expectErr: true,
},
{
name: "All zeros, valid length and hex - should return no error",
input: "00000000-0000-7000-0000-000000000000",
expectErr: false,
},
{
name: "A valid random v7-like UUID with hex fields, no invalid chars",
input: "017f22e0-79b0-7cc0-98ac-2e7517f37f9f",
expectErr: false,
},
{
name: "Maximal hex fields within allowed ranges",
input: "ffffffff-ffff-7fff-aaaa-ffffffffffff",
expectErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tm, err := uuidcheck.UUIDv7ToTimestamp(tt.input)
if tt.expectErr && err == nil {
t.Errorf("Expected an error but got none. time=%v", tm)
}
if !tt.expectErr && err != nil {
t.Errorf("Did not expect an error, but got one: %v", err)
}
// If it's a valid case, we can make some sanity checks:
if tt.input == "00000000-0000-7000-0000-000000000000" && err == nil {
// According to the logic, this represents timestamp = 0
// Which should map to Unix epoch start
if !tm.Equal(time.UnixMilli(0).UTC()) {
t.Errorf("Expected time to be Unix epoch start, got %v", tm)
}
}
if strings.HasPrefix(tt.input, "017f22e0-79b0-7cc0") && err == nil {
if tm.IsZero() {
t.Error("Expected a non-zero time for this UUID")
}
}
})
}
}
func TestUUIDv7ToTime_Success(t *testing.T) {
// Here, we take a known valid UUIDv7-like string.
// In a real scenario, you'd generate a UUIDv7 using a proper UUIDv7 generator.
uuid := "01939c67-06f5-7faf-ae43-6b450bff06af"
tm, err := uuidcheck.UUIDv7ToTimestamp(uuid)
if err != nil {
t.Fatalf("Failed to convert UUID to time: %v", err)
}
// Log the time result. This demonstrates the conversion.
t.Logf("UUID: %s converts to UTC time: %s", uuid, tm.Format(time.RFC3339Nano))
// You might also add assertions if you know what timestamp to expect,
// but here we just show that it produces a valid time.
if tm.IsZero() {
t.Error("Expected a valid non-zero time")
}
}
// Additional note:
// If we had a known specification for what timestamp a certain UUIDv7 should produce,
// we could assert that exactly. For now, we're primarily testing error conditions and
// general correctness of parsing.