File: scan-test.py

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; 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 (102 lines) | stat: -rw-r--r-- 2,649 bytes parent folder | download | duplicates (12)
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
93
94
95
96
97
98
99
100
101
102
#! /usr/bin/env python
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

"""Testing for the JSON file emitted by DMD heap scan mode when running SmokeDMD."""

import argparse
import gzip
import json
import sys

# The DMD output version this script handles.
outputVersion = 5


def parseCommandLine():
    description = """
Ensure that DMD heap scan mode creates the correct output when run with SmokeDMD.
This is only for testing. Input files can be gzipped.
"""
    p = argparse.ArgumentParser(description=description)

    p.add_argument(
        "--clamp-contents",
        action="store_true",
        help="expect that the contents of the JSON input file have had "
        "their addresses clamped",
    )

    p.add_argument("input_file", help="a file produced by DMD")

    return p.parse_args(sys.argv[1:])


def checkScanContents(contents, expected):
    if len(contents) != len(expected):
        raise Exception(
            "Expected "
            + str(len(expected))
            + " things in contents but found "
            + str(len(contents))
        )

    for i in range(len(expected)):
        if contents[i] != expected[i]:
            raise Exception(
                "Expected to find "
                + expected[i]
                + " at offset "
                + str(i)
                + " but found "
                + contents[i]
            )


def main():
    args = parseCommandLine()

    # Handle gzipped input if necessary.
    isZipped = args.input_file.endswith(".gz")
    opener = gzip.open if isZipped else open

    with opener(args.input_file, "rb") as f:
        j = json.load(f)

    if j["version"] != outputVersion:
        raise Exception(f"'version' property isn't '{outputVersion:d}'")

    invocation = j["invocation"]

    mode = invocation["mode"]
    if mode != "scan":
        raise Exception(f"bad 'mode' property: '{mode:s}'")

    blockList = j["blockList"]

    if len(blockList) != 1:
        raise Exception("Expected only one block")

    b = blockList[0]

    # The expected values are based on hard-coded values in SmokeDMD.cpp.
    if args.clamp_contents:
        expected = ["0", "0", "0", b["addr"], b["addr"]]
    else:
        addr = int(b["addr"], 16)
        expected = [
            "123",
            "0",
            str(format(addr - 1, "x")),
            b["addr"],
            str(format(addr + 1, "x")),
            "0",
        ]

    checkScanContents(b["contents"], expected)


if __name__ == "__main__":
    main()