File: cmd_noauth_test.go

package info (click to toggle)
golang-github-emersion-go-imap 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 840 kB
  • sloc: makefile: 2
file content (299 lines) | stat: -rw-r--r-- 7,622 bytes parent folder | download | duplicates (3)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package client

import (
	"crypto/tls"
	"io"
	"testing"

	"github.com/emersion/go-imap"
	"github.com/emersion/go-imap/internal"
	"github.com/emersion/go-sasl"
)

func TestClient_StartTLS(t *testing.T) {
	c, s := newTestClient(t)
	defer s.Close()

	cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
	if err != nil {
		t.Fatal("cannot load test certificate:", err)
	}

	tlsConfig := &tls.Config{
		InsecureSkipVerify: true,
		Certificates:       []tls.Certificate{cert},
	}

	if c.IsTLS() {
		t.Fatal("Client has TLS enabled before STARTTLS")
	}

	if ok, err := c.SupportStartTLS(); err != nil {
		t.Fatalf("c.SupportStartTLS() = %v", err)
	} else if !ok {
		t.Fatalf("c.SupportStartTLS() = %v, want true", ok)
	}

	done := make(chan error, 1)
	go func() {
		done <- c.StartTLS(tlsConfig)
	}()

	tag, cmd := s.ScanCmd()
	if cmd != "STARTTLS" {
		t.Fatalf("client sent command %v, want STARTTLS", cmd)
	}
	s.WriteString(tag + " OK Begin TLS negotiation now\r\n")

	ss := tls.Server(s.Conn, tlsConfig)
	if err := ss.Handshake(); err != nil {
		t.Fatal("cannot perform TLS handshake:", err)
	}

	if err := <-done; err != nil {
		t.Error("c.StartTLS() =", err)
	}

	if !c.IsTLS() {
		t.Errorf("Client has not TLS enabled after STARTTLS")
	}

	go func() {
		_, err := c.Capability()
		done <- err
	}()

	tag, cmd = newCmdScanner(ss).ScanCmd()
	if cmd != "CAPABILITY" {
		t.Fatalf("client sent command %v, want CAPABILITY", cmd)
	}
	io.WriteString(ss, "* CAPABILITY IMAP4rev1 AUTH=PLAIN\r\n")
	io.WriteString(ss, tag+" OK CAPABILITY completed.\r\n")
}

func TestClient_Authenticate(t *testing.T) {
	c, s := newTestClient(t)
	defer s.Close()

	if ok, err := c.SupportAuth(sasl.Plain); err != nil {
		t.Fatalf("c.SupportAuth(sasl.Plain) = %v", err)
	} else if !ok {
		t.Fatalf("c.SupportAuth(sasl.Plain) = %v, want true", ok)
	}

	sasl := sasl.NewPlainClient("", "username", "password")

	done := make(chan error, 1)
	go func() {
		done <- c.Authenticate(sasl)
	}()

	tag, cmd := s.ScanCmd()
	if cmd != "AUTHENTICATE PLAIN" {
		t.Fatalf("client sent command %v, want AUTHENTICATE PLAIN", cmd)
	}

	s.WriteString("+ \r\n")

	wantLine := "AHVzZXJuYW1lAHBhc3N3b3Jk"
	if line := s.ScanLine(); line != wantLine {
		t.Fatalf("client sent auth %v, want %v", line, wantLine)
	}

	s.WriteString(tag + " OK AUTHENTICATE completed\r\n")

	if err := <-done; err != nil {
		t.Fatalf("c.Authenticate() = %v", err)
	}

	if state := c.State(); state != imap.AuthenticatedState {
		t.Errorf("c.State() = %v, want %v", state, imap.AuthenticatedState)
	}
}

func TestClient_Authenticate_InitialResponse(t *testing.T) {
	c, s := newTestClientWithGreeting(t, "* OK [CAPABILITY IMAP4rev1 SASL-IR STARTTLS AUTH=PLAIN] Server ready.\r\n")
	defer s.Close()

	if ok, err := c.SupportAuth(sasl.Plain); err != nil {
		t.Fatalf("c.SupportAuth(sasl.Plain) = %v", err)
	} else if !ok {
		t.Fatalf("c.SupportAuth(sasl.Plain) = %v, want true", ok)
	}

	sasl := sasl.NewPlainClient("", "username", "password")

	done := make(chan error, 1)
	go func() {
		done <- c.Authenticate(sasl)
	}()

	tag, cmd := s.ScanCmd()
	if cmd != "AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk" {
		t.Fatalf("client sent command %v, want AUTHENTICATE PLAIN AHVzZXJuYW1lAHBhc3N3b3Jk", cmd)
	}

	s.WriteString(tag + " OK AUTHENTICATE completed\r\n")

	if err := <-done; err != nil {
		t.Fatalf("c.Authenticate() = %v", err)
	}

	if state := c.State(); state != imap.AuthenticatedState {
		t.Errorf("c.State() = %v, want %v", state, imap.AuthenticatedState)
	}
}

