Skip to content

Commit 3473aa3

Browse files
committed
decimal: add fuzzing test
Fuzzing tests in Golang, see [1] and [2], requires Go 1.18+. However in CI we use Go 1.13 that fails on running fuzzing tests. To avoid this fuzzing test has been moved to a separate file an marked with build tag. 1. https://go.dev/doc/tutorial/fuzz 2. https://go.dev/doc/fuzz/ Closes #96
1 parent e5d09e4 commit 3473aa3

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

.github/workflows/testing.yml

+17-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@ jobs:
2626
strategy:
2727
fail-fast: false
2828
matrix:
29+
golang:
30+
- 1.13
2931
tarantool:
3032
- '1.10'
3133
- '2.8'
3234
- '2.9'
3335
- '2.x-latest'
3436
coveralls: [false]
37+
fuzzing: [false]
38+
include:
39+
- tarantool: '2.x-latest'
40+
coveralls: true
41+
golang: 1.13
42+
- tarantool: '2.x-latest'
43+
fuzzing: true
44+
golang: 1.18
45+
coveralls: false
3546

3647
steps:
3748
- name: Clone the connector
@@ -52,14 +63,18 @@ jobs:
5263
- name: Setup golang for the connector and tests
5364
uses: actions/setup-go@v2
5465
with:
55-
go-version: 1.13
66+
go-version: ${{ matrix.golang }}
5667

5768
- name: Install test dependencies
5869
run: make deps
5970

60-
- name: Run tests
71+
- name: Run regression tests
6172
run: make test
6273

74+
- name: Run fuzzing tests
75+
if: ${{ matrix.fuzzing }}
76+
run: make test-fuzzing
77+
6378
- name: Run tests, collect code coverage data and send to Coveralls
6479
if: ${{ matrix.coveralls }}
6580
env:

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ test-decimal:
7676
go clean -testcache
7777
go test ./decimal/ -v -p 1
7878

79+
.PHONY: test-fuzzing
80+
test-fuzzing:
81+
@echo "Running fuzzing tests"
82+
go clean -testcache
83+
go test ./... -run=^Fuzz -v -p 1 -tags fuzzing
84+
7985
.PHONY: coverage
8086
coverage:
8187
go clean -testcache

decimal/fuzzing_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//go:build fuzzing
2+
// +build fuzzing
3+
4+
package decimal_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/shopspring/decimal"
10+
. "github.com/tarantool/go-tarantool/decimal"
11+
)
12+
13+
func strToDecimal(t *testing.T, buf string) decimal.Decimal {
14+
decNum, err := decimal.NewFromString(buf)
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
return decNum
19+
}
20+
21+
func FuzzEncodeDecodeBCD(f *testing.F) {
22+
samples := append(correctnessSamples, benchmarkSamples...)
23+
for _, testcase := range samples {
24+
if len(testcase.numString) > 0 {
25+
f.Add(testcase.numString) // Use f.Add to provide a seed corpus.
26+
}
27+
}
28+
f.Fuzz(func(t *testing.T, orig string) {
29+
if l := GetNumberLength(orig); l > DecimalPrecision {
30+
t.Skip("max number length is exceeded")
31+
}
32+
bcdBuf, err := EncodeStringToBCD(orig)
33+
if err != nil {
34+
t.Skip("Only correct requests are interesting: %w", err)
35+
}
36+
var dec string
37+
dec, err = DecodeStringFromBCD(bcdBuf)
38+
if err != nil {
39+
t.Fatalf("Failed to decode encoded value ('%s')", orig)
40+
}
41+
42+
if !strToDecimal(t, dec).Equal(strToDecimal(t, orig)) {
43+
t.Fatal("Decimal numbers are not equal")
44+
}
45+
})
46+
}

0 commit comments

Comments
 (0)