Skip to content

Commit de51fdd

Browse files
committed
auth: add cleartext auth tests
1 parent 66f8d59 commit de51fdd

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

auth_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,75 @@ func TestScrambleSHA256Pass(t *testing.T) {
5151

5252
}
5353

54+
func TestAuthSwitchCleartextPasswordNotAllowed(t *testing.T) {
55+
conn, mc := newRWMockConn(2)
56+
57+
conn.data = []byte{22, 0, 0, 2, 254, 109, 121, 115, 113, 108, 95, 99, 108,
58+
101, 97, 114, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0}
59+
conn.maxReads = 1
60+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
61+
47, 43, 9, 41, 112, 67, 110}
62+
plugin := "mysql_native_password"
63+
err := mc.handleAuthResult(authData, plugin)
64+
if err != ErrCleartextPassword {
65+
t.Errorf("expected ErrCleartextPassword, got %v", err)
66+
}
67+
}
68+
69+
func TestAuthSwitchCleartextPassword(t *testing.T) {
70+
conn, mc := newRWMockConn(2)
71+
mc.cfg.AllowCleartextPasswords = true
72+
mc.cfg.Passwd = "secret"
73+
74+
// auth switch request
75+
conn.data = []byte{22, 0, 0, 2, 254, 109, 121, 115, 113, 108, 95, 99, 108,
76+
101, 97, 114, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0}
77+
78+
// auth response
79+
conn.queuedReplies = [][]byte{{7, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0}}
80+
conn.maxReads = 2
81+
82+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
83+
47, 43, 9, 41, 112, 67, 110}
84+
plugin := "mysql_native_password"
85+
86+
if err := mc.handleAuthResult(authData, plugin); err != nil {
87+
t.Errorf("got error: %v", err)
88+
}
89+
90+
expectedReply := []byte{6, 0, 0, 3, 115, 101, 99, 114, 101, 116}
91+
if !bytes.Equal(conn.written, expectedReply) {
92+
t.Errorf("got unexpected data: %v", conn.written)
93+
}
94+
}
95+
96+
func TestAuthSwitchCleartextPasswordEmpty(t *testing.T) {
97+
conn, mc := newRWMockConn(2)
98+
mc.cfg.AllowCleartextPasswords = true
99+
mc.cfg.Passwd = ""
100+
101+
// auth switch request
102+
conn.data = []byte{22, 0, 0, 2, 254, 109, 121, 115, 113, 108, 95, 99, 108,
103+
101, 97, 114, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0}
104+
105+
// auth response
106+
conn.queuedReplies = [][]byte{{7, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0}}
107+
conn.maxReads = 2
108+
109+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
110+
47, 43, 9, 41, 112, 67, 110}
111+
plugin := "mysql_native_password"
112+
113+
if err := mc.handleAuthResult(authData, plugin); err != nil {
114+
t.Errorf("got error: %v", err)
115+
}
116+
117+
expectedReply := []byte{0, 0, 0, 3}
118+
if !bytes.Equal(conn.written, expectedReply) {
119+
t.Errorf("got unexpected data: %v", conn.written)
120+
}
121+
}
122+
54123
func TestAuthSwitchOldPasswordNotAllowed(t *testing.T) {
55124
conn, mc := newRWMockConn(2)
56125

0 commit comments

Comments
 (0)