コード例 #1
0
 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)
コード例 #2
0
 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(), [])
コード例 #3
0
 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)
コード例 #4
0
 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)
コード例 #5
0
 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)
コード例 #6
0
 def testPrimitivesInit1Push1(self):
     init_fields = [
         String('test1'),
     ]
     push_fields = [
         String('test2'),
     ]
     self._test_fields(init_fields=init_fields, push_fields=push_fields)
コード例 #7
0
 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)
コード例 #8
0
 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)
コード例 #9
0
 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)
コード例 #10
0
 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)
コード例 #11
0
 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)
コード例 #12
0
 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)
コード例 #13
0
 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)
コード例 #14
0
 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)
コード例 #15
0
 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)
コード例 #16
0
    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())
コード例 #17
0
 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)
コード例 #18
0
 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
コード例 #19
0
    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())
コード例 #20
0
 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())
コード例 #21
0
 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)
コード例 #22
0
 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)
コード例 #23
0
 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)
コード例 #24
0
 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)
コード例 #25
0
 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)
コード例 #26
0
 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())
コード例 #27
0
 def testIsNotRenderedWhenNotFuzzable(self):
     field = String('abc')
     uut = Meta(name='uut', fields=[field], fuzzable=False)
     self.assertEqual(len(uut.render()), 0)
コード例 #28
0
 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')
コード例 #29
0
 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')
コード例 #30
0
 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())
コード例 #31
0
 def testPrimitivesPush5(self):
     fields = [String('test_%d' % d) for d in range(5)]
     self._test_fields(push_fields=fields)
コード例 #32
0
 def testPrimitivesInit5(self):
     fields = [String('test_%d' % d) for d in range(5)]
     self._test_fields(init_fields=fields)
コード例 #33
0
 def testDifferentHashIfPadLengthIsDifferend(self):
     trunc1 = Trunc(11 * 8, fields=String('abc'))
     trunc2 = Trunc(10 * 8, fields=String('abc'))
     self.assertNotEqual(trunc1.hash(), trunc2.hash())
コード例 #34
0
 def testHashTheSameForTwoSimilarObjects(self):
     trunc1 = Trunc(10 * 8, fields=String('abc'))
     trunc2 = Trunc(10 * 8, fields=String('abc'))
     self.assertEqual(trunc1.hash(), trunc2.hash())
コード例 #35
0
 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())