Skip to content

Commit

Permalink
Fuzz test for compact.RangeNodes (#32)
Browse files Browse the repository at this point in the history
Reading https://github.com/transparency-dev/merkle/blob/main/docs/compact_ranges.md I was inspired to write a simple [fuzz test](https://go.dev/doc/fuzz/) for [compact.RangeNodes](https://pkg.go.dev/github.com/transparency-dev/merkle@v0.0.1/compact#RangeNodes) that checks the property ["a compact range is the minimal set of perfect nodes that cover these, and only these, leaves"](https://github.com/transparency-dev/merkle/blob/main/docs/compact_ranges.md#definition) 

Co-authored-by: Al Cutter <al@google.com>
  • Loading branch information
hickford and AlCutter authored Jul 21, 2022
1 parent 665bf6e commit ca422cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/go_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ jobs:
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v3
- run: go test -race -covermode=atomic -coverprofile=coverage.out ./...
- run: go test -v -race -covermode=atomic -coverprofile=coverage.out ./...
- uses: codecov/codecov-action@v2
33 changes: 33 additions & 0 deletions compact/node_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build go1.18

package compact

import (
"testing"
)

// Test that RangeNodes returns a slice of nodes with contiguous coverage.
// https://github.com/transparency-dev/merkle/blob/main/docs/compact_ranges.md#definition
func FuzzRangeNodes(f *testing.F) {
f.Fuzz(func(t *testing.T, begin, end uint64) {
if begin > end {
return
}
t.Logf("begin=%d, end=%d", begin, end)
nodes := RangeNodes(begin, end, nil)
t.Logf("nodes=%v", nodes)

// Nodes should be contiguous covering begin to end
previousEnd := begin
for _, node := range nodes {
b, e := node.Coverage()
if b != previousEnd {
t.Errorf("got=%d, want=%d", b, previousEnd)
}
previousEnd = e
}
if previousEnd != end {
t.Errorf("got=%d, want=%d", previousEnd, end)
}
})
}

0 comments on commit ca422cf

Please sign in to comment.