Skip to content

Commit

Permalink
Fuzz wire package with recorded data (#1168)
Browse files Browse the repository at this point in the history
Closes #1140.
  • Loading branch information
noisersup committed Sep 26, 2022
1 parent fb6600a commit 9798110
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
96 changes: 96 additions & 0 deletions internal/wire/records_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wire

import (
"bufio"
"errors"
"io"
"io/fs"
"os"
"path/filepath"
)

// loadRecords gets recursively all .bin files from the recordsPath directory,
// parses their content to wire Msgs and returns them as an array of testCase
// structs with headerB and bodyB fields set.
// If no records are found, it returns nil and no error.
func loadRecords(recordsPath string) ([]testCase, error) {
// Load recursively every file path with ".bin" extension from recordsPath directory
var recordFiles []string

err := filepath.WalkDir(recordsPath, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}

if filepath.Ext(entry.Name()) == ".bin" {
recordFiles = append(recordFiles, path)
}

return nil
})

switch {
case os.IsNotExist(err):
return nil, nil
case err != nil:
return nil, err
}

var resMsgs []testCase

// Read every record file, parse their content to wire messages
// and store them in the testCase struct
for _, path := range recordFiles {
f, err := os.Open(path)
if err != nil {
return nil, err
}

defer f.Close()

r := bufio.NewReader(f)

for {
header, body, err := ReadMessage(r)

if errors.Is(err, io.EOF) {
break
}

if err != nil {
return nil, err
}

headBytes, err := header.MarshalBinary()
if err != nil {
return nil, err
}

bodyBytes, err := body.MarshalBinary()
if err != nil {
return nil, err
}

resMsgs = append(resMsgs, testCase{
headerB: headBytes,
bodyB: bodyBytes,
})
}
}

return resMsgs, nil
}
10 changes: 10 additions & 0 deletions internal/wire/wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bufio"
"bytes"
"errors"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -118,6 +119,15 @@ func fuzzMessages(f *testing.F, testCases []testCase) {
f.Add(tc.expectedB)
}

records, err := loadRecords(filepath.Join("..", "..", "records"))
require.NoError(f, err)

f.Logf("%d recorded messages were added to the seed corpus", len(records))

for _, rec := range records {
f.Add(rec.bodyB)
}

f.Fuzz(func(t *testing.T, b []byte) {
t.Parallel()

Expand Down

1 comment on commit 9798110

@vercel
Copy link

@vercel vercel bot commented on 9798110 Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ferret-db – ./

ferret-db-git-main-ferretdb.vercel.app
ferret-db-ferretdb.vercel.app
ferret-db.vercel.app

Please sign in to comment.