xref: /aosp_15_r20/external/autotest/client/site_tests/cellular_Smoke/cellular_Smoke.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright (c) 2011 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
6from __future__ import absolute_import
7from __future__ import division
8from __future__ import print_function
9
10import logging
11import socket
12import time
13
14from six.moves import range
15
16import six.moves.urllib.parse
17
18from autotest_lib.client.bin import test
19from autotest_lib.client.common_lib import error
20from autotest_lib.client.cros import network
21from autotest_lib.client.cros.networking import shill_context
22from autotest_lib.client.cros.networking import shill_proxy
23
24
25# Default timeouts in seconds
26CONNECT_TIMEOUT = 120
27DISCONNECT_TIMEOUT = 60
28
29PORTAL_URL_PATTERN = ('https://quickaccess.verizonwireless.com/'
30                      'images_b2c/shared/nav/vz_logo_quickaccess.jpg?foo=%d')
31
32
33class cellular_Smoke(test.test):
34    """
35    Tests that 3G modem can connect to the network
36
37    The test attempts to connect using the 3G network. The test then
38    disconnects from the network, and verifies that the modem still
39    responds to modem manager DBUS API calls.  It repeats the
40    connect/disconnect sequence several times.
41
42    """
43    version = 1
44
45
46    def run_once_internal(self):
47        """Executes the test."""
48        old_modem_info = self.test_env.modem.GetModemProperties()
49
50        for i in range(self.connect_count):
51            device = self.test_env.shill.find_cellular_device_object()
52            if not device:
53                raise error.TestError('No cellular device found.')
54
55            service = self.test_env.shill.wait_for_cellular_service_object()
56            if not service:
57                raise error.TestError('No cellular service found.')
58
59            logging.info('Connecting to service %s', service.object_path)
60            self.test_env.shill.connect_service_synchronous(
61                    service, CONNECT_TIMEOUT)
62
63            state = self.test_env.shill.get_dbus_property(
64                    service, shill_proxy.ShillProxy.SERVICE_PROPERTY_STATE)
65            logging.info('Service state = %s', state)
66
67            if state == 'portal':
68                url_pattern = PORTAL_URL_PATTERN
69                bytes_to_fetch = 4476
70            elif state == 'online':
71                url_pattern = network.FETCH_URL_PATTERN_FOR_TEST
72                bytes_to_fetch = 64 * 1024
73            else:
74                raise error.TestError('Cellular state not online: %s' % state)
75
76            interface = self.test_env.shill.get_dbus_property(
77                    device, shill_proxy.ShillProxy.DEVICE_PROPERTY_INTERFACE)
78            logging.info('Expected interface for %s: %s',
79                         service.object_path, interface)
80            network.CheckThatInterfaceCanAccessDestination(
81                    six.moves.urllib.parse.urlparse(url_pattern).hostname, interface,
82                    [socket.AF_INET, socket.AF_INET6])
83
84            try:
85                fetch_time = network.FetchUrl(url_pattern, bytes_to_fetch,
86                                              self.fetch_timeout)
87            except:
88                raise error.TestError('FetchUrl timed out after %d' %
89                                      self.fetch_timeout)
90
91            self.write_perf_keyval({
92                'seconds_3G_fetch_time': fetch_time,
93                'bytes_3G_bytes_received': bytes_to_fetch,
94                'bits_second_3G_speed': 8 * bytes_to_fetch / fetch_time
95            })
96
97            self.test_env.shill.disconnect_service_synchronous(
98                    service, DISCONNECT_TIMEOUT)
99
100            # Verify that we can still get information about the modem
101            logging.info('Old modem info: %s', ', '.join(old_modem_info))
102            new_modem_info = self.test_env.modem.GetModemProperties()
103            if len(new_modem_info) != len(old_modem_info):
104                logging.info('New modem info: %s', ', '.join(new_modem_info))
105                raise error.TestFail('Test shutdown: '
106                                     'failed to leave modem in working state.')
107
108            if self.sleep_kludge:
109                logging.info('Sleeping for %.1f seconds', self.sleep_kludge)
110                time.sleep(self.sleep_kludge)
111
112
113    def run_once(self,
114                 test_env,
115                 connect_count=5,
116                 sleep_kludge=5,
117                 fetch_timeout=30):
118        """ Runs the test once """
119        with test_env, shill_context.ServiceAutoConnectContext(
120                test_env.shill.wait_for_cellular_service_object, False):
121            self.test_env = test_env
122            self.connect_count = connect_count
123            self.sleep_kludge = sleep_kludge
124            self.fetch_timeout = fetch_timeout
125
126            self.run_once_internal()
127