Ejemplo n.º 1
0
class ElementCountTests(CalculatedTestCase):

    __meta__ = False

    def setUp(self, cls=ElementCount):
        super(ElementCountTests, self).setUp(cls)
        self.length = 32
        self.bit_field = BitField(value=0, length=self.length)

    def get_default_field(self, fuzzable=False, correction=None):
        return self.cls(self.depends_on_name,
                        correction=correction,
                        length=self.length,
                        fuzzable=fuzzable,
                        name=self.uut_name)

    def calculate(self, field):
        self.bit_field.set_current_value(len(field.get_rendered_fields()))
        return self.bit_field.render()

    def testContainerWithInternalContainer(self):
        container = Container(name=self.depends_on_name,
                              fields=[
                                  String('abc'),
                                  String('def'),
                                  Container(name='counts_as_one',
                                            fields=[
                                                String('ghi'),
                                                String('jkl'),
                                            ])
                              ])
        uut = self.get_default_field()
        full = Container([container, uut])
        full.render()
        self.assertEqual(uut.render(), self.calculate(container))
        del full

    def testInternalContainer(self):
        internal_container = Container(name=self.depends_on_name,
                                       fields=[
                                           String('ghi', name='field3'),
                                           String('jkl', name='field4'),
                                       ])
        container = Container(name='this_doesnt_count',
                              fields=[
                                  String('abc', name='field1'),
                                  String('def', name='field2'),
                                  internal_container
                              ])
        uut = self.get_default_field()
        full = Container([container, uut])
        full.render()
        self.assertEqual(uut.render(), self.calculate(internal_container))
        del full

    def testInvalidCorrectionStr(self):
        with self.assertRaises(KittyException):
            self.get_default_field(correction='boom')
Ejemplo n.º 2
0
class SizeTests(CalculatedTestCase):
    __meta__ = False

    def setUp(self, cls=Size, length=32):
        super(SizeTests, self).setUp(cls)
        self.bit_field = BitField(value=0, length=length)
        self.length = length

    def get_default_field(self, length=None, calc_func=None, fuzzable=False):
        if length is None:
            length = self.length
        if calc_func is None:
            return self.cls(self.depends_on_name, length=length, fuzzable=fuzzable)
        else:
            return self.cls(self.depends_on_name, length=length, calc_func=calc_func, fuzzable=fuzzable)

    def calculate(self, value, calc_func=None):
        if calc_func:
            val = calc_func(value)
        else:
            val = len(value.bytes)
        self.bit_field.set_current_value(val)
        return self.bit_field.render()

    def test_custom_func_valid(self):
        func = lambda x: len(x)
        original_field = self.get_original_field()
        calculated_field = self.get_default_field(calc_func=func)
        container = Container([original_field, calculated_field])
        original_rendered = original_field.render()
        expected = self.calculate(original_rendered, calc_func=func)
        actual = calculated_field.render()
        self.assertEqual(expected, actual)
        while container.mutate():
            original_rendered = original_field.render()
            expected = self.calculate(original_rendered, calc_func=func)
            actual = calculated_field.render()
            self.assertEqual(expected, actual)

    def test_custom_func_invalid_arguments(self):
        with self.assertRaises(KittyException):
            self.cls(self.depends_on_name, length=8, calc_func=lambda x, y: 1)

    def test_custom_func_invalid_return_type_str(self):
        with self.assertRaises(KittyException):
            self.cls(self.depends_on_name, length=8, calc_func=lambda x: "")

    def test_custom_func_invalid_return_type_bits(self):
        with self.assertRaises(KittyException):
            self.cls(self.depends_on_name, length=8, calc_func=lambda x: Bits())

    def test_invalid_length_0(self):
        with self.assertRaises(KittyException):
            self.cls(self.depends_on_name, length=0)

    def test_invalid_length_negative(self):
        with self.assertRaises(KittyException):
            self.cls(self.depends_on_name, length=-3)
class ElementCountTests(CalculatedTestCase):

    __meta__ = False

    def setUp(self, cls=ElementCount):
        super(ElementCountTests, self).setUp(cls)
        self.length = 32
        self.bit_field = BitField(value=0, length=self.length)

    def get_default_field(self, fuzzable=False, correction=None):
        return self.cls(self.depends_on_name, correction=correction, length=self.length, fuzzable=fuzzable, name=self.uut_name)

    def calculate(self, field):
        self.bit_field.set_current_value(len(field.get_rendered_fields()))
        return self.bit_field.render()

    def testContainerWithInternalContainer(self):
        container = Container(
            name=self.depends_on_name,
            fields=[
                String('abc'),
                String('def'),
                Container(
                    name='counts_as_one',
                    fields=[
                        String('ghi'),
                        String('jkl'),
                    ])
            ])
        uut = self.get_default_field()
        full = Container([container, uut])
        full.render()
        self.assertEqual(uut.render(), self.calculate(container))
        del full

    def testInternalContainer(self):
        internal_container = Container(
            name=self.depends_on_name,
            fields=[
                String('ghi', name='field3'),
                String('jkl', name='field4'),
            ])
        container = Container(
            name='this_doesnt_count',
            fields=[
                String('abc', name='field1'),
                String('def', name='field2'),
                internal_container
            ])
        uut = self.get_default_field()
        full = Container([container, uut])
        full.render()
        self.assertEqual(uut.render(), self.calculate(internal_container))
        del full

    def testInvalidCorrectionStr(self):
        with self.assertRaises(KittyException):
            self.get_default_field(correction='boom')
