def test_unsigned32(self): a = AVP_Unsigned32(1,17) assert a.queryValue()==17 a.setValue(42); assert a.queryValue()==42 a = AVP_Unsigned32.narrow(AVP(1,b" ")) assert a.queryValue()==0x20202020 try: a = AVP_Unsigned32.narrow(AVP(1,b"12345")) assert False except InvalidAVPLengthError as ex: pass
def test_float32(self): a = AVP_Float32(1, 17.5) assert a.queryValue() == 17.5 a.setValue(42.75) assert a.queryValue() == 42.75 a = AVP_Float32.narrow(AVP(1, b"\102\053\000\000")) assert a.queryValue() == 42.75 try: a = AVP_Float32.narrow(AVP(1, b" ")) assert False except InvalidAVPLengthError as detail: pass
def test_grouped(self): a = AVP_Grouped(1) assert a.code==1 assert len(a.getAVPs())==0 a1 = AVP_Grouped(1,[AVP(2,b"u1"),AVP(2,b"u2")]) assert len(a1.getAVPs())==2 a = AVP(1,b"\000\000\000\002\000\000\000\012\165\061\000\000\000\000\000\002\000\000\000\012\165\062\000\000") a1 = AVP_Grouped.narrow(a) assert len(a1.getAVPs())==2 a = AVP(1,b"\000\000\000\002\000\000\000\012\165\061\000\000\000") try: a1 = AVP_Grouped.narrow(a) assert False except InvalidAVPLengthError as details: pass
def test_time(self): when = 1139658264 a = AVP_Time(1, when) assert a.querySecondsSince1970() == when a.setSecondsSince1970(when + 1) assert a.querySecondsSince1970() == when + 1 a = AVP_Time.narrow(AVP(1, b"\307\230\114\230")) assert a.querySecondsSince1970() == 1139658264
def test_Message(self): m1 = Message() assert len(m1)==0 p = xdrlib.Packer() m1.encode(p) e_sz = len(p.get_buffer()) assert len(p.get_buffer())==20 u = xdrlib.Unpacker(p.get_buffer()) m2 = Message() assert m2.decode(u,e_sz)==Message.decode_status_decoded a = AVP(1,b"hello") m1.append(a) assert len(m1)==1 p = xdrlib.Packer() m1.encode(p) e_sz = len(p.get_buffer()) assert len(p.get_buffer())==36 u = xdrlib.Unpacker(p.get_buffer()) m2 = Message() assert m2.decode(u,e_sz)==Message.decode_status_decoded #test container+iteration m3 = Message() m3.append(AVP(1,"user1")) m3.append(AVP(1,"user2")) assert len(m3)==2 count=0 for a in m3: count += 1 assert count==2 #test subset m4 = Message() m4.append(AVP(1,"user1")) m4.append(AVP(1,"user2")) m4.append(AVP(2,"foo1")) m4.append(AVP(1,"user3")) m4.append(AVP(2,"foo1")) assert len(m4)==5 count=0 for a in m4.subset(1): count += 1 assert count==3 count=0 for a in m4.subset(2): count += 1 assert count==2 count=0 for a in m4.subset(117): count += 1 assert count==0 #find m5 = Message() m5.append(AVP(1,"user1")) m5.append(AVP(2,"foo1")) assert m5.find(1) assert m5.find(2) assert not m5.find(117) #decode raw (good) raw = binascii.a2b_hex("0100003000000000000000000000000000000000000000010000000d7573657231000000000000020000000c666f6f31") u = xdrlib.Unpacker(raw) m6 = Message() assert m6.decode(u,len(raw))==Message.decode_status_decoded #decode raw (short) raw = binascii.a2b_hex("0100003000000000000000000000000000000000000000010000000d7573657231000000000000020000000c666f6f") u = xdrlib.Unpacker(raw) m7 = Message() assert m7.decode(u,len(raw))==Message.decode_status_not_enough #decode raw (garbage) raw = binascii.a2b_hex("0100002900000000000000000000000000000000000000010000000d7573657231000000000000020000000c666f6f") u = xdrlib.Unpacker(raw) m7 = Message() assert m7.decode(u,len(raw))==Message.decode_status_garbage #decode raw (garbage) (NUL bytes) raw = binascii.a2b_hex("0100000000000000000000000000000000000000000000010000000d7573657231000000000000020000000c666f6f") u = xdrlib.Unpacker(raw) m7 = Message() assert m7.decode(u,len(raw))==Message.decode_status_garbage
def test_AVP(self): a1 = AVP(1, b"user") a1.setMandatory(True) assert a1.isMandatory() a1.setMandatory(False) assert not a1.isMandatory() a1.setPrivate(True) assert a1.isPrivate() a1.setPrivate(False) assert not a1.isPrivate() a1 = AVP(1, b"user") e_sz1 = a1.encodeSize() p = Packer() e_sz2 = a1.encode(p) assert e_sz1 == 12 assert e_sz2 == 12 assert e_sz1 == e_sz2 u = Unpacker(p.get_buffer()) d_sz1 = AVP.decodeSize(u, e_sz2) assert d_sz1 == e_sz2 a2 = AVP() assert a2.decode(u, d_sz1) assert a1.code == a2.code assert a1.vendor_id == a2.vendor_id #same as above, but requires padding a1 = AVP(1, b"user") e_sz1 = a1.encodeSize() p = Packer() e_sz2 = a1.encode(p) assert e_sz1 == e_sz2 u = Unpacker(p.get_buffer()) d_sz1 = AVP.decodeSize(u, e_sz2) assert d_sz1 == e_sz2 a2 = AVP() assert a2.decode(u, d_sz1) assert a1.code == a2.code assert a1.vendor_id == a2.vendor_id
cap.addAcctApp(ProtocolConstants.DIAMETER_APPLICATION_CREDIT_CONTROL) settings = NodeSettings(host_id, realm, 9999, cap, 0, "cc_test_client", 1) ssc = SimpleSyncClient(settings,[Peer(peer,port)]) ssc.start() ssc.waitForConnection(timeout=3) req = Message() # < Diameter Header: 272, REQ, PXY > req.hdr.command_code = ProtocolConstants.DIAMETER_COMMAND_CC req.hdr.application_id = ProtocolConstants.DIAMETER_APPLICATION_CREDIT_CONTROL req.hdr.setRequest(True) req.hdr.setProxiable(True) # < Session-Id > req.append(AVP(ProtocolConstants.DI_SESSION_ID,ssc.node.makeNewSessionId())) # { Origin-Host } # { Origin-Realm } ssc.node.addOurHostAndRealm(req) # { Destination-Realm } req.append(AVP_UTF8String(ProtocolConstants.DI_DESTINATION_REALM,"example.net")) # { Auth-Application-Id } req.append(AVP_Unsigned32(ProtocolConstants.DI_AUTH_APPLICATION_ID,ProtocolConstants.DIAMETER_APPLICATION_CREDIT_CONTROL)) # a lie but a minor one # { Service-Context-Id } req.append(AVP_UTF8String(ProtocolConstants.DI_SERVICE_CONTEXT_ID,"*****@*****.**")) # { CC-Request-Type } req.append(AVP_Unsigned32(ProtocolConstants.DI_CC_REQUEST_TYPE,ProtocolConstants.DI_CC_REQUEST_TYPE_EVENT_REQUEST)) # { CC-Request-Number } req.append(AVP_Unsigned32(ProtocolConstants.DI_CC_REQUEST_NUMBER,0))
def test_Utils(self): #Test setMandatory avps = [] avps.append(AVP(1,"*****@*****.**")) avps.append(AVP(2,"*****@*****.**")) avps.append(AVP(999,"*****@*****.**")) avps.append(AVP(1,"*****@*****.**",-1)) Utils.setMandatory(avps,[1,2]) assert avps[0].isMandatory() assert avps[1].isMandatory() assert not avps[2].isMandatory() assert not avps[3].isMandatory() msg = Message() msg.append(AVP(1,"*****@*****.**")) msg.append(AVP(2,"*****@*****.**")) msg.append(AVP(999,"*****@*****.**")) msg.append(AVP(1,"*****@*****.**",-1)) Utils.setMandatory(msg,[1,2]) assert msg[0].isMandatory() assert msg[1].isMandatory() assert not msg[2].isMandatory() assert not msg[3].isMandatory() avps = [] avps.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"*****@*****.**")) avps.append(AVP(999,"*****@*****.**")) Utils.setMandatory_RFC3588(avps) assert avps[0].isMandatory() assert not avps[1].isMandatory() #test copyProxyInfo src_msg = Message() src_msg.append(AVP(1,"*****@*****.**")) src_msg.append(AVP_OctetString(ProtocolConstants.DI_PROXY_INFO,"fnox")) src_msg.append(AVP(999,"*****@*****.**",-1)) src_msg.append(AVP_OctetString(ProtocolConstants.DI_PROXY_INFO,"mox")) dst_msg = Message() Utils.copyProxyInfo(src_msg,dst_msg) assert len(dst_msg)==2 assert src_msg[1].code==dst_msg[0].code assert src_msg[1].payload==dst_msg[0].payload assert src_msg[3].code==dst_msg[1].code assert src_msg[3].payload==dst_msg[1].payload #Test the ABNF checker #create a conforming DPR msg = Message() msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value")) msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value")) msg.append(AVP(ProtocolConstants.DI_DISCONNECT_CAUSE,"abcd")) rc = Utils.checkABNF(msg,Utils.abnf_dpr) assert not rc #create a DPR with a missing AVP msg = Message() msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value")) msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value")) #msg.append(AVP(ProtocolConstants.DI_DISCONNECT_CAUSE,"abcd")) rc = Utils.checkABNF(msg,Utils.abnf_dpr) assert rc assert rc[1]==ProtocolConstants.DIAMETER_RESULT_MISSING_AVP #create a DPR with a duplicated AVP msg = Message() msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value")) msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value")) msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value")) msg.append(AVP(ProtocolConstants.DI_DISCONNECT_CAUSE,"abcd")) rc = Utils.checkABNF(msg,Utils.abnf_dpr) assert rc assert rc[1]==ProtocolConstants.DIAMETER_RESULT_AVP_OCCURS_TOO_MANY_TIMES assert rc[0] #create a DPR with a an extra AVP msg = Message() msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value")) msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value")) msg.append(AVP(ProtocolConstants.DI_DISCONNECT_CAUSE,"abcd")) msg.append(AVP(999,"A value")) rc = Utils.checkABNF(msg,Utils.abnf_dpr) assert rc assert rc[1]==ProtocolConstants.DIAMETER_RESULT_AVP_NOT_ALLOWED assert rc[0] #create a conforming ASR msg = Message() msg.append(AVP(ProtocolConstants.DI_SESSION_ID,"A value")) msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value")) msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value")) msg.append(AVP(ProtocolConstants.DI_DESTINATION_REALM,"A value")) msg.append(AVP(ProtocolConstants.DI_DESTINATION_HOST,"A value")) msg.append(AVP(ProtocolConstants.DI_AUTH_APPLICATION_ID,"A value")) rc = Utils.checkABNF(msg,Utils.abnf_asr) assert not rc #create a ASR with session-id the wrong palce msg = Message() msg.append(AVP(ProtocolConstants.DI_ORIGIN_HOST,"A value")) msg.append(AVP(ProtocolConstants.DI_SESSION_ID,"A value")) msg.append(AVP(ProtocolConstants.DI_ORIGIN_REALM,"A value")) msg.append(AVP(ProtocolConstants.DI_DESTINATION_REALM,"A value")) msg.append(AVP(ProtocolConstants.DI_DESTINATION_HOST,"A value")) msg.append(AVP(ProtocolConstants.DI_AUTH_APPLICATION_ID,"A value")) rc = Utils.checkABNF(msg,Utils.abnf_asr) assert rc assert rc[0]
def test_narrow2(self): try: a = AVP_Address.narrow(AVP(1, "\000\003\177\000\000\001")) except InvalidAddressTypeError as details: pass
def test_narrow1(self): try: a = AVP_Address.narrow(AVP(1, "\000\001\177\000\000")) except InvalidAVPLengthError as details: pass
def test_narrow(self): a = AVP_Address.narrow(AVP(1, "\000\001\177\000\000\001")) assert a.queryAddress()[0] == socket.AF_INET assert a.queryAddress()[1] == "127.0.0.1"