Skip to content

Commit

Permalink
fixed pytest 8 deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke committed Feb 10, 2023
1 parent 2fa9f04 commit e13f7a5
Show file tree
Hide file tree
Showing 30 changed files with 331 additions and 350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ def test_unpacker_selection(self):
def test_extraction(self):
input_file = os.path.join(TEST_DATA_DIR, 'ambarella_rom.fs')
unpacked_files, meta_data = self.unpacker.extract_files_from_file(input_file, self.tmp_dir.name)
self.assertIn("'file_count': 4", meta_data['output'], 'should be 4 files in the romfs')
self.assertGreaterEqual(len(unpacked_files), 5, 'Should contain 4 files and a header')
assert "'file_count': 4" in meta_data['output'], 'should be 4 files in the romfs'
assert len(unpacked_files) >= 5, 'Should contain 4 files and a header'
8 changes: 4 additions & 4 deletions fact_extractor/plugins/unpacking/dahua/test/test_dahua.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_extraction(self):
input_file = Path(TEST_DATA_DIR, 'dh.bin')
unpacked_files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)

self.assertIn('zip header fixed', meta_data['output'])
self.assertEqual(len(unpacked_files), 1)
self.assertIn('{}/dahua_firmware.zip'.format(self.tmp_dir.name), unpacked_files)
self.assertEqual(input_file.stat().st_size, Path(unpacked_files[0]).stat().st_size, 'file size should not change')
assert 'zip header fixed' in meta_data['output']
assert len(unpacked_files) == 1
assert f'{self.tmp_dir.name}/dahua_firmware.zip' in unpacked_files
assert input_file.stat().st_size == Path(unpacked_files[0]).stat().st_size, 'file size should not change'
4 changes: 2 additions & 2 deletions fact_extractor/plugins/unpacking/deb/test/test_deb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ def test_unpacker_selection_generic(self):
def test_extraction(self):
files, meta_data = self.unpacker.extract_files_from_file(os.path.join(TEST_DATA_DIR, 'test.deb'), self.tmp_dir.name)

self.assertEqual(len(files), 3, 'file number incorrect')
self.assertIn('./usr/bin/test_elf_sfx', meta_data['output'])
assert len(files) == 3, 'file number incorrect'
assert './usr/bin/test_elf_sfx' in meta_data['output']
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ def test_extraction_valid_image(self):
test_file = Path(TEST_DATA_DIR, 'valid_draytekvigor167_image.bin')
unpacked_files, meta_data = self.unpacker.extract_files_from_file(test_file, self.tmp_dir.name)

self.assertEqual(meta_data['output'], 'successfully unpacked image')
self.assertEqual(len(unpacked_files), 2, 'number of extracted files not correct')
self.assertIn(str(Path(self.tmp_dir.name) / 'kernel_image'), unpacked_files, 'kernel image not extracted')
self.assertIn(str(Path(self.tmp_dir.name) / 'squashfs_root'), unpacked_files, 'squashfs not extracted')
assert meta_data['output'] == 'successfully unpacked image'
assert len(unpacked_files) == 2, 'number of extracted files not correct'
assert str(Path(self.tmp_dir.name) / 'kernel_image') in unpacked_files, 'kernel image not extracted'
assert str(Path(self.tmp_dir.name) / 'squashfs_root') in unpacked_files, 'squashfs not extracted'
squashfs_binary = get_binary_from_file(Path(self.tmp_dir.name) / 'squashfs_root')
squashfs_hash = get_sha256(squashfs_binary)
self.assertEqual(squashfs_hash, '73b648f484ab0a34ce00729ce8b7ef183885b4b5c540344a8451d18fe94cc2fa')
assert squashfs_hash == '73b648f484ab0a34ce00729ce8b7ef183885b4b5c540344a8451d18fe94cc2fa'

def test_extraction_invalid_image(self):
test_file = Path(TEST_DATA_DIR, 'invalid_draytekvigor167_image_struct_error.bin')
unpacked_files, meta_data = self.unpacker.extract_files_from_file(test_file, self.tmp_dir.name)
_, meta_data = self.unpacker.extract_files_from_file(test_file, self.tmp_dir.name)

