Add fuzz test

This commit is contained in:
jangko 2023-12-26 09:38:34 +07:00
parent 61bae43e01
commit 1996de769b
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
6 changed files with 101 additions and 4 deletions

3
.gitignore vendored
View File

@ -1,6 +1,7 @@
nimcache
*.exe
nimble.develop
nimble.paths
nimble-win.paths
nimble-linux.paths
build/
vendor/

View File

@ -8,6 +8,14 @@
# those terms.
# begin Nimble config (version 1)
when fileExists("nimble.paths"):
when defined(windows):
when fileExists("nimble-win.paths"):
include "nimble-win.paths"
elif fileExists("nimble.paths"):
include "nimble.paths"
elif defined(linux):
when fileExists("nimble-win.paths"):
include "nimble-linux.paths"
elif fileExists("nimble.paths"):
include "nimble.paths"
# end Nimble config

23
fuzzer/fuzz_lexer.nim Normal file
View File

@ -0,0 +1,23 @@
# json-serialization
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import
testutils/fuzzing,
faststreams,
../json_serialization/lexer
template prepareLexer(T: type, payload: untyped) =
var stream = unsafeMemoryInput(payload)
var lex = init(JsonLexer, stream)
var value: JsonValueRef[T]
lex.scanValue(value)
test:
prepareLexer(string, payload)
prepareLexer(uint64, payload)

29
fuzzer/fuzz_parser.nim Normal file
View File

@ -0,0 +1,29 @@
# json-serialization
# Copyright (c) 2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
import
testutils/fuzzing,
faststreams,
../json_serialization/parser
func toReader(input: openArray[byte]): JsonReader[DefaultFlavor] =
var stream = unsafeMemoryInput(input)
JsonReader[DefaultFlavor].init(stream)
proc executeParser(payload: openArray[byte]) =
try:
var r = toReader(payload)
let z = r.parseValue(uint64)
discard z
except JsonReaderError:
discard
test:
executeParser(payload)

36
fuzzer/readme.md Normal file
View File

@ -0,0 +1,36 @@
## Fuzz test manual
You need to install [testutils](https://github.com/status-im/nim-testutils) and
[chronicles](https://github.com/status-im/nim-chronicles).
Then read documentation over there to prepare your execution environment.
### Compatibility
These fuzzers can be compiled with Nim v1.6.16 or newer.
### Available fuzz test
* fuzz_lexer
* fuzz_parser
### Manually with libFuzzer/llvmFuzer
#### Compiling
```sh
nim c -d:llvmFuzzer -d:release -d:chronicles_log_level=FATAL --noMain --cc=clang --passC="-fsanitize=fuzzer" --passL="-fsanitize=fuzzer" fuzzer/fuzz_lexer
```
#### Starting the Fuzzer
Starting the fuzzer is as simple as running the compiled program:
```sh
./fuzz_lexer corpus_dir -runs=1000000
```
To see the available options:
```sh
./fuzz_lexer test=1
```
You can also use the application to verify a specific test case:
```sh
./fuzz_lexer input_file
```

View File

@ -14,7 +14,7 @@ version = "0.2.0"
author = "Status Research & Development GmbH"
description = "Flexible JSON serialization not relying on run-time type information"
license = "Apache License 2.0"
skipDirs = @["tests"]
skipDirs = @["tests", "fuzzer"]
requires "nim >= 1.6.0",
"serialization",