-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhash_buffer_test.go
More file actions
362 lines (290 loc) · 8.75 KB
/
hash_buffer_test.go
File metadata and controls
362 lines (290 loc) · 8.75 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package openssl_test
import (
"bytes"
"io"
"testing"
"github.com/golang-fips/openssl/v2"
)
// TestHashBufferingWithClone tests that Clone properly copies buffered data
func TestHashBufferingWithClone(t *testing.T) {
t.Parallel()
tests := []struct {
name string
data []byte
extra []byte
}{
{
name: "buffered-clone",
data: []byte("hello"), // Small enough to stay in buffer
extra: []byte(" world"),
},
{
name: "single-byte-clone",
data: []byte("a"),
extra: []byte("b"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Write some data that stays in buffer
h := openssl.NewSHA256().(openssl.HashCloner)
h.Write(tt.data)
// Clone while data is buffered
h2, err := h.Clone()
if err != nil {
t.Fatalf("Clone failed: %v", err)
}
// Both should produce same hash
sum1 := h.Sum(nil)
sum2 := h2.Sum(nil)
if !bytes.Equal(sum1, sum2) {
t.Errorf("clone hash mismatch: got %x, want %x", sum2, sum1)
}
// Write more to original
h.Write(tt.extra)
sum3 := h.Sum(nil)
// Clone should be unaffected
sum4 := h2.Sum(nil)
if !bytes.Equal(sum2, sum4) {
t.Errorf("clone was affected by original: got %x, want %x", sum4, sum2)
}
// And they should be different
if bytes.Equal(sum3, sum4) {
t.Error("original and clone have same hash after diverging")
}
})
}
}
// TestHashBufferingMultipleSum tests that Sum can be called multiple times
func TestHashBufferingMultipleSum(t *testing.T) {
t.Parallel()
h := openssl.NewSHA256()
// Write some data (stays in buffer)
data := []byte("test data")
h.Write(data)
// Call Sum multiple times - should return same result
sum1 := h.Sum(nil)
sum2 := h.Sum(nil)
sum3 := h.Sum(nil)
if !bytes.Equal(sum1, sum2) || !bytes.Equal(sum2, sum3) {
t.Errorf("multiple Sum calls returned different results: %x, %x, %x", sum1, sum2, sum3)
}
// Should still be able to write more
h.Write([]byte(" more"))
sum4 := h.Sum(nil)
// This should be different
if bytes.Equal(sum1, sum4) {
t.Error("hash didn't change after additional write")
}
}
// TestHashBufferingFastPath tests the fast path optimization when ctx is nil
func TestHashBufferingFastPath(t *testing.T) {
t.Parallel()
// Test that small data that fits in buffer uses fast path
h := openssl.NewSHA256()
// Write small amount of data that fits in buffer
data := bytes.Repeat([]byte("a"), openssl.HashBufSize-56)
h.Write(data)
// Sum should use fast path (EVP_Digest) since ctx is still nil
sum1 := h.Sum(nil)
// Verify by comparing with one-shot hash
sum2 := openssl.SHA256(data)
if !bytes.Equal(sum1[:], sum2[:]) {
t.Errorf("fast path hash = %x, want %x", sum1, sum2)
}
}
// TestHashBufferingEmptyWrites tests edge cases with empty writes
func TestHashBufferingEmptyWrites(t *testing.T) {
t.Parallel()
h := openssl.NewSHA256()
// Empty write should do nothing
n, err := h.Write([]byte{})
if err != nil || n != 0 {
t.Errorf("empty Write returned (%v, %d), want (nil, 0)", err, n)
}
// Hash should still be empty hash
emptyHash := openssl.SHA256([]byte{})
sum := h.Sum(nil)
if !bytes.Equal(sum, emptyHash[:]) {
t.Errorf("hash after empty write = %x, want %x", sum, emptyHash)
}
// Multiple empty writes
h.Write([]byte{})
h.Write([]byte{})
h.Write([]byte{})
sum2 := h.Sum(nil)
if !bytes.Equal(sum2, emptyHash[:]) {
t.Errorf("hash after multiple empty writes = %x, want %x", sum2, emptyHash)
}
}
// TestHashBufferingWithAppend tests Sum with non-nil input slice
func TestHashBufferingWithAppend(t *testing.T) {
t.Parallel()
h := openssl.NewSHA256()
data := []byte("test")
h.Write(data)
// Sum with prefix
prefix := []byte("prefix:")
result := h.Sum(prefix)
// Should have prefix followed by hash
if !bytes.HasPrefix(result, prefix) {
t.Errorf("result doesn't have prefix: %x", result)
}
// Extract hash part
hash := result[len(prefix):]
// Verify hash is correct
expectedHash := openssl.SHA256(data)
if !bytes.Equal(hash, expectedHash[:]) {
t.Errorf("appended hash = %x, want %x", hash, expectedHash)
}
}
// TestHashBufferingExactFill tests when a single write fills the buffer exactly
func TestHashBufferingExactFill(t *testing.T) {
t.Parallel()
h := openssl.NewSHA256()
// First, write some data that leaves room in buffer
// Buffer is openssl.HashBufSize bytes, so write most of it first
firstWrite := bytes.Repeat([]byte("a"), openssl.HashBufSize-56)
h.Write(firstWrite)
// Now write exactly 56 bytes to fill buffer to openssl.HashBufSize
secondWrite := bytes.Repeat([]byte("b"), 56)
h.Write(secondWrite)
// Get hash
sum1 := h.Sum(nil)
// Compare with expected
allData := append(firstWrite, secondWrite...)
expected := openssl.SHA256(allData)
if !bytes.Equal(sum1, expected[:]) {
t.Errorf("exact fill hash = %x, want %x", sum1, expected)
}
// Also test writing exactly buffer size in one go
h2 := openssl.NewSHA256()
exactBufSize := bytes.Repeat([]byte("x"), openssl.HashBufSize)
h2.Write(exactBufSize)
sum2 := h2.Sum(nil)
expected2 := openssl.SHA256(exactBufSize)
if !bytes.Equal(sum2, expected2[:]) {
t.Errorf("exact buffer size write hash = %x, want %x", sum2, expected2)
}
}
// TestHashBufferingWriteByte tests WriteByte with buffering
func TestHashBufferingWriteByte(t *testing.T) {
t.Parallel()
// WriteByte is available on the concrete types
h := openssl.NewSHA256()
// Write bytes one at a time
data := []byte("hello")
for _, b := range data {
if err := h.(io.ByteWriter).WriteByte(b); err != nil {
t.Fatalf("WriteByte failed: %v", err)
}
}
sum1 := h.Sum(nil)
// Compare with bulk write
h2 := openssl.NewSHA256()
h2.Write(data)
sum2 := h2.Sum(nil)
if !bytes.Equal(sum1, sum2) {
t.Errorf("WriteByte hash = %x, want %x", sum1, sum2)
}
}
// TestHashBufferingWriteString tests WriteString with buffering
func TestHashBufferingWriteString(t *testing.T) {
t.Parallel()
h := openssl.NewSHA256()
// Write string
const s = "hello world"
n, err := h.(io.StringWriter).WriteString(s)
if err != nil || n != len(s) {
t.Fatalf("WriteString returned (%v, %d), want (nil, %d)", err, n, len(s))
}
sum1 := h.Sum(nil)
// Compare with Write
h2 := openssl.NewSHA256()
h2.Write([]byte(s))
sum2 := h2.Sum(nil)
if !bytes.Equal(sum1, sum2) {
t.Errorf("WriteString hash = %x, want %x", sum1, sum2)
}
}
// TestHashBufferingResetWithBufferedData tests Reset with data in buffer
func TestHashBufferingResetWithBufferedData(t *testing.T) {
t.Parallel()
h := openssl.NewSHA256()
// Write data that stays in buffer
h.Write([]byte("some data"))
// Reset should clear buffer
h.Reset()
// Should now be empty hash
emptyHash := openssl.SHA256([]byte{})
sum := h.Sum(nil)
if !bytes.Equal(sum, emptyHash[:]) {
t.Errorf("hash after Reset = %x, want %x", sum, emptyHash)
}
}
// TestHashBufferingLargeData tests buffering with data larger than buffer
func TestHashBufferingLargeData(t *testing.T) {
t.Parallel()
h := openssl.NewSHA256()
// Create data larger than buffer
largeData := bytes.Repeat([]byte("x"), openssl.HashBufSize*4)
// Write in chunks that will cause multiple buffer flushes
chunkSize := 10
for i := 0; i < len(largeData); i += chunkSize {
end := i + chunkSize
if end > len(largeData) {
end = len(largeData)
}
h.Write(largeData[i:end])
}
sum1 := h.Sum(nil)
// Compare with one-shot hash
sum2 := openssl.SHA256(largeData)
if !bytes.Equal(sum1, sum2[:]) {
t.Errorf("chunked hash = %x, want %x", sum1, sum2)
}
}
// TestHashBufferingMixedSizes tests various write sizes
func TestHashBufferingMixedSizes(t *testing.T) {
t.Parallel()
h := openssl.NewSHA256()
var all []byte
// Mix of small and large writes
writes := [][]byte{
[]byte("a"), // 1 byte
bytes.Repeat([]byte("b"), 10), // 10 bytes
[]byte("c"), // 1 byte
bytes.Repeat([]byte("d"), 100), // 100 bytes (exceeds buffer)
[]byte("e"), // 1 byte
bytes.Repeat([]byte("f"), 5), // 5 bytes
bytes.Repeat([]byte("g"), 200), // 200 bytes (exceeds buffer)
[]byte("h"), // 1 byte
}
for _, w := range writes {
h.Write(w)
all = append(all, w...)
}
sum1 := h.Sum(nil)
sum2 := openssl.SHA256(all)
if !bytes.Equal(sum1, sum2[:]) {
t.Errorf("mixed sizes hash = %x, want %x", sum1, sum2)
}
}
// TestHashBufferingCloneAtBufferBoundary tests cloning when buffer is exactly full
func TestHashBufferingCloneAtBufferBoundary(t *testing.T) {
t.Parallel()
h := openssl.NewSHA256().(openssl.HashCloner)
// Write exactly openssl.HashBufSize bytes
data := bytes.Repeat([]byte("a"), openssl.HashBufSize)
h.Write(data)
// Clone at buffer boundary
h2, err := h.Clone()
if err != nil {
t.Fatalf("Clone failed: %v", err)
}
sum1 := h.Sum(nil)
sum2 := h2.Sum(nil)
if !bytes.Equal(sum1, sum2) {
t.Errorf("clone at boundary: got %x, want %x", sum2, sum1)
}
}