class TestLittleEndianProtocol(TestCase): def setUp(self): self._protocol = Protocol('TestProtocol', little_endian=True) self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', '0xcafe')) self.tmp.add(UInt(2, 'field_2', '0xbabe')) def test_encode_little_endian_header(self): encoded = self.tmp.encode({}, {}) self.assertEquals(encoded._header.msgId.hex, '0x0005') self.assertEquals(encoded._header.length.hex, '0x0008') self.assertEquals(encoded._header.msgId._raw, to_bin('0x0500')) self.assertEquals(encoded._header.length._raw, to_bin('0x0800')) def test_decode_little_endian_header(self): byte_stream = MockStream(to_bin('0x0500 0800 cafe babe')) self._msg_stream = MessageStream(byte_stream, self._protocol, threading.RLock()) decoded = self._msg_stream.get(self.tmp) self.assertEquals(decoded._header.msgId.hex, '0x0005') self.assertEquals(decoded._header.msgId._raw, to_bin('0x0500')) self.assertEquals(decoded.field_1.hex, '0xcafe') self.assertEquals(decoded.field_1._raw, to_bin('0xcafe')) self.assertEquals(decoded.field_2.hex, '0xbabe') self.assertEquals(decoded.field_2._raw, to_bin('0xbabe'))
class TestStructs(TestCase): def test_access_struct(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('StructuredRequest', self._protocol, {}) struct = get_pair() self.tmp.add(struct) msg = self.tmp.encode({}, {}) self.assertEquals(msg.pair.first.int, 1) def test_create_struct(self): struct = get_pair() self.assertEquals(struct.name, 'pair') def test_add_fields_to_struct(self): struct = get_pair() encoded = struct.encode({}, {}) self.assertEquals(encoded.first.int, 1) def test_add_fields_to_struct_and_override_values(self): struct = get_pair() encoded = struct.encode({'pair.first': 42}, {}) self.assertEquals(encoded.first.int, 42) def test_yo_dawg_i_heard(self): str_str = get_recursive_struct() encoded = str_str.encode({}, {}) self.assertEquals(encoded.pair.first.int, 1) def test_get_recursive_names(self): pair = get_pair() names = pair._get_params_sub_tree({'pair.foo': 0, 'pairnotyourname.ploo': 2, 'pair.goo.doo': 3}) self.assertEquals(len(names), 2) self.assertEquals(names['foo'], 0) self.assertEquals(names['goo.doo'], 3) def test_set_recursive(self): str_str = get_recursive_struct() encoded = str_str.encode({'str_str.pair.first': 42}, {}) self.assertEquals(encoded.pair.first.int, 42) def test_decode_several_structs(self): str_list = get_struct_list() decoded = str_list.decode(to_bin('0xcafebabe d00df00d'), {}) self.assertEquals(decoded[0].first.hex, '0xcafe') self.assertEquals(decoded[1].second.hex, '0xf00d') def test_length_of_struct(self): pair = get_pair() encoded = pair.encode({}, {}) self.assertEquals(len(encoded), 4) def test_decode_struct(self): pair = get_pair() decoded = pair.decode(to_bin('0xcafebabe'), {}) self.assertEquals(decoded.first.hex, '0xcafe') self.assertEquals(decoded.second.hex, '0xbabe')
class TestLittleEndianProtocol(TestCase): def setUp(self): self._protocol = Protocol('TestProtocol', little_endian=True) self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', '0xcafe')) self.tmp.add(UInt(2, 'field_2', '0xbabe')) def test_encode_little_endian_header(self): encoded = self.tmp.encode({}, {}) self.assertEqual(encoded._header.msgId.hex, '0x0005') self.assertEqual(encoded._header.length.hex, '0x0008') self.assertEqual(encoded._header.msgId._raw, to_bin('0x0500')) self.assertEqual(encoded._header.length._raw, to_bin('0x0800')) def test_decode_little_endian_header(self): byte_stream = MockStream(to_bin('0x0500 0800 cafe babe')) self._msg_stream = MessageStream(byte_stream, self._protocol) decoded = self._msg_stream.get(self.tmp) self.assertEqual(decoded._header.msgId.hex, '0x0005') self.assertEqual(decoded._header.msgId._raw, to_bin('0x0500')) self.assertEqual(decoded.field_1.hex, '0xcafe') self.assertEqual(decoded.field_1._raw, to_bin('0xcafe')) self.assertEqual(decoded.field_2.hex, '0xbabe') self.assertEqual(decoded.field_2._raw, to_bin('0xbabe'))
def setUp(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', 1)) self.tmp.add(UInt(2, 'field_2', 2))
def setUp(self): self._protocol = Protocol('TestProtocol', little_endian=True) self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', '0xcafe')) self.tmp.add(UInt(2, 'field_2', '0xbabe'))
def setUp(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', '0xcafe')) self.tmp.add(UInt(2, 'field_2', '0xbabe')) self.example = self.tmp.encode({}, {})
def test_access_struct(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('StructuredRequest', self._protocol, {}) struct = get_pair() self.tmp.add(struct) msg = self.tmp.encode({}, {}) self.assertEquals(msg.pair.first.int, 1)
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.assertEquals(decoded.len.int, 4) self.assertEquals(decoded.chars.ascii, 'abcd') self.assertEquals(decoded.chars2.ascii, 'ef')
class TestMessageTemplate(TestCase): def setUp(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', 1)) self.tmp.add(UInt(2, 'field_2', 2)) def test_create_template(self): self.assertEquals(len(self.tmp._fields), 2) def test_encode_template(self): msg = self.tmp.encode({}, {}) self.assertEquals(msg.field_1.int, 1) self.assertEquals(msg.field_2.int, 2) def test_message_field_type_conversions(self): msg = self.tmp.encode({'field_1': 1024}, {}) self.assertEquals(msg.field_1.int, 1024) self.assertEquals(msg.field_1.hex, '0x0400') self.assertEquals(msg.field_1.bytes, '\x04\x00') def test_encode_template_with_params(self): msg = self.tmp.encode({'field_1': 111, 'field_2': 222}, {}) self.assertEquals(msg.field_1.int, 111) self.assertEquals(msg.field_2.int, 222) def test_encode_template_header(self): msg = self.tmp.encode({}, {}) self.assertEquals(msg._header.msgId.int, 5) self.assertEquals(msg._header.length.int, 8) def test_encode_to_bytes(self): msg = self.tmp.encode({}, {}) self.assertEquals(msg._header.msgId.int, 5) self.assertEquals(msg._raw, to_bin_of_length(8, '0x0005 0008 0001 0002')) def test_pretty_print(self): msg = self.tmp.encode({}, {}) self.assertEquals(msg._header.msgId.int, 5) self.assertEquals(str(msg), 'Message FooRequest') self.assertEquals( repr(msg), """Message FooRequest Header TestProtocol msgId = 5 (0x0005) length = 8 (0x0008) field_1 = 1 (0x0001) field_2 = 2 (0x0002) """) def test_unknown_params_cause_exception(self): self.assertRaises(AssertionError, self.tmp.encode, {'unknown': 111}, {}) def test_decode_message(self): msg = self.tmp.decode(to_bin('0xcafebabe')) self.assertEquals(msg.field_1.hex, '0xcafe')
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.assertEquals(encoded.chars.ascii, 'abcd') self.assertEquals(len(encoded.chars), 4) self.assertEquals(encoded.chars2.ascii, 'ef') self.assertEquals(len(encoded.chars2), 6)
class TestMessageTemplate(TestCase): def setUp(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', 1)) self.tmp.add(UInt(2, 'field_2', 2)) def test_create_template(self): self.assertEquals(len(self.tmp._fields), 2) def test_encode_template(self): msg = self.tmp.encode({}, {}) self.assertEquals(msg.field_1.int, 1) self.assertEquals(msg.field_2.int, 2) def test_message_field_type_conversions(self): msg = self.tmp.encode({'field_1': 1024}, {}) self.assertEquals(msg.field_1.int, 1024) self.assertEquals(msg.field_1.hex, '0x0400') self.assertEquals(msg.field_1.bytes, '\x04\x00') def test_encode_template_with_params(self): msg = self.tmp.encode({'field_1': 111, 'field_2': 222}, {}) self.assertEquals(msg.field_1.int, 111) self.assertEquals(msg.field_2.int, 222) def test_encode_template_header(self): msg = self.tmp.encode({}, {}) self.assertEquals(msg._header.msgId.int, 5) self.assertEquals(msg._header.length.int, 8) def test_encode_to_bytes(self): msg = self.tmp.encode({}, {}) self.assertEquals(msg._header.msgId.int, 5) self.assertEquals(msg._raw, to_bin_of_length(8, '0x0005 0008 0001 0002')) def test_pretty_print(self): msg = self.tmp.encode({}, {}) self.assertEquals(msg._header.msgId.int, 5) self.assertEquals(str(msg), 'Message FooRequest') self.assertEquals(repr(msg), """Message FooRequest Header TestProtocol msgId = 5 (0x0005) length = 8 (0x0008) field_1 = 1 (0x0001) field_2 = 2 (0x0002) """) def test_unknown_params_cause_exception(self): self.assertRaises(AssertionError, self.tmp.encode, {'unknown': 111}, {}) def test_decode_message(self): msg = self.tmp.decode(to_bin('0xcafebabe')) self.assertEquals(msg.field_1.hex, '0xcafe')
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_encode_dynamic_list(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) tmp.add(UInt(2, 'len', None)) lst = ListTemplate('len', 'foo', parent=None) lst.add(UInt(1, 'bar', 1)) tmp.add(lst) encoded = tmp.encode({'len': 6}, {}) self.assertEqual(len(encoded.foo), 6)
def test_decode_dynamic_list(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) tmp.add(UInt(2, 'len', None)) lst = ListTemplate('len', 'foo', parent=None) lst.add(UInt(1, 'bar', None)) tmp.add(lst) decoded = tmp.decode(to_bin('0x 00 04 6162 6364')) self.assertEqual(decoded.len.int, 4) self.assertEqual(decoded.foo[0].hex, '0x61')
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.assertEquals(encoded.chars.ascii, 'abcd') self.assertEquals(encoded.len.int, 4)
def test_encode_dynamic_list(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) tmp.add(UInt(2, 'len', None)) lst = ListTemplate('len', 'foo', parent=None) lst.add(UInt(1, 'bar', 1)) tmp.add(lst) encoded = tmp.encode({'len': 6}, {}) self.assertEquals(len(encoded.foo), 6)
def test_decode_dynamic_list(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) tmp.add(UInt(2, 'len', None)) lst = ListTemplate('len', 'foo', parent=None) lst.add(UInt(1, 'bar', None)) tmp.add(lst) decoded = tmp.decode(to_bin('0x 00 04 6162 6364')) self.assertEquals(decoded.len.int, 4) self.assertEquals(decoded.foo[0].hex, '0x61')
def test_non_existing_dynamic_variable(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) self.assertRaises(Exception, tmp.add, Char('not_existing', 'foo', None))
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_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_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"))
class TestMessageTemplateValidation(TestCase): def setUp(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', '0xcafe')) self.tmp.add(UInt(2, 'field_2', '0xbabe')) self.example = self.tmp.encode({}, {}) def test_validate_passing_hex(self): msg = self._decode_and_set_fake_header('0xcafebabe') errors = self.tmp.validate(msg, {}, {}) self.assertEquals(errors, []) def test_validate_error_default_value(self): msg = self._decode_and_set_fake_header('0xcafedead') errors = self.tmp.validate(msg, {}, {}) self.assertEquals( errors, ['Value of field field_2 does not match 0xdead!=0xbabe']) def test_validate_error_override(self): msg = self._decode_and_set_fake_header('0xcafebabe') errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {}) self.assertEquals( errors, ['Value of field field_2 does not match 0xbabe!=0xdead']) def test_validate_two_errors(self): msg = self._decode_and_set_fake_header('0xbeefbabe') errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {}) self.assertEquals(len(errors), 2) def test_validate_pattern_pass(self): msg = self._decode_and_set_fake_header('0xcafe0002') errors = self.tmp.validate(msg, {'field_2': '(0|2)'}, {}) self.assertEquals(len(errors), 0) def test_validate_pattern_failure(self): msg = self._decode_and_set_fake_header('0xcafe0002') errors = self.tmp.validate(msg, {'field_2': '(0|3)'}, {}) self.assertEquals(len(errors), 1) def test_validate_passing_int(self): msg = self._decode_and_set_fake_header('0xcafe0200') errors = self.tmp.validate(msg, {'field_2': '512'}, {}) self.assertEquals(errors, []) def _decode_and_set_fake_header(self, bin_value): msg = self.tmp.decode(to_bin(bin_value)) msg['_header'] = self.example._header return msg def test_failing_passing_int(self): msg = self._decode_and_set_fake_header('0xcafe0200') errors = self.tmp.validate(msg, {'field_2': '513'}, {}) self.assertEquals(len(errors), 1)
class TestStructs(TestCase): def test_access_struct(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('StructuredRequest', self._protocol, {}) struct = get_pair() self.tmp.add(struct) msg = self.tmp.encode({}, {}) self.assertEquals(msg.pair.first.int, 1) def test_create_struct(self): struct = get_pair() self.assertEquals(struct.name, 'pair') def test_add_fields_to_struct(self): struct = get_pair() encoded = struct.encode({}, {}) self.assertEquals(encoded.first.int, 1) def test_add_fields_to_struct_and_override_values(self): struct = get_pair() encoded = struct.encode({'pair.first': 42}, {}) self.assertEquals(encoded.first.int, 42) def test_yo_dawg_i_heard(self): str_str = get_recursive_struct() encoded = str_str.encode({}, {}) self.assertEquals(encoded.pair.first.int, 1) def test_get_recursive_names(self): pair = get_pair() names = pair._get_params_sub_tree({ 'pair.foo': 0, 'pairnotyourname.ploo': 2, 'pair.goo.doo': 3 }) self.assertEquals(len(names), 2) self.assertEquals(names['foo'], 0) self.assertEquals(names['goo.doo'], 3) def test_set_recursive(self): str_str = get_recursive_struct() encoded = str_str.encode({'str_str.pair.first': 42}, {}) self.assertEquals(encoded.pair.first.int, 42) def test_decode_several_structs(self): str_list = get_struct_list() decoded = str_list.decode(to_bin('0xcafebabe d00df00d'), {}) self.assertEquals(decoded[0].first.hex, '0xcafe') self.assertEquals(decoded[1].second.hex, '0xf00d') def test_length_of_struct(self): pair = get_pair() encoded = pair.encode({}, {}) self.assertEquals(len(encoded), 4) def test_decode_struct(self): pair = get_pair() decoded = pair.decode(to_bin('0xcafebabe'), {}) self.assertEquals(decoded.first.hex, '0xcafe') self.assertEquals(decoded.second.hex, '0xbabe') def test_decode_aligned(self): struct = get_struct_with_length_and_alignment() decoded = struct.decode(to_bin('0x00010200'), {}) self.assertEqual(decoded.first.int, 1) self.assertEqual(decoded.second.int, 2) self.assertEqual(len(decoded), 4) self.assertEqual(decoded._raw, to_bin('0x00010200')) def test_encode_aligned(self): struct = get_struct_with_length_and_alignment() encoded = struct.encode({}, {}) self.assertEqual(encoded.first.int, 1) self.assertEqual(encoded.second.int, 2) self.assertEqual(len(encoded), 4) self.assertEqual(encoded._raw, to_bin('0x00010200')) def test_encode_perfectly_aligned(self): struct = get_struct_with_length_and_alignment() struct.add(UInt(5, 'third', 3)) encoded = struct.encode({}, {}) self.assertEqual(encoded.first.int, 1) self.assertEqual(encoded.second.int, 2) self.assertEqual(encoded.third.int, 3) self.assertEqual(len(encoded), 8) self.assertEqual(encoded._raw, to_bin('0x00010200 00000003'))
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_non_existing_dynamic_list_variable(self): tmp = MessageTemplate('Dymagic', self._protocol, {}) lst = ListTemplate('not_existing', 'foo', parent=None) lst.add(UInt(1, 'bar', None)) self.assertRaises(Exception, tmp.add, lst)
class TestMessageTemplateValidation(TestCase): def setUp(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', '0xcafe')) self.tmp.add(UInt(2, 'field_2', '0xbabe')) self.example = self.tmp.encode({}, {}) def test_validate_passing_hex(self): msg = self._decode_and_set_fake_header('0xcafebabe') errors = self.tmp.validate(msg, {}, {}) self.assertEquals(errors, []) def test_validate_error_default_value(self): msg = self._decode_and_set_fake_header('0xcafedead') errors = self.tmp.validate(msg, {}, {}) self.assertEquals(errors, ['Value of field field_2 does not match 0xdead!=0xbabe']) def test_validate_error_override(self): msg = self._decode_and_set_fake_header('0xcafebabe') errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {}) self.assertEquals(errors, ['Value of field field_2 does not match 0xbabe!=0xdead']) def test_validate_two_errors(self): msg = self._decode_and_set_fake_header('0xbeefbabe') errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {}) self.assertEquals(len(errors), 2) def test_validate_pattern_pass(self): msg = self._decode_and_set_fake_header('0xcafe0002') errors = self.tmp.validate(msg, {'field_2': '(0|2)'}, {}) self.assertEquals(len(errors), 0) def test_validate_pattern_failure(self): msg = self._decode_and_set_fake_header('0xcafe0002') errors = self.tmp.validate(msg, {'field_2': '(0|3)'}, {}) self.assertEquals(len(errors), 1) def test_validate_passing_int(self): msg = self._decode_and_set_fake_header('0xcafe0200') errors = self.tmp.validate(msg, {'field_2': '512'}, {}) self.assertEquals(errors, []) def _decode_and_set_fake_header(self, bin_value): msg = self.tmp.decode(to_bin(bin_value)) msg['_header'] = self.example._header return msg def test_failing_passing_int(self): msg = self._decode_and_set_fake_header('0xcafe0200') errors = self.tmp.validate(msg, {'field_2': '513'}, {}) self.assertEquals(len(errors), 1)
class TestTBCDContainerTemplate(TestCase): def setUp(self): self._protocol = Protocol('TestProtocol') self._protocol.add(UInt(2, 'msgId', 5)) self._protocol.add(UInt(2, 'length', None)) self._protocol.add(PDU('length-4')) self.tmp = MessageTemplate('FooRequest', self._protocol, {}) self.tmp.add(UInt(2, 'field_1', 1)) self.tmp.add(UInt(2, 'field_2', 2)) def test_verify_only_tbcd_number_passes(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('4', 'first', None)) self.assertRaises(AssertionError, container.add, UInt(2, 'not allowed', None)) def test_get_even_tbcd_field(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('4', 'first', '1234')) self.tmp.add(container) msg = self.tmp.encode({}, {}) self.assertEqual('1234', msg.tbcd.first.tbcd) def test_get_odd_tbcd_field(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('3', 'first', '123')) self.tmp.add(container) msg = self.tmp.encode({}, {}) self.assertEqual('123', msg.tbcd.first.tbcd) def test_get_binlength_for_odd_value_tbcd_container(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('1', 'first', '1')) self.assertEquals(8, container.binlength) container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('3', 'first', '123')) self.assertEquals(16, container.binlength) def test_get_binlength_for_even_value_tbcd_container(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('2', 'first', '12')) self.assertEquals(8, container.binlength) container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('4', 'first', '1234')) self.assertEquals(16, container.binlength) def test_decode(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('3', 'first', None)) decoded = container.decode(to_bin('0x21f3')) self.assertEquals('123', decoded.first.tbcd) def test_little_endian_tbcd_unsupported(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('3', 'first', '123')) self.assertRaises(AssertionError, container.encode, {}, {}, little_endian=True) self.assertRaises(AssertionError, container.decode, to_bin('0x21f3'), little_endian=True) def test_get_encoded_raw_bytes(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('3', 'first', '123')) container.add(TBCD('13', 'second', '6100000000001')) encoded = container.encode({}, {}) self.assertEquals(to_bin("0b0010000101100011000000010000000000000000000000000000000000010000"), encoded._get_raw_bytes()) def test_decode_raw_bytes_with_star_length(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('3', 'first', '123')) container.add(TBCD('*', 'second', '6100000000001')) decoded = container.decode(to_bin("0b0010000101100011000000010000000000000000000000000000000000010000")) self.assertEquals('123', decoded.first.tbcd) self.assertEquals('6100000000001', decoded.second.tbcd) def test_encoded_even_value_container_returns_correct_length(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('3', 'first', '123')) container.add(TBCD('13', 'second', '6100000000001')) encoded = container.encode({}) self.assertEquals(8, len(encoded)) def test_encoded_odd_value_container_returns_correct_length(self): container = TBCDContainerTemplate('tbcd', None) container.add(TBCD('3', 'first', '456')) container.add(TBCD('4', 'second', '1234')) encoded = container.encode({}) self.assertEquals(4, len(encoded))