1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 var xxHashTests = []struct {
24 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$",
29 func TestXXHash(t *testing.T) {
31 for i, test := range xxHashTests {
33 xh.update([]byte(test.data))
34 if got := xh.digest(); got != test.hash {
35 t.Errorf("#%d: got %#x want %#x", i, got, test.hash)
40 func TestLargeXXHash(t *testing.T) {
42 t.Skip("skipping expensive test in short mode")
45 data, err := os.ReadFile("../../testdata/Isaac.Newton-Opticks.txt")
54 // Write varying amounts to test buffering.
59 xh.update(data[i : i+c])
64 want := uint64(0xf0dd39fd7e063f82)
66 t.Errorf("got %#x want %#x", got, want)
70 func findXxhsum(t testing.TB) string {
71 xxhsum, err := exec.LookPath("xxhsum")
73 t.Skip("skipping because xxhsum not found")
78 func FuzzXXHash(f *testing.F) {
79 xxhsum := findXxhsum(f)
81 for _, test := range xxHashTests {
82 f.Add([]byte(test.data))
84 f.Add(bytes.Repeat([]byte("abcdefghijklmnop"), 256))
86 for i := 0; i < 256; i++ {
87 buf.WriteByte(byte(i))
89 f.Add(bytes.Repeat(buf.Bytes(), 64))
92 f.Fuzz(func(t *testing.T, b []byte) {
93 cmd := exec.Command(xxhsum, "-H64")
94 cmd.Stdin = bytes.NewReader(b)
95 var hhsumHash bytes.Buffer
96 cmd.Stdout = &hhsumHash
97 if err := cmd.Run(); err != nil {
98 t.Fatalf("running hhsum failed: %v", err)
100 hhHashBytes := bytes.Fields(bytes.TrimSpace(hhsumHash.Bytes()))[0]
101 hhHash, err := strconv.ParseUint(string(hhHashBytes), 16, 64)
103 t.Fatalf("could not parse hash %q: %v", hhHashBytes, err)
109 goHash := xh.digest()
111 if goHash != hhHash {
112 t.Errorf("Go hash %#x != xxhsum hash %#x", goHash, hhHash)