|
1 | 1 | package client
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "net" |
4 | 5 | "testing"
|
5 | 6 |
|
6 |
| - "github.com/go-mysql-org/go-mysql/mysql" |
| 7 | + "github.com/pingcap/tidb/pkg/parser/charset" |
7 | 8 | "github.com/stretchr/testify/require"
|
| 9 | + |
| 10 | + "github.com/go-mysql-org/go-mysql/mysql" |
| 11 | + "github.com/go-mysql-org/go-mysql/packet" |
8 | 12 | )
|
9 | 13 |
|
10 | 14 | func TestConnGenAttributes(t *testing.T) {
|
@@ -34,3 +38,75 @@ func TestConnGenAttributes(t *testing.T) {
|
34 | 38 | require.Subset(t, data, fixt)
|
35 | 39 | }
|
36 | 40 | }
|
| 41 | + |
| 42 | +func TestConnCollation(t *testing.T) { |
| 43 | + collations := []string{ |
| 44 | + "big5_chinese_ci", |
| 45 | + "utf8_general_ci", |
| 46 | + "utf8mb4_0900_ai_ci", |
| 47 | + "utf8mb4_de_pb_0900_ai_ci", |
| 48 | + "utf8mb4_ja_0900_as_cs", |
| 49 | + "utf8mb4_0900_bin", |
| 50 | + "utf8mb4_zh_pinyin_tidb_as_cs", |
| 51 | + } |
| 52 | + |
| 53 | + // test all supported collations by calling writeAuthHandshake() and reading the bytes |
| 54 | + // sent to the server to ensure the collation id is set correctly |
| 55 | + for _, c := range collations { |
| 56 | + collation, err := charset.GetCollationByName(c) |
| 57 | + require.NoError(t, err) |
| 58 | + server := sendAuthResponse(t, collation.Name) |
| 59 | + // read the all the bytes of the handshake response so that client goroutine can complete without blocking |
| 60 | + // on the server read. |
| 61 | + handShakeResponse := make([]byte, 128) |
| 62 | + _, err = server.Read(handShakeResponse) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + // validate the collation id is set correctly |
| 66 | + // if the collation ID is <= 255 the collation ID is stored in the 12th byte |
| 67 | + if collation.ID <= 255 { |
| 68 | + require.Equal(t, byte(collation.ID), handShakeResponse[12]) |
| 69 | + // the 13th byte should always be 0x00 |
| 70 | + require.Equal(t, byte(0x00), handShakeResponse[13]) |
| 71 | + } else { |
| 72 | + // if the collation ID is > 255 the collation ID is stored in the 12th and 13th bytes |
| 73 | + require.Equal(t, byte(collation.ID&0xff), handShakeResponse[12]) |
| 74 | + require.Equal(t, byte(collation.ID>>8), handShakeResponse[13]) |
| 75 | + } |
| 76 | + |
| 77 | + // sanity check: validate the 22 bytes of filler with value 0x00 are set correctly |
| 78 | + for i := 14; i < 14+22; i++ { |
| 79 | + require.Equal(t, byte(0x00), handShakeResponse[i]) |
| 80 | + } |
| 81 | + |
| 82 | + // and finally the username |
| 83 | + username := string(handShakeResponse[36:40]) |
| 84 | + require.Equal(t, "test", username) |
| 85 | + |
| 86 | + require.NoError(t, server.Close()) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func sendAuthResponse(t *testing.T, collation string) net.Conn { |
| 91 | + server, client := net.Pipe() |
| 92 | + c := &Conn{ |
| 93 | + Conn: &packet.Conn{ |
| 94 | + Conn: client, |
| 95 | + }, |
| 96 | + authPluginName: "mysql_native_password", |
| 97 | + user: "test", |
| 98 | + db: "test", |
| 99 | + password: "test", |
| 100 | + proto: "tcp", |
| 101 | + collation: collation, |
| 102 | + salt: ([]byte)("123456781234567812345678"), |
| 103 | + } |
| 104 | + |
| 105 | + go func() { |
| 106 | + err := c.writeAuthHandshake() |
| 107 | + require.NoError(t, err) |
| 108 | + err = c.Close() |
| 109 | + require.NoError(t, err) |
| 110 | + }() |
| 111 | + return server |
| 112 | +} |
0 commit comments