1#!/usr/bin/env python3 2# 3# Copyright 2021 - 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 18import acts_contrib.test_utils.wifi.wifi_test_utils as wutils 19from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest 20from acts.controllers.ap_lib import hostapd_constants 21from acts.controllers.openwrt_lib.openwrt_constants import OpenWrtWifiSecurity 22from acts.test_decorators import test_tracker_info 23from acts import asserts 24from acts import utils 25from acts import signals 26 27 28WifiEnums = wutils.WifiEnums 29 30 31class WifiWpa2PersonalTest(WifiBaseTest): 32 """ Wi-Fi WPA2 test 33 34 Test Bed Requirement: 35 * One Android device 36 * One OpenWrt Wi-Fi AP. 37 """ 38 def __init__(self, configs): 39 super().__init__(configs) 40 self.enable_packet_log = True 41 42 def setup_class(self): 43 super().setup_class() 44 self.dut = self.android_devices[0] 45 req_params = ["roaming_attn"] 46 opt_params = [] 47 self.unpack_userparams(req_params, opt_params) 48 49 def setup_test(self): 50 super().setup_test() 51 for ad in self.android_devices: 52 ad.droid.wakeLockAcquireBright() 53 ad.droid.wakeUpNow() 54 wutils.wifi_toggle_state(ad, True) 55 wutils.reset_wifi(self.dut) 56 57 def teardown_test(self): 58 super().teardown_test() 59 for ad in self.android_devices: 60 ad.droid.wakeLockRelease() 61 ad.droid.goToSleepNow() 62 wutils.reset_wifi(self.dut) 63 64 def start_openwrt(self, channel_2g=None, channel_5g=None): 65 """Enable one OpenWrt to generate a Wi-Fi network. 66 67 Args: 68 channel_2g: Optional; An integer to represent a Wi-Fi 2g channel. 69 The default value is 6 if it's not given. 70 channel_5g: Optional; An integer to represent a Wi-Fi 5g channel. 71 The default value is 36 if it's not given. 72 """ 73 if not channel_2g: 74 channel_2g = hostapd_constants.AP_DEFAULT_CHANNEL_2G 75 if not channel_5g: 76 channel_5g = hostapd_constants.AP_DEFAULT_CHANNEL_5G 77 if "OpenWrtAP" in self.user_params: 78 self.openwrt = self.access_points[0] 79 self.configure_openwrt_ap_and_start(wpa_network=True, 80 channel_2g=channel_2g, 81 channel_5g=channel_5g) 82 self.wpa2_psk_2g = self.wpa_networks[0]["2g"] 83 self.wpa2_psk_5g = self.wpa_networks[0]["5g"] 84 85 def verify_wpa_network_encryption(self, encryption): 86 result = wutils.get_wlan0_link(self.dut) 87 if encryption == 'psk2+ccmp': 88 asserts.assert_true( 89 result['pairwise_cipher'] == 'CCMP' and 90 result['group_cipher'] == 'CCMP' and 91 result['key_mgmt'] == "WPA2-PSK", 92 'DUT does not connect to {} encryption network'.format(encryption)) 93 elif encryption == 'psk2+tkip': 94 asserts.assert_true( 95 result['pairwise_cipher'] == 'TKIP' and 96 result['group_cipher'] == 'TKIP' and 97 result['key_mgmt'] == "WPA2-PSK", 98 'DUT does not connect to {} encryption network'.format(encryption)) 99 elif encryption == 'psk2+tkip+ccmp': 100 asserts.assert_true( 101 result['pairwise_cipher'] == 'CCMP' and 102 result['group_cipher'] == 'TKIP' and 103 result['key_mgmt'] == "WPA2-PSK", 104 'DUT does not connect to {} encryption network'.format(encryption)) 105 106 """ Tests""" 107 108 @test_tracker_info(uuid="d1f984c9-d85f-4b0d-8d64-2e8d6ce74c48") 109 def test_connect_to_wpa2_psk_ccmp_2g(self): 110 """Generate a Wi-Fi network. 111 Change AP's security type to "WPA2" and cipher to "CCMP". 112 Connect to 2g network. 113 """ 114 self.start_openwrt() 115 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 116 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 117 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 118 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 119 120 @test_tracker_info(uuid="0f9631e8-04a9-4b9c-8225-ab30b4d1173b") 121 def test_connect_to_wpa2_psk_ccmp_5g(self): 122 """Generate a Wi-Fi network. 123 Change AP's security type to "WPA2" and cipher to "CCMP". 124 Connect to 5g network. 125 """ 126 self.start_openwrt() 127 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 128 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 129 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_5g) 130 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 131 132 @test_tracker_info(uuid="e6eb3932-10cc-476f-a5d7-936e2631afc1") 133 def test_connect_to_wpa2_psk_tkip_2g(self): 134 """Generate a Wi-Fi network. 135 Change AP's security type to "WPA2" and cipher to "TKIP". 136 Connect to 2g network. 137 """ 138 self.start_openwrt() 139 self.openwrt.log.info("Enable WPA2-PSK TKIP on OpenWrt AP") 140 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP) 141 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 142 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP) 143 144 @test_tracker_info(uuid="59ba3cd4-dbc5-44f9-9290-48ae468a51da") 145 def test_connect_to_wpa2_psk_tkip_5g(self): 146 """Generate a Wi-Fi network. 147 Change AP's security type to "WPA2" and cipher to "TKIP". 148 Connect to 5g network. 149 """ 150 self.start_openwrt() 151 self.openwrt.log.info("Enable WPA2-PSK TKIP on OpenWrt AP") 152 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP) 153 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_5g) 154 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP) 155 156 @test_tracker_info(uuid="a06be3db-d653-4549-95f3-87bbeb0db813") 157 def test_connect_to_wpa2_psk_tkip_and_ccmp_2g(self): 158 """Generate a Wi-Fi network. 159 Change AP's security type to "WPA2" and cipher to "CCMP and TKIP". 160 Connect to 2g network. 161 """ 162 self.start_openwrt() 163 self.openwrt.log.info("Enable WPA2-PSK CCMP and TKIP on OpenWrt AP") 164 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP_AND_CCMP) 165 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 166 self.verify_wpa_network_encryption( 167 OpenWrtWifiSecurity.WPA2_PSK_TKIP_AND_CCMP) 168 169 @test_tracker_info(uuid="ac9b9581-0b32-42b4-8e76-de702c837b86") 170 def test_connect_to_wpa2_psk_tkip_and_ccmp_5g(self): 171 """Generate a Wi-Fi network. 172 Change AP's security type to "WPA2" and cipher to "CCMP and TKIP". 173 Connect to 5g network. 174 """ 175 self.start_openwrt() 176 self.openwrt.log.info("Enable WPA2-PSK CCMP and TKIP on OpenWrt AP") 177 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP_AND_CCMP) 178 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_5g) 179 self.verify_wpa_network_encryption( 180 OpenWrtWifiSecurity.WPA2_PSK_TKIP_AND_CCMP) 181 182 @test_tracker_info(uuid="4e8286a0-1b7c-4186-9d86-1cc5cd7a6be2") 183 def test_connect_to_wpa2_psk_ccmp_2g_after_airplane_mode(self): 184 """Test Wi-Fi reconnection after enabling Airplane Mode. 185 186 Steps: 187 DUT connect to 2GHz Wi-Fi network. 188 DUT turns ON Airplane Mode. 189 DUT turns ON Wi-Fi. 190 DUT verify internet connection with HTTP ping. 191 DUT turns OFF Airplane Mode. 192 DUT verify internet connection with HTTP ping. 193 """ 194 self.start_openwrt() 195 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 196 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 197 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 198 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 199 # Turn ON DUT's Airplane Mode. 200 self.dut.log.info("Toggle Airplane Mode ON") 201 utils.force_airplane_mode(self.dut, True) 202 self.dut.log.info("Toggle Wi-Fi ON") 203 # Turn ON DUT's Wi-Fi 204 wutils.wifi_toggle_state(self.dut, True) 205 wutils.wait_for_connect(self.dut, 206 self.wpa2_psk_2g[WifiEnums.SSID_KEY]) 207 wutils.validate_connection(self.dut) 208 utils.force_airplane_mode(self.dut, False) 209 wutils.validate_connection(self.dut) 210 211 @test_tracker_info(uuid="091c9b6a-d729-41d0-85e7-acf777aa3d1f") 212 def test_connect_to_wpa2_psk_ccmp_2g_after_wifi_off(self): 213 """Test Wi-Fi reconnection after Turn OFF Wi-Fi. 214 215 Steps: 216 DUT connect to 2GHz Wi-Fi network. 217 DUT turns OFF Wi-Fi. 218 DUT turns ON Wi-Fi. 219 DUT verify internet connection with HTTP ping. 220 """ 221 self.start_openwrt() 222 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 223 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 224 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 225 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 226 self.dut.log.info("Toggle Wi-Fi OFF") 227 # Turn OFF DUT's Wi-Fi then Turn if ON. 228 wutils.wifi_toggle_state(self.dut, False) 229 wutils.wifi_toggle_state(self.dut, True) 230 wutils.wait_for_connect(self.dut, 231 self.wpa2_psk_2g[WifiEnums.SSID_KEY]) 232 wutils.validate_connection(self.dut) 233 234 @test_tracker_info(uuid="9eb74878-1527-4c5b-980e-1a9305f601aa") 235 def test_connect_to_wpa2_psk_ccmp_2g_after_suspend_resume(self): 236 """Test Wi-Fi reconnection after Suspend. 237 238 Steps: 239 DUT connect to 2GHz Wi-Fi network. 240 DUT suspend and resume. 241 DUT verify internet connection with HTTP ping. 242 """ 243 self.start_openwrt() 244 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 245 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 246 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 247 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 248 self.dut.log.info("Suspend the DUT and wait for 10 seconds") 249 # Suspend and Resume the DUT. 250 self.dut.go_to_sleep() 251 time.sleep(10) 252 self.dut.log.info("Resume the DUT") 253 self.dut.wakeup_screen() 254 wutils.validate_connection(self.dut) 255 256 @test_tracker_info(uuid="44e5d946-620a-4e30-9578-f921460fe3f3") 257 def test_connect_to_wpa2_psk_ccmp_2g_after_reboot(self): 258 """Test Wi-Fi reconnection after reboot. 259 260 Steps: 261 DUT connect to 2GHz Wi-Fi network. 262 DUT reboot. 263 DUT verify internet connection with HTTP ping. 264 """ 265 self.start_openwrt() 266 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 267 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 268 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 269 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 270 # Reboot the DUT. 271 self.dut.log.info("Reboot the DUT") 272 self.dut.reboot() 273 self.dut.wait_for_boot_completion() 274 wutils.wait_for_connect(self.dut, 275 self.wpa2_psk_2g[WifiEnums.SSID_KEY]) 276 wutils.validate_connection(self.dut) 277 278 @test_tracker_info(uuid="7cb462c8-0a6e-4f33-b2fc-bf630d57e087") 279 def test_connect_to_wpa2_psk_ccmp_2g_after_incorrect_password(self): 280 """Test Wi-Fi reconnection after incorrect password. 281 282 Steps: 283 DUT connect to 2GHz Wi-Fi network. 284 DUT try to connect to the Wi-Fi network with incorrect password. 285 Connection fail as expected. 286 DUT connect to the Wi-Fi network with correct password. 287 DUT verify internet connection with HTTP ping. 288 """ 289 self.start_openwrt() 290 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 291 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 292 self.wpa2_psk_2g_fail = self.wpa2_psk_2g.copy() 293 self.wpa2_psk_2g_fail["password"] = "incorrect_password" 294 # Try to connect a Wi-Fi network with incorrect password. 295 try: 296 self.dut.log.info("Connect to Wi-Fi with wrong password") 297 wutils.wifi_connect(self.dut, self.wpa2_psk_2g_fail, num_of_tries=1) 298 except: 299 self.dut.log.info("Connect to Wi-Fi with correct password") 300 wutils.wifi_connect(self.dut, self.wpa2_psk_2g) 301 else: 302 raise signals.TestFailure("DUT connect to Wi-Fi with wrong password") 303 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 304 wutils.validate_connection(self.dut) 305 306 @test_tracker_info(uuid="890712de-2a3e-4b24-8529-e93893c8d99c") 307 def test_connect_to_wpa2_psk_ccmp_2g_after_out_of_range(self): 308 """Test Wi-Fi reconnection after out of range. 309 310 Steps: 311 DUT connect to 2GHz Wi-Fi network. 312 DUT out of Wi-Fi range. 313 Make Wi-Fi network is not visible by DUT. 314 DUT back in Wi-Fi range. 315 Wi-Fi network is visible by DUT. 316 DUT connect to the Wi-Fi network. 317 DUT verify internet connection with HTTP ping. 318 """ 319 self.start_openwrt() 320 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 321 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 322 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 323 # Make the DUT out of range. 324 wutils.set_attns(self.attenuators, 325 "atten1_off_atten2_off", 326 self.roaming_attn) 327 wutils.wait_for_disconnect(self.dut, 30) # 30s is experimental choice 328 wutils.start_wifi_connection_scan_and_ensure_network_not_found( 329 self.dut, 330 self.wpa2_psk_2g[WifiEnums.SSID_KEY]) 331 # Make the DUT back in range. 332 wutils.set_attns(self.attenuators, 333 "atten1_on_atten2_on", 334 self.roaming_attn) 335 wutils.start_wifi_connection_scan_and_ensure_network_found( 336 self.dut, self.wpa2_psk_2g[WifiEnums.SSID_KEY]) 337 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 338