Ejemplo n.º 4
0
class SizeTests(CalculatedTestCase):
    __meta__ = False

    def setUp(self, cls=Size, length=32):
        super(SizeTests, self).setUp(cls)
        self.bit_field = BitField(value=0, length=length)
        self.length = length

    def get_default_field(self, length=None, calc_func=None, fuzzable=False):
        if length is None:
            length = self.length
        if calc_func is None:
            return self.cls(self.depends_on_name, length=length, fuzzable=fuzzable, name='uut')
        else:
            return self.cls(self.depends_on_name, length=length, calc_func=calc_func, fuzzable=fuzzable, name='uut')

    def calculate(self, field, calc_func=None):
        value = field.render()
        if calc_func:
            val = calc_func(value)
        else:
            val = len(value.bytes)
        self.bit_field.set_current_value(val)
        return self.bit_field.render()

    def testCustomFuncValid(self):
        def func(x):
            return len(x)
        original_field = self.get_original_field()
        calculated_field = self.get_default_field(calc_func=func)
        container = Container([original_field, calculated_field])
        expected = self.calculate(original_field, calc_func=func)
        actual = calculated_field.render()
        self.assertEqual(expected, actual)
        while container.mutate():
            expected = self.calculate(original_field, calc_func=func)
            actual = calculated_field.render()
            self.assertEqual(expected, actual)

    def testInvalidLength0(self):
        with self.assertRaises(KittyException):
            self.cls(self.depends_on_name, length=0)

    def testInvalidLengthNegative(self):
        with self.assertRaises(KittyException):
            self.cls(self.depends_on_name, length=-3)

    def testSizeInclusiveAlone(self):
        self.length = 32
        container = Container(
            name=self.depends_on_name,
            fields=[
                self.get_default_field()
            ])
        rendered = container.render()
        self.assertEqual(len(rendered), self.length)
        self.assertEquals(unpack('>I', rendered.tobytes())[0], self.length / 8)
class SizeTests(CalculatedTestCase):
    __meta__ = False

    def setUp(self, cls=Size, length=32):
        super(SizeTests, self).setUp(cls)
        self.bit_field = BitField(value=0, length=length)
        self.length = length

    def get_default_field(self, length=None, calc_func=None, fuzzable=False):
        if length is None:
            length = self.length
        if calc_func is None:
            return self.cls(self.depends_on_name, length=length, fuzzable=fuzzable, name=self.uut_name)
        else:
            return self.cls(self.depends_on_name, length=length, calc_func=calc_func, fuzzable=fuzzable, name=self.uut_name)

    def calculate(self, field, calc_func=None):
        value = field.render()
        if calc_func:
            val = calc_func(value)
        else:
            val = len(value.bytes)
        self.bit_field.set_current_value(val)
        return self.bit_field.render()

    def testCustomFuncValid(self):
        def func(x):
            return len(x)
        original_field = self.get_original_field()
        calculated_field = self.get_default_field(calc_func=func)
        container = Container([original_field, calculated_field])
        expected = self.calculate(original_field, calc_func=func)
        actual = calculated_field.render()
        self.assertEqual(expected, actual)
        while container.mutate():
            expected = self.calculate(original_field, calc_func=func)
            actual = calculated_field.render()
            self.assertEqual(expected, actual)

    def testInvalidLength0(self):
        with self.assertRaises(KittyException):
            self.cls(self.depends_on_name, length=0)

    def testInvalidLengthNegative(self):
        with self.assertRaises(KittyException):
            self.cls(self.depends_on_name, length=-3)

    def testSizeInclusiveAlone(self):
        self.length = 32
        container = Container(
            name=self.depends_on_name,
            fields=[
                self.get_default_field()
            ])
        rendered = container.render()
        self.assertEqual(len(rendered), self.length)
        self.assertEquals(unpack('>I', rendered.tobytes())[0], self.length / 8)
Ejemplo n.º 6
0
class SizeInBytesTest(CalculatedTestCase):
    __meta__ = False

    def setUp(self, cls=SizeInBytes, length=32):
        super(SizeInBytesTest, self).setUp(cls)
        self.bit_field = BitField(value=0, length=length)
        self.length = length

    def get_default_field(self, fuzzable=False):
        return self.cls(self.depends_on_name, length=self.length, fuzzable=fuzzable)

    def calculate(self, value):
        self.bit_field.set_current_value(len(value.bytes))
        return self.bit_field.render()
