File: hash_fuzz_test.go

package info (click to toggle)
golang-github-prysmaticlabs-gohashtree 0.0.4~beta-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 348 kB
  • sloc: asm: 4,730; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//go:build go1.18
// +build go1.18

package gohashtree_test

import (
	"testing"

	"github.com/prysmaticlabs/gohashtree"
)

func convertRawChunks(raw []byte) [][32]byte {
	var chunks [][32]byte
	for i := 32; i <= len(raw); i += 32 {
		var c [32]byte
		copy(c[:], raw[i-32:i])
		chunks = append(chunks, c)
	}
	return chunks
}

func FuzzHash(f *testing.F) {
	for i := 1; i <= 10; i++ {
		f.Add(make([]byte, 64*i))
	}
	f.Fuzz(func(t *testing.T, chunksRaw []byte) {
		if len(chunksRaw) < 64 || len(chunksRaw)%64 != 0 {
			return // No chunks and odd number of chunks are invalid
		}
		chunks := convertRawChunks(chunksRaw)
		digests := make([][32]byte, len(chunks)/2)
		if err := gohashtree.Hash(digests, chunks); err != nil {
			t.Fatal(err)
		}
	})
}

func FuzzHash_Differential_Minio(f *testing.F) {
	for i := uint(0); i < 128; i++ {
		d := make([]byte, 64)
		for j := 0; j < 64; j++ {
			d[j] = byte(i)
		}
		f.Add(d)
	}
	f.Fuzz(func(t *testing.T, chunksRaw []byte) {
		if len(chunksRaw) < 64 || len(chunksRaw)%64 != 0 {
			return // No chunks and odd number of chunks are invalid
		}
		chunks := convertRawChunks(chunksRaw)
		digests := make([][32]byte, len(chunks)/2)
		if err := gohashtree.Hash(digests, chunks); err != nil {
			t.Fatal(err)
		}
		for i := 64; i <= len(chunksRaw); i += 64 {
			a := OldHash(chunksRaw[i-64 : i])
			b := digests[(i/64)-1]
			if a != b {
				t.Error("minio.Hash() != gohashtree.Hash()")
			}
		}
	})
}