Skip to content

Commit

Permalink
Add fuzz test for FormatDSN (#1444)
Browse files Browse the repository at this point in the history
Run (go 1.18+): go test -fuzz FuzzFormatDSN

Note: invalid host:addr values are currently ignored as they are known
to break (ParseDSN doesn't strictly check address format).
  • Loading branch information
dolmen authored Jun 2, 2023
1 parent 8365b94 commit 65ed3c5
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions dsn_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build go1.18
// +build go1.18

package mysql

import (
"net"
"testing"
)

func FuzzFormatDSN(f *testing.F) {
for _, test := range testDSNs { // See dsn_test.go
f.Add(test.in)
}

f.Fuzz(func(t *testing.T, dsn1 string) {
// Do not waste resources
if len(dsn1) > 1000 {
t.Skip("ignore: too long")
}

cfg1, err := ParseDSN(dsn1)
if err != nil {
t.Skipf("invalid DSN: %v", err)
}

dsn2 := cfg1.FormatDSN()
if dsn2 == dsn1 {
return
}

// Skip known cases of bad config that are not strictly checked by ParseDSN
if _, _, err := net.SplitHostPort(cfg1.Addr); err != nil {
t.Skipf("invalid addr %q: %v", cfg1.Addr, err)
}

cfg2, err := ParseDSN(dsn2)
if err != nil {
t.Fatalf("%q rewritten as %q: %v", dsn1, dsn2, err)
}

dsn3 := cfg2.FormatDSN()
if dsn3 != dsn2 {
t.Errorf("%q rewritten as %q", dsn2, dsn3)
}
})
}

0 comments on commit 65ed3c5

Please sign in to comment.