-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_test.go
87 lines (77 loc) · 1.73 KB
/
hash_test.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package opensubtitles
import (
"bytes"
"io"
"testing"
)
// Reference document:
// https://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes
// Empty buffer, all the bytes have a zero value, the hash is then equal to the
// size of the file
func generateEmtpyData() io.ReadSeeker {
data := make([]byte, hashBlockSize)
for i := 0; i < len(data); i++ {
data[i] = 0
}
return bytes.NewReader(data)
}
func generateTooSmallData() io.ReadSeeker {
return bytes.NewReader([]byte{0, 0, 0, 0})
}
func TestHash(t *testing.T) {
tt := []struct {
name string
f func() io.ReadSeeker
expected uint64
expectedErr error
}{
{
name: "valid zeroed buffer with the minimal valid size",
f: generateEmtpyData,
expected: hashBlockSize,
},
{
name: "buffer should be too small",
f: generateTooSmallData,
expectedErr: ErrFileTooSmall,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
r := tc.f()
got, err := Hash(r)
if err != tc.expectedErr {
t.Fatalf("expected err: %s, got %s", tc.expectedErr, err)
}
if got != tc.expected {
t.Fatalf("invalid hash: expected %d, got %d", tc.expected, got)
}
})
}
}
func TestHashString(t *testing.T) {
tt := []struct {
name string
hash uint64
expected string
}{
{
name: "non padded hash",
hash: 10242414353417707026,
expected: "8e245d9679d31e12",
},
{
name: "padded hash",
hash: 72597339223246697,
expected: "0101eae5380a4769",
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
got := HashString(tc.hash)
if got != tc.expected {
t.Fatalf("invalid hash string: expected %s, got %s", tc.expected, got)
}
})
}
}