4
4
"bytes"
5
5
"testing"
6
6
7
+ "github.com/go-mysql-org/go-mysql/mocks"
7
8
"github.com/go-mysql-org/go-mysql/mysql"
9
+ "github.com/stretchr/testify/mock"
8
10
)
9
11
10
12
func TestReadAuthData (t * testing.T ) {
@@ -53,6 +55,50 @@ func TestDecodeFirstPart(t *testing.T) {
53
55
}
54
56
}
55
57
58
+ func TestReadDB (t * testing.T ) {
59
+ handler := & mocks.Handler {}
60
+ c := & Conn {
61
+ h : handler ,
62
+ }
63
+ c .SetCapability (mysql .CLIENT_CONNECT_WITH_DB )
64
+ var dbName string
65
+
66
+ // when handler's UseDB is called, copy dbName to local variable
67
+ handler .On ("UseDB" , mock .IsType ("" )).Return (nil ).Once ().RunFn = func (args mock.Arguments ) {
68
+ dbName = args [0 ].(string )
69
+ }
70
+
71
+ // example data from
72
+ // https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse41
73
+ data := []byte {
74
+ 0x54 , 0x00 , 0x00 , 0x01 , 0x8d , 0xa6 , 0x0f , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 ,
75
+ 0x08 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
76
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
77
+ 0x70 , 0x61 , 0x6d , 0x00 , 0x14 , 0xab , 0x09 , 0xee , 0xf6 , 0xbc , 0xb1 , 0x32 ,
78
+ 0x3e , 0x61 , 0x14 , 0x38 , 0x65 , 0xc0 , 0x99 , 0x1d , 0x95 , 0x7d , 0x75 , 0xd4 ,
79
+ 0x47 , 0x74 , 0x65 , 0x73 , 0x74 , 0x00 , 0x6d , 0x79 , 0x73 , 0x71 , 0x6c , 0x5f ,
80
+ 0x6e , 0x61 , 0x74 , 0x69 , 0x76 , 0x65 , 0x5f , 0x70 , 0x61 , 0x73 , 0x73 , 0x77 ,
81
+ 0x6f , 0x72 , 0x64 , 0x00 ,
82
+ }
83
+ pos := 61
84
+
85
+ var err error
86
+ pos , err = c .readDb (data , pos )
87
+ if err != nil {
88
+ t .Fatalf ("unexpected error: %s" , err .Error ())
89
+ }
90
+
91
+ if pos != 66 { // 61 + len("test") + 1
92
+ t .Fatalf ("unexpected pos, got %d" , pos )
93
+ }
94
+
95
+ if dbName != "test" {
96
+ t .Fatalf ("unexpected db, got %s" , dbName )
97
+ }
98
+
99
+ handler .AssertExpectations (t )
100
+ }
101
+
56
102
func TestReadPluginName (t * testing.T ) {
57
103
// example data from
58
104
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse41
@@ -84,7 +130,7 @@ func TestReadPluginName(t *testing.T) {
84
130
pos := 66
85
131
86
132
pos = c .readPluginName (mysqlNativePassword , pos )
87
- if pos != 88 {
133
+ if pos != 88 { // 66 + len("mysql_native_password") + 1
88
134
t .Fatalf ("unexpected pos, got %d" , pos )
89
135
}
90
136
@@ -99,7 +145,7 @@ func TestReadPluginName(t *testing.T) {
99
145
pos := 66
100
146
101
147
pos = c .readPluginName (otherPlugin , pos )
102
- if pos != 73 {
148
+ if pos != 73 { // 66 + len("foobar") + 1
103
149
t .Fatalf ("unexpected pos, got %d" , pos )
104
150
}
105
151
@@ -113,7 +159,7 @@ func TestReadPluginName(t *testing.T) {
113
159
pos := 123 // can be anything
114
160
115
161
pos = c .readPluginName (mysqlNativePassword , pos )
116
- if pos != 123 {
162
+ if pos != 123 { // capability not set, so same as initial pos
117
163
t .Fatalf ("unexpected pos, got %d" , pos )
118
164
}
119
165
0 commit comments