-
Notifications
You must be signed in to change notification settings - Fork 504
/
bad_server_test.go
192 lines (170 loc) · 4.53 KB
/
bad_server_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package mssql
import (
"database/sql"
"encoding/binary"
"fmt"
"net"
"testing"
)
// tests simulating bad server
func testConnectionBad(t *testing.T, connStr string) {
conn, err := sql.Open("mssql", connStr)
if err != nil {
// should not fail here
t.Fatal("Open connection failed:", err.Error())
return
}
defer conn.Close()
row := conn.QueryRow("select 1")
var val int
err = row.Scan(&val)
if err == nil {
t.Fatal("Scan should fail but it succeeded")
}
}
func testBadServer(t *testing.T, handler func(net.Conn)) {
addr := &net.TCPAddr{IP: net.IP{127, 0, 0, 1}}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
t.Fatal("Cannot start a listener", err)
}
addr = listener.Addr().(*net.TCPAddr)
go func() {
conn, err := listener.Accept()
if err != nil {
t.Log("Failed to accept connection", err)
return
}
handler(conn)
_ = conn.Close()
}()
tl := testLogger{t: t}
defer tl.StopLogging()
SetLogger(&tl)
testConnectionBad(t, fmt.Sprintf("host=%s;port=%d;log=255", addr.IP.String(), addr.Port))
}
func TestBadServerCloseConnection(t *testing.T) {
testBadServer(t, func(conn net.Conn) {})
}
func TestBadServerInvalidPreLoginPacketSize(t *testing.T) {
testBadServer(t, func(conn net.Conn) {
// indicate to the client that this is not a final packet
// but since there are no more data written, client would fail
err := binary.Write(conn, binary.BigEndian, header{
PacketType: packReply,
Size: uint16(headerSize),
Status: 0, // indicates non final packet
})
if err != nil {
t.Fatal("Writing header failed", err)
}
})
}
func TestBadServerInvalidPreLoginPacketType(t *testing.T) {
testBadServer(t, func(conn net.Conn) {
err := binary.Write(conn, binary.BigEndian, header{
PacketType: packNormal, // invalid packet type, packReply
Size: uint16(headerSize),
Status: 1, // indicate final packet
})
if err != nil {
t.Fatal("Writing header failed", err)
}
})
}
func TestBadServerEmptyPreLoginPacket(t *testing.T) {
testBadServer(t, func(conn net.Conn) {
err := binary.Write(conn, binary.BigEndian, header{
PacketType: packReply,
Size: uint16(headerSize),
Status: 1, // indicate final packet
})
if err != nil {
t.Fatal("Writing header failed", err)
}
})
}
func TestBadServerPreLoginPacketWithNoEntries(t *testing.T) {
testBadServer(t, func(conn net.Conn) {
buf := newTdsBuffer(defaultPacketSize, conn)
fields := map[uint8][]byte{}
err := writePrelogin(packReply, buf, fields)
if err != nil {
t.Fatal("Writing PRELOGIN packet failed", err)
}
})
}
func TestBadServerPreLoginPacketWithJustEncryptionField(t *testing.T) {
testBadServer(t, func(conn net.Conn) {
buf := newTdsBuffer(defaultPacketSize, conn)
fields := map[uint8][]byte{
preloginENCRYPTION: {encryptNotSup},
}
err := writePrelogin(packReply, buf, fields)
if err != nil {
t.Fatal("Writing PRELOGIN packet failed", err)
}
})
}
func goodPreloginSequence(t *testing.T, buf *tdsBuffer) {
// read prelogin request
packetType, err := buf.BeginRead()
if err != nil {
t.Fatal("Failed to read PRELOGIN request", err)
}
if packetType != packPrelogin {
t.Fatal("Client sent non PRELOGIN request packet type", packetType)
}
// write prelogin response
fields := map[uint8][]byte{
preloginENCRYPTION: {encryptNotSup},
}
err = writePrelogin(packReply, buf, fields)
if err != nil {
t.Fatal("Writing PRELOGIN packet failed", err)
}
// read login request
packetType, err = buf.BeginRead()
if err != nil {
t.Fatal("Failed to read LOGIN request", err)
}
if packetType != packLogin7 {
t.Fatal("Client sent non LOGIN request packet type", packetType)
}
}
func TestBadServerNoLoginResponse(t *testing.T) {
testBadServer(t, func(conn net.Conn) {
buf := newTdsBuffer(defaultPacketSize, conn)
goodPreloginSequence(t, buf)
// not sending login response
})
}
func TestBadServerIncorrectLoginResponseType(t *testing.T) {
testBadServer(t, func(conn net.Conn) {
buf := newTdsBuffer(defaultPacketSize, conn)
goodPreloginSequence(t, buf)
// sending incorrect packet type
buf.BeginPacket(packPrelogin, false)
err := buf.flush()
if err != nil {
t.Fatal(err)
}
})
}
func TestBadServerInvalidTokenId(t *testing.T) {
testBadServer(t, func(conn net.Conn) {
buf := newTdsBuffer(defaultPacketSize, conn)
goodPreloginSequence(t, buf)
// sending reply to LOGIN request
buf.BeginPacket(packReply, false)
// this is invalid token id
err := buf.WriteByte(0)
if err != nil {
t.Fatal(err)
}
err = buf.flush()
if err != nil {
t.Fatal(err)
}
})
}