def testDefaultValueWhenFuzzableBeforeMutation(self): fields = [String('a'), Container([Static('A'), String('B')])] default_container = Container(fields=fields, fuzzable=False) default_rendered = default_container.render() uut = self.get_uut(fields, fuzzable=True) uut_rendered = uut.render() self.assertEqual(default_rendered, uut_rendered)
def testGetRenderedFieldsCorrect(self): fields = [ String('test_string', name='field1'), If(Equal('test_group_5', 'A'), String('if', name='if_field3'), name='if2'), IfNot(Equal('test_group_5', 'A'), String('ifnot', name='ifnot_field5'), name='ifnot4'), Group(name='test_group_5', values=['A', 'B', 'C']) ] container = self.get_default_container(fields) expected_list = filter(lambda x: len(x.render()), fields) if len(container.render()): self.assertListEqual(container.get_rendered_fields(), expected_list) else: self.assertListEqual(container.get_rendered_fields(), []) while container.mutate(): expected_list = filter(lambda x: len(x.render()), fields) if len(container.render()): self.assertEqual(container.get_rendered_fields(), expected_list) else: self.assertEqual(container.get_rendered_fields(), [])
def testContainsAllDefaultMutations(self): fields = [String('a'), Container([Static('A'), String('B')])] default_container = Container(fields=fields) default_mutations = self.get_all_mutations(default_container) uut = self.get_uut(fields) uut_mutations = self.get_all_mutations(uut) for mutation in default_mutations: self.assertIn(mutation, uut_mutations)
def testPrimitivesInit1Push1(self): init_fields = [ String('test1'), ] push_fields = [ String('test2'), ] self._test_fields(init_fields=init_fields, push_fields=push_fields)
def testContainersInit2Push2(self): init_containers = [ Container(fields=[String('test_init_%d' % d)]) for d in range(2) ] push_containers = [ Container(fields=[String('test_push_%d' % d)]) for d in range(2) ] self._test_fields(init_fields=init_containers, push_fields=push_containers)
def testPrimitivesInit2Push2(self): init_fields = [ String('test11'), String('test12'), ] push_fields = [ String('test21'), String('test22'), ] self._test_fields(init_fields=init_fields, push_fields=push_fields)
def testDefaultValueWhenFuzzableAfterReset(self): fields = [String('a'), Container([Static('A'), String('B')])] default_container = Container(fields=fields, fuzzable=False) default_rendered = default_container.render() uut = self.get_uut(fields, fuzzable=True) while uut.mutate(): pass uut.reset() uut_rendered = uut.render() self.assertEqual(default_rendered, uut_rendered)
def testRepeatSingleMaxTimes5(self): max_times = 5 fields = [ String('field1') ] repeater = Repeat(fields=fields, max_times=max_times) self._test_mutations(repeater, fields, max_times=max_times)
def testInclusiveSizeOfPacketWithHashAtTheEnd(self): container = Container(name='full packet', fields=[ Container(name='hashed part', fields=[ SizeInBytes( name='packet size', sized_field='/full_packet', length=32, fuzzable=False, encoder=ENC_INT_BE), String(name='some string', value='aaa'), ]), Sha256(name='hash', depends_on='hashed part', fuzzable=False) ]) while True: rendered = container.render().tobytes() rendered_len = len(rendered) # first 4 bytes are the packet size size_in_packet = unpack('>I', rendered[:4])[0] hashed_buffer = rendered[:-32] hash_buffer = rendered[-32:] self.assertEqual(size_in_packet, rendered_len) self.assertEqual( hashlib.sha256(hashed_buffer).digest(), hash_buffer) if not container.mutate(): break
def testValueTruncedFuzzable(self): field = String(name='trunced', value='abc') uut = Trunc(max_size=self.trunc_size, fields=field, fuzzable=True) self._testValueTrunced(field, uut, self.trunc_size) self.assertEqual(uut.num_mutations(), field.num_mutations()) self.assertGreater(uut.num_mutations(), 0) while uut.mutate(): self._testValueTrunced(field, uut, self.trunc_size)
def testIsFuzzable(self): field = String('abc') uut = Meta(name='uut', fields=[field], fuzzable=True) num_mutations = uut.num_mutations() self.assertGreater(num_mutations, 0) self.assertGreaterEqual(num_mutations, field.num_mutations()) actual_num_mutations = 0 while uut.mutate(): actual_num_mutations += 1 self.assertEqual(actual_num_mutations, num_mutations)
def testNumMutations(self): field = String(name='padded', value='abc') uut = Pad(self.pad_length, fields=field, name='uut') field_num_mutations = field.num_mutations() uut_num_mutations = uut.num_mutations() self.assertEqual(uut_num_mutations, field_num_mutations) self.assertGreater(uut_num_mutations, 0) actual_num_mutations = 0 while uut.mutate(): actual_num_mutations += 1 self.assertEqual(actual_num_mutations, uut_num_mutations)
def testlways(self): field = self.get_condition_field() condition = self.get_not_applies_always_condition() condition_container = self.cls(condition=condition, fields=[String(ConditionTest.inner_field_value)], fuzzable=True) # This is done to allow field name resolution enclosing = Container(fields=[field, condition_container]) rendered = condition_container.render() self.assertEqual(rendered, Bits()) while condition_container.mutate(): rendered = condition_container.render() self.assertEqual(rendered, Bits())
def testNumMutations(self): field = String(name='trunced', value='abc') uut = Trunc(self.trunc_size, fields=field, name='uut') field_num_mutations = field.num_mutations() uut_num_mutations = uut.num_mutations() self.assertEqual(uut_num_mutations, field_num_mutations) self.assertGreater(uut_num_mutations, 0) actual_num_mutations = 0 while uut.mutate(): actual_num_mutations += 1 self.assertEqual(actual_num_mutations, uut_num_mutations)
def testDefaultValueWhenNotMutated(self): ''' Check that the default value, before mutation, is as the expected data ''' expected_data = 'Th3 L33ter ' uut = Template( name='uut', fields=[ String('Th'), BitField(value=3, length=20, encoder=ENC_INT_DEC), Static(' '), Container( name='leeter ', fields=[ Dynamic(key='hmm', default_value='L3'), String('\xde\xd7\xab', encoder=ENC_STR_BASE64_NO_NL), # 3ter RandomBytes(' ', min_length=1, max_length=100) ]) ]) self.assertEqual(uut.render().tobytes(), expected_data) uut.mutate() uut.reset() self.assertEqual(uut.render().tobytes(), expected_data)
def testHashTheSameAfterReset(self): container = Pad(pad_length=10 * 8, fields=String('abc'), pad_data='\xff') hash_after_creation = container.hash() container.mutate() hash_after_mutate = container.hash() self.assertEqual(hash_after_creation, hash_after_mutate) container.reset() hash_after_reset = container.hash() self.assertEqual(hash_after_creation, hash_after_reset) while container.mutate(): hash_after_mutate_all = container.hash() self.assertEqual(hash_after_creation, hash_after_mutate_all) container.render() hash_after_render_all = container.hash() self.assertEqual(hash_after_creation, hash_after_render_all)
def testHashTheSameAfterReset(self): container = Trunc(10 * 8, fields=String('abc')) hash_after_creation = container.hash() container.mutate() hash_after_mutate = container.hash() self.assertEqual(hash_after_creation, hash_after_mutate) container.reset() hash_after_reset = container.hash() self.assertEqual(hash_after_creation, hash_after_reset) while container.mutate(): hash_after_mutate_all = container.hash() self.assertEqual(hash_after_creation, hash_after_mutate_all) container.render() hash_after_render_all = container.hash() self.assertEqual(hash_after_creation, hash_after_render_all)
def testHashTheSameAfterReset(self): container = self.get_default_container(fields=[String('test_string')]) hash_after_creation = container.hash() container.mutate() hash_after_mutate = container.hash() self.assertEqual(hash_after_creation, hash_after_mutate) container.reset() hash_after_reset = container.hash() self.assertEqual(hash_after_creation, hash_after_reset) while container.mutate(): hash_after_mutate_all = container.hash() self.assertEqual(hash_after_creation, hash_after_mutate_all) container.render() hash_after_render_all = container.hash() self.assertEqual(hash_after_creation, hash_after_render_all)
def testConditionNotAppliesFirst(self): field = self.get_condition_field() condition = self.get_not_applies_first_condition() inner_field = String(ConditionTest.inner_field_value) condition_container = self.cls(condition=condition, fields=[inner_field], fuzzable=True) # This is done to allow field name resolution enclosing = Container(fields=[field, condition_container]) self.assertEqual(condition_container.render(), Bits()) while condition_container.mutate(): self.assertEqual(condition_container.render(), Bits()) condition_container.reset() field.mutate() self.assertEqual(condition_container.render(), inner_field.render()) while condition_container.mutate(): self.assertEqual(condition_container.render(), inner_field.render())
def testMultipleDependenciesDefaultValue(self): expected_data = ( '\x00\x00\x00\x74' + 'HAMBURGER' + '\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa' + '\xf3\xfe\xa2g8\xd6\xc1\xc2K\xef\x89\xe3\xd7\xcfh\xbaH\xf8\x83\x08JS\x82\xa5\x86f\x82\x9b\x18\xc5r\xa9' ) uut = Template(name='uut', fields=[ SizeInBytes(sized_field='uut', length=32, name='size'), Pad(name='content', fields=[ String(value='HAMBURGER'), ], pad_length=640, pad_data='\xaa'), Sha256(depends_on='content', name='hash'), ]) self.assertEqual(uut.render().tobytes(), expected_data) uut.mutate() uut.reset() self.assertEqual(uut.render().tobytes(), expected_data)
def testPrimitivesPush5(self): fields = [String('test_%d' % d) for d in range(5)] self._test_fields(push_fields=fields)
def testPrimitivesInit5(self): fields = [String('test_%d' % d) for d in range(5)] self._test_fields(init_fields=fields)
def testDifferentHashIfPadLengthIsDifferend(self): trunc1 = Trunc(11 * 8, fields=String('abc')) trunc2 = Trunc(10 * 8, fields=String('abc')) self.assertNotEqual(trunc1.hash(), trunc2.hash())
def testPadWhenFuzzable(self): field = String(name='padded', value='abc') uut = Pad(self.pad_length, fields=field, name='uut') self._testValuePadded(field, uut, self.pad_length, '\x00') while uut.mutate(): self._testValuePadded(field, uut, self.pad_length, '\x00')
def testValueTruncedNotFuzzable(self): field = String(name='trunced', value='abc') uut = Trunc(max_size=self.trunc_size, fields=field, fuzzable=False) self._testValueTrunced(field, uut, self.trunc_size) self.assertEqual(uut.num_mutations(), 0) self.assertFalse(uut.mutate())
def testDifferentHashIfPadDataIsDifferend(self): pad1 = Pad(pad_length=10 * 8, fields=String('abc'), pad_data='\x00') pad2 = Pad(pad_length=10 * 8, fields=String('abc'), pad_data='\xff') self.assertNotEqual(pad1.hash(), pad2.hash())
def testHashTheSameForTwoSimilarObjects(self): pad1 = Pad(pad_length=10 * 8, fields=String('abc'), pad_data='\xff') pad2 = Pad(pad_length=10 * 8, fields=String('abc'), pad_data='\xff') self.assertEqual(pad1.hash(), pad2.hash())
def testPadWhenNotFuzzable(self): field = String(name='padded', value='abc') uut = Pad(self.pad_length, fields=field, name='uut', fuzzable=False) self._testValuePadded(field, uut, self.pad_length, '\x00')
def testHashTheSameForTwoSimilarObjects(self): trunc1 = Trunc(10 * 8, fields=String('abc')) trunc2 = Trunc(10 * 8, fields=String('abc')) self.assertEqual(trunc1.hash(), trunc2.hash())
def testIsNotRenderedWhenNotFuzzable(self): field = String('abc') uut = Meta(name='uut', fields=[field], fuzzable=False) self.assertEqual(len(uut.render()), 0)