Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cb82992

Browse files
committedMay 5, 2022
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 c270dda commit cb82992

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed
 

‎.github/workflows/testing.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ jobs:
5454
- name: Install test dependencies
5555
run: make deps
5656

57-
- name: Run tests
57+
- name: Run regression tests
5858
run: make test
5959

60+
- name: Run fuzzing tests
61+
run: make test-fuzzing
62+
6063
- name: Run tests, collect code coverage data and send to Coveralls
6164
if: ${{ matrix.coveralls }}
6265
env:

‎Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ test-decimal:
5151
go clean -testcache
5252
go test ./decimal/ -v -p 1
5353

54+
.PHONY: test-fuzzing
55+
test-fuzzing:
56+
@echo "Running fuzzing tests"
57+
go clean -testcache
58+
go test ./... -v -p 1 -tags fuzzing
59+
5460
.PHONY: coverage
5561
coverage:
5662
go clean -testcache

‎decimal/fuzzing_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//go:build fuzzing
2+
// +build fuzzing
3+
4+
package decimal_test
5+
6+
import (
7+
"testing"
8+
9+
. "github.com/tarantool/go-tarantool/decimal"
10+
)
11+
12+
func FuzzEncodeDecodeBCD(f *testing.F) {
13+
samples := append(decimalBCDDecode, benchmarkSamples...)
14+
for _, testcase := range samples {
15+
if len(testcase.numString) > 0 {
16+
f.Add(testcase.numString) // Use f.Add to provide a seed corpus.
17+
}
18+
}
19+
f.Fuzz(func(t *testing.T, orig string) {
20+
bcdBuf, err := EncodeStringToBCD(orig)
21+
if err != nil {
22+
return
23+
}
24+
var dec string
25+
dec, err = DecodeStringFromBCD(bcdBuf)
26+
if err != nil {
27+
t.Fatalf("Failed to decode encoded value ('%s')", orig)
28+
}
29+
30+
if dec != orig {
31+
t.Fatalf("Decoded decimal (%s) is not equal to encoded one (%s)", dec, orig)
32+
}
33+
})
34+
}

0 commit comments

Comments
 (0)
Please sign in to comment.