Beispiel #1
0
 def test_decode_aligned_int(self):
     sint = Int(1, 'foo', None, align='4')
     decoded = sint.decode(to_bin('0xb800 0000'), None)
     self.assertEquals(decoded.int, -72)
     self.assertEquals(decoded.hex, '0xb8')
     self.assertEquals(len(decoded), 4)
     self.assertEquals(decoded._raw, to_bin('0xb800 0000'))
 def test_decode_little_endian_container(self):
     container = self._2_byte_container()
     decoded = container.decode(to_bin("0x0190"), little_endian=True)
     self.assertEqual(1, decoded.oneBit.int)
     self.assertEqual(1, decoded.threeBits.int)
     self.assertEqual(1, decoded.twelveBits.int)
     self.assertEquals(decoded._raw, to_bin("0x0190"))
Beispiel #3
0
 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'))
Beispiel #4
0
 def test_decode_aligned_uint(self):
     uint = UInt(1, 'foo', None, align='4')
     decoded = uint.decode(to_bin('0xff00 0000'), None)
     self.assertEquals(decoded.int, 255)
     self.assertEquals(decoded.hex, '0xff')
     self.assertEquals(len(decoded), 4)
     self.assertEquals(decoded._raw, to_bin('0xff00 0000'))
 def test_little_endian_struct_encode(self):
     pair = _get_pair()
     encoded = pair.encode({}, little_endian=True)
     self.assertEquals(encoded.first.hex, '0x0001')
     self.assertEquals(encoded.first._raw, to_bin('0x0100'))
     self.assertEquals(encoded.second.hex, '0x0002')
     self.assertEquals(encoded.second._raw, to_bin('0x0200'))
 def test_little_endian_list_encode(self):
     struct_list = _get_struct_list()
     encoded = struct_list.encode({}, None, little_endian=True)
     self.assertEquals(encoded[0].first.hex, '0x0001')
     self.assertEquals(encoded[0].first._raw, to_bin('0x0100'))
     self.assertEquals(encoded[0].second.hex, '0x0002')
     self.assertEquals(encoded[0].second._raw, to_bin('0x0200'))
Beispiel #7
0
 def _assert_align(self, value, length, raw):
     field = Field('uint', 'name', to_bin(value), aligned_len=length)
     self.assertEquals(field.int, int(value, 16))
     self.assertEquals(field.hex, value)
     self.assertEquals(field._raw, to_bin(raw))
     self.assertEquals(field.bytes, to_bin(value))
     if length:
         self.assertEquals(len(field), length)
 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'))
Beispiel #9
0
 def test_validate_int(self):
     field = Field('int', 'field', to_bin('0xffb8'))
     self._should_pass(Int(2, 'field', -72).validate({'field': field}, {}))
     self._should_pass(Int(2, 'field', '-72').validate({'field': field}, {}))
     self._should_pass(Int(2, 'field', '-0x48').validate({'field': field}, {}))
     self._should_pass(Int(2, 'field', '-0x0048').validate({'field': field}, {}))
     self._should_pass(Int(2, 'field', '(0|-72)').validate({'field': field}, {}))
Beispiel #10
0
 def test_encode_aligned_int(self):
     sint = Int(1, 'foo', '-72', align='4')
     encoded = sint.encode({}, {}, None)
     self.assertEquals(encoded.int, -72)
     self.assertEquals(encoded.hex, '0xb8')
     self.assertEquals(len(encoded), 4)
     self.assertEquals(encoded._raw, to_bin('0xb800 0000'))
