MCPcopy
hub / github.com/go-sql-driver/mysql / TestReadPacketSplit

Function TestReadPacketSplit

packets_test.go:166–271  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

164}
165
166func TestReadPacketSplit(t *testing.T) {
167 conn := new(mockConn)
168 mc := &mysqlConn{
169 netConn: conn,
170 buf: newBuffer(),
171 cfg: NewConfig(),
172 }
173
174 data := make([]byte, maxPacketSize*2+4*3)
175 const pkt2ofs = maxPacketSize + 4
176 const pkt3ofs = 2 * (maxPacketSize + 4)
177
178 // case 1: payload has length maxPacketSize
179 data = data[:pkt2ofs+4]
180
181 // 1st packet has maxPacketSize length and sequence id 0
182 // ff ff ff 00 ...
183 data[0] = 0xff
184 data[1] = 0xff
185 data[2] = 0xff
186
187 // mark the payload start and end of 1st packet so that we can check if the
188 // content was correctly appended
189 data[4] = 0x11
190 data[maxPacketSize+3] = 0x22
191
192 // 2nd packet has payload length 0 and sequence id 1
193 // 00 00 00 01
194 data[pkt2ofs+3] = 0x01
195
196 conn.data = data
197 conn.maxReads = 3
198 packet, err := mc.readPacket()
199 if err != nil {
200 t.Fatal(err)
201 }
202 if len(packet) != maxPacketSize {
203 t.Fatalf("unexpected packet length: expected %d, got %d", maxPacketSize, len(packet))
204 }
205 if packet[0] != 0x11 {
206 t.Fatalf("unexpected payload start: expected %x, got %x", 0x11, packet[0])
207 }
208 if packet[maxPacketSize-1] != 0x22 {
209 t.Fatalf("unexpected payload end: expected %x, got %x", 0x22, packet[maxPacketSize-1])
210 }
211
212 // case 2: payload has length which is a multiple of maxPacketSize
213 data = data[:cap(data)]
214
215 // 2nd packet now has maxPacketSize length
216 data[pkt2ofs] = 0xff
217 data[pkt2ofs+1] = 0xff
218 data[pkt2ofs+2] = 0xff
219
220 // mark the payload start and end of the 2nd packet
221 data[pkt2ofs+4] = 0x33
222 data[pkt2ofs+maxPacketSize+3] = 0x44
223

Callers

nothing calls this directly

Calls 3

readPacketMethod · 0.95
newBufferFunction · 0.85
NewConfigFunction · 0.85

Tested by

no test coverage detected