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 17 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 24 25 26WifiEnums = wutils.WifiEnums 27 28 29class WifiWpa2PersonalTest(WifiBaseTest): 30 """ Wi-Fi WPA2 test 31 32 Test Bed Requirement: 33 * One Android device 34 * One OpenWrt Wi-Fi AP. 35 """ 36 def __init__(self, configs): 37 super().__init__(configs) 38 self.enable_packet_log = True 39 40 def setup_class(self): 41 super().setup_class() 42 self.dut = self.android_devices[0] 43 opt_params = ["pixel_models", "cnss_diag_file"] 44 self.unpack_userparams(opt_params) 45 46 def setup_test(self): 47 super().setup_test() 48 for ad in self.android_devices: 49 ad.droid.wakeLockAcquireBright() 50 ad.droid.wakeUpNow() 51 wutils.wifi_toggle_state(ad, True) 52 wutils.reset_wifi(self.dut) 53 54 def teardown_test(self): 55 super().teardown_test() 56 for ad in self.android_devices: 57 ad.droid.wakeLockRelease() 58 ad.droid.goToSleepNow() 59 wutils.reset_wifi(self.dut) 60 61 def start_openwrt(self, channel_2g=None, channel_5g=None): 62 """Enable one OpenWrt to generate a Wi-Fi network. 63 64 Args: 65 channel_2g: Optional; An integer to represent a Wi-Fi 2g channel. 66 The default value is 6 if it's not given. 67 channel_5g: Optional; An integer to represent a Wi-Fi 5g channel. 68 The default value is 36 if it's not given. 69 """ 70 if not channel_2g: 71 channel_2g = hostapd_constants.AP_DEFAULT_CHANNEL_2G 72 if not channel_5g: 73 channel_5g = hostapd_constants.AP_DEFAULT_CHANNEL_5G 74 if "OpenWrtAP" in self.user_params: 75 self.openwrt = self.access_points[0] 76 self.configure_openwrt_ap_and_start(wpa_network=True, 77 channel_2g=channel_2g, 78 channel_5g=channel_5g) 79 self.wpa2_psk_2g = self.wpa_networks[0]["2g"] 80 self.wpa2_psk_5g = self.wpa_networks[0]["5g"] 81 82 def verify_wpa_network_encryption(self, encryption): 83 result = wutils.get_wlan0_link(self.dut) 84 if encryption == 'psk2+ccmp': 85 asserts.assert_true( 86 result['pairwise_cipher'] == 'CCMP' and 87 result['group_cipher'] == 'CCMP' and 88 result['key_mgmt'] == "WPA2-PSK", 89 'DUT does not connect to {} encryption network'.format(encryption)) 90 elif encryption == 'psk2+tkip': 91 asserts.assert_true( 92 result['pairwise_cipher'] == 'TKIP' and 93 result['group_cipher'] == 'TKIP' and 94 result['key_mgmt'] == "WPA2-PSK", 95 'DUT does not connect to {} encryption network'.format(encryption)) 96 elif encryption == 'psk2+tkip+ccmp': 97 asserts.assert_true( 98 result['pairwise_cipher'] == 'CCMP' and 99 result['group_cipher'] == 'TKIP' and 100 result['key_mgmt'] == "WPA2-PSK", 101 'DUT does not connect to {} encryption network'.format(encryption)) 102 103 """ Tests""" 104 105 @test_tracker_info(uuid="d1f984c9-d85f-4b0d-8d64-2e8d6ce74c48") 106 def test_connect_to_wpa2_psk_ccmp_2g(self): 107 """Generate a Wi-Fi network. 108 Change AP's security type to "WPA2" and cipher to "CCMP". 109 Connect to 2g network. 110 """ 111 self.start_openwrt() 112 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 113 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 114 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 115 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 116 117 @test_tracker_info(uuid="0f9631e8-04a9-4b9c-8225-ab30b4d1173b") 118 def test_connect_to_wpa2_psk_ccmp_5g(self): 119 """Generate a Wi-Fi network. 120 Change AP's security type to "WPA2" and cipher to "CCMP". 121 Connect to 5g network. 122 """ 123 self.start_openwrt() 124 self.openwrt.log.info("Enable WPA2-PSK CCMP on OpenWrt AP") 125 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 126 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_5g) 127 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_CCMP) 128 129 @test_tracker_info(uuid="e6eb3932-10cc-476f-a5d7-936e2631afc1") 130 def test_connect_to_wpa2_psk_tkip_2g(self): 131 """Generate a Wi-Fi network. 132 Change AP's security type to "WPA2" and cipher to "TKIP". 133 Connect to 2g network. 134 """ 135 self.start_openwrt() 136 self.openwrt.log.info("Enable WPA2-PSK TKIP on OpenWrt AP") 137 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP) 138 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 139 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP) 140 141 @test_tracker_info(uuid="59ba3cd4-dbc5-44f9-9290-48ae468a51da") 142 def test_connect_to_wpa2_psk_tkip_5g(self): 143 """Generate a Wi-Fi network. 144 Change AP's security type to "WPA2" and cipher to "TKIP". 145 Connect to 5g network. 146 """ 147 self.start_openwrt() 148 self.openwrt.log.info("Enable WPA2-PSK TKIP on OpenWrt AP") 149 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP) 150 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_5g) 151 self.verify_wpa_network_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP) 152 153 @test_tracker_info(uuid="a06be3db-d653-4549-95f3-87bbeb0db813") 154 def test_connect_to_wpa2_psk_tkip_and_ccmp_2g(self): 155 """Generate a Wi-Fi network. 156 Change AP's security type to "WPA2" and cipher to "CCMP and TKIP". 157 Connect to 2g network. 158 """ 159 self.start_openwrt() 160 self.openwrt.log.info("Enable WPA2-PSK CCMP and TKIP on OpenWrt AP") 161 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP_AND_CCMP) 162 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_2g) 163 self.verify_wpa_network_encryption( 164 OpenWrtWifiSecurity.WPA2_PSK_TKIP_AND_CCMP) 165 166 @test_tracker_info(uuid="ac9b9581-0b32-42b4-8e76-de702c837b86") 167 def test_connect_to_wpa2_psk_tkip_and_ccmp_5g(self): 168 """Generate a Wi-Fi network. 169 Change AP's security type to "WPA2" and cipher to "CCMP and TKIP". 170 Connect to 5g network. 171 """ 172 self.start_openwrt() 173 self.openwrt.log.info("Enable WPA2-PSK CCMP and TKIP on OpenWrt AP") 174 self.openwrt.set_wpa_encryption(OpenWrtWifiSecurity.WPA2_PSK_TKIP_AND_CCMP) 175 wutils.connect_to_wifi_network(self.dut, self.wpa2_psk_5g) 176 self.verify_wpa_network_encryption( 177 OpenWrtWifiSecurity.WPA2_PSK_TKIP_AND_CCMP) 178