-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblock_slicer_test.go
104 lines (75 loc) · 2.27 KB
/
block_slicer_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
package crabfs
import (
"bytes"
"crypto/aes"
"io"
"testing"
"github.com/golang/mock/gomock"
crabfsCrypto "github.com/runletapp/crabfs/crypto"
"github.com/runletapp/crabfs/interfaces"
"github.com/stretchr/testify/assert"
)
func setUpBlockSlicerTest(t *testing.T, reader io.Reader, blockSize int64) (interfaces.Slicer, *gomock.Controller) {
slicer, _, ctrl := setUpBlockSlicerTestWithPrivateKey(t, reader, blockSize)
return slicer, ctrl
}
func setUpBlockSlicerTestWithPrivateKey(t *testing.T, reader io.Reader, blockSize int64) (interfaces.Slicer, crabfsCrypto.PrivKey, *gomock.Controller) {
ctrl := setUpBasicTest(t)
assert := assert.New(t)
privateKey, err := GenerateKeyPair()
assert.Nil(err)
cipher, err := aes.NewCipher([]byte("0123456789abcdef"))
assert.Nil(err)
slicer, err := BlockSlicerNew(reader, blockSize, cipher)
assert.Nil(err)
return slicer, privateKey, ctrl
}
func setDownBlockSlicerTest(ctrl *gomock.Controller) {
setDownBasicTest(ctrl)
}
func TestBlockSliceSmallBlocks(t *testing.T) {
assert := assert.New(t)
buffer := &bytes.Buffer{}
buffer.Write([]byte("abc"))
slicer, _, ctrl := setUpBlockSlicerTestWithPrivateKey(t, buffer, 500*1024)
defer setDownBlockSlicerTest(ctrl)
meta, block, n, err := slicer.Next()
assert.Nil(err)
assert.NotNil(meta)
assert.NotNil(block)
assert.Equal(int64(3), n)
assert.Equal(int64(0), meta.Start)
assert.NotEqual([]byte("abc"), block.RawData())
meta, block, n, err = slicer.Next()
assert.Nil(meta)
assert.Nil(block)
assert.NotNil(err)
}
func TestBlockSliceBigBlocks(t *testing.T) {
assert := assert.New(t)
buffer := &bytes.Buffer{}
buffer.Write([]byte("0123456789"))
slicer, ctrl := setUpBlockSlicerTest(t, buffer, 2)
defer setDownBlockSlicerTest(ctrl)
meta, block, n, err := slicer.Next()
assert.Nil(err)
assert.NotNil(meta)
assert.NotNil(block)
assert.True(meta.Size > 0)
assert.Equal(int64(2), n)
assert.Equal(int64(0), meta.Start)
for count := 1; count < 5; count++ {
meta, block, n, err = slicer.Next()
assert.Nil(err)
assert.NotNil(meta)
assert.NotNil(block)
assert.True(meta.Size > 0)
assert.Equal(int64(2), n)
assert.Equal(int64(count*2), meta.Start)
}
meta, block, n, err = slicer.Next()
assert.Nil(meta)
assert.Nil(block)
assert.Equal(int64(0), n)
assert.NotNil(err)
}