• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3#   Copyright 2019 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import re
18import time
19from queue import Empty
20
21from acts import utils
22from acts import base_test
23from acts.libs.proc import job
24from acts import signals
25from acts.test_decorators import test_tracker_info
26from acts_contrib.test_utils.wifi import wifi_test_utils as wutils
27from acts_contrib.test_utils.tel import tel_test_utils as tutils
28from acts_contrib.test_utils.tel import tel_defines
29from acts_contrib.test_utils.tel.anritsu_utils import wait_for_sms_sent_success
30from acts_contrib.test_utils.tel.tel_defines import EventMmsSentSuccess
31from acts_contrib.test_utils.tel.tel_bootloader_utils import fastboot_wipe
32
33# Time it takes for the usb tethering IP to
34# show up in ifconfig and function waiting.
35DEFAULT_SETTLE_TIME = 5
36WifiEnums = wutils.WifiEnums
37USB_CHARGE_MODE = 'svc usb setFunctions'
38USB_TETHERING_MODE = 'svc usb setFunctions rndis'
39USB_MTP_MODE = 'svc usb setFunctions mtp'
40DEVICE_IP_ADDRESS = 'ip address'
41
42
43class UsbTetheringFunctionsTest(base_test.BaseTestClass):
44    """Tests for usb tethering throughput test.
45
46    Test Bed Requirement:
47        * One Android device.
48        * Wi-Fi networks visible to the device.
49        * Sim card with data call.
50    """
51
52    def setup_class(self):
53        self.dut = self.android_devices[0]
54        req_params = ['wifi_network', 'receiver_number', 'ping_count']
55        self.unpack_userparams(req_param_names=req_params)
56
57        self.ssid_map = {}
58        for network in self.wifi_network:
59            SSID = network['SSID'].replace('-', '_')
60            self.ssid_map[SSID] = network
61        self.dut.droid.setMobileDataEnabled()
62        if not tutils.verify_internet_connection(
63                self.dut.log, self.dut, retries=3):
64            tutils.abort_all_tests(self.dut.log, 'Internet connection Failed.')
65
66    def setup_test(self):
67        self.dut.droid.wakeLockAcquireBright()
68        self.dut.droid.wakeUpNow()
69        self.dut.unlock_screen()
70
71    def teardown_test(self):
72        wutils.wifi_toggle_state(self.dut, False)
73        self.dut.droid.wakeLockRelease()
74        self.dut.droid.goToSleepNow()
75        self.dut.droid.setMobileDataEnabled()
76        self.dut.droid.connectivityStopTethering(tel_defines.TETHERING_WIFI)
77        self.dut.stop_services()
78        # Set usb function back to charge mode.
79        self.dut.adb.shell(USB_CHARGE_MODE)
80        self.dut.adb.wait_for_device()
81        self.dut.start_services()
82
83    def teardown_class(self):
84        wutils.reset_wifi(self.dut)
85
86    def on_fail(self, test_name, begin_time):
87        self.dut.take_bug_report(test_name, begin_time)
88        self.dut.cat_adb_log(test_name, begin_time)
89
90    def enable_usb_tethering(self, attempts=3):
91        """Stop SL4A service and enable usb tethering.
92
93        Logic steps are
94        1. Stop SL4A service.
95        2. Enable usb tethering.
96        3. Restart SL4A service.
97        4. Check usb tethering enabled.
98        5. Attempt if fail to enable usb tethering.
99
100        Args:
101            attempts: number of times for enable usb tethering.
102
103        Raises:
104            TestFailure when unable to find rndis string.
105        """
106        for i in range(attempts):
107            self.dut.stop_services()
108            self.dut.adb.shell(USB_TETHERING_MODE, ignore_status=True)
109            self.dut.adb.wait_for_device()
110            self.dut.start_services()
111            if 'rndis' in self.dut.adb.shell(DEVICE_IP_ADDRESS):
112                self.log.info('Usb tethering is enabled.')
113                return
114            else:
115                self.log.info('Enable usb tethering - attempt %d' % (i + 1))
116        raise signals.TestFailure('Unable to enable USB tethering.')
117
118    def connect_to_wifi_network(self, network):
119        """Connection logic for wifi network.
120
121        Args:
122            network: Dictionary with network info.
123        """
124        SSID = network[WifiEnums.SSID_KEY]
125        self.dut.ed.clear_all_events()
126        wutils.start_wifi_connection_scan(self.dut)
127        scan_results = self.dut.droid.wifiGetScanResults()
128        wutils.assert_network_in_list({WifiEnums.SSID_KEY: SSID}, scan_results)
129        wutils.wifi_connect(self.dut, network, num_of_tries=3)
130
131    def enable_wifi_hotspot(self):
132        """Enable wifi hotspot."""
133        sap_config = wutils.create_softap_config()
134        wutils.start_wifi_tethering(self.dut,
135                                    sap_config[wutils.WifiEnums.SSID_KEY],
136                                    sap_config[wutils.WifiEnums.PWD_KEY],
137                                    wutils.WifiEnums.WIFI_CONFIG_APBAND_2G)
138
139    def get_rndis_interface(self):
140        """Check rndis interface after usb tethering enable.
141
142        Returns:
143            Usb tethering interface from Android device.
144
145        Raises:
146            TestFailure when unable to find correct usb tethering interface.
147        """
148        time.sleep(DEFAULT_SETTLE_TIME)
149        check_usb_tethering = job.run('ifconfig').stdout
150        # A regex that stores the tethering interface in group 1.
151        tethered_interface_regex = r'^(enp.*?):.*?broadcast 192.168.42.255'
152        match = re.search(tethered_interface_regex, check_usb_tethering,
153                          re.DOTALL + re.MULTILINE)
154        if match:
155            return match.group(1)
156        else:
157            raise signals.TestFailure(
158                'Unable to find tethering interface. The device may not be tethered.'
159            )
160
161    def can_ping(self, ip, extra_params='', count=10):
162        """Run ping test and check and check ping lost rate.
163
164        Args:
165            ip: ip address for ping.
166            extra_params: params for ping test.
167            count: default ping count.
168
169        Returns:
170            True: If no packet loss.
171            False: Otherwise.
172        """
173        out = job.run(
174            'ping -c {} {} {}'.format(count, extra_params, ip),
175            ignore_status=True).stdout
176        self.log.info(out)
177        return '0%' in out.split(' ')
178
179    def can_ping_through_usb_interface(self):
180        """Run ping test and check result after usb tethering enabled.
181
182        Raises:
183            TestFailure when unable to ping through usb tethering interface.
184        """
185        ip = '8.8.8.8'
186        interface = self.get_rndis_interface()
187        if not self.can_ping(
188                ip, '-I {}'.format(interface), count=self.ping_count):
189            raise signals.TestFailure(
190                'Fail to ping through usb tethering interface.')
191
192    def enable_usb_mtp(self):
193        """Enable usb mtp mode.
194
195        Raises:
196            TestFailure when unable to set mtp mode.
197        """
198        try:
199            self.dut.stop_services()
200            self.dut.adb.shell(USB_MTP_MODE, ignore_status=True)
201            self.dut.adb.wait_for_device()
202            self.dut.start_services()
203        except:
204            raise signals.TestFailure('Device can not enable mtp mode.')
205
206    def check_sms_send_status(self, message='usb_sms_test'):
207        """Send a SMS and check send status.
208
209        Args:
210            message: SMS string.
211
212        Raises:
213            Exception when unable to send SMS.
214        """
215        self.dut.droid.smsSendTextMessage(self.receiver_number, message, False)
216        self.log.info('Waiting for SMS sent event')
217        test_status = wait_for_sms_sent_success(self.log, self.dut)
218        if not test_status:
219            raise Exception('Failed to send SMS')
220
221    def check_mms_send_status(self,
222                              subject='usb_subject',
223                              message='usb_mms_test'):
224        """Send a MMS and check send status.
225
226        Args:
227            subject: MMS subject.
228            message: MMS string.
229
230        Raises:
231            Exception when unable to send MMS.
232        """
233        # Permission require for send MMS.
234        self.dut.adb.shell('su root setenforce 0')
235        self.dut.droid.smsSendMultimediaMessage(self.receiver_number, subject,
236                                                message)
237        self.log.info('Waiting for MMS sent event')
238        test_status = self.wait_for_mms_sent_success()
239        if not test_status:
240            raise Exception('Failed to send MMS')
241
242    def wait_for_mms_sent_success(self, time_to_wait=60):
243        """Check MMS send status.
244
245        Args:
246            time_to_wait: Time out for check MMS.
247
248        Returns:
249            status = MMS send status.
250        """
251        mms_sent_event = EventMmsSentSuccess
252        try:
253            event = self.dut.ed.pop_event(mms_sent_event, time_to_wait)
254            self.log.info(event)
255        except Empty:
256            self.log.error('Timeout: Expected event is not received.')
257            return False
258        return True
259
260    @test_tracker_info(uuid="7c2ae85e-32a2-416e-a65e-c15a15e76f86")
261    def test_usb_tethering_wifi_only(self):
262        """Enable usb tethering with wifi only then executing ping test.
263
264        Steps:
265        1. Stop SL4A services.
266        2. Enable usb tethering.
267        3. Restart SL4A services.
268        4. Mobile data disable and wifi enable.
269        5. Run ping test through usb tethering interface.
270        """
271        self.enable_usb_tethering()
272        self.log.info('Disable mobile data.')
273        self.dut.droid.setMobileDataEnabled(False)
274        self.log.info('Enable wifi.')
275        wutils.wifi_toggle_state(self.dut, True)
276        self.connect_to_wifi_network(
277            self.ssid_map[self.wifi_network[0]['SSID']])
278        self.can_ping_through_usb_interface()
279
280    @test_tracker_info(uuid="8910b07b-0beb-4d9d-b901-c4195b4e0930")
281    def test_usb_tethering_data_only(self):
282        """Enable usb tethering with data only then executing ping test.
283
284        Steps:
285        1. Stop SL4A services.
286        2. Enable usb tethering.
287        3. Restart SL4A services.
288        4. Wifi disable and mobile data enable.
289        5. Run ping test through usb tethering interface.
290        """
291        self.enable_usb_tethering()
292        wutils.wifi_toggle_state(self.dut, False)
293        self.dut.droid.setMobileDataEnabled()
294        time.sleep(DEFAULT_SETTLE_TIME)
295        self.can_ping_through_usb_interface()
296
297    @test_tracker_info(uuid="f971806e-e003-430a-bc80-321f128d31cb")
298    def test_usb_tethering_after_airplane_mode(self):
299        """Enable usb tethering after airplane mode then executing ping test.
300
301        Steps:
302        1. Stop SL4A services.
303        2. Enable usb tethering.
304        3. Restart SL4A services.
305        4. Wifi disable and mobile data enable.
306        5. Enable/disable airplane mode.
307        6. Run ping test through usb tethering interface.
308        """
309        self.enable_usb_tethering()
310        wutils.wifi_toggle_state(self.dut, False)
311        self.log.info('Enable airplane mode.')
312        utils.force_airplane_mode(self.dut, True)
313        self.log.info('Disable airplane mode.')
314        utils.force_airplane_mode(self.dut, False)
315        time.sleep(DEFAULT_SETTLE_TIME)
316        self.can_ping_through_usb_interface()
317
318    @test_tracker_info(uuid="db1c561d-67bd-47d7-b65e-d882f0e2641e")
319    def test_usb_tethering_coexist_wifi_hotspot(self):
320        """Enable usb tethering with hotspot then executing ping test.
321
322        Steps:
323        1. Enable hotspot
324        2. Stop SL4A services.
325        3. Enable usb tethering.
326        4. Restart SL4A services.
327        5. Run ping test through tethering interface during hotspot enable.
328        6. Run ping test through tethering interface during hotspot disable.
329        """
330        self.log.info('Enable wifi hotspot.')
331        self.enable_wifi_hotspot()
332        self.enable_usb_tethering()
333        self.log.info('Ping test with hotspot enable.')
334        self.can_ping_through_usb_interface()
335        self.log.info('Disable wifi hotspot.')
336        self.dut.droid.connectivityStopTethering(tel_defines.TETHERING_WIFI)
337        self.log.info('Ping test with hotspot disable.')
338        self.can_ping_through_usb_interface()
339
340    @test_tracker_info(uuid="7018abdb-049e-41aa-a4f9-e11012369d9b")
341    def test_usb_tethering_after_mtp(self):
342        """Enable usb tethering after mtp enable then executing ping test.
343
344        Steps:
345        1. Stop SL4A services.
346        2. Enable usb tethering.
347        3. Restart SL4A services.
348        4. Enable usb mtp mode.
349        5. Enable usb tethering and mtp will disable.
350        6. Run ping test through usb tethering interface.
351        """
352        self.enable_usb_tethering()
353        self.log.info('Enable usb mtp mode.')
354        self.enable_usb_mtp()
355        # Turn on mtp mode for 5 sec.
356        time.sleep(DEFAULT_SETTLE_TIME)
357        self.enable_usb_tethering()
358        self.log.info('Usb tethering enable, usb mtp mode disabled.')
359        self.can_ping_through_usb_interface()
360
361    @test_tracker_info(uuid="f1ba2cbc-1cb2-4b8a-92eb-9b366c3f4c37")
362    def test_usb_tethering_after_sms_mms(self):
363        """Enable usb tethering after send sms and mms then executing ping test.
364
365        Steps:
366        1. Stop SL4A services.
367        2. Enable usb tethering.
368        3. Restart SL4A services.
369        4. Send a sms and mms.
370        5. Run ping test through usb tethering interface.
371        """
372        self.enable_usb_tethering()
373        self.log.info('Start send SMS and check status.')
374        self.check_sms_send_status()
375        time.sleep(DEFAULT_SETTLE_TIME)
376        self.log.info('Start send MMS and check status.')
377        self.check_mms_send_status()
378        self.can_ping_through_usb_interface()
379
380    @test_tracker_info(uuid="e6586b30-4886-4c3e-8225-633aa9333968")
381    def test_usb_tethering_after_reboot(self):
382        """Enable usb tethering after reboot then executing ping test.
383
384        Steps:
385        1. Reboot device.
386        2. Stop SL4A services.
387        3. Enable usb tethering.
388        4. Restart SL4A services.
389        5. Run ping test through usb tethering interface.
390        """
391        self.enable_usb_tethering()
392        self.log.info('Reboot device.')
393        self.dut.reboot()
394        self.dut.droid.setMobileDataEnabled()
395        self.log.info('Start usb tethering and ping test.')
396        self.enable_usb_tethering()
397        self.can_ping_through_usb_interface()
398
399    @test_tracker_info(uuid="40093ab8-6db4-4af4-97ae-9467bf33bf23")
400    def test_usb_tethering_after_wipe(self):
401        """Enable usb tethering after wipe.
402
403        Steps:
404        1. Enable usb tethering.
405        2. Wipe device.
406        3. Wake up device and unlock screen.
407        4. Enable usb tethering.
408        5. Run ping test through usb tethering interface.
409        """
410        self.enable_usb_tethering()
411        fastboot_wipe(self.dut)
412        time.sleep(DEFAULT_SETTLE_TIME)
413        # Skip setup wizard after wipe.
414        self.dut.adb.shell(
415            'am start -a com.android.setupwizard.EXIT', ignore_status=True)
416        self.dut.droid.setMobileDataEnabled()
417        self.dut.droid.wakeUpNow()
418        self.dut.unlock_screen()
419        self.enable_usb_tethering()
420        self.can_ping_through_usb_interface()