|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/go-mysql-org/go-mysql/mysql" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestDecodeSessionTracking(t *testing.T) { |
| 11 | + data := []struct { |
| 12 | + input []byte |
| 13 | + result bool |
| 14 | + output *mysql.SessionTrackingInfo |
| 15 | + }{ |
| 16 | + { |
| 17 | + []byte{}, |
| 18 | + true, |
| 19 | + &mysql.SessionTrackingInfo{GTID: "", TransactionState: "", Variables: map[string]string(nil), Schema: "", State: "", Characteristics: ""}, |
| 20 | + }, |
| 21 | + { |
| 22 | + // schema=mysql, state=1 |
| 23 | + []byte{0x1, 0x6, 0x5, 0x6d, 0x79, 0x73, 0x71, 0x6c, 0x2, 0x1, 0x31}, |
| 24 | + true, |
| 25 | + &mysql.SessionTrackingInfo{GTID: "", TransactionState: "", Variables: map[string]string(nil), Schema: "mysql", State: "1", Characteristics: ""}, |
| 26 | + }, |
| 27 | + { |
| 28 | + // got unknown change type 10 |
| 29 | + []byte{0xa, 0x6, 0x5, 0x6d, 0x79, 0x73, 0x71, 0x6c, 0x2, 0x1, 0x31}, |
| 30 | + false, |
| 31 | + nil, |
| 32 | + }, |
| 33 | + { |
| 34 | + // GTID, autocommit, state |
| 35 | + []byte{0x0, 0xf, 0xa, 0x61, 0x75, 0x74, 0x6f, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x3, 0x4f, 0x46, 0x46, 0x2, 0x1, 0x31, 0x3, 0x36, 0x0, 0x34, 0x66, 0x34, 0x39, 0x39, 0x33, 0x63, 0x35, 0x65, 0x2d, 0x64, 0x33, 0x35, 0x33, 0x2d, 0x31, 0x31, 0x66, 0x30, 0x2d, 0x39, 0x62, 0x35, 0x66, 0x2d, 0x65, 0x65, 0x64, 0x65, 0x36, 0x64, 0x35, 0x36, 0x32, 0x36, 0x63, 0x38, 0x3a, 0x31, 0x2d, 0x32, 0x33, 0x38, 0x3a, 0x78, 0x6d, 0x61, 0x73, 0x3a, 0x31, 0x2d, 0x32, 0x39}, |
| 36 | + true, |
| 37 | + &mysql.SessionTrackingInfo{GTID: "f4993c5e-d353-11f0-9b5f-eede6d5626c8:1-238:xmas:1-29", TransactionState: "", Variables: map[string]string{"autocommit": "OFF"}, Schema: "", State: "1", Characteristics: ""}, |
| 38 | + }, |
| 39 | + { |
| 40 | + // Incorrect GTID format |
| 41 | + []byte{0x0, 0xf, 0xa, 0x61, 0x75, 0x74, 0x6f, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x3, 0x4f, 0x46, 0x46, 0x2, 0x1, 0x31, 0x3, 0x36, 0x1, 0x34, 0x66, 0x34, 0x39, 0x39, 0x33, 0x63, 0x35, 0x65, 0x2d, 0x64, 0x33, 0x35, 0x33, 0x2d, 0x31, 0x31, 0x66, 0x30, 0x2d, 0x39, 0x62, 0x35, 0x66, 0x2d, 0x65, 0x65, 0x64, 0x65, 0x36, 0x64, 0x35, 0x36, 0x32, 0x36, 0x63, 0x38, 0x3a, 0x31, 0x2d, 0x32, 0x33, 0x38, 0x3a, 0x78, 0x6d, 0x61, 0x73, 0x3a, 0x31, 0x2d, 0x32, 0x39}, |
| 42 | + false, |
| 43 | + nil, |
| 44 | + }, |
| 45 | + { |
| 46 | + // TransactionState, Characteristics |
| 47 | + []byte{0x5, 0x9, 0x8, 0x54, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x4, 0x1d, 0x1c, 0x53, 0x54, 0x41, 0x52, 0x54, 0x20, 0x54, 0x52, 0x41, 0x4e, 0x53, 0x41, 0x43, 0x54, 0x49, 0x4f, 0x4e, 0x20, 0x52, 0x45, 0x41, 0x44, 0x20, 0x4f, 0x4e, 0x4c, 0x59, 0x3b}, |
| 48 | + true, |
| 49 | + &mysql.SessionTrackingInfo{GTID: "", TransactionState: "T_______", Variables: map[string]string(nil), Schema: "", State: "", Characteristics: "START TRANSACTION READ ONLY;"}, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for i, tc := range data { |
| 54 | + o, err := decodeSessionTracking(tc.input) |
| 55 | + if tc.result { |
| 56 | + require.NoError(t, err, "case %d", i) |
| 57 | + require.Equal(t, tc.output, o, "case %d", i) |
| 58 | + } else { |
| 59 | + require.Error(t, err, "case %d", i) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestHandleOKPacket(t *testing.T) { |
| 65 | + c := Conn{ |
| 66 | + capability: mysql.CLIENT_PROTOCOL_41 | mysql.CLIENT_SESSION_TRACK, |
| 67 | + status: mysql.SERVER_SESSION_STATE_CHANGED, |
| 68 | + } |
| 69 | + data := []byte{0x0, 0x3, 0x0, 0x2, 0x40, 0x0, 0x0, 0x26, 0x52, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x73, 0x3a, 0x20, 0x33, 0x20, 0x20, 0x44, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x65, 0x73, 0x3a, 0x20, 0x30, 0x20, 0x20, 0x57, 0x61, 0x72, 0x6e, 0x69, 0x6e, 0x67, 0x73, 0x3a, 0x20, 0x30, 0x38, 0x3, 0x36, 0x0, 0x34, 0x66, 0x34, 0x39, 0x39, 0x33, 0x63, 0x35, 0x65, 0x2d, 0x64, 0x33, 0x35, 0x33, 0x2d, 0x31, 0x31, 0x66, 0x30, 0x2d, 0x39, 0x62, 0x35, 0x66, 0x2d, 0x65, 0x65, 0x64, 0x65, 0x36, 0x64, 0x35, 0x36, 0x32, 0x36, 0x63, 0x38, 0x3a, 0x31, 0x2d, 0x32, 0x34, 0x31, 0x3a, 0x78, 0x6d, 0x61, 0x73, 0x3a, 0x31, 0x2d, 0x32, 0x39} |
| 70 | + _, err := c.handleOKPacket(data) |
| 71 | + require.NoError(t, err) |
| 72 | +} |
0 commit comments