Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and enable fuzzing #240

Merged
merged 5 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ jobs:
- name: Init fuzzers
run: make fuzz-init

# TODO https://github.com/FerretDB/FerretDB/issues/238
- name: Run fuzzers
run: make fuzz-short
continue-on-error: true # FIXME
env:
GOMAXPROCS: 2 # otherwise, oom-killer kills fuzzer too often
GOMAXPROCS: 1 # otherwise, oom-killer kills fuzzer too often

- name: Collect Linux logs
# if: failure() FIXME
if: failure()
run: |
mkdir -p /tmp/logs
ls -al /var/log/
Expand All @@ -144,7 +144,7 @@ jobs:
sudo chmod 0666 /tmp/logs/*

- name: Upload Linux logs
# if: failure() FIXME
if: failure()
uses: actions/upload-artifact@v2
with:
name: linux-logs
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var arrayTestCases = []testCase{{
nil,
)),
b: testutil.MustParseDumpFile("testdata", "array_all.hex"),
}, {
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}, {
name: "array_fuzz1",
b: testutil.MustParseDumpFile("testdata", "array_fuzz1.hex"),
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ var binaryTestCases = []testCase{{
B: []byte("foo"),
},
b: []byte{0x03, 0x00, 0x00, 0x00, 0x80, 0x66, 0x6f, 0x6f},
}, {
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}}

func TestBinary(t *testing.T) {
Expand Down
29 changes: 13 additions & 16 deletions internal/bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ func assertEqualWithNaN(t testing.TB, expected, actual any) {
assert.Equal(t, expected, actual, "expected: %s\nactual : %s", expected, actual)
}

// lastErr returns the last error in error chain.
func lastErr(err error) error {
for {
e := errors.Unwrap(err)
if e == nil {
return err
}
err = e
}
}

func testBinary(t *testing.T, testCases []testCase, newFunc func() bsontype) {
for _, tc := range testCases {
tc := tc
Expand All @@ -75,14 +86,7 @@ func testBinary(t *testing.T, testCases []testCase, newFunc func() bsontype) {
}

require.Error(t, err)
for {
e := errors.Unwrap(err)
if e == nil {
break
}
err = e
}
require.Equal(t, tc.bErr, err.Error())
require.Equal(t, tc.bErr, lastErr(err).Error())
})

t.Run("MarshalBinary", func(t *testing.T) {
Expand Down Expand Up @@ -207,14 +211,7 @@ func benchmark(b *testing.B, testCases []testCase, newFunc func() bsontype) {
}

require.Error(b, readErr)
for {
e := errors.Unwrap(readErr)
if e == nil {
break
}
readErr = e
}
require.Equal(b, tc.bErr, readErr.Error())
require.Equal(b, tc.bErr, lastErr(readErr).Error())
})
})
}
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var dateTimeTestCases = []testCase{{
name: "9999",
v: pointer.To(DateTime(time.Date(9999, 12, 31, 23, 59, 59, 999000000, time.UTC).Local())),
b: []byte{0xff, 0xdb, 0x1f, 0xd2, 0x77, 0xe6, 0x00, 0x00},
}, {
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}}

func TestDateTime(t *testing.T) {
Expand Down
8 changes: 7 additions & 1 deletion internal/bson/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,13 @@ var (
b: testutil.MustParseDumpFile("testdata", "all.hex"),
}

documentTestCases = []testCase{handshake1, handshake2, handshake3, handshake4, all}
eof = testCase{
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}

documentTestCases = []testCase{handshake1, handshake2, handshake3, handshake4, all, eof}
)

func TestDocument(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/double_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var doubleTestCases = []testCase{{
name: "NaN",
v: pointer.To(Double(math.NaN())),
b: []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x7f},
}, {
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}}

func TestDouble(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/int32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var int32TestCases = []testCase{{
name: "min int32",
v: pointer.To(Int32(math.MinInt32)),
b: []byte{0x00, 0x00, 0x00, 0x80},
}, {
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}}

func TestInt32(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/int64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ var int64TestCases = []testCase{{
name: "min int64",
v: pointer.To(Int64(math.MinInt64)),
b: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80},
}, {
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}}

func TestInt64(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/objectid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ var objectIDTestCases = []testCase{{
name: "normal",
v: pointer.To(ObjectID{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}),
b: []byte{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
}, {
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}}

func TestObjectID(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var regexTestCases = []testCase{{
name: "empty",
v: pointer.To(Regex{Pattern: "", Options: ""}),
b: []byte{0x00, 0x00},
}, {
name: "EOF",
b: []byte{0x00},
bErr: `EOF`,
}}

func TestRegex(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ var stringTestCases = []testCase{{
name: "zero",
v: pointer.To(String("\x00")),
b: []byte{0x02, 0x00, 0x00, 0x00, 0x00, 0x00},
}, {
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}}

func TestString(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions internal/bson/timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var timestampTestCases = []testCase{{
name: "zero",
v: pointer.To(Timestamp(0)),
b: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
}, {
name: "EOF",
b: []byte{0x00},
bErr: `unexpected EOF`,
}}

func TestTimestamp(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion internal/fjson/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ var arrayTestCases = []testCase{{
nil,
)),
j: `[[],{"$b":"Qg==","s":128},true,{"$d":1627378542123},{"$k":[]},{"$f":42.13},42,{"$l":"42"},"foo",null]`,
}, {
name: "EOF",
j: `[`,
jErr: `unexpected EOF`,
}}

func TestArray(t *testing.T) {
t.Parallel()
testJSON(t, arrayTestCases, func() fjsontype { return new(Array) })
}

func FuzzArrayJSON(f *testing.F) {
func FuzzArray(f *testing.F) {
fuzzJSON(f, arrayTestCases, func() fjsontype { return new(Array) })
}

Expand Down
6 changes: 5 additions & 1 deletion internal/fjson/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ var binaryTestCases = []testCase{{
j: `{"$b":"Zm9v","s":128,"foo":"bar"}`,
canonJ: `{"$b":"Zm9v","s":128}`,
jErr: `json: unknown field "foo"`,
}, {
name: "EOF",
j: `{`,
jErr: `unexpected EOF`,
}}

func TestBinary(t *testing.T) {
t.Parallel()
testJSON(t, binaryTestCases, func() fjsontype { return new(Binary) })
}

func FuzzBinaryJSON(f *testing.F) {
func FuzzBinary(f *testing.F) {
fuzzJSON(f, binaryTestCases, func() fjsontype { return new(Binary) })
}

Expand Down
2 changes: 1 addition & 1 deletion internal/fjson/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestBool(t *testing.T) {
testJSON(t, boolTestCases, func() fjsontype { return new(Bool) })
}

func FuzzBoolJSON(f *testing.F) {
func FuzzBool(f *testing.F) {
fuzzJSON(f, boolTestCases, func() fjsontype { return new(Bool) })
}

Expand Down
6 changes: 5 additions & 1 deletion internal/fjson/cstring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ var cstringTestCases = []testCase{{
name: "empty",
v: pointer.To(CString("")),
j: `{"$c":""}`,
}, {
name: "EOF",
j: `{`,
jErr: `unexpected EOF`,
}}

func TestCString(t *testing.T) {
t.Parallel()
testJSON(t, cstringTestCases, func() fjsontype { return new(CString) })
}

func FuzzCStringJSON(f *testing.F) {
func FuzzCString(f *testing.F) {
fuzzJSON(f, cstringTestCases, func() fjsontype { return new(CString) })
}

Expand Down
6 changes: 5 additions & 1 deletion internal/fjson/datetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ var dateTimeTestCases = []testCase{{
name: "9999",
v: pointer.To(DateTime(time.Date(9999, 12, 31, 23, 59, 59, 999000000, time.UTC).Local())),
j: `{"$d":253402300799999}`,
}, {
name: "EOF",
j: `{`,
jErr: `unexpected EOF`,
}}

func TestDateTime(t *testing.T) {
t.Parallel()
testJSON(t, dateTimeTestCases, func() fjsontype { return new(DateTime) })
}

func FuzzDateTimeJSON(f *testing.F) {
func FuzzDateTime(f *testing.F) {
fuzzJSON(f, dateTimeTestCases, func() fjsontype { return new(DateTime) })
}

Expand Down
10 changes: 8 additions & 2 deletions internal/fjson/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,21 @@ var (
`"string":["foo",""],"timestamp":[{"$t":"42"},{"$t":"0"}]}`,
}

documentTestCases = []testCase{handshake1, handshake2, handshake3, handshake4, all}
eof = testCase{
name: "EOF",
j: `[`,
jErr: `unexpected EOF`,
}

documentTestCases = []testCase{handshake1, handshake2, handshake3, handshake4, all, eof}
)

func TestDocument(t *testing.T) {
t.Parallel()
testJSON(t, documentTestCases, func() fjsontype { return new(Document) })
}

func FuzzDocumentJSON(f *testing.F) {
func FuzzDocument(f *testing.F) {
fuzzJSON(f, documentTestCases, func() fjsontype { return new(Document) })
}

Expand Down
6 changes: 5 additions & 1 deletion internal/fjson/double_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ var doubleTestCases = []testCase{{
name: "NaN",
v: pointer.To(Double(math.NaN())),
j: `{"$f":"NaN"}`,
}, {
name: "EOF",
j: `{`,
jErr: `unexpected EOF`,
}}

func TestDouble(t *testing.T) {
t.Parallel()
testJSON(t, doubleTestCases, func() fjsontype { return new(Double) })
}

func FuzzDoubleJSON(f *testing.F) {
func FuzzDouble(f *testing.F) {
fuzzJSON(f, doubleTestCases, func() fjsontype { return new(Double) })
}

Expand Down
Loading