From 1292b5c9011a5689c2684937a4f6e47df3a2834d Mon Sep 17 00:00:00 2001 From: Arjun Date: Mon, 4 Nov 2024 09:20:53 +0530 Subject: [PATCH] Add fuzz testing --- Makefile | 4 ++++ message_test.go | 24 ++++++++++++++++++++++++ parser_test.go | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/Makefile b/Makefile index 8d3c2c6cc..4b4a8d96c 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,10 @@ vet: test: MONGODB_TEST_CXN=mongodb://db:27017 go test -v -cover `go list ./... | grep -v quickfix/gen` +fuzz: + go test -fuzztime 600s -fuzz=FuzzParser . + go test -fuzztime 600s -fuzz=FuzzParseMessage . + linters-install: @golangci-lint --version >/dev/null 2>&1 || { \ echo "installing linting tools..."; \ diff --git a/message_test.go b/message_test.go index b02508cd9..9973a11f9 100644 --- a/message_test.go +++ b/message_test.go @@ -515,3 +515,27 @@ func checkFieldString(s *MessageSuite, fields FieldMap, tag int, expected string s.NoError(err) s.Equal(expected, toCheck) } + +func FuzzParseMessage(f *testing.F) { + + rawMsg0 := "8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039" + rawMsg1 := "8=FIX.4.29=37235=n34=25512369=148152=20200522-07:05:33.75649=CME50=G56=OAEAAAN57=TRADE_CAPTURE143=US,IL212=261213=8=FIX.4.29=22535=BZ34=6549369=651852=20200522-07:05:33.74649=CME50=G56=9Q5000N57=DUMMY143=US,IL11=ACP159013113373460=20200522-07:05:33.734533=0893=Y1028=Y1300=991369=99612:325081373=31374=91375=15979=159013113373461769710=16710=245\"" + rawMsg2 := "8=FIX.4.29=10435=D34=249=TW52=20140515-19:49:56.65956=ISLD11=10021=140=154=155=TSLA60=00010101-00:00:00.00010=039" + rawMsg3 := "8=FIX.4.29=12635=D34=249=TW52=20140515-19:49:56.65956=ISLD10030=CUST11=10021=140=154=155=TSLA60=00010101-00:00:00.0005050=HELLO10=039" + rawMsg4 := "8=FIX.4.09=8135=D11=id21=338=10040=154=155=MSFT34=249=TW52=20140521-22:07:0956=ISLD10=250" + + f.Add(rawMsg0) + f.Add(rawMsg1) + f.Add(rawMsg2) + f.Add(rawMsg3) + f.Add(rawMsg4) + + f.Fuzz(func(_ *testing.T, input string) { + if len(input) < 8 { + return + } + + msg := NewMessage() + _ = ParseMessage(msg, bytes.NewBufferString(input)) + }) +} diff --git a/parser_test.go b/parser_test.go index 34bca808d..e643bfcf9 100644 --- a/parser_test.go +++ b/parser_test.go @@ -186,3 +186,25 @@ func (s *ParserSuite) TestReadMessageGrowBuffer() { s.Equal(tc.expectedBufferLen, len(s.parser.buffer)) } } + +func FuzzParser(f *testing.F) { + + stream0 := "8=FIXT.1.19=11135=D34=449=TW52=20140511-23:10:3456=ISLD11=ID21=340=154=155=INTC60=20140511-23:10:3410=2348=FIXT.1.19=9535=D34=549=TW52=20140511-23:10:3456=ISLD11=ID21=340=154=155=INTC60=20140511-23:10:3410=198" + stream1 := "8=\x019=\x01" + stream2 := "8=\x019=9300000000000000000\x01" + stream3 := "hello8=FIX.4.09=5blah10=1038=FIX.4.09=4foo10=103" + + f.Add(stream0) + f.Add(stream1) + f.Add(stream2) + f.Add(stream3) + + f.Fuzz(func(_ *testing.T, input string) { + if len(input) < 8 { + return + } + + parser := newParser(strings.NewReader(input)) + _, _ = parser.ReadMessage() + }) +}