def testOctetString(self): C = dictionary.avp_by_name('User-Password') enc = diameter.encode_avp(dictionary, C, b'teststr') self.assertEqual( enc, binascii.unhexlify('000000024000000f7465737473747200')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, ('User-Password', 'None', b'teststr')) # Must not fail if not byte string enc = diameter.encode_avp(dictionary, C, 'teststr') self.assertEqual( enc, binascii.unhexlify('000000024000000f7465737473747200'))
def testEnumerated(self): C = dictionary.avp_by_name('Service-Type') enc1 = diameter.encode_avp(dictionary, C, u'Framed') enc2 = diameter.encode_avp(dictionary, C, 'Framed') enc3 = diameter.encode_avp(dictionary, C, '2') enc4 = diameter.encode_avp(dictionary, C, 2) self.assertEqual(enc1, binascii.unhexlify('000000064000000c00000002')) self.assertEqual(enc1, enc2) self.assertEqual(enc1, enc3) self.assertEqual(enc1, enc4) dec = diameter.decode_avp(dictionary, enc1) self.assertEqual(dec, ('Service-Type', 'None', 2))
def testInt64(self): C = dictionary.avp_by_name('Value-Digits') enc = diameter.encode_avp(dictionary, C, 12345) self.assertEqual( enc, binascii.unhexlify('000001bf400000100000000000003039')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, (u'Value-Digits', 'None', 12345)) # Negative enc = diameter.encode_avp(dictionary, C, -1) self.assertEqual( enc, binascii.unhexlify('000001bf40000010ffffffffffffffff')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, (u'Value-Digits', 'None', -1))
def testInt32(self): C = dictionary.avp_by_name('Acct-Input-Packets') enc = diameter.encode_avp(dictionary, C, 1) self.assertEqual(enc, binascii.unhexlify('0000002f0000000c00000001')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, (u'Acct-Input-Packets', 'None', 1)) # Negative C = dictionary.avp_by_name('Number-Of-Participants') enc = diameter.encode_avp(dictionary, C, -1) self.assertEqual( enc, binascii.unhexlify('00000375c0000010000028afffffffff')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, (u'Number-Of-Participants', 'TGPP', -1))
def testUtf8(self): C = dictionary.avp_by_name('User-Name') # 70 69 3a 20 cf 80 textUTF8 = u'pi: \u03c0' enc = diameter.encode_avp(dictionary, C, textUTF8) self.assertEqual( enc, binascii.unhexlify('000000014000000e70693a20cf800000')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, ('User-Name', 'None', textUTF8)) # Must not fail if byte string enc = diameter.encode_avp(dictionary, C, b'teststr') self.assertEqual( enc, binascii.unhexlify('000000014000000f7465737473747200'))
def testIPAddressv4(self): # (IPAddress) C = dictionary.avp_by_name('Framed-IP-Address') enc = diameter.encode_avp(dictionary, C, '172.30.211.2') self.assertEqual(enc, binascii.unhexlify('000000084000000cac1ed302')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, ('Framed-IP-Address', 'None', '172.30.211.2')) # also with vendor C = dictionary.avp_by_name('3GPP-SGSN-Address') enc = diameter.encode_avp(dictionary, C, '1.2.3.4') self.assertEqual( enc, binascii.unhexlify('00000006c0000010000028af01020304')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, ('3GPP-SGSN-Address', 'TGPP', '1.2.3.4'))
def testTime(self): C = dictionary.avp_by_name('Event-Timestamp') unixtime = diameter.date2epoch(2012, 11, 17, 10, 30, 00) enc = diameter.encode_avp(dictionary, C, unixtime) self.assertEqual(enc, binascii.unhexlify('000000374000000cd451ad68')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, ('Event-Timestamp', 'None', unixtime))
def testU64(self): C = dictionary.avp_by_name('Framed-Interface-Id') enc = diameter.encode_avp(dictionary, C, 12345) self.assertEqual( enc, binascii.unhexlify('00000060400000100000000000003039')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, (u'Framed-Interface-Id', 'None', 12345))
def testNotInDict(self): # (Default to OctetString) enc = binascii.unhexlify('0FF000024000000f7465737473747200') dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, ('Unknown_AVP_0_267386882', 'None', b'teststr')) C = dictionary.avp_by_name('Unknown_AVP_0_267386882') enc1 = diameter.encode_avp(dictionary, C, 'teststr') self.assertEqual(bytearray(enc), enc1)
def testF64(self): C = dictionary.avp_by_name('Cost') enc = diameter.encode_avp(dictionary, C, 12.34) self.assertEqual( enc, binascii.unhexlify('0000025bc0000014000000c14028ae147ae147ae')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, ('Cost', 'Ericsson', 12.34))
def testIPAddressv6(self): # (IPAddress) C = dictionary.avp_by_name('Framed-IP-Address') enc = diameter.encode_avp(dictionary, C, '::ffff:d9c8:4cca') self.assertEqual( enc, binascii.unhexlify( '000000084000001800000000000000000000ffffd9c84cca')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, ('Framed-IP-Address', 'None', '::ffff:d9c8:4cca'))
def testFlags(self): # mandatory already set C = dictionary.avp_by_name('CC-Request-Number') encM = diameter.encode_avp(dictionary, C, 5) C.mandatory = 'may' C.protected = 'must' encP = diameter.encode_avp(dictionary, C, 5) C.protected = 'may' encO = diameter.encode_avp(dictionary, C, 5) C.protected = 'must' C.mandatory = 'must' encPM = diameter.encode_avp(dictionary, C, 5) self.assertEqual(encO, binascii.unhexlify('0000019f0000000c00000005')) self.assertEqual(encP, binascii.unhexlify('0000019f2000000c00000005')) self.assertEqual(encM, binascii.unhexlify('0000019f4000000c00000005')) self.assertEqual(encPM, binascii.unhexlify('0000019f6000000c00000005')) decO = diameter.decode_avp(dictionary, encO) decP = diameter.decode_avp(dictionary, encP) decM = diameter.decode_avp(dictionary, encM) decPM = diameter.decode_avp(dictionary, encPM) self.assertEqual(decO, ('CC-Request-Number', 'None', 5)) self.assertEqual(decP, ('CC-Request-Number', 'None', 5)) self.assertEqual(decM, ('CC-Request-Number', 'None', 5)) self.assertEqual(decPM, ('CC-Request-Number', 'None', 5))
def testIPv6Prefix(self): C = dictionary.avp_by_name('Framed-IPv6-Prefix') enc = diameter.encode_avp( dictionary, C, diameter.encode_ipv6prefix('21DA:D3:0:2F3B::/64')) self.assertEqual( enc, binascii.unhexlify( '000000614000001a004021da00d300002f3b00000000000000000000')) dec = diameter.decode_avp(dictionary, enc) # We need to restore it to original prefix, so we can compare restored = (dec[0], dec[1], diameter.decode_ipv6prefix(dec[2])) self.assertEqual( restored, ('Framed-IPv6-Prefix', 'None', '21DA:D3:0:2F3B::/64'.lower())) dec = diameter.decode_avp( dictionary, binascii.unhexlify('000000614000000b00010000')) restored = (dec[0], dec[1], diameter.decode_ipv6prefix(dec[2])) self.assertEqual(restored, ('Framed-IPv6-Prefix', 'None', '0::/1'))
def eAVP(self, avpname, value): C = dictionary.avp_by_name(avpname) return diameter.encode_avp(dictionary, C, value)
def testF32(self): C = dictionary.avp_by_name('Token-Rate') enc = diameter.encode_avp(dictionary, C, 12.34) self.assertEqual(enc, binascii.unhexlify('000001f00000000c414570a4')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, ('Token-Rate', 'None', 12.34000015258789))
def testU32(self): C = dictionary.avp_by_name('NAS-Port') enc = diameter.encode_avp(dictionary, C, 2345) self.assertEqual(enc, binascii.unhexlify('000000054000000c00000929')) dec = diameter.decode_avp(dictionary, enc) self.assertEqual(dec, (u'NAS-Port', 'None', 2345))