]> Cypherpunks.ru repositories - gostls13.git/blob - src/internal/zstd/xxhash_test.go
internal/zstd: new internal package for zstd decompression
[gostls13.git] / src / internal / zstd / xxhash_test.go
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.
4
5 package zstd
6
7 import (
8         "bytes"
9         "os"
10         "os/exec"
11         "strconv"
12         "testing"
13 )
14
15 var xxHashTests = []struct {
16         data string
17         hash uint64
18 }{
19         {
20                 "hello, world",
21                 0xb33a384e6d1b1242,
22         },
23         {
24                 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$",
25                 0x1032d841e824f998,
26         },
27 }
28
29 func TestXXHash(t *testing.T) {
30         var xh xxhash64
31         for i, test := range xxHashTests {
32                 xh.reset()
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)
36                 }
37         }
38 }
39
40 func TestLargeXXHash(t *testing.T) {
41         if testing.Short() {
42                 t.Skip("skipping expensive test in short mode")
43         }
44
45         data := bigData(t)
46         var xh xxhash64
47         xh.reset()
48         i := 0
49         for i < len(data) {
50                 // Write varying amounts to test buffering.
51                 c := i%4094 + 1
52                 if i+c > len(data) {
53                         c = len(data) - i
54                 }
55                 xh.update(data[i : i+c])
56                 i += c
57         }
58
59         got := xh.digest()
60         want := uint64(0xf0dd39fd7e063f82)
61         if got != want {
62                 t.Errorf("got %#x want %#x", got, want)
63         }
64 }
65
66 func FuzzXXHash(f *testing.F) {
67         if _, err := os.Stat("/usr/bin/xxhsum"); err != nil {
68                 f.Skip("skipping because /usr/bin/xxhsum does not exist")
69         }
70
71         for _, test := range xxHashTests {
72                 f.Add([]byte(test.data))
73         }
74         f.Add(bytes.Repeat([]byte("abcdefghijklmnop"), 256))
75         var buf bytes.Buffer
76         for i := 0; i < 256; i++ {
77                 buf.WriteByte(byte(i))
78         }
79         f.Add(bytes.Repeat(buf.Bytes(), 64))
80         f.Add(bigData(f))
81
82         f.Fuzz(func(t *testing.T, b []byte) {
83                 cmd := exec.Command("/usr/bin/xxhsum", "-H64")
84                 cmd.Stdin = bytes.NewReader(b)
85                 var hhsumHash bytes.Buffer
86                 cmd.Stdout = &hhsumHash
87                 if err := cmd.Run(); err != nil {
88                         t.Fatalf("running hhsum failed: %v", err)
89                 }
90                 hhHashBytes := bytes.Fields(bytes.TrimSpace(hhsumHash.Bytes()))[0]
91                 hhHash, err := strconv.ParseUint(string(hhHashBytes), 16, 64)
92                 if err != nil {
93                         t.Fatalf("could not parse hash %q: %v", hhHashBytes, err)
94                 }
95
96                 var xh xxhash64
97                 xh.reset()
98                 xh.update(b)
99                 goHash := xh.digest()
100
101                 if goHash != hhHash {
102                         t.Errorf("Go hash %#x != xxhsum hash %#x", goHash, hhHash)
103                 }
104         })
105 }