Skip to content

Commit d71ae52

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 24a8135 commit d71ae52

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

.github/workflows/testing.yml

+14-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,23 @@ jobs:
2020
strategy:
2121
fail-fast: false
2222
matrix:
23+
golang:
24+
- 1.13
2325
tarantool:
2426
- '1.10'
2527
- '2.8'
2628
- '2.9'
2729
- '2.x-latest'
2830
coveralls: [false]
31+
fuzzing: [false]
2932
include:
3033
- tarantool: '2.x-latest'
3134
coveralls: true
35+
golang: 1.13
36+
- tarantool: '2.x-latest'
37+
fuzzing: true
38+
golang: 1.18
39+
coveralls: false
3240

3341
steps:
3442
- name: Clone the connector
@@ -49,14 +57,18 @@ jobs:
4957
- name: Setup golang for the connector and tests
5058
uses: actions/setup-go@v2
5159
with:
52-
go-version: 1.13
60+
go-version: ${{ matrix.golang }}
5361

5462
- name: Install test dependencies
5563
run: make deps
5664

57-
- name: Run tests
65+
- name: Run regression tests
5866
run: make test
5967

68+
- name: Run fuzzing tests
69+
if: ${{ matrix.fuzzing }}
70+
run: make test-fuzzing
71+
6072
- name: Run tests, collect code coverage data and send to Coveralls
6173
if: ${{ matrix.coveralls }}
6274
env:

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ test-decimal:
6464
go clean -testcache
6565
go test ./decimal/ -v -p 1
6666

67+
.PHONY: test-fuzzing
68+
test-fuzzing:
69+
@echo "Running fuzzing tests"
70+
go clean -testcache
71+
go test ./... -run=^Fuzz -v -p 1 -tags fuzzing
72+
6773
.PHONY: coverage
6874
coverage:
6975
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 intresting: %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)