diff options
| author | jvoisin | 2021-12-19 22:37:45 +0100 |
|---|---|---|
| committer | jvoisin | 2021-12-19 22:37:45 +0100 |
| commit | 48680b985288048bb38116108c0b55acce23b6b9 (patch) | |
| tree | c3a9c6bf6074593e6516eb4af898e67809de5697 /tests/fuzz.py | |
| parent | d555a02c90e3e5582d3d5e78ff89ba7fbf806afc (diff) | |
Add a fuzzer based on atheris
Diffstat (limited to 'tests/fuzz.py')
| -rw-r--r-- | tests/fuzz.py | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/fuzz.py b/tests/fuzz.py new file mode 100644 index 0000000..a12f92d --- /dev/null +++ b/tests/fuzz.py | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | import mimetypes | ||
| 2 | import os | ||
| 3 | import sys | ||
| 4 | |||
| 5 | sys.path.append('..') | ||
| 6 | |||
| 7 | import atheris | ||
| 8 | |||
| 9 | with atheris.instrument_imports(enable_loader_override=False): | ||
| 10 | from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS | ||
| 11 | |||
| 12 | extensions = set() | ||
| 13 | for parser in parser_factory._get_parsers(): # type: ignore | ||
| 14 | for mtype in parser.mimetypes: | ||
| 15 | if mtype.startswith('video'): | ||
| 16 | continue | ||
| 17 | if 'aif' in mtype: | ||
| 18 | continue | ||
| 19 | if 'wav' in mtype: | ||
| 20 | continue | ||
| 21 | if 'gif' in mtype: | ||
| 22 | continue | ||
| 23 | if 'aifc' in mtype: | ||
| 24 | continue | ||
| 25 | for extension in mimetypes.guess_all_extensions(mtype): | ||
| 26 | if extension not in UNSUPPORTED_EXTENSIONS: | ||
| 27 | extensions.add(extension) | ||
| 28 | extensions = list(extensions) | ||
| 29 | |||
| 30 | |||
| 31 | |||
| 32 | def TestOneInput(data): | ||
| 33 | fdp = atheris.FuzzedDataProvider(data) | ||
| 34 | extension = fdp.PickValueInList(extensions) | ||
| 35 | data = fdp.ConsumeBytes(sys.maxsize) | ||
| 36 | |||
| 37 | fname = '/tmp/mat2_fuzz' + extension | ||
| 38 | |||
| 39 | with open(fname, 'wb') as f: | ||
| 40 | f.write(data) | ||
| 41 | try: | ||
| 42 | p, _ = parser_factory.get_parser(fname) | ||
| 43 | if p: | ||
| 44 | p.sandbox = False | ||
| 45 | p.get_meta() | ||
| 46 | p.remove_all() | ||
| 47 | p, _ = parser_factory.get_parser(fname) | ||
| 48 | p.get_meta() | ||
| 49 | except ValueError: | ||
| 50 | pass | ||
| 51 | os.remove(fname) | ||
| 52 | |||
| 53 | atheris.Setup(sys.argv, TestOneInput) | ||
| 54 | atheris.Fuzz() | ||
