Exemplo n.º 1
0
def _check_encoded_data(spec, sourcefile, actual, actual_xml, require_exact_encoding):
    sourcefile.seek(0)
    expected = sourcefile.read()
    if actual != expected:
        # The data is different, but it is possibly due to the data being able
        # to be encoded in multiple ways. Try re-decoding the data to compare
        # against the original xml.
        try:
            regenerated_xml = xmlout.to_string(spec.decode(dt.Data(actual)))
            assert_xml_equivalent(actual_xml, regenerated_xml)
        except Exception, ex:
            raise Exception('Re-decoding of encoded data failed: %s' % str(ex))

        if require_exact_encoding:
            # The files are different, but the visible entries are identical.
            # Re-compare the files in verbose mode to give a better error.
            expected_data = dt.Data(expected)
            actual_data = dt.Data(actual)
            expected_xml = xmlout.to_string(spec.decode(expected_data), verbose=True)
            actual_xml = xmlout.to_string(spec.decode(actual_data), verbose=True)

            assert_xml_equivalent(expected_xml, actual_xml)

            # The verbose decode shows identical files. We must have different
            # trailing data!
            raise Exception('Unexpected trailing data. Original file has '
                    '%s, re-encode has %s.' % (expected_data, actual_data))
Exemplo n.º 2
0
    def test_verbose(self):
        sequence = seq.Sequence("bob", [
            fld.Field("cat:", 8, fld.Field.INTEGER),
            fld.Field("dog", 24, fld.Field.TEXT)])
        text = xml.to_string(sequence, dt.Data.from_hex('6d7a6970'), verbose=True)
        expected = """<bob>
    <cat_>109<!-- hex (1 bytes): 6d --></cat_>
    <dog>zip<!-- hex (3 bytes): 7a6970 --></dog>
</bob>
"""
        self.assertEqual(expected, text)

        # Now test that we can re-encode verbose generated xml...
        data = xml.encode(sequence, text)
        self.assertEqual("mzip", data.bytes())
Exemplo n.º 3
0
def _check_encoded_data(spec, sourcefile, actual, actual_xml, require_exact_encoding):
    sourcefile.seek(0)
    expected = sourcefile.read()
    if actual != expected:
        # The data is different, but it is possibly due to the data being able
        # to be encoded in multiple ways. Try re-decoding the data to compare
        # against the original xml.
        try:
            regenerated_xml = xmlout.to_string(spec, dt.Data(actual))
            assert_xml_equivalent(actual_xml, regenerated_xml)
        except Exception, ex:
            raise Exception('Re-decoding of encoded data failed: %s' % str(ex))

        if require_exact_encoding:
            raise Exception("Encoded data doesn't match, but we require exact encoding!")
Exemplo n.º 4
0
 def test_verbose_nameless_entry(self):
     hidden = fld.Field('', 8, fld.Field.INTEGER, constraints=[Equals(0)])
     spec = seq.Sequence('blah', [hidden])
     text = xml.to_string(spec, dt.Data('\x00'), verbose=True)
     self.assertEqual('<blah>\n    <_hidden><!-- hex (1 bytes): 00 --></_hidden>\n</blah>\n', text)
Exemplo n.º 5
0
 def test_nameless_entry(self):
     hidden = fld.Field('', 8, fld.Field.INTEGER, constraints=[Equals(0)])
     spec = seq.Sequence('blah', [hidden])
     text = xml.to_string(spec, dt.Data('\x00'))
     self.assertEqual('<blah></blah>\n', text)
Exemplo n.º 6
0
 def test_re_encoding_of_whitespace(self):
     spec = fld.Field('blah', 64, fld.Field.TEXT)
     text = xml.to_string(spec, dt.Data('  bob   '))
     data = xml.encode(spec, text)
     self.assertEqual("  bob   ", data.bytes())
Exemplo n.º 7
0
 def test_hidden_entry(self):
     sequence = seq.Sequence("bob", [
         fld.Field("cat:", 8, fld.Field.INTEGER),
         fld.Field("dog", 24, fld.Field.TEXT)])
     text = xml.to_string(sequence, dt.Data.from_hex('6e7a6970'))
     self.assertEqual("<bob>\n    <dog>zip</dog>\n</bob>\n", text)
Exemplo n.º 8
0
 def test_field(self):
     field = fld.Field("bob", 8)
     text = xml.to_string(field, dt.Data.from_hex('8e'))
     self.assertEqual("<bob>10001110</bob>\n", text)
Exemplo n.º 9
0
 def test_sequence_with_expected_value(self):
     a = seq.Sequence('a', [], value=expr.compile('1'), constraints=[Equals(1)])
     text = xml.to_string(a, dt.Data())
     self.assertEqual('<a></a>\n', text)
Exemplo n.º 10
0
 def test_sequence_with_children_and_value(self):
     a = seq.Sequence('a', [fld.Field('b', length=8, format=fld.Field.INTEGER)], value=expr.compile('11'))
     text = xml.to_string(a, dt.Data('\xff'))
     self.assertEqual('<a>\n    <b>255</b>\n    11\n</a>\n', text)
Exemplo n.º 11
0
 def test_different_child_name(self):
     digit = fld.Field('digit:', length=8)
     number = seq.Sequence('number', [digit], value=expr.compile("${digit:} - 48") )
     header = seq.Sequence('header', [ent.Child('length', number), fld.Field('data', length=expr.compile('${length} * 8'), format=fld.Field.TEXT)])
     text = xml.to_string(header, dt.Data('5abcde'))
     self.assertEqual('<header>\n    <length>5</length>\n    <data>abcde</data>\n</header>\n', text)
Exemplo n.º 12
0
 def test_field_with_expected_value(self):
     a = fld.Field('a', 8, fld.Field.INTEGER, constraints=[Equals(0)])
     spec = seq.Sequence('blah', [a])
     text = xml.to_string(spec, dt.Data('\x00'))
     self.assertEqual('<blah>\n    <a></a>\n</blah>\n', text)
Exemplo n.º 13
0
 def decode_file(self, spec, common, sourcefile, should_check_encoding=True, require_exact_encoding=False):
     data = dt.Data(sourcefile)
     try:
         xml = xmlout.to_string(spec.decode(data))
     except bdec.DecodeError, ex:
         raise ExecuteError(3, ex)