forked from pubnative/mysqlproto-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_test.go
77 lines (67 loc) · 1.88 KB
/
conn_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package mysqlproto
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConnCloseStreamIsNotSet(t *testing.T) {
conn := Conn{}
assert.Equal(t, conn.Close(), ErrNoStream)
}
func TestConnCloseWriteError(t *testing.T) {
errSocket := errors.New("can't write into socket")
buf := &buffer{
writeFn: func(data []byte) (int, error) {
if string(data) == string(CommandPacket(COM_QUIT, nil)) {
return 0, errSocket
}
return 0, nil
},
}
conn := Conn{Stream: &Stream{stream: buf}}
err := conn.Close()
assert.Equal(t, err, errSocket)
assert.True(t, buf.closed)
}
func TestConnCloseServerReplyEOF(t *testing.T) {
buf := newBuffer([]byte{})
conn := Conn{Stream: &Stream{stream: buf}}
err := conn.Close()
assert.Nil(t, err)
assert.True(t, buf.closed)
}
func TestConnCloseServerReplyOKPacket(t *testing.T) {
buf := newBuffer([]byte{0x1, 0x0, 0x0, 0x1, OK_PACKET})
conn := Conn{Stream: &Stream{stream: buf}}
err := conn.Close()
assert.Nil(t, err)
assert.True(t, buf.closed)
}
func TestConnCloseServerReplyERRPacket(t *testing.T) {
data := []byte{
0x17, 0x00, 0x00, 0x01, 0xff, 0x48,
0x04, 0x23, 0x48, 0x59, 0x30, 0x30,
0x30, 0x4e, 0x6f, 0x20, 0x74, 0x61,
0x62, 0x6c, 0x65, 0x73, 0x20, 0x75,
0x73, 0x65, 0x64,
}
buf := newBuffer(data)
conn := Conn{Stream: NewStream(buf), CapabilityFlags: CLIENT_PROTOCOL_41}
err := conn.Close()
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "mysqlproto: Error: 1096 SQLSTATE: HY000 Message: No tables used")
assert.True(t, buf.closed)
}
func TestConnCloseServerReplyInvalidPacket(t *testing.T) {
data := []byte{
0x8, 0x0, 0x0, 0x0,
0xdd, 0x48, 0x04, 0x23,
0x48, 0x59, 0x30, 0x30,
}
buf := newBuffer(data)
conn := Conn{Stream: NewStream(buf)}
err := conn.Close()
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "mysqlproto: invalid ERR_PACKET payload: dd48042348593030")
assert.True(t, buf.closed)
}