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,120 @@ 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
+
102
+ func TestReadPluginName (t * testing.T ) {
103
+ // example data from
104
+ // https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse41
105
+ mysqlNativePassword := []byte {
106
+ 0x54 , 0x00 , 0x00 , 0x01 , 0x8d , 0xa6 , 0x0f , 0x00 , 0x00 , 0x00 , 0x00 ,
107
+ 0x01 , 0x08 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
108
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
109
+ 0x00 , 0x00 , 0x00 , 0x70 , 0x61 , 0x6d , 0x00 , 0x14 , 0xab , 0x09 , 0xee ,
110
+ 0xf6 , 0xbc , 0xb1 , 0x32 , 0x3e , 0x61 , 0x14 , 0x38 , 0x65 , 0xc0 , 0x99 ,
111
+ 0x1d , 0x95 , 0x7d , 0x75 , 0xd4 , 0x47 , 0x74 , 0x65 , 0x73 , 0x74 , 0x00 ,
112
+ 0x6d , 0x79 , 0x73 , 0x71 , 0x6c , 0x5f , 0x6e , 0x61 , 0x74 , 0x69 , 0x76 ,
113
+ 0x65 , 0x5f , 0x70 , 0x61 , 0x73 , 0x73 , 0x77 , 0x6f , 0x72 , 0x64 , 0x00 ,
114
+ }
115
+
116
+ // altered example data so it has different auth plugin
117
+ otherPlugin := []byte {
118
+ 0x54 , 0x00 , 0x00 , 0x01 , 0x8d , 0xa6 , 0x0f , 0x00 , 0x00 , 0x00 , 0x00 ,
119
+ 0x01 , 0x08 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
120
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
121
+ 0x00 , 0x00 , 0x00 , 0x70 , 0x61 , 0x6d , 0x00 , 0x14 , 0xab , 0x09 , 0xee ,
122
+ 0xf6 , 0xbc , 0xb1 , 0x32 , 0x3e , 0x61 , 0x14 , 0x38 , 0x65 , 0xc0 , 0x99 ,
123
+ 0x1d , 0x95 , 0x7d , 0x75 , 0xd4 , 0x47 , 0x74 , 0x65 , 0x73 , 0x74 , 0x00 ,
124
+ 0x66 , 0x6f , 0x6f , 0x62 , 0x61 , 0x72 , 0x00 ,
125
+ }
126
+
127
+ t .Run ("mysql_native_password from plugin name" , func (t * testing.T ) {
128
+ c := & Conn {}
129
+ c .SetCapability (mysql .CLIENT_PLUGIN_AUTH )
130
+ pos := 66
131
+
132
+ pos = c .readPluginName (mysqlNativePassword , pos )
133
+ if pos != 88 { // 66 + len("mysql_native_password") + 1
134
+ t .Fatalf ("unexpected pos, got %d" , pos )
135
+ }
136
+
137
+ if c .authPluginName != "mysql_native_password" {
138
+ t .Fatalf ("unexpected plugin name, got %s" , c .authPluginName )
139
+ }
140
+ })
141
+
142
+ t .Run ("other plugin" , func (t * testing.T ) {
143
+ c := & Conn {}
144
+ c .SetCapability (mysql .CLIENT_PLUGIN_AUTH )
145
+ pos := 66
146
+
147
+ pos = c .readPluginName (otherPlugin , pos )
148
+ if pos != 73 { // 66 + len("foobar") + 1
149
+ t .Fatalf ("unexpected pos, got %d" , pos )
150
+ }
151
+
152
+ if c .authPluginName != "foobar" {
153
+ t .Fatalf ("unexpected plugin name, got %s" , c .authPluginName )
154
+ }
155
+ })
156
+
157
+ t .Run ("mysql_native_password as default" , func (t * testing.T ) {
158
+ c := & Conn {}
159
+ pos := 123 // can be anything
160
+
161
+ pos = c .readPluginName (mysqlNativePassword , pos )
162
+ if pos != 123 { // capability not set, so same as initial pos
163
+ t .Fatalf ("unexpected pos, got %d" , pos )
164
+ }
165
+
166
+ if c .authPluginName != mysql .AUTH_NATIVE_PASSWORD {
167
+ t .Fatalf ("unexpected plugin name, got %s" , c .authPluginName )
168
+ }
169
+ })
170
+ }
171
+
56
172
func TestReadAttributes (t * testing.T ) {
57
173
var err error
58
174
// example data from
@@ -75,7 +191,7 @@ func TestReadAttributes(t *testing.T) {
75
191
0x6d , 0x06 , 0x78 , 0x38 , 0x36 , 0x5f , 0x36 , 0x34 , 0x03 , 0x66 , 0x6f , 0x6f ,
76
192
0x03 , 0x62 , 0x61 , 0x72 ,
77
193
}
78
- pos := 85
194
+ pos := 84
79
195
80
196
c := & Conn {}
81
197
0 commit comments