1#!/usr/bin/env python3 2# 3# Copyright 2020 - The Android Open Source Project 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 import asserts 20from acts import utils 21from acts import signals 22from acts.controllers.openwrt_lib.openwrt_constants import OpenWrtWifiSecurity 23from acts.test_decorators import test_tracker_info 24import acts_contrib.test_utils.wifi.wifi_test_utils as wutils 25from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest 26 27 28WifiEnums = wutils.WifiEnums 29 30 31class WifiWpaPersonalTest(WifiBaseTest): 32 """Test for WPA Personal. 33 34 Test Bed Requirement: 35 * One Android device without sim card. 36 * One OpenWrt Wi-Fi AP. 37 """ 38 39 def setup_class(self): 40 super().setup_class() 41 self.dut = self.android_devices[0] 42 43 if "OpenWrtAP" in self.user_params: 44 self.openwrt = self.access_points[0] 45 self.configure_openwrt_ap_and_start(wpa1_network=True) 46 47 req_params = ["OpenWrtAP", "roaming_attn"] 48 opt_params = ["pixel_models", "cnss_diag_file"] 49 self.unpack_userparams(req_params, opt_params) 50 self.wpa_psk_2g = self.wpa1_networks[0]["2g"] 51 self.wpa_psk_5g = self.wpa1_networks[0]["5g"] 52 53 def setup_test(self): 54 super().setup_test() 55 for ad in self.android_devices: 56 ad.droid.wakeLockAcquireBright() 57 ad.droid.wakeUpNow() 58 wutils.wifi_toggle_state(ad, True) 59 60 def teardown_test(self): 61 super().teardown_test() 62 for ad in self.android_devices: 63 ad.droid.wakeLockRelease() 64 ad.droid.goToSleepNow() 65 wutils.reset_wifi(self.dut) 66 utils.force_airplane_mode(self.dut, False) 67 68 def teardown_class(self): 69 super().teardown_class() 70 71 def on_fail(self, test_name, begin_time): 72 super().on_fail(test_name, begin_time) 73 self.dut.cat_adb_log(test_name, begin_time) 74 self.dut.take_bug_report(test_name, begin_time) 75 76 def verify_wpa_network_encryption(self, encryption): 77 result = wutils.get_wlan0_link(self.dut) 78 if encryption == "psk+ccmp": 79 asserts.assert_true( 80 result["pairwise_cipher"] == "CCMP" and 81 result["group_cipher"] == "CCMP", 82 "DUT does not connect to {} encryption network".format(encryption)) 83 elif encryption == "psk+tkip": 84 asserts.assert_true( 85 result["pairwise_cipher"] == "TKIP" and 86 result["group_cipher"] == "TKIP", 87 "DUT does not connect to {} encryption network".format(encryption)) 88 elif encryption == "psk+tkip+ccmp": 89 asserts.assert_true( 90 result["pairwise_cipher"] == "CCMP" and 91 result["group_cipher"] == "TKIP", 92 "DUT does not connect to {} encryption network".format(encryption)) 93 94 ### Tests ### 95 96 @test_tracker_info(uuid="0c68a772-b70c-47d6-88ab-1b069c1d8005") 97 def test_connect_to_wpa_psk_ccmp_2g(self): 98 """Test connection between DUT and WPA PSK CCMP 2G. 99 100 Steps: 101 Change AP's security type to "WPA" and cipher to "CCMP". 102 Connect to 2g network. 103 """ 104 self.openwrt.log.info("Enable WPA-PSK CCMP on OpenWrt AP") 105 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 106 wutils.start_wifi_connection_scan_and_ensure_network_found( 107 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 108 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_2g) 109 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 110 111 @test_tracker_info(uuid="4722dffc-2960-4459-9729-0f8114af2321") 112 def test_connect_to_wpa_psk_ccmp_5g(self): 113 """Test connection between DUT and WPA PSK CCMP 5G. 114 115 Steps: 116 Change AP's security type to "WPA" and cipher to "CCMP". 117 Connect to 5g network. 118 """ 119 self.openwrt.log.info("Enable WPA-PSK CCMP on OpenWrt AP") 120 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 121 wutils.start_wifi_connection_scan_and_ensure_network_found( 122 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY]) 123 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_5g) 124 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 125 126 @test_tracker_info(uuid="4759503e-ef9c-430b-9306-b96a347ca3de") 127 def test_connect_to_wpa_psk_tkip_2g(self): 128 """Test connection between DUT and WPA PSK TKIP 2G. 129 130 Steps: 131 Change AP's security type to "WPA" and cipher to "TKIP". 132 Connect to 2g network. 133 """ 134 self.openwrt.log.info("Enable WPA-TKIP on OpenWrt AP") 135 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_TKIP) 136 wutils.start_wifi_connection_scan_and_ensure_network_found( 137 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 138 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_2g) 139 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA_PSK_TKIP) 140 141 @test_tracker_info(uuid="9c836ca6-af14-4d6b-a98e-227fb29e84ee") 142 def test_connect_to_wpa_psk_tkip_5g(self): 143 """Test connection between DUT and WPA PSK TKIP 5G. 144 145 Steps: 146 Change AP's security type to "WPA" and cipher to "TKIP". 147 Connect to 5g network. 148 """ 149 self.openwrt.log.info("Enable WPA-PSK TKIP on OpenWrt AP") 150 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_TKIP) 151 wutils.start_wifi_connection_scan_and_ensure_network_found( 152 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY]) 153 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_5g) 154 155 @test_tracker_info(uuid="c03b362b-cd03-4e34-a99a-ef80a9db6db9") 156 def test_connect_to_wpa_psk_tkip_and_ccmp_2g(self): 157 """Test connection between DUT and WPA PSK CCMP+TKIP 2G. 158 159 Steps: 160 Change AP's security type to "WPA" and cipher to "CCMP and TKIP". 161 Connect to 2g network. 162 """ 163 self.openwrt.log.info("Enable WPA-PSK CCMP and TKIP on OpenWrt AP") 164 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_TKIP_AND_CCMP) 165 wutils.start_wifi_connection_scan_and_ensure_network_found( 166 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 167 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_2g) 168 self.verify_wpa_network_encryption( 169 OpenWrtWifiSecurity.WPA_PSK_TKIP_AND_CCMP) 170 171 @test_tracker_info(uuid="203d7e7f-536d-4feb-9aa2-648f1f9a685d") 172 def test_connect_to_wpa_psk_tkip_and_ccmp_5g(self): 173 """Test connection between DUT and WPA PSK CCMP+TKIP 5G. 174 175 Steps: 176 Change AP's security type to "WPA" and cipher to "CCMP and TKIP". 177 Connect to 5g network. 178 """ 179 self.openwrt.log.info("Enable WPA-PSK CCMP and TKIP on OpenWrt AP") 180 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_TKIP_AND_CCMP) 181 wutils.start_wifi_connection_scan_and_ensure_network_found( 182 self.dut, self.wpa_psk_5g[WifiEnums.SSID_KEY]) 183 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_5g) 184 self.verify_wpa_network_encryption( 185 OpenWrtWifiSecurity.WPA_PSK_TKIP_AND_CCMP) 186 187 @test_tracker_info(uuid="20a41f61-4fda-4fe9-82ee-482ecd8c82eb") 188 def test_connect_to_wpa_psk_ccmp_2g_after_airplane_mode(self): 189 """Test Wi-Fi reconnection after enabling Airplane Mode. 190 191 Steps: 192 DUT connect to 2GHz Wi-Fi network. 193 DUT turns ON Airplane Mode. 194 DUT turns ON Wi-Fi. 195 DUT verify internet connection with HTTP ping. 196 DUT turns OFF Airplane Mode. 197 DUT verify internet connection with HTTP ping. 198 """ 199 self.openwrt.log.info("Enable WPA-PSK CCMP on OpenWrt AP") 200 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 201 wutils.start_wifi_connection_scan_and_ensure_network_found( 202 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 203 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_2g) 204 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 205 # Turn ON DUT"s Airplane Mode. 206 self.dut.log.info("Toggle Airplane Mode ON") 207 utils.force_airplane_mode(self.dut, True) 208 self.dut.log.info("Toggle Wi-Fi ON") 209 # Turn ON DUT"s Wi-Fi 210 wutils.wifi_toggle_state(self.dut, True) 211 wutils.wait_for_connect(self.dut, 212 self.wpa_psk_2g[WifiEnums.SSID_KEY]) 213 wutils.validate_connection(self.dut) 214 utils.force_airplane_mode(self.dut, False) 215 wutils.validate_connection(self.dut) 216 217 @test_tracker_info(uuid="df89c92b-a30c-4485-ab45-daef5240c027") 218 def test_connect_to_wpa_psk_ccmp_2g_after_wifi_off(self): 219 """Test Wi-Fi reconnection after Turn OFF Wi-Fi. 220 221 Steps: 222 DUT connect to 2GHz Wi-Fi network. 223 DUT turns OFF Wi-Fi. 224 DUT turns ON Wi-Fi. 225 DUT verify internet connection with HTTP ping. 226 """ 227 self.openwrt.log.info("Enable WPA-PSK CCMP on OpenWrt AP") 228 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 229 wutils.start_wifi_connection_scan_and_ensure_network_found( 230 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 231 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_2g) 232 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 233 self.dut.log.info("Toggle Wi-Fi OFF") 234 # Turn OFF DUT"s Wi-Fi then Turn if ON. 235 wutils.wifi_toggle_state(self.dut, False) 236 wutils.wifi_toggle_state(self.dut, True) 237 wutils.wait_for_connect(self.dut, 238 self.wpa_psk_2g[WifiEnums.SSID_KEY]) 239 wutils.validate_connection(self.dut) 240 241 @test_tracker_info(uuid="c591e687-340c-42e6-8d85-58a1f930b6b1") 242 def test_connect_to_wpa_psk_ccmp_2g_after_suspend_resume(self): 243 """Test Wi-Fi reconnection after Suspend. 244 245 Steps: 246 DUT connect to 2GHz Wi-Fi network. 247 DUT suspend and resume. 248 DUT verify internet connection with HTTP ping. 249 """ 250 self.openwrt.log.info("Enable WPA-PSK CCMP on OpenWrt AP") 251 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 252 wutils.start_wifi_connection_scan_and_ensure_network_found( 253 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 254 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_2g) 255 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 256 self.dut.log.info("Suspend the DUT and wait for 10 seconds") 257 # Suspend and Resume the DUT. 258 self.dut.go_to_sleep() 259 time.sleep(10) 260 self.dut.log.info("Resume the DUT") 261 self.dut.wakeup_screen() 262 wutils.validate_connection(self.dut) 263 264 @test_tracker_info(uuid="d3e34869-f2ae-4614-983d-19be238d8499") 265 def test_connect_to_wpa_psk_ccmp_2g_after_reboot(self): 266 """Test Wi-Fi reconnection after reboot. 267 268 Steps: 269 DUT connect to 2GHz Wi-Fi network. 270 DUT reboot. 271 DUT verify internet connection with HTTP ping. 272 """ 273 self.openwrt.log.info("Enable WPA-PSK CCMP on OpenWrt AP") 274 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 275 wutils.start_wifi_connection_scan_and_ensure_network_found( 276 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 277 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_2g) 278 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 279 # Reboot the DUT. 280 self.dut.log.info("Reboot the DUT") 281 self.dut.reboot() 282 self.dut.wait_for_boot_completion() 283 wutils.wait_for_connect(self.dut, 284 self.wpa_psk_2g[WifiEnums.SSID_KEY]) 285 wutils.validate_connection(self.dut) 286 287 @test_tracker_info(uuid="ebd8dd7f-dc36-4e99-b18c-5f725a2f88b2") 288 def test_connect_to_wpa_psk_ccmp_2g_after_incorrect_password(self): 289 """Test Wi-Fi reconnection after incorrect password. 290 291 Steps: 292 DUT connect to 2GHz Wi-Fi network. 293 DUT try to connect to the Wi-Fi network with incorrect password. 294 Connection fail as expected. 295 DUT connect to the Wi-Fi network with correct password. 296 DUT verify internet connection with HTTP ping. 297 """ 298 self.openwrt.log.info("Enable WPA-PSK CCMP on OpenWrt AP") 299 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 300 wutils.start_wifi_connection_scan_and_ensure_network_found( 301 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 302 self.wpa_psk_2g_fail = self.wpa_psk_2g.copy() 303 self.wpa_psk_2g_fail["password"] = "incorrect_password" 304 # Try to connect a Wi-Fi network with incorrect passwlrd. 305 try: 306 self.dut.log.info("Connect to Wi-Fi with wrong password") 307 wutils.wifi_connect(self.dut, self.wpa_psk_2g_fail, num_of_tries=1) 308 except: 309 self.dut.log.info("Connect to Wi-Fi with correct password") 310 wutils.wifi_connect(self.dut, self.wpa_psk_2g) 311 else: 312 raise signals.TestFailure("DUT connect to Wi-Fi with wrong password") 313 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 314 wutils.validate_connection(self.dut) 315 316 @test_tracker_info(uuid="d20fc634-8dcc-4336-9640-2a6907ca1894") 317 def test_connect_to_wpa_psk_ccmp_2g_after_out_of_range(self): 318 """Test Wi-Fi reconnection after out of range. 319 320 Steps: 321 DUT connect to 2GHz Wi-Fi network. 322 DUT out of Wi-Fi range. 323 Make Wi-Fi network is not visible by DUT. 324 DUT back in Wi-Fi range. 325 Wi-Fi network is visible by DUT. 326 DUT connect to the Wi-Fi network. 327 DUT verify internet connection with HTTP ping. 328 """ 329 self.openwrt.log.info("Enable WPA-PSK CCMP on OpenWrt AP") 330 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA_PSK_CCMP) 331 wutils.start_wifi_connection_scan_and_ensure_network_found( 332 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 333 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_2g) 334 # Make the DUT out of range. 335 wutils.set_attns(self.attenuators, 336 "atten1_off_atten2_off", 337 self.roaming_attn) 338 wutils.start_wifi_connection_scan_and_ensure_network_not_found( 339 self.dut, 340 self.wpa_psk_2g[WifiEnums.SSID_KEY]) 341 # Make the DUT back in range. 342 wutils.set_attns(self.attenuators, 343 "atten1_on_atten2_on", 344 self.roaming_attn) 345 wutils.start_wifi_connection_scan_and_ensure_network_found( 346 self.dut, self.wpa_psk_2g[WifiEnums.SSID_KEY]) 347 wutils.connect_to_wifi_network(self.dut, self.wpa_psk_2g) 348