def test_avp_length(self): """ Tests we validate AVPs lengths are longer than minumum to decode and no longer than the maximum length encodable """ # Avp that has no payload isn't encodable with self.assertRaises(CodecException): avp_val = avp.AVP(0) out_buf = bytearray(avp_val.length) avp_val.encode(out_buf, 0) # Avp shorter than header with self.assertRaises(CodecException): avp.decode(b'\x00' * (avp.HEADER_LEN - 1)) # Too short with vendor bit set with self.assertRaises(CodecException): avp.decode(b'\x00\x00\x00\x00\x80\x00\x00\x00') # Max allowable length of payload avp_val = avp.UTF8StringAVP(1, 'a' * (0x00FFFFFF - avp.HEADER_LEN)) out_buf = bytearray(avp_val.length) avp_val.encode(out_buf, 0) self._compare_avp(avp_val, out_buf) # Avp length out of range with self.assertRaises(CodecException): avp_val = avp.UTF8StringAVP( 1, 'a' * (0x00FFFFFF - avp.HEADER_LEN + 1)) out_buf = bytearray(avp_val.length) avp_val.encode(out_buf, 0)
def test_addresses(self): """ Tests we can encode and decode addresses and handle invalid payloads """ # pylint:disable=expression-not-assigned self._compare_avp( avp.AddressAVP(257, '127.0.0.1'), memoryview(b'\x00\x00\x01\x01\x00\x00\x00\x0e' b'\x00\x01\x7f\x00\x00\x01\x00\x00')) self._compare_avp( avp.AddressAVP(257, '2001:db8::1'), memoryview(b'\x00\x00\x01\x01\x00\x00\x00\x1a\x00\x02 \x01\r' b'\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' b'\x01\x00\x00')) # Can't read invalid address type \x03 with self.assertRaises(CodecException): avp.decode(b'\x00\x00\x01\x01\x00\x00\x00\x0e' b'\x00\x03\x7f\x00\x00\x01\x00\x00').value # Can't read too short IPV4 with self.assertRaises(CodecException): avp.decode(b'\x00\x00\x01\x01\x00\x00\x00\x0e' b'\x00\x01\x7f').value # Can't read too short IPV6 with self.assertRaises(CodecException): avp.decode(b'\x00\x00\x01\x01\x00\x00\x00\x0e' b'\x00\x02\x7f\x00\x00\x01\x00\x00').value # Cant encode non-ips with self.assertRaises(CodecException): avp.Unsigned32AVP(257, 'facebook.com')
def _decode_check(self, avp_val, msg_bytes): decoded_val = avp.decode(msg_bytes) self.assertEqual(avp_val.value, decoded_val.value) self.assertEqual(avp_val.payload, decoded_val.payload)
def _decode_check(self, avp_val, msg_bytes): decoded_val = avp.decode(msg_bytes) self.assertEqual(avp_val.code, decoded_val.code) self.assertEqual(avp_val.vendor, decoded_val.vendor) self.assertEqual(avp_val.value, decoded_val.value) self.assertEqual(avp_val.flags, decoded_val.flags)