self.assertIn('failed to recognize firmware container', meta_data['output'])
assert 'failed to recognize firmware container' in meta_data['output']
14 changes: 7 additions & 7 deletions fact_extractor/plugins/unpacking/hp/test/test_update_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def test_unpacker_selection(self):
def test_extraction(self):
input_file = Path(TEST_DATA_DIR, 'update_stream.bin')
unpacked_files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual(meta_data['number_of_zero_padded_sections'], 4)
self.assertEqual(meta_data['number_of_lzma_streams'], 1)
self.assertEqual(len(unpacked_files), 5)
self.assertIn('{}/0x0'.format(self.tmp_dir.name), unpacked_files)
self.assertIn('{}/0x8d_lzma_decompressed'.format(self.tmp_dir.name), unpacked_files)
decompressed_data = get_binary_from_file('{}/0x8d_lzma_decompressed'.format(self.tmp_dir.name))
self.assertEqual(decompressed_data, b'compressed_stream_data')
assert meta_data['number_of_zero_padded_sections'] == 4
assert meta_data['number_of_lzma_streams'] == 1
assert len(unpacked_files) == 5
assert f'{self.tmp_dir.name}/0x0' in unpacked_files
assert f'{self.tmp_dir.name}/0x8d_lzma_decompressed' in unpacked_files
decompressed_data = get_binary_from_file(f'{self.tmp_dir.name}/0x8d_lzma_decompressed')
assert decompressed_data == b'compressed_stream_data'
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
import os
import sys
import unittest
from pathlib import Path
from unittest.mock import patch, mock_open

INTERNAL_DIR = Path(__file__).parent.parent / 'internal'
sys.path.append(str(INTERNAL_DIR))
from extractor import Extractor # noqa: E402 pylint: disable=import-error,wrong-import-position

sample_data = b'####-------\x1f\x8b\x08-------\x1f\x8b\x08$$$$BZh$$$$'
SAMPLE_DATA = b'####-------\x1f\x8b\x08-------\x1f\x8b\x08$$$$BZh$$$$'


class ExtractorTestCases(unittest.TestCase):
class TestExtractorCases:

def test_find_offsets(self):
with patch('builtins.open', mock_open(read_data=sample_data)) as mock_file:
with patch('builtins.open', mock_open(read_data=SAMPLE_DATA)) as mock_file:
extract = Extractor('file_name', 'output_dir')
mock_file.assert_called_with('file_name', 'rb')
ret = extract._find_offsets()
self.assertEqual({'BZIP': [28], 'GZIP': [11, 21]}, ret)
ret = extract._find_offsets() # pylint: disable=protected-access
assert ret == {'BZIP': [28], 'GZIP': [11, 21]}

def test_extract_files(self):
with patch('builtins.open', mock_open(read_data=sample_data)) as mock_file:
with patch('builtins.open', mock_open(read_data=SAMPLE_DATA)) as mock_file:
output_dir = 'output_dir'
extract = Extractor('file_name', output_dir)
mock_file.assert_called_with('file_name', 'rb')

with patch('builtins.open', mock_open(read_data='')) as mock_file:
file_generator = extract.extracted_files()
file_data = next(file_generator)
self.assertEqual(os.path.join(output_dir, 'vmlinux_GZIP_11.gz'), file_data['file_path'])
assert file_data['file_path'] == os.path.join(output_dir, 'vmlinux_GZIP_11.gz')
mock_file.assert_called_with('output_dir/vmlinux_GZIP_11.gz', 'wb')
mock_file().write.assert_called_once_with(b'\x1f\x8b\x08-------\x1f\x8b\x08$$$$BZh$$$$')

with patch('builtins.open', mock_open(read_data='')) as mock_file:
file_data = next(file_generator)
self.assertEqual(os.path.join(output_dir, 'vmlinux_GZIP_21.gz'), file_data['file_path'])
assert file_data['file_path'] == os.path.join(output_dir, 'vmlinux_GZIP_21.gz')
mock_file.assert_called_with('output_dir/vmlinux_GZIP_21.gz', 'wb')
mock_file().write.assert_called_once_with(b'\x1f\x8b\x08$$$$BZh$$$$')


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import unittest

from ..internal.pattern_searcher import PatternSearcher


class PatternSearcherTestCase(unittest.TestCase):

class TestPatternSearcherCase:
def test_instance(self):
data = b'####'
ps = PatternSearcher(data)

self.assertEqual(True, isinstance(ps, PatternSearcher))
assert isinstance(ps, PatternSearcher)

def test_search_gzip(self):
data = b'####-------\037\213\010-------\037\213\010$$$$$$$$'
ps = PatternSearcher(data)
offsets = ps.find_all_gzip_offsets()
self.assertEqual([11, 21], offsets)
assert offsets == [11, 21]

