-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |