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 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_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 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_decode(self): avp1 = ControlMessageAVP(ControlMessageAVP.SCCRQ) avp2 = Hostname("TestAVP") avps = [avp1, avp2] pkt = L2tpv3ControlPacket(0x1234, 0, 1, avps) buf = pkt.encode() pkt1 = L2tpv3ControlPacket.decode(buf) self.assertEqual(pkt1.avps[0].attrType, 0) self.assertEqual(pkt1.avps[0].messageType, ControlMessageAVP.SCCRQ) self.assertEqual(pkt1.avps[1].attrType, 7) self.assertEqual(pkt1.avps[1].avpName, 'Hostname') # pkt's avp lenght is zero avps = list() pkt = L2tpv3ControlPacket(0x1234, 0, 1, avps) buf = pkt.encode() zlb = L2tpv3ControlPacket.decode(buf) self.assertTrue(zlb.isZlb)
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 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 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 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_decode(self): buf = struct.pack( '!118B', 0xc8, 0x03, 0x00, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x12, 0x00, 0x00, 0x00, 0x07, 0x52, 0x70, 0x68, 0x79, 0x4e, 0x6f, 0x64, 0x65, 0x2d, 0x46, 0x30, 0x38, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x3c, 0x5d, 0x0e, 0x01, 0x01, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x3d, 0xdd, 0x86, 0x69, 0x5e, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x0c, 0x00, 0x0d, 0x80, 0x32, 0x11, 0x8b, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, 0x09, 0x00, 0x0a, 0x00, 0x0b, 0x00, 0x0c, 0x00, 0x0d, 0x00, 0x0e, 0x00, 0x0f, 0x00, 0x10, 0x00, 0x11, 0x00, 0x12, 0x00, 0x13, 0x00, 0x14, 0x00, 0x15, 0x00, 0x16) try: pkt1 = L2tpv3ControlPacket.decode(buf) except Exception as e: self.assertRegexpMatches( str(e), "Cannot decode the buffer avp length must larger than 6, got*")