1#!/usr/bin/env python3.4
2#
3#   Copyright 2022 - 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 5G Data scenarios
18"""
19
20import time
21
22from acts.test_decorators import test_tracker_info
23from acts_contrib.test_utils.tel.TelephonyBaseTest import TelephonyBaseTest
24from acts_contrib.test_utils.tel.tel_defines import GEN_5G
25from acts_contrib.test_utils.tel.tel_defines import MAX_WAIT_TIME_USER_PLANE_DATA
26from acts_contrib.test_utils.tel.tel_defines import NETWORK_MODE_NR_LTE_GSM_WCDMA
27from acts_contrib.test_utils.tel.tel_defines import NetworkCallbackCapabilitiesChanged
28from acts_contrib.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING
29from acts_contrib.test_utils.tel.tel_data_utils import browsing_test
30from acts_contrib.test_utils.tel.tel_data_utils import check_data_stall_detection
31from acts_contrib.test_utils.tel.tel_data_utils import check_data_stall_recovery
32from acts_contrib.test_utils.tel.tel_data_utils import check_network_validation_fail
33from acts_contrib.test_utils.tel.tel_data_utils import data_connectivity_single_bearer
34from acts_contrib.test_utils.tel.tel_data_utils import test_data_connectivity_multi_bearer
35from acts_contrib.test_utils.tel.tel_data_utils import test_wifi_connect_disconnect
36from acts_contrib.test_utils.tel.tel_data_utils import verify_for_network_callback
37from acts_contrib.test_utils.tel.tel_data_utils import wifi_cell_switching
38from acts_contrib.test_utils.tel.tel_data_utils import airplane_mode_test
39from acts_contrib.test_utils.tel.tel_data_utils import reboot_test
40from acts_contrib.test_utils.tel.tel_5g_utils import is_current_network_5g
41from acts_contrib.test_utils.tel.tel_5g_test_utils import provision_device_for_5g
42from acts_contrib.test_utils.tel.tel_phone_setup_utils import phone_setup_volte
43from acts_contrib.test_utils.tel.tel_test_utils import break_internet_except_sl4a_port
44from acts_contrib.test_utils.tel.tel_test_utils import get_current_override_network_type
45from acts_contrib.test_utils.tel.tel_test_utils import get_device_epoch_time
46from acts_contrib.test_utils.tel.tel_test_utils import iperf_test_by_adb
47from acts_contrib.test_utils.tel.tel_test_utils import iperf_udp_test_by_adb
48from acts_contrib.test_utils.tel.tel_test_utils import resume_internet_with_sl4a_port
49from acts_contrib.test_utils.tel.tel_test_utils import set_preferred_network_mode_pref
50from acts_contrib.test_utils.tel.tel_test_utils import test_data_browsing_failure_using_sl4a
51from acts_contrib.test_utils.tel.tel_test_utils import test_data_browsing_success_using_sl4a
52from acts_contrib.test_utils.tel.tel_test_utils import toggle_airplane_mode
53from acts_contrib.test_utils.tel.tel_test_utils import verify_internet_connection
54from acts_contrib.test_utils.tel.tel_wifi_utils import ensure_wifi_connected
55from acts_contrib.test_utils.tel.tel_wifi_utils import wifi_reset
56from acts_contrib.test_utils.tel.tel_wifi_utils import wifi_toggle_state
57
58
59class Nsa5gDataTest(TelephonyBaseTest):
60    def setup_class(self):
61        super().setup_class()
62        self.iperf_server_ip = self.user_params.get("iperf_server", '0.0.0.0')
63        self.iperf_tcp_port = int(
64            self.user_params.get("iperf_tcp_port", 0))
65        self.iperf_udp_port = int(
66            self.user_params.get("iperf_udp_port", 0))
67        self.iperf_duration = int(
68            self.user_params.get("iperf_duration", 60))
69
70    def setup_test(self):
71        TelephonyBaseTest.setup_test(self)
72        self.provider = self.android_devices[0]
73        self.clients = self.android_devices[1:]
74
75    def teardown_class(self):
76        TelephonyBaseTest.teardown_class(self)
77
78
79    """ Tests Begin """
80
81    @test_tracker_info(uuid="a73b749f-746c-4089-a8ad-4e47aed180f6")
82    @TelephonyBaseTest.tel_test_wrap
83    def test_5g_nsa_data_browsing(self):
84        """ Verifying connectivity of internet and  browsing websites on 5G NSA network.
85
86        Ensure
87            1. ping to IP of websites is successful.
88            2. http ping to IP of websites is successful.
89            3. browsing websites is successful.
90        Returns:
91            True if pass; False if fail.
92        """
93        ad = self.android_devices[0]
94        wifi_toggle_state(ad.log, ad, False)
95        sub_id = ad.droid.subscriptionGetDefaultSubId()
96        if not set_preferred_network_mode_pref(ad.log, ad, sub_id,
97                                               NETWORK_MODE_NR_LTE_GSM_WCDMA):
98            ad.log.error("Failed to set network mode to NSA")
99            return False
100        ad.log.info("Set network mode to NSA successfully")
101        ad.log.info("Waiting for 5g NSA attach for 60 secs")
102        if is_current_network_5g(ad, nr_type = 'nsa', timeout=60):
103            ad.log.info("Success! attached on 5g NSA")
104        else:
105            ad.log.error("Failure - expected NR_NSA, current %s",
106                         get_current_override_network_type(ad))
107            # Can't attach 5g NSA, exit test!
108            return False
109        for iteration in range(3):
110            connectivity = False
111            browsing = False
112            ad.log.info("Attempt %d", iteration + 1)
113            if not verify_internet_connection(self.log, ad):
114                ad.log.error("Failed to connect to internet!")
115            else:
116                ad.log.info("Connect to internet successfully!")
117                connectivity = True
118            if not browsing_test(ad.log, ad):
119                ad.log.error("Failed to browse websites!")
120            else:
121                ad.log.info("Successful to browse websites!")
122                browsing = True
123            if connectivity and browsing:
124                return True
125            time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
126        ad.log.error("5G NSA Connectivity and Data Browsing test FAIL for all 3 iterations")
127        return False
128
129
130    @test_tracker_info(uuid="c7727c26-b588-461f-851b-802bfa3a86af")
131    @TelephonyBaseTest.tel_test_wrap
132    def test_5g_nsa_data_stall_recovery(self):
133        """ Verifies 5G NSA data stall
134
135        Set Mode to 5G
136        Wait for 5G attached on NSA
137        Browse websites for success
138        Trigger data stall and verify browsing fails
139        Resume data and verify browsing success
140
141        Returns:
142            True if pass; False if fail.
143        """
144        ad = self.android_devices[0]
145        result = True
146        wifi_toggle_state(ad.log, ad, False)
147        toggle_airplane_mode(ad.log, ad, False)
148
149        if not provision_device_for_5g(ad.log, ad, nr_type='nsa'):
150            return False
151
152        cmd = ('ss -l -p -n | grep "tcp.*droid_script" | tr -s " " '
153               '| cut -d " " -f 5 | sed s/.*://g')
154        sl4a_port = ad.adb.shell(cmd)
155
156        if not test_data_browsing_success_using_sl4a(ad.log, ad):
157            ad.log.error("Browsing failed before the test, aborting!")
158            return False
159
160        begin_time = get_device_epoch_time(ad)
161        break_internet_except_sl4a_port(ad, sl4a_port)
162
163        if not test_data_browsing_failure_using_sl4a(ad.log, ad):
164            ad.log.error("Browsing after breaking the internet, aborting!")
165            result = False
166
167        if not check_data_stall_detection(ad):
168            ad.log.warning("NetworkMonitor unable to detect Data Stall")
169
170        if not check_network_validation_fail(ad, begin_time):
171            ad.log.warning("Unable to detect NW validation fail")
172
173        if not check_data_stall_recovery(ad, begin_time):
174            ad.log.error("Recovery was not triggered")
175            result = False
176
177        resume_internet_with_sl4a_port(ad, sl4a_port)
178        time.sleep(MAX_WAIT_TIME_USER_PLANE_DATA)
179        if not test_data_browsing_success_using_sl4a(ad.log, ad):
180            ad.log.error("Browsing failed after resuming internet")
181            result = False
182        if result:
183            ad.log.info("PASS - data stall over 5G NSA")
184        else:
185            ad.log.error("FAIL - data stall over 5G NSA")
186        return result
187
188
189    @test_tracker_info(uuid="bb823c3e-d84e-44b5-a9b4-89f65ab2d02c")
190    @TelephonyBaseTest.tel_test_wrap
191    def test_5g_nsa_metered_cellular(self):
192        """ Verifies 5G Meteredness API
193
194        Set Mode to 5G
195        Wait for 5G attached on NSA
196        Register for Connectivity callback
197        Verify value of metered flag
198
199        Returns:
200            True if pass; False if fail.
201        """
202        ad = self.android_devices[0]
203        try:
204            wifi_toggle_state(ad.log, ad, False)
205            toggle_airplane_mode(ad.log, ad, False)
206            if not provision_device_for_5g(ad.log, ad, nr_type='nsa'):
207                return False
208
209            return verify_for_network_callback(ad.log, ad,
210                NetworkCallbackCapabilitiesChanged, apm_mode=False)
211        except Exception as e:
212            ad.log.error(e)
213            return False
214
215
216    @test_tracker_info(uuid="192a605c-d7a9-4c34-800a-96a7d3177d7b")
217    @TelephonyBaseTest.tel_test_wrap
218    def test_5g_nsa_metered_wifi(self):
219        """ Verifies 5G Meteredness API
220
221        Set Mode to 5G, Wifi Connected
222        Register for Connectivity callback
223        Verify value of metered flag
224
225        Returns:
226            True if pass; False if fail.
227        """
228        ad = self.android_devices[0]
229        try:
230            toggle_airplane_mode(ad.log, ad, False)
231            if not provision_device_for_5g(ad.log, ad, nr_type='nsa'):
232                return False
233            wifi_toggle_state(ad.log, ad, True)
234            if not ensure_wifi_connected(ad.log, ad,
235                                         self.wifi_network_ssid,
236                                         self.wifi_network_pass):
237                ad.log.error("WiFi connect fail.")
238                return False
239            return verify_for_network_callback(ad.log, ad,
240                 NetworkCallbackCapabilitiesChanged)
241        except Exception as e:
242            ad.log.error(e)
243            return False
244        finally:
245            wifi_toggle_state(ad.log, ad, False)
246
247
248    @test_tracker_info(uuid="be0c110d-52d4-4af8-bf1c-96c4807d1f07")
249    @TelephonyBaseTest.tel_test_wrap
250    def test_5g_nsa_udp_throughput(self):
251        """ Verifies UDP DL throughput over 5G
252
253        Set Mode to 5G, Wifi Disconnected
254        Verify device attached to 5G NSA
255        Perform iperf test using UDP and measure throughput
256
257        Returns:
258            True if pass; False if fail.
259        """
260        ad = self.android_devices[0]
261        try:
262            toggle_airplane_mode(ad.log, ad, False)
263            if not provision_device_for_5g(ad.log, ad, nr_type='nsa'):
264                return False
265            wifi_toggle_state(ad.log, ad, False)
266            return iperf_udp_test_by_adb(ad.log,
267                                         ad,
268                                         self.iperf_server_ip,
269                                         self.iperf_udp_port,
270                                         True,
271                                         self.iperf_duration)
272        except Exception as e:
273            ad.log.error(e)
274            return False
275
276
277    @test_tracker_info(uuid="47b87533-dc33-4c27-95ff-0b5316e6a193")
278    @TelephonyBaseTest.tel_test_wrap
279    def test_5g_nsa_tcp_throughput(self):
280        """ Verifies TCP DL throughput over 5G
281
282        Set Mode to 5G, Wifi Disconnected
283        Verify device attached to 5G NSA
284        Perform iperf test using TCP and measure throughput
285
286        Returns:
287            True if pass; False if fail.
288        """
289        ad = self.android_devices[0]
290        try:
291            toggle_airplane_mode(ad.log, ad, False)
292            if not provision_device_for_5g(ad.log, ad, nr_type='nsa'):
293                return False
294            wifi_toggle_state(ad.log, ad, False)
295            return iperf_test_by_adb(ad.log,
296                                     ad,
297                                     self.iperf_server_ip,
298                                     self.iperf_tcp_port,
299                                     True,
300                                     self.iperf_duration)
301        except Exception as e:
302            ad.log.error(e)
303            return False
304
305
306    @test_tracker_info(uuid="79393af4-cbc1-4d00-8e02-fe76e8b28367")
307    @TelephonyBaseTest.tel_test_wrap
308    def test_5g_nsa_bursty_data(self):
309        """ Verifies Bursty data transfer over 5G
310
311        Set Mode to 5G, Wifi Disconnected
312        Verify device attached to 5G NSA
313        Perform iperf test using burst of data
314
315        Returns:
316            True if pass; False if fail.
317        """
318        ad = self.android_devices[0]
319        try:
320            toggle_airplane_mode(ad.log, ad, False)
321            if not provision_device_for_5g(ad.log, ad, nr_type='nsa'):
322                return False
323            wifi_toggle_state(ad.log, ad, False)
324            return iperf_udp_test_by_adb(ad.log,
325                                         ad,
326                                         self.iperf_server_ip,
327                                         self.iperf_udp_port,
328                                         True,
329                                         self.iperf_duration,
330                                         limit_rate="10M",
331                                         pacing_timer="1000000")
332        except Exception as e:
333            ad.log.error(e)
334            return False
335
336    @test_tracker_info(uuid="7179f0f1-f0ca-4496-8f4a-7eebc616a41a")
337    @TelephonyBaseTest.tel_test_wrap
338    def test_5g_nsa_wifi_switching(self):
339        """Test data connection network switching when phone camped on nsa 5G.
340
341        Ensure phone is camped on nsa 5G
342        Ensure WiFi can connect to live network,
343        Airplane mode is off, data connection is on, WiFi is on.
344        Turn off WiFi, verify data is on cell and browse to google.com is OK.
345        Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
346        Turn off WiFi, verify data is on cell and browse to google.com is OK.
347
348        Returns:
349            True if pass.
350        """
351        ad = self.android_devices[0]
352        return wifi_cell_switching(ad.log, ad, GEN_5G, self.wifi_network_ssid,
353                                   self.wifi_network_pass, nr_type='nsa')
354
355
356    @test_tracker_info(uuid="75066e0a-0e2e-4346-a253-6ed11d1c4d23")
357    @TelephonyBaseTest.tel_test_wrap
358    def test_5g_nsa_multi_bearer(self):
359        """Test nsa5G data connection before call and in call. (VoLTE call)
360
361        Turn off airplane mode, disable WiFi, enable Cellular Data.
362        Make sure phone in nsa5G, verify Internet.
363        Initiate a voice call. verify Internet.
364        Disable Cellular Data, verify Internet is inaccessible.
365        Enable Cellular Data, verify Internet.
366        Hangup Voice Call, verify Internet.
367
368        Returns:
369            True if success.
370            False if failed.
371        """
372        return test_data_connectivity_multi_bearer(
373            self.log, self.android_devices, '5g_volte', nr_type='nsa')
374
375
376    @test_tracker_info(uuid="e88b226e-3842-4c45-a33e-d4fee7d8f6f0")
377    @TelephonyBaseTest.tel_test_wrap
378    def test_5g_nsa_data_connectivity(self):
379        """Test data connection in nsa5g.
380
381        Turn off airplane mode, disable WiFi, enable Cellular Data.
382        Ensure phone data generation is nsa 5g.
383        Verify Internet.
384        Disable Cellular Data, verify Internet is inaccessible.
385        Enable Cellular Data, verify Internet.
386
387        Returns:
388            True if success.
389            False if failed.
390        """
391        ad = self.android_devices[0]
392        wifi_reset(ad.log, ad)
393        wifi_toggle_state(ad.log, ad, False)
394        return data_connectivity_single_bearer(ad.log, ad, GEN_5G, nr_type='nsa')
395
396
397    @test_tracker_info(uuid="4c70e09d-f215-4c5b-8c61-f9e9def43d30")
398    @TelephonyBaseTest.tel_test_wrap
399    def test_5g_nsa_wifi_not_associated(self):
400        """Test data connection in nsa 5g.
401
402        Turn off airplane mode, enable WiFi (but not connected), enable Cellular Data.
403        Ensure phone data generation is nsa 5g.
404        Verify Internet.
405        Disable Cellular Data, verify Internet is inaccessible.
406        Enable Cellular Data, verify Internet.
407
408        Returns:
409            True if success.
410            False if failed.
411        """
412        ad = self.android_devices[0]
413        wifi_reset(ad.log, ad)
414        wifi_toggle_state(ad.log, ad, False)
415        wifi_toggle_state(ad.log, ad, True)
416        return data_connectivity_single_bearer(ad.log, ad, GEN_5G, nr_type='nsa')
417
418
419    @test_tracker_info(uuid="8308bf40-7f1b-443f-bde6-19d9ff97e471")
420    @TelephonyBaseTest.tel_test_wrap
421    def test_5g_nsa_wifi_connect_disconnect(self):
422        """Perform multiple connects and disconnects from WiFi and verify that
423            data switches between WiFi and Cell.
424
425        Steps:
426        1. DUT Cellular Data is on nsa 5G. Reset Wifi on DUT
427        2. Connect DUT to a WiFi AP
428        3. Repeat steps 1-2, alternately disconnecting and disabling wifi
429
430        Expected Results:
431        1. Verify Data on Cell
432        2. Verify Data on Wifi
433
434        Returns:
435            True if success.
436            False if failed.
437        """
438        if not provision_device_for_5g(self.log, self.provider, nr_type='nsa'):
439            return False
440
441        return test_wifi_connect_disconnect(self.log, self.provider, self.wifi_network_ssid, self.wifi_network_pass)
442
443    @test_tracker_info(uuid="4a61c7c9-f4ed-4e21-b04b-d7a81347b8aa")
444    @TelephonyBaseTest.tel_test_wrap
445    def test_5g_nsa_airplane_mode(self):
446        """Test airplane mode basic on Phone and Live SIM on 5G NSA.
447
448        Ensure phone is on 5G NSA.
449        Ensure phone attach, data on, WiFi off and verify Internet.
450        Turn on airplane mode to make sure detach.
451        Turn off airplane mode to make sure attach.
452        Verify Internet connection.
453
454        Returns:
455            True if pass; False if fail.
456        """
457        if not provision_device_for_5g(self.log, self.android_devices[0], nr_type='nsa'):
458            return False
459        return airplane_mode_test(self.log, self.android_devices[0])
460
461    @test_tracker_info(uuid="091cde37-0bac-4399-83aa-cbd5a83b07a1")
462    @TelephonyBaseTest.tel_test_wrap
463    def test_5g_nsa_reboot(self):
464        """Test 5G NSA service availability after reboot.
465
466        Ensure phone is on 5G NSA.
467        Ensure phone attach, data on, WiFi off and verify Internet.
468        Reboot Device.
469        Verify Network Connection.
470
471        Returns:
472            True if pass; False if fail.
473        """
474        if not provision_device_for_5g(self.log, self.android_devices[0], nr_type='nsa'):
475            return False
476        if not verify_internet_connection(self.log, self.android_devices[0]):
477            return False
478        return reboot_test(self.log, self.android_devices[0])
479
480    """ Tests End """
481