| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
import os |
| 7 |
import sys |
| 8 |
import binwalk |
| 9 |
|
| 10 |
test_script_template = """ |
| 11 |
import os |
| 12 |
import binwalk |
| 13 |
|
| 14 |
def test_%s(): |
| 15 |
''' |
| 16 |
Test: Open %s, scan for signatures |
| 17 |
verify that all (and only) expected signatures are detected |
| 18 |
''' |
| 19 |
expected_results = [ |
| 20 |
%s |
| 21 |
] |
| 22 |
|
| 23 |
input_vector_file = os.path.join(os.path.dirname(__file__), |
| 24 |
"input-vectors", |
| 25 |
"%s") |
| 26 |
|
| 27 |
scan_result = binwalk.scan(input_vector_file, |
| 28 |
signature=True, |
| 29 |
quiet=True) |
| 30 |
|
| 31 |
# Test number of modules used |
| 32 |
assert len(scan_result) == 1 |
| 33 |
|
| 34 |
# Test number of results for that module |
| 35 |
assert len(scan_result[0].results) == len(expected_results) |
| 36 |
|
| 37 |
# Test result-description |
| 38 |
for result, (expected_offset, expected_description) in zip(scan_result[0].results, expected_results): |
| 39 |
assert result.offset == expected_offset |
| 40 |
assert result.description == expected_description |
| 41 |
""" |
| 42 |
|
| 43 |
def main(): |
| 44 |
try: |
| 45 |
target_file = sys.argv[1] |
| 46 |
except IndexError: |
| 47 |
sys.stderr.write("Usage: %s <input vector file>\n" % sys.argv[0]) |
| 48 |
sys.exit(1) |
| 49 |
|
| 50 |
target_file_basename = os.path.basename(target_file) |
| 51 |
scan_function_name = target_file_basename.replace('.', '_').replace('-', '_') |
| 52 |
expected_results = "" |
| 53 |
|
| 54 |
signature = binwalk.scan(target_file, signature=True, term=True)[0] |
| 55 |
for result in signature.results: |
| 56 |
expected_results += "\t[%d, %r],\n" % (result.offset, result.description) |
| 57 |
|
| 58 |
test_script = test_script_template % (scan_function_name, |
| 59 |
target_file_basename, |
| 60 |
expected_results, |
| 61 |
target_file_basename) |
| 62 |
|
| 63 |
test_script_path = os.path.join("tests", "test_%s.py" % scan_function_name) |
| 64 |
|
| 65 |
with open(test_script_path, "w") as fp: |
| 66 |
fp.write(test_script) |
| 67 |
|
| 68 |
sys.stdout.write("Generated test script for '%s' and saved it to '%s'\n" % (target_file, test_script_path)) |
| 69 |
|
| 70 |
|
| 71 |
if __name__ == '__main__': |
| 72 |
main() |