-
Notifications
You must be signed in to change notification settings - Fork 6
/
read_and_decrypt_test.go
178 lines (165 loc) · 4.46 KB
/
read_and_decrypt_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
// -----------------------------------------------------------------------------
// github.com/balacode/udpt /[read_and_decrypt_test.go]
// (c) [email protected] License: MIT
// -----------------------------------------------------------------------------
package udpt
import (
"errors"
"testing"
"time"
)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// readAndDecrypt(conn netUDPConn, tempBuf []byte, timeout time.Duration)
//
// go test -run Test_readAndDecrypt_*
// must succeed
func Test_readAndDecrypt_1(t *testing.T) {
data, addr, err := readAndDecrypt(
&mockNetUDPConn{}, // conn
time.Second, // timeout
newTestAESCipher(t), // decryptor
newTestAESCiphertext(), // tempBuf
)
if string(data) != "abc" {
t.Error("0xE6E2DD")
}
if addr == nil ||
addr.Network() != "udp" || addr.String() != "127.8.9.10:11" {
t.Error("0xE58EB3")
}
if err != nil {
t.Error("0xEA21C0", err)
}
}
// must fail because connection is nil, before any other checks
func Test_readAndDecrypt_2(t *testing.T) {
data, addr, err := readAndDecrypt(
nil, // conn <- failure
time.Second, // timeout
nil, // decryptor
nil, // tempBuf
)
if len(data) != 0 {
t.Error("0xEF5EA3")
}
if addr != nil {
t.Error("0xE6FD31")
}
if !matchError(err, "nil connection") {
t.Error("0xE7B3EA", "wrong error:", err)
}
}
// must fail because decryptor is nil
func Test_readAndDecrypt_3(t *testing.T) {
data, addr, err := readAndDecrypt(
&mockNetUDPConn{}, // conn,
time.Second, // timeout
nil, // decryptor <-failure: nil decryptor
make([]byte, 65536), // tempBuf
)
if len(data) != 0 {
t.Error("0xEA7CA1")
}
if addr != nil {
t.Error("0xED4D55")
}
if !matchError(err, "nil decryptor") {
t.Error("0xED13F3", "wrong error:", err)
}
}
// must fail because tempBuf is nil
func Test_readAndDecrypt_4(t *testing.T) {
data, addr, err := readAndDecrypt(
&mockNetUDPConn{}, // conn,
time.Second, // timeout
newTestAESCipher(t), // decryptor
nil, // tempBuf <-failure: nil tempBuf
)
if len(data) != 0 {
t.Error("0xE6E42A")
}
if addr != nil {
t.Error("0xE65A6E")
}
if !matchError(err, "nil tempBuf") {
t.Error("0xE12E41", "wrong error:", err)
}
}
// must fail when conn.SetReadDeadline() fails
func Test_readAndDecrypt_5(t *testing.T) {
data, addr, err := readAndDecrypt(
&mockNetUDPConn{failSetReadDeadline: true}, // conn <-failure
time.Second, // timeout
newTestAESCipher(t), // decryptor
make([]byte, 65536), // tempBuf
)
if len(data) != 0 {
t.Error("0xEC3C98")
}
if addr != nil {
t.Error("0xED8ED3")
}
if !matchError(err, "failed SetReadDeadline") {
t.Error("0xEE04B0", "wrong error:", err)
}
}
// must fail when conn.ReadFrom() fails
func Test_readAndDecrypt_6(t *testing.T) {
data, addr, err := readAndDecrypt(
&mockNetUDPConn{failReadFrom: true}, // conn <-failure
time.Second, // timeout
newTestAESCipher(t), // decryptor
make([]byte, 65536), // tempBuf
)
if len(data) != 0 {
t.Error("0xED40F6")
}
if addr != nil {
t.Error("0xE14D91")
}
if !matchError(err, "failed SetReadDeadline") {
t.Error("0xE3E57D", "wrong error:", err)
}
}
// must fail because ciphertext is garbage
func Test_readAndDecrypt_7(t *testing.T) {
data, addr, err := readAndDecrypt(
&mockNetUDPConn{}, // conn
time.Second, // timeout
newTestAESCipher(t), // decryptor
[]byte{0xA8, 0xE1, 0x7D, 0xD6}, // tempBuf <-failure: bad ciphertext
)
if data != nil {
t.Error("0xEC9C89")
}
if addr != nil {
t.Error("0xED11F4")
}
if !matchError(err, "invalid ciphertext") {
t.Error("0xEA53B8", "wrong error:", err)
}
}
// -----------------------------------------------------------------------------
// netError(err error, otherErrorID uint32) error
//
// go test -run Test_netError_
//
func Test_netError_(t *testing.T) {
err := netError(nil, 0xE12345)
if err != nil {
t.Error("0xEA7F2E", err)
}
err = netError(errors.New("..use of closed network connection.."), 0xE47EB8)
if err != errClosed {
t.Error("0xE8BD57")
}
err = netError(errors.New("..i/o timeout.."), 0xEA11C7)
if err != errTimeout {
t.Error("0xE53ED7")
}
err = netError(errors.New("some other error"), 0xE3E3D8)
if !matchError(err, "some other error") {
t.Error("0xE6F1BB", "wrong error:", err)
}
}
// end