File: test_fuzzing_interface.py

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (92 lines) | stat: -rw-r--r-- 2,725 bytes parent folder | download | duplicates (10)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import os
import os.path
from pathlib import Path
from platform import system
from shutil import copy, move
from subprocess import TimeoutExpired, run

import mozunit
from moztest.selftest import fixtures
from pytest import mark, param, skip

MOZ_AUTOMATION = os.getenv("MOZ_AUTOMATION", "0") == "1"


def _find_ffbin():
    fetch_dir = Path(os.getenv("MOZ_FETCHES_DIR", ""))

    mozinfo_json = fetch_dir / "target.mozinfo.json"
    if mozinfo_json.is_file():
        mozinfo = json.loads(mozinfo_json.read_text())
        if mozinfo["buildtype"] == "tsan":
            # TSan builds not supported, see bug 1592250"
            return None

    ffbin = fixtures.binary().replace("$MOZ_FETCHES_DIR", str(fetch_dir)).strip('"')
    if MOZ_AUTOMATION and (fetch_dir / "gtest" / "gtest_bin" / "gtest").is_dir():
        ffdir = Path(ffbin).parent
        # move gtest deps to required location
        copy(
            str(fetch_dir / "gtest" / "dependentlibs.list.gtest"),
            str(ffdir / "dependentlibs.list.gtest"),
        )
        move(str(fetch_dir / "gtest" / "gtest_bin" / "gtest"), str(ffdir))

    if not os.path.exists(ffbin):
        raise AssertionError("Missing Firefox build. Build it or set GECKO_BINARY_PATH")

    return ffbin


FF_BIN = _find_ffbin() if system() == "Linux" else None


def _available_targets():
    if system() != "Linux" or FF_BIN is None:
        # OS or build type not supported
        return

    try:
        results = run(
            [FF_BIN, "--headless"],
            env={"FUZZER": "list"},
            capture_output=True,
            check=False,
            timeout=5,
        )
    except TimeoutExpired:
        yield param(None, marks=skip(reason="Requires '--enable-fuzzing' build"))
        return

    if results.returncode != 0 and b"Couldn't load XPCOM" in results.stderr:
        yield param(None, marks=skip(reason="Fuzzing interface requires gtests"))
        return
    assert results.returncode == 0, results.stderr

    has_targets = False
    for line in results.stdout.split(b"\n"):
        if not has_targets:
            if line == b"===== Targets =====":
                has_targets = True
            continue
        if has_targets and line == b"===== End of list =====":
            break
        yield param(line.decode())
    assert has_targets, "No fuzzing targets found!"


@mark.parametrize("target", _available_targets())
def test_fuzzing_interface_smoke(tmp_path, target):
    result = run(
        [FF_BIN, str(tmp_path), "-runs=10"],
        env={"FUZZER": target},
        capture_output=True,
        check=False,
        timeout=300,
    )
    assert result.returncode == 0


if __name__ == "__main__":
    mozunit.main()