forked from fkie-cad/fact_extractor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
331 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 8 additions & 13 deletions
21
fact_extractor/plugins/unpacking/linuxkernel/test/test_extractor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 16 additions & 38 deletions
54
fact_extractor/plugins/unpacking/linuxkernel/test/test_plugin_linuxkernel.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
fact_extractor/plugins/unpacking/patool/test/test_plugin_patool.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.