Skip to content

Commit caae030

Browse files
committed
auth: add tests for switch to native password
1 parent a27458c commit caae030

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

auth_test.go

+100
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,106 @@ func TestAuthFastCleartextPasswordEmpty(t *testing.T) {
373373
}
374374
}
375375

376+
func TestAuthFastNativePasswordNotAllowed(t *testing.T) {
377+
_, mc := newRWMockConn(1)
378+
mc.cfg.User = "root"
379+
mc.cfg.Passwd = "secret"
380+
mc.cfg.AllowNativePasswords = false
381+
382+
authData := []byte{70, 114, 92, 94, 1, 38, 11, 116, 63, 114, 23, 101, 126,
383+
103, 26, 95, 81, 17, 24, 21}
384+
plugin := "mysql_native_password"
385+
386+
// Send Client Authentication Packet
387+
_, err := mc.auth(authData, plugin)
388+
if err != ErrNativePassword {
389+
t.Errorf("expected ErrNativePassword, got %v", err)
390+
}
391+
}
392+
393+
func TestAuthFastNativePassword(t *testing.T) {
394+
conn, mc := newRWMockConn(1)
395+
mc.cfg.User = "root"
396+
mc.cfg.Passwd = "secret"
397+
398+
authData := []byte{70, 114, 92, 94, 1, 38, 11, 116, 63, 114, 23, 101, 126,
399+
103, 26, 95, 81, 17, 24, 21}
400+
plugin := "mysql_native_password"
401+
402+
// Send Client Authentication Packet
403+
authResp, err := mc.auth(authData, plugin)
404+
if err != nil {
405+
t.Fatal(err)
406+
}
407+
if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
408+
t.Fatal(err)
409+
}
410+
411+
// check written auth response
412+
authRespStart := 4 + 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1
413+
authRespEnd := authRespStart + 1 + len(authResp)
414+
writtenAuthRespLen := conn.written[authRespStart]
415+
writtenAuthResp := conn.written[authRespStart+1 : authRespEnd]
416+
expectedAuthResp := []byte{53, 177, 140, 159, 251, 189, 127, 53, 109, 252,
417+
172, 50, 211, 192, 240, 164, 26, 48, 207, 45}
418+
if writtenAuthRespLen != 20 || !bytes.Equal(writtenAuthResp, expectedAuthResp) {
419+
t.Fatalf("unexpected written auth response (%d bytes): %v", writtenAuthRespLen, writtenAuthResp)
420+
}
421+
conn.written = nil
422+
423+
// auth response
424+
conn.data = []byte{
425+
7, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, // OK
426+
}
427+
conn.maxReads = 1
428+
429+
// Handle response to auth packet
430+
if err := mc.handleAuthResult(authData, plugin); err != nil {
431+
t.Errorf("got error: %v", err)
432+
}
433+
}
434+
435+
func TestAuthFastNativePasswordEmpty(t *testing.T) {
436+
conn, mc := newRWMockConn(1)
437+
mc.cfg.User = "root"
438+
mc.cfg.Passwd = ""
439+
440+
authData := []byte{70, 114, 92, 94, 1, 38, 11, 116, 63, 114, 23, 101, 126,
441+
103, 26, 95, 81, 17, 24, 21}
442+
plugin := "mysql_native_password"
443+
444+
// Send Client Authentication Packet
445+
authResp, err := mc.auth(authData, plugin)
446+
if err != nil {
447+
t.Fatal(err)
448+
}
449+
if err = mc.writeHandshakeResponsePacket(authResp, plugin); err != nil {
450+
t.Fatal(err)
451+
}
452+
453+
// check written auth response
454+
authRespStart := 4 + 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1
455+
authRespEnd := authRespStart + 1 + len(authResp)
456+
writtenAuthRespLen := conn.written[authRespStart]
457+
writtenAuthResp := conn.written[authRespStart+1 : authRespEnd]
458+
if writtenAuthRespLen != 0 {
459+
t.Fatalf("unexpected written auth response (%d bytes): %v",
460+
writtenAuthRespLen, writtenAuthResp)
461+
}
462+
conn.written = nil
463+
464+
// auth response
465+
conn.data = []byte{
466+
7, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, // OK
467+
}
468+
conn.maxReads = 1
469+
470+
// Handle response to auth packet
471+
if err := mc.handleAuthResult(authData, plugin); err != nil {
472+
t.Errorf("got error: %v", err)
473+
}
474+
}
475+
376476
func TestAuthSwitchCachingSHA256PasswordCached(t *testing.T) {
377477
conn, mc := newRWMockConn(2)
378478
mc.cfg.Passwd = "secret"

0 commit comments

Comments
 (0)