def test_doppelganger(self): """ Test doppelganger feature in ATSession using AT+CPIN """ session = ATSession([]) #First prepare its doppelganger cpin_enter_pin = ATCommand("AT+CPIN=${SIM_PIN}", "OK") #Let's define an AT Command (AT+CPIN?) cpin_command = ATCommand("AT+CPIN?", "READY", 10, 0, None, cpin_enter_pin) #Add command to session session.add_command(cpin_command) #Add SIM PIN to session storage sim_pin = 7782 session.set_session_value("SIM_PIN", 7782) #Verify session storage read_pin = session.get_session_value("SIM_PIN") self.assertEqual( sim_pin, read_pin, "SIM PIN should be %d, but is %d" % (sim_pin, read_pin)) #Get command next_command = session.get_next_command() #Verify command self.assertEqual( next_command.command, "AT+CPIN?", "Next command should be AT+CPIN?, but is %s" % next_command.command) #Let's create a fake response serial_response = ["CPIN:SIM PIN", "OK"] response = session.validate_response(serial_response, 100) #Last command should have failed (we expected READY) self.assertTrue(session.last_command_failed) self.assertEqual( response.full_response, serial_response, "Response associated to command is different from the response got from serial device" ) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) #Let's get the next command, that should be the doppelganger next_command = session.get_next_command() self.assertEqual( next_command.command, "AT+CPIN=%d" % sim_pin, "The next command should be the doppelganger, but is %s" % next_command.command) print("Next command is doppelganger with replaced session value: %s" % next_command.command) #Let's invent a response for it, let's say it's OK serial_response = ["OK"] response = session.validate_response(serial_response, 100) #Last command should have succeded self.assertFalse(session.last_command_failed) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.response)) self.assertEqual( response.response, next_command.expected_response, "The response should be OK, but is %s" % response.response)
def __test_exec_and_validate(self): """ Test ATSession just adding a command and evaluating its response """ session = ATSession() simple_command = ATCommand("AT", "OK") session.add_command(simple_command) next_command = session.get_next_command() #Prepare a response for it; in this case we'll simulate a successful response serial_response = ["OK"] response = session.validate_response(serial_response, 100) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) self.assertFalse(session.last_command_failed) self.assertEqual( response.response, "OK", "Command should have OK as response but has %s" % response.response) #Let's simulate a command that will fail simple_command2 = ATCommand( "AT+CGDCONT=1, \"IP\", \"internet.foo.bar\"", "OK") session.add_command(simple_command2) next_command = session.get_next_command() #Prepare a response for it; in this case we'll simulate a bad response serial_response = ["ERROR"] response = session.validate_response(serial_response, 100) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) self.assertTrue(session.last_command_failed) #Regex in response #Let's get the device serial, imagine that all the device has a serial number of 16 hex digits gsn_command = ATCommand("AT+GSN", "^[0-9,A-F]{16}$") session.add_command(gsn_command) next_command = session.get_next_command() #Prepare a response for it; in this case we'll simulate a successful response serial_response = ["AC03C7F3D2EB7832", "OK"] response = session.validate_response(serial_response, 100) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) self.assertFalse(session.last_command_failed) self.assertEqual( response.response, "AC03C7F3D2EB7832", "Command should have AC03C7F3D2EB7832 as response but has %s" % response.response) #Bad cases #Not existing session key command_not_replaceable = ATCommand("AT+CGDATA=${CONTEXT}", "OK") session.add_command(command_not_replaceable) self.assertIsNotNone(session.get_next_command()) serial_response = ["+CME: ERRROR"] response = session.validate_response(serial_response, 100) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) self.assertTrue(session.last_command_failed)
def test_add_command(self): self.atre = ATRuntimeEnvironment(True) #Configure virtual communicator self.atre.configure_virtual_communicator("virtualAdapter", 115200, 10, "\r", read_callback, write_callback, in_waiting) cmd = ATCommand("AT", "OK") self.assertTrue(self.atre.add_command(cmd), "ATRE add_command failed")
def test_at_commands(self): cmd = ATCommand("AT+CSQ", "OK", 5, 1000, ["AT+CSQ=?{rssi},"]) resp = ATResponse("OK", ["+CSQ: 32,2", "", "OK"], cmd, 632) #Test response self.assertEqual(resp.response, "OK", "Expected response 'OK', got %s" % resp.response) self.assertEqual(len(resp.full_response), 3, "Expected full response length: 3, got %d" % len(resp.full_response)) self.assertEqual(resp.execution_time, 632, "Expected execution time 632, got %d" % resp.execution_time) #Add collectable resp.add_collectable("rssi", 32) self.assertEqual(resp.get_collectable("rssi"), 32, "Expected rssi value 32, got %s" % resp.get_collectable("rssi"))
def test_at_commands(self): cmd = ATCommand("AT+CSQ", "OK", 5, 1000, ["AT+CSQ=?{dbm},"], None) self.assertEqual(cmd.command, "AT+CSQ", "Expected command AT+CSQ, got %s" % cmd.command) self.assertEqual( cmd.expected_response, "OK", "Expected response OK, got %s" % cmd.expected_response) self.assertEqual(cmd.timeout, 5, "Expected timeout 5, got %d" % cmd.timeout) self.assertEqual(cmd.delay, 1000, "Expected delay 1000, got %d" % cmd.delay) self.assertEqual( len(cmd.collectables), 1, "Expected collectables length 1, got %d" % len(cmd.collectables)) self.assertEqual( cmd.collectables[0], "AT+CSQ=?{dbm},", "Expected collectable 'AT+CSQ=?{dbm},', got %s" % cmd.collectables[0])
def test_at_commands(self): cmd = ATCommand("AT+CSQ", "OK", 5, 1000, ["AT+CSQ=?{rssi},"]) resp = ATResponse("OK", ["+CSQ: 32,2", "", "OK"], cmd, 632) #Test response self.assertEqual(resp.response, "OK", "Expected response 'OK', got %s" % resp.response) self.assertEqual( len(resp.full_response), 3, "Expected full response length: 3, got %d" % len(resp.full_response)) self.assertEqual( resp.execution_time, 632, "Expected execution time 632, got %d" % resp.execution_time) #Add collectable resp.add_collectable("rssi", 32) self.assertEqual( resp.get_collectable("rssi"), 32, "Expected rssi value 32, got %s" % resp.get_collectable("rssi")) #Setters / Getters resp.response = "OK" self.assertEqual(resp.response, "OK", "Response should be OK but is %s" % resp.response) resp.full_response = ["+CSQ: 99,99", "", "OK"] self.assertEqual( resp.full_response[0], "+CSQ: 99,99", "First element in full response should be +CSQ: 99,99, but is %s" % resp.full_response[0]) self.assertEqual( len(resp.full_response), 3, "Length of full response should be 3 but is %d" % len(resp.full_response)) self.assertEqual( resp.command, cmd, "Associated command is not the command previously set") resp.execution_time = 5000 self.assertEqual( resp.execution_time, 5000, "Execution time should be 5000, but is %d" % resp.execution_time) #Test bad cases resp.execution_time = -1 self.assertEqual( resp.execution_time, 0, "Execution time should be 0, but is %d" % resp.execution_time)
def __test_collectables(self): """ Test collectables feature in ATSession using AT+CSQ """ csq_command = ATCommand("AT+CSQ", "OK", 10, 0, ["AT+CSQ=?{rssi},"]) self.session.add_command(csq_command) #Let's get command next_command = self.session.get_next_command() #Invent a response for it serial_response = ["AT+CSQ=31,1", "OK"] response = self.session.validate_response(serial_response, 50) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) #Last command should have succeded self.assertFalse(self.session.last_command_failed) #Verify if collectable has actually been collected try: rssi = self.session.get_session_value("rssi") self.assertEqual(rssi, 31, "rssi should be 31, but is %s" % rssi) except KeyError as err: raise err print("RSSI from AT+CSQ: %d" % rssi) #Let's try a collectable which searches for 15 digits (for getting the IMEI for example) => ^[0-9]{15}$ imei_command = ATCommand("AT+CGSN", "OK", 10, 0, ["?{IMEI::^[0-9]{15}$}"]) self.session.add_command(imei_command) #Let's get command next_command = self.session.get_next_command() #Invent a response for it serial_response = ["AT+CGSN", "123456789012345", "OK"] response = self.session.validate_response(serial_response, 50) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) #Last command should have succeded self.assertFalse(self.session.last_command_failed) #Verify if collectable has actually been collected try: imei = self.session.get_session_value("IMEI") self.assertEqual( imei, 123456789012345, "rssi should be '123456789012345', but is %s" % imei) except KeyError as err: raise err print("IMEI from AT+CGSN: %d" % imei) #Combining line content with key regex and session keys #Look for both rssi and ber, but use key regex and session values csq_command = ATCommand("AT+CSQ", "OK", 10, 0, [ "AT+CSQ=?{rssi::[0-9]{1,2}},", "AT+CSQ=${rssi},?{ber::[0-9]{1,2}}" ]) self.session.add_command(csq_command) #Let's get command next_command = self.session.get_next_command() #Invent a response for it serial_response = ["AT+CSQ=31,2", "OK"] response = self.session.validate_response(serial_response, 50) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) #Last command should have succeded self.assertFalse(self.session.last_command_failed) #Verify if collectable has actually been collected try: rssi = self.session.get_session_value("rssi") self.assertEqual(rssi, 31, "rssi should be 31, but is %s" % rssi) ber = self.session.get_session_value("ber") self.assertEqual(ber, 2, "ber should be 2, but is %s" % ber) except KeyError as err: raise err print("RSSI from AT+CSQ: %d; ber from AT+CSQ: %d" % (rssi, ber))
def test_session_values(self): """ Test set/get session values """ session = ATSession([]) #Try with not existing key with self.assertRaises(KeyError): session.get_session_value("foobar") session.set_session_value("foo", "bar") self.assertEqual( session.get_session_value("foo"), "bar", "get_session_value failed; expected 'bar' got %s" % session.get_session_value("foo")) """ Test ATSession just adding a command and evaluating its response """ session = ATSession([]) simple_command = ATCommand("AT", "OK") session.add_command(simple_command) next_command = session.get_next_command() #Prepare a response for it; in this case we'll simulate a successful response serial_response = ["OK"] response = session.validate_response(serial_response, 100) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) self.assertFalse(session.last_command_failed) self.assertEqual( response.response, "OK", "Command should have OK as response but has %s" % response.response) #Let's simulate a command that will fail simple_command2 = ATCommand( "AT+CGDCONT=1, \"IP\", \"internet.foo.bar\"", "OK") session.add_command(simple_command2) next_command = session.get_next_command() #Prepare a response for it; in this case we'll simulate a bad response serial_response = ["ERROR"] response = session.validate_response(serial_response, 100) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) self.assertTrue(session.last_command_failed) #Regex in response #Let's get the device serial, imagine that all the device has a serial number of 16 hex digits gsn_command = ATCommand("AT+GSN", "^[0-9,A-F]{16}$") session.add_command(gsn_command) next_command = session.get_next_command() #Prepare a response for it; in this case we'll simulate a successful response serial_response = ["AC03C7F3D2EB7832", "OK"] response = session.validate_response(serial_response, 100) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) self.assertFalse(session.last_command_failed) self.assertEqual( response.response, "AC03C7F3D2EB7832", "Command should have AC03C7F3D2EB7832 as response but has %s" % response.response) #Try with CSQ too #Let's get the device serial, imagine that all the device has a serial number of 16 hex digits csq_command = ATCommand("AT+CSQ", "[0-9]{1,2}") session.add_command(csq_command) next_command = session.get_next_command() #Prepare a response for it; in this case we'll simulate a successful response csq_response = ["+CSQ: 22,99", "", "OK"] response = session.validate_response(csq_response, 100) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) self.assertFalse(session.last_command_failed) self.assertEqual( response.response, "22", "Command should have 22 as response but has %s" % response.response) #Bad cases #Not existing session key command_not_replaceable = ATCommand("AT+CGDATA=${CONTEXT}", "OK") session.add_command(command_not_replaceable) self.assertIsNotNone(session.get_next_command()) serial_response = ["+CME: ERRROR"] response = session.validate_response(serial_response, 100) print("%s (expected %s) has response: %s" % (next_command.command, next_command.expected_response, response.full_response)) self.assertTrue(session.last_command_failed)
def test_session_reset(self): #Try to reset session cmd = ATCommand("AT", "OK") cmds = [cmd] self.atre.init_session(cmds)
def test_at_commands(self): cmd = ATCommand("AT+CSQ", "OK", 5, 1000, ["AT+CSQ=?{dbm},"], None) self.assertEqual(cmd.command, "AT+CSQ", "Expected command AT+CSQ, got %s" % cmd.command) self.assertEqual( cmd.expected_response, "OK", "Expected response OK, got %s" % cmd.expected_response) self.assertEqual(cmd.timeout, 5, "Expected timeout 5, got %d" % cmd.timeout) self.assertEqual(cmd.delay, 1000, "Expected delay 1000, got %d" % cmd.delay) self.assertEqual( len(cmd.collectables), 1, "Expected collectables length 1, got %d" % len(cmd.collectables)) self.assertEqual( cmd.collectables[0], "AT+CSQ=?{dbm},", "Expected collectable 'AT+CSQ=?{dbm},', got %s" % cmd.collectables[0]) #Constructor bad case cmd2 = ATCommand("AT+CSQ", "OK", 5, 1000, "AT+CSQ=?{dbm},", None) self.assertIsNone( cmd2.collectables, "Collectables should be None, since a string has been provided") cmd3 = ATCommand("AT+CSQ", "OK", 5, 1000, "AT+CSQ=?{dbm},", ATCommand("AT+CSQ?")) self.assertIsNone( cmd3.collectables, "Collectables should be None, since a string has been provided") self.assertIsNotNone(cmd3.doppel_ganger) cmd4 = ATCommand("AT+CSQ", "OK", 5, 1000, "AT+CSQ=?{dbm},", "foobar") self.assertIsNone( cmd4.doppel_ganger, "Doppelganger should be None, since a string has been provided") #Test setters getters cmd = ATCommand("AT") #Command cmd.command = "ATE0" self.assertEqual(cmd.command, "ATE0", "Expected ATE0 as command, got %s" % cmd.command) #Expected response cmd.expected_response = "OK" self.assertEqual( cmd.expected_response, "OK", "Expected OK as expected response, got %s" % cmd.expected_response) #Timeout cmd.timeout = 10 self.assertEqual(cmd.timeout, 10, "Expected 10 as timeout, got %d" % cmd.timeout) cmd.timeout = 0 #Test bad case self.assertEqual(cmd.timeout, 1, "Expected 1 as timeout, got %d" % cmd.timeout) #Delay cmd.delay = 5000 self.assertEqual(cmd.delay, 5000, "Expected 5000 as delay, got %d" % cmd.delay) cmd.delay = -1 #Test bad case self.assertEqual(cmd.delay, 0, "Expected 0 as delay, got %d" % cmd.delay) #Collectables cmd.collectables = [ "AT+CSQ=?{rssi::[0-9]{1,2}},", "AT+CSQ=${rssi},?{ber::[0-9]{1,2}}" ] self.assertEqual( len(cmd.collectables), len([ "AT+CSQ=?{rssi::[0-9]{1,2}},", "AT+CSQ=${rssi},?{ber::[0-9]{1,2}}" ]), "Bad collectables") cmd.collectables = "foobar" self.assertIsNone( cmd.collectables, "Collectables should be None, since a string has been provided, but it's not" ) #Doppelganger cmd.doppel_ganger = cmd3 self.assertEqual(cmd.doppel_ganger, cmd3) bad_doppelganger = "foobar" cmd.doppel_ganger = bad_doppelganger self.assertIsNone( cmd.doppel_ganger, "Doppelganger should be None, since a string has been provided, but it's not" ) #Response response_ok = ATResponse("OK", ["+CSQ=32,99", "", "OK"], cmd, 5000) cmd.response = response_ok self.assertEqual(cmd.response, response_ok, "Response should be an ATResponse instance") response_nok = "OK" cmd.response = response_nok self.assertIsNone( cmd.response, "Response should be None, since a string has been passed")