def test_small_field_with_expected_value(self): # Test saving a small field with length that isn't a multiple of # either... a = fld.Field('a', length=3, constraints=[Equals(dt.Data('\x02', 5))]) expected = """ <protocol> <field name="a" length="3" value="0x02" /> </protocol>""" assert_xml_equivalent(expected, xml.save(a))
def test_sequenceof_with_end_entry(self): a = fld.Field('a', length=8) b = sof.SequenceOf('b', seq.Sequence('c', [a]), count=None, end_entries=[a]) expected = """<protocol> <sequenceof name="b"> <sequence name="c"> <field name="a" length="8"><end-sequenceof/></field> </sequence> </sequenceof> </protocol>""" assert_xml_equivalent(expected, xml.save(b))
def test_expression(self): a = seq.Sequence('a', [ fld.Field('b', format=fld.Field.INTEGER, length=8), fld.Field('c', length=expr.compile('${a} * 8'))]) expected = """<protocol> <sequence name="a"> <field name="b" length="8" type="integer" /> <field name="c" length="(${a} * 8)" /> </sequence> </protocol>""" assert_xml_equivalent(expected, xml.save(a))
def test_choice(self): a = chc.Choice('a', [ fld.Field('b', length=8, constraints=[Equals(dt.Data('\x01'))]), fld.Field('c', length=8, constraints=[Equals(dt.Data('\x02'))])]) expected = """<protocol> <choice name="a"> <field name="b" length="8" value="0x01" /> <field name="c" length="8" value="0x02" /> </choice> </protocol>""" assert_xml_equivalent(expected, xml.save(a))
def test_optional_entry(self): # Test loading an optional entry text = ''' <protocol> <sequence name="a"> <field name="has footer" length="8" /> <field name="data" length="8" /> <field name="footer" length="8" if="${has footer} > 0" /> </sequence> </protocol>''' a = loads(text)[0] print xml.save(a) # Test decoding without a footer list(a.decode(dt.Data('\x00\x00'))) # Test decoding with a footer data = dt.Data('\x01\x00\x00') list(a.decode(data)) self.assertEqual(0, len(data)) self.assertRaises(ConstraintError, list, a.decode(dt.Data('\x01\x00')))
def test_sequence(self): a = seq.Sequence('a', [fld.Field('b', length=8)]) c = seq.Sequence('c', [a, fld.Field('d', length=16)]) expected = """<protocol> <sequence name="c"> <sequence name="a"> <field name="b" length="8" /> </sequence> <field name="d" length="2 * 8" /> </sequence> </protocol>""" assert_xml_equivalent(expected, xml.save(c))
def test_common_entries(self): a = fld.Field('a', length=8) b = seq.Sequence('b', [a, a]) expected = """<protocol> <sequence name="b"> <reference name="a" /> <reference name="a" /> </sequence> <common> <field name="a" length="8" /> </common> </protocol>""" assert_xml_equivalent(expected, xml.save(b, [a, b]))
def test_sequenceof_with_count(self): a = sof.SequenceOf('a', fld.Field('b', length=8), count=4) expected = '<protocol><sequenceof name="a" count="4"><field name="b" length="8" /></sequenceof></protocol>' assert_xml_equivalent(expected, xml.save(a))
def test_not_equals(self): a = fld.Field('a', format=fld.Field.INTEGER, length=8, constraints=[NotEquals(0)]) assert_xml_equivalent('<protocol><field name="a" type="integer" length="8" not_equal="0" /></protocol>', xml.save(a))
def test_text_field_with_expected_value(self): a = fld.Field('a', format=fld.Field.TEXT, length=32, constraints=[Equals('abcd')]) assert_xml_equivalent('<protocol><field name="a" type="text" length="4 * 8" value="abcd" /></protocol>', xml.save(a))
def test_text_field(self): a = fld.Field('a', format=fld.Field.TEXT, length=32) assert_xml_equivalent('<protocol><field name="a" type="text" length="4 * 8" /></protocol>', xml.save(a))
def test_field_expected_value(self): a = fld.Field('a', length=8, constraints=[Equals(dt.Data('\x63'))]) assert_xml_equivalent('<protocol><field name="a" length="8" value="0x63" /></protocol>', xml.save(a))
def test_simple_field(self): a = fld.Field('a', length=8) assert_xml_equivalent('<protocol><field name="a" length="8" /></protocol>', xml.save(a))