From c3798729278c55b9094b9a29f76ccf3ac649e46b Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Fri, 27 Oct 2023 21:03:11 -0300 Subject: [PATCH] strconv: add tests and fuzzing --- strconv/decimal_test.go | 16 ++++++++--- strconv/float_test.go | 60 +++++++++++++++++++++++++++++++++++++++++ strconv/int_test.go | 19 +++++++++++++ strconv/number_test.go | 34 +++++++++++++++++++++++ 4 files changed, 126 insertions(+), 3 deletions(-) diff --git a/strconv/decimal_test.go b/strconv/decimal_test.go index e5b3b90..9fbd76b 100644 --- a/strconv/decimal_test.go +++ b/strconv/decimal_test.go @@ -16,14 +16,14 @@ func TestParseDecimal(t *testing.T) { {"5.1", 5.1}, {"0.0000000000000000000000000005", 5e-28}, {"18446744073709551620", 18446744073709551620.0}, - //{"1000000000000000000000000.0000", 1e24}, // TODO - //{"1000000000000000000000000000000000000000000", 1e42}, // TODO + {"1000000000000000000000000.0000", 1e24}, // TODO + {"1000000000000000000000000000000000000000000", 1e42}, // TODO } for _, tt := range tests { t.Run(fmt.Sprint(tt.f), func(t *testing.T) { f, n := ParseDecimal([]byte(tt.f)) test.T(t, n, len(tt.f)) - test.T(t, f, tt.expected) + test.Float(t, f, tt.expected) }) } } @@ -47,3 +47,13 @@ func TestParseDecimalError(t *testing.T) { }) } } + +func FuzzParseDecimal(f *testing.F) { + f.Add("5") + f.Add("5.1") + f.Add("18446744073709551620") + f.Add("0.0000000000000000000000000005") + f.Fuzz(func(t *testing.T, s string) { + ParseDecimal([]byte(s)) + }) +} diff --git a/strconv/float_test.go b/strconv/float_test.go index 213ed77..7ba5757 100644 --- a/strconv/float_test.go +++ b/strconv/float_test.go @@ -115,6 +115,66 @@ func TestAppendFloat(t *testing.T) { test.String(t, string(b[:5]), "12.34", "in buffer") } +func FuzzParseFloat(f *testing.F) { + f.Add("5") + f.Add("99") + f.Add("18446744073709551615") + f.Add("5") + f.Add("5.1") + f.Add("-5.1") + f.Add("5.1e-2") + f.Add("5.1e+2") + f.Add("0.0e1") + f.Add("18446744073709551620") + f.Add("1e23") + f.Fuzz(func(t *testing.T, s string) { + ParseFloat([]byte(s)) + }) +} + +func FuzzAppendFloat(f *testing.F) { + f.Add(0.0, 6) + f.Add(1.0, 6) + f.Add(9.0, 6) + f.Add(9.99999, 6) + f.Add(123.0, 6) + f.Add(0.123456, 6) + f.Add(0.066, 6) + f.Add(0.0066, 6) + f.Add(12e2, 6) + f.Add(12e3, 6) + f.Add(0.1, 6) + f.Add(0.001, 6) + f.Add(0.0001, 6) + f.Add(-1.0, 6) + f.Add(-123.0, 6) + f.Add(-123.456, 6) + f.Add(-12e3, 6) + f.Add(-0.1, 6) + f.Add(-0.0001, 6) + f.Add(0.000100009, 10) + f.Add(0.0001000009, 10) + f.Add(1e18, 0) + f.Add(1e1, 0) + f.Add(1e2, 1) + f.Add(1e3, 2) + f.Add(1e10, -1) + f.Add(1e15, -1) + f.Add(1e-5, 6) + f.Add(math.NaN(), 0) + f.Add(math.Inf(1), 0) + f.Add(math.Inf(-1), 0) + f.Add(0.0, 19) + f.Add(0.000923361977200859392, -1) + f.Add(1234.0, 2) + f.Add(12345.0, 2) + f.Add(12.345, 2) + f.Add(12.345, 3) + f.Fuzz(func(t *testing.T, f float64, prec int) { + AppendFloat([]byte{}, f, prec) + }) +} + //////////////////////////////////////////////////////////////// func TestAppendFloatRandom(t *testing.T) { diff --git a/strconv/int_test.go b/strconv/int_test.go index cadb842..70af543 100644 --- a/strconv/int_test.go +++ b/strconv/int_test.go @@ -53,6 +53,16 @@ func TestParseIntError(t *testing.T) { } } +func FuzzParseInt(f *testing.F) { + f.Add("5") + f.Add("-99") + f.Add("9223372036854775807") + f.Add("-9223372036854775808") + f.Fuzz(func(t *testing.T, s string) { + ParseInt([]byte(s)) + }) +} + func TestParseUint(t *testing.T) { intTests := []struct { i string @@ -91,6 +101,15 @@ func TestParseUintError(t *testing.T) { } } +func FuzzParseUint(f *testing.F) { + f.Add("5") + f.Add("99") + f.Add("18446744073709551615") + f.Fuzz(func(t *testing.T, s string) { + ParseUint([]byte(s)) + }) +} + func TestLenInt(t *testing.T) { lenIntTests := []struct { i int64 diff --git a/strconv/number_test.go b/strconv/number_test.go index b1a09fb..e25a5e4 100644 --- a/strconv/number_test.go +++ b/strconv/number_test.go @@ -62,3 +62,37 @@ func TestAppendNumber(t *testing.T) { // coverage test.String(t, string(AppendNumber(make([]byte, 0, 7), 12345, 1, 3, -1, -1)), "1.234,5") } + +func FuzzParseNumber(f *testing.F) { + f.Add("5") + f.Add("-5") + f.Add("5,0") + f.Add("5,0a") + f.Add("-1000,00") + f.Add("9223372036854775807") + f.Add("-9223372036854775807") + f.Add("-9223372036854775808") + f.Add("92233720368547758070") + f.Add("-92233720368547758080") + f.Fuzz(func(t *testing.T, s string) { + ParseNumber([]byte(s), '.', ',') + }) +} + +func FuzzAppendNumber(f *testing.F) { + f.Add(int64(0), 0) + f.Add(int64(0), -1) + f.Add(int64(0), 2) + f.Add(int64(1), 2) + f.Add(int64(-1), 2) + f.Add(int64(100), 2) + f.Add(int64(-100), 2) + f.Add(int64(1000), 0) + f.Add(int64(100000), 2) + f.Add(int64(123456789012), 2) + f.Add(int64(9223372036854775807), 2) + f.Add(int64(-9223372036854775808), 2) + f.Fuzz(func(t *testing.T, num int64, dec int) { + AppendNumber([]byte{}, num, dec, 3, '.', ',') + }) +}