Skip to content

Commit 070f91f

Browse files
committed
auth: add sha256_password tests
1 parent 99c830f commit 070f91f

File tree

1 file changed

+253
-0
lines changed

1 file changed

+253
-0
lines changed

auth_test.go

+253
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,153 @@ func TestAuthFastNativePasswordEmpty(t *testing.T) {
481481
}
482482
}
483483

484+
func TestAuthFastSHA256PasswordEmpty(t *testing.T) {
485+
conn, mc := newRWMockConn(1)
486+
mc.cfg.User = "root"
487+
mc.cfg.Passwd = ""
488+
489+
authData := []byte{6, 81, 96, 114, 14, 42, 50, 30, 76, 47, 1, 95, 126, 81,
490+
62, 94, 83, 80, 52, 85}
491+
plugin := "sha256_password"
492+
493+
// Send Client Authentication Packet
494+
authResp, addNUL, err := mc.auth(authData, plugin)
495+
if err != nil {
496+
t.Fatal(err)
497+
}
498+
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
499+
if err != nil {
500+
t.Fatal(err)
501+
}
502+
503+
// check written auth response
504+
authRespStart := 4 + 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1
505+
authRespEnd := authRespStart + 1 + len(authResp)
506+
writtenAuthRespLen := conn.written[authRespStart]
507+
writtenAuthResp := conn.written[authRespStart+1 : authRespEnd]
508+
if writtenAuthRespLen != 0 {
509+
t.Fatalf("unexpected written auth response (%d bytes): %v", writtenAuthRespLen, writtenAuthResp)
510+
}
511+
conn.written = nil
512+
513+
// auth response (pub key response)
514+
conn.data = append([]byte{byte(len(serverPubKey)), 1, 0, 2}, serverPubKey...)
515+
conn.queuedReplies = [][]byte{
516+
// OK
517+
{7, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0},
518+
}
519+
conn.maxReads = 2
520+
521+
// Handle response to auth packet
522+
if err := mc.handleAuthResult(authData, plugin); err != nil {
523+
t.Errorf("got error: %v", err)
524+
}
525+
526+
if !bytes.HasPrefix(conn.written, []byte{0, 1, 0, 3}) {
527+
t.Errorf("unexpected written data: %v", conn.written)
528+
}
529+
}
530+
531+
func TestAuthFastSHA256PasswordRSA(t *testing.T) {
532+
conn, mc := newRWMockConn(1)
533+
mc.cfg.User = "root"
534+
mc.cfg.Passwd = "secret"
535+
536+
authData := []byte{6, 81, 96, 114, 14, 42, 50, 30, 76, 47, 1, 95, 126, 81,
537+
62, 94, 83, 80, 52, 85}
538+
plugin := "sha256_password"
539+
540+
// Send Client Authentication Packet
541+
authResp, addNUL, err := mc.auth(authData, plugin)
542+
if err != nil {
543+
t.Fatal(err)
544+
}
545+
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
546+
if err != nil {
547+
t.Fatal(err)
548+
}
549+
550+
// check written auth response
551+
authRespStart := 4 + 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1
552+
authRespEnd := authRespStart + 1 + len(authResp)
553+
writtenAuthRespLen := conn.written[authRespStart]
554+
writtenAuthResp := conn.written[authRespStart+1 : authRespEnd]
555+
expectedAuthResp := []byte{1}
556+
if writtenAuthRespLen != 1 || !bytes.Equal(writtenAuthResp, expectedAuthResp) {
557+
t.Fatalf("unexpected written auth response (%d bytes): %v", writtenAuthRespLen, writtenAuthResp)
558+
}
559+
conn.written = nil
560+
561+
// auth response (pub key response)
562+
conn.data = append([]byte{byte(len(serverPubKey)), 1, 0, 2}, serverPubKey...)
563+
conn.queuedReplies = [][]byte{
564+
// OK
565+
{7, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0},
566+
}
567+
conn.maxReads = 2
568+
569+
// Handle response to auth packet
570+
if err := mc.handleAuthResult(authData, plugin); err != nil {
571+
t.Errorf("got error: %v", err)
572+
}
573+
574+
if !bytes.HasPrefix(conn.written, []byte{0, 1, 0, 3}) {
575+
t.Errorf("unexpected written data: %v", conn.written)
576+
}
577+
}
578+
579+
func TestAuthFastSHA256PasswordSecure(t *testing.T) {
580+
conn, mc := newRWMockConn(1)
581+
mc.cfg.User = "root"
582+
mc.cfg.Passwd = "secret"
583+
584+
// hack to make the caching_sha2_password plugin believe that the connection
585+
// is secure
586+
mc.cfg.tls = &tls.Config{InsecureSkipVerify: true}
587+
588+
authData := []byte{6, 81, 96, 114, 14, 42, 50, 30, 76, 47, 1, 95, 126, 81,
589+
62, 94, 83, 80, 52, 85}
590+
plugin := "sha256_password"
591+
592+
// send Client Authentication Packet
593+
authResp, addNUL, err := mc.auth(authData, plugin)
594+
if err != nil {
595+
t.Fatal(err)
596+
}
597+
598+
// unset TLS config to prevent the actual establishment of a TLS wrapper
599+
mc.cfg.tls = nil
600+
601+
err = mc.writeHandshakeResponsePacket(authResp, addNUL, plugin)
602+
if err != nil {
603+
t.Fatal(err)
604+
}
605+
606+
// check written auth response
607+
authRespStart := 4 + 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1
608+
authRespEnd := authRespStart + 1 + len(authResp) + 1
609+
writtenAuthRespLen := conn.written[authRespStart]
610+
writtenAuthResp := conn.written[authRespStart+1 : authRespEnd]
611+
expectedAuthResp := []byte{115, 101, 99, 114, 101, 116, 0}
612+
if writtenAuthRespLen != 6 || !bytes.Equal(writtenAuthResp, expectedAuthResp) {
613+
t.Fatalf("unexpected written auth response (%d bytes): %v", writtenAuthRespLen, writtenAuthResp)
614+
}
615+
conn.written = nil
616+
617+
// auth response (OK)
618+
conn.data = []byte{7, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0}
619+
conn.maxReads = 1
620+
621+
// Handle response to auth packet
622+
if err := mc.handleAuthResult(authData, plugin); err != nil {
623+
t.Errorf("got error: %v", err)
624+
}
625+
626+
if !bytes.Equal(conn.written, []byte{}) {
627+
t.Errorf("unexpected written data: %v", conn.written)
628+
}
629+
}
630+
484631
func TestAuthSwitchCachingSHA256PasswordCached(t *testing.T) {
485632
conn, mc := newRWMockConn(2)
486633
mc.cfg.Passwd = "secret"
@@ -851,3 +998,109 @@ func TestAuthSwitchOldPasswordEmpty(t *testing.T) {
851998
t.Errorf("got unexpected data: %v", conn.written)
852999
}
8531000
}
1001+
1002+
func TestAuthSwitchSHA256PasswordEmpty(t *testing.T) {
1003+
conn, mc := newRWMockConn(2)
1004+
mc.cfg.Passwd = ""
1005+
1006+
// auth switch request
1007+
conn.data = []byte{38, 0, 0, 2, 254, 115, 104, 97, 50, 53, 54, 95, 112, 97,
1008+
115, 115, 119, 111, 114, 100, 0, 78, 82, 62, 40, 100, 1, 59, 31, 44, 69,
1009+
33, 112, 8, 81, 51, 96, 65, 82, 16, 114, 0}
1010+
1011+
conn.queuedReplies = [][]byte{
1012+
// OK
1013+
{7, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0},
1014+
}
1015+
conn.maxReads = 3
1016+
1017+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
1018+
47, 43, 9, 41, 112, 67, 110}
1019+
plugin := "mysql_native_password"
1020+
1021+
if err := mc.handleAuthResult(authData, plugin); err != nil {
1022+
t.Errorf("got error: %v", err)
1023+
}
1024+
1025+
expectedReplyPrefix := []byte{
1026+
// 1. Packet: Empty Password
1027+
1, 0, 0, 3, 0,
1028+
}
1029+
if !bytes.HasPrefix(conn.written, expectedReplyPrefix) {
1030+
t.Errorf("got unexpected data: %v", conn.written)
1031+
}
1032+
}
1033+
1034+
func TestAuthSwitchSHA256PasswordRSA(t *testing.T) {
1035+
conn, mc := newRWMockConn(2)
1036+
mc.cfg.Passwd = "secret"
1037+
1038+
// auth switch request
1039+
conn.data = []byte{38, 0, 0, 2, 254, 115, 104, 97, 50, 53, 54, 95, 112, 97,
1040+
115, 115, 119, 111, 114, 100, 0, 78, 82, 62, 40, 100, 1, 59, 31, 44, 69,
1041+
33, 112, 8, 81, 51, 96, 65, 82, 16, 114, 0}
1042+
1043+
conn.queuedReplies = [][]byte{
1044+
// Pub Key Response
1045+
append([]byte{byte(len(serverPubKey)), 1, 0, 4}, serverPubKey...),
1046+
1047+
// OK
1048+
{7, 0, 0, 6, 0, 0, 0, 2, 0, 0, 0},
1049+
}
1050+
conn.maxReads = 3
1051+
1052+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
1053+
47, 43, 9, 41, 112, 67, 110}
1054+
plugin := "mysql_native_password"
1055+
1056+
if err := mc.handleAuthResult(authData, plugin); err != nil {
1057+
t.Errorf("got error: %v", err)
1058+
}
1059+
1060+
expectedReplyPrefix := []byte{
1061+
// 1. Packet: Pub Key Request
1062+
1, 0, 0, 3, 1,
1063+
1064+
// 2. Packet: Encrypted Password
1065+
0, 1, 0, 5, // [changing bytes]
1066+
}
1067+
if !bytes.HasPrefix(conn.written, expectedReplyPrefix) {
1068+
t.Errorf("got unexpected data: %v", conn.written)
1069+
}
1070+
}
1071+
1072+
func TestAuthSwitchSHA256PasswordSecure(t *testing.T) {
1073+
conn, mc := newRWMockConn(2)
1074+
mc.cfg.Passwd = "secret"
1075+
1076+
// Hack to make the caching_sha2_password plugin believe that the connection
1077+
// is secure
1078+
mc.cfg.tls = &tls.Config{InsecureSkipVerify: true}
1079+
1080+
// auth switch request
1081+
conn.data = []byte{38, 0, 0, 2, 254, 115, 104, 97, 50, 53, 54, 95, 112, 97,
1082+
115, 115, 119, 111, 114, 100, 0, 78, 82, 62, 40, 100, 1, 59, 31, 44, 69,
1083+
33, 112, 8, 81, 51, 96, 65, 82, 16, 114, 0}
1084+
1085+
conn.queuedReplies = [][]byte{
1086+
// OK
1087+
{7, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0},
1088+
}
1089+
conn.maxReads = 2
1090+
1091+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
1092+
47, 43, 9, 41, 112, 67, 110}
1093+
plugin := "mysql_native_password"
1094+
1095+
if err := mc.handleAuthResult(authData, plugin); err != nil {
1096+
t.Errorf("got error: %v", err)
1097+
}
1098+
1099+
expectedReplyPrefix := []byte{
1100+
// 1. Packet: Cleartext Password
1101+
7, 0, 0, 3, 115, 101, 99, 114, 101, 116, 0,
1102+
}
1103+
if !bytes.Equal(conn.written, expectedReplyPrefix) {
1104+
t.Errorf("got unexpected data: %v", conn.written)
1105+
}
1106+
}

0 commit comments

Comments
 (0)