MCPcopy
hub / github.com/jackc/pgx / FuzzBackend

Function FuzzBackend

pgproto3/fuzz_test.go:59–98  ·  view source on GitHub ↗
(f *testing.F)

Source from the content-addressed store, hash-verified

57}
58
59func FuzzBackend(f *testing.F) {
60 testcases := []struct {
61 msgType byte
62 msgLen uint32
63 msgBody []byte
64 }{
65 {msgType: 'B', msgLen: 14, msgBody: []byte{0, 0, 0, 0, 0, 1, 0, 0, 0, 1}}, // Bind
66 {msgType: 'P', msgLen: 8, msgBody: []byte{0, 0, 0, 0}}, // Parse
67 {msgType: 'Q', msgLen: 6, msgBody: []byte{0}}, // Query
68 {msgType: 'F', msgLen: 18, msgBody: []byte{0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1}}, // FunctionCall
69 }
70 for _, tc := range testcases {
71 f.Add(tc.msgType, tc.msgLen, tc.msgBody)
72 }
73 f.Fuzz(func(t *testing.T, msgType byte, msgLen uint32, msgBody []byte) {
74 // Prune any msgLen > len(msgBody) because they would hang the test waiting for more input.
75 if int(msgLen) > len(msgBody)+4 {
76 return
77 }
78
79 // Prune any messages that are too long.
80 if msgLen > 128 || len(msgBody) > 128 {
81 return
82 }
83
84 r := &bytes.Buffer{}
85 w := &bytes.Buffer{}
86 be := pgproto3.NewBackend(r, w)
87
88 var encodedMsg []byte
89 encodedMsg = append(encodedMsg, msgType)
90 encodedMsg = pgio.AppendUint32(encodedMsg, msgLen)
91 encodedMsg = append(encodedMsg, msgBody...)
92 _, err := r.Write(encodedMsg)
93 require.NoError(t, err)
94
95 // Not checking anything other than no panic.
96 be.Receive()
97 })
98}
99
100// Fuzz individual Decode methods directly. This provides better coverage than
101// going through Frontend/Backend.Receive because there is no message framing

Callers

nothing calls this directly

Calls 4

ReceiveMethod · 0.95
NewBackendFunction · 0.92
AppendUint32Function · 0.92
WriteMethod · 0.45

Tested by

no test coverage detected