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 SA 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_ONLY
27from acts_contrib.test_utils.tel.tel_defines import WAIT_TIME_ANDROID_STATE_SETTLING
28from acts_contrib.test_utils.tel.tel_5g_test_utils import provision_device_for_5g
29from acts_contrib.test_utils.tel.tel_5g_utils import is_current_network_5g
30from acts_contrib.test_utils.tel.tel_data_utils import browsing_test
31from acts_contrib.test_utils.tel.tel_data_utils import check_data_stall_detection
32from acts_contrib.test_utils.tel.tel_data_utils import check_data_stall_recovery
33from acts_contrib.test_utils.tel.tel_data_utils import check_network_validation_fail
34from acts_contrib.test_utils.tel.tel_data_utils import data_connectivity_single_bearer
35from acts_contrib.test_utils.tel.tel_data_utils import reboot_test
36from acts_contrib.test_utils.tel.tel_data_utils import test_wifi_connect_disconnect
37from acts_contrib.test_utils.tel.tel_data_utils import wifi_cell_switching
38from acts_contrib.test_utils.tel.tel_test_utils import break_internet_except_sl4a_port
39from acts_contrib.test_utils.tel.tel_test_utils import get_current_override_network_type
40from acts_contrib.test_utils.tel.tel_test_utils import get_device_epoch_time
41from acts_contrib.test_utils.tel.tel_test_utils import resume_internet_with_sl4a_port
42from acts_contrib.test_utils.tel.tel_test_utils import set_preferred_network_mode_pref
43from acts_contrib.test_utils.tel.tel_test_utils import test_data_browsing_failure_using_sl4a
44from acts_contrib.test_utils.tel.tel_test_utils import test_data_browsing_success_using_sl4a
45from acts_contrib.test_utils.tel.tel_test_utils import toggle_airplane_mode
46from acts_contrib.test_utils.tel.tel_test_utils import verify_internet_connection
47from acts_contrib.test_utils.tel.tel_wifi_utils import wifi_reset
48from acts_contrib.test_utils.tel.tel_wifi_utils import wifi_toggle_state
49
50
51class Sa5gDataTest(TelephonyBaseTest):
52    def setup_class(self):
53        super().setup_class()
54
55    def setup_test(self):
56        TelephonyBaseTest.setup_test(self)
57        self.provider = self.android_devices[0]
58        self.clients = self.android_devices[1:]
59
60    def teardown_class(self):
61        TelephonyBaseTest.teardown_class(self)
62
63
64    """ Tests Begin """
65
66    @test_tracker_info(uuid="2bb5fa22-d931-426f-a11d-22f514d867d0")
67    @TelephonyBaseTest.tel_test_wrap
68    def test_5g_sa_data_browsing(self):
69        """ Verifying connectivity of internet and  browsing websites on 5G SA network.
70
71        Ensure
72            1. ping to IP of websites is successful.
73            2. http ping to IP of websites is successful.
74            3. browsing websites is successful.
75        Returns:
76            True if pass; False if fail.
77        """
78        ad = self.android_devices[0]
79        wifi_toggle_state(ad.log, ad, False)
80        sub_id = ad.droid.subscriptionGetDefaultSubId()
81        if not set_preferred_network_mode_pref(ad.log, ad, sub_id,
82                                               NETWORK_MODE_NR_ONLY):
83            ad.log.error("Failed to set network mode to SA")
84            return False
85        ad.log.info("Set network mode to SA successfully")
86        ad.log.info("Waiting for 5g SA attach for 60 secs")
87        if is_current_network_5g(ad, nr_type = 'sa'):
88            ad.log.info("Success! attached on 5g SA")
89        else:
90            ad.log.error("Failure - expected NR, current %s",
91                         get_current_override_network_type(ad))
92            return False
93        for iteration in range(3):
94            connectivity = False
95            browsing = False
96            ad.log.info("Attempt %d", iteration + 1)
97            if not verify_internet_connection(self.log, ad):
98                ad.log.error("Failed to connect to internet!")
99            else:
100                ad.log.info("Connect to internet successfully!")
101                connectivity = True
102            if not browsing_test(ad.log, ad):
103                ad.log.error("Failed to browse websites!")
104            else:
105                ad.log.info("Successful to browse websites!")
106                browsing = True
107            if connectivity and browsing:
108                return True
109            time.sleep(WAIT_TIME_ANDROID_STATE_SETTLING)
110        ad.log.error("5G SA Connectivity and Data Browsing test FAIL for all 3 iterations")
111        return False
112
113
114    @test_tracker_info(uuid="785b0ef8-b326-42f8-a399-51ebc1f9e93e")
115    @TelephonyBaseTest.tel_test_wrap
116    def test_5g_sa_data_stall_recovery(self):
117        """ Verifies 5G SA data stall
118
119        Set Mode to 5G
120        Wait for 5G attached on SA
121        Browse websites for success
122        Trigger data stall and verify browsing fails
123        Resume data and verify browsing success
124
125        Returns:
126            True if pass; False if fail.
127        """
128        ad = self.android_devices[0]
129        result = True
130        wifi_toggle_state(ad.log, ad, False)
131        toggle_airplane_mode(ad.log, ad, False)
132
133        if not provision_device_for_5g(ad.log, ad, nr_type= 'sa'):
134            return False
135
136        cmd = ('ss -l -p -n | grep "tcp.*droid_script" | tr -s " " '
137               '| cut -d " " -f 5 | sed s/.*://g')
138        sl4a_port = ad.adb.shell(cmd)
139
140        if not test_data_browsing_success_using_sl4a(ad.log, ad):
141            ad.log.error("Browsing failed before the test, aborting!")
142            return False
143
144        begin_time = get_device_epoch_time(ad)
145        break_internet_except_sl4a_port(ad, sl4a_port)
146
147        if not test_data_browsing_failure_using_sl4a(ad.log, ad):
148            ad.log.error("Browsing after breaking the internet, aborting!")
149            result = False
150
151        if not check_data_stall_detection(ad):
152            ad.log.warning("NetworkMonitor unable to detect Data Stall")
153
154        if not check_network_validation_fail(ad, begin_time):
155            ad.log.warning("Unable to detect NW validation fail")
156
157        if not check_data_stall_recovery(ad, begin_time):
158            ad.log.error("Recovery was not triggered")
159            result = False
160
161        resume_internet_with_sl4a_port(ad, sl4a_port)
162        time.sleep(MAX_WAIT_TIME_USER_PLANE_DATA)
163        if not test_data_browsing_success_using_sl4a(ad.log, ad):
164            ad.log.error("Browsing failed after resuming internet")
165            result = False
166        if result:
167            ad.log.info("PASS - data stall over 5G SA")
168        else:
169            ad.log.error("FAIL - data stall over 5G SA")
170        return result
171
172
173    @test_tracker_info(uuid="6c0ef257-6381-4c1d-8b8e-0371db7e08ac")
174    @TelephonyBaseTest.tel_test_wrap
175    def test_5g_sa_wifi_connect_disconnect(self):
176        """Perform multiple connects and disconnects from WiFi and verify that
177            data switches between WiFi and Cell.
178
179        Steps:
180        1. DUT Cellular Data is on sa 5G. Reset Wifi on DUT
181        2. Connect DUT to a WiFi AP
182        3. Repeat steps 1-2, alternately disconnecting and disabling wifi
183
184        Expected Results:
185        1. Verify Data on Cell
186        2. Verify Data on Wifi
187
188        Returns:
189            True if success.
190            False if failed.
191        """
192        if not provision_device_for_5g(self.log, self.provider, nr_type= 'sa'):
193            return False
194
195        return test_wifi_connect_disconnect(self.log, self.provider, self.wifi_network_ssid, self.wifi_network_pass)
196
197
198    @test_tracker_info(uuid="f70ad253-4b77-4d9b-b7dc-d7cd3e945e5f")
199    @TelephonyBaseTest.tel_test_wrap
200    def test_5g_sa_wifi_switching(self):
201        """Test data connection network switching when phone camped on sa 5G.
202
203        Ensure phone is camped on sa 5G
204        Ensure WiFi can connect to live network,
205        Airplane mode is off, data connection is on, WiFi is on.
206        Turn off WiFi, verify data is on cell and browse to google.com is OK.
207        Turn on WiFi, verify data is on WiFi and browse to google.com is OK.
208        Turn off WiFi, verify data is on cell and browse to google.com is OK.
209
210        Returns:
211            True if pass.
212        """
213        ad = self.android_devices[0]
214        return wifi_cell_switching(ad.log, ad, GEN_5G, self.wifi_network_ssid,
215                                   self.wifi_network_pass, nr_type= 'sa')
216
217
218    @test_tracker_info(uuid="8df1b65c-197e-40b3-83a4-6da1f0a51b97")
219    @TelephonyBaseTest.tel_test_wrap
220    def test_5g_sa_wifi_not_associated(self):
221        """Test data connection in sa 5g.
222
223        Turn off airplane mode, enable WiFi (but not connected), enable Cellular Data.
224        Ensure phone data generation is sa 5g.
225        Verify Internet.
226        Disable Cellular Data, verify Internet is inaccessible.
227        Enable Cellular Data, verify Internet.
228
229        Returns:
230            True if success.
231            False if failed.
232        """
233        ad = self.android_devices[0]
234        wifi_reset(ad.log, ad)
235        wifi_toggle_state(ad.log, ad, False)
236        wifi_toggle_state(ad.log, ad, True)
237        return data_connectivity_single_bearer(ad.log, ad, GEN_5G, nr_type= 'sa')
238
239
240    @test_tracker_info(uuid="6c1ec0a6-223e-4bcd-b958-b85f5eb03943")
241    @TelephonyBaseTest.tel_test_wrap
242    def test_5g_sa_reboot(self):
243        """Test 5G SA service availability after reboot.
244
245        Ensure phone is on 5G SA.
246        Ensure phone attach, data on, WiFi off and verify Internet.
247        Reboot Device.
248        Verify Network Connection.
249
250        Returns:
251            True if pass; False if fail.
252        """
253        if not provision_device_for_5g(self.log, self.android_devices[0], nr_type='sa'):
254            return False
255        if not verify_internet_connection(self.log, self.android_devices[0]):
256            return False
257        return reboot_test(self.log, self.android_devices[0])
258
259    """ Tests End """
260