def test_that_a_file_can_be_fragmented_into_two_fragments(self, path_test_file_4k): frags = File(path_test_file_4k).fragment((1,), block_size=512) assert len(frags) == 2 assert _replace_uuids(frags[0].metadata) == _replace_uuids( FileFragment(File(path_test_file_4k), 1, 0, 512).metadata) assert _replace_uuids(frags[1].metadata) == _replace_uuids( FileFragment(File(path_test_file_4k), 2, 512, 4096).metadata)
def test_that_a_file_can_be_fragmented_into_four_fragments(self, path_test_file_4k): frags = File(path_test_file_4k).fragment((1, 3, 7), block_size=512) assert len(frags) == 4 assert _replace_uuids(frags[0].metadata) == _replace_uuids( FileFragment(File(path_test_file_4k), 1, 0, 512).metadata) assert _replace_uuids(frags[1].metadata) == _replace_uuids( FileFragment(File(path_test_file_4k), 2, 512, 1536).metadata) assert _replace_uuids(frags[2].metadata) == _replace_uuids( FileFragment(File(path_test_file_4k), 3, 1536, 3584).metadata) assert _replace_uuids(frags[3].metadata) == _replace_uuids( FileFragment(File(path_test_file_4k), 4, 3584, 4096).metadata)
def test_that_the_correct_hash_is_computed(self, path_test_file_4k): assert FileFragment( File(path_test_file_4k), 1, 0, 1024 ).hash == '06aed7f43b72ab019f06f2cbf0a94237ad29cc36d2c91e27a9d3e734c90b665b' assert FileFragment( File(path_test_file_4k), 1, 1024, 2048 ).hash == 'dbbed6c65649c043888d421b8a950374faa0f5f3af28a12f9a2224d3b7c3fd9a' assert FileFragment( File(path_test_file_4k), 1, 2048, 3072 ).hash == '036c079fccd6a7527eb31f9dc7aaeb1cac70796c1a4a1627b81309ee018d11da' assert FileFragment( File(path_test_file_4k), 1, 3072, 4096 ).hash == '57434187458631358bbf7241a53990c77be44fa37194ae149b32427c5bc5fcac'
def test_an_image_with_a_single_file_fragment(self, test_corpus_path): woodblock.random.seed(13) woodblock.file.corpus(test_corpus_path) image_path = HERE / 'simple-file.dd' metadata_path = pathlib.Path('.'.join((str(image_path), 'json'))) s = Scenario('simple file fragment') s.add(File(test_corpus_path / '512').as_fragment()) image = Image(block_size=512) image.add(s) image.write(image_path) assert image_path.is_file() assert image_path.stat().st_size == 512 assert image_path.open('rb').read() == b'A' * 512 assert metadata_path.is_file() assert _replace_uuids(json.load(metadata_path.open('r'))) == { 'block_size': 512, 'corpus': str(test_corpus_path), 'seed': woodblock.random.get_seed(), 'scenarios': [{ 'name': 'simple file fragment', 'files': [{ 'original': { 'type': 'file', 'path': str((HERE.parent / 'data' / 'corpus' / '512').relative_to(get_corpus())), 'size': 512, 'id': 'uuid', 'sha256': '32beecb58a128af8248504600bd203dcc676adf41045300485655e6b8780a01d' }, 'fragments': [{ 'number': 1, 'size': 512, 'sha256': '32beecb58a128af8248504600bd203dcc676adf41045300485655e6b8780a01d', 'file_offsets': { 'start': 0, 'end': 512 }, 'image_offsets': { 'start': 0, 'end': 512 } }] }] }] } os.remove(image_path) os.remove(metadata_path)
def test_that_fragment_is_created_correctly(self, path_test_file_512): fragment = FileFragment(File(path_test_file_512), 1, 0, 512) assert fragment.size == 512 call_count = 0 data = b'' for chunk in fragment: call_count += 1 data = b''.join((data, chunk)) assert call_count == ceil(512 / 8192) assert len(data) == 512 assert data == b'A' * 512
def test_that_the_metadata_is_correct(self, path_test_file_512): fragment = FileFragment(File(path_test_file_512), 1, 0, 512) meta = _replace_uuids(fragment.metadata) assert meta == { 'file': { 'sha256': '32beecb58a128af8248504600bd203dcc676adf41045300485655e6b8780a01d', 'id': 'uuid', 'type': 'file', 'size': 512, 'path': str(path_test_file_512.relative_to(get_corpus())) }, 'fragment': { 'sha256': '32beecb58a128af8248504600bd203dcc676adf41045300485655e6b8780a01d', 'size': 512, 'number': 1, 'file_offsets': { 'start': 0, 'end': 512 } } }
def test_that_an_empty_list_of_fragmentation_points_return_a_single_fragment(self, path_test_file_4k): frags = File(path_test_file_4k).fragment(tuple(), block_size=512) assert len(frags) == 1 assert _replace_uuids(frags[0].metadata) == _replace_uuids( FileFragment(File(path_test_file_4k), 1, 0, 4096).metadata)
def test_that_invalid_num_fragments_raises_an_error(self, num_fragments, path_test_file_4k): with pytest.raises(InvalidFragmentationPointError): File(path_test_file_4k).fragment_randomly(num_fragments, 512)
def test_an_image_with_two_reversed_file_fragments_and_three_filler_fragments(self, test_corpus_path): woodblock.random.seed(13) woodblock.file.corpus(test_corpus_path) image_path = HERE / 'simple-fragmentation.dd' metadata_path = pathlib.Path('.'.join((str(image_path), 'json'))) file_path = test_corpus_path / '4096' frags = File(file_path).fragment_evenly(num_fragments=2) s = Scenario('simple fragmentation with fillers') s.add(RandomDataFragment(512)) s.add(frags[1]) s.add(ZeroesFragment(512)) s.add(frags[0]) s.add(RandomDataFragment(512)) image = Image(block_size=512) image.add(s) image.write(image_path) assert image_path.is_file() assert image_path.stat().st_size == 5632 assert metadata_path.is_file() meta = _replace_uuids(json.load(metadata_path.open('r'))) assert meta['block_size'] == 512 assert meta['corpus'] == str(test_corpus_path) assert meta['seed'] == 13 assert len(meta['scenarios']) == 1 sc_meta = meta['scenarios'][0] assert sc_meta['name'] == 'simple fragmentation with fillers' assert len(sc_meta['files']) == 4 assert sc_meta['files'][0]['original']['type'] == 'filler' assert sc_meta['files'][0]['original']['path'] == 'random' assert sc_meta['files'][0]['original']['size'] == 512 assert len(sc_meta['files'][0]['fragments']) == 1 assert sc_meta['files'][0]['fragments'][0]['number'] == 1 assert sc_meta['files'][0]['fragments'][0]['size'] == 512 assert sc_meta['files'][0]['fragments'][0]['file_offsets'] == {'start': 0, 'end': 512} assert sc_meta['files'][0]['fragments'][0]['image_offsets'] == {'start': 0, 'end': 512} assert sc_meta['files'][1] == { 'original': {'type': 'file', 'path': str(file_path.relative_to(get_corpus())), 'size': 4096, 'id': 'uuid', 'sha256': '0ec91e13ced59cfc1f297cfedd8c595a6200ac2b8c99bcc321e8c68cf1f166a0'}, 'fragments': [{'number': 1, 'size': 2048, 'sha256': '291b808471ca4772e260dd50604e93082c22fbcf821fa3db7531e51343473717', 'file_offsets': {'start': 0, 'end': 2048}, 'image_offsets': {'start': 3072, 'end': 5120}}, {'number': 2, 'size': 2048, 'sha256': '570dc7f7ceea7fa7204588456fd00e53b0abf1cca9d35cc074383e8dcc418114', 'file_offsets': {'start': 2048, 'end': 4096}, 'image_offsets': {'start': 512, 'end': 2560}} ]} assert sc_meta['files'][2] == { 'original': {'type': 'filler', 'path': 'zeroes', 'size': 512, 'id': 'uuid', 'sha256': '076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560'}, 'fragments': [{'number': 1, 'size': 512, 'sha256': '076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560', 'file_offsets': {'start': 0, 'end': 512}, 'image_offsets': {'start': 2560, 'end': 3072}}]} assert sc_meta['files'][3]['original']['type'] == 'filler' assert sc_meta['files'][3]['original']['path'] == 'random' assert sc_meta['files'][3]['original']['size'] == 512 assert len(sc_meta['files'][3]['fragments']) == 1 assert sc_meta['files'][3]['fragments'][0]['number'] == 1 assert sc_meta['files'][3]['fragments'][0]['size'] == 512 assert sc_meta['files'][3]['fragments'][0]['file_offsets'] == {'start': 0, 'end': 512} assert sc_meta['files'][3]['fragments'][0]['image_offsets'] == {'start': 5120, 'end': 5632} os.remove(image_path) os.remove(metadata_path)
def test_that_invalid_fragmentation_points_raise_an_error(self, fragmentation_points, path_test_file_4k): f = File(path_test_file_4k) with pytest.raises(InvalidFragmentationPointError): f.fragment(fragmentation_points)
def test_that_a_too_small_file_cannot_be_fragmented(self, path_test_file_4k): with pytest.raises(InvalidFragmentationPointError): File(path_test_file_4k).fragment_randomly(2, File(path_test_file_4k).size * 2)
def test_that_an_error_is_raised_when_no_corpus_is_set(self, path_test_file_4k): woodblock.file.corpus(None) with pytest.raises(WoodblockError): File(path_test_file_4k)
def test_that_the_correct_number_of_fragments_is_generated(self, num_fragments, path_test_file_4k): frags = File(path_test_file_4k).fragment_evenly(num_fragments=num_fragments, block_size=512) assert len(frags) == num_fragments
def test_that_max_fragments_raises_for_invalid_block_sizes(self, block_size, path_test_file_4k): with pytest.raises(WoodblockError): File(path_test_file_4k).max_fragments(block_size)
def test_that_the_correct_number_of_fragments_is_generated_for_2k_file(self, num_fragments, path_test_file_2000): frags = File(path_test_file_2000).fragment_randomly(num_fragments=num_fragments, block_size=512) assert len(frags) == num_fragments
def test_that_max_fragments_returns_correct_number(self, block_size, expected_frags, path_test_file_4k): assert File(path_test_file_4k).max_fragments(block_size) == expected_frags
def test_that_an_empty_file_raises_an_error(self): with NamedTemporaryFile() as tmp: with pytest.raises(WoodblockError): File(pathlib.Path(tmp.name))
def test_that_a_non_existing_file_raises_an_error(self): with pytest.raises(FileNotFoundError): File(pathlib.Path('/nonexisting'))
def test_that_all_fragments_have_the_same_id(self, num_fragments, path_test_file_4k): frags = File(path_test_file_4k).fragment_evenly(num_fragments=num_fragments, block_size=512) for i in range(len(frags)): assert frags[i - 1].metadata['file']['id'] == frags[i].metadata['file']['id']
def test_that_the_file_is_split_evenly(self, block_size, num_fragments, expected_frag_offsets, path_test_file_4k): frags = File(path_test_file_4k).fragment_evenly(num_fragments=num_fragments, block_size=block_size) frag_offsets = tuple(itemgetter('start', 'end')(x.metadata['fragment']['file_offsets']) for x in frags) assert frag_offsets == expected_frag_offsets