-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwriter_test.go
496 lines (468 loc) · 9.97 KB
/
writer_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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package cipherio_test
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
"testing"
"github.com/golang/mock/gomock"
"github.com/connesc/cipherio"
"github.com/connesc/cipherio/internal/mocks"
)
type writerMockCall struct {
ReqLen int
ResLen int
ResErr error
}
type writeAction struct {
BufLen int
ExpectedLen int
ExpectedErr error
}
type closeAction struct {
ExpectedErr error
}
type writerStep struct {
Action interface{}
MockCalls []writerMockCall
}
type writerTest struct {
Name string
Padding *paddingMock
Steps []writerStep
}
func TestWriter(t *testing.T) {
// Generate a random AES key
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
t.Fatal(err)
}
// Initialize the AES-CBC encrypter
aesCipher, err := aes.NewCipher(key)
if err != nil {
t.Fatal(err)
}
iv := make([]byte, aesCipher.BlockSize())
_, err = rand.Read(iv)
if err != nil {
t.Fatal(err)
}
// Generate random test data
originalBytes := make([]byte, 1056*aesCipher.BlockSize())
_, err = rand.Read(originalBytes)
if err != nil {
t.Fatal(err)
}
expectedBytes := make([]byte, len(originalBytes))
cipher.NewCBCEncrypter(aesCipher, iv).CryptBlocks(expectedBytes, originalBytes)
// Prepare a custom error
testErr := fmt.Errorf("test error")
_ = testErr // TODO
// Prepare test cases
testCases := []writerTest{
{
Name: "VariousWritePatterns",
Steps: []writerStep{
// Write the first 3 bytes of the first block.
// The writer should not call the mock because the block is incomplete.
{
Action: writeAction{
BufLen: 3,
ExpectedLen: 3,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
// Write the 6 next bytes of the first block.
// The writer should now have 9 bytes in its internal buffer.
{
Action: writeAction{
BufLen: 6,
ExpectedLen: 6,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
// Write 20 additional bytes.
// This should complete the first block and leave 13 bytes in the internal buffer.
// The mock should be called with the first encrypted block.
{
Action: writeAction{
BufLen: 20,
ExpectedLen: 20,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{
{
ReqLen: 16,
ResLen: 16,
ResErr: nil,
},
},
},
// Write 1025 complete blocks plus 9 bytes.
// This should complete the last block, then fill 1025 blocks and then leave 6
// bytes in the internal buffer.
// Since there are 1026 encrypted blocks to write, the mock should be called twice.
{
Action: writeAction{
BufLen: 1025*16 + 9,
ExpectedLen: 1025*16 + 9,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{
{
ReqLen: 1024 * 16,
ResLen: 1024 * 16,
ResErr: nil,
},
{
ReqLen: 32,
ResLen: 32,
ResErr: nil,
},
},
},
// Write 42 additional bytes.
// This should complete the last block, then fill 2 blocks and then leave the
// internal buffer empty.
// The mock should be called with the 3 encrypted blocks.
{
Action: writeAction{
BufLen: 42,
ExpectedLen: 42,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{
{
ReqLen: 48,
ResLen: 48,
ResErr: nil,
},
},
},
// Write exactly 3 blocks.
// The internal buffer should remain empty.
// The mock should be called with the 3 encrypted blocks.
{
Action: writeAction{
BufLen: 48,
ExpectedLen: 48,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{
{
ReqLen: 48,
ResLen: 48,
ResErr: nil,
},
},
},
// Close the writer.
// This should succeed since the internal buffer is supposed to be empty.
{
Action: closeAction{
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
// Close the writer again.
// This should be a no-op.
{
Action: closeAction{
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
},
},
{
Name: "UnexpectedEOF",
Steps: []writerStep{
{
Action: writeAction{
BufLen: 3,
ExpectedLen: 3,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: io.ErrUnexpectedEOF,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: io.ErrUnexpectedEOF,
},
MockCalls: []writerMockCall{},
},
},
},
{
Name: "WriteErr",
Steps: []writerStep{
{
Action: writeAction{
BufLen: 20,
ExpectedLen: 12,
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{
{
ReqLen: 16,
ResLen: 12,
ResErr: testErr,
},
},
},
{
Action: writeAction{
BufLen: 3,
ExpectedLen: 0,
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
},
},
{
Name: "WriteErrWithBuffer",
Steps: []writerStep{
{
Action: writeAction{
BufLen: 3,
ExpectedLen: 3,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
{
Action: writeAction{
BufLen: 20,
ExpectedLen: 9,
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{
{
ReqLen: 16,
ResLen: 12,
ResErr: testErr,
},
},
},
{
Action: writeAction{
BufLen: 3,
ExpectedLen: 0,
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
},
},
{
Name: "WriteErrWithBufferOnly",
Steps: []writerStep{
{
Action: writeAction{
BufLen: 3,
ExpectedLen: 3,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
{
Action: writeAction{
BufLen: 20,
ExpectedLen: 0,
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{
{
ReqLen: 16,
ResLen: 2,
ResErr: testErr,
},
},
},
{
Action: writeAction{
BufLen: 3,
ExpectedLen: 0,
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
},
},
{
Name: "Padding",
Padding: &paddingMock{
Len: 13,
},
Steps: []writerStep{
{
Action: writeAction{
BufLen: 3,
ExpectedLen: 3,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: nil,
},
MockCalls: []writerMockCall{
{
ReqLen: 16,
ResLen: 16,
ResErr: nil,
},
},
},
{
Action: closeAction{
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
},
},
{
Name: "PaddingErr",
Padding: &paddingMock{
Len: 13,
},
Steps: []writerStep{
{
Action: writeAction{
BufLen: 3,
ExpectedLen: 3,
ExpectedErr: nil,
},
MockCalls: []writerMockCall{},
},
{
Action: closeAction{
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{
{
ReqLen: 16,
ResLen: 12,
ResErr: testErr,
},
},
},
{
Action: closeAction{
ExpectedErr: testErr,
},
MockCalls: []writerMockCall{},
},
},
},
}
// Run test cases
for index := range testCases {
testCase := testCases[index]
t.Run(testCase.Name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mock := mocks.NewMockWriter(mockCtrl)
blockMode := cipher.NewCBCEncrypter(aesCipher, iv)
var lastMockCall *gomock.Call
originalOffset := 0
expectedOffset := 0
var writer io.WriteCloser
if testCase.Padding != nil {
padding := testCase.Padding.NewMock(mockCtrl, cipherio.PaddingFunc(func(dst []byte) {
copy(dst, originalBytes[originalOffset:])
}))
writer = cipherio.NewBlockWriterWithPadding(mock, blockMode, padding)
} else {
writer = cipherio.NewBlockWriter(mock, blockMode)
}
for _, step := range testCase.Steps {
for index := range step.MockCalls {
mockCallSpec := step.MockCalls[index]
mockCall := mock.EXPECT().Write(gomock.Len(mockCallSpec.ReqLen)).DoAndReturn(func(p []byte) (int, error) {
if !bytes.Equal(p, expectedBytes[expectedOffset:expectedOffset+len(p)]) {
t.Fatalf("unexpected write bytes")
}
expectedOffset += len(p)
return mockCallSpec.ResLen, mockCallSpec.ResErr
})
if lastMockCall != nil {
mockCall.After(lastMockCall)
}
lastMockCall = mockCall
}
switch action := step.Action.(type) {
case writeAction:
src := originalBytes[originalOffset : originalOffset+action.BufLen]
buf := append([]byte(nil), src...)
n, err := writer.Write(buf)
if n != action.ExpectedLen {
t.Fatalf("unexpected write length: %d != %d", n, action.ExpectedLen)
}
if err != action.ExpectedErr {
t.Fatalf("unexpected write err: %v != %v", err, action.ExpectedErr)
}
if !bytes.Equal(buf, src) {
t.Fatalf("unexpected modification in write buffer")
}
originalOffset += n
case closeAction:
err := writer.Close()
if err != action.ExpectedErr {
t.Fatalf("unexpected close err: %v != %v", err, action.ExpectedErr)
}
}
}
})
}
}