def test_union_primitive_length(self): self._check_length(2, UInt(1, 'a', 1), UInt(2, 'b', 1)) self._check_length(1, UInt(1, 'a', 1), UInt(1, 'b', 1)) self._check_length(4, UInt(4, 'a', 4), UInt(1, 'b', 1), UInt(2, 'c', 1)) self._check_length(16, UInt(1, 'a', 1), Char(16, 'b', 1)) self._check_length(10, Char(10, 'a', None), Char(10, 'b', None))
def test_decode_dynamic_primitive(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) tmp.add(UInt(1, 'len', None)) tmp.add(Char('len', 'chars', None)) tmp.add(UInt(1, 'len2', None)) tmp.add(Char('len2', 'chars2', None)) decoded = tmp.decode(to_bin('0x 04 6162 6364 02 6566')) self.assertEqual(decoded.len.int, 4) self.assertEqual(decoded.chars.ascii, 'abcd') self.assertEqual(decoded.chars2.ascii, 'ef')
def test_encode_dynamic_primitive_with_defined_value(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) tmp.add(UInt(4, 'len', '4')) tmp.add(Char('len', 'chars', 'abcd')) tmp.add(UInt(4, 'len2', None)) tmp.add(Char('len2', 'chars2', 'ef')) encoded = tmp.encode({'len2': '6'}, {}) self.assertEqual(encoded.chars.ascii, 'abcd') self.assertEqual(len(encoded.chars), 4) self.assertEqual(encoded.chars2.ascii, 'ef') self.assertEqual(len(encoded.chars2), 6)
def test_fail_on_dynamic_length(self): union = UnionTemplate('NotLegal', 'dymagic', parent=None) union.add(UInt(2, 'bar', None)) struct = StructTemplate('Foo', 'foo', parent=None) struct.add(UInt(1, 'len', 22)) struct.add(Char('len', 'dymagic', 'foo')) self.assertRaises(Exception, union.add, struct)
def test_encode_dynamic_primitive_automatically(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) tmp.add(UInt(4, 'len', None)) tmp.add(Char('len', 'chars', 'abcd')) encoded = tmp.encode({}, {}) self.assertEqual(encoded.chars.ascii, 'abcd') self.assertEqual(encoded.len.int, 4)
def test_char_static_field(self): field = Char(5, "char_field", 'foo') self.assertTrue(field.length.static) self.assertEquals(field.name, "char_field") self.assertEquals(field.default_value, 'foo') self.assertEquals(field.type, 'chars') self.assertEquals(field.encode({}, {}, None)._raw, 'foo\x00\x00') self.assertEquals(field.encode({}, {}, None).bytes, 'foo\x00\x00')
def test_dynamic_field_validation(self): struct = StructTemplate('Foo', 'foo', parent=None) struct.add(UInt(2, 'len', None)) struct.add(Char('len', 'text', None)) encoded = struct.encode({'foo.len': 6, 'foo.text': 'fobba'}) self._should_pass( struct.validate({'foo': encoded}, {'foo.text': 'fobba'})) self._should_fail( struct.validate({'foo': encoded}, {'foo.text': 'fob'}), 1)
def test_set_char_value_from_message_field(self): msg_field = Field('chars', 'char_field', '\x00a\x00b', aligned_len=5) field = Char(5, "char_field", '') self.assertEquals( field.encode({ 'char_field': msg_field }, {}, None)._raw, '\x00a\x00b\x00') self.assertEquals( field.encode({ 'char_field': msg_field }, {}, None).bytes, '\x00a\x00b\x00')
def test_validate_chars(self): field = Field('chars', 'field', 'foo\x00\x00') field_regEx = Field('chars', 'field', '{ Message In Braces }') self._should_pass(Char(5, 'field', 'foo').validate({'field': field}, {})) self._should_pass(Char(5, 'field', '(what|foo|bar)').validate({'field': field}, {})) self._should_pass(Char(5, 'field', 'REGEXP:^{[a-zA-Z ]+}$').validate({'field': field_regEx}, {})) self._should_pass(Char(5, 'field', 'REGEXP:^foo').validate({'field': field}, {})) self._should_pass(Char(5, 'field', 'REGEXP:').validate({'field': field}, {})) self._should_fail(Char(5, 'field', 'REGEXP:^abc').validate({'field': field}, {}), 1)
def test_decode_chars(self): field_template = Char(2, 'field', 6) decoded = field_template.decode(to_bin('0xcafe'), {}) self.assertEquals(decoded.hex, '0xcafe')
def test_validate_chars(self): field = Field('chars', 'field', 'foo\x00\x00') self._should_pass( Char(5, 'field', 'foo').validate({'field': field}, {})) self._should_pass( Char(5, 'field', '(what|foo|bar)').validate({'field': field}, {}))
def test_little_endian_char_decode(self): template = Char(5, 'field', None) field = template.decode('hello', None, little_endian=True) self.assertEquals(field._raw, 'hello') self.assertEquals(field.bytes, 'hello') self.assertEquals(field.ascii, 'hello')
def test_add_field_with_length_reference_missing(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) tmp.add(UInt(2, 'len', None)) str = StructTemplate('FooType', 'foo', tmp) self.assertRaises(AssertionError, str.add, Char('notfound', "bar"))
def test_add_field_with_length_reference_to_parent(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) tmp.add(UInt(2, 'len', None)) str = StructTemplate('FooType', 'foo', tmp) str.add(Char('len', "bar"))
def test_non_existing_dynamic_variable(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) self.assertRaises(Exception, tmp.add, Char('not_existing', 'foo', None))