Beispiel #11
0
 def test_encode_aligned_uint(self):
     uint = UInt(1, 'foo', '0xff', align='4')
     encoded = uint.encode({}, {}, None)
     self.assertEquals(encoded.int, 255)
     self.assertEquals(encoded.hex, '0xff')
     self.assertEquals(len(encoded), 4)
     self.assertEquals(encoded._raw, to_bin('0xff00 0000'))
 def test_decode_message(self):
     list = ListTemplate('5', 'five', parent=None)
     list.add(UInt(4, None, 3))
     decoded = list.decode(to_bin('0x' + ('00000003' * 5)), {})
     self.assertEquals(len(decoded[4]), 4)
     self.assertEquals(len(decoded), 20)
     self.assertEquals(decoded[0].int, 3)
 def test_encode_little_endian_container(self):
     container = self._2_byte_container()
     encoded = container.encode({'foo.threeBits': 1, 'foo.twelveBits': 1}, little_endian=True)
     self.assertEqual(1, encoded.oneBit.int)
     self.assertEqual(1, encoded.threeBits.int)
     self.assertEqual(1, encoded.twelveBits.int)
     self.assertEquals(encoded._raw, to_bin("0x0190"))
Beispiel #14
0
 def test_validate_uint(self):
     field = Field('uint', 'field', to_bin('0x0004'))
     self._should_pass(UInt(2, 'field', 4).validate({'field': field}, {}))
     self._should_pass(UInt(2, 'field', '4').validate({'field': field}, {}))
     self._should_pass(UInt(2, 'field', '0x04').validate({'field': field}, {}))
     self._should_pass(UInt(2, 'field', '0x0004').validate({'field': field}, {}))
     self._should_pass(UInt(2, 'field', '(0|4)').validate({'field': field}, {}))
Beispiel #15
0
 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)
Beispiel #16
0
 def test_conversions(self):
     field = Field('unit', 'name', to_bin('0x00616200'))
     self.assertEquals(field.int, 0x00616200)
     self.assertEquals(field.hex, '0x00616200')
     self.assertEquals(field.ascii, 'ab')
     self.assertEquals(field.bytes, '\x00\x61\x62\x00')
     self.assertEquals(field.chars, 'ab')
     self.assertEquals(field.bin, '0b00000000' + '01100001' + '01100010' + '00000000')
 def test_conversions(self):
     field = Field("unit", "name", to_bin("0x00616200"))
     self.assertEquals(field.int, 0x00616200)
     self.assertEquals(field.hex, "0x00616200")
     self.assertEquals(field.ascii, "ab")
     self.assertEquals(field.bytes, "\x00\x61\x62\x00")
     self.assertEquals(field.chars, "ab")
     self.assertEquals(field.bin, "0b00000000" + "01100001" + "01100010" + "00000000")
Beispiel #18
0
 def _matches(self, header, fields, header_filter):
     if header_filter:
         if header_filter not in fields:
             raise AssertionError('Trying to filter messages by header field %s, but no value has been set for %s' %
                                  (header_filter, header_filter))
         if header[header_filter].bytes != to_bin(fields[header_filter]):
             return False
     return True
Beispiel #19
0
 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'))