def test_search_all(self):
data = b'####-------\037\213\010-------\037\213\010$$$$$$$$'
Expand All @@ -27,19 +24,17 @@ def test_search_all(self):
data += b'\002!L\030'
data += b'(\265/\375'

expected = {'BZIP': [38],
'GZIP': [11, 21, 48],
'LZ4': [51],
'LZMA': [41],
'LZOP': [45],
'XZ': [32],
'ZSTD': [55]}
expected = {
'BZIP': [38],
'GZIP': [11, 21, 48],
'LZ4': [51],
'LZMA': [41],
'LZOP': [45],
'XZ': [32],
'ZSTD': [55],
}

ps = PatternSearcher(data)
offset_data = ps.find_all_headers()

self.assertEqual(expected, offset_data)


if __name__ == '__main__':
unittest.main()
assert offset_data == expected
Original file line number Diff line number Diff line change
@@ -1,53 +1,31 @@
import os
import sys
from pathlib import Path

from test.unit.unpacker.test_unpacker import TestUnpackerBase
import pytest

CODE_DIR = Path(__file__).parent.parent / 'code'
sys.path.append(str(CODE_DIR))
from test.unit.unpacker.test_unpacker import TestUnpackerBase

TEST_DATA_DIR = Path(__file__).parent / 'data'

sample_data = b'####-------\x1f\x8b\x08-------\x1f\x8b\x08$$$$BZh$$$$'
SAMPLE_DATA = b'####-------\x1f\x8b\x08-------\x1f\x8b\x08$$$$BZh$$$$'


class TestLinuxKernelUnpacker(TestUnpackerBase):

def test_unpacker_selection_generic(self):
self.check_unpacker_selection('linux/kernel', 'LinuxKernel')

def test_extraction_valid_bzImage_bzip2(self):
input_file = TEST_DATA_DIR / 'bzImage_bzip2'
files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual([os.path.join(self.tmp_dir.name, 'vmlinux_BZIP_17001')], files)

def test_extraction_valid_bzImage_gzip(self):
input_file = TEST_DATA_DIR / 'bzImage_gzip'
files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual([os.path.join(self.tmp_dir.name, 'vmlinux_GZIP_17001')], files)

def test_extraction_valid_bzImage_lz4(self):
input_file = TEST_DATA_DIR / 'bzImage_lz4'
files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual([os.path.join(self.tmp_dir.name, 'vmlinux_LZ4_17001')], files)

def test_extraction_valid_bzImage_lzma(self):
input_file = TEST_DATA_DIR / 'bzImage_lzma'
files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual([os.path.join(self.tmp_dir.name, 'vmlinux_LZMA_17001')], files)

def test_extraction_valid_bzImage_lzo(self):
input_file = TEST_DATA_DIR / 'bzImage_lzo'
files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual([os.path.join(self.tmp_dir.name, 'vmlinux_LZOP_17001')], files)

def test_extraction_valid_bzImage_xz(self):
input_file = TEST_DATA_DIR / 'bzImage_xz'
files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual([os.path.join(self.tmp_dir.name, 'vmlinux_XZ_17001')], files)
@pytest.mark.parametrize('input_file, expected', [
('bzImage_bzip2', 'vmlinux_BZIP_17001'),
('bzImage_gzip', 'vmlinux_GZIP_17001'),
('bzImage_lz4', 'vmlinux_LZ4_17001'),
('bzImage_lzma', 'vmlinux_LZMA_17001'),
('bzImage_lzo', 'vmlinux_LZOP_17001'),
('bzImage_xz', 'vmlinux_XZ_17001'),
])
def test_extraction_valid_bz_image(self, input_file, expected):
files, _ = self.unpacker.extract_files_from_file(str(TEST_DATA_DIR / input_file), self.tmp_dir.name)
assert files == [str(Path(self.tmp_dir.name) / expected)]

def test_extraction_invalid_image(self):
input_file = TEST_DATA_DIR / 'bogus_image.bin'
files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual([], files)
files, _ = self.unpacker.extract_files_from_file(str(TEST_DATA_DIR / 'bogus_image.bin'), self.tmp_dir.name)
assert files == []
24 changes: 18 additions & 6 deletions fact_extractor/plugins/unpacking/patool/test/test_plugin_patool.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import os
from test.unit.unpacker.test_unpacker import TestUnpackerBase

