Skip to content

Commit 624b728

Browse files
liguriooleg-jukovec
andcommitted
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 Co-authored-by: Oleg Jukovec <[email protected]>
1 parent 2c13fdf commit 624b728

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

.github/workflows/testing.yml

+22-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,17 +63,21 @@ 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

6374
- name: Run tests with call_17
6475
run: make test TAGS="go_tarantool_call_17"
6576

77+
- name: Run fuzzing tests
78+
if: ${{ matrix.fuzzing }}
79+
run: make fuzzing TAGS="go_tarantool_decimal_fuzzing"
80+
6681
- name: Run tests, collect code coverage data and send to Coveralls
6782
if: ${{ matrix.coveralls }}
6883
env:
@@ -96,6 +111,7 @@ jobs:
96111
- '1.10.11-0-gf0b0e7ecf-r470'
97112
- '2.8.3-21-g7d35cd2be-r470'
98113
coveralls: [false]
114+
fuzzing: [false]
99115
ssl: [false]
100116
include:
101117
- sdk-version: '2.10.0-1-gfa775b383-r486-linux-x86_64'
@@ -144,6 +160,10 @@ jobs:
144160
env:
145161
TEST_TNT_SSL: ${{matrix.ssl}}
146162

163+
- name: Run fuzzing tests
164+
if: ${{ matrix.fuzzing }}
165+
run: make fuzzing TAGS="go_tarantool_decimal_fuzzing"
166+
147167
- name: Run tests, collect code coverage data and send to Coveralls
148168
if: ${{ matrix.coveralls }}
149169
env:

CONTRIBUTING.md

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ For example, for running tests in `multi`, `uuid` and `main` packages, call
4545
make test-multi test-uuid test-main
4646
```
4747

48+
To run [fuzz tests](https://go.dev/doc/tutorial/fuzz) for the main package and each subpackage:
49+
```bash
50+
make TAGS="go_tarantool_decimal_fuzzing" fuzzing
51+
```
52+
4853
To check if the current changes will pass the linter in CI, install
4954
golangci-lint from [sources](https://golangci-lint.run/usage/install/)
5055
and run it with next command:

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,9 @@ bench-diff: ${BENCH_FILES}
118118
@echo "Comparing performance between master and the current branch"
119119
@echo "'old' is a version in master branch, 'new' is a version in a current branch"
120120
benchstat ${BENCH_FILES} | grep -v pkg:
121+
122+
.PHONY: fuzzing
123+
fuzzing:
124+
@echo "Running fuzzing tests"
125+
go clean -testcache
126+
go test -tags "$(TAGS)" ./... -run=^Fuzz -v -p 1

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ This allows us to introduce new features without losing backward compatibility.
6262
```
6363
go_tarantool_ssl_disable
6464
```
65-
2. to change the default `Call` behavior from `Call16` to `Call17`, you can use the build
66-
tag:
65+
2. To change the default `Call` behavior from `Call16` to `Call17`, you can use
66+
the build tag:
6767
```
6868
go_tarantool_call_17
6969
```
7070
**Note:** In future releases, `Call17` may be used as default `Call` behavior.
71+
3. To run fuzz tests with decimals, you can use the build tag:
72+
```
73+
go_tarantool_decimal_fuzzing
74+
```
75+
**Note:** It crashes old Tarantool versions and requires Go 1.18+.
7176

7277
## Documentation
7378

decimal/fuzzing_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//go:build go_tarantool_decimal_fuzzing
2+
// +build go_tarantool_decimal_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)