Beispiel #20
0
 def _matches(self, header, fields, header_filter):
     # FIXME: Matching should not be assuming matching string presentation
     if header_filter:
         if header_filter not in fields:
             raise AssertionError('Trying to filter messages by header field %s, but no value has been set for %s' %
                                  (header_filter, header_filter))
         if header[header_filter].bytes != to_bin(fields[header_filter]):
             return False
     return True
    def test_pretty_print_primitive_list(self):
        decoded = _get_list_of_three().decode(to_bin('0x' + ('0003' * 3)), {})
        self.assertEquals('\n' + repr(decoded),
                          """
uint topthree[]
  0 = 3 (0x0003)
  1 = 3 (0x0003)
  2 = 3 (0x0003)
""")
 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_binary_to_bin(self):
     self.assertEquals(to_bin("0b0"), "\x00")
     self.assertEquals(to_bin("0b1"), "\x01")
     self.assertEquals(to_bin("0b1111 1111"), "\xff")
     self.assertEquals(to_bin("0b1 0000 0000"), "\x01\x00")
     self.assertEquals(to_bin("0b01 0b01 0b01"), "\x15")
     self.assertEquals(to_bin("0b11" * 32), "\xff\xff\xff\xff\xff\xff\xff\xff")
     self.assertEquals(to_bin("0b11" * 1024), "\xff\xff\xff\xff\xff\xff\xff\xff" * 32)
 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')
 def setUp(self):
     self._protocol = Protocol("Test")
     self._protocol.add(UInt(1, "id", 1))
     self._protocol.add(UInt(2, "length", None))
     self._protocol.add(PDU("length-2"))
     self._msg = MessageTemplate("FooRequest", self._protocol, {"id": "0xaa"})
     self._msg.add(UInt(1, "field_1", None))
     self._msg.add(UInt(1, "field_2", None))
     byte_stream = MockStream(to_bin("0xff0004cafe aa0004dead dd0004beef"))
     self._msg_stream = MessageStream(byte_stream, self._protocol, threading.RLock())
 def setUp(self):
     self._protocol = Protocol('Test')
     self._protocol.add(UInt(1, 'id', 1))
     self._protocol.add(UInt(2, 'length', None))
     self._protocol.add(PDU('length-2'))
     self._msg = MessageTemplate('FooRequest', self._protocol, {'id': '0xaa'})
     self._msg.add(UInt(1, 'field_1', None))
     self._msg.add(UInt(1, 'field_2', None))
     byte_stream = MockStream(to_bin('0xff0004cafe aa0004dead dd0004beef'))
     self._msg_stream = MessageStream(byte_stream, self._protocol)
    def test_pretty_print_list_list(self):
        decoded = _get_list_list().decode(to_bin('0x' + ('0003' * 4)), {})
        self.assertEquals('\n' + repr(decoded),
                          """
List listlist[]
  uint 0[]
    0 = 3 (0x0003)
    1 = 3 (0x0003)
  uint 1[]
    0 = 3 (0x0003)
    1 = 3 (0x0003)
""")
 def test_hex_to_bin(self):
     self.assertEquals(to_bin("0x0"), "\x00")
     self.assertEquals(to_bin("0x00"), "\x00")
     self.assertEquals(to_bin("0x5"), "\x05")
     self.assertEquals(to_bin("0xff"), "\xff")
     self.assertEquals(to_bin("0x100"), "\x01\x00")
     self.assertEquals(to_bin("0x01 0x02 0x03"), "\x01\x02\x03")
 def test_to_tbcd_binary(self):
     self.assertEquals(to_bin("0b11110001"), to_tbcd_binary("1"))
     self.assertEquals(to_bin("0b00010001"), to_tbcd_binary("11"))
     self.assertEquals(to_bin("0b11110010"), to_tbcd_binary("2"))
     self.assertEquals(to_bin("0b00110010"), to_tbcd_binary("23"))
     self.assertEquals(to_bin("0b0010000111110011"), to_tbcd_binary("123"))
     self.assertEquals(
         to_bin("0b0110001000010010000000100000000000000000000000000000000011110001"),
         to_tbcd_binary("262120000000001"),
     )
 def _matches(self, header, fields, header_filter):
     if header_filter:
         if header_filter not in fields:
             raise AssertionError('Trying to filter messages by header field %s, but no value has been set for %s' %
                                  (header_filter, header_filter))
         field = header[header_filter]
         if field._type == 'chars':
             if fields[header_filter].startswith('REGEXP:'):
                 try:
                     regexp = fields[header_filter].split(':')[1].strip()
                     return bool(re.match(regexp, field.ascii))
                 except re.error as e:
                     raise Exception("Invalid RegEx Error : " + str(e))
             return field.ascii == fields[header_filter]
         if field._type == 'uint':
             return field.uint == to_int(fields[header_filter])
         if header[header_filter].bytes != to_bin(fields[header_filter]):
             return False
     return True
Beispiel #31
0
 def test_validate_union(self):
     union = self._get_foo_union()
     decoded = union.decode(to_bin('0xcafebabe'))
     self._should_pass(union.validate({'foo': decoded}, {'foo.small': '', 'foo.medium': ''}))
     self._should_fail(union.validate({'foo': decoded}, {'foo.small': '0xff', 'foo.medium': ''}), 1)
