Skip to content

Commit

Permalink
Merge pull request #165 from hashcloak/feature/fuzzing-init
Browse files Browse the repository at this point in the history
  • Loading branch information
onokatio authored Jun 20, 2023
2 parents b9a37ee + 20a54ab commit 93e4913
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
os: ["ubuntu-latest", "macOS-latest"]
go: ["1.17.x", "1.18.x", "1.19.x"]
go: ["1.18.x", "1.19.x"]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ site

# Test binary, built with `go test -c`
*.test
testdata

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
Expand All @@ -29,4 +30,4 @@ data/
# docker genesis documents
testnet/conf/node*/config/genesis.json

plugin/ops/__pycache__
plugin/ops/__pycache__
17 changes: 17 additions & 0 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ docker run --rm \
-w /client \
golang:buster \
/bin/bash -c "GORACE=history_size=7 go test -race"
## or to run fuzzing use below command
# go test -fuzz ./...
```

The above can be de-constructed as follows:
Expand All @@ -24,3 +26,18 @@ The above can be de-constructed as follows:
- `-w /client`: Working directory for the docker image
- `golang:buster`: The docker image to be used
- `/bin/bash -c "GORACE=history_size=7 go test -race"`: The command to run inside the container


### Fuzzing test

To run fuzzing test
```
go test -fuzz=<Test Name here>
```

example: `go test -fuzz=FuzzQueuePushSerial`

- Tests
- FuzzQueue
- FuzzQueuePushSerial
- FuzzQueuePushParalell
67 changes: 66 additions & 1 deletion client/queue_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package client

import (
"testing"
"sync"

"github.com/katzenpost/client/constants"
"github.com/stretchr/testify/assert"
"testing"
)

type foo struct {
Expand Down Expand Up @@ -39,3 +41,66 @@ func TestQueue(t *testing.T) {
_, err = q.Pop()
assert.Error(err)
}

func FuzzQueue(f *testing.F) {
f.Fuzz(func(t *testing.T, _, s string) {
q := new(Queue)
t.Log("Pushing", s)
err := q.Push(foo{s})
if err != nil {
t.Errorf("Push %v %v", s, err)
}
item, err := q.Pop()
if err != nil {
t.Errorf("Pop %v %v %v", s, item, err)
}
})
}

func FuzzQueuePushSerial(f *testing.F) {
f.Fuzz(func(t *testing.T, i int, s string) {
q := new(Queue)
if i > constants.MaxEgressQueueSize {
return
}
for j := 0; j < i; j++ {
t.Log("Pushing", s)
err := q.Push(foo{s})
if err != nil {
t.Errorf("Push %v %v", s, err)
}
}
})
}

func FuzzQueuePushParalell(f *testing.F) {
f.Add(1, "abcde")
f.Add(1, "")
f.Add(1, string([]byte{0x00}))
f.Fuzz(func(t *testing.T, i int, s string) {
q := new(Queue)
if i > constants.MaxEgressQueueSize {
return
}
if i < 0 {
return
}
t.Logf("Pushing '%v' for %v times", s, i)
var wg sync.WaitGroup
for j := 0; j < i; j++ {
t.Log("Pushing", s)
wg.Add(1)
go func(wg *sync.WaitGroup, s string, q *Queue, t *testing.T) {
defer wg.Done()
err := q.Push(foo{s})
if err != nil {
t.Errorf("Push %v %v", s, err)
}
}(&wg, s, q, t)
}
wg.Wait()
if q.len != i {
t.Errorf("Expected len: %v, actual %v", i, q.len)
}
})
}

0 comments on commit 93e4913

Please sign in to comment.