1# Copyright (c) 2014 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 import test_environment
11from autotest_lib.client.cros.cellular.pseudomodem import modem_3gpp
12from autotest_lib.client.cros.cellular.pseudomodem import modem_cdma
13from autotest_lib.client.cros.cellular.pseudomodem import sim
14
15TEST_MODEMS_MODULE_PATH = __file__
16
17# Use valid carrier info since shill looks this up in its database.
18TEST_3GPP_HOME_CARRIER = 'Orange'
19TEST_3GPP_HOME_CARRIER_MCC = '232'
20TEST_3GPP_HOME_CARRIER_MNC = '05'
21TEST_3GPP_ROAMING_CARRIER = 'T-Mobile'
22TEST_3GPP_ROAMING_OPERATOR_CODE = '23203'
23TEST_CDMA_CARRIER = 'Test Network'
24TEST_CDMA_SID = 99998
25
26class TestModemRoaming(modem_3gpp.Modem3gpp):
27    """
28    Test modem that registers with a roaming network.
29
30    """
31    def __init__(self):
32        roaming_networks = [modem_3gpp.Modem3gpp.GsmNetwork(
33                operator_long=TEST_3GPP_ROAMING_CARRIER,
34                operator_short=TEST_3GPP_ROAMING_CARRIER,
35                operator_code=TEST_3GPP_ROAMING_OPERATOR_CODE,
36                status=mm1_constants.
37                        MM_MODEM_3GPP_NETWORK_AVAILABILITY_AVAILABLE,
38                access_technology=mm1_constants.MM_MODEM_ACCESS_TECHNOLOGY_LTE)]
39        modem_3gpp.Modem3gpp.__init__(self, roaming_networks=roaming_networks)
40
41
42    def RegisterWithNetwork(
43        self, operator_id='', return_cb=None, raise_cb=None):
44        """ Overriden from superclass. """
45        logging.info('Force modem to register with roaming network |%s| '
46                     'instead of |%s|',
47                     TEST_3GPP_ROAMING_OPERATOR_CODE, operator_id)
48        modem_3gpp.Modem3gpp.RegisterWithNetwork(
49                self, TEST_3GPP_ROAMING_OPERATOR_CODE, return_cb, raise_cb)
50
51
52class TestSIM(sim.SIM):
53    """
54    Test SIM with a specific carrier name that the tests below are expecting.
55
56    """
57    def __init__(self):
58        carrier = sim.SIM.Carrier()
59        carrier.mcc = TEST_3GPP_HOME_CARRIER_MCC
60        carrier.mnc = TEST_3GPP_HOME_CARRIER_MNC
61        carrier.operator_name = TEST_3GPP_HOME_CARRIER
62        carrier.operator_id = carrier.mcc + carrier.mnc
63        sim.SIM.__init__(self, carrier,
64                         mm1_constants.MM_MODEM_ACCESS_TECHNOLOGY_LTE)
65
66
67class TestCdmaModem(modem_cdma.ModemCdma):
68    """
69    Test modem that simulates a CDMA modem.
70
71    """
72    def __init__(self):
73        network = modem_cdma.ModemCdma.CdmaNetwork(sid=TEST_CDMA_SID)
74        modem_cdma.ModemCdma.__init__(self, home_network=network)
75
76
77class cellular_ServiceName(test.test):
78    """
79    Verifies that shill reports the correct service name depending on the SIM
80    provider information and the network registration status.
81
82    """
83    version = 1
84
85    def _verify_service_name(self, expected_name):
86        """
87        Verifies the service name is as expected.
88
89        @param expected_name: Service name that is expected.
90        @raises error.TestFail() if the service name and expected name does not
91                match.
92
93        """
94        cellular_service = \
95                self.test_env.shill.wait_for_cellular_service_object()
96        service_name = cellular_service.GetProperties()['Name']
97        if service_name != expected_name:
98            raise error.TestFail('Expected service name: |%s|, '
99                                 'actual service name: |%s|' %
100                                 (expected_name, service_name))
101        logging.info('Successfully verified service name |%s|',
102                     expected_name)
103
104
105    def _test_3gpp_no_roaming(self):
106        """
107        Checks the service name when the SIM and the network is the same
108        carrier.
109
110        """
111        logging.info('Testing service name for 3GPP no roaming')
112        self.test_env = test_environment.CellularPseudoMMTestEnvironment(
113                pseudomm_args=({'family': '3GPP',
114                                'test-module': TEST_MODEMS_MODULE_PATH,
115                                'test-sim-class': 'TestSIM'},))
116        with self.test_env:
117            self._verify_service_name(TEST_3GPP_HOME_CARRIER)
118
119
120    def _test_3gpp_roaming(self):
121        """
122        Checks the service name when roaming.
123
124        The service name while roaming should be (per 3GPP TS 31.102 and
125        annex A of 122.101):
126                <home provider> | <serving operator>
127
128        """
129        logging.info('Testing service name for 3GPP roaming')
130        self.test_env = test_environment.CellularPseudoMMTestEnvironment(
131                pseudomm_args=({'family': '3GPP',
132                                'test-module': TEST_MODEMS_MODULE_PATH,
133                                'test-modem-class': 'TestModemRoaming',
134                                'test-sim-class': 'TestSIM'},))
135        with self.test_env:
136            expected_name = (TEST_3GPP_HOME_CARRIER + ' | ' +
137                             TEST_3GPP_ROAMING_CARRIER)
138            self._verify_service_name(expected_name)
139
140
141    def _test_cdma(self):
142        """ Checks the service name for a CDMA network. """
143        logging.info('Testing service name for CDMA')
144        self.test_env = test_environment.CellularPseudoMMTestEnvironment(
145                pseudomm_args=({'family': 'CDMA',
146                                'test-module': TEST_MODEMS_MODULE_PATH,
147                                'test-modem-class': 'TestCdmaModem'},))
148        with self.test_env:
149            self._verify_service_name(TEST_CDMA_CARRIER)
150
151
152    def run_once(self):
153        tests = [self._test_3gpp_no_roaming,
154                 self._test_3gpp_roaming,
155                 self._test_cdma]
156
157        for test in tests:
158            test()
159