-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathstream_test.go
68 lines (52 loc) · 1.5 KB
/
stream_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
package openssl
import (
"bytes"
"crypto/aes"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type limitedSizeReader struct {
size int
r io.Reader
}
func (o *limitedSizeReader) Read(b []byte) (int, error) {
if len(b) == 0 {
return 0, nil
}
return o.r.Read(b[:o.size]) //nolint:wrapcheck
}
func TestReader(t *testing.T) {
o := New()
pass := "abcd"
plaintext := []byte("123abc,./vvvczcekdewfeojdosndsdlsndlncnepcnodcnviorf409eofnvkdfvjfvdsoijvo")
data, err := o.EncryptBinaryBytes(pass, plaintext, BytesToKeyMD5)
require.NoError(t, err)
for i := 1; i <= aes.BlockSize+1; i++ {
t.Run(fmt.Sprintf("read_size_%d", i), func(t *testing.T) {
var (
buf = new(bytes.Buffer)
bytesBuf = make([]byte, aes.BlockSize+1)
r = &limitedSizeReader{i, bytes.NewReader(data)}
)
_, err = io.CopyBuffer(buf, NewReader(r, pass, BytesToKeyMD5), bytesBuf)
require.NoError(t, err)
assert.Equal(t, plaintext, buf.Bytes())
})
}
}
func TestWriter(t *testing.T) {
o := New()
pass := "abcd"
plaintext := []byte("123abc,./vvvczcekdewfeojzaasdsddsdosnd432pdneonkefnoescndisbcisfheosfbdk vsdovsdn]sdlsndlncnepcnodcnviorf409eofnvkdfvjfvdsoijvo")
buf := bytes.NewBuffer(nil)
es := NewWriter(buf, pass, BytesToKeyMD5)
_, err := es.Write(plaintext)
require.NoError(t, err)
require.NoError(t, es.Close())
da, err := o.DecryptBinaryBytes(pass, buf.Bytes(), BytesToKeyMD5)
require.NoError(t, err)
require.Equal(t, da, plaintext)
}