import pytest

from test.unit.unpacker.test_unpacker import TestUnpackerBase

TEST_DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')


class TestPaToolUnpacker(TestUnpackerBase):

def test_unpacker_selection_generic(self):
self.check_unpacker_selection('application/vnd.ms-cab-compressed', 'PaTool')

def test_extraction(self):
in_files = ['test.cab', 'test.zoo', 'test.tar.bz2', 'test.tar.zip']
for in_file in in_files:
self.check_unpacking_of_standard_unpack_set(os.path.join(TEST_DATA_DIR, in_file), additional_prefix_folder='get_files_test', output=False)
@pytest.mark.parametrize(
'in_file, ignore',
[
('test.cab', None),
('test.tar.bz2', None),
('test.tar.zip', None),
],
)
def test_extraction(self, in_file, ignore):
self.check_unpacking_of_standard_unpack_set(
os.path.join(TEST_DATA_DIR, in_file),
additional_prefix_folder='get_files_test',
output=False,
ignore=ignore,
)
23 changes: 11 additions & 12 deletions fact_extractor/plugins/unpacking/raw/test/test_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,26 @@ class TestRawUnpacker(TestUnpackerBase):

def test_unpacker_selection(self):
self.check_unpacker_selection('data/raw', 'RAW')
# self.check_unpacker_selection('application/octet-stream', 'RAW')

def test_extraction(self):
input_file = Path(TEST_DATA_DIR, 'raw.bin')
unpacked_files, meta_data = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual(meta_data['padding seperated sections'], 3)
self.assertEqual(meta_data['LZMA'], 1)
self.assertEqual(len(unpacked_files), 4)
self.assertIn('{}/0x2f'.format(self.tmp_dir.name), unpacked_files)
self.assertIn('{}/0x8d.lzma'.format(self.tmp_dir.name), unpacked_files)
assert meta_data['padding seperated sections'] == 3
assert meta_data['LZMA'] == 1
assert len(unpacked_files) == 4
assert f'{self.tmp_dir.name}/0x2f' in unpacked_files
assert f'{self.tmp_dir.name}/0x8d.lzma' in unpacked_files

def test_extraction_encoded(self):
input_file = Path(TEST_DATA_DIR, 'encoded.bin')
unpacked_files, meta_data = self.unpacker._extract_files_from_file_using_specific_unpacker(str(input_file), self.tmp_dir.name, self.unpacker.unpacker_plugins['data/raw'])
self.assertEqual(meta_data['Intel Hex'], 1)
self.assertEqual(meta_data['Motorola S-Record'], 1)
self.assertIn('{}/0x6.ihex'.format(self.tmp_dir.name), unpacked_files)
self.assertIn('{}/0x291f.srec'.format(self.tmp_dir.name), unpacked_files)
self.assertEqual(len(unpacked_files), 2)
assert meta_data['Intel Hex'] == 1
assert meta_data['Motorola S-Record'] == 1
assert f'{self.tmp_dir.name}/0x6.ihex' in unpacked_files
assert f'{self.tmp_dir.name}/0x291f.srec' in unpacked_files
assert len(unpacked_files) == 2

def test_extraction_nothing_included(self):
input_file = Path(TEST_DATA_DIR, 'nothing.bin')
unpacked_files, _ = self.unpacker.extract_files_from_file(str(input_file), self.tmp_dir.name)
self.assertEqual(len(unpacked_files), 0)
assert len(unpacked_files) == 0
6 changes: 3 additions & 3 deletions fact_extractor/plugins/unpacking/ros/test/test_ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def test_unpacker_selection_generic(self):
def test_extraction(self):
in_file = os.path.join(TEST_DATA_DIR, 'test.ros')
files, meta_data = self.unpacker.extract_files_from_file(in_file, self.tmp_dir.name)
self.assertEqual(len(files), 6, 'file number incorrect ({})'.format(files.__str__()))
self.assertIn('{}/DATETIME_C'.format(self.tmp_dir.name), files, 'Not all files found')
self.assertIn('file_information', meta_data, 'Output meta not set')
assert len(files) == 6, f'file number incorrect ({files})'
assert f'{self.tmp_dir.name}/DATETIME_C' in files, 'Not all files found'
assert 'file_information' in meta_data, 'Output meta not set'


def test_infer_endianness():
Expand Down
Loading

0 comments on commit e13f7a5

Please sign in to comment.