Skip to content

Commit 42c35f2

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/ Part of #96
1 parent 225dbb6 commit 42c35f2

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Makefile

+6
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

+34
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)