File: fuzz.py

package info (click to toggle)
blueprint-compiler 0.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,392 kB
  • sloc: python: 9,822; sh: 31; makefile: 6
file content (61 lines) | stat: -rw-r--r-- 1,540 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
54
55
56
57
58
59
60
61
import os
import sys

from pythonfuzz.main import PythonFuzz

from blueprintcompiler.outputs.xml import XmlOutput

sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

from blueprintcompiler import gir, parser, tokenizer
from blueprintcompiler.completions import complete
from blueprintcompiler.errors import (
    CompilerBugError,
    PrintableError,
)
from blueprintcompiler.linter import lint
from blueprintcompiler.lsp import LanguageServer

fuzz_level = int(os.getenv("FUZZ_LEVEL") or "0")


@PythonFuzz
def fuzz(buf: bytes):
    try:
        blueprint = buf.decode("ascii")

        tokens = tokenizer.tokenize(blueprint)
        ast, errors, warnings = parser.parse(tokens)

        if fuzz_level >= 1:
            assert_ast_doesnt_crash(blueprint, tokens, ast)

        xml = XmlOutput()
        if errors is None and ast is not None:
            xml.emit(ast)
    except CompilerBugError as e:
        raise e
    except PrintableError:
        pass
    except UnicodeDecodeError:
        pass


def assert_ast_doesnt_crash(text, tokens, ast):
    lsp = LanguageServer()
    for i in range(len(text) + 1):
        ast.get_docs(i)
    for i in range(len(text) + 1):
        list(complete(lsp, ast, tokens, i))
    for i in range(len(text) + 1):
        ast.get_reference(i)
    ast.get_document_symbols()
    lint(ast)


if __name__ == "__main__":
    # Make sure Gtk 4.0 is accessible, otherwise every test will fail on that
    # and nothing interesting will be tested
    gir.get_namespace("Gtk", "4.0")

    fuzz()