Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
test(buzhash): fuzz
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Mar 26, 2020
1 parent ca4d27b commit b4e4e73
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions buzhash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chunk

import (
"bytes"
"fmt"
"io"
"testing"

Expand All @@ -11,33 +10,48 @@ import (

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

r := NewBuzhash(bytes.NewReader(data))
chunkCount := 0
rounds := 100

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

for {
chunk, err := r.NextBytes()
if err != nil {
if err == io.EOF {
break
r := NewBuzhash(bytes.NewReader(data))

var chunks [][]byte

for {
chunk, err := r.NextBytes()
if err != nil {
if err == io.EOF {
break
}
t.Fatal(err)
}
t.Fatal(err)

chunks = append(chunks, chunk)
}
chunkCount += len(chunks)

chunks = append(chunks, chunk)
}
for i, chunk := range chunks {
if len(chunk) == 0 {
t.Fatalf("chunk %d/%d is empty", i+1, len(chunks))
}
}

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

unchunked := bytes.Join(chunks, nil)
if !bytes.Equal(unchunked, data) {
fmt.Printf("%d %d\n", len(unchunked), len(data))
//ioutil.WriteFile("./incorrect", unchunked, 0777)
//ioutil.WriteFile("./correct", data, 0777)
t.Fatal("data was chunked incorrectly")
unchunked := bytes.Join(chunks, nil)
if !bytes.Equal(unchunked, data) {
t.Fatal("data was chunked incorrectly")
}
}
t.Logf("average block size: %d\n", len(data)*rounds/chunkCount)
}

func TestBuzhashChunkReuse(t *testing.T) {
Expand Down

0 comments on commit b4e4e73

Please sign in to comment.