spec_tests.py: Add option to generate fuzz corpus

Add an option `--fuzz-corpus` that writes the test cases to separate
files including the options header, so they can be used as seed corpus
for fuzz testing.
This commit is contained in:
Nick Wellnhofer 2023-02-02 12:45:23 +01:00 committed by John MacFarlane
parent 587e0774ff
commit dfb1526b38

View File

@ -6,6 +6,7 @@ from difflib import unified_diff
import argparse import argparse
import re import re
import json import json
import os
from cmark import CMark from cmark import CMark
from normalize import normalize_html from normalize import normalize_html
@ -29,6 +30,8 @@ parser.add_argument('--debug-normalization', dest='debug_normalization',
default=False, help='filter stdin through normalizer for testing') default=False, help='filter stdin through normalizer for testing')
parser.add_argument('-n', '--number', type=int, default=None, parser.add_argument('-n', '--number', type=int, default=None,
help='only consider the test with the given number') help='only consider the test with the given number')
parser.add_argument('--fuzz-corpus',
help='convert test cases to fuzz corpus')
args = parser.parse_args(sys.argv[1:]) args = parser.parse_args(sys.argv[1:])
def out(str): def out(str):
@ -124,6 +127,19 @@ if __name__ == "__main__":
exit(0) exit(0)
all_tests = get_tests(args.spec) all_tests = get_tests(args.spec)
if args.fuzz_corpus:
i = 1
base = os.path.basename(args.spec)
(name, ext) = os.path.splitext(base)
for test in all_tests:
filename = os.path.join(args.fuzz_corpus, '%s.%d' % (name, i))
with open(filename, 'wb') as f:
f.write(b'\0' * 8) # options header
f.write(test['markdown'].encode())
i += 1
exit(0)
if args.pattern: if args.pattern:
pattern_re = re.compile(args.pattern, re.IGNORECASE) pattern_re = re.compile(args.pattern, re.IGNORECASE)
else: else: