def test_grouped(self): """ Tests we can encode and decode grouped AVPs """ grouped_avp = avp.GroupedAVP( 260, [avp.UTF8StringAVP(1, 'Hello'), avp.UTF8StringAVP(1, 'World')]) self._compare_avp(grouped_avp, (b'\x00\x00\x01\x04\x00\x00\x00(\x00\x00' b'\x00\x01\x00\x00\x00\rHello\x00\x00\x00' b'\x00\x00\x00\x01\x00\x00\x00\rWorld\x00' b'\x00\x00')) self._compare_avp(avp.GroupedAVP(260, [grouped_avp, grouped_avp]), (b'\x00\x00\x01\x04\x00\x00\x00X\x00\x00\x01\x04' b'\x00\x00\x00(\x00\x00\x00\x01\x00\x00\x00\rHello' b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\rWorld' b'\x00\x00\x00\x00\x00\x01\x04\x00\x00\x00(\x00\x00' b'\x00\x01\x00\x00\x00\rHello\x00\x00\x00\x00\x00' b'\x00\x01\x00\x00\x00\rWorld\x00\x00\x00')) # Test filtering self.assertEqual(len(list(grouped_avp.filter_avps(0, 1))), 2) self.assertEqual(len(list(grouped_avp.filter_avps(0, 2))), 0) # Test find returns first self.assertEqual(grouped_avp.find_avp(0, 1).value, 'Hello') self.assertEqual(grouped_avp.find_avp(0, 2), None)
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_unicode_strings(self): """ Tests we can encode and decode unicode strings """ self._compare_avp( avp.UTF8StringAVP(1, u'\u0123\u0490'), memoryview(b'\x00\x00\x00\x01\x00\x00\x00\x0c\xc4\xa3\xd2\x90')) # Octet strings won't load with self.assertRaises(CodecException): avp.UTF8StringAVP(1, b'hello')
def test_integer_identifier(self): """ Tests we can create an AVP with a code and it defaults to the defaults vendor. """ self._compare_avp( avp.AVP(1, 'Hello'), avp.UTF8StringAVP( 1, value='Hello', vendor=avp.VendorId.DEFAULT, flags=avp.FLAG_MANDATORY, name='User-Name', ), ) # Unknown AVPs default to unknown AVP self._compare_avp( avp.AVP(0xdeadb33f, b'wut'), avp.UnknownAVP( 0xdeadb33f, value=b'wut', vendor=avp.VendorId.DEFAULT, flags=0, name='Unknown-AVP', ), )
def test_tuple_identifier(self): """ Tests we can create an AVP with a (vendor, code) tuple """ # This will resolve to the Username AVP self._compare_avp( avp.AVP((avp.VendorId.DEFAULT, 1), 'a username'), avp.UTF8StringAVP(1, value='a username', vendor=avp.VendorId.DEFAULT, flags=avp.FLAG_MANDATORY, name='User-Name')) self._compare_avp( avp.AVP((avp.VendorId.TGPP, 701), b'msisdn'), avp.OctetStringAVP(701, value=b'msisdn', vendor=avp.VendorId.TGPP, flags=avp.FLAG_MANDATORY | avp.FLAG_VENDOR, name='MSISDN')) # Unknown AVPs default to unknown AVP self._compare_avp( avp.AVP((0xfac3b00c, 1), b'wut'), avp.UnknownAVP(1, value=b'wut', vendor=0xfac3b00c, flags=0, name='Unknown-AVP'))
def test_string_identifier(self): """ Tests we can create an AVP with a code and it defaults to the defaults vendor. """ self._compare_avp( avp.AVP('User-Name', 'Hello'), avp.UTF8StringAVP(1, value='Hello', vendor=avp.VendorId.DEFAULT, flags=avp.FLAG_MANDATORY, name='User-Name')) # Unknown names will cause an error with self.assertRaises(ValueError): avp.AVP('Wut', 'error')