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 NSA MMWAVE 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_5g_test_utils import provision_device_for_5g
30from acts_contrib.test_utils.tel.tel_5g_test_utils import set_preferred_mode_for_5g
31from acts_contrib.test_utils.tel.tel_5g_utils import is_current_network_5g
32from acts_contrib.test_utils.tel.tel_data_utils import airplane_mode_test
33from acts_contrib.test_utils.tel.tel_data_utils import browsing_test
34from acts_contrib.test_utils.tel.tel_data_utils import check_data_stall_detection
35from acts_contrib.test_utils.tel.tel_data_utils import check_data_stall_recovery
36from acts_contrib.test_utils.tel.tel_data_utils import check_network_validation_fail
37from acts_contrib.test_utils.tel.tel_data_utils import data_connectivity_single_bearer
38from acts_contrib.test_utils.tel.tel_data_utils import reboot_test
39from acts_contrib.test_utils.tel.tel_data_utils import test_wifi_connect_disconnect
40from acts_contrib.test_utils.tel.tel_data_utils import verify_for_network_callback
41from acts_contrib.test_utils.tel.tel_data_utils import wifi_cell_switching
42from acts_contrib.test_utils.tel.tel_test_utils import break_internet_except_sl4a_port
43from acts_contrib.test_utils.tel.tel_test_utils import get_current_override_network_type
44from acts_contrib.test_utils.tel.tel_test_utils import get_device_epoch_time
45from acts_contrib.test_utils.tel.tel_test_utils import resume_internet_with_sl4a_port
46from acts_contrib.test_utils.tel.tel_test_utils import set_phone_silent_mode
47from acts_contrib.test_utils.tel.tel_test_utils import test_data_browsing_failure_using_sl4a
48from acts_contrib.test_utils.tel.tel_test_utils import test_data_browsing_success_using_sl4a
49from acts_contrib.test_utils.tel.tel_test_utils import toggle_airplane_mode
50from acts_contrib.test_utils.tel.tel_test_utils import verify_internet_connection
51from acts_contrib.test_utils.tel.tel_wifi_utils import ensure_wifi_connected
52from acts_contrib.test_utils.tel.tel_wifi_utils import wifi_reset
53from acts_contrib.test_utils.tel.tel_wifi_utils import wifi_toggle_state
54
55
56class Nsa5gMmwDataTest(TelephonyBaseTest):
57    def setup_class(self):
58        super().setup_class()
59        self.iperf_server_ip = self.user_params.get("iperf_server", '0.0.0.0')
60        self.iperf_tcp_port = self.user_params.get("iperf_tcp_port", 0)
61        self.iperf_udp_port = self.user_params.get("iperf_udp_port", 0)
62        self.iperf_duration = self.user_params.get("iperf_duration", 60)
63        for ad in self.android_devices:
64            set_phone_silent_mode(self.log, ad, True)
65
66    def setup_test(self):
67        TelephonyBaseTest.setup_test(self)
68        self.provider = self.android_devices[0]
69        self.clients = self.android_devices[1:]
70
71    def teardown_class(self):
72        TelephonyBaseTest.teardown_class(self)
73
74
75    """ Tests Begin """
76
77
78    @test_tracker_info(uuid="069d05c0-1fa0-4fd4-a4df-a0eff753b38d")
79    @TelephonyBaseTest.tel_test_wrap
80    def test_5g_nsa_mmw_data_browsing(self):
81        """ Verifying connectivity of internet and  browsing websites on 5G NSA MMW network.
82
83        Ensure
84            1. ping to IP of websites is successful.
85            2. http ping to IP of websites is successful.
86            3. browsing websites is successful.
87        Returns:
88            True if pass; False if fail.
89        """
90        ad = self.android_devices[0]
91        wifi_toggle_state(ad.log, ad, False)
92        sub_id = ad.droid.subscriptionGetDefaultSubId()
93        if not set_preferred_mode_for_5g(ad, sub_id,
94                                               NETWORK_MODE_NR_LTE_GSM_WCDMA):
95            ad.log.error("Failed to set network mode to NSA")
96            return False
97        ad.log.info("Set network mode to NSA successfully")
98        ad.log.info("Waiting for 5G NSA MMW attach for 60 secs")
99        if is_current_network_5g(ad, nr_type = 'mmwave', timeout=60):
100            ad.log.info("Success! attached on 5G NSA MMW")
101        else:
102            ad.log.error("Failure - expected NR_NSA MMW, current %s",
103                         get_current_override_network_type(ad))
104            # Can't attach 5G NSA MMW, exit test!
105            return False
106        for iteration in range(3):
107            connectivity = False
108            browsing = False
109            ad.log.info("Attempt %d", iteration + 1)
110            if not verify_internet_connection(self.log, ad):
111                ad.log.error("Failed to connect to internet!")
112            else:
113                ad.log.info("Connect to internet successfully!")
114                connectivity = True
115            if not browsing_test(ad.log, ad):
116                ad.log.error("Failed to browse websites!")
117            else:
118                ad.log.info("Successful to browse websites!")
119                browsing = True
120            if connectivity and browsing:
121                return True
122            time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
123        ad.log.error("5G NSA MMW Connectivity and Data Browsing test FAIL for all 3 iterations")
124        return False
125
126
127    @test_tracker_info(uuid="f1638e11-c686-4431-8b6c-4dc7cbff6406")
128    @TelephonyBaseTest.tel_test_wrap
129    def test_5g_nsa_mmw_data_stall_recovery(self):
130        """ Verifies 5G NSA MMW data stall
131
132        Set Mode to 5G NSA MMW
133        Wait for 5G attached on NSA MMW
134        Browse websites for success
135        Trigger data stall and verify browsing fails
136        Resume data and verify browsing success
137
138        Returns:
139            True if pass; False if fail.
140        """
141        ad = self.android_devices[0]
142        result = True
143        wifi_toggle_state(ad.log, ad, False)
144        toggle_airplane_mode(ad.log, ad, False)
145
146        if not provision_device_for_5g(ad.log, ad, nr_type='mmwave'):
147            return False
148
149        cmd = ('ss -l -p -n | grep "tcp.*droid_script" | tr -s " " '
150               '| cut -d " " -f 5 | sed s/.*://g')
151        sl4a_port = ad.adb.shell(cmd)
152
153        if not test_data_browsing_success_using_sl4a(ad.log, ad):
154            ad.log.error("Browsing failed before the test, aborting!")
155            return False
156
157        begin_time = get_device_epoch_time(ad)
158        break_internet_except_sl4a_port(ad, sl4a_port)
159
160        if not test_data_browsing_failure_using_sl4a(ad.log, ad):
161            ad.log.error("Browsing after breaking the internet, aborting!")
162            result = False
163
164        if not check_data_stall_detection(ad):
165            ad.log.warning("NetworkMonitor unable to detect Data Stall")
166
167        if not check_network_validation_fail(ad, begin_time):
168            ad.log.warning("Unable to detect NW validation fail")
169
170        if not check_data_stall_recovery(ad, begin_time):
171            ad.log.error("Recovery was not triggered")
172            result = False
173
174        resume_internet_with_sl4a_port(ad, sl4a_port)
175        time.sleep(MAX_WAIT_TIME_USER_PLANE_DATA)
176        if not test_data_browsing_success_using_sl4a(ad.log, ad):
177            ad.log.error("Browsing failed after resuming internet")
178            result = False
179        if result:
180            ad.log.info("PASS - data stall over 5G NSA MMW")
181        else:
182            ad.log.error("FAIL - data stall over 5G NSA MMW")
183        return result
184
185
186    @test_tracker_info(uuid="38fd987d-2a9a-44d5-bea4-e524359390c6")
187    @TelephonyBaseTest.tel_test_wrap
188    def test_5g_nsa_mmw_metered_cellular(self):
189        """ Verifies 5G NSA MMW Meteredness API
190
191        Set Mode to 5G NSA MMW
192        Wait for 5G attached on NSA NSA MMW
193        Register for Connectivity callback
194        Verify value of metered flag
195
196        Returns:
197            True if pass; False if fail.
198        """
199        ad = self.android_devices[0]
200        try:
201            wifi_toggle_state(ad.log, ad, False)
202            toggle_airplane_mode(ad.log, ad, False)
203            if not provision_device_for_5g(ad.log, ad, nr_type='mmwave'):
204                return False
205
206            return verify_for_network_callback(ad.log, ad,
207                NetworkCallbackCapabilitiesChanged, apm_mode=False)
208        except Exception as e:
209            ad.log.error(e)
210            return False
211
212
213    @test_tracker_info(uuid="8d4ce840-6261-4395-bf7b-e1f6cdf4d9a9")
214    @TelephonyBaseTest.tel_test_wrap
215    def test_5g_nsa_mmw_metered_wifi(self):
216        """ Verifies 5G NSA MMW Meteredness API
217
218        Set Mode to 5G NSA MMW, Wifi Connected
219        Register for Connectivity callback
220        Verify value of metered flag
221
222        Returns:
223            True if pass; False if fail.
224        """
225        ad = self.android_devices[0]
226        try:
227            toggle_airplane_mode(ad.log, ad, False)
228            if not provision_device_for_5g(ad.log, ad, nr_type='mmwave'):
229                return False
230            wifi_toggle_state(ad.log, ad, True)
231            if not ensure_wifi_connected(ad.log, ad,
232                                         self.wifi_network_ssid,
233                                         self.wifi_network_pass):
234                ad.log.error("WiFi connect fail.")
235                return False
236            return verify_for_network_callback(ad.log, ad,
237                 NetworkCallbackCapabilitiesChanged)
238        except Exception as e:
239            ad.log.error(e)
240            return False
241        finally:
242            wifi_toggle_state(ad.log, ad, False)
243
244
245    @test_tracker_info(uuid="1661cd40-0eed-43f0-bd2a-8e02392af3b1")
246    @TelephonyBaseTest.tel_test_wrap
247    def test_5g_nsa_mmw_wifi_switching(self):
248        """Test data connection network switching when phone camped on 5G NSA MMW.
249
250        Ensure phone is camped on 5G NSA MMW
251        Ensure WiFi can connect to live network,
252        Airplane mode is off, data connection is on, WiFi is on.
253        Turn off WiFi, verify data is on cell and browse to google.com is OK.
254        Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
255        Turn off WiFi, verify data is on cell and browse to google.com is OK.
256
257        Returns:
258            True if pass.
259        """
260        ad = self.android_devices[0]
261        return wifi_cell_switching(ad.log, ad, GEN_5G, self.wifi_network_ssid,
262                                   self.wifi_network_pass, nr_type='mmwave')
263
264
265    @test_tracker_info(uuid="8033a359-1b92-45ff-b766-bb0010132eb7")
266    @TelephonyBaseTest.tel_test_wrap
267    def test_5g_nsa_mmw_data_connectivity(self):
268        """Test data connection in 5g NSA MMW.
269
270        Turn off airplane mode, disable WiFi, enable Cellular Data.
271        Ensure phone data generation is 5g NSA MMW.
272        Verify Internet.
273        Disable Cellular Data, verify Internet is inaccessible.
274        Enable Cellular Data, verify Internet.
275
276        Returns:
277            True if success.
278            False if failed.
279        """
280        ad = self.android_devices[0]
281        wifi_reset(ad.log, ad)
282        wifi_toggle_state(ad.log, ad, False)
283        return data_connectivity_single_bearer(ad.log, ad, GEN_5G, nr_type='mmwave')
284
285
286    @test_tracker_info(uuid="633526fa-9e58-47a4-8957-bb0a95eef4ab")
287    @TelephonyBaseTest.tel_test_wrap
288    def test_5g_nsa_mmw_wifi_not_associated(self):
289        """Test data connection in 5g NSA MMW.
290
291        Turn off airplane mode, enable WiFi (but not connected), enable Cellular Data.
292        Ensure phone data generation is 5g MMW.
293        Verify Internet.
294        Disable Cellular Data, verify Internet is inaccessible.
295        Enable Cellular Data, verify Internet.
296
297        Returns:
298            True if success.
299            False if failed.
300        """
301        ad = self.android_devices[0]
302        wifi_reset(ad.log, ad)
303        wifi_toggle_state(ad.log, ad, False)
304        wifi_toggle_state(ad.log, ad, True)
305        return data_connectivity_single_bearer(ad.log, ad, GEN_5G, nr_type='mmwave')
306
307
308    @test_tracker_info(uuid="c56324a2-5eda-4027-9068-7e120d2b178e")
309    @TelephonyBaseTest.tel_test_wrap
310    def test_5g_nsa_mmw_wifi_connect_disconnect(self):
311        """Perform multiple connects and disconnects from WiFi and verify that
312            data switches between WiFi and Cell.
313
314        Steps:
315        1. DUT Cellular Data is on 5G NSA MMW. Reset Wifi on DUT
316        2. Connect DUT to a WiFi AP
317        3. Repeat steps 1-2, alternately disconnecting and disabling wifi
318
319        Expected Results:
320        1. Verify Data on Cell
321        2. Verify Data on Wifi
322
323        Returns:
324            True if success.
325            False if failed.
326        """
327        if not provision_device_for_5g(self.log, self.provider, nr_type='mmwave'):
328            return False
329
330        return test_wifi_connect_disconnect(self.log, self.provider, self.wifi_network_ssid, self.wifi_network_pass)
331
332
333    @test_tracker_info(uuid="88cd3f68-08c3-4635-94ce-a1dffc3ffbf2")
334    @TelephonyBaseTest.tel_test_wrap
335    def test_5g_nsa_mmw_airplane_mode(self):
336        """Test airplane mode basic on Phone and Live SIM on 5G NSA MMW.
337
338        Ensure phone is on 5G NSA MMW.
339        Ensure phone attach, data on, WiFi off and verify Internet.
340        Turn on airplane mode to make sure detach.
341        Turn off airplane mode to make sure attach.
342        Verify Internet connection.
343
344        Returns:
345            True if pass; False if fail.
346        """
347        if not provision_device_for_5g(self.log, self.android_devices[0], nr_type='mmwave'):
348            return False
349        return airplane_mode_test(self.log, self.android_devices[0])
350
351
352    @test_tracker_info(uuid="b99967b9-96da-4f1b-90cb-6dbd6578236b")
353    @TelephonyBaseTest.tel_test_wrap
354    def test_5g_nsa_mmw_reboot(self):
355        """Test 5G NSA MMWAVE service availability after reboot.
356
357        Ensure phone is on 5G NSA MMWAVE.
358        Ensure phone attach, data on, WiFi off and verify Internet.
359        Reboot Device.
360        Verify Network Connection.
361
362        Returns:
363            True if pass; False if fail.
364        """
365        if not provision_device_for_5g(self.log, self.android_devices[0], nr_type='mmwave'):
366            return False
367        if not verify_internet_connection(self.log, self.android_devices[0]):
368            return False
369        return reboot_test(self.log, self.android_devices[0])
370
371
372    """ Tests End """
373