def test_recvSLI(self): conn_address = '127.11.2.17' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.19' conn = L2tpConnection(18, 18, conn_address, local_addr) session = L2tpv3Session(1, 2, 'receive', conn) conn.addSession(session) localSessionID = session.remoteSessionId localAvp = LocalSessionID(localSessionID) avp1 = ControlMessageAVP(ControlMessageAVP.SLI) avp2 = CallSerialNumber() avps = [avp1, avp2] avps.append(localAvp) sli = L2tpv3ControlPacket(0, 0, 1, avps) self.assertEqual(sli.length, 40) self.assertEqual(sli.avps[0].messageType, ControlMessageAVP.SLI) ret = conn.recvSLI(sli) self.assertIsNone(ret) # Abnormal SLI message avps = list() sli = L2tpv3ControlPacket(0, 0, 1, avps) ret = conn.HandlePkt(sli) self.assertIsNone(ret) conn.CloseConnection()
def test_mcast_leave(self): """ICRQ request:""" session_receiver = L2tpv3Session(0, 0x40001111, 'receiver', self.conn) icrq = L2tpv3ControlPacket.decode(self.icrq_buf) print icrq icrq.SetPktConnection(self.conn) icrq.SetPktSession(session_receiver) icrp = session_receiver.ReceiveICRQ(icrq) print icrp session_receiver.fsmStateRecipientEnterStateEstablished( session_receiver.fsm.EventRecvGoodICCN) time.sleep(1) session_receiver_1 = L2tpv3Session(0, 0x40001112, 'receiver', self.conn) icrq = L2tpv3ControlPacket.decode(self.icrq_buf) avp_Mcast = L2tpv3CableLabsAvps.DepiRemoteMulticastJoin( ("5.6.7.8", "1.1.1.255")) icrq.avps.append(avp_Mcast) icrq.SetPktConnection(self.conn) icrq.SetPktSession(session_receiver) icrp = session_receiver.ReceiveICRQ(icrq) session_receiver.fsmStateRecipientEnterStateEstablished( session_receiver.fsm.EventRecvGoodICCN) time.sleep(1) session_receiver.fsmStateRecipientLeaveStateEstablished( session_receiver.fsm.EventRecvCDN) session_receiver.fsm.recvCDN() self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle)
def testReceiveUnnormalPacket(self): self.logger.info("start testing...") self.logger.info("************************************************") self.logger.info("*** ReceivePacket:unnormal ***") self.logger.info("************************************************") # struct unnormal ns/nr msgAvp = ControlMessageAVP(ControlMessageAVP.SCCRQ) assignedAvp = AssignedControlConnectionID(self.connection.localConnID) sccrq = L2tpv3ControlPacket(666, 100000, 100000, avps=(msgAvp, assignedAvp)) # before ReceivePacket, the nr is not change lastNr = self.transport.ackNr self.transport.ReceivePacket( sccrq, (self.connection.remoteAddr, self.connection.localConnID)) # after Receive the unnormal packets, the nr did't change self.assertEqual(self.transport.ackNr, lastNr) # struct duplicated packet zlb = ControlMessageAVP(ControlMessageAVP.ZLB) hostName = Hostname("TestAVP") avps = [] pkt1 = L2tpv3ControlPacket(self.connection.remoteConnID, 0, 0, avps) pkt2 = L2tpv3ControlPacket(self.connection.remoteConnID, 0, 0, avps) pkts = [pkt1, pkt2] # before ReceivePacket, the nr is not change lastNr = self.transport.ackNr for pkt in pkts: self.transport.ReceivePacket(pkt, None) # after Receive the unnormal packets, the nr did't change self.assertEqual(self.transport.ackNr, lastNr)
def test_ReceiveSLI(self): avp1 = ControlMessageAVP(ControlMessageAVP.SLI) avp2 = LocalSessionID(1) avp3 = CallSerialNumber(12) # Can't handle AVP in SLI , return CDN avps = [avp1, avp2, avp3] session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) sli = L2tpv3ControlPacket(0, 0, 1, avps) sli.SetPktConnection(self.conn) sli.Connection.remoteConnID = 1 ret = session_receiver.ReceiveSLI(sli) self.assertIsNone(ret) # AVP length is 1, return None avps_none = [avp1] session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) sli = L2tpv3ControlPacket(0, 0, 1, avps_none) sli.SetPktConnection(self.conn) sli.Connection.remoteConnID = 1 ret = session_receiver.ReceiveSLI(sli) self.assertIsNone(ret) self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle) L2tpv3GlobalSettings.MustAvpsCheck = True ret = session_receiver.ReceiveSLI(sli) sli.avps.append(avp2) avp_fake = fake_avp() sli.avps.append(avp_fake) ret = session_receiver.ReceiveSLI(sli) L2tpv3GlobalSettings.MustAvpsCheck = False self.assertIsInstance(ret, L2tpv3CDN) ret = session_receiver.ReceiveSLI(sli) self.assertIsInstance(ret, L2tpv3CDN)
def test_ReceiveICRQ(self): icrq = L2tpv3ControlPacket.decode(self.icrq_buf) icrq.avps.pop(4) icrq.SetPktConnection(self.conn) session_receiver = L2tpv3Session(0, 1, 'receiver', self.conn) icrq.Connection.remoteConnID = 1 # ICRQ session is None, receive a bad ICRQ, send a CDN cdn = session_receiver.ReceiveICRQ(icrq) self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle) self.assertEqual(cdn.avps[0].messageType, ControlMessageAVP.CDN) self.assertEqual(cdn.avps[1].messageType, ControlMessageAVP.StopCCN) # Receive a good ICRQ, send a ICRP icrq = L2tpv3ControlPacket.decode(self.icrq_buf) print icrq icrq.SetPktConnection(self.conn) icrq.SetPktSession(session_receiver) icrp = session_receiver.ReceiveICRQ(icrq) self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateWaitConn) self.assertEqual(icrp.avps[0].messageType, ControlMessageAVP.ICRP) L2tpv3GlobalSettings.MustAvpsCheck = True ret = session_receiver.ReceiveICRQ(icrp) self.assertIsInstance(ret, L2tpv3CDN) ret = session_receiver.ReceiveICRQ(icrq) L2tpv3GlobalSettings.MustAvpsCheck = False self.assertEqual(ret.avps[0].messageType, ControlMessageAVP.ICRP)
def test_ReceiveCDN(self): session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) # receive icrq, send icrp, state becomes to waitCtlConn icrq = L2tpv3ControlPacket.decode(self.icrq_buf) icrq.SetPktConnection(self.conn) icrq.SetPktSession(session_receiver) icrq.Connection.remoteConnID = 1 icrp = session_receiver.ReceiveICRQ(icrq) print icrp self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateWaitConn) # receive iccn, , state becomes to established avp1 = ControlMessageAVP(ControlMessageAVP.ICCN) avps = [avp1] iccn = L2tpv3ControlPacket(0, 0, 1, avps) ret = session_receiver.ReceiveICCN(iccn) self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateEstablished) # receive cdn message, clean up, state becomes to idle avp = ControlMessageAVP(ControlMessageAVP.CDN) avp_Mcast_leave = L2tpv3CableLabsAvps.DepiRemoteMulticastLeave( ("5.6.7.8", "229.1.1.255")) avps = [avp, avp_Mcast_leave] cdn = L2tpv3ControlPacket(0, 0, 1, avps) session_receiver.ReceiveCDN(cdn) self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle)
def testCreatePacketFromBuf(self): avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp2 = Hostname("TestAVP") avps = [avp1, avp2] pkt = L2tpv3ControlPacket(0x1234, 0, 1, avps) buf = pkt.encode() pkt1 = L2tpv3ControlPacket.decode(buf) print pkt1
def testHandlePkt_SCCCN(self): """Handle control packet: SCCCN. Function: recvSCCCN. :return: """ conn_address = '127.11.2.9' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.11' conn = L2tpConnection(10, 10, conn_address, local_addr) # Receive a good SCCCN, return NULL avp1 = ControlMessageAVP(ControlMessageAVP.SCCCN) avp2 = AssignedControlConnectionID(10) avps = [avp1, avp2] scccn = L2tpv3ControlPacket(0x1234, 0, 1, avps) ret = conn.HandlePkt(scccn) self.assertIsNotNone(ret) self.assertIsInstance(ret, L2tpv3ACK) # Receive a bad SCCCN, return stopCCN session = L2tpv3Session(1, 2, 'receive', conn) conn.addSession(session) localSessionID = session.remoteSessionId localAvp = LocalSessionID(localSessionID) avp3 = CallSerialNumber() avps.append(localAvp) avps.append(avp3) scccn = L2tpv3ControlPacket(0x1234, 0, 1, avps) ret = conn.HandlePkt(scccn) self.assertIsNotNone(ret) self.assertIsInstance(ret, L2tpv3ACK) # Recovery tunnel receive SCCCN recover_conn = L2tpConnection(100, 100, conn_address, local_addr) recover_conn.isInRecovery = True conn.recoverConnection = recover_conn conn.isRecoveryTunnel = True avp1 = ControlMessageAVP(ControlMessageAVP.SCCCN) avp2 = AssignedControlConnectionID(10) avps = [avp1, avp2] scccn = L2tpv3ControlPacket(0x1234, 0, 1, avps) ret = conn.HandlePkt(scccn) self.assertFalse(recover_conn.isInRecovery) conn.CloseConnection()
def test_handleAvp(self): vsp_avp_attrType1 = 10 vsp_avp_attrType2 = 20 vsp_avp1 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID, vsp_avp_attrType1, attrValue="11111111111") vsp_avp2 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID, vsp_avp_attrType2, notifyVendor=1, attrValue="222222222222") ctrlIdsList = [] ctrlIdsList.append(L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP) vsp_avp_attrType3 = 30 vsp_avp3_attrValue = "3333333333" vsp_avp3 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID, vsp_avp_attrType3, OutCtrlIds=ctrlIdsList, attrValue=vsp_avp3_attrValue) # Receive a good ICRQ, send a ICRP session_receiver = L2tpv3Session(0, 1, 'receiver') icrq = L2tpv3ControlPacket.decode(self.icrq_buf) # append couple VSP AVP icrq.avps.append(vsp_avp1) icrq.avps.append(vsp_avp2) icrq.avps.append(vsp_avp3) # print icrq # icrq.SetPktConnection(self.conn) # icrq.SetPktSession(session_receiver) #icrp = session_receiver.ReceiveICRQ(icrq) # Observe that only vsp_avp3 is appended to icrp # self.assertEqual( # session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateWaitConn) #self.assertEqual(icrp.avps[0].messageType, ControlMessageAVP.ICRP) avps = list() avps.append( L2tpv3RFC3931AVPs.ControlMessageAVP( L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP)) icrp = L2tpv3ControlPacket(6600, 0, 0, avps) for avp in icrq.avps: avp.handleAvp(icrq, icrp) # vsp_avp3 has non-None OutCtrlIds so it should be moved from icrq to icrp now found_vsp_avp_in_icrp = 0 for avp in icrp.avps: if (isinstance(avp, l2tpv3SampleVendorAvp) and avp.attrValue == vsp_avp3_attrValue): found_vsp_avp_in_icrp = 1 self.assertEqual(found_vsp_avp_in_icrp, 1)
def test_isFSR(self): # SCCRQ avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp2 = Hostname("TestAVP") avps = [avp1, avp2] pkt = L2tpv3ControlPacket(1, 0, 1, avps, "testL2tpv3ControlPacket") self.assertFalse(pkt.isFSR()) # FSR avp1 = ControlMessageAVP(ControlMessageAVP.FSR) avp2 = FailoverSessionState(1, 2) avps = [avp1, avp2] pkt = L2tpv3ControlPacket(1, 0, 1, avps, "testL2tpv3ControlPacket") self.assertTrue(pkt.isFSR())
def test_sendSLI(self): session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) # receive icrq, send icrp, state becomes to waitCtlConn icrq = L2tpv3ControlPacket.decode(self.icrq_buf) icrq.SetPktConnection(self.conn) icrq.SetPktSession(session_receiver) icrq.Connection.remoteConnID = 1 icrp = session_receiver.ReceiveICRQ(icrq) self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateWaitConn) # receive iccn, , state becomes to established avp1 = ControlMessageAVP(ControlMessageAVP.ICCN) avps = [avp1] iccn = L2tpv3ControlPacket(0, 0, 1, avps) ret = session_receiver.ReceiveICCN(iccn) self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateEstablished) # start test msg = L2tpv3Hal_pb2.t_l2tpSessionCircuitStatus() msg.status = False session_receiver.local_circuit_status = True session_receiver.ReceiveHalMsg(msg=msg) self.assertFalse(session_receiver.local_circuit_status) session_receiver.local_circuit_status = False session_receiver.ReceiveHalMsg(msg=msg) self.assertFalse(session_receiver.local_circuit_status) msg.status = True session_receiver.local_circuit_status = True session_receiver.ReceiveHalMsg(msg=msg) self.assertTrue(session_receiver.local_circuit_status) session_receiver.local_circuit_status = False session_receiver.ReceiveHalMsg(msg=msg) self.assertTrue(session_receiver.local_circuit_status) msg = L2tpv3Hal_pb2.t_l2tpSessionRsp() session_receiver.local_circuit_status = False msg.result = False session_receiver.ReceiveHalMsg(msg=msg) self.assertFalse(session_receiver.local_circuit_status) msg.result = True msg.req_data.circuit_status = True session_receiver.ReceiveHalMsg(msg=msg) self.assertTrue(session_receiver.local_circuit_status)
def testHandlePkt_SCCRQ(self): """Handle control packet: SCCRQ. Function: recvSCCRQ. :return: """ conn_address = '127.11.2.7' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.9' conn = L2tpConnection(8, 8, conn_address, local_addr) # Receive a good SCCRQ, return SCCRP avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp2 = Hostname("TestAVP") avp3 = RouterID(0) avp4 = AssignedControlConnectionID(1) avp5 = PseudowireCapList(()) avps = [avp1, avp2, avp3, avp4, avp5] sccrq = L2tpv3ControlPacket(0x1234, 0, 1, avps) sccrp = conn.HandlePkt(sccrq) self.assertEqual(sccrp.connectionID, 8) self.assertEqual(sccrp.length, 28) self.assertEqual(sccrp.avps[0].length, 8) self.assertEqual(conn.fsm.current, L2tpv3ConnectionFsm.StateWaitCtlConn) # self.assertEqual(sccrp.avps[0].messageType, ControlMessageAVP.SCCRP) # Receive a bad SCCRQ, return stopCCN session = L2tpv3Session(1, 2, 'receiver', conn) conn.addSession(session) localSessionID = session.remoteSessionId localAvp = LocalSessionID(localSessionID) avps.append(localAvp) sccrp = L2tpv3ControlPacket(0x1234, 0, 1, avps) stopCCN = conn.HandlePkt(sccrp) self.assertEqual(stopCCN.length, 58) self.assertEqual(stopCCN.connectionID, 8) self.assertEqual(stopCCN.avps[0].length, 8) self.assertEqual( stopCCN.avps[0].messageType, ControlMessageAVP.StopCCN)
def test_allocate_local_session_id(self): conn_address = '127.11.2.25' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.24' conn = L2tpConnection(12, 12, conn_address, local_addr) pkt = L2tpv3ControlPacket() ret = conn._findSession(pkt=pkt) self.assertIsNone(ret) conn.recvICCN(pkt) conn.recvCDN(pkt) conn.recvICRP(pkt) conn.recvSLI(pkt) L2tpv3GlobalSettings.MustAvpsCheck = True ret = conn.recvSCCRQ(pkt) self.assertIsInstance(ret, L2tpv3StopCCN) ret = conn.recvSCCRP(pkt) ret = conn.recvSCCCN(pkt) ret = conn.recvStopCCN(pkt) ret = conn.allocate_local_session_id(pkt=pkt) self.assertEqual(ret, 0) remoteEndId = RemoteEndID() localsession = LocalSessionID(0x112) pkt.avps.append(remoteEndId) pkt.avps.append(localsession) ret = conn.allocate_local_session_id(pkt=pkt) self.assertEqual(ret, 0) sublayer = DepiL2SpecificSublayerSubtype(17) pkt.avps.append(sublayer) ret = conn.allocate_local_session_id(pkt=pkt) self.assertEqual(ret, 0x1100000) icrq = L2tpv3ControlPacket.decode(self.ircq_buf) ret = conn.allocate_local_session_id(pkt=icrq) self.assertEqual(ret, 0x10030000) for avp in icrq.avps: if isinstance(avp, RemoteSessionID): icrq.avps.remove(avp) avp_remote = RemoteSessionID(0x80000001) icrq.avps.append(avp_remote) ret = conn.allocate_local_session_id(pkt=icrq) self.assertEqual(ret, 0x80000001) conn.CloseConnection() conn.CloseConnection()
def test_isRecoveryTunnelSCCRQ(self): # Normal SCCRQ avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp2 = Hostname("TestAVP") avps = [avp1, avp2] pkt = L2tpv3ControlPacket(1, 0, 1, avps, "testL2tpv3ControlPacket") self.assertEqual(pkt.isRecoveryTunnelSCCRQ(), (False, 0, 0)) # RecoveryTunnelSCCRQ avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp2 = Hostname("TestAVP") avp3 = TunnelRecovery(1, 2) avps = [avp1, avp2, avp3] pkt = L2tpv3ControlPacket(1, 0, 1, avps, "testL2tpv3ControlPacket") self.assertTrue(pkt.isRecoveryTunnelSCCRQ())
def test_GetLocalConnectionID(self): avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp2 = Hostname("TestAVP") avps = [avp1, avp2] pkt = L2tpv3ControlPacket(1, 0, 1, avps, "testL2tpv3ControlPacket") LocalConnectionID = pkt.GetLocalConnectionID() self.assertEqual(LocalConnectionID, 1)
def testHandlePkt_FirstAvpIsNotControlAvp(self): """Handle control packet: the first avp is not control avp. :return: """ conn_address = '127.11.2.4' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.6' conn = L2tpConnection(5, 5, conn_address, local_addr) avp1 = Hostname("TestAVP") avp2 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp3 = RouterID(0) avp4 = AssignedControlConnectionID(1) avp5 = PseudowireCapList(()) avps = [avp1, avp2, avp3, avp4, avp5] sccrq = L2tpv3ControlPacket(0x1234, 0, 1, avps) ret = conn.HandlePkt(sccrq) self.assertIsNone(ret) conn.CloseConnection() self.assertIsNone(ret)
def test_recvICCN(self): conn_address = '127.11.2.13' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.15' conn = L2tpConnection(14, 14, conn_address, local_addr) session = L2tpv3Session(3, 4, 'receive', conn) conn.addSession(session) # localSessionID = session.remoteSessionId remoteSessionID = session.localSessionId # localAvp = LocalSessionID(localSessionID) remoteAvp = RemoteSessionID(remoteSessionID) # Normal case, session is not none avp = ControlMessageAVP(ControlMessageAVP.ICCN) avps = [avp] avps.append(remoteAvp) iccn = L2tpv3ControlPacket(0, 0, 1, avps) self.assertEqual(iccn.length, 30) self.assertEqual(iccn.avps[0].messageType, 12) ret = conn.HandlePkt(iccn) self.assertIsNotNone(ret) self.assertIsInstance(ret, L2tpv3ACK) conn.CloseConnection()
def test_resetTransport(self): conn_address = '127.11.2.33' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.35' conn = L2tpConnection(18, 18, conn_address, local_addr) avp = ControlMessageAVP(ControlMessageAVP.HELLO) avps = [avp] hello = L2tpv3ControlPacket(0, 0, 1, avps) conn.transport.sendList.append({ "time": 1, "pkt": hello, "sendTimes": 0, }) conn.transport.receiveWindow.add(hello) conn.transport.ackNr = 105 conn.transport.ns = 56 conn.resetTransport() self.assertFalse(conn.transport.sendList) self.assertFalse(conn.transport.receiveWindow) self.assertEqual(conn.transport.ackNr, 0) self.assertEqual(conn.transport.ackNr, 0) conn.CloseConnection()
def test_recvFSR(self): """Handle control packet: FSR. Function: recvFSR. :return: """ conn_address = '127.11.2.29' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.31' conn = L2tpConnection(18, 18, conn_address, local_addr) session = L2tpv3Session(1, 2, 'receive', conn) conn.addSession(session) avp1 = ControlMessageAVP(ControlMessageAVP.FSR) avp2 = FailoverSessionState(session.remoteSessionId, session.localSessionId) avps = [avp1, avp2] fsr = L2tpv3ControlPacket(0, 0, 1, avps) ret = conn.HandlePkt(fsr) self.assertIsNone(ret) conn.CloseConnection()
def test_recvFSQ(self): """Handle control packet: FSQ. Function: recvFSQ. :return: """ conn_address = '127.11.2.28' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = True local_addr = '127.11.2.30' conn = L2tpConnection(18, 18, conn_address, local_addr) session = L2tpv3Session(1, 2, 'receive', conn) conn.addSession(session) avp1 = ControlMessageAVP(ControlMessageAVP.FSQ) avp2 = FailoverSessionState(session.remoteSessionId, session.localSessionId) avp3 = FailoverSessionState(4, 5) avps = [avp1, avp2, avp3] fsq = L2tpv3ControlPacket(0, 0, 1, avps) fsr = conn.HandlePkt(fsq) self.assertIsNotNone(fsr) self.assertEqual(fsr.avps[1].sessionID, 1) self.assertEqual(fsr.avps[1].remoteSessionID, 2) self.assertEqual(fsr.avps[2].sessionID, 0) self.assertEqual(fsr.avps[2].remoteSessionID, 4) conn.CloseConnection()
def test_ReceiveICRP(self): avp1 = ControlMessageAVP(ControlMessageAVP.ICRP) avp2 = LocalSessionID(1) avps = [avp1, avp2] icrp = L2tpv3ControlPacket(0, 0, 1, avps) # fsm is L2tpv3SessionRecipientFsm session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) session_receiver.ReceiveICRP(icrp) self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle) # fsm is L2tpv3SessionSenderFsm, receive a bad icrp, return cdn session_sender = L2tpv3Session(1, 2, 'sender', self.conn) cdn = session_sender.ReceiveICRP(icrp) self.assertEqual(cdn.avps[0].messageType, ControlMessageAVP.CDN) # fsm is L2tpv3SessionSenderFsm, receive a good icrp, return iccn icrp.SetPktSession(session_sender) iccn = session_sender.ReceiveICRP(icrp) self.assertEqual(iccn.avps[0].messageType, ControlMessageAVP.ICCN) L2tpv3GlobalSettings.MustAvpsCheck = True ret = session_sender.ReceiveICRP(icrp) L2tpv3GlobalSettings.MustAvpsCheck = False self.assertIsInstance(ret, L2tpv3CDN) session_sender = L2tpv3Session(1, 0, 'sender', self.conn) icrp.SetPktSession(session_sender) icrp.avps.remove(avp2) iccn = session_sender.ReceiveICRP(icrp) self.assertIsInstance(ret, L2tpv3CDN)
def test_recvHELLO(self): conn_address = '127.11.2.16' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.18' conn = L2tpConnection(17, 17, conn_address, local_addr) session = L2tpv3Session(1, 2, 'receive', conn) conn.addSession(session) localSessionID = session.remoteSessionId localAvp = LocalSessionID(localSessionID) avp = ControlMessageAVP(ControlMessageAVP.HELLO) avps = [avp] avps.append(localAvp) hello = L2tpv3ControlPacket(0, 0, 1, avps) self.assertEqual(hello.length, 30) self.assertEqual(hello.avps[0].messageType, 6) zlb = conn.HandlePkt(hello) self.assertEqual(zlb.length, 12) conn.CloseConnection()
def test_recvICRP(self): conn_address = '127.11.2.15' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.17' conn = L2tpConnection(16, 16, conn_address, local_addr) session = L2tpv3Session(1, 2, 'receive', conn) conn.addSession(session) localSessionID = session.remoteSessionId localAvp = LocalSessionID(localSessionID) avp = ControlMessageAVP(ControlMessageAVP.ICRP) avps = [avp] avps.append(localAvp) icrp = L2tpv3ControlPacket(0, 0, 1, avps) self.assertEqual(icrp.length, 30) self.assertEqual(icrp.avps[0].messageType, 11) ret = conn.HandlePkt(icrp) self.assertIsNone(ret) conn.CloseConnection()
def test_recvCDN(self): conn_address = '127.11.2.14' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.16' conn = L2tpConnection(15, 15, conn_address, local_addr) session = L2tpv3Session(1, 2, 'receive', conn) conn.addSession(session) localSessionID = session.remoteSessionId localAvp = LocalSessionID(localSessionID) avp = ControlMessageAVP(ControlMessageAVP.CDN) avps = [avp] avps.append(localAvp) cdn = L2tpv3ControlPacket(0, 0, 1, avps) self.assertEqual(cdn.length, 30) self.assertEqual(cdn.avps[0].messageType, 14) conn.HandlePkt(cdn) self.assertEqual(conn.fsm.current, 'idle') conn.CloseConnection()
def testTimetickCallback(self): self.logger.info("start testing...") self.logger.info("************************************************") self.logger.info("*** TimetickCallback:normal ***") self.logger.info("************************************************") connection = L2tpv3Connection.L2tpConnection( 1666, 1666, L2tpv3GlobalSettings.L2tpv3GlobalSettings.LocalIPAddress) # send a SCCRQ form remote msgAvp = ControlMessageAVP(ControlMessageAVP.SCCRQ) assignedAvp = AssignedControlConnectionID(connection.localConnID) sccrq = L2tpv3ControlPacket(1666, avps=(msgAvp, assignedAvp)) connection.transport.SendPacket( sccrq, (connection.remoteAddr, connection.localConnID)) # before timetick callback, the value nr/ns # lastNr = transport.ackNr lastNs = connection.transport.ns connection.transport.sendTimeout = 0 connection.transport.resendTimes = 1 connection.transport.TimetickCallback() # after timetick callback, need to send the stopccn, sothe ns +1 self.assertEqual(lastNs, connection.transport.ns) connection.transport.sendTimeout = 0 connection.transport.resendTimes = 0.5 connection.transport.TimetickCallback() self.assertEqual(connection.connection_status, L2tpv3Connection.L2tpConnection.CLOSED)
def test_recvICRQ_normal_1(self): """Handle control packet: ICRQ, session exist. :return: none """ conn_address = '127.11.2.11' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.13' conn = L2tpConnection(12, 12, conn_address, local_addr) icrq = L2tpv3ControlPacket.decode(self.ircq_buf) icrp = conn.HandlePkt(icrq) print icrp self.assertEqual(icrp.connectionID, 12) ret = conn.checkMustAvps( L2tpv3ControlPacket.ICRPMandatoryAVPs, icrp.avps) self.assertTrue(ret) self.assertEqual(icrp.avps[0].messageType, ControlMessageAVP.ICRP) session = conn.findSessionByRemoteSessionID(0x40000001) self.assertIsNotNone(session) conn.CloseConnection()
def testCreateControlPacket(self): avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp2 = Hostname("TestAVP") avps = [avp1, avp2] pkt = L2tpv3ControlPacket(0x1234, 0, 1, avps) print pkt
def testGenerateBuffer(self): avp1 = ControlMessageAVP(ControlMessageAVP.ZLB) avp2 = Hostname("TestAVP") avps = [] pkt = L2tpv3ControlPacket(0x1234, 0, 1, avps) print pkt
def test_encode(self): avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp2 = Hostname("TestAVP") avps = [avp1, avp2] pkt = L2tpv3ControlPacket(0x1234, 0, 1, avps, "testL2tpv3ControlPacket") pkt.encode(False) pkt.encode(True)
def testUnnormalTimetickCallback(self): self.logger.info("start testing...") self.logger.info("************************************************") self.logger.info("*** TimetickCallback:unnormal ***") self.logger.info("************************************************") connection = L2tpv3Connection.L2tpConnection( 1888, 1888, L2tpv3GlobalSettings.L2tpv3GlobalSettings.LocalIPAddress) # send a zlb form remote msgAvp = ControlMessageAVP(ControlMessageAVP.ZLB) assignedAvp = AssignedControlConnectionID(connection.localConnID) zlb = L2tpv3ControlPacket(1888, avps=(msgAvp, assignedAvp)) connection.transport.SendPacket( zlb, (connection.remoteAddr, connection.localConnID)) # send a ZLB form remote, before timetick callback is True lastNeedSendZlb = connection.transport.needSendZlb = True connection.transport.sendZlbTimeout = 0 connection.transport.TimetickCallback() # after send ZLB, the needSendZlb change to False self.assertNotEqual(connection.transport.needSendZlb, lastNeedSendZlb) connection.CloseConnection() connection = L2tpv3Connection.L2tpConnection( 2000, 2000, L2tpv3GlobalSettings.L2tpv3GlobalSettings.LocalIPAddress) # send a zlb form remote msgAvp = ControlMessageAVP(ControlMessageAVP.ZLB) assignedAvp = AssignedControlConnectionID(connection.localConnID) zlb = L2tpv3ControlPacket(2000, avps=(msgAvp, assignedAvp)) connection.transport.SendPacket( zlb, (connection.remoteAddr, connection.localConnID)) connection.transport.lastTimetick = time.time() - 60 sendListTime = connection.transport.sendList[0]["time"] connection.transport.TimetickCallback() # after timetick callback, this time should be updated self.assertTrue( connection.transport.sendList[0]["time"] > sendListTime) """connection.transport.lastTimetick = time.time() + 60 sendListTime = connection.transport.sendList[0]["time"] connection.transport.TimetickCallback() # after timetick callback, this time should be updated self.assertFalse(connection.transport.sendList[0]["time"] > sendListTime)""" connection.CloseConnection()