Beispiel #32
0
 def test_validate_struct_union(self):
     struct = get_pair()
     union = self._get_foo_union()
     struct.add(union)
     decoded = struct.decode(to_bin('0xcafebabe f00dd00d'))
     self._should_fail(struct.validate({'pair': decoded}, {}), 3)
Beispiel #33
0
 def test_not_enough_data(self):
     template = get_list_of_three()
     self.assertRaises(Exception, template.decode, to_bin('0x00010002'))
Beispiel #34
0
 def test_decode_union(self):
     union = self._get_foo_union()
     decoded = union.decode(to_bin('0xcafebabe'))
     self.assertEquals(decoded.small.hex, '0xca')
     self.assertEquals(decoded.medium.hex, '0xcafe')
     self.assertEquals(decoded.large.hex, '0xcafebabe')
Beispiel #35
0
 def test_conversions(self):
     self.assertEqual(self.cont.seven.int, 42)
     self.assertEqual(self.cont.seven.bytes, to_bin(42))
     self.assertEqual(self.cont.seven.ascii, '*')
Beispiel #36
0
 def test_little_endian_with_align(self):
     field = Field('uint', 'name', to_bin('0x0100'), aligned_len=5, little_endian=True)
     self.assertEquals(field._raw, to_bin('0x0100000000'))
     self.assertEquals(field.int, 1)
     self.assertEquals(field.bytes, to_bin('0x0001'))
     self.assertEquals(field.hex, '0x0001')
Beispiel #37
0
 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')
Beispiel #38
0
 def test_encode_union_with_param(self):
     union = self._get_foo_union()
     encoded = union.encode({'foo': 'small', 'foo.small': '0xff'}, {})
     self.assertEquals(encoded._raw, to_bin('0xff00 0000'))
Beispiel #39
0
 def test_get_bytes_from_decoded_union(self):
     union = self._get_foo_union()
     decoded = union.decode(to_bin('0xcafebabe'))
     self.assertEquals(decoded._raw, to_bin('0xcafebabe'))
 def test_fail_validating_uint(self):
     template = UInt(2, 'field', 4)
     field = Field('uint', 'field', to_bin('0x0004'))
     self._should_fail(template.validate({'field': field}, {'field': '42'}), 1)
Beispiel #41
0
 def _bin_container(self, little_endian=False):
     cont = BinaryContainer('foo', little_endian=little_endian)
     cont['three'] = BinaryField(3, 'three', to_bin('0b101'))
     cont['six'] = BinaryField(6, 'six', to_bin('0b101010'))
     cont['seven'] = BinaryField(7, 'seven', to_bin('0b0101010'))
     return cont
 def test_little_endian_int_encode(self):
     template = Int(2, 'field', -72)
     field = template.encode({}, {}, None, little_endian=True)
     self.assertEquals(field._raw, to_bin('0xb8ff'))
     self.assertEquals(field.int, -72)
     self.assertEquals(field.bytes, to_bin('0xffb8'))
 def test_little_endian_uint_encode(self):
     template = UInt(2, 'field', 1)
     field = template.encode({}, {}, None, little_endian=True)
     self.assertEquals(field._raw, to_bin('0x0100'))
     self.assertEquals(field.int, 1)
     self.assertEquals(field.bytes, to_bin('0x0001'))
Beispiel #44
0
 def _binary_substring(self, bin_str, data_index, field):
     return to_bin("0b" +
                   bin_str[data_index:data_index + field.length.value])
Beispiel #45
0
 def test_little_endian_bin_container(self):
     little = self._bin_container(little_endian=True)
     self.assertEqual(little.three.bin, '0b101')
     self.assertEqual(little.six.bin, '0b101010')
     self.assertEqual(little.seven.bin, '0b0101010')
     self.assertEqual(little._raw, to_bin('0b0010 1010 1011 0101'))
