Skip to content

Commit 358dbca

Browse files
committed
test(buzhash): fuzz
This commit was moved from ipfs/go-ipfs-chunker@b4e4e73
1 parent 679311d commit 358dbca

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

chunker/buzhash_test.go

+33-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package chunk
22

33
import (
44
"bytes"
5-
"fmt"
65
"io"
76
"testing"
87

@@ -11,33 +10,48 @@ import (
1110

1211
func TestBuzhashChunking(t *testing.T) {
1312
data := make([]byte, 1024*1024*16)
14-
util.NewTimeSeededRand().Read(data)
1513

16-
r := NewBuzhash(bytes.NewReader(data))
14+
chunkCount := 0
15+
rounds := 100
1716

18-
var chunks [][]byte
17+
for i := 0; i < rounds; i++ {
18+
util.NewTimeSeededRand().Read(data)
1919

20-
for {
21-
chunk, err := r.NextBytes()
22-
if err != nil {
23-
if err == io.EOF {
24-
break
20+
r := NewBuzhash(bytes.NewReader(data))
21+
22+
var chunks [][]byte
23+
24+
for {
25+
chunk, err := r.NextBytes()
26+
if err != nil {
27+
if err == io.EOF {
28+
break
29+
}
30+
t.Fatal(err)
2531
}
26-
t.Fatal(err)
32+
33+
chunks = append(chunks, chunk)
2734
}
35+
chunkCount += len(chunks)
2836

29-
chunks = append(chunks, chunk)
30-
}
37+
for i, chunk := range chunks {
38+
if len(chunk) == 0 {
39+
t.Fatalf("chunk %d/%d is empty", i+1, len(chunks))
40+
}
41+
}
3142

32-
t.Logf("average block size: %d\n", len(data)/len(chunks))
43+
for i, chunk := range chunks[:len(chunks)-1] {
44+
if len(chunk) < buzMin {
45+
t.Fatalf("chunk %d/%d is less than the minimum size", i+1, len(chunks))
46+
}
47+
}
3348

34-
unchunked := bytes.Join(chunks, nil)
35-
if !bytes.Equal(unchunked, data) {
36-
fmt.Printf("%d %d\n", len(unchunked), len(data))
37-
//ioutil.WriteFile("./incorrect", unchunked, 0777)
38-
//ioutil.WriteFile("./correct", data, 0777)
39-
t.Fatal("data was chunked incorrectly")
49+
unchunked := bytes.Join(chunks, nil)
50+
if !bytes.Equal(unchunked, data) {
51+
t.Fatal("data was chunked incorrectly")
52+
}
4053
}
54+
t.Logf("average block size: %d\n", len(data)*rounds/chunkCount)
4155
}
4256

4357
func TestBuzhashChunkReuse(t *testing.T) {

0 commit comments

Comments
 (0)