Skip to content

Commit 0a0d6f2

Browse files
committed
added test for readDB
1 parent aea4caa commit 0a0d6f2

File tree

4 files changed

+216
-4
lines changed

4 files changed

+216
-4
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24
1515
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726
1616
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07
17+
github.com/stretchr/testify v1.7.0
1718
go.uber.org/multierr v1.6.0 // indirect
1819
go.uber.org/zap v1.16.0 // indirect
1920
golang.org/x/text v0.3.6 // indirect

go.sum

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 h1:xT+JlYxNGqyT+XcU8
5151
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
5252
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07 h1:oI+RNwuC9jF2g2lP0u0cVEEZrc/AYBCuFdvwrLWM/6Q=
5353
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07/go.mod h1:yFdBgwXP24JziuRl2NMUahT7nGLNOKi1SIiFxMttVD4=
54+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
5455
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5556
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
56-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
5757
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
58+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
59+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5860
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
5961
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
6062
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
@@ -112,5 +114,7 @@ gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXL
112114
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
113115
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
114116
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
117+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
118+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
115119
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
116120
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=

mocks/Handler.go

+161
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/handshake_resp_test.go

+49-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"bytes"
55
"testing"
66

7+
"github.com/go-mysql-org/go-mysql/mocks"
78
"github.com/go-mysql-org/go-mysql/mysql"
9+
"github.com/stretchr/testify/mock"
810
)
911

1012
func TestReadAuthData(t *testing.T) {
@@ -53,6 +55,50 @@ func TestDecodeFirstPart(t *testing.T) {
5355
}
5456
}
5557

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+
56102
func TestReadPluginName(t *testing.T) {
57103
// example data from
58104
// https://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse41
@@ -84,7 +130,7 @@ func TestReadPluginName(t *testing.T) {
84130
pos := 66
85131

86132
pos = c.readPluginName(mysqlNativePassword, pos)
87-
if pos != 88 {
133+
if pos != 88 { // 66 + len("mysql_native_password") + 1
88134
t.Fatalf("unexpected pos, got %d", pos)
89135
}
90136

@@ -99,7 +145,7 @@ func TestReadPluginName(t *testing.T) {
99145
pos := 66
100146

101147
pos = c.readPluginName(otherPlugin, pos)
102-
if pos != 73 {
148+
if pos != 73 { // 66 + len("foobar") + 1
103149
t.Fatalf("unexpected pos, got %d", pos)
104150
}
105151

@@ -113,7 +159,7 @@ func TestReadPluginName(t *testing.T) {
113159
pos := 123 // can be anything
114160

115161
pos = c.readPluginName(mysqlNativePassword, pos)
116-
if pos != 123 {
162+
if pos != 123 { // capability not set, so same as initial pos
117163
t.Fatalf("unexpected pos, got %d", pos)
118164
}
119165

0 commit comments

Comments
 (0)