1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wifi; 18 19 import static android.net.wifi.WifiManager.WIFI_FEATURE_CONTROL_ROAMING; 20 21 import static org.junit.Assert.*; 22 import static org.mockito.Mockito.*; 23 24 import android.app.test.MockAnswerUtil.AnswerWithArguments; 25 26 import androidx.test.filters.SmallTest; 27 28 import org.junit.After; 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.mockito.ArgumentCaptor; 32 import org.mockito.Captor; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 36 import java.util.ArrayList; 37 38 /** 39 * Unit tests for {@link com.android.server.wifi.WifiConnectivityHelper}. 40 */ 41 @SmallTest 42 public class WifiConnectivityHelperTest { 43 /** Sets up test. */ 44 @Before setUp()45 public void setUp() throws Exception { 46 MockitoAnnotations.initMocks(this); 47 setupWifiNative(); 48 49 mWifiConnectivityHelper = new WifiConnectivityHelper(mWifiNative); 50 } 51 52 /** Cleans up test. */ 53 @After cleanup()54 public void cleanup() { 55 validateMockitoUsage(); 56 } 57 58 private WifiConnectivityHelper mWifiConnectivityHelper; 59 @Mock private WifiNative mWifiNative; 60 @Captor ArgumentCaptor<WifiNative.RoamingConfig> mRoamingConfigCaptor; 61 private int mFeatureSetValue; 62 private static final String TAG = "WifiConnectivityHelperTest"; 63 private static final int MAX_BSSID_BLACKLIST_SIZE = 16; 64 private static final int MAX_SSID_WHITELIST_SIZE = 8; 65 setupWifiNative()66 private void setupWifiNative() { 67 // Return firmware roaming feature as supported by default. 68 when(mWifiNative.getSupportedFeatureSet(any())) 69 .thenReturn((long) WIFI_FEATURE_CONTROL_ROAMING); 70 71 doAnswer(new AnswerWithArguments() { 72 public boolean answer(String ifaceName, WifiNative.RoamingCapabilities roamCap) 73 throws Exception { 74 roamCap.maxBlocklistSize = MAX_BSSID_BLACKLIST_SIZE; 75 roamCap.maxAllowlistSize = MAX_SSID_WHITELIST_SIZE; 76 return true; 77 }}).when(mWifiNative).getRoamingCapabilities(any(), anyObject()); 78 79 when(mWifiNative.configureRoaming(any(), anyObject())).thenReturn(true); 80 } 81 buildBssidBlacklist(int size)82 private ArrayList<String> buildBssidBlacklist(int size) { 83 ArrayList<String> bssidBlacklist = new ArrayList<String>(); 84 85 for (int i = 0; i < size; i++) { 86 StringBuilder bssid = new StringBuilder("11:22:33:44:55:66"); 87 bssid.setCharAt(16, (char) ('0' + i)); 88 bssidBlacklist.add(bssid.toString()); 89 } 90 91 return bssidBlacklist; 92 } 93 buildSsidWhitelist(int size)94 private ArrayList<String> buildSsidWhitelist(int size) { 95 ArrayList<String> ssidWhitelist = new ArrayList<String>(); 96 97 for (int i = 0; i < size; i++) { 98 StringBuilder ssid = new StringBuilder("\"Test_Ap_0\""); 99 ssid.setCharAt(9, (char) ('0' + i)); 100 ssidWhitelist.add(ssid.toString()); 101 } 102 103 return ssidWhitelist; 104 } 105 106 /** 107 * When WifiNative has WIFI_FEATURE_CONTROL_ROAMING set, verify that 108 * WifiConnectivityHelper#isFirmwareRoamingSupported returns true. 109 */ 110 @Test returnFirmwareRoamingSupported()111 public void returnFirmwareRoamingSupported() { 112 //By default WifiNative has WIFI_FEATURE_CONTROL_ROAMING set in its feature set. 113 assertTrue(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 114 assertTrue(mWifiConnectivityHelper.isFirmwareRoamingSupported()); 115 } 116 117 /** 118 * When WifiNative doesn't have WIFI_FEATURE_CONTROL_ROAMING set, verify that 119 * WifiConnectivityHelper#isFirmwareRoamingSupported returns false. 120 */ 121 @Test returnFirmwareRoamingNotSupported()122 public void returnFirmwareRoamingNotSupported() { 123 when(mWifiNative.getSupportedFeatureSet(any())) 124 .thenReturn((long) ~WIFI_FEATURE_CONTROL_ROAMING); 125 assertTrue(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 126 assertFalse(mWifiConnectivityHelper.isFirmwareRoamingSupported()); 127 } 128 129 /** 130 * Verify that correct firmware roaming capability values are returned if querying 131 * WifiNative for roaming capability succeeded. 132 */ 133 @Test verifyFirmwareRoamingCapabilityWithSuccessfulNativeCall()134 public void verifyFirmwareRoamingCapabilityWithSuccessfulNativeCall() { 135 assertTrue(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 136 assertTrue(mWifiConnectivityHelper.isFirmwareRoamingSupported()); 137 assertEquals(MAX_BSSID_BLACKLIST_SIZE, mWifiConnectivityHelper.getMaxNumBlacklistBssid()); 138 assertEquals(MAX_SSID_WHITELIST_SIZE, mWifiConnectivityHelper.getMaxNumWhitelistSsid()); 139 } 140 141 /** 142 * Verify that firmware roaming is set to not supported if WifiNative returned firmware roaming 143 * is supported but failed to return roaming capabilities. Firmware roaming capabilty values 144 * should be reset to INVALID_LIST_SIZE. 145 */ 146 @Test verifyFirmwareRoamingCapabilityWithFailureNativeCall()147 public void verifyFirmwareRoamingCapabilityWithFailureNativeCall() { 148 doAnswer(new AnswerWithArguments() { 149 public boolean answer(String ifaceName, WifiNative.RoamingCapabilities roamCap) 150 throws Exception { 151 return false; 152 }}).when(mWifiNative).getRoamingCapabilities(any(), anyObject()); 153 assertFalse(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 154 assertFalse(mWifiConnectivityHelper.isFirmwareRoamingSupported()); 155 assertEquals(WifiConnectivityHelper.INVALID_LIST_SIZE, 156 mWifiConnectivityHelper.getMaxNumBlacklistBssid()); 157 assertEquals(WifiConnectivityHelper.INVALID_LIST_SIZE, 158 mWifiConnectivityHelper.getMaxNumWhitelistSsid()); 159 } 160 161 /** 162 * Verify that firmware roaming is set to not supported if WifiNative returned firmware roaming 163 * is supported but returned invalid max BSSID balcklist size. Firmware roaming capabilty values 164 * should be reset to INVALID_LIST_SIZE. 165 */ 166 @Test verifyFirmwareRoamingCapabilityWithInvalidMaxBssidBlacklistSize()167 public void verifyFirmwareRoamingCapabilityWithInvalidMaxBssidBlacklistSize() { 168 doAnswer(new AnswerWithArguments() { 169 public boolean answer(String ifaceName, WifiNative.RoamingCapabilities roamCap) 170 throws Exception { 171 roamCap.maxBlocklistSize = -5; 172 roamCap.maxAllowlistSize = MAX_SSID_WHITELIST_SIZE; 173 return true; 174 }}).when(mWifiNative).getRoamingCapabilities(any(), anyObject()); 175 assertFalse(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 176 assertFalse(mWifiConnectivityHelper.isFirmwareRoamingSupported()); 177 assertEquals(WifiConnectivityHelper.INVALID_LIST_SIZE, 178 mWifiConnectivityHelper.getMaxNumBlacklistBssid()); 179 assertEquals(WifiConnectivityHelper.INVALID_LIST_SIZE, 180 mWifiConnectivityHelper.getMaxNumWhitelistSsid()); 181 } 182 183 /** 184 * Verify that firmware roaming is set to not supported if WifiNative returned firmware roaming 185 * is supported but returned invalid max SSID whitelist size. Firmware roaming capabilty values 186 * should be reset to INVALID_LIST_SIZE. 187 */ 188 @Test verifyFirmwareRoamingCapabilityWithInvalidMaxSsidWhitelistSize()189 public void verifyFirmwareRoamingCapabilityWithInvalidMaxSsidWhitelistSize() { 190 doAnswer(new AnswerWithArguments() { 191 public boolean answer(String ifaceName, WifiNative.RoamingCapabilities roamCap) 192 throws Exception { 193 roamCap.maxBlocklistSize = MAX_BSSID_BLACKLIST_SIZE; 194 roamCap.maxAllowlistSize = -2; 195 return true; 196 }}).when(mWifiNative).getRoamingCapabilities(any(), anyObject()); 197 assertFalse(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 198 assertFalse(mWifiConnectivityHelper.isFirmwareRoamingSupported()); 199 assertEquals(WifiConnectivityHelper.INVALID_LIST_SIZE, 200 mWifiConnectivityHelper.getMaxNumBlacklistBssid()); 201 assertEquals(WifiConnectivityHelper.INVALID_LIST_SIZE, 202 mWifiConnectivityHelper.getMaxNumWhitelistSsid()); 203 } 204 205 /** 206 * Verify that correct size BSSID blacklist and SSID whitelist are accepted. 207 */ 208 @Test verifySetFirmwareRoamingConfigurationWithGoodInput()209 public void verifySetFirmwareRoamingConfigurationWithGoodInput() { 210 assertTrue(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 211 ArrayList<String> blacklist = buildBssidBlacklist(MAX_BSSID_BLACKLIST_SIZE); 212 ArrayList<String> whitelist = buildSsidWhitelist(MAX_SSID_WHITELIST_SIZE); 213 assertTrue(mWifiConnectivityHelper.setFirmwareRoamingConfiguration(blacklist, whitelist)); 214 215 blacklist = buildBssidBlacklist(MAX_BSSID_BLACKLIST_SIZE - 2); 216 whitelist = buildSsidWhitelist(MAX_SSID_WHITELIST_SIZE - 3); 217 assertTrue(mWifiConnectivityHelper.setFirmwareRoamingConfiguration(blacklist, whitelist)); 218 } 219 220 /** 221 * Verify that null BSSID blacklist or SSID whitelist is rejected. 222 */ 223 @Test verifySetFirmwareRoamingConfigurationWithNullInput()224 public void verifySetFirmwareRoamingConfigurationWithNullInput() { 225 assertTrue(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 226 ArrayList<String> blacklist = buildBssidBlacklist(MAX_BSSID_BLACKLIST_SIZE); 227 ArrayList<String> whitelist = buildSsidWhitelist(MAX_SSID_WHITELIST_SIZE); 228 assertFalse(mWifiConnectivityHelper.setFirmwareRoamingConfiguration(null, whitelist)); 229 assertFalse(mWifiConnectivityHelper.setFirmwareRoamingConfiguration(blacklist, null)); 230 } 231 232 /** 233 * Verify that incorrect size BSSID blacklist is rejected. 234 */ 235 @Test verifySetFirmwareRoamingConfigurationWithIncorrectBlacklist()236 public void verifySetFirmwareRoamingConfigurationWithIncorrectBlacklist() { 237 assertTrue(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 238 ArrayList<String> blacklist = buildBssidBlacklist(MAX_BSSID_BLACKLIST_SIZE + 1); 239 ArrayList<String> whitelist = buildSsidWhitelist(MAX_SSID_WHITELIST_SIZE); 240 assertFalse(mWifiConnectivityHelper.setFirmwareRoamingConfiguration(blacklist, whitelist)); 241 } 242 243 /** 244 * Verify that incorrect size SSID whitelist is rejected. 245 */ 246 @Test verifySetFirmwareRoamingConfigurationWithIncorrectWhitelist()247 public void verifySetFirmwareRoamingConfigurationWithIncorrectWhitelist() { 248 assertTrue(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 249 ArrayList<String> blacklist = buildBssidBlacklist(MAX_BSSID_BLACKLIST_SIZE); 250 ArrayList<String> whitelist = buildSsidWhitelist(MAX_SSID_WHITELIST_SIZE + 1); 251 assertFalse(mWifiConnectivityHelper.setFirmwareRoamingConfiguration(blacklist, whitelist)); 252 } 253 254 /** 255 * Verify that empty BSSID blacklist and SSID whitelist are sent to WifiNative 256 * to reset the firmware roaming configuration. 257 */ 258 @Test verifySetFirmwareRoamingConfigurationWithEmptyBlacklistAndWhitelist()259 public void verifySetFirmwareRoamingConfigurationWithEmptyBlacklistAndWhitelist() { 260 assertTrue(mWifiConnectivityHelper.getFirmwareRoamingInfo()); 261 ArrayList<String> blacklist = buildBssidBlacklist(0); 262 ArrayList<String> whitelist = buildSsidWhitelist(0); 263 assertTrue(mWifiConnectivityHelper.setFirmwareRoamingConfiguration(blacklist, whitelist)); 264 verify(mWifiNative).configureRoaming(any(), mRoamingConfigCaptor.capture()); 265 assertEquals(0, mRoamingConfigCaptor.getValue().blocklistBssids.size()); 266 assertEquals(0, mRoamingConfigCaptor.getValue().allowlistSsids.size()); 267 } 268 } 269