Beispiel #46
0
 def test_binary_to_bin(self):
     print(to_bin('0b0'))
     print(to_bin('0b1'))
     print(to_bin('0b1111 1111'))
     print(to_bin('0b1 0000 0000'))
     print(to_bin('0b01 0b01 0b01'))
     self.assertEqual(to_bin('0b0'), b'\x00')
     self.assertEqual(to_bin('0b1'), b'\x01')
     self.assertEqual(to_bin('0b1111 1111'), b'\xff')
     self.assertEqual(to_bin('0b1 0000 0000'), b'\x01\x00')
     self.assertEqual(to_bin('0b01 0b01 0b01'), b'\x15')
     self.assertEqual(to_bin('0b11' * 32),
                      b'\xff\xff\xff\xff\xff\xff\xff\xff')
     self.assertEqual(to_bin('0b11' * 1024),
                      b'\xff\xff\xff\xff\xff\xff\xff\xff' * 32)
Beispiel #47
0
 def test_encode_union(self):
     union = self._get_foo_union()
     encoded = union.encode({'foo': 'medium'}, {})
     self.assertEquals(encoded._raw, to_bin('0xf00d 0000'))
Beispiel #48
0
 def test_little_endian(self):
     field = Field('uint', 'name', to_bin('0x0100'), little_endian=True)
     self.assertEqual(field._raw, to_bin('0x0100'))
     self.assertEqual(field.int, 1)
     self.assertEqual(field.bytes, to_bin('0x0001'))
     self.assertEqual(field.hex, '0x0001')
Beispiel #49
0
 def __init__(self, length, name, default_value=None, terminator=None):
     _TemplateField.__init__(self, name, default_value)
     self._terminator = to_bin(terminator)
     self.length = Length(length)
 def test_read_header_and_pdu(self):
     stream = MockStream(to_bin('0xff0004cafe'))
     header, data = self._protocol.read(stream)
     self.assertEquals(header.id.hex, '0xff')
     self.assertEquals(data, b'\xca\xfe')
Beispiel #51
0
 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')
Beispiel #52
0
 def test_conditional_decode_has_element(self):
     cond = self._get_conditional()
     decoded = cond.decode(to_bin('0x0001 000a 0043'))
     self.assertEquals(decoded.mycondition.exists, True)
     self.assertEquals(decoded.mycondition.myvalue.int, 10)
Beispiel #53
0
 def test_union_length(self):
     union = self._get_foo_union()
     decoded = union.decode(to_bin('0xcafebabe'))
     self.assertEquals(4, len(decoded))
Beispiel #54
0
 def test_conditional_decode(self):
     cond = self._get_conditional()
     decoded = cond.decode(to_bin('0x00004242'))
     self.assertEquals(decoded.mycondition.exists, False)
Beispiel #55
0
 def test_empty_values_to_bin(self):
     self.assertEqual('', to_bin(None))
     self.assertEqual('', to_bin(''))
Beispiel #56
0
def uint_field(value='0x00'):
    return Field('uint', 'name', to_bin(value))
Beispiel #57
0
 def test_hex_larger_than_8_bytes_works(self):
     self.assertEqual(to_bin('0xcafebabe f00dd00d deadbeef'),
                      '\xca\xfe\xba\xbe\xf0\x0d\xd0\x0d\xde\xad\xbe\xef')
Beispiel #58
0
 def test_integer_larger_than_8_bytes_works(self):
     self.assertEqual(to_bin('18446744073709551616'),
                      '\x01\x00\x00\x00\x00\x00\x00\x00\x00')
Beispiel #59
0
 def test_get_binary_container_bytes(self):
     self.assertEqual(self.cont._raw, to_bin('0b1011 0101 0010 1010'))
Beispiel #60
0
 def test_to_tbcd_value(self):
     self.assertEqual('1', to_tbcd_value(to_bin('0b11110001')))
     self.assertEqual('11', to_tbcd_value(to_bin('0b00010001')))
     self.assertEqual('2', to_tbcd_value(to_bin('0b11110010')))
     self.assertEqual('23', to_tbcd_value(to_bin('0b00110010')))
     self.assertEqual('123', to_tbcd_value(to_bin('0b0010000111110011')))