-
Notifications
You must be signed in to change notification settings - Fork 0
/
encio_test.go
185 lines (163 loc) · 3.88 KB
/
encio_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
// Package encio provides encrypted input/output functions
package encio
import (
"bytes"
"crypto/aes"
"io"
"path/filepath"
"strings"
"testing"
)
const plaintext = "1234567890123456"
func TestNewReadWriter(t *testing.T) {
var buf bytes.Buffer
w, err := NewWriter(&buf)
if err != nil {
t.Fatal(err)
}
n, err := w.Write([]byte(plaintext))
if err != nil {
t.Fatalf("error encrypting text: %s", err)
}
if err := w.Close(); err != nil {
t.Errorf("error closing writer: %s", err)
}
if n != len(plaintext) {
t.Errorf("incosistent write byte count: want=%d, got=%d", len(plaintext), n)
}
if sz := len(plaintext) + aes.BlockSize; sz != buf.Len() {
t.Errorf("invalid encrypted message size: want=%d, got=%d", sz, buf.Len())
}
r, err := NewReader(&buf)
if err != nil {
t.Errorf("error creating reader: %s", err)
}
var result strings.Builder
if _, err := io.Copy(&result, r); err != nil {
t.Errorf("error reading encrypted data: %s", err)
}
if !strings.EqualFold(result.String(), plaintext) {
t.Errorf("invalid decrypted text: want=%q, got=%q", plaintext, result.String())
}
}
func TestCreateOpen(t *testing.T) {
const testfile = "testfile"
td := t.TempDir()
f, err := Create(filepath.Join(td, testfile))
if err != nil {
t.Fatalf("error creating a test file: %s", err)
}
if n, err := f.Write([]byte(plaintext)); err != nil {
t.Errorf("error writing test data: %s", err)
} else if n != len(plaintext) {
t.Errorf("unexpected number of bytes written: want=%d, got=%d", len(plaintext), n)
}
if err := f.Close(); err != nil {
t.Errorf("error while closing R/W file: %s", err)
}
g, err := Open(filepath.Join(td, testfile))
if err != nil {
t.Errorf("error opening a test file: %s", err)
}
defer func() {
if err := g.Close(); err != nil {
t.Errorf("error while closing R/O file: %s", err)
}
}()
var result strings.Builder
if _, err := io.Copy(&result, g); err != nil {
t.Errorf("error reading encrypted data: %s", err)
}
if !strings.EqualFold(result.String(), plaintext) {
t.Errorf("invalid decrypted text: want=%q, got=%q", plaintext, result.String())
}
}
func TestSetAppID(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"set app ID",
args{"test"},
false,
},
{
"empty app ID",
args{""},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
oldAppID := appID
defer func() {
appID = oldAppID
}()
err := SetAppID(tt.args.s)
if (err != nil) != tt.wantErr {
t.Errorf("SetAppID() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
return
}
if appID != tt.args.s {
t.Errorf("SetAppID failed to set the appID. want=%q, got=%q", tt.args.s, appID)
}
})
}
}
func Test_generateIV(t *testing.T) {
iv1, err := generateIV()
if err != nil {
t.Errorf("unexpected error: %s", err)
}
iv2, err2 := generateIV()
if err2 != nil {
t.Errorf("unexpected error 2: %s", err2)
}
if bytes.EqualFold(iv1[:], iv2[:]) {
t.Errorf("same IV was generated, while it must be random: iv1=%#v, iv2=%#v", iv1, iv2)
}
}
type fakeCloser struct {
closeCalledTimes int
}
func (c *fakeCloser) Close() error {
c.closeCalledTimes++
return nil
}
type fakeReadWriter struct {
fakeCloser
readCalledTimes int
writeCalledTimes int
}
func (frw *fakeReadWriter) Read(b []byte) (int64, error) {
frw.readCalledTimes++
return int64(len(b)), nil
}
func (frw *fakeReadWriter) Write(b []byte) (int, error) {
frw.writeCalledTimes++
return len(b), nil
}
func Test_writeCloser_Close(t *testing.T) {
var (
frw = new(fakeReadWriter)
fc = new(fakeCloser)
)
fw := writeCloser{
f: fc,
WriteCloser: frw,
}
fw.Close()
if fc.closeCalledTimes != 1 {
t.Errorf("f.Close called unexpected number of times: %d", 1)
}
if frw.closeCalledTimes != 1 {
t.Errorf("wc.Close called unexpected number of times: %d", 1)
}
}