func TestClient_Login_Success(t *testing.T) {
	c, s := newTestClient(t)
	defer s.Close()

	done := make(chan error, 1)
	go func() {
		done <- c.Login("username", "password")
	}()

	tag, cmd := s.ScanCmd()
	if cmd != "LOGIN \"username\" \"password\"" {
		t.Fatalf("client sent command %v, want LOGIN username password", cmd)
	}
	s.WriteString(tag + " OK LOGIN completed\r\n")

	if err := <-done; err != nil {
		t.Fatalf("c.Login() = %v", err)
	}

	if state := c.State(); state != imap.AuthenticatedState {
		t.Errorf("c.State() = %v, want %v", state, imap.AuthenticatedState)
	}
}

func TestClient_Login_8bitSync(t *testing.T) {
	c, s := newTestClientWithGreeting(t, "* OK [CAPABILITY IMAP4rev1 SASL-IR STARTTLS AUTH=PLAIN] Server ready.\r\n")
	defer s.Close()

	// Use of UTF-8 will force go-imap to send password in literal.
	done := make(chan error, 1)
	go func() {
		done <- c.Login("username", "пароль")
	}()

	tag, cmd := s.ScanCmd()
	if cmd != "LOGIN \"username\" {12}" {
		t.Fatalf("client sent command %v, want LOGIN \"username\" {12}", cmd)
	}
	s.WriteString("+ send literal\r\n")
	pass := s.ScanLine()
	if pass != "пароль" {
		t.Fatalf("client sent %v, want {12}'пароль' literal", pass)
	}
	s.WriteString(tag + " OK LOGIN completed\r\n")

	if err := <-done; err != nil {
		t.Fatalf("c.Login() = %v", err)
	}

	if state := c.State(); state != imap.AuthenticatedState {
		t.Errorf("c.State() = %v, want %v", state, imap.AuthenticatedState)
	}
}

func TestClient_Login_8bitNonSync(t *testing.T) {
	c, s := newTestClientWithGreeting(t, "* OK [CAPABILITY IMAP4rev1 LITERAL- SASL-IR STARTTLS AUTH=PLAIN] Server ready.\r\n")
	defer s.Close()

	// Use of UTF-8 will force go-imap to send password in literal.
	done := make(chan error, 1)
	go func() {
		done <- c.Login("username", "пароль")
	}()

	tag, cmd := s.ScanCmd()
	if cmd != "LOGIN \"username\" {12+}" {
		t.Fatalf("client sent command %v, want LOGIN \"username\" {12+}", cmd)
	}
	pass := s.ScanLine()
	if pass != "пароль" {
		t.Fatalf("client sent %v, want {12+}'пароль' literal", pass)
	}
	s.WriteString(tag + " OK LOGIN completed\r\n")

	if err := <-done; err != nil {
		t.Fatalf("c.Login() = %v", err)
	}

	if state := c.State(); state != imap.AuthenticatedState {
		t.Errorf("c.State() = %v, want %v", state, imap.AuthenticatedState)
	}
}

func TestClient_Login_Error(t *testing.T) {
	c, s := newTestClient(t)
	defer s.Close()

	done := make(chan error, 1)
	go func() {
		done <- c.Login("username", "password")
	}()

	tag, cmd := s.ScanCmd()
	if cmd != "LOGIN \"username\" \"password\"" {
		t.Fatalf("client sent command %v, want LOGIN username password", cmd)
	}
	s.WriteString(tag + " NO LOGIN incorrect\r\n")

	if err := <-done; err == nil {
		t.Fatal("c.Login() = nil, want LOGIN incorrect")
	}

	if state := c.State(); state != imap.NotAuthenticatedState {
		t.Errorf("c.State() = %v, want %v", state, imap.NotAuthenticatedState)
	}
}

func TestClient_Login_State_Allowed(t *testing.T) {
	c, s := newTestClient(t)
	defer s.Close()

	done := make(chan error, 1)
	go func() {
		done <- c.Login("username", "password")
	}()

	tag, cmd := s.ScanCmd()
	if cmd != "LOGIN \"username\" \"password\"" {
		t.Fatalf("client sent command %v, want LOGIN \"username\" \"password\"", cmd)
	}
	s.WriteString(tag + " OK LOGIN completed\r\n")

	if err := <-done; err != nil {
		t.Fatalf("c.Login() = %v", err)
	}

	if state := c.State(); state != imap.AuthenticatedState {
		t.Errorf("c.State() = %v, want %v", state, imap.AuthenticatedState)
	}

	go func() {
		done <- c.Login("username", "password")
	}()
	if err := <-done; err != ErrAlreadyLoggedIn {
		t.Fatalf("c.Login() = %v, want %v", err, ErrAlreadyLoggedIn)
	}

	go func() {
		done <- c.Logout()
	}()

	s.ScanCmd()
	s.WriteString("* BYE Client asked to close the connection.\r\n")
	s.WriteString(tag + " OK LOGOUT completed\r\n")

	if err := <-done; err != nil {
		t.Fatalf("c.Logout() = %v", err)
	}

	if err := c.Login("username", "password"); err == ErrAlreadyLoggedIn {
		t.Errorf("Client is logout, login must not give %v", ErrAlreadyLoggedIn)
	}
}