Skip to content

Commit a27458c

Browse files
committed
auth: add tests for switch to cleartext password
1 parent f252087 commit a27458c

File tree

1 file changed

+103
-3
lines changed

1 file changed

+103
-3
lines changed

auth_test.go

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ func TestAuthFastCachingSHA256PasswordEmpty(t *testing.T) {
146146
authRespEnd := authRespStart + 1 + len(authResp)
147147
writtenAuthRespLen := conn.written[authRespStart]
148148
writtenAuthResp := conn.written[authRespStart+1 : authRespEnd]
149-
expectedAuthResp := []byte{}
150-
if writtenAuthRespLen != 0 || !bytes.Equal(writtenAuthResp, expectedAuthResp) {
151-
t.Fatalf("unexpected written auth response (%d bytes): %v", writtenAuthRespLen, writtenAuthResp)
149+
if writtenAuthRespLen != 0 {
150+
t.Fatalf("unexpected written auth response (%d bytes): %v",
151+
writtenAuthRespLen, writtenAuthResp)
152152
}
153153
conn.written = nil
154154

@@ -273,6 +273,106 @@ func TestAuthFastCachingSHA256PasswordFullSecure(t *testing.T) {
273273
}
274274
}
275275

276+
func TestAuthFastCleartextPasswordNotAllowed(t *testing.T) {
277+
_, mc := newRWMockConn(1)
278+
mc.cfg.User = "root"
279+
mc.cfg.Passwd = "secret"
280+
281+
authData := []byte{70, 114, 92, 94, 1, 38, 11, 116, 63, 114, 23, 101, 126,
282+
103, 26, 95, 81, 17, 24, 21}
283+
plugin := "mysql_clear_password"
284+
285+
// Send Client Authentication Packet
286+
_, err := mc.auth(authData, plugin)
287+
if err != ErrCleartextPassword {
288+
t.Errorf("expected ErrCleartextPassword, got %v", err)
289+
}
290+
}
291+
292+
func TestAuthFastCleartextPassword(t *testing.T) {
293+
conn, mc := newRWMockConn(1)
294+
mc.cfg.User = "root"
295+
mc.cfg.Passwd = "secret"
296+
mc.cfg.AllowCleartextPasswords = true
297+
298+
authData := []byte{70, 114, 92, 94, 1, 38, 11, 116, 63, 114, 23, 101, 126,
299+
103, 26, 95, 81, 17, 24, 21}
300+
plugin := "mysql_clear_password"
301+
302+
// Send Client Authentication Packet
303+
authResp, err := mc.auth(authData, plugin)
304+
if err != nil {
305+
t.Fatal(err)
306+
}
307+
if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
308+
t.Fatal(err)
309+
}
310+
311+
// check written auth response
312+
authRespStart := 4 + 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1
313+
authRespEnd := authRespStart + 1 + len(authResp)
314+
writtenAuthRespLen := conn.written[authRespStart]
315+
writtenAuthResp := conn.written[authRespStart+1 : authRespEnd]
316+
expectedAuthResp := []byte{115, 101, 99, 114, 101, 116}
317+
if writtenAuthRespLen != 6 || !bytes.Equal(writtenAuthResp, expectedAuthResp) {
318+
t.Fatalf("unexpected written auth response (%d bytes): %v", writtenAuthRespLen, writtenAuthResp)
319+
}
320+
conn.written = nil
321+
322+
// auth response
323+
conn.data = []byte{
324+
7, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, // OK
325+
}
326+
conn.maxReads = 1
327+
328+
// Handle response to auth packet
329+
if err := mc.handleAuthResult(authData, plugin); err != nil {
330+
t.Errorf("got error: %v", err)
331+
}
332+
}
333+
334+
func TestAuthFastCleartextPasswordEmpty(t *testing.T) {
335+
conn, mc := newRWMockConn(1)
336+
mc.cfg.User = "root"
337+
mc.cfg.Passwd = ""
338+
mc.cfg.AllowCleartextPasswords = true
339+
340+
authData := []byte{70, 114, 92, 94, 1, 38, 11, 116, 63, 114, 23, 101, 126,
341+
103, 26, 95, 81, 17, 24, 21}
342+
plugin := "mysql_clear_password"
343+
344+
// Send Client Authentication Packet
345+
authResp, err := mc.auth(authData, plugin)
346+
if err != nil {
347+
t.Fatal(err)
348+
}
349+
if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
350+
t.Fatal(err)
351+
}
352+
353+
// check written auth response
354+
authRespStart := 4 + 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1
355+
authRespEnd := authRespStart + 1 + len(authResp)
356+
writtenAuthRespLen := conn.written[authRespStart]
357+
writtenAuthResp := conn.written[authRespStart+1 : authRespEnd]
358+
if writtenAuthRespLen != 0 {
359+
t.Fatalf("unexpected written auth response (%d bytes): %v",
360+
writtenAuthRespLen, writtenAuthResp)
361+
}
362+
conn.written = nil
363+
364+
// auth response
365+
conn.data = []byte{
366+
7, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, // OK
367+
}
368+
conn.maxReads = 1
369+
370+
// Handle response to auth packet
371+
if err := mc.handleAuthResult(authData, plugin); err != nil {
372+
t.Errorf("got error: %v", err)
373+
}
374+
}
375+
276376
func TestAuthSwitchCachingSHA256PasswordCached(t *testing.T) {
277377
conn, mc := newRWMockConn(2)
278378
mc.cfg.Passwd = "secret"

0 commit comments

Comments
 (0)