def test_decode_failures(self): # An object goes past the end dec = derlite.Decoder(bytes.fromhex('020201')) self.assertRaises(DecodeError, dec.read_integer) # A sequence goes past the end dec = derlite.Decoder(bytes.fromhex('020101 3004 020101')) self.assertEqual(dec.read_integer(), 1) self.assertRaises(DecodeError, dec.enter) # An object goes past the end of its container dec = derlite.Decoder(bytes.fromhex('020101 3003 02020100')) self.assertEqual(dec.read_integer(), 1) self.assertEqual(dec.enter(), Tag.Sequence) self.assertRaises(DecodeError, dec.read_integer) dec = derlite.Decoder(bytes.fromhex('020101 3001 02020100')) self.assertEqual(dec.read_integer(), 1) self.assertEqual(dec.enter(), Tag.Sequence) self.assertRaises(DecodeError, dec.read_integer) dec = derlite.Decoder(bytes.fromhex('0282000101 3003 0282000101')) self.assertEqual(dec.read_integer(), 1) self.assertEqual(dec.enter(), Tag.Sequence) self.assertRaises(DecodeError, dec.read_integer)
def test_strings_1(self): # Test decoding some strings. # IA5String and UTF8String dec = derlite.Decoder(b'\x16\x06flambe\x0c\x07flamb\xC3\xA9') self.assertEqual(dec.read_string(), 'flambe') self.assertEqual(dec.read_string(), 'flamb\u00E9') self.assertTrue(dec.eof()) # PrintableString and (simple) GeneralString. dec = derlite.Decoder(b'\x13\x05hello\x1B\x06world!') self.assertEqual(dec.read_string(), 'hello') self.assertEqual(dec.read_string(), 'world!') self.assertTrue(dec.eof())
def query_variables(socket, agent, community, *oids): # Create the request query_message = PDU(0) for oid in oids: query_message.bindings.append( (oid, None) ) query_message.debug_print() # Send it socket.sendto(encode_v2c_message(community, query_message), 0, (agent, 161)) print("\nWaiting for response...") while True: (dgram, fromaddr) = socket.recvfrom(8192) print("\nDatagram from", fromaddr) decoder = derlite.Decoder(dgram) (resp_community, resp_pdu) = decode_v2c_message(dgram) print("Community:", resp_community) print("Response:", resp_pdu) resp_pdu.debug_print(indent=' ') if community == resp_community and query_message.request_id == resp_pdu.request_id: break
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 decode_v2c_message(dgram): dec = derlite.Decoder(dgram) dec.enter(Tag.Sequence) vnum = dec.read_integer() if vnum != 1: raise derlite.DecodeError('Not a SNMPv2 datagram!') community = dec.read_octet_string() pdu = dec.read_type(PDU) dec.leave() return (community, pdu)
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_utc(self): utc = datetime.timezone.utc self.roundtrip(datetime.datetime.fromtimestamp(0, utc), b'\x18\x0f19700101000000Z') self.roundtrip(datetime.datetime.fromtimestamp(1.25, utc), b'\x18\x1219700101000001.25Z') dec = derlite.Decoder(b'\x18\x0d198002010000Z' + b'\x18\x0b1980020100Z') self.assertEqual(datetime.datetime(1980, 2, 1, 0, 0, 0, tzinfo=utc), dec.read_generalizedtime()) self.assertEqual(datetime.datetime(1980, 2, 1, 0, 0, 0, tzinfo=utc), dec.read_generalizedtime())
def test_strings_teletex(self): dec = derlite.Decoder( b'\x14\x03See\x14\x07\xECe Olde' + b'\x14\x28\xABM\xC8uller, Fran\xCBcois, \xEArsted, l\'H\xC3opital\xBB' + b'\x14\x03(\xA4)') self.assertEqual(dec.read_string(), 'See') self.assertEqual(dec.read_string(), '\u00DEe Olde') self.assertEqual( dec.read_string(), '\u00ABM\u00FCller, Fran\u00E7ois, \u0152rsted, l\'H\u00F4pital\u00BB' ) self.assertEqual(dec.read_string(), '($)') self.assertTrue(dec.eof())
def roundtrip(self, fs, flags, der): der = bytes.fromhex(der) self.assertEqual(fs.make_der(flags, tl=True), der) dec = derlite.Decoder(der) self.assertEqual(fs.decode_der(dec), flags)
def test_strings_teletex_ascii(self): dec = derlite.Decoder(b'\x14\x1FSome parts of T.61 match ASCII.') self.assertEqual(dec.read_string(), 'Some parts of T.61 match ASCII.') self.assertTrue(dec.eof())
def around(self, enc, der): der = bytes.fromhex(der) got = enc.getvalue() self.assertEqual(got, der) return derlite.Decoder(got)