Skip to content

Commit

Permalink
Added new tests and a signature test script generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
devttys0 committed Sep 14, 2017
1 parent 51f2dc4 commit 42374f5
Show file tree
Hide file tree
Showing 14 changed files with 978 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def run(self):
# 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
Expand Down
66 changes: 66 additions & 0 deletions testing/test_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/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)

Binary file added testing/tests/input-vectors/firmware.cpio
Binary file not shown.
Binary file added testing/tests/input-vectors/firmware.gzip
Binary file not shown.
File renamed without changes.
Binary file added testing/tests/input-vectors/firmware.zip
Binary file not shown.
File renamed without changes.
File renamed without changes.
848 changes: 848 additions & 0 deletions testing/tests/test_firmware_cpio.py

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions testing/tests/test_firmware_gzip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

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])
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions testing/tests/test_firmware_zip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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])
3 changes: 1 addition & 2 deletions tests/test_ihex.py → testing/tests/test_ihex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tests/test_srec.py → testing/tests/test_srec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 42374f5

Please sign in to comment.