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 testConnection_init(self): conn_address = '127.11.2.1' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.2' # localConnectionID not equal to zero conn1 = L2tpConnection(1, 1, conn_address, local_addr) session1 = L2tpv3Session(1, 2, 'sender', conn1) conn1.addSession(session1) self.assertEqual(conn1.sessions.keys(), [1]) self.assertEqual(conn1.sessionsByRemoteSessionId.keys(), [2]) self.assertEqual(conn1.localConnID, 1) self.assertIsNotNone(conn1.ConnectionDb) # localConnectionID equal to zero local_addr = '127.11.2.3' conn2 = L2tpConnection(0, 2, conn_address, local_addr) session2 = L2tpv3Session(1, 2, 'sender', conn2) conn2.addSession(session2) self.assertEqual(conn2.sessions.keys(), [1]) self.assertEqual(conn2.sessionsByRemoteSessionId.keys(), [2]) self.assertIsNotNone(conn2.localConnID) self.assertIsNotNone(conn2.ConnectionDb) conn1.CloseConnection() conn2.CloseConnection()
def test_fsmStateRecipientIdle_1(self): session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) session_receiver.fsm.recvGoodICRQ() self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateWaitConn) # receive CDN session_receiver.fsm.recvCDN() # self.assertIsNone(session_receiver.connection) self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle) session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) session_receiver.fsm.recvGoodICRQ() self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateWaitConn) # receive closeRequest session_receiver.fsm.closeRequest() self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle) # receive other event to idle session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) session_receiver.fsm.recvGoodICRQ() session_receiver.fsm.recvBadICCN() self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle)
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_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 testL2tpv3Session_init(self): session1 = L2tpv3Session(1, 2, 'sender', self.conn) self.assertEqual(session1.localSessionId, 1) self.assertEqual(session1.remoteSessionId, 2) session2 = L2tpv3Session(1, 2, 'receive', self.conn) self.assertEqual(session2.remoteSessionId, 2)
def test_CloseSession(self): session_sender = L2tpv3Session(1, 2, 'sender', self.conn) session_sender.CloseSession() self.assertEqual(session_sender.fsm.current, L2tpv3SessionSenderFsm.StateIdle) session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) session_receiver.CloseSession() self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle)
def test_receive_hal_message(self): addr = "127.0.0.1" conn = L2tpConnection(localConnectionID=0, remoteConnectionID=1, remoteAddr=addr, localAddr=addr) session_receiver = L2tpv3Session(1, 2, 'receiver', conn) conn.addSession(session_receiver) msg = L2tpv3Hal_pb2.t_l2tpSessionCircuitStatus() msg.session_selector.local_ip = addr msg.session_selector.remote_ip = addr msg.session_selector.local_session_id = 1 self.l2tp_dispatcher.receive_hal_message(msg) msg.session_selector.local_session_id = 2 self.l2tp_dispatcher.receive_hal_message(msg) # case 2 msg = L2tpv3Hal_pb2.t_l2tpSessionRsp() msg.session_selector.local_ip = addr msg.session_selector.remote_ip = addr msg.session_selector.local_session_id = 1 conn.removeSession(session_receiver) self.l2tp_dispatcher.receive_hal_message(msg) addr = "::1" conn = L2tpConnection(localConnectionID=0, remoteConnectionID=1, remoteAddr=addr, localAddr=addr) session_receiver = L2tpv3Session(1, 2, 'receiver', conn) conn.addSession(session_receiver) msg = L2tpv3Hal_pb2.t_l2tpSessionCircuitStatus() msg.session_selector.local_ip = addr msg.session_selector.remote_ip = addr msg.session_selector.local_session_id = 1 self.l2tp_dispatcher.receive_hal_message(msg) msg.session_selector.local_session_id = 2 self.l2tp_dispatcher.receive_hal_message(msg) # case 4 msg = L2tpv3Hal_pb2.t_l2tpSessionRsp() msg.session_selector.local_ip = addr msg.session_selector.remote_ip = addr msg.session_selector.local_session_id = 1 conn.removeSession(session_receiver) self.l2tp_dispatcher.receive_hal_message(msg)
def test_LocalRequest(self): # fsm is L2tpv3SessionRecipientFsm session_receiver = L2tpv3Session(1, 2, 'receiver', self.conn) session_receiver.LocalRequest() self.assertEqual(session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateIdle) # fsm is L2tpv3SessionSenderFsm session_sender = L2tpv3Session(1, 2, 'sender', self.conn) session_sender.LocalRequest() self.assertEqual(session_sender.fsm.current, L2tpv3SessionSenderFsm.StateWaitReply)
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 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_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_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_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_add_remove_find_Session(self): conn_address = '127.11.2.21' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.23' conn = L2tpConnection(22, 22, conn_address, local_addr) session = L2tpv3Session(1, 2, 'sender', conn) session1 = None conn.addSession(session) conn.addSession(session1) self.assertEqual(conn.sessions.keys(), [1]) self.assertEqual(conn.sessionsByRemoteSessionId.keys(), [2]) self.assertIsNotNone( conn.findSessionByLocalSessionID(session.localSessionId)) self.assertIsNotNone( conn.findSessionByRemoteSessionID(session.remoteSessionId)) conn.removeSession(session) self.assertEqual(conn.sessions.keys(), []) self.assertEqual(conn.sessionsByRemoteSessionId.keys(), []) self.assertIsNone( conn.findSessionByLocalSessionID(session.localSessionId)) self.assertIsNone( conn.findSessionByRemoteSessionID(session.remoteSessionId)) 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_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_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 testStopConnection(self): conn_address = '127.11.2.26' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False L2tpv3GlobalSettings.l2tp_hal_client = fake_hal() local_addr = '127.11.2.27' L2tpConnection.ConnectionDb.clear() conn = L2tpConnection(3, 3, conn_address, local_addr) session = L2tpv3Session(1, 2, 'receive', conn) conn.addSession(session) conn.ReceiveHalMsg(msg=None) msg = L2tpv3Hal_pb2.t_l2tpLcceAssignmentRsp() msg.result = False try: conn.ReceiveHalMsg(msg) except Exception as e: pass msg.result = True conn.ReceiveHalMsg(msg) conn.SendHalMsg(msg_type=L2tpConnection.ADD_LCCE) conn.fsmEventrecvBadSCCRP(event=None) conn.fsmEventrecvSCCRQLoseTieGood(event=None) conn.fsmEventrecvSCCRQLoseTieBad(event=None) conn.fsmEventrecvSCCRQWinSCCRQ(event=None) conn.fsmEventcloseRequest(event=None) conn.fsmEventHalError(event=None) conn.StopConnection() conn.StopConnection() self.assertEqual(len(conn.ConnectionDb), 0) L2tpv3GlobalSettings.l2tp_hal_client = None
def test_SendHalMsg(self): halclient = fake_halclient() L2tpv3GlobalSettings.l2tp_hal_client = halclient session_receiver = L2tpv3Session(0x1098, 0x40001111, 'receiver', self.conn) session_receiver.SendHalMsg(L2tpv3Session.ADD_SESSION) L2tpv3GlobalSettings.l2tp_hal_client = None self.assertEqual(halclient.called, 1)
def test_fsm(self): session_receiver = L2tpv3Session(0x1098, 0x40001111, 'receiver', self.conn) session_receiver.fsmEventSenderRecvICRQLoseTie(None) session_receiver.fsmEventSenderRecvICRQWinTie(None) session_receiver.fsmEventSenderRecvICCN(None) session_receiver.fsmEventSenderRecvCDN(None) pass
def test_ICRQ_1(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 ret = self.conn.checkMustAvps(L2tpv3ControlPacket.ICRPMandatoryAVPs, icrp.avps) self.assertTrue(ret)
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_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 testL2tpv3CDN(self): conn_address = '127.13.3.6' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher(global_dispatcher, conn_address, False, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.LocalIPAddress = '127.13.3.6' conn = L2tpConnection(5, 5, conn_address) session = L2tpv3Session(1, 2, 'receiver', conn) cdn = L2tpv3CDN(session, 2, 4, "Avp cannot be handled correctly") self.assertEqual(cdn.avps[0].messageType, ControlMessageAVP.CDN) self.assertEqual(cdn.avps[1].messageType, ControlMessageAVP.StopCCN) self.assertEqual(cdn.avps[2].attrType, 63) self.assertEqual(cdn.avps[3].attrType, 64)
def test_queryStaleSessions(self): conn_address = '127.11.2.31' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.33' conn = L2tpConnection(18, 18, conn_address, local_addr) session = L2tpv3Session(1, 2, 'receive', conn) session.stale = True conn.addSession(session) conn.queryStaleSessions() conn.CloseConnection()
def test_closeUnEstSessions(self): conn_address = '127.11.2.32' global_dispatcher = Dispatcher() dispatcher = L2tpv3Dispatcher( global_dispatcher, conn_address, True, None) L2tpv3GlobalSettings.Dispatcher = dispatcher L2tpv3GlobalSettings.MustAvpsCheck = False local_addr = '127.11.2.34' conn = L2tpConnection(18, 18, conn_address, local_addr) session = L2tpv3Session(1, 2, 'receive', conn) conn.addSession(session) session.fsm.recvGoodICRQ() conn.closeUnEstSessions() self.assertFalse(conn.sessions) session = L2tpv3Session(3, 4, 'receive', conn) conn.addSession(session) session.fsm.recvGoodICRQ() session.fsm.recvGoodICCN() conn.closeUnEstSessions() self.assertTrue(conn.sessions) self.assertEqual(session.fsm.current, L2tpv3Fsm.L2tpv3SessionRecipientFsm.StateEstablished) conn.CloseConnection()