Skip to content

Commit

Permalink
Merge pull request #4 from knei-knurow/feature/add_fuzz_test
Browse files Browse the repository at this point in the history
Add fuzz test for `Create`
  • Loading branch information
bartekpacia committed Apr 18, 2022
2 parents e6a088a + 5fa0e8b commit eefad48
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Test

on:
workflow_dispatch:
push:
tags:
- "v*"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: ncipollo/release-action@v1
8 changes: 3 additions & 5 deletions .github/workflows/go.yaml → .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: Test

on:
push:
branches: [master]
pull_request:
branches: [master]
workflow_dispatch:
pull_request_target:

jobs:
build:
Expand All @@ -16,4 +14,4 @@ jobs:
uses: actions/setup-go@v2

- name: Run tests
run: go test
run: go test -fuzz Fuzz -fuzztime 10s
24 changes: 23 additions & 1 deletion frames_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package frames_test
import (
"bytes"
"fmt"
"math"
"testing"

"github.com/knei-knurow/frames"
Expand All @@ -12,7 +13,6 @@ var testCases = []struct {
inputHeader [2]byte
inputData []byte
expectedChecksum byte
expectedLength int
frame []byte
}{
{
Expand Down Expand Up @@ -188,3 +188,25 @@ func TestRecreate(t *testing.T) {
})
}
}

func FuzzCreate(f *testing.F) {
for _, tc := range testCases {
f.Add(tc.inputData)
}

f.Fuzz(func(t *testing.T, orig []byte) {
if len(orig) > math.MaxUint8 {
return
}

created := frames.Create([2]byte{'L', 'D'}, orig)

if created.LenData() != len(orig) {
t.Errorf("got data length %d, want data %d", created.LenData(), len(orig))
}

if !frames.Verify(created) {
t.Errorf("frame recreation failed")
}
})
}

0 comments on commit eefad48

Please sign in to comment.