scan-test.py (2649B)
1 #! /usr/bin/env python 2 # 3 # This Source Code Form is subject to the terms of the Mozilla Public 4 # License, v. 2.0. If a copy of the MPL was not distributed with this 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 7 """Testing for the JSON file emitted by DMD heap scan mode when running SmokeDMD.""" 8 9 import argparse 10 import gzip 11 import json 12 import sys 13 14 # The DMD output version this script handles. 15 outputVersion = 5 16 17 18 def parseCommandLine(): 19 description = """ 20 Ensure that DMD heap scan mode creates the correct output when run with SmokeDMD. 21 This is only for testing. Input files can be gzipped. 22 """ 23 p = argparse.ArgumentParser(description=description) 24 25 p.add_argument( 26 "--clamp-contents", 27 action="store_true", 28 help="expect that the contents of the JSON input file have had " 29 "their addresses clamped", 30 ) 31 32 p.add_argument("input_file", help="a file produced by DMD") 33 34 return p.parse_args(sys.argv[1:]) 35 36 37 def checkScanContents(contents, expected): 38 if len(contents) != len(expected): 39 raise Exception( 40 "Expected " 41 + str(len(expected)) 42 + " things in contents but found " 43 + str(len(contents)) 44 ) 45 46 for i in range(len(expected)): 47 if contents[i] != expected[i]: 48 raise Exception( 49 "Expected to find " 50 + expected[i] 51 + " at offset " 52 + str(i) 53 + " but found " 54 + contents[i] 55 ) 56 57 58 def main(): 59 args = parseCommandLine() 60 61 # Handle gzipped input if necessary. 62 isZipped = args.input_file.endswith(".gz") 63 opener = gzip.open if isZipped else open 64 65 with opener(args.input_file, "rb") as f: 66 j = json.load(f) 67 68 if j["version"] != outputVersion: 69 raise Exception(f"'version' property isn't '{outputVersion:d}'") 70 71 invocation = j["invocation"] 72 73 mode = invocation["mode"] 74 if mode != "scan": 75 raise Exception(f"bad 'mode' property: '{mode:s}'") 76 77 blockList = j["blockList"] 78 79 if len(blockList) != 1: 80 raise Exception("Expected only one block") 81 82 b = blockList[0] 83 84 # The expected values are based on hard-coded values in SmokeDMD.cpp. 85 if args.clamp_contents: 86 expected = ["0", "0", "0", b["addr"], b["addr"]] 87 else: 88 addr = int(b["addr"], 16) 89 expected = [ 90 "123", 91 "0", 92 str(format(addr - 1, "x")), 93 b["addr"], 94 str(format(addr + 1, "x")), 95 "0", 96 ] 97 98 checkScanContents(b["contents"], expected) 99 100 101 if __name__ == "__main__": 102 main()