def test_an_image_padding_with_a_single_zeroes_fragment( self, test_corpus_path): woodblock.random.seed(13) woodblock.file.corpus(test_corpus_path) image_path = HERE / 'simple-zeroes-with-padding.dd' metadata_path = pathlib.Path('.'.join((str(image_path), 'json'))) s = Scenario('simple zeroes with padding') s.add(ZeroesFragment(1000)) image = Image(block_size=512, padding_generator=lambda x: b'A' * x) image.add(s) image.write(image_path) assert image_path.is_file() assert image_path.stat().st_size == 1024 assert image_path.open('rb').read() == b''.join( (b'\x00' * 1000, b'A' * 24)) assert metadata_path.is_file() metadata = _replace_uuids(json.load(metadata_path.open('r'))) assert metadata == { 'block_size': 512, 'corpus': str(test_corpus_path), 'seed': woodblock.random.get_seed(), 'scenarios': [{ 'name': 'simple zeroes with padding', 'files': [{ 'original': { 'type': 'filler', 'path': 'zeroes', 'size': 1000, 'id': 'uuid', 'sha256': '541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53' }, 'fragments': [{ 'number': 1, 'size': 1000, 'sha256': '541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53', 'file_offsets': { 'start': 0, 'end': 1000 }, 'image_offsets': { 'start': 0, 'end': 1000 } }] }] }] } os.remove(image_path) os.remove(metadata_path)
def test_that_it_is_possible_to_read_the_framgment_multiple_times( self, iterations): fragment = ZeroesFragment(512) data = b''.join(c for c in fragment) for _ in range(iterations): data2 = b''.join(c for c in fragment) print(len(data), len(data2)) assert data2 == data
def test_that_a_tuple_of_fragments_can_be_added(self, num_fragments): s = Scenario('Test Scenario') fragments = tuple(ZeroesFragment(512) for _ in range(num_fragments)) s.add(fragments) call_count = 0 for _ in s: call_count += 1 assert call_count == num_fragments
def test_that_a_scenario_can_be_written(self): s = Scenario('scenario') s.add(ZeroesFragment(512)) image = Image() image.add(s) f = io.BytesIO() image.write(f) f.seek(0) assert len(f.read()) == 512
def test_an_image_with_a_single_zeroes_fragment(self, test_corpus_path): woodblock.random.seed(13) woodblock.file.corpus(test_corpus_path) image_path = HERE / 'simple-zeroes.dd' metadata_path = pathlib.Path('.'.join((str(image_path), 'json'))) s = Scenario('simple zeroes') s.add(ZeroesFragment(1024)) image = Image() image.add(s) image.write(image_path) assert image_path.is_file() assert image_path.stat().st_size == 1024 assert image_path.open('rb').read() == b'\x00' * 1024 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 zeroes', 'files': [{ 'original': { 'type': 'filler', 'path': 'zeroes', 'size': 1024, 'id': 'uuid', 'sha256': '5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef' }, 'fragments': [{ 'number': 1, 'size': 1024, 'sha256': '5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef', 'file_offsets': { 'start': 0, 'end': 1024 }, 'image_offsets': { 'start': 0, 'end': 1024 } }] }] }] } os.remove(image_path) os.remove(metadata_path)
def test_that_fragment_is_created_correctly(self, size): fragment = ZeroesFragment(size) assert fragment.size == size call_count = 0 data = b'' for chunk in fragment: call_count += 1 data = b''.join((data, chunk)) assert call_count == ceil(size / 8192) assert len(data) == size assert data == b'\x00' * size
def test_with_multiple_fragments(self): s = Scenario('test scenario with a single file') s.add(ZeroesFragment(1024)) s.add(ZeroesFragment(512)) s.add(ZeroesFragment(2050)) expected = {'name': 'test scenario with a single file', 'files': [ { 'original': { 'size': 1024, 'id': 'uuid', 'type': 'filler', 'path': 'zeroes', 'sha256': '5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef'}, 'fragments': [ { 'size': 1024, 'number': 1, 'file_offsets': {'start': 0, 'end': 1024}, 'sha256': '5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef' } ] }, { 'original': { 'size': 512, 'id': 'uuid', 'type': 'filler', 'path': 'zeroes', 'sha256': '076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560'}, 'fragments': [ { 'size': 512, 'number': 1, 'file_offsets': {'start': 0, 'end': 512}, 'sha256': '076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560' } ] }, { 'original': { 'size': 2050, 'id': 'uuid', 'type': 'filler', 'path': 'zeroes', 'sha256': '382e008e5bc647359ed6f41cb932f732b000a96c6048edd3cae85e65df04c175'}, 'fragments': [ { 'size': 2050, 'number': 1, 'file_offsets': {'start': 0, 'end': 2050}, 'sha256': '382e008e5bc647359ed6f41cb932f732b000a96c6048edd3cae85e65df04c175' } ] } ]} assert replace_uuids(s.metadata) == expected
def test_that_fragments_can_be_read(self, num_fragments): chunk_size = 512 s = Scenario('Test Scenario') for _ in range(num_fragments): s.add(ZeroesFragment(512, chunk_size=chunk_size)) call_count = 0 for fragment in s: call_count += 1 assert fragment.size == 512 for chunk in fragment: assert chunk == b'\x00' * chunk_size assert call_count == num_fragments
def test_that_correct_padding_is_applied(self, fragment_size, expected_padding_size): s = Scenario('scenario') s.add(ZeroesFragment(fragment_size)) padder = FakePaddingGenerator() image = Image(padding_generator=padder.generate) image.add(s) f = io.BytesIO() image.write(f) f.seek(0) data = f.read() assert len(data) == ceil(fragment_size / 512) * 512 assert padder.size == expected_padding_size if expected_padding_size > 0: assert data[ -expected_padding_size:] == b'A' * expected_padding_size
def test_with_a_single_fragment(self): s = Scenario('test scenario with a single file') s.add(ZeroesFragment(1024)) expected = {'name': 'test scenario with a single file', 'files': [ { 'original': { 'size': 1024, 'id': 'uuid', 'type': 'filler', 'path': 'zeroes', 'sha256': '5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef'}, 'fragments': [ { 'size': 1024, 'number': 1, 'file_offsets': {'start': 0, 'end': 1024}, 'sha256': '5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef' } ] } ]} assert replace_uuids(s.metadata) == expected
def test_that_the_metadata_is_correct(self): fragment = ZeroesFragment(512) meta = _replace_uuids(fragment.metadata) assert meta == { 'file': { 'sha256': '076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560', 'id': 'uuid', 'type': 'filler', 'size': 512, 'path': 'zeroes' }, 'fragment': { 'sha256': '076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560', 'size': 512, 'number': 1, 'file_offsets': { 'start': 0, 'end': 512 } } }
def test_implicit_hash_computation(self, size, expected_hash): fragment = ZeroesFragment(size) for _ in fragment: pass assert fragment.hash == expected_hash
def test_explicit_hash_computation(self, size, expected_hash): fragment = ZeroesFragment(size) assert fragment.hash == expected_hash
def test_that_a_size_of_zero_raises_an_error(self): with pytest.raises(WoodblockError): ZeroesFragment(0)
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_an_image_padding_and_two_zeroes_fragments(self, test_corpus_path): woodblock.random.seed(13) woodblock.file.corpus(test_corpus_path) image_path = HERE / 'two-zeroes-with-padding.dd' metadata_path = pathlib.Path('.'.join((str(image_path), 'json'))) s = Scenario('two zeroes with padding') s.add(ZeroesFragment(1000)) s.add(ZeroesFragment(2048)) image = Image(block_size=512, padding_generator=lambda x: b'A' * x) image.add(s) image.write(image_path) assert image_path.is_file() assert image_path.stat().st_size == 1024 + 2048 assert image_path.open('rb').read() == b''.join( (b'\x00' * 1000, b'A' * 24, b'\x00' * 2048)) assert metadata_path.is_file() metadata = _replace_uuids(json.load(open(metadata_path, 'r'))) assert metadata == { 'block_size': 512, 'corpus': str(test_corpus_path), 'seed': woodblock.random.get_seed(), 'scenarios': [{ 'name': 'two zeroes with padding', 'files': [{ 'original': { 'type': 'filler', 'path': 'zeroes', 'size': 1000, 'id': 'uuid', 'sha256': '541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53' }, 'fragments': [{ 'number': 1, 'size': 1000, 'sha256': '541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53', 'file_offsets': { 'start': 0, 'end': 1000 }, 'image_offsets': { 'start': 0, 'end': 1000 } }] }, { 'original': { 'type': 'filler', 'path': 'zeroes', 'size': 2048, 'id': 'uuid', 'sha256': 'e5a00aa9991ac8a5ee3109844d84a55583bd20572ad3ffcd42792f3c36b183ad' }, 'fragments': [{ 'number': 1, 'size': 2048, 'sha256': 'e5a00aa9991ac8a5ee3109844d84a55583bd20572ad3ffcd42792f3c36b183ad', 'file_offsets': { 'start': 0, 'end': 2048 }, 'image_offsets': { 'start': 1024, 'end': 3072 } }] }] }] } os.remove(image_path) os.remove(metadata_path)