github.com/jackc/pgx/v5@v5.5.5/pgproto3/fuzz_test.go (about) 1 package pgproto3_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/jackc/pgx/v5/internal/pgio" 8 "github.com/jackc/pgx/v5/pgproto3" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func FuzzFrontend(f *testing.F) { 13 testcases := []struct { 14 msgType byte 15 msgLen uint32 16 msgBody []byte 17 }{ 18 { 19 msgType: 'Z', 20 msgLen: 2, 21 msgBody: []byte{'I'}, 22 }, 23 { 24 msgType: 'Z', 25 msgLen: 5, 26 msgBody: []byte{'I'}, 27 }, 28 } 29 for _, tc := range testcases { 30 f.Add(tc.msgType, tc.msgLen, tc.msgBody) 31 } 32 f.Fuzz(func(t *testing.T, msgType byte, msgLen uint32, msgBody []byte) { 33 // Prune any msgLen > len(msgBody) because they would hang the test waiting for more input. 34 if int(msgLen) > len(msgBody)+4 { 35 return 36 } 37 38 // Prune any messages that are too long. 39 if msgLen > 128 || len(msgBody) > 128 { 40 return 41 } 42 43 r := &bytes.Buffer{} 44 w := &bytes.Buffer{} 45 fe := pgproto3.NewFrontend(r, w) 46 47 var encodedMsg []byte 48 encodedMsg = append(encodedMsg, msgType) 49 encodedMsg = pgio.AppendUint32(encodedMsg, msgLen) 50 encodedMsg = append(encodedMsg, msgBody...) 51 _, err := r.Write(encodedMsg) 52 require.NoError(t, err) 53 54 // Not checking anything other than no panic. 55 fe.Receive() 56 }) 57 }