-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_test.go
More file actions
312 lines (271 loc) · 7.7 KB
/
hash_test.go
File metadata and controls
312 lines (271 loc) · 7.7 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package granular
import (
"bytes"
"io"
"path/filepath"
"testing"
"github.com/cespare/xxhash/v2"
"github.com/spf13/afero"
)
// TestHashFile tests the standalone hashFile function
// The main idea is to test if the hashing interacting with the abstractions preserve the results compared to using the hash directly
func TestHashFile(t *testing.T) {
// Create a temporary directory for the test
memFs := afero.NewMemMapFs()
tmpDir, err := afero.TempDir(memFs, "", "hash-test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
// Test cases
testCases := []struct {
name string
content []byte
fileFunc func(string) string
}{
{
name: "Normal file",
content: []byte("test content"),
fileFunc: func(dir string) string {
path := filepath.Join(dir, "normal.txt")
if err := afero.WriteFile(memFs, path, []byte("test content"), 0o644); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
return path
},
},
{
name: "Empty file",
content: []byte{},
fileFunc: func(dir string) string {
path := filepath.Join(dir, "empty.txt")
if err := afero.WriteFile(memFs, path, []byte{}, 0o644); err != nil {
t.Fatalf("Failed to write empty file: %v", err)
}
return path
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
filePath := tc.fileFunc(tmpDir)
// Create two hash instances to compare results
h1 := xxhash.New()
h2 := xxhash.New()
file, err := memFs.Open(filePath)
if err != nil {
t.Fatalf("Failed to open file: %v", err)
}
defer file.Close()
// Hash the file using our hashFile
err = hashFile(file, h1)
// Check error expectation
if err != nil {
t.Errorf("hashFile() error = %v", err)
return
}
// Hash the content directly
h2.Write(tc.content)
// Compare the hashes
if !bytes.Equal(h1.Sum(nil), h2.Sum(nil)) {
t.Errorf("hashFile() produced different hash than direct hashing")
}
})
}
}
// TestHashFile tests failures for the standalone hashFile function
func TestHashFile_Fail(t *testing.T) {
// Create a temporary directory for the test
memFs := afero.NewMemMapFs()
tmpDir, err := afero.TempDir(memFs, "", "hash-test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
t.Run("Non-existent file", func(t *testing.T) {
filePath := filepath.Join(tmpDir, "nonexistent.txt")
// For non-existent file test, try to open it to generate the expected error
_, err := memFs.Open(filePath)
if err == nil {
t.Errorf("Expected error opening non-existent file, but got none")
}
})
}
// TestCacheHashFile tests the Cache.hashInput method
// The main idea is to test if the hashing interacting with the abstractions preserve the results compared to using the hash directly
func TestCacheHashFile(t *testing.T) {
// Create a cache with memory filesystem
memFs := afero.NewMemMapFs()
cache, err := New("", WithNowFunc(fixedNowFunc), WithFs(memFs), WithHashFunc(defaultHashFunc))
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
// Test cases
testCases := []struct {
name string
content []byte
size int64
fileFunc func(afero.Fs) string
}{
{
name: "Small file",
content: []byte("small file content"),
size: int64(len([]byte("small file content"))),
fileFunc: func(fs afero.Fs) string {
path := "/small.txt"
if err := afero.WriteFile(fs, path, []byte("small file content"), 0o644); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
return path
},
},
{
name: "Empty file",
content: []byte{},
size: 0,
fileFunc: func(fs afero.Fs) string {
path := "/empty.txt"
if err := afero.WriteFile(fs, path, []byte{}, 0o644); err != nil {
t.Fatalf("Failed to write empty file: %v", err)
}
return path
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cache.hash.Reset()
filePath := tc.fileFunc(memFs)
// Create two hash instances to compare results
h1 := cache.hash
h2 := xxhash.New()
input := FileInput{
Path: filePath,
Fs: memFs,
}
// Hash the input
err := cache.hashInput(input)
if err != nil {
t.Errorf("Cache.hashInput() error = %v, but expected none", err)
return
}
// Hash the content directly
h2.Write(tc.content)
// Compare the hashes
if !bytes.Equal(h1.Sum(nil), h2.Sum(nil)) {
t.Errorf("Cache.hashFile() produced different hash than direct hashing")
}
})
}
}
func TestCacheHashFile_Fail(t *testing.T) {
// Create a cache with memory filesystem
memFs := afero.NewMemMapFs()
cache, err := New("", WithNowFunc(fixedNowFunc), WithFs(memFs))
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
// Test cases
testCases := []struct {
name string
fileFunc func(afero.Fs) string
}{
{
name: "Non-existent file",
fileFunc: func(fs afero.Fs) string {
return "/nonexistent.txt"
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
filePath := tc.fileFunc(memFs)
input := FileInput{
Path: filePath,
Fs: memFs,
}
err := cache.hashInput(input)
if err == nil {
t.Error("Cache.hashInput() should fail, but there is no error")
return
}
})
}
}
// TestSpecialCharacters tests hashing files with special characters in their names
func TestSpecialCharacters(t *testing.T) {
memFs := afero.NewMemMapFs()
cache, err := New("", WithNowFunc(fixedNowFunc), WithFs(memFs), WithHashFunc(defaultHashFunc))
if err != nil {
t.Fatalf("Failed to create cache: %v", err)
}
// Test content
content := []byte("content for special character test")
// Test cases with special characters in filenames
specialNames := []string{
"/special-!@#$%^&*().txt",
"/space file.txt",
"/unicode-文件.txt",
"/emoji-😀.txt",
}
for _, name := range specialNames {
t.Run(name, func(t *testing.T) {
cache.hash.Reset()
// Write file
if err := afero.WriteFile(memFs, name, content, 0o644); err != nil {
t.Fatalf("Failed to write file %s: %v", name, err)
}
// Create hash instances
h1 := cache.hash // From cache
h2 := xxhash.New() // For direct hashing
input := FileInput{
Path: name,
Fs: memFs,
}
// Hash using Cache.hashFile
if err := cache.hashInput(input); err != nil {
t.Fatalf("Cache.hashFile failed for %s: %v", name, err)
}
// Hash directly
h2.Write(content)
// Compare hashes
if !bytes.Equal(h1.Sum(nil), h2.Sum(nil)) {
t.Errorf("Cache.hashFile produced different hash than direct hashing for %s", name)
}
})
}
}
// TestBufferPoolReuse tests that the buffer pool is properly reused
func TestBufferPoolReuse(t *testing.T) {
// Create a memory filesystem
memFs := afero.NewMemMapFs()
// Create a test file
filePath := "/test.txt"
content := []byte("test content for buffer pool test")
if err := afero.WriteFile(memFs, filePath, content, 0o644); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
// Get a buffer from the pool
bufPtr1 := bufferPool.Get().(*[]byte)
buffer1 := *bufPtr1
// Use the buffer
h := xxhash.New()
file, err := memFs.Open(filePath)
if err != nil {
t.Fatalf("Failed to open file: %v", err)
}
defer file.Close()
_, err = io.CopyBuffer(h, file, buffer1)
if err != nil {
t.Fatalf("Failed to copy: %v", err)
}
// Put the buffer back
bufferPool.Put(bufPtr1)
// Get another buffer
bufPtr2 := bufferPool.Get().(*[]byte)
buffer2 := *bufPtr2
defer bufferPool.Put(bufPtr2)
// Check if it's the same buffer (by capacity and length)
if cap(buffer1) != cap(buffer2) || len(buffer1) != len(buffer2) {
t.Errorf("Buffer pool not reusing buffers: cap1=%d, len1=%d, cap2=%d, len2=%d",
cap(buffer1), len(buffer1), cap(buffer2), len(buffer2))
}
}