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