1# Lint as: python2, python3 2# Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6import logging 7 8from autotest_lib.client.bin import test 9from autotest_lib.client.bin import utils 10from autotest_lib.client.common_lib import error 11from autotest_lib.client.cros.cellular import mm1_constants 12from autotest_lib.client.cros.cellular.pseudomodem import sim 13 14# Disable pylint warning W1201 because we pass the string to the log as well 15# as use it to raise an error, see _ValidateIdentifier(). 16# W1201: Specify string format arguments as logging function parameters 17# pylint: disable=W1201 18 19SERVICE_REGISTRATION_TIMEOUT = 60 20class TestSIM(sim.SIM): 21 """ sim.SIM subclass to set the network as needed. """ 22 def __init__(self): 23 sim.SIM.__init__(self, 24 sim.SIM.Carrier('att'), 25 mm1_constants.MM_MODEM_ACCESS_TECHNOLOGY_GSM) 26 27 28class cellular_Identifiers(test.test): 29 """This test verifies that a modem returns valid identifiers.""" 30 version = 1 31 32 def _ValidateIdentifier(self, label, device_value, modem_value, 33 min_length, max_length): 34 """Validates a specific identifier by matching the values reported by 35 Shill and ModemManager as well as verifying its length.""" 36 if device_value != modem_value: 37 message = ('Shill value "%s" for "%s" does not match MM value "%s"' 38 % (device_value, label, modem_value)) 39 logging.error(message) 40 raise error.TestFail(message) 41 if (len(device_value) < min_length or len(device_value) > max_length): 42 message = 'Invalid %s value "%s"' % (label, device_value) 43 logging.error(message) 44 raise error.TestFail(message) 45 logging.info(' %s = %s' % (label, device_value)) 46 47 def _ValidateGsmIdentifiers(self, device_props, service_props, modem_props): 48 """Validates GSM identifiers.""" 49 self._ValidateIdentifier('IMEI', 50 device_props['Cellular.IMEI'], 51 modem_props['Imei'], 52 14, 16) 53 self._ValidateIdentifier('IMSI', 54 device_props['Cellular.IMSI'], 55 modem_props['Imsi'], 56 0, 15) 57 if self.is_modemmanager: 58 operator_identifier = modem_props.get('OperatorIdentifier', '') 59 if operator_identifier != '': 60 # If modemmanager fails to expose this property, the 61 # HomeProvider information is obtained offline from 62 # mobile_provider_database. We don't check that case here. 63 self._ValidateIdentifier( 64 'HomeProvider.code', 65 device_props['Cellular.HomeProvider']['code'], 66 operator_identifier, 67 5, 6) 68 self._ValidateIdentifier('ICCID', 69 device_props['Cellular.ICCID'], 70 modem_props['SimIdentifier'], 71 0, 20) 72 73 self._ValidateIdentifier( 74 'ServingOperator.code', 75 service_props['Cellular.ServingOperator']['code'], 76 modem_props['OperatorCode'], 77 5, 6) 78 79 80 def _ValidateCdmaIdentifiers(self, device_props, modem_props): 81 """Validates CDMA identifiers.""" 82 self._ValidateIdentifier('ESN', 83 device_props['Cellular.ESN'], 84 modem_props['Esn'], 85 8, 8) 86 self._ValidateIdentifier('MEID', 87 device_props['Cellular.MEID'], 88 modem_props['Meid'], 89 14, 14) 90 91 def run_once(self, test_env): 92 """Called by autotest to run this test.""" 93 with test_env: 94 device = test_env.shill.find_cellular_device_object() 95 device_props = device.GetProperties() 96 service = test_env.shill.find_cellular_service_object() 97 service_props = service.GetProperties() 98 self.is_modemmanager = 'freedesktop' in device_props['DBus.Service'] 99 100 utils.poll_for_condition( 101 test_env.modem.ModemIsRegistered, 102 exception = error.TestFail( 103 'Modem failed to register with the network'), 104 timeout = SERVICE_REGISTRATION_TIMEOUT) 105 modem_props = test_env.modem.GetModemProperties() 106 107 logging.debug('shill service properties: %s', service_props) 108 logging.debug('shill device_properties: %s', device_props) 109 logging.debug('mm properties: %s', modem_props) 110 111 technology_family = device_props['Cellular.Family'] 112 if technology_family == 'GSM': 113 logging.info('Validating GSM identifiers') 114 self._ValidateGsmIdentifiers(device_props, service_props, 115 modem_props) 116 elif technology_family == 'CDMA': 117 logging.info('Validating CDMA identifiers') 118 self._ValidateCdmaIdentifiers(device_props, modem_props) 119 else: 120 raise error.TestFail('Invalid technology family %s' % 121 technology_family) 122