Skip to content

Commit

Permalink
feat: add fuzzing for statement parser (#250)
Browse files Browse the repository at this point in the history
Co-authored-by: Knut Olav Løite <[email protected]>
  • Loading branch information
egonelbre and olavloite authored Jun 17, 2024
1 parent ec15462 commit 2e74813
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions statement_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,16 @@ SELECT SchoolID FROM Roster`,
}
}

func FuzzRemoveCommentsAndTrim(f *testing.F) {
for _, sample := range fuzzQuerySamples {
f.Add(sample)
}

f.Fuzz(func(t *testing.T, input string) {
_, _ = removeCommentsAndTrim(input)
})
}

func TestFindParams(t *testing.T) {
tests := []struct {
input string
Expand Down Expand Up @@ -672,6 +682,16 @@ func TestFindParams(t *testing.T) {
}
}

func FuzzFindParams(f *testing.F) {
for _, sample := range fuzzQuerySamples {
f.Add(sample)
}

f.Fuzz(func(t *testing.T, input string) {
_, _, _ = parseParameters(input)
})
}

// note: isDDL function does not check validity of statement
// just that the statement begins with a DDL instruction.
// Other checking performed by database.
Expand Down Expand Up @@ -834,6 +854,16 @@ func TestIsDdl(t *testing.T) {
}
}

func FuzzIsDdl(f *testing.F) {
for _, sample := range fuzzQuerySamples {
f.Add(sample)
}

f.Fuzz(func(t *testing.T, input string) {
_, _ = isDDL(input)
})
}

func TestParseClientSideStatement(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -927,6 +957,16 @@ func TestParseClientSideStatement(t *testing.T) {
}
}

func FuzzParseClientSideStatement(f *testing.F) {
for _, sample := range fuzzQuerySamples {
f.Add(sample)
}

f.Fuzz(func(t *testing.T, input string) {
_, _ = parseClientSideStatement(&conn{}, input)
})
}

func TestRemoveCommentsAndTrim_Errors(t *testing.T) {
_, err := removeCommentsAndTrim("SELECT 'Hello World FROM SomeTable")
if g, w := spanner.ErrCode(err), codes.InvalidArgument; g != w {
Expand All @@ -948,3 +988,17 @@ func TestFindParams_Errors(t *testing.T) {
t.Errorf("error code mismatch\nGot: %v\nWant: %v\n", g, w)
}
}

var fuzzQuerySamples = []string{"", "SELECT 1;", "RUN BATCH", "ABORT BATCH", "Show variable Retry_Aborts_Internally", "@{JOIN_METHOD=HASH_JOIN SELECT * FROM PersonsTable"}

func init() {
for ddl := range ddlStatements {
fuzzQuerySamples = append(fuzzQuerySamples, ddl)
}
for ddl := range selectStatements {
fuzzQuerySamples = append(fuzzQuerySamples, ddl)
}
for ddl := range dmlStatements {
fuzzQuerySamples = append(fuzzQuerySamples, ddl)
}
}

0 comments on commit 2e74813

Please sign in to comment.