• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3.4
2#
3#   Copyright 2016 - Google
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"""
17    Test Script for Telephony Pre Check In Sanity
18"""
19
20import time
21from acts.base_test import BaseTestClass
22from queue import Empty
23from acts.test_utils.tel.tel_subscription_utils import \
24    get_subid_from_slot_index
25from acts.test_utils.tel.tel_subscription_utils import set_subid_for_data
26from acts.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
27from acts.test_utils.tel.tel_defines import DIRECTION_MOBILE_ORIGINATED
28from acts.test_utils.tel.tel_defines import DIRECTION_MOBILE_TERMINATED
29from acts.test_utils.tel.tel_defines import DATA_STATE_CONNECTED
30from acts.test_utils.tel.tel_defines import GEN_2G
31from acts.test_utils.tel.tel_defines import GEN_3G
32from acts.test_utils.tel.tel_defines import GEN_4G
33from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
34from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_VOICE
35from acts.test_utils.tel.tel_defines import RAT_2G
36from acts.test_utils.tel.tel_defines import RAT_3G
37from acts.test_utils.tel.tel_defines import RAT_4G
38from acts.test_utils.tel.tel_defines import RAT_FAMILY_LTE
39from acts.test_utils.tel.tel_defines import SIM1_SLOT_INDEX
40from acts.test_utils.tel.tel_defines import SIM2_SLOT_INDEX
41from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION
42from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_TETHERING_ENTITLEMENT_CHECK
43from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_WIFI_CONNECTION
44from acts.test_utils.tel.tel_defines import TETHERING_MODE_WIFI
45from acts.test_utils.tel.tel_defines import WAIT_TIME_AFTER_REBOOT
46from acts.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING
47from acts.test_utils.tel.tel_defines import WAIT_TIME_BETWEEN_REG_AND_CALL
48from acts.test_utils.tel.tel_defines import \
49    WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING
50from acts.test_utils.tel.tel_defines import WAIT_TIME_TETHERING_AFTER_REBOOT
51from acts.test_utils.tel.tel_data_utils import airplane_mode_test
52from acts.test_utils.tel.tel_data_utils import change_data_sim_and_verify_data
53from acts.test_utils.tel.tel_data_utils import data_connectivity_single_bearer
54from acts.test_utils.tel.tel_data_utils import ensure_wifi_connected
55from acts.test_utils.tel.tel_data_utils import tethering_check_internet_connection
56from acts.test_utils.tel.tel_data_utils import wifi_cell_switching
57from acts.test_utils.tel.tel_data_utils import wifi_tethering_cleanup
58from acts.test_utils.tel.tel_data_utils import wifi_tethering_setup_teardown
59from acts.test_utils.tel.tel_test_utils import WifiUtils
60from acts.test_utils.tel.tel_test_utils import call_setup_teardown
61from acts.test_utils.tel.tel_test_utils import ensure_phones_default_state
62from acts.test_utils.tel.tel_test_utils import ensure_phones_idle
63from acts.test_utils.tel.tel_test_utils import ensure_network_generation
64from acts.test_utils.tel.tel_test_utils import \
65    ensure_network_generation_for_subscription
66from acts.test_utils.tel.tel_test_utils import get_slot_index_from_subid
67from acts.test_utils.tel.tel_test_utils import get_network_rat_for_subscription
68from acts.test_utils.tel.tel_test_utils import hangup_call
69from acts.test_utils.tel.tel_test_utils import multithread_func
70from acts.test_utils.tel.tel_test_utils import set_call_state_listen_level
71from acts.test_utils.tel.tel_test_utils import setup_sim
72from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode
73from acts.test_utils.tel.tel_test_utils import toggle_volte
74from acts.test_utils.tel.tel_test_utils import verify_http_connection
75from acts.test_utils.tel.tel_test_utils import verify_incall_state
76from acts.test_utils.tel.tel_test_utils import wait_for_cell_data_connection
77from acts.test_utils.tel.tel_test_utils import wait_for_network_rat
78from acts.test_utils.tel.tel_test_utils import \
79    wait_for_voice_attach_for_subscription
80from acts.test_utils.tel.tel_test_utils import \
81    wait_for_data_attach_for_subscription
82from acts.test_utils.tel.tel_test_utils import wait_for_wifi_data_connection
83from acts.test_utils.tel.tel_voice_utils import is_phone_in_call_3g
84from acts.test_utils.tel.tel_voice_utils import is_phone_in_call_csfb
85from acts.test_utils.tel.tel_voice_utils import is_phone_in_call_volte
86from acts.test_utils.tel.tel_voice_utils import phone_setup_voice_3g
87from acts.test_utils.tel.tel_voice_utils import phone_setup_csfb
88from acts.test_utils.tel.tel_voice_utils import phone_setup_voice_general
89from acts.test_utils.tel.tel_voice_utils import phone_setup_volte
90from acts.test_utils.wifi.wifi_test_utils import WifiEnums
91from acts.utils import disable_doze
92from acts.utils import enable_doze
93from acts.utils import rand_ascii_str
94
95
96class TelLiveDataTest(TelephonyBaseTest):
97    def __init__(self, controllers):
98        TelephonyBaseTest.__init__(self, controllers)
99        self.tests = ("test_airplane_mode",
100                      "test_4g",
101                      "test_3g",
102                      "test_2g",
103                      "test_lte_wifi_switching",
104                      "test_wcdma_wifi_switching",
105                      "test_gsm_wifi_switching",
106                      "test_wifi_connect_disconnect",
107                      "test_lte_multi_bearer",
108                      "test_wcdma_multi_bearer",
109                      "test_2g_wifi_not_associated",
110                      "test_3g_wifi_not_associated",
111                      "test_4g_wifi_not_associated",
112
113                      # WiFi Tethering tests
114                      "test_tethering_entitlement_check",
115                      "test_tethering_2g_to_2gwifi",
116                      "test_tethering_2g_to_5gwifi",
117                      "test_tethering_3g_to_5gwifi",
118                      "test_tethering_3g_to_2gwifi",
119                      "test_tethering_4g_to_5gwifi",
120                      "test_tethering_4g_to_2gwifi",
121                      "test_tethering_4g_to_2gwifi_2clients",
122                      "test_toggle_apm_during_active_wifi_tethering",
123                      "test_toggle_data_during_active_wifi_tethering",
124                      "test_disable_wifi_tethering_resume_connected_wifi",
125                      "test_tethering_wifi_ssid_quotes",
126                      "test_tethering_wifi_no_password",
127                      "test_tethering_wifi_password_escaping_characters",
128                      "test_tethering_wifi_ssid",
129                      "test_tethering_wifi_password",
130                      "test_tethering_wifi_volte_call",
131                      "test_tethering_wifi_csfb_call",
132                      "test_tethering_wifi_3g_call",
133                      "test_tethering_wifi_reboot",
134                      "test_connect_wifi_start_tethering_wifi_reboot",
135                      "test_connect_wifi_reboot_start_tethering_wifi",
136                      "test_tethering_wifi_screen_off_enable_doze_mode",
137
138                      # stress tests
139                      "test_4g_stress",
140                      "test_3g_stress",
141                      "test_lte_multi_bearer_stress",
142                      "test_wcdma_multi_bearer_stress",
143                      "test_tethering_4g_to_2gwifi_stress",)
144        self.stress_test_number = int(self.user_params["stress_test_number"])
145        self.wifi_network_ssid = self.user_params["wifi_network_ssid"]
146
147        try:
148            self.wifi_network_pass = self.user_params["wifi_network_pass"]
149        except KeyError:
150            self.wifi_network_pass = None
151
152    @TelephonyBaseTest.tel_test_wrap
153    def test_airplane_mode(self):
154        """ Test airplane mode basic on Phone and Live SIM.
155
156        Ensure phone attach, data on, WiFi off and verify Internet.
157        Turn on airplane mode to make sure detach.
158        Turn off airplane mode to make sure attach.
159        Verify Internet connection.
160
161        Returns:
162            True if pass; False if fail.
163        """
164        return airplane_mode_test(self.log, self.android_devices[0])
165
166    @TelephonyBaseTest.tel_test_wrap
167    def test_lte_wifi_switching(self):
168        """Test data connection network switching when phone camped on LTE.
169
170        Ensure phone is camped on LTE
171        Ensure WiFi can connect to live network,
172        Airplane mode is off, data connection is on, WiFi is on.
173        Turn off WiFi, verify data is on cell and browse to google.com is OK.
174        Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
175        Turn off WiFi, verify data is on cell and browse to google.com is OK.
176
177        Returns:
178            True if pass.
179        """
180        return wifi_cell_switching(self.log, self.android_devices[0],
181                                   self.wifi_network_ssid,
182                                   self.wifi_network_pass, GEN_4G)
183
184    @TelephonyBaseTest.tel_test_wrap
185    def test_wcdma_wifi_switching(self):
186        """Test data connection network switching when phone camped on WCDMA.
187
188        Ensure phone is camped on WCDMA
189        Ensure WiFi can connect to live network,
190        Airplane mode is off, data connection is on, WiFi is on.
191        Turn off WiFi, verify data is on cell and browse to google.com is OK.
192        Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
193        Turn off WiFi, verify data is on cell and browse to google.com is OK.
194
195        Returns:
196            True if pass.
197        """
198        return wifi_cell_switching(self.log, self.android_devices[0],
199                                   self.wifi_network_ssid,
200                                   self.wifi_network_pass, GEN_3G)
201
202    @TelephonyBaseTest.tel_test_wrap
203    def test_gsm_wifi_switching(self):
204        """Test data connection network switching when phone camped on GSM.
205
206        Ensure phone is camped on GSM
207        Ensure WiFi can connect to live network,,
208        Airplane mode is off, data connection is on, WiFi is on.
209        Turn off WiFi, verify data is on cell and browse to google.com is OK.
210        Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
211        Turn off WiFi, verify data is on cell and browse to google.com is OK.
212
213        Returns:
214            True if pass.
215        """
216        return wifi_cell_switching(self.log, self.android_devices[0],
217                                   self.wifi_network_ssid,
218                                   self.wifi_network_pass, GEN_2G)
219
220    @TelephonyBaseTest.tel_test_wrap
221    def test_lte_multi_bearer(self):
222        """Test LTE data connection before call and in call. (VoLTE call)
223
224
225        Turn off airplane mode, disable WiFi, enable Cellular Data.
226        Make sure phone in LTE, verify Internet.
227        Initiate a voice call. verify Internet.
228        Disable Cellular Data, verify Internet is inaccessible.
229        Enable Cellular Data, verify Internet.
230        Hangup Voice Call, verify Internet.
231
232        Returns:
233            True if success.
234            False if failed.
235        """
236        if not phone_setup_volte(self.log, self.android_devices[0]):
237            self.log.error("Failed to setup VoLTE")
238            return False
239        return self._test_data_connectivity_multi_bearer(GEN_4G)
240
241    @TelephonyBaseTest.tel_test_wrap
242    def test_wcdma_multi_bearer(self):
243        """Test WCDMA data connection before call and in call.
244
245        Turn off airplane mode, disable WiFi, enable Cellular Data.
246        Make sure phone in WCDMA, verify Internet.
247        Initiate a voice call. verify Internet.
248        Disable Cellular Data, verify Internet is inaccessible.
249        Enable Cellular Data, verify Internet.
250        Hangup Voice Call, verify Internet.
251
252        Returns:
253            True if success.
254            False if failed.
255        """
256
257        return self._test_data_connectivity_multi_bearer(GEN_3G)
258
259    @TelephonyBaseTest.tel_test_wrap
260    def test_gsm_multi_bearer_mo(self):
261        """Test gsm data connection before call and in call.
262
263        Turn off airplane mode, disable WiFi, enable Cellular Data.
264        Make sure phone in GSM, verify Internet.
265        Initiate a MO voice call. Verify there is no Internet during call.
266        Hangup Voice Call, verify Internet.
267
268        Returns:
269            True if success.
270            False if failed.
271        """
272
273        return self._test_data_connectivity_multi_bearer(GEN_2G,
274            False, DIRECTION_MOBILE_ORIGINATED)
275
276    @TelephonyBaseTest.tel_test_wrap
277    def test_gsm_multi_bearer_mt(self):
278        """Test gsm data connection before call and in call.
279
280        Turn off airplane mode, disable WiFi, enable Cellular Data.
281        Make sure phone in GSM, verify Internet.
282        Initiate a MT voice call. Verify there is no Internet during call.
283        Hangup Voice Call, verify Internet.
284
285        Returns:
286            True if success.
287            False if failed.
288        """
289
290        return self._test_data_connectivity_multi_bearer(GEN_2G,
291            False, DIRECTION_MOBILE_TERMINATED)
292
293    @TelephonyBaseTest.tel_test_wrap
294    def test_wcdma_multi_bearer_stress(self):
295        """Stress Test WCDMA data connection before call and in call.
296
297        This is a stress test for "test_wcdma_multi_bearer".
298        Default MINIMUM_SUCCESS_RATE is set to 95%.
299
300        Returns:
301            True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
302            False otherwise.
303        """
304        ads = self.android_devices
305        MINIMUM_SUCCESS_RATE = .95
306        success_count = 0
307        fail_count = 0
308
309        for i in range(1, self.stress_test_number + 1):
310
311            ensure_phones_default_state(
312                self.log, [self.android_devices[0], self.android_devices[1]])
313
314            if self.test_wcdma_multi_bearer():
315                success_count += 1
316                result_str = "Succeeded"
317            else:
318                fail_count += 1
319                result_str = "Failed"
320            self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
321                i, result_str, success_count, self.stress_test_number))
322
323        self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
324            success_count, fail_count, str(100 * success_count / (
325                success_count + fail_count))))
326        if success_count / (
327                success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
328            return True
329        else:
330            return False
331
332    @TelephonyBaseTest.tel_test_wrap
333    def test_lte_multi_bearer_stress(self):
334        """Stress Test LTE data connection before call and in call. (VoLTE call)
335
336        This is a stress test for "test_lte_multi_bearer".
337        Default MINIMUM_SUCCESS_RATE is set to 95%.
338
339        Returns:
340            True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
341            False otherwise.
342        """
343        ads = self.android_devices
344        MINIMUM_SUCCESS_RATE = .95
345        success_count = 0
346        fail_count = 0
347
348        for i in range(1, self.stress_test_number + 1):
349
350            ensure_phones_default_state(
351                self.log, [self.android_devices[0], self.android_devices[1]])
352
353            if self.test_lte_multi_bearer():
354                success_count += 1
355                result_str = "Succeeded"
356            else:
357                fail_count += 1
358                result_str = "Failed"
359            self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
360                i, result_str, success_count, self.stress_test_number))
361
362        self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
363            success_count, fail_count, str(100 * success_count / (
364                success_count + fail_count))))
365        if success_count / (
366                success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
367            return True
368        else:
369            return False
370
371    def _test_data_connectivity_multi_bearer(self, nw_gen,
372        simultaneous_voice_data=True,
373        call_direction=DIRECTION_MOBILE_ORIGINATED):
374        """Test data connection before call and in call.
375
376        Turn off airplane mode, disable WiFi, enable Cellular Data.
377        Make sure phone in <nw_gen>, verify Internet.
378        Initiate a voice call.
379        if simultaneous_voice_data is True, then:
380            Verify Internet.
381            Disable Cellular Data, verify Internet is inaccessible.
382            Enable Cellular Data, verify Internet.
383        if simultaneous_voice_data is False, then:
384            Verify Internet is not available during voice call.
385        Hangup Voice Call, verify Internet.
386
387        Returns:
388            True if success.
389            False if failed.
390        """
391
392        class _LocalException(Exception):
393            pass
394
395        ad_list = [self.android_devices[0], self.android_devices[1]]
396        ensure_phones_idle(self.log, ad_list)
397
398        if not ensure_network_generation_for_subscription(self.log,
399            self.android_devices[0],
400            self.android_devices[0].droid.subscriptionGetDefaultDataSubId(),
401            nw_gen, MAX_WAIT_TIME_NW_SELECTION,
402            NETWORK_SERVICE_DATA):
403            self.log.error("Device failed to reselect in {}s.".format(
404                MAX_WAIT_TIME_NW_SELECTION))
405            return False
406
407        if not wait_for_voice_attach_for_subscription(
408                self.log, self.android_devices[0], self.android_devices[
409                    0].droid.subscriptionGetDefaultVoiceSubId(),
410                MAX_WAIT_TIME_NW_SELECTION):
411            return False
412
413        self.log.info("Step1 WiFi is Off, Data is on Cell.")
414        toggle_airplane_mode(self.log, self.android_devices[0], False)
415        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], False)
416        self.android_devices[0].droid.telephonyToggleDataConnection(True)
417        if (not wait_for_cell_data_connection(self.log,
418                                              self.android_devices[0], True) or
419                not verify_http_connection(self.log, self.android_devices[0])):
420            self.log.error("Data not available on cell")
421            return False
422
423        try:
424            self.log.info("Step2 Initiate call and accept.")
425            if call_direction == DIRECTION_MOBILE_ORIGINATED:
426                ad_caller = self.android_devices[0]
427                ad_callee = self.android_devices[1]
428            else:
429                ad_caller = self.android_devices[1]
430                ad_callee = self.android_devices[0]
431            if not call_setup_teardown(self.log, ad_caller, ad_callee, None,
432                                       None, None):
433                self.log.error("Failed to Establish {} Voice Call".format(
434                    call_direction))
435                return False
436            if simultaneous_voice_data:
437                self.log.info("Step3 Verify internet.")
438                time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
439                if not verify_http_connection(self.log, self.android_devices[0]):
440                    raise _LocalException("Internet Inaccessible when Enabled")
441
442                self.log.info("Step4 Turn off data and verify not connected.")
443                self.android_devices[0].droid.telephonyToggleDataConnection(False)
444                if not wait_for_cell_data_connection(
445                        self.log, self.android_devices[0], False):
446                    raise _LocalException("Failed to Disable Cellular Data")
447
448                if verify_http_connection(self.log, self.android_devices[0]):
449                    raise _LocalException("Internet Accessible when Disabled")
450
451                self.log.info("Step5 Re-enable data.")
452                self.android_devices[0].droid.telephonyToggleDataConnection(True)
453                if not wait_for_cell_data_connection(
454                        self.log, self.android_devices[0], True):
455                    raise _LocalException("Failed to Re-Enable Cellular Data")
456                if not verify_http_connection(self.log, self.android_devices[0]):
457                    raise _LocalException("Internet Inaccessible when Enabled")
458            else:
459                self.log.info("Step3 Verify no Internet and skip step 4-5.")
460                if verify_http_connection(self.log, self.android_devices[0],
461                    retry=0):
462                    raise _LocalException("Internet Accessible.")
463
464            self.log.info("Step6 Verify phones still in call and Hang up.")
465            if not verify_incall_state(
466                    self.log,
467                [self.android_devices[0], self.android_devices[1]], True):
468                return False
469            if not hangup_call(self.log, self.android_devices[0]):
470                self.log.error("Failed to hang up call")
471                return False
472            if not verify_http_connection(self.log, self.android_devices[0]):
473                raise _LocalException("Internet Inaccessible when Enabled")
474
475        except _LocalException as e:
476            self.log.error(str(e))
477            try:
478                hangup_call(self.log, self.android_devices[0])
479                self.android_devices[0].droid.telephonyToggleDataConnection(
480                    True)
481            except Exception:
482                pass
483            return False
484
485        return True
486
487    @TelephonyBaseTest.tel_test_wrap
488    def test_2g(self):
489        """Test data connection in 2G.
490
491        Turn off airplane mode, disable WiFi, enable Cellular Data.
492        Ensure phone data generation is 2G.
493        Verify Internet.
494        Disable Cellular Data, verify Internet is inaccessible.
495        Enable Cellular Data, verify Internet.
496
497        Returns:
498            True if success.
499            False if failed.
500        """
501        WifiUtils.wifi_reset(self.log, self.android_devices[0])
502        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], False)
503        return data_connectivity_single_bearer(self.log,
504                                               self.android_devices[0], RAT_2G)
505
506    @TelephonyBaseTest.tel_test_wrap
507    def test_2g_wifi_not_associated(self):
508        """Test data connection in 2G.
509
510        Turn off airplane mode, enable WiFi (but not connected), enable Cellular Data.
511        Ensure phone data generation is 2G.
512        Verify Internet.
513        Disable Cellular Data, verify Internet is inaccessible.
514        Enable Cellular Data, verify Internet.
515
516        Returns:
517            True if success.
518            False if failed.
519        """
520        WifiUtils.wifi_reset(self.log, self.android_devices[0])
521        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], False)
522        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], True)
523        return data_connectivity_single_bearer(self.log,
524                                               self.android_devices[0], RAT_2G)
525
526    @TelephonyBaseTest.tel_test_wrap
527    def test_3g(self):
528        """Test data connection in 3G.
529
530        Turn off airplane mode, disable WiFi, enable Cellular Data.
531        Ensure phone data generation is 3G.
532        Verify Internet.
533        Disable Cellular Data, verify Internet is inaccessible.
534        Enable Cellular Data, verify Internet.
535
536        Returns:
537            True if success.
538            False if failed.
539        """
540        WifiUtils.wifi_reset(self.log, self.android_devices[0])
541        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], False)
542        return data_connectivity_single_bearer(self.log,
543                                               self.android_devices[0], RAT_3G)
544
545    @TelephonyBaseTest.tel_test_wrap
546    def test_3g_wifi_not_associated(self):
547        """Test data connection in 3G.
548
549        Turn off airplane mode, enable WiFi (but not connected), enable Cellular Data.
550        Ensure phone data generation is 3G.
551        Verify Internet.
552        Disable Cellular Data, verify Internet is inaccessible.
553        Enable Cellular Data, verify Internet.
554
555        Returns:
556            True if success.
557            False if failed.
558        """
559        WifiUtils.wifi_reset(self.log, self.android_devices[0])
560        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], False)
561        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], True)
562        return data_connectivity_single_bearer(self.log,
563                                               self.android_devices[0], RAT_3G)
564
565    @TelephonyBaseTest.tel_test_wrap
566    def test_4g(self):
567        """Test data connection in 4g.
568
569        Turn off airplane mode, disable WiFi, enable Cellular Data.
570        Ensure phone data generation is 4g.
571        Verify Internet.
572        Disable Cellular Data, verify Internet is inaccessible.
573        Enable Cellular Data, verify Internet.
574
575        Returns:
576            True if success.
577            False if failed.
578        """
579        WifiUtils.wifi_reset(self.log, self.android_devices[0])
580        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], False)
581        return data_connectivity_single_bearer(self.log,
582                                               self.android_devices[0], RAT_4G)
583
584    @TelephonyBaseTest.tel_test_wrap
585    def test_4g_wifi_not_associated(self):
586        """Test data connection in 4g.
587
588        Turn off airplane mode, enable WiFi (but not connected), enable Cellular Data.
589        Ensure phone data generation is 4g.
590        Verify Internet.
591        Disable Cellular Data, verify Internet is inaccessible.
592        Enable Cellular Data, verify Internet.
593
594        Returns:
595            True if success.
596            False if failed.
597        """
598        WifiUtils.wifi_reset(self.log, self.android_devices[0])
599        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], False)
600        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], True)
601        return data_connectivity_single_bearer(self.log,
602                                               self.android_devices[0], RAT_4G)
603
604    @TelephonyBaseTest.tel_test_wrap
605    def test_3g_stress(self):
606        """Stress Test data connection in 3G.
607
608        This is a stress test for "test_3g".
609        Default MINIMUM_SUCCESS_RATE is set to 95%.
610
611        Returns:
612            True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
613            False otherwise.
614        """
615        ads = self.android_devices
616        MINIMUM_SUCCESS_RATE = .95
617        success_count = 0
618        fail_count = 0
619
620        for i in range(1, self.stress_test_number + 1):
621
622            ensure_phones_default_state(
623                self.log, [self.android_devices[0], self.android_devices[1]])
624            WifiUtils.wifi_reset(self.log, self.android_devices[0])
625            WifiUtils.wifi_toggle_state(self.log, self.android_devices[0],
626                                        False)
627
628            if data_connectivity_single_bearer(
629                    self.log, self.android_devices[0], RAT_3G):
630                success_count += 1
631                result_str = "Succeeded"
632            else:
633                fail_count += 1
634                result_str = "Failed"
635            self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
636                i, result_str, success_count, self.stress_test_number))
637
638        self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
639            success_count, fail_count, str(100 * success_count / (
640                success_count + fail_count))))
641        if success_count / (
642                success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
643            return True
644        else:
645            return False
646
647    @TelephonyBaseTest.tel_test_wrap
648    def test_4g_stress(self):
649        """Stress Test data connection in 4g.
650
651        This is a stress test for "test_4g".
652        Default MINIMUM_SUCCESS_RATE is set to 95%.
653
654        Returns:
655            True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
656            False otherwise.
657        """
658        ads = self.android_devices
659        MINIMUM_SUCCESS_RATE = .95
660        success_count = 0
661        fail_count = 0
662
663        for i in range(1, self.stress_test_number + 1):
664
665            ensure_phones_default_state(
666                self.log, [self.android_devices[0], self.android_devices[1]])
667            WifiUtils.wifi_reset(self.log, self.android_devices[0])
668            WifiUtils.wifi_toggle_state(self.log, self.android_devices[0],
669                                        False)
670
671            if data_connectivity_single_bearer(
672                    self.log, self.android_devices[0], RAT_4G):
673                success_count += 1
674                result_str = "Succeeded"
675            else:
676                fail_count += 1
677                result_str = "Failed"
678            self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
679                i, result_str, success_count, self.stress_test_number))
680
681        self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
682            success_count, fail_count, str(100 * success_count / (
683                success_count + fail_count))))
684        if success_count / (
685                success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
686            return True
687        else:
688            return False
689
690    def _test_setup_tethering(self, ads, network_generation=None):
691        """Pre setup steps for WiFi tethering test.
692
693        Ensure all ads are idle.
694        Ensure tethering provider:
695            turn off APM, turn off WiFI, turn on Data.
696            have Internet connection, no active ongoing WiFi tethering.
697
698        Returns:
699            True if success.
700            False if failed.
701        """
702        ensure_phones_idle(self.log, ads)
703
704        if network_generation is not None:
705            if not ensure_network_generation_for_subscription(self.log,
706                self.android_devices[0],
707                self.android_devices[0].droid.subscriptionGetDefaultDataSubId(),
708                network_generation, MAX_WAIT_TIME_NW_SELECTION,
709                NETWORK_SERVICE_DATA):
710                self.log.error("Device failed to reselect in {}s.".format(
711                    MAX_WAIT_TIME_NW_SELECTION))
712                return False
713
714        self.log.info("Airplane Off, Wifi Off, Data On.")
715        toggle_airplane_mode(self.log, self.android_devices[0], False)
716        WifiUtils.wifi_toggle_state(self.log, self.android_devices[0], False)
717        self.android_devices[0].droid.telephonyToggleDataConnection(True)
718        if not wait_for_cell_data_connection(self.log, self.android_devices[0],
719                                             True):
720            self.log.error("Failed to enable data connection.")
721            return False
722
723        self.log.info("Verify internet")
724        if not verify_http_connection(self.log, self.android_devices[0]):
725            self.log.error("Data not available on cell.")
726            return False
727
728        # Turn off active SoftAP if any.
729        if ads[0].droid.wifiIsApEnabled():
730            WifiUtils.stop_wifi_tethering(self.log, ads[0])
731
732        return True
733
734    @TelephonyBaseTest.tel_test_wrap
735    def test_tethering_4g_to_2gwifi(self):
736        """WiFi Tethering test: LTE to WiFI 2.4G Tethering
737
738        1. DUT in LTE mode, idle.
739        2. DUT start 2.4G WiFi Tethering
740        3. PhoneB disable data, connect to DUT's softAP
741        4. Verify Internet access on DUT and PhoneB
742
743        Returns:
744            True if success.
745            False if failed.
746        """
747        ads = self.android_devices
748        if not self._test_setup_tethering(ads, RAT_4G):
749            self.log.error("Verify 4G Internet access failed.")
750            return False
751
752        return wifi_tethering_setup_teardown(
753            self.log,
754            ads[0],
755            [ads[1]],
756            ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
757            check_interval=10,
758            check_iteration=10)
759
760    @TelephonyBaseTest.tel_test_wrap
761    def test_tethering_4g_to_5gwifi(self):
762        """WiFi Tethering test: LTE to WiFI 5G Tethering
763
764        1. DUT in LTE mode, idle.
765        2. DUT start 5G WiFi Tethering
766        3. PhoneB disable data, connect to DUT's softAP
767        4. Verify Internet access on DUT and PhoneB
768
769        Returns:
770            True if success.
771            False if failed.
772        """
773        ads = self.android_devices
774        if not self._test_setup_tethering(ads, RAT_4G):
775            self.log.error("Verify 4G Internet access failed.")
776            return False
777
778        return wifi_tethering_setup_teardown(
779            self.log,
780            ads[0],
781            [ads[1]],
782            ap_band=WifiUtils.WIFI_CONFIG_APBAND_5G,
783            check_interval=10,
784            check_iteration=10)
785
786    @TelephonyBaseTest.tel_test_wrap
787    def test_tethering_3g_to_2gwifi(self):
788        """WiFi Tethering test: 3G to WiFI 2.4G Tethering
789
790        1. DUT in 3G mode, idle.
791        2. DUT start 2.4G WiFi Tethering
792        3. PhoneB disable data, connect to DUT's softAP
793        4. Verify Internet access on DUT and PhoneB
794
795        Returns:
796            True if success.
797            False if failed.
798        """
799        ads = self.android_devices
800        if not self._test_setup_tethering(ads, RAT_3G):
801            self.log.error("Verify 3G Internet access failed.")
802            return False
803
804        return wifi_tethering_setup_teardown(
805            self.log,
806            ads[0],
807            [ads[1]],
808            ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
809            check_interval=10,
810            check_iteration=10)
811
812    @TelephonyBaseTest.tel_test_wrap
813    def test_tethering_3g_to_5gwifi(self):
814        """WiFi Tethering test: 3G to WiFI 5G Tethering
815
816        1. DUT in 3G mode, idle.
817        2. DUT start 5G WiFi Tethering
818        3. PhoneB disable data, connect to DUT's softAP
819        4. Verify Internet access on DUT and PhoneB
820
821        Returns:
822            True if success.
823            False if failed.
824        """
825        ads = self.android_devices
826        if not self._test_setup_tethering(ads, RAT_3G):
827            self.log.error("Verify 3G Internet access failed.")
828            return False
829
830        return wifi_tethering_setup_teardown(
831            self.log,
832            ads[0],
833            [ads[1]],
834            ap_band=WifiUtils.WIFI_CONFIG_APBAND_5G,
835            check_interval=10,
836            check_iteration=10)
837
838    @TelephonyBaseTest.tel_test_wrap
839    def test_tethering_4g_to_2gwifi_2clients(self):
840        """WiFi Tethering test: LTE to WiFI 2.4G Tethering, with multiple clients
841
842        1. DUT in 3G mode, idle.
843        2. DUT start 5G WiFi Tethering
844        3. PhoneB and PhoneC disable data, connect to DUT's softAP
845        4. Verify Internet access on DUT and PhoneB PhoneC
846
847        Returns:
848            True if success.
849            False if failed.
850        """
851        ads = self.android_devices
852        if not self._test_setup_tethering(ads, RAT_4G):
853            self.log.error("Verify 4G Internet access failed.")
854            return False
855
856        return wifi_tethering_setup_teardown(
857            self.log,
858            ads[0],
859            [ads[1], ads[2]],
860            ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
861            check_interval=10,
862            check_iteration=10)
863
864    @TelephonyBaseTest.tel_test_wrap
865    def test_tethering_2g_to_2gwifi(self):
866        """WiFi Tethering test: 2G to WiFI 2.4G Tethering
867
868        1. DUT in 2G mode, idle.
869        2. DUT start 2.4G WiFi Tethering
870        3. PhoneB disable data, connect to DUT's softAP
871        4. Verify Internet access on DUT and PhoneB
872
873        Returns:
874            True if success.
875            False if failed.
876        """
877        ads = self.android_devices
878        if not self._test_setup_tethering(ads, RAT_2G):
879            self.log.error("Verify 2G Internet access failed.")
880            return False
881
882        return wifi_tethering_setup_teardown(
883            self.log,
884            ads[0],
885            [ads[1]],
886            ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
887            check_interval=10,
888            check_iteration=10)
889
890    @TelephonyBaseTest.tel_test_wrap
891    def test_tethering_2g_to_5gwifi(self):
892        """WiFi Tethering test: 2G to WiFI 5G Tethering
893
894        1. DUT in 2G mode, idle.
895        2. DUT start 5G WiFi Tethering
896        3. PhoneB disable data, connect to DUT's softAP
897        4. Verify Internet access on DUT and PhoneB
898
899        Returns:
900            True if success.
901            False if failed.
902        """
903        ads = self.android_devices
904        if not self._test_setup_tethering(ads, RAT_2G):
905            self.log.error("Verify 2G Internet access failed.")
906            return False
907
908        return wifi_tethering_setup_teardown(
909            self.log,
910            ads[0],
911            [ads[1]],
912            ap_band=WifiUtils.WIFI_CONFIG_APBAND_5G,
913            check_interval=10,
914            check_iteration=10)
915
916    @TelephonyBaseTest.tel_test_wrap
917    def test_disable_wifi_tethering_resume_connected_wifi(self):
918        """WiFi Tethering test: WiFI connected to 2.4G network,
919        start (LTE) 2.4G WiFi tethering, then stop tethering
920
921        1. DUT in LTE mode, idle. WiFi connected to 2.4G Network
922        2. DUT start 2.4G WiFi Tethering
923        3. PhoneB disable data, connect to DUT's softAP
924        4. Verify Internet access on DUT and PhoneB
925        5. Disable WiFi Tethering on DUT.
926        6. Verify DUT automatically connect to previous WiFI network
927
928        Returns:
929            True if success.
930            False if failed.
931        """
932        ads = self.android_devices
933        if not self._test_setup_tethering(ads, RAT_4G):
934            self.log.error("Verify 4G Internet access failed.")
935            return False
936        self.log.info("Connect WiFi.")
937        if not ensure_wifi_connected(self.log, ads[0], self.wifi_network_ssid,
938                                     self.wifi_network_pass):
939            self.log.error("WiFi connect fail.")
940            return False
941        self.log.info("Start WiFi Tethering.")
942        if not wifi_tethering_setup_teardown(self.log,
943                                             ads[0],
944                                             [ads[1]],
945                                             check_interval=10,
946                                             check_iteration=2):
947            self.log.error("WiFi Tethering failed.")
948            return False
949
950        if (not wait_for_wifi_data_connection(self.log, ads[0], True) or
951                not verify_http_connection(self.log, ads[0])):
952            self.log.error("Provider data did not return to Wifi")
953            return False
954        return True
955
956    @TelephonyBaseTest.tel_test_wrap
957    def test_toggle_data_during_active_wifi_tethering(self):
958        """WiFi Tethering test: Toggle Data during active WiFi Tethering
959
960        1. DUT in LTE mode, idle.
961        2. DUT start 2.4G WiFi Tethering
962        3. PhoneB disable data, connect to DUT's softAP
963        4. Verify Internet access on DUT and PhoneB
964        5. Disable Data on DUT, verify PhoneB still connected to WiFi, but no Internet access.
965        6. Enable Data on DUT, verify PhoneB still connected to WiFi and have Internet access.
966
967        Returns:
968            True if success.
969            False if failed.
970        """
971        ads = self.android_devices
972        if not self._test_setup_tethering(ads, RAT_4G):
973            self.log.error("Verify 4G Internet access failed.")
974            return False
975        try:
976            ssid = rand_ascii_str(10)
977            if not wifi_tethering_setup_teardown(
978                    self.log,
979                    ads[0],
980                [ads[1]],
981                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
982                    check_interval=10,
983                    check_iteration=2,
984                    do_cleanup=False,
985                    ssid=ssid):
986                self.log.error("WiFi Tethering failed.")
987                return False
988
989            if not ads[0].droid.wifiIsApEnabled():
990                self.log.error("Provider WiFi tethering stopped.")
991                return False
992
993            self.log.info(
994                "Disable Data on Provider, verify no data on Client.")
995            ads[0].droid.telephonyToggleDataConnection(False)
996            time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
997            if verify_http_connection(self.log, ads[0]):
998                self.log.error("Disable data on provider failed.")
999                return False
1000            if not ads[0].droid.wifiIsApEnabled():
1001                self.log.error("Provider WiFi tethering stopped.")
1002                return False
1003            wifi_info = ads[1].droid.wifiGetConnectionInfo()
1004
1005            if wifi_info[WifiEnums.SSID_KEY] != ssid:
1006                self.log.error("WiFi error. Info: {}".format(wifi_info))
1007                return False
1008            if verify_http_connection(self.log, ads[1]):
1009                self.log.error("Client should not have Internet connection.")
1010                return False
1011
1012            self.log.info(
1013                "Enable Data on Provider, verify data available on Client.")
1014            ads[0].droid.telephonyToggleDataConnection(True)
1015            time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
1016            if not verify_http_connection(self.log, ads[0]):
1017                self.log.error("Enable data on provider failed.")
1018                return False
1019            if not ads[0].droid.wifiIsApEnabled():
1020                self.log.error("Provider WiFi tethering stopped.")
1021                return False
1022            wifi_info = ads[1].droid.wifiGetConnectionInfo()
1023
1024            if wifi_info[WifiEnums.SSID_KEY] != ssid:
1025                self.log.error("WiFi error. Info: {}".format(wifi_info))
1026                return False
1027            if not verify_http_connection(self.log, ads[1]):
1028                self.log.error("Client have no Internet connection!")
1029                return False
1030        finally:
1031            if not wifi_tethering_cleanup(self.log, ads[0], [ads[1]]):
1032                return False
1033        return True
1034
1035    # Invalid Live Test. Can't rely on the result of this test with live network.
1036    # Network may decide not to change the RAT when data conenction is active.
1037    @TelephonyBaseTest.tel_test_wrap
1038    def test_change_rat_during_active_wifi_tethering_lte_to_3g(self):
1039        """WiFi Tethering test: Change Cellular Data RAT generation from LTE to 3G,
1040            during active WiFi Tethering.
1041
1042        1. DUT in LTE mode, idle.
1043        2. DUT start 2.4G WiFi Tethering
1044        3. PhoneB disable data, connect to DUT's softAP
1045        4. Verily Internet access on DUT and PhoneB
1046        5. Change DUT Cellular Data RAT generation from LTE to 3G.
1047        6. Verify both DUT and PhoneB have Internet access.
1048
1049        Returns:
1050            True if success.
1051            False if failed.
1052        """
1053        ads = self.android_devices
1054        if not self._test_setup_tethering(ads, RAT_4G):
1055            self.log.error("Verify 4G Internet access failed.")
1056            return False
1057        try:
1058            if not wifi_tethering_setup_teardown(
1059                    self.log,
1060                    ads[0],
1061                [ads[1]],
1062                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1063                    check_interval=10,
1064                    check_iteration=2,
1065                    do_cleanup=False):
1066                self.log.error("WiFi Tethering failed.")
1067                return False
1068
1069            if not ads[0].droid.wifiIsApEnabled():
1070                self.log.error("Provider WiFi tethering stopped.")
1071                return False
1072
1073            self.log.info("Provider change RAT from LTE to 3G.")
1074            if not ensure_network_generation(
1075                    self.log,
1076                    ads[0],
1077                    RAT_3G,
1078                    voice_or_data=NETWORK_SERVICE_DATA,
1079                    toggle_apm_after_setting=False):
1080                self.log.error("Provider failed to reselect to 3G.")
1081                return False
1082            time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
1083            if not verify_http_connection(self.log, ads[0]):
1084                self.log.error("Data not available on Provider.")
1085                return False
1086            if not ads[0].droid.wifiIsApEnabled():
1087                self.log.error("Provider WiFi tethering stopped.")
1088                return False
1089            if not tethering_check_internet_connection(self.log, ads[0],
1090                                                       [ads[1]], 10, 5):
1091                return False
1092        finally:
1093            if not wifi_tethering_cleanup(self.log, ads[0], [ads[1]]):
1094                return False
1095        return True
1096
1097    # Invalid Live Test. Can't rely on the result of this test with live network.
1098    # Network may decide not to change the RAT when data conenction is active.
1099    @TelephonyBaseTest.tel_test_wrap
1100    def test_change_rat_during_active_wifi_tethering_3g_to_lte(self):
1101        """WiFi Tethering test: Change Cellular Data RAT generation from 3G to LTE,
1102            during active WiFi Tethering.
1103
1104        1. DUT in 3G mode, idle.
1105        2. DUT start 2.4G WiFi Tethering
1106        3. PhoneB disable data, connect to DUT's softAP
1107        4. Verily Internet access on DUT and PhoneB
1108        5. Change DUT Cellular Data RAT generation from 3G to LTE.
1109        6. Verify both DUT and PhoneB have Internet access.
1110
1111        Returns:
1112            True if success.
1113            False if failed.
1114        """
1115        ads = self.android_devices
1116        if not self._test_setup_tethering(ads, RAT_3G):
1117            self.log.error("Verify 3G Internet access failed.")
1118            return False
1119        try:
1120            if not wifi_tethering_setup_teardown(
1121                    self.log,
1122                    ads[0],
1123                [ads[1]],
1124                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1125                    check_interval=10,
1126                    check_iteration=2,
1127                    do_cleanup=False):
1128                self.log.error("WiFi Tethering failed.")
1129                return False
1130
1131            if not ads[0].droid.wifiIsApEnabled():
1132                self.log.error("Provider WiFi tethering stopped.")
1133                return False
1134
1135            self.log.info("Provider change RAT from 3G to 4G.")
1136            if not ensure_network_generation(
1137                    self.log,
1138                    ads[0],
1139                    RAT_4G,
1140                    voice_or_data=NETWORK_SERVICE_DATA,
1141                    toggle_apm_after_setting=False):
1142                self.log.error("Provider failed to reselect to 4G.")
1143                return False
1144            time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
1145            if not verify_http_connection(self.log, ads[0]):
1146                self.log.error("Data not available on Provider.")
1147                return False
1148            if not ads[0].droid.wifiIsApEnabled():
1149                self.log.error("Provider WiFi tethering stopped.")
1150                return False
1151            if not tethering_check_internet_connection(self.log, ads[0],
1152                                                       [ads[1]], 10, 5):
1153                return False
1154        finally:
1155            if not wifi_tethering_cleanup(self.log, ads[0], [ads[1]]):
1156                return False
1157        return True
1158
1159    @TelephonyBaseTest.tel_test_wrap
1160    def test_toggle_apm_during_active_wifi_tethering(self):
1161        """WiFi Tethering test: Toggle APM during active WiFi Tethering
1162
1163        1. DUT in LTE mode, idle.
1164        2. DUT start 2.4G WiFi Tethering
1165        3. PhoneB disable data, connect to DUT's softAP
1166        4. Verify Internet access on DUT and PhoneB
1167        5. DUT toggle APM on, verify WiFi tethering stopped, PhoneB lost WiFi connection.
1168        6. DUT toggle APM off, verify PhoneA have cellular data and Internet connection.
1169
1170        Returns:
1171            True if success.
1172            False if failed.
1173        """
1174        ads = self.android_devices
1175        if not self._test_setup_tethering(ads, RAT_4G):
1176            self.log.error("Verify 4G Internet access failed.")
1177            return False
1178        try:
1179            ssid = rand_ascii_str(10)
1180            if not wifi_tethering_setup_teardown(
1181                    self.log,
1182                    ads[0],
1183                [ads[1]],
1184                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1185                    check_interval=10,
1186                    check_iteration=2,
1187                    do_cleanup=False,
1188                    ssid=ssid):
1189                self.log.error("WiFi Tethering failed.")
1190                return False
1191
1192            if not ads[0].droid.wifiIsApEnabled():
1193                self.log.error("Provider WiFi tethering stopped.")
1194                return False
1195
1196            self.log.info(
1197                "Provider turn on APM, verify no wifi/data on Client.")
1198            if not toggle_airplane_mode(self.log, ads[0], True):
1199                self.log.error("Provider turn on APM failed.")
1200                return False
1201            time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
1202            if ads[0].droid.wifiIsApEnabled():
1203                self.log.error("Provider WiFi tethering not stopped.")
1204                return False
1205            if verify_http_connection(self.log, ads[1]):
1206                self.log.error("Client should not have Internet connection.")
1207                return False
1208            wifi_info = ads[1].droid.wifiGetConnectionInfo()
1209            self.log.info("WiFi Info: {}".format(wifi_info))
1210
1211            if wifi_info[WifiEnums.SSID_KEY] == ssid:
1212                self.log.error(
1213                    "WiFi error. WiFi should not be connected.".format(
1214                        wifi_info))
1215                return False
1216
1217            self.log.info("Provider turn off APM.")
1218            if not toggle_airplane_mode(self.log, ads[0], False):
1219                self.log.error("Provider turn on APM failed.")
1220                return False
1221            time.sleep(WAIT_TIME_DATA_STATUS_CHANGE_DURING_WIFI_TETHERING)
1222            if ads[0].droid.wifiIsApEnabled():
1223                self.log.error("Provider WiFi tethering should not on.")
1224                return False
1225            if not verify_http_connection(self.log, ads[0]):
1226                self.log.error("Provider should have Internet connection.")
1227                return False
1228        finally:
1229            ads[1].droid.telephonyToggleDataConnection(True)
1230            WifiUtils.wifi_reset(self.log, ads[1])
1231        return True
1232
1233    @TelephonyBaseTest.tel_test_wrap
1234    def test_tethering_entitlement_check(self):
1235        """Tethering Entitlement Check Test
1236
1237        Get tethering entitlement check result.
1238
1239        Returns:
1240            True if entitlement check returns True.
1241        """
1242        ad = self.android_devices[0]
1243
1244        result = ad.droid.carrierConfigIsTetheringModeAllowed(
1245            TETHERING_MODE_WIFI, MAX_WAIT_TIME_TETHERING_ENTITLEMENT_CHECK)
1246        self.log.info("{} tethering entitlement check result: {}.".format(
1247            ad.serial, result))
1248        return result
1249
1250    @TelephonyBaseTest.tel_test_wrap
1251    def test_tethering_4g_to_2gwifi_stress(self):
1252        """Stress Test LTE to WiFI 2.4G Tethering
1253
1254        This is a stress test for "test_tethering_4g_to_2gwifi".
1255        Default MINIMUM_SUCCESS_RATE is set to 95%.
1256
1257        Returns:
1258            True stress pass rate is higher than MINIMUM_SUCCESS_RATE.
1259            False otherwise.
1260        """
1261        MINIMUM_SUCCESS_RATE = .95
1262        success_count = 0
1263        fail_count = 0
1264
1265        for i in range(1, self.stress_test_number + 1):
1266
1267            ensure_phones_default_state(
1268                self.log, [self.android_devices[0], self.android_devices[1]])
1269
1270            if self.test_tethering_4g_to_2gwifi():
1271                success_count += 1
1272                result_str = "Succeeded"
1273            else:
1274                fail_count += 1
1275                result_str = "Failed"
1276            self.log.info("Iteration {} {}. Current: {} / {} passed.".format(
1277                i, result_str, success_count, self.stress_test_number))
1278
1279        self.log.info("Final Count - Success: {}, Failure: {} - {}%".format(
1280            success_count, fail_count, str(100 * success_count / (
1281                success_count + fail_count))))
1282        if success_count / (
1283                success_count + fail_count) >= MINIMUM_SUCCESS_RATE:
1284            return True
1285        else:
1286            return False
1287
1288    @TelephonyBaseTest.tel_test_wrap
1289    def test_tethering_wifi_ssid_quotes(self):
1290        """WiFi Tethering test: SSID name have quotes.
1291        1. Set SSID name have double quotes.
1292        2. Start LTE to WiFi (2.4G) tethering.
1293        3. Verify tethering.
1294
1295        Returns:
1296            True if success.
1297            False if failed.
1298        """
1299        ads = self.android_devices
1300        if not self._test_setup_tethering(ads):
1301            self.log.error("Verify Internet access failed.")
1302            return False
1303        ssid = "\"" + rand_ascii_str(10) + "\""
1304        self.log.info("Starting WiFi Tethering test with ssid: {}".format(
1305            ssid))
1306
1307        return wifi_tethering_setup_teardown(
1308            self.log,
1309            ads[0],
1310            [ads[1]],
1311            ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1312            check_interval=10,
1313            check_iteration=10,
1314            ssid=ssid)
1315
1316    @TelephonyBaseTest.tel_test_wrap
1317    def test_tethering_wifi_password_escaping_characters(self):
1318        """WiFi Tethering test: password have escaping characters.
1319        1. Set password have escaping characters.
1320            e.g.: '"DQ=/{Yqq;M=(^_3HzRvhOiL8S%`]w&l<Qp8qH)bs<4E9v_q=HLr^)}w$blA0Kg'
1321        2. Start LTE to WiFi (2.4G) tethering.
1322        3. Verify tethering.
1323
1324        Returns:
1325            True if success.
1326            False if failed.
1327        """
1328        ads = self.android_devices
1329        if not self._test_setup_tethering(ads):
1330            self.log.error("Verify Internet access failed.")
1331            return False
1332
1333        password = '"DQ=/{Yqq;M=(^_3HzRvhOiL8S%`]w&l<Qp8qH)bs<4E9v_q=HLr^)}w$blA0Kg'
1334        self.log.info("Starting WiFi Tethering test with password: {}".format(
1335            password))
1336
1337        return wifi_tethering_setup_teardown(
1338            self.log,
1339            ads[0],
1340            [ads[1]],
1341            ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1342            check_interval=10,
1343            check_iteration=10,
1344            password=password)
1345
1346    def _test_start_wifi_tethering_connect_teardown(self, ad_host, ad_client,
1347                                                    ssid, password):
1348        """Private test util for WiFi Tethering.
1349
1350        1. Host start WiFi tethering.
1351        2. Client connect to tethered WiFi.
1352        3. Host tear down WiFi tethering.
1353
1354        Args:
1355            ad_host: android device object for host
1356            ad_client: android device object for client
1357            ssid: WiFi tethering ssid
1358            password: WiFi tethering password
1359
1360        Returns:
1361            True if no error happen, otherwise False.
1362        """
1363        result = True
1364        # Turn off active SoftAP if any.
1365        if ad_host.droid.wifiIsApEnabled():
1366            WifiUtils.stop_wifi_tethering(self.log, ad_host)
1367
1368        time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
1369        if not WifiUtils.start_wifi_tethering(self.log, ad_host, ssid,
1370                                              password,
1371                                              WifiUtils.WIFI_CONFIG_APBAND_2G):
1372            self.log.error("Start WiFi tethering failed.")
1373            result = False
1374        time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
1375        if not ensure_wifi_connected(self.log, ad_client, ssid, password):
1376            self.log.error("Client connect to WiFi failed.")
1377            result = False
1378        if not WifiUtils.wifi_reset(self.log, ad_client):
1379            self.log.error("Reset client WiFi failed. {}".format(
1380                ad_client.serial))
1381            result = False
1382        if not WifiUtils.stop_wifi_tethering(self.log, ad_host):
1383            self.log.error("Stop WiFi tethering failed.")
1384            result = False
1385        return result
1386
1387    @TelephonyBaseTest.tel_test_wrap
1388    def test_tethering_wifi_ssid(self):
1389        """WiFi Tethering test: start WiFi tethering with all kinds of SSIDs.
1390
1391        For each listed SSID, start WiFi tethering on DUT, client connect WiFi,
1392        then tear down WiFi tethering.
1393
1394        Returns:
1395            True if WiFi tethering succeed on all SSIDs.
1396            False if failed.
1397        """
1398        ads = self.android_devices
1399        if not self._test_setup_tethering(ads, RAT_4G):
1400            self.log.error("Setup Failed.")
1401            return False
1402        ssid_list = [" !\"#$%&'()*+,-./0123456789:;<=>?",
1403                     "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_",
1404                     "`abcdefghijklmnopqrstuvwxyz{|}~", " a ", "!b!", "#c#",
1405                     "$d$", "%e%", "&f&", "'g'", "(h(", ")i)", "*j*", "+k+",
1406                     "-l-", ".m.", "/n/", "_",
1407                     " !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}",
1408                     "\u0644\u062c\u0648\u062c", "\u8c37\u6b4c", "\uad6c\uae00"
1409                     "\u30b0\u30fc\u30eb",
1410                     "\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0443\u0439"]
1411        fail_list = {}
1412
1413        for ssid in ssid_list:
1414            password = rand_ascii_str(8)
1415            self.log.info("SSID: <{}>, Password: <{}>".format(ssid, password))
1416            if not self._test_start_wifi_tethering_connect_teardown(
1417                    ads[0], ads[1], ssid, password):
1418                fail_list[ssid] = password
1419
1420        if (len(fail_list) > 0):
1421            self.log.error("Failed cases: {}".format(fail_list))
1422            return False
1423        else:
1424            return True
1425
1426    @TelephonyBaseTest.tel_test_wrap
1427    def test_tethering_wifi_password(self):
1428        """WiFi Tethering test: start WiFi tethering with all kinds of passwords.
1429
1430        For each listed password, start WiFi tethering on DUT, client connect WiFi,
1431        then tear down WiFi tethering.
1432
1433        Returns:
1434            True if WiFi tethering succeed on all passwords.
1435            False if failed.
1436        """
1437        ads = self.android_devices
1438        if not self._test_setup_tethering(ads, RAT_4G):
1439            self.log.error("Setup Failed.")
1440            return False
1441        password_list = [
1442            " !\"#$%&'()*+,-./0123456789:;<=>?",
1443            "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_",
1444            "`abcdefghijklmnopqrstuvwxyz{|}~",
1445            " !\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}", "abcdefgh",
1446            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!",
1447            " a12345 ", "!b12345!", "#c12345#", "$d12345$", "%e12345%",
1448            "&f12345&", "'g12345'", "(h12345(", ")i12345)", "*j12345*",
1449            "+k12345+", "-l12345-", ".m12345.", "/n12345/"
1450        ]
1451        fail_list = {}
1452
1453        for password in password_list:
1454            result = True
1455            ssid = rand_ascii_str(8)
1456            self.log.info("SSID: <{}>, Password: <{}>".format(ssid, password))
1457            if not self._test_start_wifi_tethering_connect_teardown(
1458                    ads[0], ads[1], ssid, password):
1459                fail_list[ssid] = password
1460
1461        if (len(fail_list) > 0):
1462            self.log.error("Failed cases: {}".format(fail_list))
1463            return False
1464        else:
1465            return True
1466
1467    def _test_tethering_wifi_and_voice_call(
1468            self, provider, client, provider_data_rat, provider_setup_func,
1469            provider_in_call_check_func):
1470        if not self._test_setup_tethering(
1471            [provider, client], provider_data_rat):
1472            self.log.error("Verify 4G Internet access failed.")
1473            return False
1474
1475        tasks = [(provider_setup_func, (self.log, provider)),
1476                 (phone_setup_voice_general, (self.log, client))]
1477        if not multithread_func(self.log, tasks):
1478            self.log.error("Phone Failed to Set Up VoLTE.")
1479            return False
1480
1481        try:
1482            self.log.info("1. Setup WiFi Tethering.")
1483            if not wifi_tethering_setup_teardown(
1484                    self.log,
1485                    provider,
1486                [client],
1487                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1488                    check_interval=10,
1489                    check_iteration=2,
1490                    do_cleanup=False):
1491                self.log.error("WiFi Tethering failed.")
1492                return False
1493            self.log.info("2. Make outgoing call.")
1494            if not call_setup_teardown(
1495                    self.log,
1496                    provider,
1497                    client,
1498                    ad_hangup=None,
1499                    verify_caller_func=provider_in_call_check_func):
1500                self.log.error("Setup Call Failed.")
1501                return False
1502            self.log.info("3. Verify data.")
1503            if not verify_http_connection(self.log, provider):
1504                self.log.error("Provider have no Internet access.")
1505            if not verify_http_connection(self.log, client):
1506                self.log.error("Client have no Internet access.")
1507            hangup_call(self.log, provider)
1508
1509            time.sleep(WAIT_TIME_BETWEEN_REG_AND_CALL)
1510
1511            self.log.info("4. Make incoming call.")
1512            if not call_setup_teardown(
1513                    self.log,
1514                    client,
1515                    provider,
1516                    ad_hangup=None,
1517                    verify_callee_func=provider_in_call_check_func):
1518                self.log.error("Setup Call Failed.")
1519                return False
1520            self.log.info("5. Verify data.")
1521            if not verify_http_connection(self.log, provider):
1522                self.log.error("Provider have no Internet access.")
1523            if not verify_http_connection(self.log, client):
1524                self.log.error("Client have no Internet access.")
1525            hangup_call(self.log, provider)
1526
1527        finally:
1528            if not wifi_tethering_cleanup(self.log, provider, [client]):
1529                return False
1530        return True
1531
1532    @TelephonyBaseTest.tel_test_wrap
1533    def test_tethering_wifi_volte_call(self):
1534        """WiFi Tethering test: VoLTE call during WiFi tethering
1535        1. Start LTE to WiFi (2.4G) tethering.
1536        2. Verify tethering.
1537        3. Make outgoing VoLTE call on tethering provider.
1538        4. Verify tethering still works.
1539        5. Make incoming VoLTE call on tethering provider.
1540        6. Verify tethering still works.
1541
1542        Returns:
1543            True if success.
1544            False if failed.
1545        """
1546        return self._test_tethering_wifi_and_voice_call(
1547            self.android_devices[0], self.android_devices[1], RAT_4G,
1548            phone_setup_volte, is_phone_in_call_volte)
1549
1550    @TelephonyBaseTest.tel_test_wrap
1551    def test_tethering_wifi_csfb_call(self):
1552        """WiFi Tethering test: CSFB call during WiFi tethering
1553        1. Start LTE to WiFi (2.4G) tethering.
1554        2. Verify tethering.
1555        3. Make outgoing CSFB call on tethering provider.
1556        4. Verify tethering still works.
1557        5. Make incoming CSFB call on tethering provider.
1558        6. Verify tethering still works.
1559
1560        Returns:
1561            True if success.
1562            False if failed.
1563        """
1564        return self._test_tethering_wifi_and_voice_call(
1565            self.android_devices[0], self.android_devices[1], RAT_4G,
1566            phone_setup_csfb, is_phone_in_call_csfb)
1567
1568    @TelephonyBaseTest.tel_test_wrap
1569    def test_tethering_wifi_3g_call(self):
1570        """WiFi Tethering test: 3G call during WiFi tethering
1571        1. Start 3G to WiFi (2.4G) tethering.
1572        2. Verify tethering.
1573        3. Make outgoing CS call on tethering provider.
1574        4. Verify tethering still works.
1575        5. Make incoming CS call on tethering provider.
1576        6. Verify tethering still works.
1577
1578        Returns:
1579            True if success.
1580            False if failed.
1581        """
1582        return self._test_tethering_wifi_and_voice_call(
1583            self.android_devices[0], self.android_devices[1], RAT_3G,
1584            phone_setup_voice_3g, is_phone_in_call_3g)
1585
1586    @TelephonyBaseTest.tel_test_wrap
1587    def test_tethering_wifi_no_password(self):
1588        """WiFi Tethering test: Start WiFi tethering with no password
1589
1590        1. DUT is idle.
1591        2. DUT start 2.4G WiFi Tethering, with no WiFi password.
1592        3. PhoneB disable data, connect to DUT's softAP
1593        4. Verify Internet access on DUT and PhoneB
1594
1595        Returns:
1596            True if success.
1597            False if failed.
1598        """
1599        ads = self.android_devices
1600        if not self._test_setup_tethering(ads):
1601            self.log.error("Verify Internet access failed.")
1602            return False
1603
1604        return wifi_tethering_setup_teardown(
1605            self.log,
1606            ads[0],
1607            [ads[1]],
1608            ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1609            check_interval=10,
1610            check_iteration=10,
1611            password="")
1612
1613    @TelephonyBaseTest.tel_test_wrap
1614    def test_tethering_wifi_reboot(self):
1615        """WiFi Tethering test: Start WiFi tethering then Reboot device
1616
1617        1. DUT is idle.
1618        2. DUT start 2.4G WiFi Tethering.
1619        3. PhoneB disable data, connect to DUT's softAP
1620        4. Verify Internet access on DUT and PhoneB
1621        5. Reboot DUT
1622        6. After DUT reboot, verify tethering is stopped.
1623
1624        Returns:
1625            True if success.
1626            False if failed.
1627        """
1628        ads = self.android_devices
1629        if not self._test_setup_tethering(ads):
1630            self.log.error("Verify Internet access failed.")
1631            return False
1632        try:
1633            if not wifi_tethering_setup_teardown(
1634                    self.log,
1635                    ads[0],
1636                [ads[1]],
1637                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1638                    check_interval=10,
1639                    check_iteration=2,
1640                    do_cleanup=False):
1641                self.log.error("WiFi Tethering failed.")
1642                return False
1643
1644            if not ads[0].droid.wifiIsApEnabled():
1645                self.log.error("Provider WiFi tethering stopped.")
1646                return False
1647
1648            self.log.info("Reboot DUT:{}".format(ads[0].serial))
1649            ads[0].reboot()
1650            time.sleep(WAIT_TIME_AFTER_REBOOT +
1651                       WAIT_TIME_TETHERING_AFTER_REBOOT)
1652
1653            self.log.info("After reboot check if tethering stopped.")
1654            if ads[0].droid.wifiIsApEnabled():
1655                self.log.error("Provider WiFi tethering did NOT stopped.")
1656                return False
1657        finally:
1658            ads[1].droid.telephonyToggleDataConnection(True)
1659            WifiUtils.wifi_reset(self.log, ads[1])
1660            if ads[0].droid.wifiIsApEnabled():
1661                WifiUtils.stop_wifi_tethering(self.log, ads[0])
1662        return True
1663
1664    @TelephonyBaseTest.tel_test_wrap
1665    def test_connect_wifi_start_tethering_wifi_reboot(self):
1666        """WiFi Tethering test: WiFI connected, then start WiFi tethering,
1667            then reboot device.
1668
1669        Initial Condition: DUT in 4G mode, idle, DUT connect to WiFi.
1670        1. DUT start 2.4G WiFi Tethering.
1671        2. PhoneB disable data, connect to DUT's softAP
1672        3. Verify Internet access on DUT and PhoneB
1673        4. Reboot DUT
1674        5. After DUT reboot, verify tethering is stopped. DUT is able to connect
1675            to previous WiFi AP.
1676
1677        Returns:
1678            True if success.
1679            False if failed.
1680        """
1681        ads = self.android_devices
1682        if not self._test_setup_tethering(ads):
1683            self.log.error("Verify Internet access failed.")
1684            return False
1685
1686        self.log.info("Make sure DUT can connect to live network by WIFI")
1687        if ((not ensure_wifi_connected(self.log, ads[0],
1688                                       self.wifi_network_ssid,
1689                                       self.wifi_network_pass)) or
1690            (not verify_http_connection(self.log, ads[0]))):
1691            self.log.error("WiFi connect fail.")
1692            return False
1693
1694        try:
1695            if not wifi_tethering_setup_teardown(
1696                    self.log,
1697                    ads[0],
1698                [ads[1]],
1699                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1700                    check_interval=10,
1701                    check_iteration=2,
1702                    do_cleanup=False):
1703                self.log.error("WiFi Tethering failed.")
1704                return False
1705
1706            if not ads[0].droid.wifiIsApEnabled():
1707                self.log.error("Provider WiFi tethering stopped.")
1708                return False
1709
1710            self.log.info("Reboot DUT:{}".format(ads[0].serial))
1711            ads[0].reboot()
1712            time.sleep(WAIT_TIME_AFTER_REBOOT)
1713            time.sleep(WAIT_TIME_TETHERING_AFTER_REBOOT)
1714
1715            self.log.info("After reboot check if tethering stopped.")
1716            if ads[0].droid.wifiIsApEnabled():
1717                self.log.error("Provider WiFi tethering did NOT stopped.")
1718                return False
1719
1720            self.log.info("Make sure WiFi can connect automatically.")
1721            if (not wait_for_wifi_data_connection(self.log, ads[0], True) or
1722                    not verify_http_connection(self.log, ads[0])):
1723                self.log.error("Data did not return to WiFi")
1724                return False
1725
1726        finally:
1727            ads[1].droid.telephonyToggleDataConnection(True)
1728            WifiUtils.wifi_reset(self.log, ads[1])
1729            if ads[0].droid.wifiIsApEnabled():
1730                WifiUtils.stop_wifi_tethering(self.log, ads[0])
1731        return True
1732
1733    @TelephonyBaseTest.tel_test_wrap
1734    def test_connect_wifi_reboot_start_tethering_wifi(self):
1735        """WiFi Tethering test: DUT connected to WiFi, then reboot,
1736        After reboot, start WiFi tethering, verify tethering actually works.
1737
1738        Initial Condition: Device set to 4G mode, idle, DUT connect to WiFi.
1739        1. Verify Internet is working on DUT (by WiFi).
1740        2. Reboot DUT.
1741        3. DUT start 2.4G WiFi Tethering.
1742        4. PhoneB disable data, connect to DUT's softAP
1743        5. Verify Internet access on DUT and PhoneB
1744
1745        Returns:
1746            True if success.
1747            False if failed.
1748        """
1749        ads = self.android_devices
1750
1751        if not self._test_setup_tethering(ads):
1752            self.log.error("Verify Internet access failed.")
1753            return False
1754
1755        self.log.info("Make sure DUT can connect to live network by WIFI")
1756        if ((not ensure_wifi_connected(self.log, ads[0],
1757                                       self.wifi_network_ssid,
1758                                       self.wifi_network_pass)) or
1759            (not verify_http_connection(self.log, ads[0]))):
1760            self.log.error("WiFi connect fail.")
1761            return False
1762
1763        self.log.info("Reboot DUT:{}".format(ads[0].serial))
1764        ads[0].reboot()
1765        time.sleep(WAIT_TIME_AFTER_REBOOT)
1766        time.sleep(WAIT_TIME_TETHERING_AFTER_REBOOT)
1767
1768        return wifi_tethering_setup_teardown(
1769            self.log,
1770            ads[0],
1771            [ads[1]],
1772            ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1773            check_interval=10,
1774            check_iteration=10)
1775
1776    @TelephonyBaseTest.tel_test_wrap
1777    def test_tethering_wifi_screen_off_enable_doze_mode(self):
1778        """WiFi Tethering test: Start WiFi tethering, then turn off DUT's screen,
1779            then enable doze mode.
1780
1781        1. Start WiFi tethering on DUT.
1782        2. PhoneB disable data, and connect to DUT's softAP
1783        3. Verify Internet access on DUT and PhoneB
1784        4. Turn off DUT's screen. Wait for 1 minute and
1785            verify Internet access on Client PhoneB.
1786        5. Enable doze mode on DUT. Wait for 1 minute and
1787            verify Internet access on Client PhoneB.
1788
1789        Returns:
1790            True if success.
1791            False if failed.
1792        """
1793        ads = self.android_devices
1794        if not self._test_setup_tethering(ads):
1795            self.log.error("Verify Internet access failed.")
1796            return False
1797        try:
1798            if not wifi_tethering_setup_teardown(
1799                    self.log,
1800                    ads[0],
1801                [ads[1]],
1802                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
1803                    check_interval=10,
1804                    check_iteration=2,
1805                    do_cleanup=False):
1806                self.log.error("WiFi Tethering failed.")
1807                return False
1808
1809            if not ads[0].droid.wifiIsApEnabled():
1810                self.log.error("Provider WiFi tethering stopped.")
1811                return False
1812
1813            self.log.info("Turn off screen on provider: <{}>.".format(ads[
1814                0].serial))
1815            ads[0].droid.goToSleepNow()
1816            time.sleep(60)
1817            if not verify_http_connection(self.log, ads[1]):
1818                self.log.error("Client have no Internet access.")
1819                return False
1820
1821            self.log.info("Enable doze mode on provider: <{}>.".format(ads[
1822                0].serial))
1823            if not enable_doze(ads[0]):
1824                self.log.error("Failed to enable doze mode.")
1825                return False
1826            time.sleep(60)
1827            if not verify_http_connection(self.log, ads[1]):
1828                self.log.error("Client have no Internet access.")
1829                return False
1830        finally:
1831            self.log.info("Disable doze mode.")
1832            if not disable_doze(ads[0]):
1833                self.log.error("Failed to disable doze mode.")
1834                return False
1835            if not wifi_tethering_cleanup(self.log, ads[0], [ads[1]]):
1836                return False
1837        return True
1838
1839    @TelephonyBaseTest.tel_test_wrap
1840    def test_msim_switch_data_sim_2g(self):
1841        """Switch Data SIM on 2G network.
1842
1843        Steps:
1844        1. Data on default Data SIM.
1845        2. Switch Data to another SIM. Make sure data is still available.
1846        3. Switch Data back to previous SIM. Make sure data is still available.
1847
1848        Expected Results:
1849        1. Verify Data on Cell
1850        2. Verify Data on Wifi
1851
1852        Returns:
1853            True if success.
1854            False if failed.
1855        """
1856        ad = self.android_devices[0]
1857        current_data_sub_id = ad.droid.subscriptionGetDefaultDataSubId()
1858        current_sim_slot_index = get_slot_index_from_subid(self.log, ad,
1859            current_data_sub_id)
1860        if current_sim_slot_index == SIM1_SLOT_INDEX:
1861            next_sim_slot_index = SIM2_SLOT_INDEX
1862        else:
1863            next_sim_slot_index = SIM1_SLOT_INDEX
1864        next_data_sub_id = get_subid_from_slot_index(self.log, ad,
1865            next_sim_slot_index)
1866        self.log.info("Current Data is on subId: {}, SIM slot: {}".format(
1867            current_data_sub_id, current_sim_slot_index))
1868        if not ensure_network_generation_for_subscription(
1869            self.log, ad, ad.droid.subscriptionGetDefaultDataSubId(), GEN_2G,
1870            voice_or_data=NETWORK_SERVICE_DATA):
1871            self.log.error("Device data does not attach to 2G.")
1872            return False
1873        if not verify_http_connection(self.log, ad):
1874            self.log.error("No Internet access on default Data SIM.")
1875            return False
1876
1877        self.log.info("Change Data to subId: {}, SIM slot: {}".format(
1878            next_data_sub_id, next_sim_slot_index))
1879        if not change_data_sim_and_verify_data(self.log, ad, next_sim_slot_index):
1880            self.log.error("Failed to change data SIM.")
1881            return False
1882
1883        next_data_sub_id = current_data_sub_id
1884        next_sim_slot_index = current_sim_slot_index
1885        self.log.info("Change Data back to subId: {}, SIM slot: {}".format(
1886            next_data_sub_id, next_sim_slot_index))
1887        if not change_data_sim_and_verify_data(self.log, ad, next_sim_slot_index):
1888            self.log.error("Failed to change data SIM.")
1889            return False
1890
1891        return True
1892
1893    def _test_wifi_connect_disconnect(self):
1894        """Perform multiple connects and disconnects from WiFi and verify that
1895            data switches between WiFi and Cell.
1896
1897        Steps:
1898        1. Reset Wifi on DUT
1899        2. Connect DUT to a WiFi AP
1900        3. Repeat steps 1-2, alternately disconnecting and disabling wifi
1901
1902        Expected Results:
1903        1. Verify Data on Cell
1904        2. Verify Data on Wifi
1905
1906        Returns:
1907            True if success.
1908            False if failed.
1909        """
1910        ad = self.android_devices[0]
1911
1912        wifi_toggles = [True, False, True, False, False, True, False, False,
1913                        False, False, True, False, False, False, False, False,
1914                        False, False, False]
1915
1916        for toggle in wifi_toggles:
1917
1918            WifiUtils.wifi_reset(self.log, ad, toggle)
1919
1920            if not wait_for_cell_data_connection(
1921                    self.log, ad, True, MAX_WAIT_TIME_WIFI_CONNECTION):
1922                self.log.error("Failed wifi connection, aborting!")
1923                return False
1924
1925            if not verify_http_connection(self.log, ad,
1926                                          'http://www.google.com', 100, .1):
1927                self.log.error("Failed to get user-plane traffic, aborting!")
1928                return False
1929
1930            if toggle:
1931                WifiUtils.wifi_toggle_state(self.log, ad, True)
1932
1933            WifiUtils.wifi_connect(self.log, ad, self.wifi_network_ssid,
1934                                   self.wifi_network_pass)
1935
1936            if not wait_for_wifi_data_connection(
1937                    self.log, ad, True, MAX_WAIT_TIME_WIFI_CONNECTION):
1938                self.log.error("Failed wifi connection, aborting!")
1939                return False
1940
1941            if not verify_http_connection(self.log, ad,
1942                                          'http://www.google.com', 100, .1):
1943                self.log.error("Failed to get user-plane traffic, aborting!")
1944                return False
1945        return True
1946
1947    @TelephonyBaseTest.tel_test_wrap
1948    def test_wifi_connect_disconnect_4g(self):
1949        """Perform multiple connects and disconnects from WiFi and verify that
1950            data switches between WiFi and Cell.
1951
1952        Steps:
1953        1. DUT Cellular Data is on 4G. Reset Wifi on DUT
1954        2. Connect DUT to a WiFi AP
1955        3. Repeat steps 1-2, alternately disconnecting and disabling wifi
1956
1957        Expected Results:
1958        1. Verify Data on Cell
1959        2. Verify Data on Wifi
1960
1961        Returns:
1962            True if success.
1963            False if failed.
1964        """
1965
1966        ad = self.android_devices[0]
1967        if not ensure_network_generation_for_subscription(self.log, ad,
1968            ad.droid.subscriptionGetDefaultDataSubId(), GEN_4G,
1969            MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
1970            self.log.error("Device {} failed to reselect in {}s.".format(
1971                ad.serial, MAX_WAIT_TIME_NW_SELECTION))
1972            return False
1973        return self._test_wifi_connect_disconnect()
1974
1975    @TelephonyBaseTest.tel_test_wrap
1976    def test_wifi_connect_disconnect_3g(self):
1977        """Perform multiple connects and disconnects from WiFi and verify that
1978            data switches between WiFi and Cell.
1979
1980        Steps:
1981        1. DUT Cellular Data is on 3G. Reset Wifi on DUT
1982        2. Connect DUT to a WiFi AP
1983        3. Repeat steps 1-2, alternately disconnecting and disabling wifi
1984
1985        Expected Results:
1986        1. Verify Data on Cell
1987        2. Verify Data on Wifi
1988
1989        Returns:
1990            True if success.
1991            False if failed.
1992        """
1993
1994        ad = self.android_devices[0]
1995        if not ensure_network_generation_for_subscription(self.log, ad,
1996            ad.droid.subscriptionGetDefaultDataSubId(), GEN_3G,
1997            MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
1998            self.log.error("Device {} failed to reselect in {}s.".format(
1999                ad.serial, MAX_WAIT_TIME_NW_SELECTION))
2000            return False
2001        return self._test_wifi_connect_disconnect()
2002
2003    @TelephonyBaseTest.tel_test_wrap
2004    def test_wifi_connect_disconnect_2g(self):
2005        """Perform multiple connects and disconnects from WiFi and verify that
2006            data switches between WiFi and Cell.
2007
2008        Steps:
2009        1. DUT Cellular Data is on 2G. Reset Wifi on DUT
2010        2. Connect DUT to a WiFi AP
2011        3. Repeat steps 1-2, alternately disconnecting and disabling wifi
2012
2013        Expected Results:
2014        1. Verify Data on Cell
2015        2. Verify Data on Wifi
2016
2017        Returns:
2018            True if success.
2019            False if failed.
2020        """
2021        ad = self.android_devices[0]
2022        if not ensure_network_generation_for_subscription(self.log, ad,
2023            ad.droid.subscriptionGetDefaultDataSubId(), GEN_2G,
2024            MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
2025            self.log.error("Device {} failed to reselect in {}s.".format(
2026                ad.serial, MAX_WAIT_TIME_NW_SELECTION))
2027            return False
2028        return self._test_wifi_connect_disconnect()
2029
2030    def _test_wifi_tethering_enabled_add_voice_call(self, network_generation,
2031        voice_call_direction, is_data_available_during_call):
2032        """Tethering enabled + voice call.
2033
2034        Steps:
2035        1. DUT data is on <network_generation>. Start WiFi Tethering.
2036        2. PhoneB connect to DUT's softAP
2037        3. DUT make a MO/MT (<voice_call_direction>) phone call.
2038        4. DUT end phone call.
2039
2040        Expected Results:
2041        1. DUT is able to start WiFi tethering.
2042        2. PhoneB connected to DUT's softAP and able to browse Internet.
2043        3. DUT WiFi tethering is still on. Phone call works OK.
2044            If is_data_available_during_call is True, then PhoneB still has
2045            Internet access.
2046            Else, then Data is suspend, PhoneB has no Internet access.
2047        4. WiFi Tethering still on, voice call stopped, and PhoneB have Internet
2048            access.
2049
2050        Returns:
2051            True if success.
2052            False if failed.
2053        """
2054        ads = self.android_devices
2055        if not self._test_setup_tethering(ads, network_generation):
2056            self.log.error("Verify Internet access failed.")
2057            return False
2058        try:
2059            # Start WiFi Tethering
2060            if not wifi_tethering_setup_teardown(
2061                    self.log,
2062                    ads[0],
2063                    [ads[1]],
2064                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
2065                    check_interval=10,
2066                    check_iteration=2,
2067                    do_cleanup=False):
2068                self.log.error("WiFi Tethering failed.")
2069                return False
2070
2071            if not ads[0].droid.wifiIsApEnabled():
2072                self.log.error("Provider WiFi tethering stopped.")
2073                return False
2074
2075            # Make a voice call
2076            if voice_call_direction == DIRECTION_MOBILE_ORIGINATED:
2077                ad_caller = ads[0]
2078                ad_callee = ads[1]
2079            else:
2080                ad_caller = ads[1]
2081                ad_callee = ads[0]
2082            if not call_setup_teardown(self.log, ad_caller, ad_callee, None,
2083                                       None, None):
2084                self.log.error("Failed to Establish {} Voice Call".format(
2085                    voice_call_direction))
2086                return False
2087
2088            # Tethering should still be on.
2089            if not ads[0].droid.wifiIsApEnabled():
2090                self.log.error("Provider WiFi tethering stopped.")
2091                return False
2092            if not is_data_available_during_call:
2093                if verify_http_connection(self.log, ads[1], retry=0):
2094                    self.log.error("Client should not have Internet Access.")
2095                    return False
2096            else:
2097                if not verify_http_connection(self.log, ads[1]):
2098                    self.log.error("Client should have Internet Access.")
2099                    return False
2100
2101            # Hangup call. Client should have data.
2102            if not hangup_call(self.log, ads[0]):
2103                self.log.error("Failed to hang up call")
2104                return False
2105            if not ads[0].droid.wifiIsApEnabled():
2106                self.log.error("Provider WiFi tethering stopped.")
2107                return False
2108            if not verify_http_connection(self.log, ads[1]):
2109                self.log.error("Client should have Internet Access.")
2110                return False
2111        finally:
2112            ads[1].droid.telephonyToggleDataConnection(True)
2113            WifiUtils.wifi_reset(self.log, ads[1])
2114            if ads[0].droid.wifiIsApEnabled():
2115                WifiUtils.stop_wifi_tethering(self.log, ads[0])
2116        return True
2117
2118    @TelephonyBaseTest.tel_test_wrap
2119    def test_wifi_tethering_enabled_add_mo_voice_call_2g_dsds(self):
2120        """Tethering enabled + voice call
2121
2122        Steps:
2123        1. DUT is DSDS device, Data on 2G. Start WiFi Tethering on <Data SIM>
2124        2. PhoneB connect to DUT's softAP
2125        3. DUT make a mo phone call on <Voice SIM>
2126        4. DUT end phone call.
2127
2128        Expected Results:
2129        1. DUT is able to start WiFi tethering.
2130        2. PhoneB connected to DUT's softAP and able to browse Internet.
2131        3. DUT WiFi tethering is still on. Phone call works OK. Data is suspend,
2132            PhoneB still connected to DUT's softAP, but no data available.
2133        4. DUT data resumes, and PhoneB have Internet access.
2134
2135        Returns:
2136            True if success.
2137            False if failed.
2138        """
2139
2140        return self._test_wifi_tethering_enabled_add_voice_call(GEN_2G,
2141            DIRECTION_MOBILE_ORIGINATED, False)
2142
2143    @TelephonyBaseTest.tel_test_wrap
2144    def test_wifi_tethering_enabled_add_mt_voice_call_2g_dsds(self):
2145        """Tethering enabled + voice call
2146
2147        Steps:
2148        1. DUT is DSDS device, Data on 2G. Start WiFi Tethering on <Data SIM>
2149        2. PhoneB connect to DUT's softAP
2150        3. DUT make a mt phone call on <Voice SIM>
2151        4. DUT end phone call.
2152
2153        Expected Results:
2154        1. DUT is able to start WiFi tethering.
2155        2. PhoneB connected to DUT's softAP and able to browse Internet.
2156        3. DUT WiFi tethering is still on. Phone call works OK. Data is suspend,
2157            PhoneB still connected to DUT's softAP, but no data available.
2158        4. DUT data resumes, and PhoneB have Internet access.
2159
2160        Returns:
2161            True if success.
2162            False if failed.
2163        """
2164
2165        return self._test_wifi_tethering_enabled_add_voice_call(GEN_2G,
2166            DIRECTION_MOBILE_TERMINATED, False)
2167
2168    @TelephonyBaseTest.tel_test_wrap
2169    def test_wifi_tethering_msim_switch_data_sim(self):
2170        """Tethering enabled + switch data SIM.
2171
2172        Steps:
2173        1. Start WiFi Tethering on <Default Data SIM>
2174        2. PhoneB connect to DUT's softAP
2175        3. DUT change Default Data SIM.
2176
2177        Expected Results:
2178        1. DUT is able to start WiFi tethering.
2179        2. PhoneB connected to DUT's softAP and able to browse Internet.
2180        3. DUT Data changed to 2nd SIM, WiFi tethering should continues,
2181            PhoneB should have Internet access.
2182
2183        Returns:
2184            True if success.
2185            False if failed.
2186        """
2187        ads = self.android_devices
2188        current_data_sub_id = ads[0].droid.subscriptionGetDefaultDataSubId()
2189        current_sim_slot_index = get_slot_index_from_subid(self.log, ads[0],
2190            current_data_sub_id)
2191        self.log.info("Current Data is on subId: {}, SIM slot: {}".format(
2192            current_data_sub_id, current_sim_slot_index))
2193        if not self._test_setup_tethering(ads):
2194            self.log.error("Verify Internet access failed.")
2195            return False
2196        try:
2197            # Start WiFi Tethering
2198            if not wifi_tethering_setup_teardown(
2199                    self.log,
2200                    ads[0],
2201                    [ads[1]],
2202                    ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
2203                    check_interval=10,
2204                    check_iteration=2,
2205                    do_cleanup=False):
2206                self.log.error("WiFi Tethering failed.")
2207                return False
2208            for i in range(0, 2):
2209                next_sim_slot_index = \
2210                    {SIM1_SLOT_INDEX : SIM2_SLOT_INDEX,
2211                     SIM2_SLOT_INDEX : SIM1_SLOT_INDEX}[current_sim_slot_index]
2212                self.log.info("Change Data to SIM slot: {}".
2213                    format(next_sim_slot_index))
2214                if not change_data_sim_and_verify_data(self.log, ads[0],
2215                    next_sim_slot_index):
2216                    self.log.error("Failed to change data SIM.")
2217                    return False
2218                current_sim_slot_index = next_sim_slot_index
2219                if not verify_http_connection(self.log, ads[1]):
2220                    self.log.error("Client should have Internet Access.")
2221                    return False
2222        finally:
2223            ads[1].droid.telephonyToggleDataConnection(True)
2224            WifiUtils.wifi_reset(self.log, ads[1])
2225            if ads[0].droid.wifiIsApEnabled():
2226                WifiUtils.stop_wifi_tethering(self.log, ads[0])
2227        return True
2228
2229    @TelephonyBaseTest.tel_test_wrap
2230    def test_msim_cell_data_switch_to_wifi_switch_data_sim_2g(self):
2231        """Switch Data SIM on 2G network.
2232
2233        Steps:
2234        1. Data on default Data SIM.
2235        2. Turn on WiFi, then data should be on WiFi.
2236        3. Switch Data to another SIM. Disable WiFi.
2237
2238        Expected Results:
2239        1. Verify Data on Cell
2240        2. Verify Data on WiFi
2241        3. After WiFi disabled, Cell Data is available on 2nd SIM.
2242
2243        Returns:
2244            True if success.
2245            False if failed.
2246        """
2247        ad = self.android_devices[0]
2248        current_data_sub_id = ad.droid.subscriptionGetDefaultDataSubId()
2249        current_sim_slot_index = get_slot_index_from_subid(self.log, ad,
2250                                                           current_data_sub_id)
2251        if current_sim_slot_index == SIM1_SLOT_INDEX:
2252            next_sim_slot_index = SIM2_SLOT_INDEX
2253        else:
2254            next_sim_slot_index = SIM1_SLOT_INDEX
2255        next_data_sub_id = get_subid_from_slot_index(self.log, ad,
2256                                                     next_sim_slot_index)
2257        self.log.info("Current Data is on subId: {}, SIM slot: {}".format(
2258            current_data_sub_id, current_sim_slot_index))
2259        if not ensure_network_generation_for_subscription(
2260                self.log,
2261                ad,
2262                ad.droid.subscriptionGetDefaultDataSubId(),
2263                GEN_2G,
2264                voice_or_data=NETWORK_SERVICE_DATA):
2265            self.log.error("Device data does not attach to 2G.")
2266            return False
2267        if not verify_http_connection(self.log, ad):
2268            self.log.error("No Internet access on default Data SIM.")
2269            return False
2270
2271        self.log.info("Connect to WiFi and verify Internet access.")
2272        if not ensure_wifi_connected(self.log, ad, self.wifi_network_ssid,
2273                                     self.wifi_network_pass):
2274            self.log.error("WiFi connect fail.")
2275            return False
2276        if (not wait_for_wifi_data_connection(self.log, ad, True) or
2277                not verify_http_connection(self.log, ad)):
2278            self.log.error("Data is not on WiFi")
2279            return False
2280
2281        try:
2282            self.log.info(
2283                "Change Data SIM, Disable WiFi and verify Internet access.")
2284            set_subid_for_data(ad, next_data_sub_id)
2285            WifiUtils.wifi_toggle_state(self.log, ad, False)
2286            if not wait_for_data_attach_for_subscription(
2287                    self.log, ad, next_data_sub_id,
2288                    MAX_WAIT_TIME_NW_SELECTION):
2289                self.log.error("Failed to attach data on subId:{}".format(
2290                    next_data_sub_id))
2291                return False
2292            if not verify_http_connection(self.log, ad):
2293                self.log.error("No Internet access after changing Data SIM.")
2294                return False
2295
2296        finally:
2297            self.log.info("Change Data SIM back.")
2298            set_subid_for_data(ad, current_data_sub_id)
2299
2300        return True
2301
2302    @TelephonyBaseTest.tel_test_wrap
2303    def test_disable_data_on_non_active_data_sim(self):
2304        """Switch Data SIM on 2G network.
2305
2306        Steps:
2307        1. Data on default Data SIM.
2308        2. Disable data on non-active Data SIM.
2309
2310        Expected Results:
2311        1. Verify Data Status on Default Data SIM and non-active Data SIM.
2312        1. Verify Data Status on Default Data SIM and non-active Data SIM.
2313
2314        Returns:
2315            True if success.
2316            False if failed.
2317        """
2318        ad = self.android_devices[0]
2319        current_data_sub_id = ad.droid.subscriptionGetDefaultDataSubId()
2320        current_sim_slot_index = get_slot_index_from_subid(self.log, ad,
2321                                                           current_data_sub_id)
2322        if current_sim_slot_index == SIM1_SLOT_INDEX:
2323            non_active_sim_slot_index = SIM2_SLOT_INDEX
2324        else:
2325            non_active_sim_slot_index = SIM1_SLOT_INDEX
2326        non_active_sub_id = get_subid_from_slot_index(
2327            self.log, ad, non_active_sim_slot_index)
2328        self.log.info("Current Data is on subId: {}, SIM slot: {}".format(
2329            current_data_sub_id, current_sim_slot_index))
2330
2331        if not ensure_network_generation_for_subscription(
2332                self.log,
2333                ad,
2334                ad.droid.subscriptionGetDefaultDataSubId(),
2335                GEN_2G,
2336                voice_or_data=NETWORK_SERVICE_DATA):
2337            self.log.error("Device data does not attach to 2G.")
2338            return False
2339        if not verify_http_connection(self.log, ad):
2340            self.log.error("No Internet access on default Data SIM.")
2341            return False
2342
2343        if ad.droid.telephonyGetDataConnectionState() != DATA_STATE_CONNECTED:
2344            self.log.error("Data Connection State should be connected.")
2345            return False
2346        # TODO: Check Data state for non-active subId.
2347
2348        try:
2349            self.log.info("Disable Data on Non-Active Sub ID")
2350            ad.droid.telephonyToggleDataConnectionForSubscription(
2351                non_active_sub_id, False)
2352            # TODO: Check Data state for non-active subId.
2353            if ad.droid.telephonyGetDataConnectionState(
2354            ) != DATA_STATE_CONNECTED:
2355                self.log.error("Data Connection State should be connected.")
2356                return False
2357        finally:
2358            self.log.info("Enable Data on Non-Active Sub ID")
2359            ad.droid.telephonyToggleDataConnectionForSubscription(
2360                non_active_sub_id, True)
2361        return True
2362        """ Tests End """
2363