def test_submit_sm_resp_deferred(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp smpp.PDUReceived = mock.Mock(wraps=smpp.PDUReceived) smpp.sendPDU = mock.Mock(wraps=smpp.sendPDU) # Send submit_sm SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=self.shortMsg, ) yield smpp.sendDataRequest(SubmitSmPDU).addCallback(self.handleSubmitSmResp) # Unbind & Disconnect yield smpp.unbindAndDisconnect() ############## # Assertions : self.assertEquals(2, smpp.PDUReceived.call_count) self.assertEquals(2, smpp.sendPDU.call_count) recv1 = smpp.PDUReceived.call_args_list[0][0][0] recv2 = smpp.PDUReceived.call_args_list[1][0][0] sent1 = smpp.sendPDU.call_args_list[0][0][0] sent2 = smpp.sendPDU.call_args_list[1][0][0] self.assertTrue(isinstance(recv1, SubmitSMResp)) self.assertTrue(isinstance(recv1, sent1.requireAck)) self.assertEqual(recv1.status, CommandStatus.ESME_ROK) self.verifyUnbindSuccess(smpp, sent2, recv2)
def test_very_long_submit_sm_16bit(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp self.prepareMocks(smpp) # Send submit_sm UCS2 = {'\x0623', '\x0631', '\x0646', '\x0628'} content = self.composeMessage(UCS2, 3350) # 3350 = 67 * 10 SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=content, data_coding = 8, ) yield smpp.sendDataRequest(SubmitSmPDU) # Unbind & Disconnect yield smpp.unbindAndDisconnect() ############## # Assertions : self.assertEquals(self.long_content_max_parts + 1, smpp.PDUReceived.call_count) self.assertEquals(self.long_content_max_parts + 1, smpp.sendPDU.call_count)
def test_long_submit_sm_gsm0338(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp smpp.PDUReceived = mock.Mock(wraps=smpp.PDUReceived) smpp.sendPDU = mock.Mock(wraps=smpp.sendPDU) smpp.startLongSubmitSmTransaction = mock.Mock(wraps=smpp.startLongSubmitSmTransaction) smpp.cancelLongSubmitSmTransactions = mock.Mock(wraps=smpp.cancelLongSubmitSmTransactions) # Send submit_sm SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=self.concatenated2Msgs, ) yield self.assertFailure(smpp.sendDataRequest(SubmitSmPDU), SMPPClientConnectionCorruptedError) # Unbind & Disconnect yield smpp.unbindAndDisconnect() ############## # Assertions : self.assertEquals(1, smpp.PDUReceived.call_count) self.assertEquals(2, smpp.sendPDU.call_count) recv1 = smpp.PDUReceived.call_args_list[0][0][0] sent1 = smpp.sendPDU.call_args_list[0][0][0] self.assertEqual(recv1.status, CommandStatus.ESME_RINVCMDLEN) self.assertEqual(2, sent1.params['sar_total_segments']) self.assertEqual(1, sent1.params['sar_segment_seqnum']) self.assertEqual(0, len(smpp.longSubmitSmTxns)) self.assertEquals(1, smpp.startLongSubmitSmTransaction.call_count) self.assertEquals(1, smpp.cancelLongSubmitSmTransactions.call_count)
def test_long_submit_sm_gsm0338(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp smpp.PDUReceived = mock.Mock(wraps=smpp.PDUReceived) smpp.sendPDU = mock.Mock(wraps=smpp.sendPDU) smpp.startLongSubmitSmTransaction = mock.Mock(wraps=smpp.startLongSubmitSmTransaction) # Send submit_sm SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=self.concatenated2Msgs, ) yield smpp.sendDataRequest(SubmitSmPDU) # Unbind & Disconnect yield smpp.unbindAndDisconnect() ############## # Assertions : self.assertEquals(3, smpp.PDUReceived.call_count) self.assertEquals(3, smpp.sendPDU.call_count) recv1 = smpp.PDUReceived.call_args_list[0][0][0] recv2 = smpp.PDUReceived.call_args_list[1][0][0] recv3 = smpp.PDUReceived.call_args_list[2][0][0] sent1 = smpp.sendPDU.call_args_list[0][0][0] sent2 = smpp.sendPDU.call_args_list[1][0][0] sent3 = smpp.sendPDU.call_args_list[2][0][0] self.assertTrue(isinstance(recv1, SubmitSMResp)) self.assertTrue(isinstance(recv1, sent1.requireAck)) self.assertEqual(recv1.status, CommandStatus.ESME_RINVESMCLASS) self.assertEqual(2, sent1.params['sar_total_segments']) self.assertEqual(2, sent2.params['sar_total_segments']) self.assertEqual(1, sent1.params['sar_segment_seqnum']) self.assertEqual(2, sent2.params['sar_segment_seqnum']) self.assertEqual(sent1.params['sar_msg_ref_num'], sent2.params['sar_msg_ref_num']) self.assertTrue(isinstance(recv2, SubmitSMResp)) self.assertTrue(isinstance(recv2, sent1.requireAck)) self.assertEqual(recv2.status, CommandStatus.ESME_RINVESMCLASS) self.assertEqual(0, len(smpp.longSubmitSmTxns)) self.assertEquals(1, smpp.startLongSubmitSmTransaction.call_count) self.verifyUnbindSuccess(smpp, sent3, recv3)
def test_reconnect_on_connection_failure(self): client = SMPPClientFactory(self.config) client.reConnect = mock.Mock(wraps=client.reConnect) # Connect and bind yield client.connectAndBind() smpp = client.smpp smpp.PDUReceived = mock.Mock(wraps=smpp.PDUReceived) smpp.sendPDU = mock.Mock(wraps=smpp.sendPDU) # Unbind & Disconnect yield client.disconnect() ############## # Assertions : # Protocol verification self.assertEquals(1, smpp.PDUReceived.call_count) self.assertEquals(1, smpp.sendPDU.call_count) self.assertNotEqual(0, client.reConnect.call_count)
def test_reconnect_on_authentication_failure(self): client = SMPPClientFactory(self.config) client.reConnect = mock.Mock(wraps=client.reConnect) # Connect and bind yield client.connectAndBind() smpp = client.smpp reactor.callLater(5, client.disconnectAndDontRetryToConnect) # Normally, the client shall not exit since it should retry to connect # Triggering the exitDeferred callback is a way to continue this test reactor.callLater(6, client.exitDeferred.callback, None) yield client.getExitDeferred() ############## # Assertions : # Protocol verification self.assertNotEqual(0, client.reConnect.call_count)
class SMPPClientService(service.Service): def __init__(self, SMPPClientConfig, config): self.startCounter = 0 self.stopCounter = 0 self.config = config self.SMPPClientConfig = SMPPClientConfig self.SMPPClientFactory = SMPPClientFactory(SMPPClientConfig) self.SMPPClientServiceConfig = SMPPClientServiceConfig(self.config.getConfigFile()) # Set up a dedicated logger self.log = logging.getLogger(LOG_CATEGORY) if len(self.log.handlers) != 1: self.log.setLevel(self.SMPPClientServiceConfig.log_level) handler = TimedRotatingFileHandler( filename=self.SMPPClientServiceConfig.log_file, when=self.SMPPClientServiceConfig.log_rotate) formatter = logging.Formatter(self.SMPPClientServiceConfig.log_format, self.SMPPClientServiceConfig.log_date_format) handler.setFormatter(formatter) self.log.addHandler(handler) self.log.propagate = False self.log.info('New SMPPClientService for [%s]', self.SMPPClientConfig.id) def startService(self): self.startCounter += 1 service.Service.startService(self) self.log.info('Started service for [%s]', self.SMPPClientConfig.id) return self.SMPPClientFactory.connectAndBind().addErrback(self._startServiceErr) def stopService(self): self.stopCounter += 1 service.Service.stopService(self) self.log.info('Stopped service for [%s]', self.SMPPClientConfig.id) return self.SMPPClientFactory.disconnectAndDontRetryToConnect() def _startServiceErr(self, reason): self.log.info('Service starting failed with reason: %s', reason) self.stopService()
def test_submit_sm_receiver_failure(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp smpp.PDUReceived = mock.Mock(wraps=smpp.PDUReceived) smpp.sendPDU = mock.Mock(wraps=smpp.sendPDU) # Send submit_sm SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=self.shortMsg, ) try: yield smpp.sendDataRequest(SubmitSmPDU) except SMPPTransactionError: pass else: self.assertTrue(False, "SMPPTransactionError not raised")
def test_bind_with_systemType_defined(self): "Testing for #64, systemType must be always string" # Test 1: systemType in string self.config.bindOperation = 'transmitter' self.config.systemType = '999999' client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp # Session state check self.assertEqual(SMPPSessionStates.BOUND_TX, smpp.sessionState) # Unbind & Disconnect yield client.disconnect() # Test 1: systemType in integer self.config.systemType = 999999 client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp # Session state check self.assertEqual(SMPPSessionStates.BIND_TX_PENDING, smpp.sessionState) # Unbind & Disconnect yield client.disconnect()
def __init__(self, SMPPClientConfig, config): self.startCounter = 0 self.stopCounter = 0 self.config = config self.SMPPClientConfig = SMPPClientConfig self.SMPPClientFactory = SMPPClientFactory(SMPPClientConfig) self.SMPPClientServiceConfig = SMPPClientServiceConfig(self.config.getConfigFile()) # Set up a dedicated logger self.log = logging.getLogger(LOG_CATEGORY) if len(self.log.handlers) != 1: self.log.setLevel(self.SMPPClientServiceConfig.log_level) handler = TimedRotatingFileHandler( filename=self.SMPPClientServiceConfig.log_file, when=self.SMPPClientServiceConfig.log_rotate) formatter = logging.Formatter(self.SMPPClientServiceConfig.log_format, self.SMPPClientServiceConfig.log_date_format) handler.setFormatter(formatter) self.log.addHandler(handler) self.log.propagate = False self.log.info('New SMPPClientService for [%s]', self.SMPPClientConfig.id)
def test_bind_unbind_transceiver(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp smpp.PDUReceived = mock.Mock(wraps=smpp.PDUReceived) smpp.sendPDU = mock.Mock(wraps=smpp.sendPDU) # Session state check self.assertEqual(SMPPSessionStates.BOUND_TRX, smpp.sessionState) # Unbind & Disconnect yield client.disconnect() ############## # Assertions : # Protocol verification recv1, sent1 = self.verify(smpp, UnbindResp) # Unbind successfull self.assertEqual(recv1.status, CommandStatus.ESME_ROK) # Session state check self.assertEqual(SMPPSessionStates.UNBOUND, smpp.sessionState)
def test_long_submit_sm_7bit(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp self.prepareMocks(smpp) # Send submit_sm content = self.composeMessage(GSM0338, 1224) # 1224 = 153 * 8 SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=content, data_coding = 0, ) yield smpp.sendDataRequest(SubmitSmPDU) # Unbind & Disconnect yield smpp.unbindAndDisconnect() ############## # Assertions : self.runAsserts(smpp, content, len(content) / 153)
def test_bind_unbind_transceiver(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp smpp.PDUReceived = mock.Mock(wraps=smpp.PDUReceived) smpp.sendPDU = mock.Mock(wraps=smpp.sendPDU) # Session state check self.assertEqual(SMPPSessionStates.BOUND_TRX, smpp.sessionState) # Unbind & Disconnect yield client.disconnect() ############## # Assertions : # Protocol verification recv1, sent1 = self.verify(smpp, UnbindResp) # Unbind successfull self.assertEqual(recv1.status, CommandStatus.ESME_ROK) # Session state check self.assertEqual(SMPPSessionStates.UNBOUND, smpp.sessionState)
def test_long_submit_sm_8bit(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp self.prepareMocks(smpp) # Send submit_sm content = self.composeMessage(ISO8859_1, 1072) # 1072 = 134 * 8 SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=content, data_coding=3, ) yield smpp.sendDataRequest(SubmitSmPDU) # Unbind & Disconnect yield smpp.unbindAndDisconnect() ############## # Assertions : self.runAsserts(smpp, content, len(content) / 134)
def test_long_submit_sm_gsm0338(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp smpp.PDUReceived = mock.Mock(wraps=smpp.PDUReceived) smpp.sendPDU = mock.Mock(wraps=smpp.sendPDU) smpp.startLongSubmitSmTransaction = mock.Mock( wraps=smpp.startLongSubmitSmTransaction) smpp.cancelLongSubmitSmTransactions = mock.Mock( wraps=smpp.cancelLongSubmitSmTransactions) # Send submit_sm SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=self.concatenated2Msgs, ) yield self.assertFailure(smpp.sendDataRequest(SubmitSmPDU), SMPPClientConnectionCorruptedError) # Unbind & Disconnect yield smpp.unbindAndDisconnect() ############## # Assertions : self.assertEquals(1, smpp.PDUReceived.call_count) self.assertEquals(2, smpp.sendPDU.call_count) recv1 = smpp.PDUReceived.call_args_list[0][0][0] sent1 = smpp.sendPDU.call_args_list[0][0][0] self.assertEqual(recv1.status, CommandStatus.ESME_RINVCMDLEN) self.assertEqual(2, sent1.params['sar_total_segments']) self.assertEqual(1, sent1.params['sar_segment_seqnum']) self.assertEqual(0, len(smpp.longSubmitSmTxns)) self.assertEquals(1, smpp.startLongSubmitSmTransaction.call_count) self.assertEquals(1, smpp.cancelLongSubmitSmTransactions.call_count)
def test_long_submit_sm_16bit(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp self.prepareMocks(smpp) # Send submit_sm UCS2 = {'\x0623', '\x0631', '\x0646', '\x0628'} content = self.composeMessage(UCS2, 536) # 536 = 67 * 8 SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=content, data_coding = 8, ) yield smpp.sendDataRequest(SubmitSmPDU) # Unbind & Disconnect yield smpp.unbindAndDisconnect() ############## # Assertions : self.runAsserts(smpp, content, len(content) / 67)
def test_long_submit_sm_16bit(self): client = SMPPClientFactory(self.config) # Connect and bind yield client.connectAndBind() smpp = client.smpp self.prepareMocks(smpp) # Send submit_sm UCS2 = {'\x0623', '\x0631', '\x0646', '\x0628'} content = self.composeMessage(UCS2, 536) # 536 = 67 * 8 SubmitSmPDU = self.opFactory.SubmitSM( source_addr=self.source_addr, destination_addr=self.destination_addr, short_message=content, data_coding=8, ) yield smpp.sendDataRequest(SubmitSmPDU) # Unbind & Disconnect yield smpp.unbindAndDisconnect() ############## # Assertions : self.runAsserts(smpp, content, len(content) / 67)
def __init__(self, SMPPClientConfig, config): self.startCounter = 0 self.stopCounter = 0 self.config = config self.SMPPClientConfig = SMPPClientConfig self.SMPPClientFactory = SMPPClientFactory(SMPPClientConfig) self.SMPPClientServiceConfig = SMPPClientServiceConfig(self.config.getConfigFile()) # Set up a dedicated logger self.log = logging.getLogger(LOG_CATEGORY) if len(self.log.handlers) != 1: self.log.setLevel(self.SMPPClientServiceConfig.log_level) handler = logging.FileHandler(filename=self.SMPPClientServiceConfig.log_file) formatter = logging.Formatter(self.SMPPClientServiceConfig.log_format, self.SMPPClientServiceConfig.log_date_format) handler.setFormatter(formatter) self.log.addHandler(handler) self.log.propagate = False self.log.info('New SMPPClientService for [%s]', self.SMPPClientConfig.id)
def test_multiple_clients(self): """Reference to #28: SMPP client's logging does not work correctly with multiple connectors """ args = self.configArgs.copy() args['id'] = 'test-id-2' args['port'] = self.testPort args['bindOperation'] = 'transmitter' args['log_level'] = self.configArgs.get('log_level', logging.DEBUG) self.config2 = SMPPClientConfig(**args) client1 = SMPPClientFactory(self.config) client2 = SMPPClientFactory(self.config2) lc1 = LogCapture("smpp.client.%s" % client1.config.id) lc2 = LogCapture("smpp.client.%s" % client2.config.id) # Connect and bind yield client1.connectAndBind() yield client2.connectAndBind() # Unbind & Disconnect yield client1.disconnect() yield client2.disconnect() # Assert logging of client1 bindRequestsCount = 0 for record in lc1.records: if record.getMessage()[:30] == 'Requesting bind as transceiver': bindRequestsCount += 1 self.assertEqual(bindRequestsCount, 1) # Assert logging of client2 bindRequestsCount = 0 for record in lc2.records: if record.getMessage()[:30] == 'Requesting bind as transmitter': bindRequestsCount += 1 self.assertEqual(bindRequestsCount, 1)
def test_multiple_clients(self): """Reference to #28: SMPP client's logging does not work correctly with multiple connectors """ args = self.configArgs.copy() args['id'] = 'test-id-2' args['port'] = self.testPort args['bindOperation'] = 'transmitter' args['log_level'] = self.configArgs.get('log_level', logging.DEBUG) self.config2 = SMPPClientConfig(**args) client1 = SMPPClientFactory(self.config) client2 = SMPPClientFactory(self.config2) lc1 = LogCapture("smpp.client.%s" % client1.config.id) lc2 = LogCapture("smpp.client.%s" % client2.config.id) # Connect and bind yield client1.connectAndBind() yield client2.connectAndBind() # Unbind & Disconnect yield client1.disconnect() yield client2.disconnect() # Assert logging of client1 bindRequestsCount = 0 for record in lc1.records: if record.getMessage()[:30] == 'Requesting bind as transceiver': bindRequestsCount+= 1 self.assertEqual(bindRequestsCount, 1) # Assert logging of client2 bindRequestsCount = 0 for record in lc2.records: if record.getMessage()[:30] == 'Requesting bind as transmitter': bindRequestsCount+= 1 self.assertEqual(bindRequestsCount, 1)
def buildProtocol(self, addr): self.lastProto = SMPPClientFactory.buildProtocol(self, addr) return self.lastProto
def buildProtocol(self, addr): self.lastProto = SMPPClientFactory.buildProtocol(self, addr) return self.lastProto
def test_bind_to_blackhole(self): client = SMPPClientFactory(self.config) # Connect and bind yield self.assertFailure(client.connectAndBind(), SMPPSessionInitTimoutError)
def test_bind_to_blackhole(self): client = SMPPClientFactory(self.config) # Connect and bind yield self.assertFailure(client.connectAndBind(), SMPPSessionInitTimoutError)