def test_set2(self): # More set tests enc = derlite.Encoder() enc.write_set(bytes([1, b]) for b in (4, 2, 8)) dec = self.around(enc, '310C 04020102 04020104 04020108') enc = derlite.Encoder() enc.write_set([None, False, [], Oid((1, 10)), True]) dec = self.around(enc, '310D 010100 0101FF 0500 060132 3000')
def test_implicit_tagging(self): enc = derlite.Encoder() enc.enter_implicit_tag(1) enc.write(42) enc.write(1) enc.enter_implicit_tag(Tag(0, True, Tag.Application)) enc.enter(Tag.Sequence) enc.write(b'foo') enc.write(b'bar') enc.leave() dec = self.around(enc, '81012A 020101 600A 0403666F6F 0403626172') self.assertEqual(dec.peek(), Tag(1, False, Tag.Context)) self.assertRaises(DecodeError, dec.read_integer) dec.enter_implicit_tag(Tag(1, False, Tag.Context), Tag.Integer) self.assertEqual(dec.read_integer(), 42) self.assertIsNone(dec.enter_implicit_tag(1, Tag.Integer, optional=True)) self.assertEqual(dec.read_integer(), 1) self.assertIsNone(dec.enter(Tag.Sequence, optional=True)) dec.enter_implicit_tag(Tag(0, True, Tag.Application), Tag.Sequence) dec.enter(Tag.Sequence) self.assertEqual(dec.read_type(bytes), b'foo') self.assertEqual(dec.read_octet_string(), b'bar') self.assertRaises( DecodeError, lambda: dec.enter_implicit_tag( Tag(2, False, Tag.Context), Tag.Sequence)) dec.leave() self.assertTrue(dec.eof())
def test_simple_compound(self): # A quick test of enter/leave enc = derlite.Encoder() enc.write(-128) enc.write([None]) enc.write([]) enc.write(128) dec = self.around(enc, '020180 30020500 3000 02020080') self.assertEqual(dec.read_integer(), -128) self.assertFalse(dec.eof()) self.assertEqual(dec.peek(), Tag.Sequence) dec.enter(Tag.Sequence) self.assertEqual(dec.peek(), Tag.Null) self.assertEqual(dec.read_octet_string(Tag.Null), b'') self.assertTrue(dec.eof()) self.assertIsNone(dec.peek()) dec.leave() self.assertFalse(dec.eof()) dec.enter(Tag.Sequence) self.assertTrue(dec.eof()) dec.leave() self.assertFalse(dec.eof()) self.assertEqual(dec.read_integer(), 128) self.assertTrue(dec.eof())
def test_tagforms(self): # Test encoding of tags enc = derlite.Encoder() # Tag numbers >= 31 have a different encoding enc.enter(31) enc.write_tagged_bytes(Tag(16, constructed=False, cls=Tag.Application), b' ') # Object lengths >= 128 bytes have a different encoding ablob = b'ABCDE' * 100 enc.write_tagged_bytes(Tag(1000, constructed=False, cls=Tag.Context), ablob) enc.leave() dec = self.around( enc, 'BF1F820202 5006202020202020' + '9F87688201F4 ' + ('4142434445' * 100)) self.assertIsNone(dec.enter(32, optional=True)) self.assertEqual(dec.enter(31), Tag(31, constructed=True, cls=Tag.Context)) self.assertRaises(derlite.DecodeError, dec.read_octet_string) self.assertEqual(dec.read_octet_string(Tag(16, cls=Tag.Application)), b' ') self.assertEqual(dec.read_octet_string(Tag(1000, cls=Tag.Context)), ablob) self.assertTrue(dec.eof()) dec.leave() self.assertTrue(dec.eof()) self.assertRaises(derlite.Error, dec.leave)
def test_integers(self): # Test correct encoding of integers of various widths enc = derlite.Encoder() enc.write(-129) enc.write(128) enc.write(-128) enc.write(127) enc.write(-1) enc.write(0) enc.write(1) enc.write(-127) enc.write(-256) enc.write(255) dec = self.around( enc, '0202FF7F 02020080 020180 02017F 0201FF 020100 020101 020181 0202FF00 020200FF' ) self.assertEqual(dec.read_integer(), -129) self.assertEqual(dec.read_integer(), 128) self.assertEqual(dec.read_integer(), -128) self.assertEqual(dec.read_integer(), 127) self.assertEqual(dec.read_integer(), -1) self.assertEqual(dec.read_integer(), 0) self.assertEqual(dec.read_integer(), 1) self.assertEqual(dec.read_integer(), -127) self.assertEqual(dec.read_integer(), -256) self.assertEqual(dec.read_integer(), 255)
def roundtrip(self, dt, der): enc = derlite.Encoder() enc.write(dt) got = enc.getvalue() self.assertEqual(got, der) dec = derlite.Decoder(der) got = dec.read_generalizedtime() self.assertEqual(dt, got)
def roundtrip(self, d, dt, der): der = bytes.fromhex(der) enc = derlite.Encoder() enc.write_value_of_type(d, dt) got = enc.getvalue() self.assertEqual(got, der) dec = derlite.Decoder(der) got = dec.read_type(dt) self.assertEqual(d, got)
def test_bad_usage(self): # Trying to getvalue() when we don't have a complete object enc = derlite.Encoder() enc.enter(Tag(1, True, Tag.Application)) enc.write(True) self.assertRaises(derlite.Error, enc.getvalue) # Trying to leave() more than we enter() enc = derlite.Encoder() enc.enter(2) enc.write(1) enc.leave() enc.write(2) self.assertRaises(derlite.Error, enc.leave) # Trying to write an unsupported type enc = derlite.Encoder() enc.write(b'foo') self.assertRaises(TypeError, enc.write, u'bar')
def encode_v2c_message(community, pdu): enc = derlite.Encoder() enc.enter(Tag.Sequence) enc.write(1) # constant 1 = SNMPv2 if not isinstance(community, bytes): enc.write(community.encode('utf-8')) else: enc.write(community) enc.enter_implicit_tag(0) pdu.encode_der(enc) enc.leave() # matches opening Sequence return enc.getvalue()
def test_set1(self): # Simple test of set encoding: the DER encoder is responsible # for ensuring the element ordering required by DER enc = derlite.Encoder() enc.write(set([-1, 0, 1])) dec = self.around(enc, '3109 020100 020101 0201FF') dec.enter(Tag.Set) self.assertEqual(dec.read_integer(), 0) self.assertEqual(dec.read_integer(), 1) self.assertEqual(dec.read_integer(), -1) dec.leave() self.assertTrue(dec.eof())
def test_simple_values(self): # Test round-tripping some simple values, and some of the # decoder status methods. enc = derlite.Encoder() enc.write(1) enc.write(True) enc.write(b'\x00\x42\xFE') dec = self.around(enc, '020101 0101FF 04030042FE') self.assertFalse(dec.eof()) self.assertEqual(dec.read_integer(), 1) self.assertEqual(dec.read_boolean(), True) self.assertEqual(dec.peek(), Tag.OctetString) self.assertEqual(dec.read_octet_string(), b'\x00\x42\xFE') self.assertEqual(dec.peek(), None) self.assertTrue(dec.eof())
def test_set3(self): enc = derlite.Encoder() enc.write_set([1, 0], pythontype=derlite.ExplicitlyTagged(0, int)) self.assertEqual(enc.getvalue(), bytes.fromhex('310A A003020100 A003020101'))