• 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
17import time
18
19from acts.utils import rand_ascii_str
20from acts.test_utils.tel.tel_subscription_utils import \
21    get_subid_from_slot_index
22from acts.test_utils.tel.tel_subscription_utils import set_subid_for_data
23from acts.test_utils.tel.tel_defines import MAX_WAIT_TIME_NW_SELECTION
24from acts.test_utils.tel.tel_defines import NETWORK_SERVICE_DATA
25from acts.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING
26from acts.test_utils.tel.tel_subscription_utils import get_default_data_sub_id
27from acts.test_utils.tel.tel_test_utils import WifiUtils
28from acts.test_utils.tel.tel_test_utils import ensure_network_generation_for_subscription
29from acts.test_utils.tel.tel_test_utils import ensure_phones_idle
30from acts.test_utils.tel.tel_test_utils import ensure_wifi_connected
31from acts.test_utils.tel.tel_test_utils import get_network_rat_for_subscription
32from acts.test_utils.tel.tel_test_utils import is_droid_in_network_generation_for_subscription
33from acts.test_utils.tel.tel_test_utils import rat_generation_from_rat
34from acts.test_utils.tel.tel_test_utils import toggle_airplane_mode
35from acts.test_utils.tel.tel_test_utils import verify_http_connection
36from acts.test_utils.tel.tel_test_utils import wait_for_cell_data_connection
37from acts.test_utils.tel.tel_test_utils import wait_for_wifi_data_connection
38from acts.test_utils.tel.tel_test_utils import wait_for_data_attach_for_subscription
39
40
41def wifi_tethering_cleanup(log, provider, client_list):
42    """Clean up steps for WiFi Tethering.
43
44    Make sure provider turn off tethering.
45    Make sure clients reset WiFi and turn on cellular data.
46
47    Args:
48        log: log object.
49        provider: android object provide WiFi tethering.
50        client_list: a list of clients using tethered WiFi.
51
52    Returns:
53        True if no error happened. False otherwise.
54    """
55    for client in client_list:
56        client.droid.telephonyToggleDataConnection(True)
57        if not WifiUtils.wifi_reset(log, client):
58            log.error("Reset client WiFi failed. {}".format(client.serial))
59            return False
60    if not provider.droid.wifiIsApEnabled():
61        log.error("Provider WiFi tethering stopped.")
62        return False
63    if not WifiUtils.stop_wifi_tethering(log, provider):
64        log.error("Provider strop WiFi tethering failed.")
65        return False
66    return True
67
68
69def wifi_tethering_setup_teardown(log,
70                                  provider,
71                                  client_list,
72                                  ap_band=WifiUtils.WIFI_CONFIG_APBAND_2G,
73                                  check_interval=30,
74                                  check_iteration=4,
75                                  do_cleanup=True,
76                                  ssid=None,
77                                  password=None):
78    """Test WiFi Tethering.
79
80    Turn off WiFi on clients.
81    Turn off data and reset WiFi on clients.
82    Verify no Internet access on clients.
83    Turn on WiFi tethering on provider.
84    Clients connect to provider's WiFI.
85    Verify Internet on provider and clients.
86    Tear down WiFi tethering setup and clean up.
87
88    Args:
89        log: log object.
90        provider: android object provide WiFi tethering.
91        client_list: a list of clients using tethered WiFi.
92        ap_band: setup WiFi tethering on 2G or 5G.
93            This is optional, default value is WifiUtils.WIFI_CONFIG_APBAND_2G
94        check_interval: delay time between each around of Internet connection check.
95            This is optional, default value is 30 (seconds).
96        check_iteration: check Internet connection for how many times in total.
97            This is optional, default value is 4 (4 times).
98        do_cleanup: after WiFi tethering test, do clean up to tear down tethering
99            setup or not. This is optional, default value is True.
100        ssid: use this string as WiFi SSID to setup tethered WiFi network.
101            This is optional. Default value is None.
102            If it's None, a random string will be generated.
103        password: use this string as WiFi password to setup tethered WiFi network.
104            This is optional. Default value is None.
105            If it's None, a random string will be generated.
106
107    Returns:
108        True if no error happened. False otherwise.
109    """
110    log.info("--->Start wifi_tethering_setup_teardown<---")
111    log.info("Provider: {}".format(provider.serial))
112    if not provider.droid.connectivityIsTetheringSupported():
113        log.error("Provider does not support tethering. Stop tethering test.")
114        return False
115
116    if ssid is None:
117        ssid = rand_ascii_str(10)
118    if password is None:
119        password = rand_ascii_str(8)
120
121    # No password
122    if password == "":
123        password = None
124
125    try:
126        for client in client_list:
127            log.info("Client: {}".format(client.serial))
128            WifiUtils.wifi_toggle_state(log, client, False)
129            client.droid.telephonyToggleDataConnection(False)
130        log.info("WiFI Tethering: Verify client have no Internet access.")
131        for client in client_list:
132            if verify_http_connection(log, client):
133                log.error("Turn off Data on client fail. {}".format(
134                    client.serial))
135                return False
136
137        log.info(
138            "WiFI Tethering: Turn on WiFi tethering on {}. SSID: {}, password: {}".format(
139                provider.serial, ssid, password))
140
141        if not WifiUtils.start_wifi_tethering(log, provider, ssid, password,
142                                              ap_band):
143            log.error("Provider start WiFi tethering failed.")
144            return False
145        time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
146
147        log.info("Provider {} check Internet connection.".format(
148            provider.serial))
149        if not verify_http_connection(log, provider):
150            return False
151        for client in client_list:
152            log.info(
153                "WiFI Tethering: {} connect to WiFi and verify AP band correct.".format(
154                    client.serial))
155            if not ensure_wifi_connected(log, client, ssid, password):
156                log.error("Client connect to WiFi failed.")
157                return False
158
159            wifi_info = client.droid.wifiGetConnectionInfo()
160            if ap_band == WifiUtils.WIFI_CONFIG_APBAND_5G:
161                if wifi_info["is_24ghz"]:
162                    log.error("Expected 5g network. WiFi Info: {}".format(
163                        wifi_info))
164                    return False
165            else:
166                if wifi_info["is_5ghz"]:
167                    log.error("Expected 2g network. WiFi Info: {}".format(
168                        wifi_info))
169                    return False
170
171            log.info("Client{} check Internet connection.".format(
172                client.serial))
173            if (not wait_for_wifi_data_connection(log, client, True) or
174                    not verify_http_connection(log, client)):
175                log.error("No WiFi Data on client: {}.".format(client.serial))
176                return False
177
178        if not tethering_check_internet_connection(
179                log, provider, client_list, check_interval, check_iteration):
180            return False
181
182    finally:
183        if (do_cleanup and
184            (not wifi_tethering_cleanup(log, provider, client_list))):
185            return False
186    return True
187
188
189def tethering_check_internet_connection(log, provider, client_list,
190                                        check_interval, check_iteration):
191    """During tethering test, check client(s) and provider Internet connection.
192
193    Do the following for <check_iteration> times:
194        Delay <check_interval> seconds.
195        Check Tethering provider's Internet connection.
196        Check each client's Internet connection.
197
198    Args:
199        log: log object.
200        provider: android object provide WiFi tethering.
201        client_list: a list of clients using tethered WiFi.
202        check_interval: delay time between each around of Internet connection check.
203        check_iteration: check Internet connection for how many times in total.
204
205    Returns:
206        True if no error happened. False otherwise.
207    """
208    for i in range(1, check_iteration):
209        time.sleep(check_interval)
210        log.info(
211            "Provider {} check Internet connection after {} seconds.".format(
212                provider.serial, check_interval * i))
213        if not verify_http_connection(log, provider):
214            return False
215        for client in client_list:
216            log.info(
217                "Client {} check Internet connection after {} seconds.".format(
218                    client.serial, check_interval * i))
219            if not verify_http_connection(log, client):
220                return False
221    return True
222
223
224def wifi_cell_switching(log, ad, wifi_network_ssid, wifi_network_pass, nw_gen):
225    """Test data connection network switching when phone on <nw_gen>.
226
227    Ensure phone is on <nw_gen>
228    Ensure WiFi can connect to live network,
229    Airplane mode is off, data connection is on, WiFi is on.
230    Turn off WiFi, verify data is on cell and browse to google.com is OK.
231    Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
232    Turn off WiFi, verify data is on cell and browse to google.com is OK.
233
234    Args:
235        log: log object.
236        ad: android object.
237        wifi_network_ssid: ssid for live wifi network.
238        wifi_network_pass: password for live wifi network.
239        nw_gen: network generation the phone should be camped on.
240
241    Returns:
242        True if pass.
243    """
244    try:
245
246        if not ensure_network_generation_for_subscription(
247                log, ad, get_default_data_sub_id(ad), nw_gen,
248                MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
249            log.error("Device failed to register in {}".format(nw_gen))
250            return False
251
252        # Ensure WiFi can connect to live network
253        log.info("Make sure phone can connect to live network by WIFI")
254        if not ensure_wifi_connected(log, ad, wifi_network_ssid,
255                                     wifi_network_pass):
256            log.error("WiFi connect fail.")
257            return False
258        log.info("Phone connected to WIFI.")
259
260        log.info("Step1 Airplane Off, WiFi On, Data On.")
261        toggle_airplane_mode(log, ad, False)
262        WifiUtils.wifi_toggle_state(log, ad, True)
263        ad.droid.telephonyToggleDataConnection(True)
264        if (not wait_for_wifi_data_connection(log, ad, True) or
265                not verify_http_connection(log, ad)):
266            log.error("Data is not on WiFi")
267            return False
268
269        log.info("Step2 WiFi is Off, Data is on Cell.")
270        WifiUtils.wifi_toggle_state(log, ad, False)
271        if (not wait_for_cell_data_connection(log, ad, True) or
272                not verify_http_connection(log, ad)):
273            log.error("Data did not return to cell")
274            return False
275
276        log.info("Step3 WiFi is On, Data is on WiFi.")
277        WifiUtils.wifi_toggle_state(log, ad, True)
278        if (not wait_for_wifi_data_connection(log, ad, True) or
279                not verify_http_connection(log, ad)):
280            log.error("Data did not return to WiFi")
281            return False
282
283        log.info("Step4 WiFi is Off, Data is on Cell.")
284        WifiUtils.wifi_toggle_state(log, ad, False)
285        if (not wait_for_cell_data_connection(log, ad, True) or
286                not verify_http_connection(log, ad)):
287            log.error("Data did not return to cell")
288            return False
289        return True
290
291    finally:
292        WifiUtils.wifi_toggle_state(log, ad, False)
293
294
295def airplane_mode_test(log, ad):
296    """ Test airplane mode basic on Phone and Live SIM.
297
298    Ensure phone attach, data on, WiFi off and verify Internet.
299    Turn on airplane mode to make sure detach.
300    Turn off airplane mode to make sure attach.
301    Verify Internet connection.
302
303    Args:
304        log: log object.
305        ad: android object.
306
307    Returns:
308        True if pass; False if fail.
309    """
310    if not ensure_phones_idle(log, [ad]):
311        log.error("Failed to return phones to idle.")
312        return False
313
314    try:
315        ad.droid.telephonyToggleDataConnection(True)
316        WifiUtils.wifi_toggle_state(log, ad, False)
317
318        log.info("Step1: ensure attach")
319        if not toggle_airplane_mode(log, ad, False):
320            log.error("Failed initial attach")
321            return False
322        if not verify_http_connection(log, ad):
323            log.error("Data not available on cell.")
324            return False
325
326        log.info("Step2: enable airplane mode and ensure detach")
327        if not toggle_airplane_mode(log, ad, True):
328            log.error("Failed to enable Airplane Mode")
329            return False
330        if not wait_for_cell_data_connection(log, ad, False):
331            log.error("Failed to disable cell data connection")
332            return False
333        if verify_http_connection(log, ad):
334            log.error("Data available in airplane mode.")
335            return False
336
337        log.info("Step3: disable airplane mode and ensure attach")
338        if not toggle_airplane_mode(log, ad, False):
339            log.error("Failed to disable Airplane Mode")
340            return False
341
342        if not wait_for_cell_data_connection(log, ad, True):
343            log.error("Failed to enable cell data connection")
344            return False
345
346        time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
347
348        log.info("Step4 verify internet")
349        return verify_http_connection(log, ad)
350    finally:
351        toggle_airplane_mode(log, ad, False)
352
353
354def data_connectivity_single_bearer(log, ad, nw_gen):
355    """Test data connection: single-bearer (no voice).
356
357    Turn off airplane mode, enable Cellular Data.
358    Ensure phone data generation is expected.
359    Verify Internet.
360    Disable Cellular Data, verify Internet is inaccessible.
361    Enable Cellular Data, verify Internet.
362
363    Args:
364        log: log object.
365        ad: android object.
366        nw_gen: network generation the phone should on.
367
368    Returns:
369        True if success.
370        False if failed.
371    """
372    ensure_phones_idle(log, [ad])
373
374    if not ensure_network_generation_for_subscription(
375            log, ad, get_default_data_sub_id(ad), nw_gen,
376            MAX_WAIT_TIME_NW_SELECTION, NETWORK_SERVICE_DATA):
377        log.error("Device failed to reselect in {}s.".format(
378            MAX_WAIT_TIME_NW_SELECTION))
379        return False
380
381    try:
382        log.info("Step1 Airplane Off, Data On.")
383        toggle_airplane_mode(log, ad, False)
384        ad.droid.telephonyToggleDataConnection(True)
385        if not wait_for_cell_data_connection(log, ad, True):
386            log.error("Failed to enable data connection.")
387            return False
388
389        log.info("Step2 Verify internet")
390        if not verify_http_connection(log, ad):
391            log.error("Data not available on cell.")
392            return False
393
394        log.info("Step3 Turn off data and verify not connected.")
395        ad.droid.telephonyToggleDataConnection(False)
396        if not wait_for_cell_data_connection(log, ad, False):
397            log.error("Step3 Failed to disable data connection.")
398            return False
399
400        if verify_http_connection(log, ad):
401            log.error("Step3 Data still available when disabled.")
402            return False
403
404        log.info("Step4 Re-enable data.")
405        ad.droid.telephonyToggleDataConnection(True)
406        if not wait_for_cell_data_connection(log, ad, True):
407            log.error("Step4 failed to re-enable data.")
408            return False
409        if not verify_http_connection(log, ad):
410            log.error("Data not available on cell.")
411            return False
412
413        if not is_droid_in_network_generation_for_subscription(
414                log, ad, get_default_data_sub_id(ad), nw_gen,
415                NETWORK_SERVICE_DATA):
416            log.error("Failed: droid is no longer on correct network")
417            log.info("Expected:{}, Current:{}".format(
418                nw_gen, rat_generation_from_rat(
419                    get_network_rat_for_subscription(
420                        log, ad, get_default_data_sub_id(
421                            ad), NETWORK_SERVICE_DATA))))
422            return False
423        return True
424    finally:
425        ad.droid.telephonyToggleDataConnection(True)
426
427
428def change_data_sim_and_verify_data(log, ad, sim_slot):
429    """Change Data SIM and verify Data attach and Internet access
430
431    Args:
432        log: log object.
433        ad: android device object.
434        sim_slot: SIM slot index.
435
436    Returns:
437        Data SIM changed successfully, data attached and Internet access is OK.
438    """
439    sub_id = get_subid_from_slot_index(log, ad, sim_slot)
440    log.info("Change Data to subId: {}, SIM slot: {}".format(sub_id, sim_slot))
441    set_subid_for_data(ad, sub_id)
442    if not wait_for_data_attach_for_subscription(log, ad, sub_id,
443                                                 MAX_WAIT_TIME_NW_SELECTION):
444        log.error("Failed to attach data on subId:{}".format(sub_id))
445        return False
446    if not verify_http_connection(log, ad):
447        log.error("No Internet access after changing Data SIM.")
448        return False
449    return True
450