forked from go-sql-driver/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress_test.go
220 lines (181 loc) · 6.07 KB
/
compress_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
package mysql
import (
"bytes"
"crypto/rand"
"fmt"
"io"
"testing"
)
func makeRandByteSlice(size int) []byte {
randBytes := make([]byte, size)
rand.Read(randBytes)
return randBytes
}
func newMockConn() *mysqlConn {
newConn := &mysqlConn{}
return newConn
}
type mockBuf struct {
reader io.Reader
}
func newMockBuf(reader io.Reader) *mockBuf {
return &mockBuf{
reader: reader,
}
}
func (mb *mockBuf) readNext(need int) ([]byte, error) {
data := make([]byte, need)
_, err := mb.reader.Read(data)
if err != nil {
return nil, err
}
return data, nil
}
// compressHelper compresses uncompressedPacket and checks state variables
func compressHelper(t *testing.T, mc *mysqlConn, uncompressedPacket []byte) []byte {
// get status variables
cs := mc.compressionSequence
var b bytes.Buffer
connWriter := &b
cw := NewCompressedWriter(connWriter, mc)
n, err := cw.Write(uncompressedPacket)
if err != nil {
t.Fatal(err.Error())
}
if n != len(uncompressedPacket) {
t.Fatal(fmt.Sprintf("expected to write %d bytes, wrote %d bytes", len(uncompressedPacket), n))
}
if len(uncompressedPacket) > 0 {
if mc.compressionSequence != (cs + 1) {
t.Fatal(fmt.Sprintf("mc.compressionSequence updated incorrectly, expected %d and saw %d", (cs + 1), mc.compressionSequence))
}
} else {
if mc.compressionSequence != cs {
t.Fatal(fmt.Sprintf("mc.compressionSequence updated incorrectly for case of empty write, expected %d and saw %d", cs, mc.compressionSequence))
}
}
return b.Bytes()
}
// roundtripHelper compresses then uncompresses uncompressedPacket and checks state variables
func roundtripHelper(t *testing.T, cSend *mysqlConn, cReceive *mysqlConn, uncompressedPacket []byte) []byte {
compressed := compressHelper(t, cSend, uncompressedPacket)
return uncompressHelper(t, cReceive, compressed, len(uncompressedPacket))
}
// uncompressHelper uncompresses compressedPacket and checks state variables
func uncompressHelper(t *testing.T, mc *mysqlConn, compressedPacket []byte, expSize int) []byte {
// get status variables
cs := mc.compressionSequence
// mocking out buf variable
mockConnReader := bytes.NewReader(compressedPacket)
mockBuf := newMockBuf(mockConnReader)
cr := NewCompressedReader(mockBuf, mc)
uncompressedPacket, err := cr.readNext(expSize)
if err != nil {
if err != io.EOF {
t.Fatal(fmt.Sprintf("non-nil/non-EOF error when reading contents: %s", err.Error()))
}
}
if expSize > 0 {
if mc.compressionSequence != (cs + 1) {
t.Fatal(fmt.Sprintf("mc.compressionSequence updated incorrectly, expected %d and saw %d", (cs + 1), mc.compressionSequence))
}
} else {
if mc.compressionSequence != cs {
t.Fatal(fmt.Sprintf("mc.compressionSequence updated incorrectly for case of empty read, expected %d and saw %d", cs, mc.compressionSequence))
}
}
return uncompressedPacket
}
// TestCompressedReaderThenWriter tests reader and writer seperately.
func TestCompressedReaderThenWriter(t *testing.T) {
makeTestUncompressedPacket := func(size int) []byte {
uncompressedHeader := make([]byte, 4)
uncompressedHeader[0] = byte(size)
uncompressedHeader[1] = byte(size >> 8)
uncompressedHeader[2] = byte(size >> 16)
payload := make([]byte, size)
for i := range payload {
payload[i] = 'b'
}
uncompressedPacket := append(uncompressedHeader, payload...)
return uncompressedPacket
}
tests := []struct {
compressed []byte
uncompressed []byte
desc string
}{
{compressed: []byte{5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 'a'},
uncompressed: []byte{1, 0, 0, 0, 'a'},
desc: "a"},
{compressed: []byte{10, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 'g', 'o', 'l', 'a', 'n', 'g'},
uncompressed: []byte{6, 0, 0, 0, 'g', 'o', 'l', 'a', 'n', 'g'},
desc: "golang"},
{compressed: []byte{19, 0, 0, 0, 104, 0, 0, 120, 156, 74, 97, 96, 96, 72, 162, 3, 0, 4, 0, 0, 255, 255, 182, 165, 38, 173},
uncompressed: makeTestUncompressedPacket(100),
desc: "100 bytes letter b"},
{compressed: []byte{63, 0, 0, 0, 236, 128, 0, 120, 156, 236, 192, 129, 0, 0, 0, 8, 3, 176, 179, 70, 18, 110, 24, 129, 124, 187, 77, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 241, 1, 0, 0, 255, 255, 42, 107, 93, 24},
uncompressed: makeTestUncompressedPacket(33000),
desc: "33000 bytes letter b"},
}
for _, test := range tests {
s := fmt.Sprintf("Test compress uncompress with %s", test.desc)
// test uncompression only
c := newMockConn()
uncompressed := uncompressHelper(t, c, test.compressed, len(test.uncompressed))
if bytes.Compare(uncompressed, test.uncompressed) != 0 {
t.Fatal(fmt.Sprintf("%s: uncompression failed", s))
}
// test compression only
c = newMockConn()
compressed := compressHelper(t, c, test.uncompressed)
if bytes.Compare(compressed, test.compressed) != 0 {
t.Fatal(fmt.Sprintf("%s: compression failed", s))
}
}
}
// TestRoundtrip tests two connections, where one is reading and the other is writing
func TestRoundtrip(t *testing.T) {
tests := []struct {
uncompressed []byte
desc string
}{
{uncompressed: []byte("a"),
desc: "a"},
{uncompressed: []byte{0},
desc: "0 byte"},
{uncompressed: []byte("hello world"),
desc: "hello world"},
{uncompressed: make([]byte, 100),
desc: "100 bytes"},
{uncompressed: make([]byte, 32768),
desc: "32768 bytes"},
{uncompressed: make([]byte, 330000),
desc: "33000 bytes"},
{uncompressed: make([]byte, 0),
desc: "nothing"},
{uncompressed: makeRandByteSlice(10),
desc: "10 rand bytes",
},
{uncompressed: makeRandByteSlice(100),
desc: "100 rand bytes",
},
{uncompressed: makeRandByteSlice(32768),
desc: "32768 rand bytes",
},
{uncompressed: makeRandByteSlice(33000),
desc: "33000 rand bytes",
},
}
cSend := newMockConn()
cReceive := newMockConn()
for _, test := range tests {
s := fmt.Sprintf("Test roundtrip with %s", test.desc)
//t.Run(s, func(t *testing.T) {
uncompressed := roundtripHelper(t, cSend, cReceive, test.uncompressed)
if bytes.Compare(uncompressed, test.uncompressed) != 0 {
t.Fatal(fmt.Sprintf("%s: roundtrip failed", s))
}
//})
}
}