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 testExceptionIfDefaultKeyNotInDict(self):
     with self.assertRaises(KittyException):
         field_dict = {
             1: String('1'),
             2: String('2'),
             3: String('3'),
         }
         self.get_uut(field_dict, 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)
 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 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 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 testIsFuzzable(self):
     field = String('abc')
     uut = Meta(name=self.uut_name, 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 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 testIsFuzzable(self):
     field = String('abc')
     uut = Meta(name=self.uut_name, 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 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 testNumMutations(self):
     field = String(name='padded', value='abc')
     uut = Pad(self.pad_length, fields=field, name=self.uut_name)
     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 testNumMutations(self):
     field = String(name='padded', value='abc')
     uut = Pad(self.pad_length, fields=field, name=self.uut_name)
     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 testNumMutations(self):
     field = String(name='trunced', value='abc')
     uut = Trunc(self.trunc_size, fields=field, name=self.uut_name)
     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 testNumMutations(self):
     field = String(name='trunced', value='abc')
     uut = Trunc(self.trunc_size, fields=field, name=self.uut_name)
     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 testCopy(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'])
     ]
     uut = self.get_default_container(fields)
     uut_copy = uut.copy()
     uut_mutations = self.get_all_mutations(uut, reset=False)
     copy_mutations = self.get_all_mutations(uut_copy, reset=False)
     self.assertEqual(uut_mutations, copy_mutations)
 def testNumMutationsMatchesMutateCount(self):
     field_dict = {
         1: String('1'),
         2: String('2'),
         3: String('3'),
     }
     uut = self.get_uut(field_dict, 2)
     key_field = self.get_default_key_field()
     container = Container([uut, key_field])
     num_mutations = uut.num_mutations()
     actual_num_mutations = len(self.get_all_mutations(uut))
     self.assertEqual(num_mutations, actual_num_mutations)
     del container
 def testNumMutationsIsAtLeastSumOfFieldsNumMutations(self):
     field_dict = {
         1: String('1'),
         2: String('2'),
         3: String('3'),
     }
     uut = self.get_uut(field_dict, 2)
     key_field = self.get_default_key_field()
     container = Container([uut, key_field])
     num_mutations = uut.num_mutations()
     actual_num_mutations = sum(v.num_mutations() for k, v in field_dict.items())
     self.assertGreaterEqual(num_mutations, actual_num_mutations)
     del container
 def testZeroMutationsIfNotFuzzable(self):
     field_dict = {
         1: String('1'),
         2: String('2'),
         3: String('3'),
     }
     uut = self.get_uut(field_dict, 2, fuzzable=False)
     key_field = self.get_default_key_field()
     container = Container([uut, key_field])
     num_mutations = uut.num_mutations()
     actual_num_mutations = len(self.get_all_mutations(uut))
     self.assertEqual(num_mutations, actual_num_mutations)
     self.assertEqual(num_mutations, 0)
     del container
 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)
     if len(container.render()):
         self.assertEqual(len(container.get_rendered_fields()), 1)
     while container.mutate():
         if len(container.render()):
             self.assertEqual(len(container.get_rendered_fields()), 1)
         else:
             self.assertEqual(len(container.get_rendered_fields()), 0)
 def testKeyFieldValueSetBySwitchWhenMutating(self):
     field_dict = {
         1: String('1'),
         2: String('2'),
         3: String('3'),
     }
     uut = self.get_uut(field_dict, 2)
     key_field = self.get_default_key_field()
     container = Container([uut, key_field])
     while uut.mutate():
         key_rendered = key_field.render().tobytes()
         key_value = key_field._current_value
         self.assertEqual(key_value, unpack('>I', key_rendered)[0])
         self.assertEqual(key_value, uut._keys[uut._field_idx])
     del container
Esempio n. 23
0
def createPayload(service, function, arguments):
    # container for created nodes
    action_param_nodes = []
    for k in filter(lambda x: x.get('direction') != 'out', arguments):
        action_param_nodes.append(XmlElement(name=k['name'], element_name=k['name'], content='Value'))
    # function attribute
    u = [Static('urn:schemas-upnp-org:service:'), String(service), Static(':1')]
    # create the Function element and set its attribute
    function_element = [XmlElement(
        name=function,
        element_name='u:%s' % function,
        attributes=[XmlAttribute(name='attr_u', attribute='xmlns:u', value=u)],
        content=action_param_nodes
    )]
    # create the Body element
    body = [XmlElement(name='Body', element_name='s:Body', content=function_element)]
    # create the Envelope element and set its attributes
    envelope = XmlElement(
        name='Envelope',
        element_name='s:Envelope',
        content=body,
        attributes=[
            XmlAttribute(name='attr_s', attribute='xmlns:s', value='http://schemas.xmlsoap.org/soap/envelope/'),
            XmlAttribute(name='attr_encodingStyle', attribute='s:encodingStyle', value='http://schemas.xmlsoap.org/soap/encoding/'),
        ],
    )
    return Template(envelope, name='upnp_body')
 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 get_default_container(self,
                           fields=[],
                           fuzzable=True,
                           mutated_field=None):
     if mutated_field is None:
         mutated_field = String('static field')
     return OneOf(fields=fields, fuzzable=fuzzable, name=self.uut_name)
 def testPadWhenNotFuzzable(self):
     field = String(name='padded', value='abc')
     uut = Pad(self.pad_length,
               fields=field,
               name=self.uut_name,
               fuzzable=False)
     self._testValuePadded(field, uut, self.pad_length, '\x00')
 def testSwitchWithStaticKeyField(self):
     field_dict = {
         '1': String('1'),
         '2': String('2'),
         '3': String('3'),
     }
     default_key = '3'
     uut = self.get_uut(field_dict, default_key)
     key_field = Static(name=self.key_field_name, value='someval')
     container = Container([uut, key_field])
     while key_field.mutate():
         key = key_field._current_value
         uut_rendered = uut.render()
         uut_key = key if key in field_dict else default_key
         field_rendred = field_dict[uut_key].render()
         self.assertEqual(uut_rendered, field_rendred)
     del container
 def testDelimiterExist(self):
     num_elems = 3
     fields = [Static('A') for i in range(num_elems)]
     uut = self.get_uut(fields=fields, delim=String('/'))
     mutations = [m.tobytes().decode() for m in self.get_all_mutations(uut)]
     for m in mutations:
         if m != '':
             self.assertEqual(m.count('/'), (len(m) - 1) // 2)
 def testSwitchFieldSetByKeyFieldWhenNotMutating(self):
     field_dict = {
         1: String('1'),
         2: String('2'),
         3: String('3'),
     }
     default_key = 2
     uut = self.get_uut(field_dict, default_key)
     key_field = self.get_default_key_field()
     container = Container([uut, key_field])
     while key_field.mutate():
         key = key_field._current_value
         uut_rendered = uut.render()
         uut_key = key if key in field_dict else default_key
         field_rendred = field_dict[uut_key].render()
         self.assertEqual(uut_rendered, field_rendred)
     del container
 def testSwitchWithStringKey(self):
     field_dict = {
         '1': String('1'),
         '2': String('2'),
         '3': String('3'),
     }
     default_key = '3'
     uut = self.get_uut(field_dict, default_key)
     key_field = String(name=self.key_field_name, value='someval')
     container = Container([uut, key_field])
     while key_field.mutate():
         key = key_field._current_value
         uut_rendered = uut.render()
         uut_key = key if key in field_dict else default_key
         field_rendred = field_dict[uut_key].render()
         self.assertEqual(uut_rendered, field_rendred)
     del container
    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())

        del enclosing
    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())

        del enclosing
 def testAlwaysRenderedAsEmptyBits(self):
     field = String('abc')
     uut = Meta(name=self.uut_name, fields=[field], fuzzable=True)
     self.assertEqual(len(uut.render()), 0)
     while uut.mutate():
         self.assertEqual(len(uut.render()), 0)
     uut.reset()
     self.assertEqual(len(uut.render()), 0)
     while uut.mutate():
         self.assertEqual(len(uut.render()), 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())
     del enclosing
 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)