|
1 |
| -#!/usr/bin/python3 |
| 1 | +#!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import errno
|
4 | 4 | import json
|
|
13 | 13 |
|
14 | 14 | if any(s == "--help" for s in sys.argv):
|
15 | 15 | print("""Usage:
|
16 |
| -GenerateFlowTestCase.py specsToTest.csv projectPom.xml outdir [--force] |
| 16 | +GenerateFlowTestCase.py specsToTest projectPom.xml outdir [--force] |
17 | 17 |
|
18 |
| -This generates test cases exercising function model specifications found in specsToTest.csv |
| 18 | +This generates test cases exercising function model specifications found in specsToTest |
19 | 19 | producing files Test.java, test.ql, test.ext.yml and test.expected in outdir.
|
20 | 20 |
|
| 21 | +specsToTest should either be a .csv file, a .yml file, or a directory of .yml files, containing the |
| 22 | +model specifications to test. |
| 23 | +
|
21 | 24 | projectPom.xml should be a Maven pom sufficient to resolve the classes named in specsToTest.csv.
|
22 | 25 | Typically this means supplying a skeleton POM <dependencies> section that retrieves whatever jars
|
23 | 26 | contain the needed classes.
|
|
40 | 43 |
|
41 | 44 | if len(sys.argv) != 4:
|
42 | 45 | print(
|
43 |
| - "Usage: GenerateFlowTestCase.py specsToTest.csv projectPom.xml outdir [--force]", file=sys.stderr) |
44 |
| - print("specsToTest.csv should contain CSV rows describing method taint-propagation specifications to test", file=sys.stderr) |
45 |
| - print("projectPom.xml should import dependencies sufficient to resolve the types used in specsToTest.csv", file=sys.stderr) |
| 46 | + "Usage: GenerateFlowTestCase.py specsToTest projectPom.xml outdir [--force]", file=sys.stderr) |
| 47 | + print("specsToTest should contain CSV rows or YAML models describing method taint-propagation specifications to test", file=sys.stderr) |
| 48 | + print("projectPom.xml should import dependencies sufficient to resolve the types used in specsToTest", file=sys.stderr) |
| 49 | + print("\nRun with --help for more details.", file=sys.stderr) |
46 | 50 | sys.exit(1)
|
47 | 51 |
|
48 | 52 | try:
|
49 | 53 | os.makedirs(sys.argv[3])
|
50 |
| -except Exception as e: |
| 54 | +except OSError as e: |
51 | 55 | if e.errno != errno.EEXIST:
|
52 | 56 | print("Failed to create output directory %s: %s" % (sys.argv[3], e))
|
53 | 57 | sys.exit(1)
|
|
75 | 79 | (sys.argv[2], e), file=sys.stderr)
|
76 | 80 | sys.exit(1)
|
77 | 81 |
|
78 |
| -commentRegex = re.compile("^\s*(//|#)") |
| 82 | +commentRegex = re.compile(r"^\s*(//|#)") |
79 | 83 |
|
80 | 84 |
|
81 | 85 | def isComment(s):
|
82 | 86 | return commentRegex.match(s) is not None
|
83 | 87 |
|
84 | 88 |
|
85 |
| -try: |
86 |
| - with open(sys.argv[1], "r") as f: |
87 |
| - specs = [l for l in f if not isComment(l)] |
88 |
| -except Exception as e: |
89 |
| - print("Failed to open %s: %s\n" % (sys.argv[1], e)) |
| 89 | +def readCsv(file): |
| 90 | + try: |
| 91 | + with open(file, "r") as f: |
| 92 | + specs = [l.strip() for l in f if not isComment(l)] |
| 93 | + except Exception as e: |
| 94 | + print("Failed to open %s: %s\n" % (file, e)) |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | + specs = [row.split(";") for row in specs] |
| 98 | + return specs |
| 99 | + |
| 100 | + |
| 101 | +def readYml(file): |
| 102 | + try: |
| 103 | + import yaml |
| 104 | + with open(file, "r") as f: |
| 105 | + doc = yaml.load(f.read(), yaml.Loader) |
| 106 | + specs = [] |
| 107 | + for ext in doc['extensions']: |
| 108 | + if ext['addsTo']['extensible'] == 'summaryModel': |
| 109 | + for row in ext['data']: |
| 110 | + if isinstance(row[2], bool): |
| 111 | + row[2] = str(row[2]).lower() |
| 112 | + specs.append(row) |
| 113 | + return specs |
| 114 | + except ImportError: |
| 115 | + print("PyYAML not found - try \n pip install pyyaml") |
| 116 | + sys.exit(1) |
| 117 | + except ValueError as e: |
| 118 | + print("Invalid yaml model in %s: %s\n" % (file, e)) |
| 119 | + sys.exit(1) |
| 120 | + except OSError as e: |
| 121 | + print("Failed to open %s: %s\n" % (file, e)) |
| 122 | + sys.exit(1) |
| 123 | + |
| 124 | + |
| 125 | +def readYmlDir(dirname): |
| 126 | + specs = [] |
| 127 | + for f in os.listdir(dirname): |
| 128 | + if f.endswith('.yml'): |
| 129 | + specs += readYml(f"{dirname}/{f}") |
| 130 | + return specs |
| 131 | + |
| 132 | + |
| 133 | +specsFile = sys.argv[1] |
| 134 | +if os.path.isdir(specsFile): |
| 135 | + specs = readYmlDir(specsFile) |
| 136 | +elif specsFile.endswith(".yml") or specsFile.endswith(".yaml"): |
| 137 | + specs = readYml(specsFile) |
| 138 | +elif specsFile.endswith(".csv"): |
| 139 | + specs = readCsv(specsFile) |
| 140 | +else: |
| 141 | + print(f"Invalid specs {specsFile}. Must be a csv file, a yml file, or a directory of yml files.") |
90 | 142 | sys.exit(1)
|
91 | 143 |
|
| 144 | + |
92 | 145 | projectTestPkgDir = os.path.join(projectDir, "src", "main", "java", "test")
|
93 | 146 | projectTestFile = os.path.join(projectTestPkgDir, "Test.java")
|
94 | 147 |
|
95 | 148 | os.makedirs(projectTestPkgDir)
|
96 | 149 |
|
97 | 150 |
|
98 |
| -def qualifiedOuterNameFromCsvRow(row): |
99 |
| - cells = row.split(";") |
100 |
| - if len(cells) < 2: |
| 151 | +def qualifiedOuterNameFromRow(row): |
| 152 | + if len(row) < 2: |
101 | 153 | return None
|
102 |
| - return cells[0] + "." + cells[1].replace("$", ".") |
| 154 | + return row[0] + "." + row[1].replace("$", ".") |
103 | 155 |
|
104 | 156 |
|
105 | 157 | with open(projectTestFile, "w") as testJava:
|
106 | 158 | testJava.write("package test;\n\npublic class Test {\n\n")
|
107 | 159 |
|
108 | 160 | for i, spec in enumerate(specs):
|
109 |
| - outerName = qualifiedOuterNameFromCsvRow(spec) |
| 161 | + outerName = qualifiedOuterNameFromRow(spec) |
110 | 162 | if outerName is None:
|
111 | 163 | print("A taint specification has the wrong format: should be 'package;classname;methodname....'", file=sys.stderr)
|
112 | 164 | print("Mis-formatted row: " + spec, file=sys.stderr)
|
@@ -140,7 +192,7 @@ def qualifiedOuterNameFromCsvRow(row):
|
140 | 192 | with open(qlFile, "w") as f:
|
141 | 193 | f.write(
|
142 | 194 | "import java\nimport utils.flowtestcasegenerator.GenerateFlowTestCase\n\nclass GenRow extends TargetSummaryModelCsv {\n\n\toverride predicate row(string r) {\n\t\tr = [\n")
|
143 |
| - f.write(",\n".join('\t\t\t"%s"' % spec.strip() for spec in specs)) |
| 195 | + f.write(",\n".join('\t\t\t"%s"' % ';'.join(spec) for spec in specs)) |
144 | 196 | f.write("\n\t\t]\n\t}\n}\n")
|
145 | 197 |
|
146 | 198 | print("Generating tests")
|
@@ -221,7 +273,7 @@ def copyfile(fromName, toFileHandle):
|
221 | 273 | # Make a test extension file
|
222 | 274 | with open(resultYml, "w") as f:
|
223 | 275 | models = "\n".join(' - [%s]' %
|
224 |
| - modelSpecRow[0].strip() for modelSpecRow in supportModelRows) |
| 276 | + modelSpecRow[0].strip() for modelSpecRow in supportModelRows) |
225 | 277 | dataextensions = f"""extensions:
|
226 | 278 | - addsTo:
|
227 | 279 | pack: codeql/java-tests
|
|
0 commit comments