Debian Codegraph @

codec_test.go

raw symbols SCIP
1 // Copyright (C) 2013-2018 by Maxim Bublis <[email protected]>
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 package uuid
23
24 import (
25 "bytes"
26 "regexp"
27 "strings"
28 "testing"
29 )
30
31 // codecTestData holds []byte data for a UUID we commonly use for testing.
32 var codecTestData = []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
33
34 // codecTestUUID is the UUID value corresponding to codecTestData.
35 var codecTestUUID = UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
36
37 func TestFromBytes(t *testing.T) {
38 t.Run("Valid", func(t *testing.T) {
39 got, err := FromBytes(codecTestData)
40 if err != nil {
41 t.Fatal(err)
42 }
43 if got != codecTestUUID {
44 t.Fatalf("FromBytes(%x) = %v, want %v", codecTestData, got, codecTestUUID)
45 }
46 })
47 t.Run("Invalid", func(t *testing.T) {
48 var short [][]byte
49 for i := range codecTestData {
50 short = append(short, codecTestData[:i])
51 }
52 var long [][]byte
53 for i := 1; i < 17; i++ {
54 tmp := append(codecTestData, make([]byte, i)...)
55 long = append(long, tmp)
56 }
57 invalid := append(short, long...)
58 for _, b := range invalid {
59 got, err := FromBytes(b)
60 if err == nil {
61 t.Fatalf("FromBytes(%x): want err != nil, got %v", b, got)
62 }
63 }
64 })
65 }
66
67 func TestFromBytesOrNil(t *testing.T) {
68 t.Run("Invalid", func(t *testing.T) {
69 b := []byte{4, 8, 15, 16, 23, 42}
70 got := FromBytesOrNil(b)
71 if got != Nil {
72 t.Errorf("FromBytesOrNil(%x): got %v, want %v", b, got, Nil)
73 }
74 })
75 t.Run("Valid", func(t *testing.T) {
76 got := FromBytesOrNil(codecTestData)
77 if got != codecTestUUID {
78 t.Errorf("FromBytesOrNil(%x): got %v, want %v", codecTestData, got, codecTestUUID)
79 }
80 })
81
82 }
83
84 type fromStringTest struct {
85 input string
86 variant string
87 }
88
89 // Run runs the FromString test in a subtest of t, named by fst.variant.
90 func (fst fromStringTest) TestFromString(t *testing.T) {
91 t.Run(fst.variant, func(t *testing.T) {
92 got, err := FromString(fst.input)
93 if err != nil {
94 t.Fatalf("FromString(%q): %v", fst.input, err)
95 }
96 if want := codecTestUUID; got != want {
97 t.Fatalf("FromString(%q) = %v, want %v", fst.input, got, want)
98 }
99 })
100 }
101
102 func (fst fromStringTest) TestUnmarshalText(t *testing.T) {
103 t.Run(fst.variant, func(t *testing.T) {
104 var u UUID
105 err := u.UnmarshalText([]byte(fst.input))
106 if err != nil {
107 t.Fatalf("UnmarshalText(%q) (%s): %v", fst.input, fst.variant, err)
108 }
109 if want := codecTestData; !bytes.Equal(u[:], want[:]) {
110 t.Fatalf("UnmarshalText(%q) (%s) = %v, want %v", fst.input, fst.variant, u, want)
111 }
112 })
113 }
114
115 // fromStringTests contains UUID variants that are expected to be parsed
116 // successfully by UnmarshalText / FromString.
117 //
118 // variants must be unique across elements of this slice. Please see the
119 // comment in fuzz.go if you change this slice or add new tests to it.
120 var fromStringTests = []fromStringTest{
121 {
122 input: "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
123 variant: "Canonical",
124 },
125 {
126 input: "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
127 variant: "BracedCanonical",
128 },
129 {
130 input: "{6ba7b8109dad11d180b400c04fd430c8}",
131 variant: "BracedHashlike",
132 },
133 {
134 input: "6ba7b8109dad11d180b400c04fd430c8",
135 variant: "Hashlike",
136 },
137 {
138 input: "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
139 variant: "URNCanonical",
140 },
141 {
142 input: "urn:uuid:6ba7b8109dad11d180b400c04fd430c8",
143 variant: "URNHashlike",
144 },
145 }
146
147 var invalidFromStringInputs = []string{
148 // short
149 "6ba7b810-9dad-11d1-80b4-00c04fd430c",
150 "6ba7b8109dad11d180b400c04fd430c",
151
152 // invalid hex
153 "6ba7b8109dad11d180b400c04fd430q8",
154
155 // long
156 "6ba7b810-9dad-11d1-80b4-00c04fd430c8=",
157 "6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
158 "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}f",
159 "6ba7b810-9dad-11d1-80b4-00c04fd430c800c04fd430c8",
160
161 // malformed in other ways
162 "ba7b8109dad11d180b400c04fd430c8}",
163 "6ba7b8109dad11d180b400c04fd430c86ba7b8109dad11d180b400c04fd430c8",
164 "urn:uuid:{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
165 "uuid:urn:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
166 "uuid:urn:6ba7b8109dad11d180b400c04fd430c8",
167 "6ba7b8109-dad-11d1-80b4-00c04fd430c8",
168 "6ba7b810-9dad1-1d1-80b4-00c04fd430c8",
169 "6ba7b810-9dad-11d18-0b4-00c04fd430c8",
170 "6ba7b810-9dad-11d1-80b40-0c04fd430c8",
171 "6ba7b810+9dad+11d1+80b4+00c04fd430c8",
172 "(6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
173 "{6ba7b810-9dad-11d1-80b4-00c04fd430c8>",
174 "zba7b810-9dad-11d1-80b4-00c04fd430c8",
175 "6ba7b810-9dad11d180b400c04fd430c8",
176 "6ba7b8109dad-11d180b400c04fd430c8",
177 "6ba7b8109dad11d1-80b400c04fd430c8",
178 "6ba7b8109dad11d180b4-00c04fd430c8",
179 }
180
181 func TestFromString(t *testing.T) {
182 t.Run("Valid", func(t *testing.T) {
183 for _, fst := range fromStringTests {
184 fst.TestFromString(t)
185 }
186 })
187 t.Run("Invalid", func(t *testing.T) {
188 for _, s := range invalidFromStringInputs {
189 got, err := FromString(s)
190 if err == nil {
191 t.Errorf("FromString(%q): want err != nil, got %v", s, got)
192 }
193 }
194 })
195 }
196
197 func TestFromStringOrNil(t *testing.T) {
198 t.Run("Invalid", func(t *testing.T) {
199 s := "bad"
200 got := FromStringOrNil(s)
201 if got != Nil {
202 t.Errorf("FromStringOrNil(%q): got %v, want Nil", s, got)
203 }
204 })
205 t.Run("Valid", func(t *testing.T) {
206 s := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
207 got := FromStringOrNil(s)
208 if got != codecTestUUID {
209 t.Errorf("FromStringOrNil(%q): got %v, want %v", s, got, codecTestUUID)
210 }
211 })
212 }
213
214 func TestUnmarshalText(t *testing.T) {
215 t.Run("Valid", func(t *testing.T) {
216 for _, fst := range fromStringTests {
217 fst.TestUnmarshalText(t)
218 }
219 })
220 t.Run("Invalid", func(t *testing.T) {
221 for _, s := range invalidFromStringInputs {
222 var u UUID
223 err := u.UnmarshalText([]byte(s))
224 if err == nil {
225 t.Errorf("FromBytes(%q): want err != nil, got %v", s, u)
226 }
227 }
228 })
229 }
230
231 // Test that UnmarshalText() and Parse() return identical errors
232 func TestUnmarshalTextParseErrors(t *testing.T) {
233 for _, s := range invalidFromStringInputs {
234 var u UUID
235 e1 := u.UnmarshalText([]byte(s))
236 e2 := u.Parse(s)
237 if e1 == nil || e1.Error() != e2.Error() {
238 t.Errorf("%q: errors don't match: UnmarshalText: %v Parse: %v", s, e1, e2)
239 }
240 }
241 }
242
243 func TestMarshalBinary(t *testing.T) {
244 got, err := codecTestUUID.MarshalBinary()
245 if err != nil {
246 t.Fatal(err)
247 }
248 if !bytes.Equal(got, codecTestData) {
249 t.Fatalf("%v.MarshalBinary() = %x, want %x", codecTestUUID, got, codecTestData)
250 }
251 }
252
253 func TestMarshalText(t *testing.T) {
254 want := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
255 got, err := codecTestUUID.MarshalText()
256 if err != nil {
257 t.Fatal(err)
258 }
259 if !bytes.Equal(got, want) {
260 t.Errorf("%v.MarshalText(): got %s, want %s", codecTestUUID, got, want)
261 }
262 }
263
264 func TestDecodePlainWithWrongLength(t *testing.T) {
265 arg := []byte{'4', '2'}
266
267 u := UUID{}
268
269 if u.UnmarshalText(arg) == nil {
270 t.Errorf("%v.UnmarshalText(%q): should return error, but it did not", u, arg)
271 }
272 }
273
274 func TestFromHexChar(t *testing.T) {
275 const hextable = "0123456789abcdef"
276
277 t.Run("Valid", func(t *testing.T) {
278 t.Run("Lower", func(t *testing.T) {
279 for i, c := range []byte(hextable) {
280 x := fromHexChar(c)
281 if int(x) != i {
282 t.Errorf("fromHexChar(%c): got %d want %d", c, x, i)
283 }
284 }
285 })
286 t.Run("Upper", func(t *testing.T) {
287 for i, c := range []byte(strings.ToUpper(hextable)) {
288 x := fromHexChar(c)
289 if int(x) != i {
290 t.Errorf("fromHexChar(%c): got %d want %d", c, x, i)
291 }
292 }
293 })
294 })
295
296 t.Run("Invalid", func(t *testing.T) {
297 skip := make(map[byte]bool)
298 for _, c := range []byte(hextable + strings.ToUpper(hextable)) {
299 skip[c] = true
300 }
301 for i := range 256 {
302 c := byte(i)
303 if !skip[c] {
304 v := fromHexChar(c)
305 if v != 255 {
306 t.Errorf("fromHexChar(%c): got %d want: %d", c, v, 255)
307 }
308 }
309 }
310 })
311 }
312
313 var stringBenchmarkSink string
314
315 func BenchmarkString(b *testing.B) {
316 for range b.N {
317 stringBenchmarkSink = codecTestUUID.String()
318 }
319 }
320
321 func BenchmarkFromBytes(b *testing.B) {
322 for range b.N {
323 _, _ = FromBytes(codecTestData)
324 }
325 }
326
327 func BenchmarkFromString(b *testing.B) {
328 b.Run("canonical", func(b *testing.B) {
329 for range b.N {
330 _, _ = FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
331 }
332 })
333 b.Run("urn", func(b *testing.B) {
334 for range b.N {
335 _, _ = FromString("urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8")
336 }
337 })
338 b.Run("braced", func(b *testing.B) {
339 for range b.N {
340 _, _ = FromString("{6ba7b810-9dad-11d1-80b4-00c04fd430c8}")
341 }
342 })
343 }
344
345 var FromBytesOrNilResult UUID
346
347 func BenchmarkFromBytesOrNil(b *testing.B) {
348 b.Run("valid", func(b *testing.B) {
349 for range b.N {
350 FromBytesOrNilResult = FromBytesOrNil(codecTestData)
351 }
352 })
353
354 b.Run("empty", func(b *testing.B) {
355 for range b.N {
356 FromBytesOrNilResult = FromBytesOrNil([]byte{})
357 }
358 })
359 }
360
361 func BenchmarkUnmarshalText(b *testing.B) {
362 b.Run("canonical", func(b *testing.B) {
363 text := []byte(Must(FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")).String())
364 u := new(UUID)
365 if err := u.UnmarshalText(text); err != nil {
366 b.Fatal(err)
367 }
368 b.ResetTimer()
369 for range b.N {
370 _ = u.UnmarshalText(text)
371 }
372 })
373 b.Run("urn", func(b *testing.B) {
374 text := []byte(Must(FromString("urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8")).String())
375 u := new(UUID)
376 if err := u.UnmarshalText(text); err != nil {
377 b.Fatal(err)
378 }
379 b.ResetTimer()
380 for range b.N {
381 _ = u.UnmarshalText(text)
382 }
383 })
384 b.Run("braced", func(b *testing.B) {
385 text := []byte(Must(FromString("{6ba7b810-9dad-11d1-80b4-00c04fd430c8}")).String())
386 u := new(UUID)
387 if err := u.UnmarshalText(text); err != nil {
388 b.Fatal(err)
389 }
390 b.ResetTimer()
391 for range b.N {
392 _ = u.UnmarshalText(text)
393 }
394 })
395 }
396
397 func BenchmarkMarshalBinary(b *testing.B) {
398 for range b.N {
399 _, _ = codecTestUUID.MarshalBinary()
400 }
401 }
402
403 func BenchmarkMarshalText(b *testing.B) {
404 for range b.N {
405 _, _ = codecTestUUID.MarshalText()
406 }
407 }
408
409 func BenchmarkParseV4(b *testing.B) {
410 const text = "f52a747a-983f-45f7-90b5-e84d70f470dd"
411 for range b.N {
412 var u UUID
413 if err := u.Parse(text); err != nil {
414 b.Fatal(err)
415 }
416 }
417 }
418
419 const uuidPattern = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
420
421 var fromBytesCorpus = [][]byte{
422 {0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8},
423 {4, 8, 15, 16, 23, 42},
424 }
425
426 // FuzzFromBytesFunc is a fuzz testing suite that exercises the FromBytes function
427 func FuzzFromBytesFunc(f *testing.F) {
428 for _, seed := range fromBytesCorpus {
429 f.Add(seed)
430 }
431 uuidRegexp, err := regexp.Compile(uuidPattern)
432 if err != nil {
433 f.Fatal("uuid regexp failed to compile")
434 }
435 f.Fuzz(func(t *testing.T, payload []byte) {
436 u, err := FromBytes(payload)
437 if len(payload) != Size && err == nil {
438 t.Errorf("%v did not result in an error", payload)
439 }
440 if len(payload) == Size && u == Nil {
441 t.Errorf("%v resulted in Nil uuid", payload)
442 }
443 if len(payload) == Size && !uuidRegexp.MatchString(u.String()) {
444 t.Errorf("%v resulted in invalid uuid %s", payload, u.String())
445 }
446 // otherwise, allow to pass if no panic
447 })
448 }
449
450 // FuzzFromBytesOrNilFunc is a fuzz testing suite that exercises the FromBytesOrNil function
451 func FuzzFromBytesOrNilFunc(f *testing.F) {
452 for _, seed := range fromBytesCorpus {
453 f.Add(seed)
454 }
455 uuidRegexp, err := regexp.Compile(uuidPattern)
456 if err != nil {
457 f.Error("uuid regexp failed to compile")
458 }
459 f.Fuzz(func(t *testing.T, payload []byte) {
460 u := FromBytesOrNil(payload)
461 if len(payload) != Size && u != Nil {
462 t.Errorf("%v resulted in non Nil uuid %s", payload, u.String())
463 }
464 if len(payload) == Size && u == Nil {
465 t.Errorf("%v resulted Nil uuid", payload)
466 }
467 if len(payload) == Size && !uuidRegexp.MatchString(u.String()) {
468 t.Errorf("%v resulted in invalid uuid %s", payload, u.String())
469 }
470 // otherwise, allow to pass if no panic
471 })
472 }
473
474 var fromStringCorpus = []string{
475 "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
476 "6BA7B810-9DAD-11D1-80B4-00C04FD430C8",
477 "{6BA7B810-9DAD-11D1-80B4-00C04FD430C8}",
478 "urn:uuid:6BA7B810-9DAD-11D1-80B4-00C04FD430C8",
479 "6BA7B8109DAD11D180B400C04FD430C8",
480 "{6BA7B8109DAD11D180B400C04FD430C8}",
481 "urn:uuid:6BA7B8109DAD11D180B400C04FD430C8",
482 }
483
484 // FuzzFromStringFunc is a fuzz testing suite that exercises the FromString function
485 func FuzzFromStringFunc(f *testing.F) {
486 for _, seed := range fromStringCorpus {
487 f.Add(seed)
488 }
489 uuidRegexp, err := regexp.Compile(uuidPattern)
490 if err != nil {
491 f.Fatal("uuid regexp failed to compile")
492 }
493 f.Fuzz(func(t *testing.T, payload string) {
494 u, err := FromString(payload)
495 if err != nil {
496 if u == Nil {
497 t.Errorf("%s resulted in Nil uuid", payload)
498 }
499 if !uuidRegexp.MatchString(u.String()) {
500 t.Errorf("%s resulted in invalid uuid %s", payload, u.String())
501 }
502 }
503 // otherwise, allow to pass if no panic
504 })
505 }
506
507 // FuzzFromStringOrNil is a fuzz testing suite that exercises the FromStringOrNil function
508 func FuzzFromStringOrNilFunc(f *testing.F) {
509 for _, seed := range fromStringCorpus {
510 f.Add(seed)
511 }
512 uuidRegexp, err := regexp.Compile(uuidPattern)
513 if err != nil {
514 f.Error("uuid regexp failed to compile")
515 }
516 f.Fuzz(func(t *testing.T, payload string) {
517 u := FromStringOrNil(payload)
518 if u != Nil {
519 if !uuidRegexp.MatchString(u.String()) {
520 t.Errorf("%s resulted in invalid uuid %s", payload, u.String())
521 }
522 }
523 // otherwise, allow to pass if no panic
524 })
525 }