Esempio n. 1
0
 def test_an_image_with_a_single_filler_fragment(self, test_corpus_path):
     woodblock.random.seed(13)
     woodblock.file.corpus(test_corpus_path)
     image_path = HERE / 'simple-filler.dd'
     metadata_path = pathlib.Path('.'.join((str(image_path), 'json')))
     s = Scenario('simple filler fragment')
     s.add(FillerFragment(1024))
     image = Image(block_size=512)
     image.add(s)
     image.write(image_path)
     assert image_path.is_file()
     assert image_path.stat().st_size == 1024
     assert metadata_path.is_file()
     meta = json.load(metadata_path.open('r'))
     assert meta['block_size'] == 512
     assert meta['seed'] == 13
     assert len(meta['scenarios']) == 1
     assert len(meta['scenarios'][0]['files']) == 1
     assert meta['scenarios'][0]['files'][0]['original']['type'] == 'filler'
     assert meta['scenarios'][0]['files'][0]['original']['size'] == 1024
     assert len(meta['scenarios'][0]['files'][0]['fragments']) == 1
     assert meta['scenarios'][0]['files'][0]['fragments'][0]['number'] == 1
     assert meta['scenarios'][0]['files'][0]['fragments'][0]['size'] == 1024
     assert meta['scenarios'][0]['files'][0]['fragments'][0][
         'file_offsets'] == {
             'start': 0,
             'end': 1024
         }
     assert meta['scenarios'][0]['files'][0]['fragments'][0][
         'image_offsets'] == {
             'start': 0,
             'end': 1024
         }
     os.remove(image_path)
     os.remove(metadata_path)
Esempio n. 2
0
 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)
Esempio n. 3
0
 def test_that_an_empty_with_an_empty_scenario_can_be_written(self):
     s = Scenario('empty')
     image = Image()
     image.add(s)
     f = io.BytesIO()
     image.write(f)
     f.seek(0)
     assert len(f.read()) == 0
Esempio n. 4
0
 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
Esempio n. 5
0
 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)
Esempio n. 6
0
 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)
Esempio n. 7
0
 def test_an_image_with_a_single_scenario(self, test_corpus_path):
     woodblock.random.seed(13)
     image = Image(block_size=513)
     image.add(Scenario('some scenario'))
     assert image.metadata == {
         'block_size': 513,
         'seed': 13,
         'corpus': str(test_corpus_path),
         'scenarios': [{
             'name': 'some scenario',
             'files': []
         }]
     }
Esempio n. 8
0
 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
Esempio n. 9
0
 def test_an_image_with_multiple_scenarios(self, test_corpus_path):
     woodblock.random.seed(13)
     image = Image(block_size=513)
     image.add(Scenario('first scenario'))
     image.add(Scenario('second scenario'))
     image.add(Scenario('third scenario'))
     assert image.metadata == {
         'block_size':
         513,
         'seed':
         13,
         'corpus':
         str(test_corpus_path),
         'scenarios': [{
             'name': 'first scenario',
             'files': []
         }, {
             'name': 'second scenario',
             'files': []
         }, {
             'name': 'third scenario',
             'files': []
         }]
     }
Esempio n. 10
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)
Esempio n. 11
0
 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)