forked from couchbaselabs/cbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_test.go
218 lines (197 loc) · 4.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"bytes"
"encoding/hex"
"errors"
"io/ioutil"
"math/rand"
"os"
"reflect"
"strings"
"sync"
"testing"
)
var once = &sync.Once{}
var randomData []byte
var hashOfRandomData string
func initData() {
randomData = make([]byte, 64*1024)
randomSrc := randomDataMaker{rand.NewSource(1028890720402726901)}
n, err := randomSrc.Read(randomData)
if err != nil {
panic(err)
}
if n != len(randomData) {
panic("Didn't initialize random data properly")
}
sh := getHash()
written, err := sh.Write(randomData)
if err != nil {
panic(err)
}
if written != len(randomData) {
panic("short write")
}
hashOfRandomData = hex.EncodeToString(sh.Sum([]byte{}))
}
func benchHash(h string, b *testing.B) {
once.Do(initData)
globalConfig.Hash = h
b.SetBytes(int64(len(randomData)))
for i := 0; i < b.N; i++ {
sh := getHash()
written, err := sh.Write(randomData)
if err != nil {
b.Fatalf("Error writing data: %v", err)
}
if written != len(randomData) {
b.Fatalf("Didn't write the correct amount of data: %v != %v",
written, len(randomData))
}
}
}
func BenchmarkHashSHA1(b *testing.B) {
benchHash("sha1", b)
}
func BenchmarkHashSHA256(b *testing.B) {
benchHash("sha256", b)
}
func BenchmarkHashSHA512(b *testing.B) {
benchHash("sha512", b)
}
func BenchmarkHashMD5(b *testing.B) {
benchHash("md5", b)
}
func testWithTempDir(t *testing.T, f func(string)) {
once.Do(initData)
t.Parallel()
tmpdir, err := ioutil.TempDir("", "hashtest")
if err != nil {
t.Fatalf("Error getting temp dir: %v", err)
}
defer os.RemoveAll(tmpdir)
f(tmpdir)
}
func validateHashFile(fn string) error {
f, err := os.Open(fn)
if err != nil {
return err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
if !reflect.DeepEqual(randomData, b) {
return errors.New("Didn't read the same data")
}
return nil
}
func TestHashWriterClose(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, "")
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
hr.Close()
})
}
func TestHashWriterDoubleClose(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, "")
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
hr.Close()
hr.Close()
})
}
func TestHashWriterUninitClose(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr := hashRecord{}
hr.Close()
})
}
func TestHashWriterNilClose(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr := (*hashRecord)(nil)
hr.Close()
})
}
func TestHashWriterNoHash(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, "")
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
defer hr.Close()
hr.base = tmpdir
h, l, err := hr.Process(bytes.NewReader(randomData))
if err != nil {
t.Fatalf("Error processing: %v", err)
}
if int(l) != len(randomData) {
t.Fatalf("Processing was short: %v != %v",
l, len(randomData))
}
if h != hashOfRandomData {
t.Fatalf("Expected hash %v, got %v",
hashOfRandomData, h)
}
err = validateHashFile(hashFilename(tmpdir, hashOfRandomData))
if err != nil {
t.Fatalf("Didn't find valid hash: %v", err)
}
})
}
func TestHashWriterGoodHash(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, hashOfRandomData)
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
defer hr.Close()
hr.base = tmpdir
h, l, err := hr.Process(bytes.NewReader(randomData))
if err != nil {
t.Fatalf("Error processing: %v", err)
}
if int(l) != len(randomData) {
t.Fatalf("Processing was short: %v != %v",
l, len(randomData))
}
if h != hashOfRandomData {
t.Fatalf("Expected hash %v, got %v",
hashOfRandomData, h)
}
err = validateHashFile(hashFilename(tmpdir, hashOfRandomData))
if err != nil {
t.Fatalf("Didn't find valid hash: %v", err)
}
})
}
func TestHashWriterWithBadHash(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, "fde65ea0f4a6d1b0eb20c3b6b7e054512d2c45dc")
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
defer hr.Close()
hr.base = tmpdir
_, l, err := hr.Process(bytes.NewReader(randomData))
if err == nil {
t.Fatalf("Expected error processing")
} else {
if !strings.Contains(err.Error(), "Invalid hash") {
t.Fatalf("Expected invalid hash error, got %v", err)
}
}
if int(l) != len(randomData) {
t.Fatalf("Processing was short: %v != %v",
l, len(randomData))
}
err = validateHashFile(hashFilename(tmpdir, hashOfRandomData))
if err == nil {
t.Fatalf("Unexpectedly found valid hash.")
}
})
}