class SizeInBytesTest(CalculatedTestCase):
    __meta__ = False

    def setUp(self, cls=SizeInBytes, length=32):
        super(SizeInBytesTest, self).setUp(cls)
        self.bit_field = BitField(value=0, length=length)
        self.length = length

    def get_default_field(self, fuzzable=False):
        return self.cls(self.depends_on_name, length=self.length, fuzzable=fuzzable, name=self.uut_name)

    def calculate(self, field):
        value = field.render()
        self.bit_field.set_current_value(len(value.bytes))
        return self.bit_field.render()
Ejemplo n.º 8
0
class IndexOfTestCase(CalculatedTestCase):

    __meta__ = False

    def setUp(self, cls=IndexOf):
        super(IndexOfTestCase, self).setUp(cls)
        self.length = 32
        self.bit_field = BitField(value=0, length=self.length)

    def get_default_field(self, fuzzable=False):
        return self.cls(self.depends_on_name, length=self.length, fuzzable=fuzzable, name='uut')

    def calculate(self, field):
        rendered = field._enclosing.get_rendered_fields()
        if field in rendered:
            value = rendered.index(field)
        else:
            value = len(rendered)
        self.bit_field.set_current_value(value)
        return self.bit_field.render()

    def _testCorrectIndex(self, expected_index):
        field_list = [String('%d' % i) for i in range(20)]
        field_list[expected_index] = self.get_original_field()
        uut = self.get_default_field()
        t = Container(name='level1', fields=[uut, Container(name='level2', fields=field_list)])
        rendered = uut.render().tobytes()
        result = unpack('>I', rendered)[0]
        self.assertEqual(result, expected_index)
        del t

    def testCorrectIndexFirst(self):
        self._testCorrectIndex(0)

    def testCorrectIndexMiddle(self):
        self._testCorrectIndex(10)

    def testCorrectIndexLast(self):
        self._testCorrectIndex(19)

    def testFieldNotRenderedAlone(self):
        expected_index = 0
        uut = self.get_default_field()
        the_field = Static(name=self.depends_on_name, value='')
        t = Container(name='level1', fields=[uut, Container(name='level2', fields=the_field)])
        rendered = uut.render().tobytes()
        result = unpack('>I', rendered)[0]
        self.assertEqual(result, expected_index)
        del t

    def testFieldNotRenderedWithOtherFields(self):
        expected_index = 3
        uut = self.get_default_field()
        fields = [
            Static(name=self.depends_on_name, value=''),
            Static('field1'),
            Static('field2'),
            Static('field3'),
        ]
        t = Container(name='level1', fields=[uut, Container(name='level2', fields=fields)])
        rendered = uut.render().tobytes()
        result = unpack('>I', rendered)[0]
        self.assertEqual(result, expected_index)
        del t
class IndexOfTestCase(CalculatedTestCase):

    __meta__ = False

    def setUp(self, cls=IndexOf):
        super(IndexOfTestCase, self).setUp(cls)
        self.length = 32
        self.bit_field = BitField(value=0, length=self.length)

    def get_default_field(self, fuzzable=False):
        return self.cls(self.depends_on_name, length=self.length, fuzzable=fuzzable, name=self.uut_name)

    def calculate(self, field):
        rendered = field._enclosing.get_rendered_fields()
        if field in rendered:
            value = rendered.index(field)
        else:
            value = len(rendered)
        self.bit_field.set_current_value(value)
        return self.bit_field.render()

    def _testCorrectIndex(self, expected_index):
        field_list = [String('%d' % i) for i in range(20)]
        field_list[expected_index] = self.get_original_field()
        uut = self.get_default_field()
        t = Container(name='level1', fields=[uut, Container(name='level2', fields=field_list)])
        rendered = uut.render().tobytes()
        result = unpack('>I', rendered)[0]
        self.assertEqual(result, expected_index)
        del t

    def testCorrectIndexFirst(self):
        self._testCorrectIndex(0)

    def testCorrectIndexMiddle(self):
        self._testCorrectIndex(10)

    def testCorrectIndexLast(self):
        self._testCorrectIndex(19)

    def testFieldNotRenderedAlone(self):
        expected_index = 0
        uut = self.get_default_field()
        the_field = Static(name=self.depends_on_name, value='')
        t = Container(name='level1', fields=[uut, Container(name='level2', fields=the_field)])
        rendered = uut.render().tobytes()
        result = unpack('>I', rendered)[0]
        self.assertEqual(result, expected_index)
        del t

    def testFieldNotRenderedWithOtherFields(self):
        expected_index = 3
        uut = self.get_default_field()
        fields = [
            Static(name=self.depends_on_name, value=''),
            Static('field1'),
            Static('field2'),
            Static('field3'),
        ]
        t = Container(name='level1', fields=[uut, Container(name='level2', fields=fields)])
        rendered = uut.render().tobytes()
        result = unpack('>I', rendered)[0]
        self.assertEqual(result, expected_index)
        del t