forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buf_test.go
316 lines (278 loc) · 8.36 KB
/
buf_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package mssql
import (
"bytes"
"errors"
"testing"
)
type closableBuffer struct {
*bytes.Buffer
}
func (closableBuffer) Close() error {
return nil
}
type failBuffer struct {
}
func (failBuffer) Read([]byte) (int, error) {
return 0, errors.New("read failed")
}
func (failBuffer) Write([]byte) (int, error) {
return 0, errors.New("write failed")
}
func (failBuffer) Close() error {
return nil
}
func makeBuf(bufSize uint16, testData []byte) *tdsBuffer {
buffer := closableBuffer{bytes.NewBuffer(testData)}
return newTdsBuffer(bufSize, &buffer)
}
func TestStreamShorterThanHeader(t *testing.T) {
//buffer := closableBuffer{*bytes.NewBuffer([]byte{0xFF, 0xFF})}
//buffer := closableBuffer{*bytes.NewBuffer([]byte{0x6F, 0x96, 0x19, 0xFF, 0x8B, 0x86, 0xD0, 0x11, 0xB4, 0x2D, 0x00, 0xC0, 0x4F, 0xC9, 0x64, 0xFF})}
//tdsBuffer := newTdsBuffer(100, &buffer)
buffer := makeBuf(100, []byte{0xFF, 0xFF})
_, err := buffer.BeginRead()
if err == nil {
t.Fatal("BeginRead was expected to return error but it didn't")
} else {
t.Log("BeginRead failed as expected with error:", err.Error())
}
}
func TestInvalidLengthInHeaderTooLong(t *testing.T) {
buffer := makeBuf(8, []byte{0xFF, 0xFF, 0x0, 0x9, 0xff, 0xff, 0xff, 0xff})
_, err := buffer.BeginRead()
if err == nil {
t.Fatal("BeginRead was expected to return error but it didn't")
} else {
if err.Error() != "Invalid packet size, it is longer than buffer size" {
t.Fatal("BeginRead failed with incorrect error", err)
} else {
t.Log("BeginRead failed as expected with error:", err.Error())
}
}
}
func TestInvalidLengthInHeaderTooShort(t *testing.T) {
buffer := makeBuf(100, []byte{0xFF, 0xFF, 0x0, 0x1, 0xff, 0xff, 0xff, 0xff})
_, err := buffer.BeginRead()
if err == nil {
t.Fatal("BeginRead was expected to return error but it didn't")
} else {
t.Log("BeginRead failed as expected with error:", err.Error())
}
}
func TestInvalidLengthInHeaderLongerThanIncomingBuffer(t *testing.T) {
buffer := makeBuf(9, []byte{0xFF, 0xFF, 0x0, 0x9, 0xff, 0xff, 0xff, 0xff})
_, err := buffer.BeginRead()
if err == nil {
t.Fatal("BeginRead was expected to return error but it didn't")
} else {
t.Log("BeginRead failed as expected with error:", err.Error())
}
}
func TestBeginReadSucceeds(t *testing.T) {
buffer := makeBuf(9, []byte{0x01 /*id*/, 0xFF /*status*/, 0x0, 0x9 /*size*/, 0xff, 0xff, 0xff, 0xff, 0x02 /*test byte*/})
id, err := buffer.BeginRead()
if err != nil {
t.Fatal("BeginRead failed:", err.Error())
}
if id != 1 {
t.Fatalf("Expected id to be 1 but it is %d", id)
}
b, err := buffer.ReadByte()
if err != nil {
t.Fatal("ReadByte failed:", err.Error())
}
if b != 2 {
t.Fatalf("Expected read byte to be 2 but it is %d", b)
}
// should fail because no more bytes left
_, err = buffer.ReadByte()
if err == nil {
t.Fatal("ReadByte was expected to return error but it didn't")
} else {
t.Log("ReadByte failed as expected with error:", err.Error())
}
testBuf := []byte{0, 1, 2}
// should fail because no more bytes left
_, err = buffer.Read(testBuf)
if err == nil {
t.Fatal("Read was expected to return error but it didn't")
} else {
t.Log("Read failed as expected with error:", err.Error())
}
}
func TestReadByteFailsOnSecondPacket(t *testing.T) {
buffer := makeBuf(9, []byte{
0x01 /*id*/, 0x0 /*not final*/, 0x0, 0x9 /*size*/, 0xff, 0xff, 0xff, 0xff, 0x02, /*test byte*/
0x01 /*next id, this packet is invalid, it is too short*/})
_, err := buffer.BeginRead()
if err != nil {
t.Fatal("BeginRead failed:", err.Error())
}
_, err = buffer.ReadByte()
if err != nil {
t.Fatal("ReadByte failed:", err.Error())
}
_, err = buffer.ReadByte()
if err == nil {
t.Fatal("ReadByte was expected to return error but it didn't")
} else {
t.Log("ReadByte failed as expected with error:", err.Error())
}
t.Run("test byte() panic", func(t *testing.T) {
defer func() {
recover()
}()
buffer.byte()
t.Fatal("byte() should panic, but it didn't")
})
t.Run("test ReadFull() panic", func(t *testing.T) {
defer func() {
recover()
}()
buf := make([]byte, 10)
buffer.ReadFull(buf)
t.Fatal("ReadFull() should panic, but it didn't")
})
}
func TestReadFailsOnSecondPacket(t *testing.T) {
buffer := makeBuf(9, []byte{
0x01 /*id*/, 0x0 /*not final*/, 0x0, 0x9 /*size*/, 0xff, 0xff, 0xff, 0xff, 0x02, /*test byte*/
0x01 /*next id, this packet is invalid, it is too short*/})
_, err := buffer.BeginRead()
if err != nil {
t.Fatal("BeginRead failed:", err.Error())
}
testBuf := []byte{0}
_, err = buffer.Read(testBuf)
if err != nil {
t.Fatal("Read failed:", err.Error())
}
if testBuf[0] != 2 {
t.Fatal("Read returned invalid value")
}
_, err = buffer.Read(testBuf)
if err == nil {
t.Fatal("ReadByte was expected to return error but it didn't")
} else {
t.Log("ReadByte failed as expected with error:", err.Error())
}
}
func TestWrite(t *testing.T) {
memBuf := bytes.NewBuffer([]byte{})
buf := newTdsBuffer(11, closableBuffer{memBuf})
buf.BeginPacket(1, false)
err := buf.WriteByte(2)
if err != nil {
t.Fatal("WriteByte failed:", err.Error())
}
wrote, err := buf.Write([]byte{3, 4})
if err != nil {
t.Fatal("Write failed:", err.Error())
}
if wrote != 2 {
t.Fatalf("Write returned invalid value of written bytes %d", wrote)
}
err = buf.FinishPacket()
if err != nil {
t.Fatal("FinishPacket failed:", err.Error())
}
if bytes.Compare(memBuf.Bytes(), []byte{1, 1, 0, 11, 0, 0, 1, 0, 2, 3, 4}) != 0 {
t.Fatalf("Written buffer has invalid content: %v", memBuf.Bytes())
}
buf.BeginPacket(2, false)
wrote, err = buf.Write([]byte{3, 4, 5, 6})
if err != nil {
t.Fatal("Write failed:", err.Error())
}
if wrote != 4 {
t.Fatalf("Write returned invalid value of written bytes %d", wrote)
}
err = buf.FinishPacket()
if err != nil {
t.Fatal("FinishPacket failed:", err.Error())
}
expectedBuf := []byte{
1, 1, 0, 11, 0, 0, 1, 0, 2, 3, 4, // packet 1
2, 0, 0, 11, 0, 0, 1, 0, 3, 4, 5, // packet 2
2, 1, 0, 9, 0, 0, 2, 0, 6, // packet 3
}
if bytes.Compare(memBuf.Bytes(), expectedBuf) != 0 {
t.Fatalf("Written buffer has invalid content:\n got: %v\nwant: %v", memBuf.Bytes(), expectedBuf)
}
}
func TestWriteErrors(t *testing.T) {
// write should fail if underlying transport fails
buf := newTdsBuffer(uint16(headerSize)+1, failBuffer{})
buf.BeginPacket(1, false)
wrote, err := buf.Write([]byte{0, 0})
// may change from error to panic in future
if err == nil {
t.Fatal("Write should fail but it didn't")
}
if wrote != 1 {
t.Fatal("Should write 1 byte but it wrote ", wrote)
}
// writebyte should fail if underlying transport fails
buf = newTdsBuffer(uint16(headerSize)+1, failBuffer{})
buf.BeginPacket(1, false)
// first write should not fail because if fits in the buffer
err = buf.WriteByte(0)
if err != nil {
t.Fatal("First WriteByte should not fail because it should fit in the buffer, but it failed", err)
}
err = buf.WriteByte(0)
// may change from error to panic in future
if err == nil {
t.Fatal("Second WriteByte should fail but it didn't")
}
}
func TestWrite_BufferBounds(t *testing.T) {
memBuf := bytes.NewBuffer([]byte{})
buf := newTdsBuffer(11, closableBuffer{memBuf})
buf.BeginPacket(1, false)
// write bytes enough to complete a package
_, err := buf.Write([]byte{1, 1, 1})
if err != nil {
t.Fatal("Write failed:", err.Error())
}
err = buf.WriteByte(1)
if err != nil {
t.Fatal("WriteByte failed:", err.Error())
}
_, err = buf.Write([]byte{1, 1, 1})
if err != nil {
t.Fatal("Write failed:", err.Error())
}
err = buf.FinishPacket()
if err != nil {
t.Fatal("FinishPacket failed:", err.Error())
}
}
func TestReadUsVarCharOrPanic(t *testing.T) {
memBuf := bytes.NewBuffer([]byte{3, 0, 0x31, 0, 0x32, 0, 0x33, 0})
s := readUsVarCharOrPanic(memBuf)
if s != "123" {
t.Errorf("UsVarChar expected to return 123 but it returned %s", s)
}
// test invalid usvarchar
defer func() {
recover()
}()
memBuf = bytes.NewBuffer([]byte{})
s = readUsVarCharOrPanic(memBuf)
t.Fatal("UsVarChar() should panic, but it didn't")
}
func TestReadBVarCharOrPanic(t *testing.T) {
memBuf := bytes.NewBuffer([]byte{3, 0x31, 0, 0x32, 0, 0x33, 0})
s := readBVarCharOrPanic(memBuf)
if s != "123" {
t.Errorf("readBVarCharOrPanic expected to return 123 but it returned %s", s)
}
// test invalid varchar
defer func() {
recover()
}()
memBuf = bytes.NewBuffer([]byte{})
s = readBVarCharOrPanic(memBuf)
t.Fatal("readBVarCharOrPanic() should panic on empty buffer, but it didn't")
}