Commit 42374f58 by Craig Heffner

Added new tests and a signature test script generator.

parent 51f2dc48
......@@ -297,7 +297,7 @@ class TestCommand(Command):
# directory with a bunch of .pyc files and b) will fail anyway
# unless a build/install has already been run which creates
# the version.py file.
testing_directory = os.path.join(MODULE_DIRECTORY, "tests")
testing_directory = os.path.join(MODULE_DIRECTORY, "testing", "tests")
os.chdir(testing_directory)
# Run the tests
......
#!/usr/bin/env python
# Automatically generates a binwalk signature test script for
# a given input vector file. The test script will be written
# to the tests directory, and will expect the input vector file
# to be located in the tests/input-vectors/ directory.
import os
import sys
import binwalk
test_script_template = """
import binwalk
from os.path import dirname
from nose.tools import eq_, ok_
def test_%s():
'''
Test: Open %s, scan for signatures
verify that all (and only) expected signatures are detected
'''
expected_results = [
%s
]
scan_result = binwalk.scan(dirname(__file__) + '/input-vectors/%s',
signature=True,
quiet=True)
# Test number of modules used
eq_(len(scan_result), 1)
# Test number of results for that module
eq_(len(scan_result[0].results), len(expected_results))
# Test result-description
for i in range(0, len(scan_result[0].results)):
eq_(scan_result[0].results[i].offset, expected_results[i][0])
eq_(scan_result[0].results[i].description, expected_results[i][1])
"""
try:
target_file = sys.argv[1]
except IndexError:
sys.stderr.write("Usage: %s <input vector file>\n" % sys.argv[0])
sys.exit(1)
target_file_basename = os.path.basename(target_file)
scan_function_name = target_file_basename.replace('.', '_')
expected_results = ""
signature = binwalk.scan(target_file, signature=True)[0]
for result in signature.results:
expected_results += "\t[%d, '%s'],\n" % (result.offset, result.description)
test_script = test_script_template % (scan_function_name,
target_file_basename,
expected_results,
target_file_basename)
test_script_path = os.path.join("tests", "test_%s.py" % scan_function_name)
with open(test_script_path, "w") as fp:
fp.write(test_script)
sys.stdout.write("Generated test script for '%s' and saved it to '%s'\n" % (target_file, test_script_path))
sys.exit(0)
This source diff could not be displayed because it is too large. You can view the blob instead.
import binwalk
from os.path import dirname
from nose.tools import eq_, ok_
def test_firmware_gzip():
'''
Test: Open firmware.gzip, scan for signatures
verify that all (and only) expected signatures are detected
'''
expected_results = [
[0, 'uImage header, header size: 64 bytes, header CRC: 0x29953343, created: 2011-06-27 07:33:02, image size: 6395843 bytes, Data Address: 0x40100000, Entry Point: 0x408A6270, data CRC: 0x3D73C1BC, OS: Linux, image type: OS Kernel Image, compression type: gzip, image name: "Unknown - IP7160_DIR855_F_Board"'],
[64, 'gzip compressed data, maximum compression, from Unix, last modified: 2011-06-27 07:33:00'],
]
scan_result = binwalk.scan(dirname(__file__) + '/input-vectors/firmware.gzip',
signature=True,
quiet=True)
# Test number of modules used
eq_(len(scan_result), 1)
# Test number of results for that module
eq_(len(scan_result[0].results), len(expected_results))
# Test result-description
for i in range(0, len(scan_result[0].results)):
eq_(scan_result[0].results[i].offset, expected_results[i][0])
eq_(scan_result[0].results[i].description, expected_results[i][1])
......@@ -20,8 +20,7 @@ def test_firmware_squashfs():
scan_result = binwalk.scan(
dirname(__file__) + '/input-vectors/firmware.squashfs',
signature=True,
quiet=True,
extract=True) # Throws a warning for missing external extractor
quiet=True)
# Test number of modules used
eq_(len(scan_result), 1)
# Test number of results for that module
......
import binwalk
from os.path import dirname
from nose.tools import eq_, ok_
def test_firmware_zip():
'''
Test: Open firmware.zip, scan for signatures
verify that all (and only) expected signatures are detected
'''
expected_results = [
[0, 'Zip archive data, at least v1.0 to extract, name: dir655_revB_FW_203NA/'],
[51, 'Zip archive data, at least v2.0 to extract, compressed size: 6395868, uncompressed size: 6422554, name: dir655_revB_FW_203NA/DIR655B1_FW203NAB02.bin'],
[6395993, 'Zip archive data, at least v2.0 to extract, compressed size: 14243, uncompressed size: 61440, name: dir655_revB_FW_203NA/dir655_revB_release_notes_203NA.doc'],
[6410581, 'End of Zip archive, footer length: 22'],
]
scan_result = binwalk.scan(dirname(__file__) + '/input-vectors/firmware.zip',
signature=True,
quiet=True)
# Test number of modules used
eq_(len(scan_result), 1)
# Test number of results for that module
eq_(len(scan_result[0].results), len(expected_results))
# Test result-description
for i in range(0, len(scan_result[0].results)):
eq_(scan_result[0].results[i].offset, expected_results[i][0])
eq_(scan_result[0].results[i].description, expected_results[i][1])
......@@ -13,8 +13,7 @@ def test_hello_world_simple_scan():
scan_result = binwalk.scan(
dirname(__file__) + '/input-vectors/hello-world.ihex',
signature=True,
quiet=True,
extract=True) # Throws a warning for missing external extractor
quiet=True)
# Test number of modules used
eq_(len(scan_result), 1)
# Test number of results for that module
......
......@@ -13,8 +13,7 @@ def test_hello_world_simple_scan():
scan_result = binwalk.scan(
dirname(__file__) + '/input-vectors/hello-world.srec',
signature=True,
quiet=True,
extract=True) # Throws a warning for missing external extractor
quiet=True)
# Test number of modules used
eq_(len(scan_result), 1)
# Test number of results for that module
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment