forked from secure-io/sio-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
182 lines (155 loc) · 5.27 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
// Copyright (c) 2019 Andreas Auernhammer. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.
package sio
import (
"bytes"
"io"
"io/ioutil"
"testing"
)
func TestWrite(t *testing.T) {
s := NewStream(AES128GCM, BufferSize())
data := make([]byte, PlaintextSize())
nonce := make([]byte, s.NonceSize())
associatedData := make([]byte, 32)
plaintext := bytes.NewBuffer(make([]byte, 0, len(data)))
ciphertext := bytes.NewBuffer(make([]byte, 0, int64(len(data))+s.Overhead(int64(len(data)))))
ew := s.EncryptWriter(ciphertext, nonce, associatedData)
dw := s.DecryptWriter(plaintext, nonce, associatedData)
half := len(data) / 2
if n, err := ew.Write(data[:half]); err != nil || n != half {
t.Fatalf("Write failed: got %d - want %d err: %v", n, half, err)
}
if n, err := ew.Write(data[half:]); err != nil || n != len(data)-half {
t.Fatalf("Write failed: got %d - want %d err: %v", n, len(data)-half, err)
}
if err := ew.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
half = len(ciphertext.Bytes()) / 2
if n, err := dw.Write(ciphertext.Bytes()[:half]); err != nil || n != half {
t.Fatalf("Write failed: got %d - want %d err: %v", n, half, err)
}
if n, err := dw.Write(ciphertext.Bytes()[half:]); err != nil || n != len(ciphertext.Bytes())-half {
t.Fatalf("Write failed: got %d - want %d err: %v", n, len(ciphertext.Bytes())-half, err)
}
if err := dw.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
if p := plaintext.Bytes(); !bytes.Equal(p, data) {
t.Fatal("Decrypted data does not match the plaintext")
}
}
func TestWriteByte(t *testing.T) {
s := NewStream(AES128GCM, BufferSize())
data := make([]byte, PlaintextSize())
nonce := make([]byte, s.NonceSize())
associatedData := make([]byte, 32)
plaintext := bytes.NewBuffer(make([]byte, 0, len(data)))
ciphertext := bytes.NewBuffer(make([]byte, 0, len(data)))
ew := s.EncryptWriter(ciphertext, nonce, associatedData)
for i, b := range data {
if err := ew.WriteByte(b); err != nil {
t.Fatalf("WriteByte failed at byte %d with: %v", i, err)
}
}
if err := ew.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
dw := s.DecryptWriter(plaintext, nonce, associatedData)
for i, b := range ciphertext.Bytes() {
if err := dw.WriteByte(b); err != nil {
t.Fatalf("WriteByte failed at byte %d with: %v", i, err)
}
}
if err := dw.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
if p := plaintext.Bytes(); !bytes.Equal(p, data) {
t.Fatal("Decrypted data does not match plaintext")
}
}
func TestReadFrom(t *testing.T) {
s := NewStream(AES128GCM, BufferSize())
data := make([]byte, PlaintextSize())
nonce := make([]byte, s.NonceSize())
associatedData := make([]byte, 32)
plaintext := bytes.NewBuffer(make([]byte, 0, len(data)))
ciphertext := bytes.NewBuffer(make([]byte, 0, int64(len(data))+s.Overhead(int64(len(data)))))
sw := s.EncryptWriter(ciphertext, nonce, associatedData)
ow := s.DecryptWriter(plaintext, nonce, associatedData)
if n, err := sw.ReadFrom(bytes.NewReader(data)); err != nil || n != int64(len(data)) {
t.Fatalf("ReadFrom failed: got %d - want %d err: %v", n, int64(len(data)), err)
}
if !sw.closed {
t.Fatal("EncWriter wasn't closed by ReadFrom")
}
ctLen := int64(ciphertext.Len())
if n, err := ow.ReadFrom(ciphertext); err != nil || n != ctLen {
t.Fatalf("ReadFrom failed: got %d - want %d err: %v", n, ctLen, err)
}
if !ow.closed {
t.Fatal("DecWriter wasn't closed by ReadFrom")
}
if p := plaintext.Bytes(); !bytes.Equal(p, data) {
t.Fatal("Decrypted data does not match the plaintext")
}
}
func TestWriteAfterClose(t *testing.T) {
shouldPanicOnWrite := func(context string, w io.Writer, t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("%sWriter did not panic", context)
}
}()
w.Write(nil)
}
s := NewStream(AES128GCM, BufferSize())
nonce := make([]byte, s.NonceSize())
dw := s.DecryptWriter(ioutil.Discard, nonce, nil)
ew := s.EncryptWriter(dw, nonce, nil)
if err := ew.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
shouldPanicOnWrite("Enc", ew, t)
shouldPanicOnWrite("Dec", dw, t)
}
func TestWriteByteAfterClose(t *testing.T) {
shouldPanicOnWrite := func(context string, w io.ByteWriter, t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("%sWriter did not panic", context)
}
}()
w.WriteByte(0)
}
s := NewStream(AES128GCM, BufferSize())
nonce := make([]byte, s.NonceSize())
dw := s.DecryptWriter(ioutil.Discard, nonce, nil)
ew := s.EncryptWriter(dw, nonce, nil)
if err := ew.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
shouldPanicOnWrite("Enc", ew, t)
shouldPanicOnWrite("Dec", dw, t)
}
func TestReadFromAfterClose(t *testing.T) {
shouldPanicOnReadFrom := func(context string, w io.ReaderFrom, t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("%sWriter did not panic", context)
}
}()
w.ReadFrom(bytes.NewReader(nil))
}
s := NewStream(AES128GCM, BufferSize())
nonce := make([]byte, s.NonceSize())
dw := s.DecryptWriter(ioutil.Discard, nonce, nil)
ew := s.EncryptWriter(dw, nonce, nil)
if err := ew.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
shouldPanicOnReadFrom("Enc", ew, t)
shouldPanicOnReadFrom("Dec", dw, t)
}