-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_test.go
78 lines (65 loc) · 2.67 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
package hashlink
/*
Copyright 2019 Nicholas Krichevsky
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"crypto/sha256"
"encoding/hex"
"hash"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSerialWalkHasher_HashWalk(t *testing.T) {
testWalkHasherInterface(t, func(walker pathWalker, hashConstructor func() hash.Hash) WalkHasher {
return makeSerialHashWalker(walker, hashConstructor)
})
}
func TestParallelWalkhasher_HashWalk(t *testing.T) {
testWalkHasherInterface(t, func(walker pathWalker, hashConstructor func() hash.Hash) WalkHasher {
return makeParallelHashWalker(2, walker, hashConstructor)
})
}
func testWalkHasherInterface(t *testing.T, makeHasher func(walker pathWalker, hashConstructor func() hash.Hash) WalkHasher) {
files := map[string]string{
"a/b": "hello world",
"a/bb/c": "my awesome file!",
"a/bb/d": "unit testing...",
"a/bb/e": "this is the last file I'm testing",
}
hashes := map[string]string{
"a/b": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
"a/bb/c": "6cd8ca076b44600d0c183520c0c30bd6d65995b11a36727dcee777fa8e6f5ad0",
"a/bb/d": "100182cad7531dc4c202e34ee5c666ea284c66196f1bfee24812d11ba1543d86",
"a/bb/e": "d6f542548b05eeef1e909a850dd3f3e383caffdb7e59f059b739584322fee77f",
}
expectedFiles := []string{"a/b", "a/bb/c", "a/bb/d", "a/bb/e"}
// staticWalker defined in walk_test.go
walker := staticWalker{files: files, readers: make(map[string]*closableStringReader, len(files))}
hasher := makeHasher(walker, sha256.New)
walkedHashes, err := hasher.WalkAndHash("a")
assert.Nil(t, err)
hashBuffer := make([]byte, 0)
hashedFiles := make([]string, 0, len(walkedHashes))
for path, hash := range walkedHashes {
hashedFiles = append(hashedFiles, path)
sum := hash.Sum(hashBuffer)
// Assert that the hash matches for the given path
assert.Equal(t, hashes[path], hex.EncodeToString(sum))
}
assert.ElementsMatch(t, expectedFiles, hashedFiles)
// Ensure that we have made ar eader for every single file
assert.Equal(t, len(files), len(walker.readers))
// Assert that every file has been closed exactly once.
for filename, reader := range walker.readers {
assert.Equal(t, 1, reader.closeCount, "file="+filename)
}
}