Example #1
0
 def length(self):
     """bytes: Return the BER encoded byte length of self.value."""
     return ber_encode(len(self))
Example #2
0
    def test_ber_encode_decode(self):
        from klvdata.common import ber_decode
        from klvdata.common import ber_encode

        self.assertEqual(ber_decode(ber_encode(0)), 0)
        self.assertEqual(ber_decode(ber_encode(1)), 1)
        self.assertEqual(ber_decode(ber_encode(8)), 8)
        self.assertEqual(ber_decode(ber_encode(127)), 127)
        self.assertEqual(ber_decode(ber_encode(128)), 128)
        self.assertEqual(ber_decode(ber_encode(254)), 254)
        self.assertEqual(ber_decode(ber_encode(255)), 255)
        self.assertEqual(ber_decode(ber_encode(256)), 256)
        self.assertEqual(ber_decode(ber_encode(900)), 900)
        self.assertEqual(ber_decode(ber_encode(9000)), 9000)
        self.assertEqual(ber_decode(ber_encode(90000)), 90000)
        self.assertEqual(ber_decode(ber_encode(900000)), 900000)
Example #3
0
 def length(self):
     """bytes: Return the BER encoded byte length of self.value."""
     if self is None or self.value is None:
         return 0
     return ber_encode(len(self))
Example #4
0
    def test_ber_decode_encode(self):
        from klvdata.common import ber_decode
        from klvdata.common import ber_encode

        # BER Short Form
        self.assertEqual(ber_encode(ber_decode(b'\x00')), b'\x00')
        self.assertEqual(ber_encode(ber_decode(b'\x01')), b'\x01')
        self.assertEqual(ber_encode(ber_decode(b'\x08')), b'\x08')
        self.assertEqual(ber_encode(ber_decode(b'\x7F')), b'\x7F')

        # BER Long Form
        self.assertEqual(ber_encode(ber_decode(b'\x81\x80')), b'\x81\x80')
        self.assertEqual(ber_encode(ber_decode(b'\x81\xFF')), b'\x81\xFF')
        self.assertEqual(ber_encode(ber_decode(b'\x82\xFF\xFF')),
                         b'\x82\xFF\xFF')
        self.assertEqual(ber_encode(ber_decode(b'\x83\xFF\xFF\xFF')),
                         b'\x83\xFF\xFF\xFF')

        # BER encode using the fewest possible bytes
        self.assertEqual(ber_encode(ber_decode(b'\x80')), b'\x00')
        self.assertEqual(ber_encode(ber_decode(b'\x81\x00')), b'\x00')
        self.assertEqual(ber_encode(ber_decode(b'\x81\x01')), b'\x01')
        self.assertEqual(ber_encode(ber_decode(b'\x81\x7F')), b'\x7F')