File: fuzz.py

package info (click to toggle)
mat2 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,376 kB
  • sloc: python: 3,772; makefile: 7
file content (53 lines) | stat: -rw-r--r-- 1,332 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import mimetypes
import os
import sys

sys.path.append('..')

import atheris

with atheris.instrument_imports(enable_loader_override=False):
    from libmat2 import parser_factory, UNSUPPORTED_EXTENSIONS

extensions = set()
for parser in parser_factory._get_parsers():  # type: ignore
    for mtype in parser.mimetypes:
        if mtype.startswith('video'):
            continue
        if 'aif' in mtype:
            continue
        if 'wav' in mtype:
            continue
        if 'gif' in mtype:
            continue
        if 'aifc' in mtype:
            continue
        for extension in mimetypes.guess_all_extensions(mtype):
            if extension not in UNSUPPORTED_EXTENSIONS:
                extensions.add(extension)
extensions = list(extensions)



def TestOneInput(data):
    fdp = atheris.FuzzedDataProvider(data)
    extension = fdp.PickValueInList(extensions)
    data = fdp.ConsumeBytes(sys.maxsize)

    fname = '/tmp/mat2_fuzz' + extension

    with open(fname, 'wb') as f:
        f.write(data)
    try:
        p, _ = parser_factory.get_parser(fname)
        if p:
            p.get_meta()
            p.remove_all()
            p, _ = parser_factory.get_parser(fname)
            p.get_meta()
    except ValueError:
        pass
    os.remove(fname)

atheris.Setup(sys.argv, TestOneInput)
atheris.Fuzz()