Skip to content

Commit f252087

Browse files
committed
auth: add tests for switch to caching sha2
1 parent fba0ad9 commit f252087

File tree

1 file changed

+157
-4
lines changed

1 file changed

+157
-4
lines changed

auth_test.go

Lines changed: 157 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestScrambleSHA256Pass(t *testing.T) {
7979
}
8080
}
8181

82-
func TestAuthCachingSHA256PasswordCached(t *testing.T) {
82+
func TestAuthFastCachingSHA256PasswordCached(t *testing.T) {
8383
conn, mc := newRWMockConn(1)
8484
mc.cfg.User = "root"
8585
mc.cfg.Passwd = "secret"
@@ -123,7 +123,7 @@ func TestAuthCachingSHA256PasswordCached(t *testing.T) {
123123
}
124124
}
125125

126-
func TestAuthCachingSHA256PasswordEmpty(t *testing.T) {
126+
func TestAuthFastCachingSHA256PasswordEmpty(t *testing.T) {
127127
conn, mc := newRWMockConn(1)
128128
mc.cfg.User = "root"
129129
mc.cfg.Passwd = ""
@@ -164,7 +164,7 @@ func TestAuthCachingSHA256PasswordEmpty(t *testing.T) {
164164
}
165165
}
166166

167-
func TestAuthCachingSHA256PasswordFullRSA(t *testing.T) {
167+
func TestAuthFastCachingSHA256PasswordFullRSA(t *testing.T) {
168168
conn, mc := newRWMockConn(1)
169169
mc.cfg.User = "root"
170170
mc.cfg.Passwd = "secret"
@@ -218,7 +218,7 @@ func TestAuthCachingSHA256PasswordFullRSA(t *testing.T) {
218218
}
219219
}
220220

221-
func TestAuthCachingSHA256PasswordFullSecure(t *testing.T) {
221+
func TestAuthFastCachingSHA256PasswordFullSecure(t *testing.T) {
222222
conn, mc := newRWMockConn(1)
223223
mc.cfg.User = "root"
224224
mc.cfg.Passwd = "secret"
@@ -273,6 +273,159 @@ func TestAuthCachingSHA256PasswordFullSecure(t *testing.T) {
273273
}
274274
}
275275

276+
func TestAuthSwitchCachingSHA256PasswordCached(t *testing.T) {
277+
conn, mc := newRWMockConn(2)
278+
mc.cfg.Passwd = "secret"
279+
280+
// auth switch request
281+
conn.data = []byte{44, 0, 0, 2, 254, 99, 97, 99, 104, 105, 110, 103, 95,
282+
115, 104, 97, 50, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0, 101,
283+
11, 26, 18, 94, 97, 22, 72, 2, 46, 70, 106, 29, 55, 45, 94, 76, 90, 84,
284+
50, 0}
285+
286+
// auth response
287+
conn.queuedReplies = [][]byte{
288+
{7, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0}, // OK
289+
}
290+
conn.maxReads = 3
291+
292+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
293+
47, 43, 9, 41, 112, 67, 110}
294+
plugin := "mysql_native_password"
295+
296+
if err := mc.handleAuthResult(authData, plugin); err != nil {
297+
t.Errorf("got error: %v", err)
298+
}
299+
300+
expectedReply := []byte{
301+
// 1. Packet: Hash
302+
32, 0, 0, 3, 129, 93, 132, 95, 114, 48, 79, 215, 128, 62, 193, 118, 128,
303+
54, 75, 208, 159, 252, 227, 215, 129, 15, 242, 97, 19, 159, 31, 20, 58,
304+
153, 9, 130,
305+
}
306+
if !bytes.Equal(conn.written, expectedReply) {
307+
t.Errorf("got unexpected data: %v", conn.written)
308+
}
309+
}
310+
311+
func TestAuthSwitchCachingSHA256PasswordEmpty(t *testing.T) {
312+
conn, mc := newRWMockConn(2)
313+
mc.cfg.Passwd = ""
314+
315+
// auth switch request
316+
conn.data = []byte{44, 0, 0, 2, 254, 99, 97, 99, 104, 105, 110, 103, 95,
317+
115, 104, 97, 50, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0, 101,
318+
11, 26, 18, 94, 97, 22, 72, 2, 46, 70, 106, 29, 55, 45, 94, 76, 90, 84,
319+
50, 0}
320+
321+
// auth response
322+
conn.queuedReplies = [][]byte{{7, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0}}
323+
conn.maxReads = 2
324+
325+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
326+
47, 43, 9, 41, 112, 67, 110}
327+
plugin := "mysql_native_password"
328+
329+
if err := mc.handleAuthResult(authData, plugin); err != nil {
330+
t.Errorf("got error: %v", err)
331+
}
332+
333+
expectedReply := []byte{0, 0, 0, 3}
334+
if !bytes.Equal(conn.written, expectedReply) {
335+
t.Errorf("got unexpected data: %v", conn.written)
336+
}
337+
}
338+
339+
func TestAuthSwitchCachingSHA256PasswordFullRSA(t *testing.T) {
340+
conn, mc := newRWMockConn(2)
341+
mc.cfg.Passwd = "secret"
342+
343+
// auth switch request
344+
conn.data = []byte{44, 0, 0, 2, 254, 99, 97, 99, 104, 105, 110, 103, 95,
345+
115, 104, 97, 50, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0, 101,
346+
11, 26, 18, 94, 97, 22, 72, 2, 46, 70, 106, 29, 55, 45, 94, 76, 90, 84,
347+
50, 0}
348+
349+
conn.queuedReplies = [][]byte{
350+
// Perform Full Authentication
351+
{2, 0, 0, 4, 1, 4},
352+
353+
// Pub Key Response
354+
append([]byte{byte(len(serverPubKey)), 1, 0, 6}, serverPubKey...),
355+
356+
// OK
357+
{7, 0, 0, 8, 0, 0, 0, 2, 0, 0, 0},
358+
}
359+
conn.maxReads = 4
360+
361+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
362+
47, 43, 9, 41, 112, 67, 110}
363+
plugin := "mysql_native_password"
364+
365+
if err := mc.handleAuthResult(authData, plugin); err != nil {
366+
t.Errorf("got error: %v", err)
367+
}
368+
369+
expectedReplyPrefix := []byte{
370+
// 1. Packet: Hash
371+
32, 0, 0, 3, 129, 93, 132, 95, 114, 48, 79, 215, 128, 62, 193, 118, 128,
372+
54, 75, 208, 159, 252, 227, 215, 129, 15, 242, 97, 19, 159, 31, 20, 58,
373+
153, 9, 130,
374+
375+
// 2. Packet: Pub Key Request
376+
1, 0, 0, 5, 2,
377+
378+
// 3. Packet: Encrypted Password
379+
0, 1, 0, 7, // [changing bytes]
380+
}
381+
if !bytes.HasPrefix(conn.written, expectedReplyPrefix) {
382+
t.Errorf("got unexpected data: %v", conn.written)
383+
}
384+
}
385+
386+
func TestAuthSwitchCachingSHA256PasswordFullSecure(t *testing.T) {
387+
conn, mc := newRWMockConn(2)
388+
mc.cfg.Passwd = "secret"
389+
390+
// Hack to make the caching_sha2_password plugin believe that the connection
391+
// is secure
392+
mc.cfg.tls = &tls.Config{InsecureSkipVerify: true}
393+
394+
// auth switch request
395+
conn.data = []byte{44, 0, 0, 2, 254, 99, 97, 99, 104, 105, 110, 103, 95,
396+
115, 104, 97, 50, 95, 112, 97, 115, 115, 119, 111, 114, 100, 0, 101,
397+
11, 26, 18, 94, 97, 22, 72, 2, 46, 70, 106, 29, 55, 45, 94, 76, 90, 84,
398+
50, 0}
399+
400+
// auth response
401+
conn.queuedReplies = [][]byte{
402+
{2, 0, 0, 4, 1, 4}, // Perform Full Authentication
403+
{7, 0, 0, 6, 0, 0, 0, 2, 0, 0, 0}, // OK
404+
}
405+
conn.maxReads = 3
406+
407+
authData := []byte{123, 87, 15, 84, 20, 58, 37, 121, 91, 117, 51, 24, 19,
408+
47, 43, 9, 41, 112, 67, 110}
409+
plugin := "mysql_native_password"
410+
411+
if err := mc.handleAuthResult(authData, plugin); err != nil {
412+
t.Errorf("got error: %v", err)
413+
}
414+
415+
expectedReply := []byte{
416+
// 1. Packet: Hash
417+
32, 0, 0, 3, 129, 93, 132, 95, 114, 48, 79, 215, 128, 62, 193, 118, 128,
418+
54, 75, 208, 159, 252, 227, 215, 129, 15, 242, 97, 19, 159, 31, 20, 58,
419+
153, 9, 130,
420+
421+
// 2. Packet: Cleartext password
422+
6, 0, 0, 5, 115, 101, 99, 114, 101, 116,
423+
}
424+
if !bytes.Equal(conn.written, expectedReply) {
425+
t.Errorf("got unexpected data: %v", conn.written)
426+
}
427+
}
428+
276429
func TestAuthSwitchCleartextPasswordNotAllowed(t *testing.T) {
277430
conn, mc := newRWMockConn(2)
278431

0 commit comments

Comments
 (0)