Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/pkg/utils/hash_test.go
1560 views
1
package utils
2
3
import (
4
"bytes"
5
"github.com/stretchr/testify/assert"
6
"github.com/stretchr/testify/require"
7
"testing"
8
)
9
10
type hashTest struct {
11
input []byte
12
output map[*HashType]string
13
}
14
15
var hashTestSet = []hashTest{
16
{
17
input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
18
output: map[*HashType]string{
19
MD5: "bf13fc19e5151ac57d4252e0e0f87abe",
20
SHA1: "3ab6543c08a75f292a5ecedac87ec41642d12166",
21
SHA256: "c839e57675862af5c21bd0a15413c3ec579e0d5522dab600bc6c3489b05b8f54",
22
},
23
},
24
// Empty data set
25
{
26
input: []byte{},
27
output: map[*HashType]string{
28
MD5: "d41d8cd98f00b204e9800998ecf8427e",
29
SHA1: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
30
SHA256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
31
},
32
},
33
}
34
35
func TestMultiHasher(t *testing.T) {
36
for _, test := range hashTestSet {
37
mh := NewMultiHasher([]*HashType{MD5, SHA1, SHA256})
38
n, err := CopyWithBuffer(mh, bytes.NewBuffer(test.input))
39
require.NoError(t, err)
40
assert.Len(t, test.input, int(n))
41
hashInfo := mh.GetHashInfo()
42
for k, v := range hashInfo.h {
43
expect, ok := test.output[k]
44
require.True(t, ok, "test output for hash not found")
45
assert.Equal(t, expect, v)
46
}
47
// Test that all are present
48
for k, v := range test.output {
49
expect, ok := hashInfo.h[k]
50
require.True(t, ok, "test output for hash not found")
51
assert.Equal(t, expect, v)
52
}
53
for k, v := range test.output {
54
expect := hashInfo.GetHash(k)
55
require.True(t, len(expect) > 0, "test output for hash not found")
56
assert.Equal(t, expect, v)
57
}
58
expect := hashInfo.GetHash(nil)
59
require.True(t, len(expect) == 0, "unknown type should return empty string")
60
str := hashInfo.String()
61
Log.Info("str=" + str)
62
newHi := FromString(str)
63
assert.Equal(t, newHi.h, hashInfo.h)
64
65
}
66
}
67
68