-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdistributor-commands_test.go
282 lines (227 loc) · 7.6 KB
/
distributor-commands_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
/*
import (
"bufio"
"fmt"
stdlog "log"
"testing"
"crypto/tls"
"github.com/go-pluto/pluto/imap"
"github.com/go-pluto/pluto/utils"
)
// Variables
var testEnv *utils.TestEnv
// Structs
var capabilityTests = []struct {
in string
out string
}{
{"a CAPABILITY", "* CAPABILITY IMAP4rev1 AUTH=PLAIN\r\na OK CAPABILITY completed"},
{"b capability", "* CAPABILITY IMAP4rev1 AUTH=PLAIN\r\nb OK CAPABILITY completed"},
{"c CAPABILITY ", "c BAD Command CAPABILITY was sent with extra parameters"},
{"CAPABILITY", "* BAD Received invalid IMAP command"},
}
var logoutTests = []struct {
in string
out string
}{
{"a LOGOUT", "* BYE Terminating connection\r\na OK LOGOUT completed"},
{"b logout", "* BYE Terminating connection\r\nb OK LOGOUT completed"},
{"c LOGOUT ", "c BAD Received invalid IMAP command"},
{"LOGOUT some more parameters", "* BAD Received invalid IMAP command"},
{"d LOGOUT some more parameters", "d BAD Command LOGOUT was sent with extra parameters"},
}
var starttlsTests = []struct {
in string
out string
}{
{"a STARTTLS", "a BAD TLS is already active"},
{"b starttls", "b BAD TLS is already active"},
{"c STARTTLS ", "c BAD Command STARTTLS was sent with extra parameters"},
{"STARTTLS", "* BAD Received invalid IMAP command"},
}
var loginTests = []struct {
in string
out string
}{
{"a LOGIN smith sesame", "a NO Name and / or password wrong"},
{"b login smith sesame", "b NO Name and / or password wrong"},
{"c LOGIN user3 password3", "c BAD Received invalid IMAP command"},
{"LOGIN ernie bert", "* BAD Received invalid IMAP command"},
{"d LOL ernie bert", "d BAD Received invalid IMAP command"},
{"e LOGIN let me in please", "e BAD Command LOGIN was not sent with exactly two parameters"},
{"f LOGIN user1 password1", "f OK LOGIN completed"},
{"g LOGIN user1 password1", "g BAD Command LOGIN cannot be executed in this state"},
{"h LOGIN user1 password1", "h BAD Command LOGIN cannot be executed in this state"},
}
// Functions
// TestCapability executes a black-box table test on the
// implemented Capability() function.
func TestCapability(t *testing.T) {
// Connect to IMAP distributor.
conn, err := tls.Dial("tcp", testEnv.Addr, testEnv.TLSConfig)
if err != nil {
t.Fatalf("[imap.TestCapability] Error during connection attempt to IMAP distributor: %s\n", err.Error())
}
// Create new connection struct.
c := &imap.Connection{
OutConn: conn,
OutReader: bufio.NewReader(conn),
}
// Consume mandatory IMAP greeting.
_, err = c.Receive()
if err != nil {
t.Errorf("[imap.TestCapability] Error during receiving initial distributor greeting: %s\n", err.Error())
}
for i, tt := range capabilityTests {
var answer string
// Table test: send 'in' part of each line.
err = c.Send(false, tt.in)
if err != nil {
t.Fatalf("[imap.TestCapability] Sending message to distributor failed with: %s\n", err.Error())
}
// Receive options listed in CAPABILITY command.
capAnswer, err := c.Receive()
if err != nil {
t.Errorf("[imap.TestCapability] Error during receiving table test CAPABILITY: %s\n", err.Error())
}
if i < 2 {
// Receive command termination message from distributor.
okAnswer, err := c.Receive()
if err != nil {
t.Errorf("[imap.TestCapability] Error during receiving table test CAPABILITY: %s\n", err.Error())
}
answer = fmt.Sprintf("%s\r\n%s", capAnswer, okAnswer)
} else {
answer = capAnswer
}
if answer != tt.out {
t.Fatalf("[imap.TestCapability] Expected '%s' but received '%s'\n", tt.out, answer)
}
}
// At the end of each test, terminate connection.
c.IncConn.Close()
}
// TestLogout executes a black-box table test on the
// implemented Logout() function.
func TestLogout(t *testing.T) {
for i, tt := range logoutTests {
var answer string
// Connect to IMAP distributor.
conn, err := tls.Dial("tcp", testEnv.Addr, testEnv.TLSConfig)
if err != nil {
t.Fatalf("[imap.TestLogout] Error during connection attempt to IMAP distributor: %s\n", err.Error())
}
// Create new connection struct.
c := &imap.Connection{
OutConn: conn,
OutReader: bufio.NewReader(conn),
}
// Consume mandatory IMAP greeting.
_, err = c.Receive()
if err != nil {
t.Errorf("[imap.TestLogout] Error during receiving initial distributor greeting: %s\n", err.Error())
}
// Table test: send 'in' part of each line.
err = c.Send(false, tt.in)
if err != nil {
t.Fatalf("[imap.TestLogout] Sending message to distributor failed with: %s\n", err.Error())
}
// Receive logout response.
logoutAnswer, err := c.Receive()
if err != nil {
t.Errorf("[imap.TestLogout] Error during receiving table test LOGOUT: %s\n", err.Error())
}
if i < 2 {
// Receive command termination message from distributor.
okAnswer, err := c.Receive()
if err != nil {
t.Errorf("[imap.TestLogout] Error during receiving table test LOGOUT: %s\n", err.Error())
}
answer = fmt.Sprintf("%s\r\n%s", logoutAnswer, okAnswer)
} else {
answer = logoutAnswer
}
if answer != tt.out {
t.Fatalf("[imap.TestLogout] Expected '%s' but received '%s'\n", tt.out, answer)
}
// At the end of each test, terminate connection.
err = c.IncConn.Close()
if err != nil {
stdlog.Fatal(err)
}
}
}
// TestStartTLS executes a black-box table test on the
// implemented StartTLS() function.
func TestStartTLS(t *testing.T) {
// Connect to IMAP server.
conn, err := tls.Dial("tcp", testEnv.Addr, testEnv.TLSConfig)
if err != nil {
t.Fatalf("[imap.TestStartTLS] Error during connection attempt to IMAP server: %s\n", err.Error())
}
// Create new connection struct.
c := &imap.Connection{
OutConn: conn,
OutReader: bufio.NewReader(conn),
}
// Consume mandatory IMAP greeting.
_, err = c.Receive()
if err != nil {
t.Errorf("[imap.TestStartTLS] Error during receiving initial server greeting: %s\n", err.Error())
}
for _, tt := range starttlsTests {
// Table test: send 'in' part of each line.
err = c.Send(false, tt.in)
if err != nil {
t.Fatalf("[imap.TestStartTLS] Sending message to server failed with: %s\n", err.Error())
}
// Receive go ahead signal for TLS negotiation.
answer, err := c.Receive()
if err != nil {
t.Errorf("[imap.TestStartTLS] Error during receiving table test LOGIN: %s\n", err.Error())
}
if answer != tt.out {
t.Fatalf("[imap.TestStartTLS] Expected '%s' but received '%s'\n", tt.out, answer)
}
}
// At the end of each test, terminate connection.
c.IncConn.Close()
}
// TestLogin executes a black-box table test on the
// implemented Login() function.
func TestLogin(t *testing.T) {
// Connect to IMAP distributor.
conn, err := tls.Dial("tcp", testEnv.Addr, testEnv.TLSConfig)
if err != nil {
t.Fatalf("[imap.TestLogin] Error during connection attempt to IMAP distributor: %s\n", err.Error())
}
// Create new connection struct.
c := &imap.Connection{
OutConn: conn,
OutReader: bufio.NewReader(conn),
}
// Consume mandatory IMAP greeting.
_, err = c.Receive()
if err != nil {
t.Errorf("[imap.TestLogin] Error during receiving initial distributor greeting: %s\n", err.Error())
}
for _, tt := range loginTests {
// Table test: send 'in' part of each line.
err = c.Send(false, tt.in)
if err != nil {
t.Fatalf("[imap.TestLogin] Sending message to distributor failed with: %s\n", err.Error())
}
// Receive successful LOGIN message.
answer, err := c.Receive()
if err != nil {
t.Errorf("[imap.TestLogin] Error during receiving table test LOGIN: %s\n", err.Error())
}
if answer != tt.out {
t.Fatalf("[imap.TestLogin] Expected '%s' but received '%s'\n", tt.out, answer)
}
}
// At the end of each test, terminate connection.
c.IncConn.Close()
}
*/