Skip to content

Commit bb31fe5

Browse files
authored
Update fuzz tests to use go fuzz features (#148)
* added two fuzz tests for parser functionality * ran test for v7 generation that was accidentally excluded * added fuzz tests for FromBinary family of functions, moved to the codec_tests.go file * refined logic for FromX fuzz functions * fixed logical errors with fuzz tests * removed harden from some other github actions workflows * fix missing codecov token * Apply suggestions from code review * fixed code review feedback * Update .github/workflows/go.yml
1 parent 56e03f7 commit bb31fe5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+104
-133
lines changed

.github/workflows/codeql.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ jobs:
4040
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
4141

4242
steps:
43-
- name: Harden Runner
44-
uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1
45-
with:
46-
egress-policy: audit
47-
4843
- name: Checkout repository
4944
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
5045

.github/workflows/dependency-review.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ jobs:
1616
dependency-review:
1717
runs-on: ubuntu-latest
1818
steps:
19-
- name: Harden Runner
20-
uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1
21-
with:
22-
egress-policy: audit
23-
2419
- name: 'Checkout Repository'
2520
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
2621
- name: 'Dependency Review'

.github/workflows/go.yml

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ jobs:
1717
env:
1818
GO111MODULE: auto
1919
steps:
20-
21-
- name: Harden Runner
22-
uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1
23-
with:
24-
egress-policy: audit
25-
2620
- name: Build
2721
uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
2822
with:
@@ -38,20 +32,16 @@ jobs:
3832
run: go test ./... -race -coverprofile=coverage.txt -covermode=atomic
3933

4034
- name: Coverage
41-
uses: codecov/codecov-action@125fc84a9a348dbcf27191600683ec096ec9021c # v4.4.1
35+
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4.5.0
36+
env:
37+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
4238

4339
build-legacy:
4440
name: Build + Test Previous Stable
4541
runs-on: ubuntu-latest
4642
env:
4743
GO111MODULE: auto
4844
steps:
49-
50-
- name: Harden Runner
51-
uses: step-security/harden-runner@a4aa98b93cab29d9b1101a6143fb8bce00e2eac4 # v2.7.1
52-
with:
53-
egress-policy: audit
54-
5545
- name: Build
5646
uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1
5747
with:

codec_test.go

Lines changed: 100 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ package uuid
2323

2424
import (
2525
"bytes"
26-
"flag"
27-
"fmt"
28-
"os"
29-
"path/filepath"
26+
"regexp"
3027
"strings"
3128
"testing"
3229
)
@@ -403,28 +400,110 @@ func BenchmarkParseV4(b *testing.B) {
403400
}
404401
}
405402

406-
var seedFuzzCorpus = flag.Bool("seed_fuzz_corpus", false, "seed fuzz test corpus")
403+
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}"
407404

