|
14 | 14 | package server |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "bufio" |
| 18 | + "bytes" |
| 19 | + "encoding/binary" |
| 20 | + |
17 | 21 | . "github.com/pingcap/check" |
18 | 22 | "github.com/pingcap/tidb/mysql" |
19 | 23 | ) |
@@ -97,6 +101,37 @@ func (ts ConnTestSuite) TestIssue1768(c *C) { |
97 | 101 | c.Assert(len(p.Auth) > 0, IsTrue) |
98 | 102 | } |
99 | 103 |
|
| 104 | +func (ts ConnTestSuite) TestInitialHandshake(c *C) { |
| 105 | + c.Parallel() |
| 106 | + var outBuffer bytes.Buffer |
| 107 | + cc := &clientConn{ |
| 108 | + connectionID: 1, |
| 109 | + salt: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10}, |
| 110 | + pkt: &packetIO{ |
| 111 | + wb: bufio.NewWriter(&outBuffer), |
| 112 | + }, |
| 113 | + } |
| 114 | + err := cc.writeInitialHandshake() |
| 115 | + c.Assert(err, IsNil) |
| 116 | + |
| 117 | + expected := new(bytes.Buffer) |
| 118 | + expected.WriteByte(0x0a) // Protocol |
| 119 | + expected.WriteString(mysql.ServerVersion) // Version |
| 120 | + expected.WriteByte(0x00) // NULL |
| 121 | + binary.Write(expected, binary.LittleEndian, int32(1)) // Connection ID |
| 122 | + expected.Write([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x00}) // Salt |
| 123 | + binary.Write(expected, binary.LittleEndian, int16(defaultCapability&0xFFFF)) // Server Capability |
| 124 | + expected.WriteByte(uint8(mysql.DefaultCollationID)) // Server Language |
| 125 | + binary.Write(expected, binary.LittleEndian, mysql.ServerStatusAutocommit) // Server Status |
| 126 | + binary.Write(expected, binary.LittleEndian, int16((defaultCapability>>16)&0xFFFF)) // Extended Server Capability |
| 127 | + expected.WriteByte(0x15) // Authentication Plugin Length |
| 128 | + expected.Write([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}) // Unused |
| 129 | + expected.Write([]byte{0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x00}) // Salt |
| 130 | + expected.WriteString("mysql_native_password") // Authentication Plugin |
| 131 | + expected.WriteByte(0x00) // NULL |
| 132 | + c.Assert(outBuffer.Bytes()[4:], DeepEquals, expected.Bytes()) |
| 133 | +} |
| 134 | + |
100 | 135 | func mapIdentical(m1, m2 map[string]string) bool { |
101 | 136 | return mapBelong(m1, m2) && mapBelong(m2, m1) |
102 | 137 | } |
|
0 commit comments