Skip to content

Commit e849989

Browse files
committed
re-implement TestReadPacketFail
1 parent 1c6e980 commit e849989

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

packets.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ func (mc *mysqlConn) readPacket(ctx context.Context) ([]byte, error) {
3434
// read packet header
3535
err := mc.readFull(ctx, mc.data[:4])
3636
if err != nil {
37+
if err == context.Canceled || err == context.DeadlineExceeded {
38+
return nil, err
39+
}
3740
mc.cfg.Logger.Print(err)
3841
mc.closeContext(ctx)
39-
return nil, err
42+
return nil, ErrInvalidConn
4043
}
4144

4245
// packet length [24 bit]
@@ -69,6 +72,9 @@ func (mc *mysqlConn) readPacket(ctx context.Context) ([]byte, error) {
6972
data := make([]byte, pktLen)
7073
err = mc.readFull(ctx, data)
7174
if err != nil {
75+
if err == context.Canceled || err == context.DeadlineExceeded {
76+
return nil, err
77+
}
7278
mc.cfg.Logger.Print(err)
7379
mc.closeContext(ctx)
7480
return nil, ErrInvalidConn

packets_test.go

+39-36
Original file line numberDiff line numberDiff line change
@@ -226,47 +226,50 @@ func TestReadPacketSplit(t *testing.T) {
226226
})
227227
}
228228

229-
// func TestReadPacketFail(t *testing.T) {
230-
// conn := new(mockConn)
231-
// mc := &mysqlConn{
232-
// buf: newBuffer(conn),
233-
// closech: make(chan struct{}),
234-
// cfg: NewConfig(),
235-
// }
229+
func TestReadPacketFail(t *testing.T) {
230+
t.Run("illegal empty (stand-alone) packet", func(t *testing.T) {
231+
conn, mc := newRWMockConn(t, 0)
232+
go func() {
233+
conn.Write([]byte{0x00, 0x00, 0x00, 0x00})
234+
}()
235+
go func() {
236+
io.Copy(io.Discard, conn)
237+
}()
236238

237-
// // illegal empty (stand-alone) packet
238-
// conn.data = []byte{0x00, 0x00, 0x00, 0x00}
239-
// conn.maxReads = 1
240-
// _, err := mc.readPacket()
241-
// if err != ErrInvalidConn {
242-
// t.Errorf("expected ErrInvalidConn, got %v", err)
243-
// }
239+
_, err := mc.readPacket(context.Background())
240+
if err != ErrInvalidConn {
241+
t.Errorf("expected ErrInvalidConn, got %v", err)
242+
}
243+
})
244244

245-
// // reset
246-
// conn.reads = 0
247-
// mc.sequence = 0
248-
// mc.buf = newBuffer(conn)
245+
t.Run("fail to read header", func(t *testing.T) {
246+
conn, mc := newRWMockConn(t, 0)
247+
go func() {
248+
conn.Close()
249+
}()
249250

250-
// // fail to read header
251-
// conn.closed = true
252-
// _, err = mc.readPacket()
253-
// if err != ErrInvalidConn {
254-
// t.Errorf("expected ErrInvalidConn, got %v", err)
255-
// }
251+
_, err := mc.readPacket(context.Background())
252+
if err != ErrInvalidConn {
253+
t.Errorf("expected ErrInvalidConn, got %v", err)
254+
}
255+
})
256256

257-
// // reset
258-
// conn.closed = false
259-
// conn.reads = 0
260-
// mc.sequence = 0
261-
// mc.buf = newBuffer(conn)
257+
t.Run("fail to read body", func(t *testing.T) {
258+
conn, mc := newRWMockConn(t, 0)
259+
go func() {
260+
conn.Write([]byte{0x01, 0x00, 0x00, 0x00})
261+
conn.Close()
262+
}()
263+
go func() {
264+
io.Copy(io.Discard, conn)
265+
}()
262266

263-
// // fail to read body
264-
// conn.maxReads = 1
265-
// _, err = mc.readPacket()
266-
// if err != ErrInvalidConn {
267-
// t.Errorf("expected ErrInvalidConn, got %v", err)
268-
// }
269-
// }
267+
_, err := mc.readPacket(context.Background())
268+
if err != ErrInvalidConn {
269+
t.Errorf("expected ErrInvalidConn, got %v", err)
270+
}
271+
})
272+
}
270273

271274
// // https://github.com/go-sql-driver/mysql/pull/801
272275
// // not-NUL terminated plugin_name in init packet

0 commit comments

Comments
 (0)