408-
func TestSeedFuzzCorpus(t *testing.T) {
409-
// flag.Parse() is called for us by the test binary.
410-
if !*seedFuzzCorpus {
411-
t.Skip("seeding fuzz test corpus only on demand")
405+
var fromBytesCorpus = [][]byte{
406+
{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8},
407+
{4, 8, 15, 16, 23, 42},
408+
}
409+
410+
// FuzzFromBytesFunc is a fuzz testing suite that exercises the FromBytes function
411+
func FuzzFromBytesFunc(f *testing.F) {
412+
for _, seed := range fromBytesCorpus {
413+
f.Add(seed)
412414
}
413-
corpusDir := filepath.Join(".", "testdata", "corpus")
414-
writeSeedFile := func(name, data string) error {
415-
path := filepath.Join(corpusDir, name)
416-
return os.WriteFile(path, []byte(data), os.ModePerm)
415+
uuidRegexp, err := regexp.Compile(uuidPattern)
416+
if err != nil {
417+
f.Fatal("uuid regexp failed to compile")
417418
}
418-
for _, fst := range fromStringTests {
419-
name := "seed_valid_" + fst.variant
420-
if err := writeSeedFile(name, fst.input); err != nil {
421-
t.Fatal(err)
419+
f.Fuzz(func(t *testing.T, payload []byte) {
420+
u, err := FromBytes(payload)
421+
if len(payload) != Size && err == nil {
422+
t.Errorf("%v did not result in an error", payload)
423+
}
424+
if len(payload) == Size && u == Nil {
425+
t.Errorf("%v resulted in Nil uuid", payload)
422426
}
427+
if len(payload) == Size && !uuidRegexp.MatchString(u.String()) {
428+
t.Errorf("%v resulted in invalid uuid %s", payload, u.String())
429+
}
430+
// otherwise, allow to pass if no panic
431+
})
432+
}
433+
434+
// FuzzFromBytesOrNilFunc is a fuzz testing suite that exercises the FromBytesOrNil function
435+
func FuzzFromBytesOrNilFunc(f *testing.F) {
436+
for _, seed := range fromBytesCorpus {
437+
f.Add(seed)
423438
}
424-
for i, s := range invalidFromStringInputs {
425-
name := fmt.Sprintf("seed_invalid_%d", i)
426-
if err := writeSeedFile(name, s); err != nil {
427-
t.Fatal(err)
439+
uuidRegexp, err := regexp.Compile(uuidPattern)
440+
if err != nil {
441+
f.Error("uuid regexp failed to compile")
442+
}
443+
f.Fuzz(func(t *testing.T, payload []byte) {
444+
u := FromBytesOrNil(payload)
445+
if len(payload) != Size && u != Nil {
446+
t.Errorf("%v resulted in non Nil uuid %s", payload, u.String())
447+
}
448+
if len(payload) == Size && u == Nil {
449+
t.Errorf("%v resulted Nil uuid", payload)
450+
}
451+
if len(payload) == Size && !uuidRegexp.MatchString(u.String()) {
452+
t.Errorf("%v resulted in invalid uuid %s", payload, u.String())
453+
}
454+
// otherwise, allow to pass if no panic
455+
})
456+
}
457+
458+
var fromStringCorpus = []string{
459+
"6ba7b810-9dad-11d1-80b4-00c04fd430c8",
460+
"6BA7B810-9DAD-11D1-80B4-00C04FD430C8",
461+
"{6BA7B810-9DAD-11D1-80B4-00C04FD430C8}",
462+
"urn:uuid:6BA7B810-9DAD-11D1-80B4-00C04FD430C8",
463+
"6BA7B8109DAD11D180B400C04FD430C8",
464+
"{6BA7B8109DAD11D180B400C04FD430C8}",
465+
"urn:uuid:6BA7B8109DAD11D180B400C04FD430C8",
466+
}
467+
468+
// FuzzFromStringFunc is a fuzz testing suite that exercises the FromString function
469+
func FuzzFromStringFunc(f *testing.F) {
470+
for _, seed := range fromStringCorpus {
471+
f.Add(seed)
472+
}
473+
uuidRegexp, err := regexp.Compile(uuidPattern)
474+
if err != nil {
475+
f.Fatal("uuid regexp failed to compile")
476+
}
477+
f.Fuzz(func(t *testing.T, payload string) {
478+
u, err := FromString(payload)
479+
if err != nil {
480+
if u == Nil {
481+
t.Errorf("%s resulted in Nil uuid", payload)
482+
}
483+
if !uuidRegexp.MatchString(u.String()) {
484+
t.Errorf("%s resulted in invalid uuid %s", payload, u.String())
485+
}
428486
}
487+
// otherwise, allow to pass if no panic
488+
})
489+
}
490+
491+
// FuzzFromStringOrNil is a fuzz testing suite that exercises the FromStringOrNil function
492+
func FuzzFromStringOrNilFunc(f *testing.F) {
493+
for _, seed := range fromStringCorpus {
494+
f.Add(seed)
495+
}
496+
uuidRegexp, err := regexp.Compile(uuidPattern)
497+
if err != nil {
498+
f.Error("uuid regexp failed to compile")
429499
}
500+
f.Fuzz(func(t *testing.T, payload string) {
501+
u := FromStringOrNil(payload)
502+
if u != Nil {
503+
if !uuidRegexp.MatchString(u.String()) {
504+
t.Errorf("%s resulted in invalid uuid %s", payload, u.String())
505+
}
506+
}
507+
// otherwise, allow to pass if no panic
508+
})
430509
}

fuzz.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

generator_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ func testNewV7(t *testing.T) {
611611
t.Run("FaultyRand", makeTestNewV7FaultyRand())
612612
t.Run("FaultyRandWithOptions", makeTestNewV7FaultyRandWithOptions())
613613
t.Run("ShortRandomRead", makeTestNewV7ShortRandomRead())
614+
t.Run("ShortRandomReadWithOptions", makeTestNewV7ShortRandomReadWithOptions())
614615
t.Run("KSortable", makeTestNewV7KSortable())
615616
t.Run("ClockSequence", makeTestNewV7ClockSequence())
616617
}

testdata/corpus/1416586f4a34d02bcb506f6107b40df512b9f2f9

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/3b46a7e7b02ec193581e6c9fa2c8a72f50a64e08-1

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/50c54bb75fcfdc488f162bf2f0c6dec6103bfa18-5

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/69c581ab749cbd56be8684d3a58ac2cfab9af0f4-5

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/752bf000e0bff06777dd0d6f0be6353844de678a-3

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/a4483762d4ece8466d82cca5cacd35a0829c4e60-2

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/d0952c45e0c823fc5cc12bcf7d9b877d150ab523-1

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/da39a3ee5e6b4b0d3255bfef95601890afd80709

Whitespace-only changes.

testdata/corpus/e2b84d2065846891f18ae109b12e01d224e1c7c3-4

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/e320d749435115e874f77420e17d0937e07f69f3-2

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/ed132d47d757f6468443a22df8a2a965efb34098-7

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/eeefb01f7bb3c627aedb292c994b20f739ffd613-6

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_0

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_1

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_10

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_11

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_12

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_13

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_14

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_15

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_16

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_17

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_18

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_19

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_2

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_20

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_21

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_22

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_23

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_3

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_4

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_5

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_6

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_7

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_8

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_invalid_9

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_valid_BracedCanonical

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_valid_BracedHashlike

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_valid_Canonical

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_valid_Hashlike

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_valid_URNCanonical

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/corpus/seed_valid_URNHashlike

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)