1 /* 2 * Copyright (C) 2018 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.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; 20 import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE; 21 import static android.app.AppOpsManager.MODE_ALLOWED; 22 import static android.app.AppOpsManager.MODE_IGNORED; 23 import static android.app.AppOpsManager.OPSTR_CHANGE_WIFI_STATE; 24 import static android.net.wifi.WifiManager.ACTION_REMOVE_SUGGESTION_DISCONNECT; 25 import static android.net.wifi.WifiManager.ACTION_REMOVE_SUGGESTION_LINGER; 26 27 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; 28 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; 29 import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; 30 import static com.android.server.wifi.WifiNetworkSuggestionsManager.DEFAULT_LINGER_DELAY_MS; 31 import static com.android.server.wifi.WifiNetworkSuggestionsManager.NOTIFICATION_USER_ALLOWED_APP_INTENT_ACTION; 32 import static com.android.server.wifi.WifiNetworkSuggestionsManager.NOTIFICATION_USER_DISALLOWED_APP_INTENT_ACTION; 33 import static com.android.server.wifi.WifiNetworkSuggestionsManager.NOTIFICATION_USER_DISMISSED_INTENT_ACTION; 34 import static com.android.server.wifi.TestUtil.createCapabilityBitset; 35 36 import static org.junit.Assert.assertEquals; 37 import static org.junit.Assert.assertFalse; 38 import static org.junit.Assert.assertNotNull; 39 import static org.junit.Assert.assertNull; 40 import static org.junit.Assert.assertTrue; 41 import static org.junit.Assume.assumeFalse; 42 import static org.junit.Assume.assumeTrue; 43 import static org.mockito.Mockito.any; 44 import static org.mockito.Mockito.anyBoolean; 45 import static org.mockito.Mockito.anyInt; 46 import static org.mockito.Mockito.anyLong; 47 import static org.mockito.Mockito.anyString; 48 import static org.mockito.Mockito.argThat; 49 import static org.mockito.Mockito.atLeastOnce; 50 import static org.mockito.Mockito.calls; 51 import static org.mockito.Mockito.doThrow; 52 import static org.mockito.Mockito.eq; 53 import static org.mockito.Mockito.inOrder; 54 import static org.mockito.Mockito.isNull; 55 import static org.mockito.Mockito.lenient; 56 import static org.mockito.Mockito.mock; 57 import static org.mockito.Mockito.never; 58 import static org.mockito.Mockito.nullable; 59 import static org.mockito.Mockito.reset; 60 import static org.mockito.Mockito.times; 61 import static org.mockito.Mockito.verify; 62 import static org.mockito.Mockito.verifyNoMoreInteractions; 63 64 import android.app.ActivityManager; 65 import android.app.AppOpsManager; 66 import android.app.Notification; 67 import android.app.PendingIntent; 68 import android.content.BroadcastReceiver; 69 import android.content.Context; 70 import android.content.Intent; 71 import android.content.res.Resources; 72 import android.net.wifi.EAPConstants; 73 import android.net.wifi.ISuggestionConnectionStatusListener; 74 import android.net.wifi.ISuggestionUserApprovalStatusListener; 75 import android.net.wifi.ScanResult; 76 import android.net.wifi.WifiConfiguration; 77 import android.net.wifi.WifiContext; 78 import android.net.wifi.WifiEnterpriseConfig; 79 import android.net.wifi.WifiManager; 80 import android.net.wifi.WifiNetworkSuggestion; 81 import android.net.wifi.WifiScanner; 82 import android.net.wifi.WifiSsid; 83 import android.net.wifi.hotspot2.PasspointConfiguration; 84 import android.net.wifi.hotspot2.pps.Credential; 85 import android.net.wifi.hotspot2.pps.HomeSp; 86 import android.os.IBinder; 87 import android.os.ParcelUuid; 88 import android.os.RemoteException; 89 import android.os.UserHandle; 90 import android.os.test.TestLooper; 91 import android.telephony.SubscriptionManager; 92 import android.telephony.TelephonyManager; 93 import android.text.TextUtils; 94 import android.util.LocalLog; 95 import android.view.LayoutInflater; 96 97 import androidx.test.filters.SmallTest; 98 99 import com.android.dx.mockito.inline.extended.ExtendedMockito; 100 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; 101 import com.android.modules.utils.build.SdkLevel; 102 import com.android.net.module.util.MacAddressUtils; 103 import com.android.server.wifi.WifiNetworkSuggestionsManager.ExtendedWifiNetworkSuggestion; 104 import com.android.server.wifi.WifiNetworkSuggestionsManager.PerAppInfo; 105 import com.android.server.wifi.hotspot2.PasspointManager; 106 import com.android.server.wifi.util.LruConnectionTracker; 107 import com.android.server.wifi.util.WifiPermissionsUtil; 108 import com.android.wifi.resources.R; 109 110 import org.junit.After; 111 import org.junit.Before; 112 import org.junit.Test; 113 import org.mockito.ArgumentCaptor; 114 import org.mockito.ArgumentMatcher; 115 import org.mockito.InOrder; 116 import org.mockito.Mock; 117 import org.mockito.MockitoAnnotations; 118 import org.mockito.MockitoSession; 119 import org.mockito.quality.Strictness; 120 import org.mockito.stubbing.Answer; 121 122 import java.util.ArrayList; 123 import java.util.Arrays; 124 import java.util.Collection; 125 import java.util.Collections; 126 import java.util.HashMap; 127 import java.util.HashSet; 128 import java.util.List; 129 import java.util.Map; 130 import java.util.Set; 131 import java.util.stream.Collectors; 132 133 /** 134 * Unit tests for {@link com.android.server.wifi.WifiNetworkSuggestionsManager}. 135 */ 136 @SmallTest 137 public class WifiNetworkSuggestionsManagerTest extends WifiBaseTest { 138 139 private static final String TEST_PACKAGE_1 = "com.test12345"; 140 private static final String TEST_PACKAGE_2 = "com.test54321"; 141 private static final String TEST_PACKAGE_3 = "com.test54321"; 142 private static final String TEST_APP_NAME_1 = "test12345"; 143 private static final String TEST_APP_NAME_2 = "test54321"; 144 private static final String TEST_APP_NAME_3 = "test66666"; 145 private static final String TEST_FEATURE = "testFeature"; 146 private static final String TEST_BSSID = "00:11:22:33:44:55"; 147 private static final String TEST_FQDN = "FQDN"; 148 private static final String TEST_FRIENDLY_NAME = "test_friendly_name"; 149 private static final String TEST_REALM = "realm.test.com"; 150 private static final String TEST_CARRIER_NAME = "test_carrier"; 151 private static final int TEST_UID_1 = 5667; 152 private static final int TEST_UID_2 = 4537; 153 private static final int TEST_UID_3 = 7837; 154 private static final int VALID_CARRIER_ID = 100; 155 private static final int TEST_SUBID = 1; 156 private static final int TEST_NETWORK_ID = 110; 157 private static final int TEST_CARRIER_ID = 1911; 158 private static final String TEST_IMSI = "123456*"; 159 private static final int DEFAULT_PRIORITY_GROUP = 0; 160 private static final int TEST_PRIORITY_GROUP = 1; 161 private static final String TEST_ANONYMOUS_IDENTITY_1 = "AnonymousIdentity1"; 162 private static final String TEST_ANONYMOUS_IDENTITY_2 = "AnonymousIdentity2"; 163 private static final String USER_CONNECT_CHOICE = "SomeNetworkProfileId"; 164 private static final int TEST_RSSI = -50; 165 private static final ParcelUuid GROUP_UUID = ParcelUuid 166 .fromString("0000110B-0000-1000-8000-00805F9B34FB"); 167 private static final String UNTRANSLATED_HEX_SSID = "abcdef"; 168 169 private @Mock WifiContext mContext; 170 private @Mock Resources mResources; 171 private @Mock AppOpsManager mAppOpsManager; 172 private @Mock WifiNotificationManager mWifiNotificationManager; 173 private @Mock WifiDialogManager mWifiDialogManager; 174 private @Mock WifiPermissionsUtil mWifiPermissionsUtil; 175 private @Mock WifiInjector mWifiInjector; 176 private @Mock FrameworkFacade mFrameworkFacade; 177 private @Mock WifiConfigStore mWifiConfigStore; 178 private @Mock WifiConfigManager mWifiConfigManager; 179 private @Mock NetworkSuggestionStoreData mNetworkSuggestionStoreData; 180 private @Mock WifiMetrics mWifiMetrics; 181 private @Mock WifiCarrierInfoManager mWifiCarrierInfoManager; 182 private @Mock PasspointManager mPasspointManager; 183 private @Mock ISuggestionConnectionStatusListener mConnectionStatusListener; 184 private @Mock ISuggestionUserApprovalStatusListener mUserApprovalStatusListener; 185 private @Mock IBinder mBinder; 186 private @Mock ActivityManager mActivityManager; 187 private @Mock WifiScoreCard mWifiScoreCard; 188 private @Mock WifiKeyStore mWifiKeyStore; 189 private @Mock WifiDialogManager.DialogHandle mDialogHandle; 190 private @Mock Notification.Builder mNotificationBuilder; 191 private @Mock Notification mNotification; 192 private @Mock LruConnectionTracker mLruConnectionTracker; 193 private @Mock ActiveModeWarden mActiveModeWarden; 194 private @Mock ClientModeManager mPrimaryClientModeManager; 195 private @Mock WifiGlobals mWifiGlobals; 196 private @Mock SsidTranslator mSsidTranslator; 197 private @Mock Clock mClock; 198 private MockitoSession mStaticMockSession = null; 199 private TestLooper mLooper; 200 private final ArgumentCaptor<AppOpsManager.OnOpChangedListener> mAppOpChangedListenerCaptor = 201 ArgumentCaptor.forClass(AppOpsManager.OnOpChangedListener.class); 202 private final ArgumentCaptor<BroadcastReceiver> mBroadcastReceiverCaptor = 203 ArgumentCaptor.forClass(BroadcastReceiver.class); 204 private final ArgumentCaptor<WifiCarrierInfoManager.OnImsiProtectedOrUserApprovedListener> 205 mUserApproveCarrierListenerArgumentCaptor = ArgumentCaptor.forClass( 206 WifiCarrierInfoManager.OnImsiProtectedOrUserApprovedListener.class); 207 private final ArgumentCaptor<WifiConfigManager.OnNetworkUpdateListener> mNetworkListenerCaptor = 208 ArgumentCaptor.forClass(WifiConfigManager.OnNetworkUpdateListener.class); 209 210 private InOrder mInorder; 211 212 private WifiNetworkSuggestionsManager mWifiNetworkSuggestionsManager; 213 private NetworkSuggestionStoreData.DataSource mDataSource; 214 215 /** 216 * Setup the mocks. 217 */ 218 @Before setUp()219 public void setUp() throws Exception { 220 MockitoAnnotations.initMocks(this); 221 mStaticMockSession = mockitoSession() 222 .mockStatic(WifiInjector.class) 223 .startMocking(); 224 lenient().when(WifiInjector.getInstance()).thenReturn(mWifiInjector); 225 mLooper = new TestLooper(); 226 227 mInorder = inOrder(mContext, mWifiPermissionsUtil); 228 229 when(mWifiInjector.makeNetworkSuggestionStoreData(any())) 230 .thenReturn(mNetworkSuggestionStoreData); 231 when(mWifiInjector.getFrameworkFacade()).thenReturn(mFrameworkFacade); 232 when(mWifiInjector.getPasspointManager()).thenReturn(mPasspointManager); 233 when(mWifiInjector.getWifiScoreCard()).thenReturn(mWifiScoreCard); 234 when(mWifiInjector.getActiveModeWarden()).thenReturn(mActiveModeWarden); 235 when(mWifiInjector.getWifiGlobals()).thenReturn(mWifiGlobals); 236 when(mWifiInjector.getWifiNotificationManager()).thenReturn(mWifiNotificationManager); 237 when(mWifiInjector.getWifiDialogManager()).thenReturn(mWifiDialogManager); 238 when(mWifiInjector.getSsidTranslator()).thenReturn(mSsidTranslator); 239 when(mNotificationBuilder.setSmallIcon(any())).thenReturn(mNotificationBuilder); 240 when(mNotificationBuilder.setTicker(any())).thenReturn(mNotificationBuilder); 241 when(mNotificationBuilder.setContentTitle(any())).thenReturn(mNotificationBuilder); 242 when(mNotificationBuilder.setStyle(any())).thenReturn(mNotificationBuilder); 243 when(mNotificationBuilder.setDeleteIntent(any())).thenReturn(mNotificationBuilder); 244 when(mNotificationBuilder.setShowWhen(anyBoolean())).thenReturn(mNotificationBuilder); 245 when(mNotificationBuilder.setLocalOnly(anyBoolean())).thenReturn(mNotificationBuilder); 246 when(mNotificationBuilder.setColor(anyInt())).thenReturn(mNotificationBuilder); 247 when(mNotificationBuilder.addAction(any())).thenReturn(mNotificationBuilder); 248 when(mNotificationBuilder.setTimeoutAfter(anyLong())).thenReturn(mNotificationBuilder); 249 when(mNotificationBuilder.build()).thenReturn(mNotification); 250 when(mWifiDialogManager.createSimpleDialog(any(), any(), any(), any(), any(), any(), any())) 251 .thenReturn(mDialogHandle); 252 when(mFrameworkFacade.makeNotificationBuilder(any(), anyString())) 253 .thenReturn(mNotificationBuilder); 254 when(mFrameworkFacade.getBroadcast(any(), anyInt(), any(), anyInt())) 255 .thenReturn(mock(PendingIntent.class)); 256 when(mContext.getResources()).thenReturn(mResources); 257 when(mContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mAppOpsManager); 258 when(mContext.getSystemService(ActivityManager.class)).thenReturn(mActivityManager); 259 when(mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)) 260 .thenReturn(mock(LayoutInflater.class)); 261 when(mContext.getWifiOverlayApkPkgName()).thenReturn("test.com.android.wifi.resources"); 262 when(mActivityManager.isLowRamDevice()).thenReturn(false); 263 when(mActivityManager.getPackageImportance(any())).thenReturn( 264 IMPORTANCE_FOREGROUND_SERVICE); 265 when(mWifiPermissionsUtil.doesUidBelongToCurrentUserOrDeviceOwner(anyInt())) 266 .thenReturn(true); 267 when(mActiveModeWarden.getPrimaryClientModeManager()).thenReturn(mPrimaryClientModeManager); 268 when(mPrimaryClientModeManager.getSupportedFeatures()).thenReturn( 269 createCapabilityBitset( 270 WifiManager.WIFI_FEATURE_WPA3_SAE, WifiManager.WIFI_FEATURE_OWE)); 271 when(mWifiGlobals.isWpa3SaeUpgradeEnabled()).thenReturn(true); 272 when(mWifiGlobals.isOweUpgradeEnabled()).thenReturn(true); 273 when(mSsidTranslator.getAllPossibleOriginalSsids(any())).thenAnswer( 274 (Answer<List<WifiSsid>>) invocation -> Arrays.asList(invocation.getArgument(0), 275 WifiSsid.fromString(UNTRANSLATED_HEX_SSID)) 276 ); 277 when(mConnectionStatusListener.asBinder()).thenReturn(mBinder); 278 when(mUserApprovalStatusListener.asBinder()).thenReturn(mBinder); 279 280 // setup resource strings for notification. 281 when(mResources.getString(eq(R.string.wifi_suggestion_title), anyString())) 282 .thenAnswer(s -> "blah" + s.getArguments()[1]); 283 when(mResources.getString(eq(R.string.wifi_suggestion_content), anyString())) 284 .thenAnswer(s -> "blah" + s.getArguments()[1]); 285 when(mResources.getText(eq(R.string.wifi_suggestion_action_allow_app))) 286 .thenReturn("blah"); 287 when(mResources.getText(eq(R.string.wifi_suggestion_action_disallow_app))) 288 .thenReturn("blah"); 289 290 // test app info 291 when(mFrameworkFacade.getAppName(any(), eq(TEST_PACKAGE_1), eq(TEST_UID_1))) 292 .thenReturn(TEST_APP_NAME_1); 293 when(mFrameworkFacade.getAppName(any(), eq(TEST_PACKAGE_2), eq(TEST_UID_2))) 294 .thenReturn(TEST_APP_NAME_2); 295 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(any())).thenReturn( 296 TelephonyManager.UNKNOWN_CARRIER_ID); 297 when(mWifiCarrierInfoManager.isSubIdMatchingCarrierId(anyInt(), anyInt())).thenReturn(true); 298 when(mWifiCarrierInfoManager.getBestMatchSubscriptionId(any())).thenReturn( 299 SubscriptionManager.INVALID_SUBSCRIPTION_ID); 300 when(mWifiCarrierInfoManager.isSimReady(SubscriptionManager.INVALID_SUBSCRIPTION_ID)) 301 .thenReturn(false); 302 when(mWifiCarrierInfoManager.areMergedCarrierWifiNetworksAllowed(anyInt())).thenReturn( 303 false); 304 when(mWifiCarrierInfoManager.getActiveSubscriptionIdInGroup(GROUP_UUID)) 305 .thenReturn(TEST_SUBID); 306 307 when(mWifiKeyStore.updateNetworkKeys(any(), any())).thenReturn(true); 308 309 mWifiNetworkSuggestionsManager = 310 new WifiNetworkSuggestionsManager( 311 mContext, 312 new RunnerHandler(mLooper.getLooper(), 100, new LocalLog(128)), 313 mWifiInjector, 314 mWifiPermissionsUtil, 315 mWifiConfigManager, 316 mWifiConfigStore, 317 mWifiMetrics, 318 mWifiCarrierInfoManager, 319 mWifiKeyStore, 320 mLruConnectionTracker, 321 mClock); 322 mWifiNetworkSuggestionsManager.enableVerboseLogging(true); 323 mLooper.dispatchAll(); 324 verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), any(), any(), any()); 325 326 ArgumentCaptor<NetworkSuggestionStoreData.DataSource> dataSourceArgumentCaptor = 327 ArgumentCaptor.forClass(NetworkSuggestionStoreData.DataSource.class); 328 329 verify(mWifiInjector).makeNetworkSuggestionStoreData(dataSourceArgumentCaptor.capture()); 330 mDataSource = dataSourceArgumentCaptor.getValue(); 331 assertNotNull(mDataSource); 332 mDataSource.fromDeserialized(Map.of()); 333 334 verify(mWifiCarrierInfoManager).addImsiProtectedOrUserApprovedListener( 335 mUserApproveCarrierListenerArgumentCaptor.capture()); 336 verify(mWifiConfigManager).addOnNetworkUpdateListener(mNetworkListenerCaptor.capture()); 337 } 338 339 @After cleanUp()340 public void cleanUp() throws Exception { 341 if (null != mStaticMockSession) { 342 mStaticMockSession.finishMocking(); 343 } 344 } 345 346 /** 347 * Verify successful addition of network suggestions. 348 */ 349 @Test testAddNetworkSuggestionsSuccess()350 public void testAddNetworkSuggestionsSuccess() { 351 PasspointConfiguration passpointConfiguration = 352 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 353 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 354 passpointConfiguration.getUniqueId()); 355 placeholderConfig.restricted = true; 356 placeholderConfig.trusted = false; 357 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 358 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 359 DEFAULT_PRIORITY_GROUP); 360 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 361 placeholderConfig, passpointConfiguration, false, false, true, true, 362 DEFAULT_PRIORITY_GROUP); 363 364 List<WifiNetworkSuggestion> networkSuggestionList1 = 365 List.of(networkSuggestion1); 366 List<WifiNetworkSuggestion> networkSuggestionList2 = 367 List.of(networkSuggestion2); 368 when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), 369 anyInt(), anyString(), eq(true), anyBoolean(), anyBoolean())).thenReturn(true); 370 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 371 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 372 TEST_PACKAGE_1, TEST_FEATURE)); 373 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 374 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 375 TEST_PACKAGE_2, TEST_FEATURE)); 376 verify(mPasspointManager).addOrUpdateProvider( 377 passpointConfiguration, TEST_UID_2, TEST_PACKAGE_2, true, false, true); 378 verify(mWifiMetrics, times(2)) 379 .incrementNetworkSuggestionApiUsageNumOfAppInType( 380 WifiNetworkSuggestionsManager.APP_TYPE_NON_PRIVILEGED); 381 Set<WifiNetworkSuggestion> allNetworkSuggestions = 382 mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); 383 Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = 384 Set.of(networkSuggestion1, networkSuggestion2); 385 assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); 386 387 verify(mWifiMetrics, times(2)).incrementNetworkSuggestionApiNumModification(); 388 verify(mWifiMetrics, times(2)).addNetworkSuggestionPriorityGroup(anyInt()); 389 ArgumentCaptor<List<Integer>> maxSizesCaptor = ArgumentCaptor.forClass(List.class); 390 verify(mWifiMetrics, times(2)).noteNetworkSuggestionApiListSizeHistogram( 391 maxSizesCaptor.capture()); 392 assertNotNull(maxSizesCaptor.getValue()); 393 assertEquals(maxSizesCaptor.getValue(), List.of(1, 1)); 394 } 395 396 /** 397 * Verify successful removal of network suggestions. 398 */ 399 @Test testRemoveNetworkSuggestionsSuccess()400 public void testRemoveNetworkSuggestionsSuccess() { 401 PasspointConfiguration passpointConfiguration = 402 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 403 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 404 passpointConfiguration.getUniqueId()); 405 placeholderConfig.setPasspointUniqueId(passpointConfiguration.getUniqueId()); 406 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 407 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 408 DEFAULT_PRIORITY_GROUP); 409 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 410 placeholderConfig, passpointConfiguration, false, false, true, true, 411 DEFAULT_PRIORITY_GROUP); 412 413 List<WifiNetworkSuggestion> networkSuggestionList1 = 414 List.of(networkSuggestion1); 415 List<WifiNetworkSuggestion> networkSuggestionList2 = 416 List.of(networkSuggestion2); 417 when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), 418 anyInt(), anyString(), eq(true), eq(true), eq(false))).thenReturn(true); 419 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 420 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 421 TEST_PACKAGE_1, TEST_FEATURE)); 422 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 423 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 424 TEST_PACKAGE_2, TEST_FEATURE)); 425 426 // Now remove all of them. 427 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 428 mWifiNetworkSuggestionsManager.remove(networkSuggestionList1, TEST_UID_1, 429 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 430 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 431 mWifiNetworkSuggestionsManager.remove(networkSuggestionList2, TEST_UID_2, 432 TEST_PACKAGE_2, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 433 verify(mPasspointManager).removeProvider(eq(TEST_UID_2), eq(false), 434 eq(passpointConfiguration.getUniqueId()), isNull()); 435 verify(mWifiScoreCard).removeNetwork(anyString()); 436 verify(mLruConnectionTracker).removeNetwork(any()); 437 438 assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); 439 440 verify(mWifiMetrics, times(4)).incrementNetworkSuggestionApiNumModification(); 441 verify(mWifiMetrics, times(2)).addNetworkSuggestionPriorityGroup(anyInt()); 442 ArgumentCaptor<List<Integer>> maxSizesCaptor = ArgumentCaptor.forClass(List.class); 443 verify(mWifiMetrics, times(4)).noteNetworkSuggestionApiListSizeHistogram( 444 maxSizesCaptor.capture()); 445 // Only non-passpoint suggestion will trigger remove connect choice, passpoint suggestion 446 // will trigger this in passpointManager 447 verify(mWifiConfigManager).removeConnectChoiceFromAllNetworks(anyString()); 448 assertNotNull(maxSizesCaptor.getValue()); 449 assertEquals(maxSizesCaptor.getValue(), List.of(1, 1)); 450 } 451 452 /** 453 * Add or remove suggestion before user data store loaded will fail. 454 */ 455 @Test testAddRemoveSuggestionBeforeUserDataLoaded()456 public void testAddRemoveSuggestionBeforeUserDataLoaded() { 457 // Clear the data source, and user data store is not loaded 458 mDataSource.reset(); 459 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 460 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 461 DEFAULT_PRIORITY_GROUP); 462 List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); 463 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_INTERNAL, 464 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 465 TEST_PACKAGE_1, TEST_FEATURE)); 466 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_INTERNAL, 467 mWifiNetworkSuggestionsManager.remove(networkSuggestionList, TEST_UID_1, 468 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 469 } 470 471 @Test testAddRemoveEnterpriseNetworkSuggestion()472 public void testAddRemoveEnterpriseNetworkSuggestion() { 473 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 474 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 475 DEFAULT_PRIORITY_GROUP); 476 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 477 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 478 DEFAULT_PRIORITY_GROUP); 479 480 List<WifiNetworkSuggestion> networkSuggestionList = 481 List.of(networkSuggestion1, networkSuggestion2); 482 when(mWifiKeyStore.updateNetworkKeys(eq(networkSuggestion1.wifiConfiguration), any())) 483 .thenReturn(true); 484 when(mWifiKeyStore.updateNetworkKeys(eq(networkSuggestion2.wifiConfiguration), any())) 485 .thenReturn(false); 486 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 487 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 488 TEST_PACKAGE_1, TEST_FEATURE)); 489 490 Set<WifiNetworkSuggestion> allNetworkSuggestions = 491 mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); 492 assertEquals(1, allNetworkSuggestions.size()); 493 WifiNetworkSuggestion removingSuggestion = createWifiNetworkSuggestion( 494 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 495 DEFAULT_PRIORITY_GROUP); 496 removingSuggestion.wifiConfiguration.SSID = networkSuggestion1.wifiConfiguration.SSID; 497 // Remove with the networkSuggestion from external. 498 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 499 mWifiNetworkSuggestionsManager.remove( 500 List.of(removingSuggestion), 501 TEST_UID_1, TEST_PACKAGE_1, 502 ACTION_REMOVE_SUGGESTION_DISCONNECT)); 503 // Make sure remove the keyStore with the internal config 504 verify(mWifiKeyStore).removeKeys(eq(networkSuggestion1.wifiConfiguration.enterpriseConfig), 505 eq(false)); 506 verify(mLruConnectionTracker).removeNetwork(any()); 507 } 508 509 @Test testAddNetworkSuggestionWithInvalidKeyChainKeyAlias()510 public void testAddNetworkSuggestionWithInvalidKeyChainKeyAlias() { 511 assumeTrue(SdkLevel.isAtLeastS()); 512 513 final WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork(); 514 config.enterpriseConfig.setClientKeyPairAlias("some-alias"); 515 final WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 516 config, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 517 final List<WifiNetworkSuggestion> networkSuggestionList = 518 List.of(networkSuggestion1); 519 when(mWifiKeyStore.updateNetworkKeys(eq(networkSuggestion1.wifiConfiguration), any())) 520 .thenReturn(true); 521 when(mWifiKeyStore.validateKeyChainAlias(any(String.class), anyInt())).thenReturn(false); 522 523 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 524 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 525 TEST_PACKAGE_1, TEST_FEATURE)); 526 } 527 528 @Test testAddNetworkSuggestionWithValidKeyChainKeyAlias()529 public void testAddNetworkSuggestionWithValidKeyChainKeyAlias() { 530 assumeTrue(SdkLevel.isAtLeastS()); 531 532 final WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork(); 533 config.enterpriseConfig.setClientKeyPairAlias("some-alias"); 534 final WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 535 config, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 536 final List<WifiNetworkSuggestion> networkSuggestionList = 537 List.of(networkSuggestion1); 538 when(mWifiKeyStore.updateNetworkKeys(eq(networkSuggestion1.wifiConfiguration), any())) 539 .thenReturn(true); 540 when(mWifiKeyStore.validateKeyChainAlias(any(String.class), anyInt())).thenReturn(true); 541 542 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 543 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 544 TEST_PACKAGE_1, TEST_FEATURE)); 545 } 546 547 @Test testAddInsecureEnterpriseNetworkSuggestion()548 public void testAddInsecureEnterpriseNetworkSuggestion() { 549 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 550 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 551 DEFAULT_PRIORITY_GROUP); 552 networkSuggestion.wifiConfiguration.enterpriseConfig.setCaPath(null); 553 List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); 554 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 555 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 556 TEST_PACKAGE_1, TEST_FEATURE)); 557 558 networkSuggestion = createWifiNetworkSuggestion( 559 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 560 DEFAULT_PRIORITY_GROUP); 561 networkSuggestion.wifiConfiguration.enterpriseConfig.setDomainSuffixMatch(""); 562 networkSuggestionList = Arrays.asList(networkSuggestion); 563 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 564 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 565 TEST_PACKAGE_1, TEST_FEATURE)); 566 } 567 568 @Test testAddOemPaidNetworkSuggestionOnPreSDevices()569 public void testAddOemPaidNetworkSuggestionOnPreSDevices() { 570 assumeFalse(SdkLevel.isAtLeastS()); 571 572 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 573 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 574 DEFAULT_PRIORITY_GROUP); 575 networkSuggestion.wifiConfiguration.oemPaid = true; 576 List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); 577 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 578 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 579 TEST_PACKAGE_1, TEST_FEATURE)); 580 } 581 582 @Test testAddOemPrivateNetworkSuggestionOnPreSDevices()583 public void testAddOemPrivateNetworkSuggestionOnPreSDevices() { 584 assumeFalse(SdkLevel.isAtLeastS()); 585 586 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 587 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 588 DEFAULT_PRIORITY_GROUP); 589 networkSuggestion.wifiConfiguration.oemPrivate = true; 590 List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); 591 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 592 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 593 TEST_PACKAGE_1, TEST_FEATURE)); 594 } 595 596 @Test testSetSubscriptionIdOnPreSDevices()597 public void testSetSubscriptionIdOnPreSDevices() { 598 assumeFalse(SdkLevel.isAtLeastS()); 599 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 600 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 601 DEFAULT_PRIORITY_GROUP); 602 networkSuggestion.wifiConfiguration.subscriptionId = TEST_SUBID; 603 List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); 604 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 605 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 606 TEST_PACKAGE_1, TEST_FEATURE)); 607 } 608 609 @Test testSetPriorityGroupOnPreSDevices()610 public void testSetPriorityGroupOnPreSDevices() { 611 assumeFalse(SdkLevel.isAtLeastS()); 612 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 613 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 614 TEST_PRIORITY_GROUP); 615 List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); 616 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 617 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 618 TEST_PACKAGE_1, TEST_FEATURE)); 619 } 620 621 @Test testSetSubscriptionGroupOnPreTDevices()622 public void testSetSubscriptionGroupOnPreTDevices() { 623 assumeFalse(SdkLevel.isAtLeastT()); 624 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 625 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 626 DEFAULT_PRIORITY_GROUP); 627 networkSuggestion.wifiConfiguration.setSubscriptionGroup(GROUP_UUID); 628 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 629 mWifiNetworkSuggestionsManager.add(List.of(networkSuggestion), TEST_UID_1, 630 TEST_PACKAGE_1, TEST_FEATURE)); 631 } 632 633 /** 634 * Verify successful removal of all network suggestions. 635 */ 636 @Test testRemoveAllNetworkSuggestionsSuccess()637 public void testRemoveAllNetworkSuggestionsSuccess() { 638 PasspointConfiguration passpointConfiguration = 639 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 640 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 641 passpointConfiguration.getUniqueId()); 642 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 643 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 644 DEFAULT_PRIORITY_GROUP); 645 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 646 placeholderConfig, passpointConfiguration, false, false, true, true, 647 DEFAULT_PRIORITY_GROUP); 648 649 650 List<WifiNetworkSuggestion> networkSuggestionList1 = 651 List.of(networkSuggestion1); 652 List<WifiNetworkSuggestion> networkSuggestionList2 = 653 List.of(networkSuggestion2); 654 655 when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), 656 anyInt(), anyString(), eq(true), eq(true), eq(false))).thenReturn(true); 657 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 658 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 659 TEST_PACKAGE_1, TEST_FEATURE)); 660 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 661 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 662 TEST_PACKAGE_2, TEST_FEATURE)); 663 664 // Now remove all of them by sending an empty list. 665 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 666 mWifiNetworkSuggestionsManager.remove(new ArrayList<>(), TEST_UID_1, 667 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 668 verify(mLruConnectionTracker).removeNetwork(any()); 669 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 670 mWifiNetworkSuggestionsManager.remove(new ArrayList<>(), TEST_UID_2, 671 TEST_PACKAGE_2, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 672 verify(mPasspointManager).removeProvider(eq(TEST_UID_2), eq(false), 673 eq(passpointConfiguration.getUniqueId()), isNull()); 674 675 assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); 676 } 677 678 /** 679 * Verify successful replace (add,remove, add) of network suggestions. 680 */ 681 @Test testReplaceNetworkSuggestionsSuccess()682 public void testReplaceNetworkSuggestionsSuccess() { 683 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 684 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 685 DEFAULT_PRIORITY_GROUP); 686 687 List<WifiNetworkSuggestion> networkSuggestionList1 = 688 List.of(networkSuggestion); 689 690 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 691 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 692 TEST_PACKAGE_1, TEST_FEATURE)); 693 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 694 mWifiNetworkSuggestionsManager.remove(networkSuggestionList1, TEST_UID_1, 695 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 696 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 697 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 698 TEST_PACKAGE_1, TEST_FEATURE)); 699 700 Set<WifiNetworkSuggestion> allNetworkSuggestions = 701 mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); 702 Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = 703 Set.of(networkSuggestion); 704 assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); 705 } 706 707 /** 708 * Verify that modify networks that are already active is allowed. 709 * - Adding two suggestions and not add into the WifiConfigManager - before network selection 710 * - Set user connect choice, Anonymous Identity and auto-join on suggestion 711 * - Adding the suggestions with same profile should succeed. And no WifiConfigManager update. 712 * - After in-place modify, user connect choice, Anonymous Identity and auto-join should be 713 * preserved. 714 */ 715 @Test testAddNetworkSuggestionsSuccessOnInPlaceModificationWhenNotInWcm()716 public void testAddNetworkSuggestionsSuccessOnInPlaceModificationWhenNotInWcm() { 717 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 718 .thenReturn(true); 719 720 // Create an EAP-SIM suggestion and a passpoint suggestion 721 PasspointConfiguration passpointConfiguration = 722 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 723 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 724 passpointConfiguration.getUniqueId()); 725 WifiConfiguration eapSimConfig = WifiConfigurationTestUtil.createEapNetwork( 726 WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); 727 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 728 eapSimConfig, null, false, false, true, true, 729 DEFAULT_PRIORITY_GROUP); 730 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 731 placeholderConfig, passpointConfiguration, false, false, true, true, 732 DEFAULT_PRIORITY_GROUP); 733 List<WifiNetworkSuggestion> networkSuggestionList1 = 734 List.of(networkSuggestion1, networkSuggestion2); 735 736 // Verify adding suggestion succeed. 737 when(mPasspointManager.addOrUpdateProvider(any(), 738 anyInt(), anyString(), eq(true), anyBoolean(), eq(false))).thenReturn(true); 739 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 740 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 741 TEST_PACKAGE_1, TEST_FEATURE)); 742 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 743 744 // Nothing in WCM. 745 when(mWifiConfigManager.getConfiguredNetwork(networkSuggestion1.wifiConfiguration 746 .getProfileKey())).thenReturn(null); 747 when(mWifiConfigManager.getConfiguredNetwork(networkSuggestion2.wifiConfiguration 748 .getProfileKey())).thenReturn(null); 749 750 // Set user connect choice, Anonymous Identity and auto join. 751 WifiConfiguration config = new WifiConfiguration(eapSimConfig); 752 config.fromWifiNetworkSuggestion = true; 753 config.shared = false; 754 config.ephemeral = true; 755 config.creatorName = TEST_PACKAGE_1; 756 config.creatorUid = TEST_UID_1; 757 config.enterpriseConfig.setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY_1); 758 WifiConfigManager.OnNetworkUpdateListener listener = mNetworkListenerCaptor.getValue(); 759 listener.onConnectChoiceSet(List.of(config), USER_CONNECT_CHOICE, TEST_RSSI); 760 mWifiNetworkSuggestionsManager.setAnonymousIdentity(config); 761 mWifiNetworkSuggestionsManager.allowNetworkSuggestionAutojoin(config, false); 762 763 // Keep the same suggestion as auto-join enabled, but user already mark it disabled. 764 WifiNetworkSuggestion networkSuggestion3 = createWifiNetworkSuggestion( 765 eapSimConfig, null, false, false, true, true, 766 DEFAULT_PRIORITY_GROUP); 767 // Modify the same suggestion to mark it auto-join disabled. 768 WifiNetworkSuggestion networkSuggestion4 = createWifiNetworkSuggestion( 769 placeholderConfig, passpointConfiguration, false, false, true, false, 770 DEFAULT_PRIORITY_GROUP); 771 List<WifiNetworkSuggestion> networkSuggestionList2 = 772 List.of(networkSuggestion3, networkSuggestion4); 773 774 // Replace attempt should success. 775 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 776 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_1, 777 TEST_PACKAGE_1, TEST_FEATURE)); 778 779 Set<ExtendedWifiNetworkSuggestion> matchedPasspointSuggestions = 780 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForFqdn(TEST_FQDN); 781 assertEquals(1, matchedPasspointSuggestions.size()); 782 // As user didn't change the auto-join, will follow the newly added one. 783 assertFalse(matchedPasspointSuggestions.iterator().next().isAutojoinEnabled); 784 785 ScanDetail scanDetail = createScanDetailForNetwork(eapSimConfig); 786 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = 787 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 788 assertEquals(1, matchedSuggestions.size()); 789 ExtendedWifiNetworkSuggestion matchedSuggestion = matchedSuggestions.iterator().next(); 790 // As user changes the auto-join, will keep the user choice. 791 assertFalse(matchedSuggestion.isAutojoinEnabled); 792 // Verify user connect choice and Anonymous Identity are preserved during the modify. 793 assertEquals(TEST_ANONYMOUS_IDENTITY_1, matchedSuggestion.anonymousIdentity); 794 assertEquals(USER_CONNECT_CHOICE, matchedSuggestion.connectChoice); 795 assertEquals(TEST_RSSI, matchedSuggestion.connectChoiceRssi); 796 797 // Verify we did not update config in WCM. 798 verify(mWifiConfigManager, never()).addOrUpdateNetwork(any(), anyInt(), any(), eq(false)); 799 } 800 801 /** 802 * Verify that modify networks that are already active and is cached in WifiConfigManager is 803 * allowed and also updates the cache in WifiConfigManager. 804 */ 805 @Test testAddNetworkSuggestionsSuccessOnInPlaceModificationWhenInWcm()806 public void testAddNetworkSuggestionsSuccessOnInPlaceModificationWhenInWcm() { 807 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 808 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 809 DEFAULT_PRIORITY_GROUP); 810 List<WifiNetworkSuggestion> networkSuggestionList1 = 811 List.of(networkSuggestion); 812 813 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 814 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 815 TEST_PACKAGE_1, TEST_FEATURE)); 816 // Assert that the original config was not metered. 817 assertEquals(WifiConfiguration.METERED_OVERRIDE_NONE, 818 networkSuggestion.wifiConfiguration.meteredOverride); 819 verify(mWifiMetrics).incrementNetworkSuggestionApiUsageNumOfAppInType( 820 WifiNetworkSuggestionsManager.APP_TYPE_NON_PRIVILEGED); 821 reset(mWifiMetrics); 822 // Store the original WifiConfiguration from WifiConfigManager. 823 WifiConfiguration configInWcm = 824 new WifiConfiguration(networkSuggestion.wifiConfiguration); 825 configInWcm.creatorUid = TEST_UID_1; 826 configInWcm.creatorName = TEST_PACKAGE_1; 827 configInWcm.fromWifiNetworkSuggestion = true; 828 configInWcm.shared = false; 829 setupGetConfiguredNetworksFromWcm(configInWcm); 830 831 // Modify the original suggestion to mark it metered. 832 networkSuggestion.wifiConfiguration.meteredOverride = 833 WifiConfiguration.METERED_OVERRIDE_METERED; 834 835 when(mWifiConfigManager.addOrUpdateNetwork(any(), eq(TEST_UID_1), eq(TEST_PACKAGE_1), 836 eq(false))).thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)); 837 // Replace attempt should success. 838 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 839 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 840 TEST_PACKAGE_1, TEST_FEATURE)); 841 assertEquals(WifiConfiguration.METERED_OVERRIDE_METERED, 842 mWifiNetworkSuggestionsManager 843 .get(TEST_PACKAGE_1, TEST_UID_1).get(0).wifiConfiguration.meteredOverride); 844 verify(mWifiMetrics, never()).incrementNetworkSuggestionApiUsageNumOfAppInType(anyInt()); 845 // Verify we did update config in WCM. 846 ArgumentCaptor<WifiConfiguration> configCaptor = 847 ArgumentCaptor.forClass(WifiConfiguration.class); 848 verify(mWifiConfigManager).addOrUpdateNetwork( 849 configCaptor.capture(), eq(TEST_UID_1), eq(TEST_PACKAGE_1), eq(false)); 850 assertNotNull(configCaptor.getValue()); 851 assertEquals(WifiConfiguration.METERED_OVERRIDE_METERED, 852 configCaptor.getValue().meteredOverride); 853 } 854 855 /** 856 * Verify that an attempt to add networks beyond the max per app is rejected. 857 */ 858 @Test testAddNetworkSuggestionsFailureOnExceedsMaxPerApp()859 public void testAddNetworkSuggestionsFailureOnExceedsMaxPerApp() { 860 // Add the max per app first. 861 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 862 for (int i = 0; i < WifiManager.NETWORK_SUGGESTIONS_MAX_PER_APP_HIGH_RAM; i++) { 863 networkSuggestionList.add(createWifiNetworkSuggestion( 864 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 865 DEFAULT_PRIORITY_GROUP)); 866 } 867 // The first add should succeed. 868 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 869 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 870 TEST_PACKAGE_1, TEST_FEATURE)); 871 List<WifiNetworkSuggestion> originalNetworkSuggestionsList = networkSuggestionList; 872 873 // Now add 3 more. 874 networkSuggestionList = new ArrayList<>(); 875 for (int i = 0; i < 3; i++) { 876 networkSuggestionList.add(createWifiNetworkSuggestion( 877 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 878 DEFAULT_PRIORITY_GROUP)); 879 } 880 // The second add should fail. 881 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_EXCEEDS_MAX_PER_APP, 882 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 883 TEST_PACKAGE_1, TEST_FEATURE)); 884 885 // Now remove 3 of the initially added ones. 886 networkSuggestionList = new ArrayList<>(); 887 for (int i = 0; i < 3; i++) { 888 networkSuggestionList.add(originalNetworkSuggestionsList.get(i)); 889 } 890 // The remove should succeed. 891 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 892 mWifiNetworkSuggestionsManager.remove(networkSuggestionList, TEST_UID_1, 893 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 894 895 // Now add 2 more. 896 networkSuggestionList = new ArrayList<>(); 897 for (int i = 0; i < 2; i++) { 898 networkSuggestionList.add(createWifiNetworkSuggestion( 899 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 900 DEFAULT_PRIORITY_GROUP)); 901 } 902 // This add should now succeed. 903 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 904 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 905 TEST_PACKAGE_1, TEST_FEATURE)); 906 } 907 908 @Test testAddNetworkSuggestionWithMismatchBetweenCarrierIdAndSubId()909 public void testAddNetworkSuggestionWithMismatchBetweenCarrierIdAndSubId() { 910 assumeTrue(SdkLevel.isAtLeastS()); 911 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 912 WifiConfigurationTestUtil.createEapNetwork(), null, false, false, true, true, 913 DEFAULT_PRIORITY_GROUP); 914 networkSuggestion.wifiConfiguration.carrierId = TEST_CARRIER_ID; 915 networkSuggestion.wifiConfiguration.subscriptionId = TEST_SUBID; 916 when(mWifiCarrierInfoManager 917 .isSubIdMatchingCarrierId(anyInt(), anyInt())).thenReturn(false); 918 List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); 919 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED, 920 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 921 TEST_PACKAGE_1, TEST_FEATURE)); 922 923 } 924 925 /** 926 * Verify that an attempt to remove an invalid set of network suggestions is rejected. 927 */ 928 @Test testRemoveNetworkSuggestionsFailureOnInvalid()929 public void testRemoveNetworkSuggestionsFailureOnInvalid() { 930 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 931 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 932 DEFAULT_PRIORITY_GROUP); 933 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 934 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 935 DEFAULT_PRIORITY_GROUP); 936 937 List<WifiNetworkSuggestion> networkSuggestionList1 = 938 List.of(networkSuggestion1); 939 List<WifiNetworkSuggestion> networkSuggestionList2 = 940 List.of(networkSuggestion2); 941 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 942 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 943 TEST_PACKAGE_1, TEST_FEATURE)); 944 // Remove should fail because the network list is different. 945 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_REMOVE_INVALID, 946 mWifiNetworkSuggestionsManager.remove(networkSuggestionList2, TEST_UID_1, 947 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 948 } 949 950 /** 951 * Verify a successful lookup of a single network suggestion matching the provided scan detail. 952 */ 953 @Test 954 public void testGetNetworkSuggestionsForScanDetailSuccessWithOneMatchForCarrierProvisioningApp()955 testGetNetworkSuggestionsForScanDetailSuccessWithOneMatchForCarrierProvisioningApp() { 956 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 957 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 958 DEFAULT_PRIORITY_GROUP); 959 List<WifiNetworkSuggestion> networkSuggestionList1 = 960 List.of(networkSuggestion); 961 // This app should be pre-approved. No need to explicitly call 962 // |setHasUserApprovedForApp(true, TEST_PACKAGE_1)| 963 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 964 .thenReturn(true); 965 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 966 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 967 TEST_PACKAGE_1, TEST_FEATURE)); 968 verify(mWifiMetrics).incrementNetworkSuggestionApiUsageNumOfAppInType( 969 WifiNetworkSuggestionsManager.APP_TYPE_NETWORK_PROVISIONING); 970 ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion.wifiConfiguration); 971 972 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 973 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 974 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = 975 Set.of(networkSuggestion); 976 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 977 } 978 979 /** 980 * Verify add or remove suggestion list with null object will result error code. 981 */ 982 @Test testAddRemoveNetworkSuggestionWithNullObject()983 public void testAddRemoveNetworkSuggestionWithNullObject() { 984 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 985 mWifiNetworkSuggestionsManager.add(Collections.singletonList(null), 986 TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); 987 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 988 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 989 DEFAULT_PRIORITY_GROUP); 990 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 991 mWifiNetworkSuggestionsManager.add(List.of(networkSuggestion), 992 TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); 993 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_REMOVE_INVALID, 994 mWifiNetworkSuggestionsManager.remove(Collections.singletonList(null), TEST_UID_1, 995 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 996 } 997 998 /** 999 * Verify a successful lookup of a single network suggestion matching the provided scan detail. 1000 */ 1001 @Test testGetNetworkSuggestionsForScanDetailSuccessWithOneMatch()1002 public void testGetNetworkSuggestionsForScanDetailSuccessWithOneMatch() { 1003 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1004 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 1005 DEFAULT_PRIORITY_GROUP); 1006 List<WifiNetworkSuggestion> networkSuggestionList1 = 1007 List.of(networkSuggestion); 1008 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1009 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1010 TEST_PACKAGE_1, TEST_FEATURE)); 1011 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1012 1013 ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion.wifiConfiguration); 1014 1015 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 1016 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 1017 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = 1018 Set.of(networkSuggestion); 1019 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 1020 } 1021 1022 /** 1023 * Verify a successful lookup of a single network suggestion matching the provided scan detail. 1024 * 1025 * The wifi configuration in the network suggestion is a type which could have upgradable types. 1026 */ 1027 @Test testGetNetworkSuggestionsForScanDetailSuccessWithOneMatchForUpgradableConfig()1028 public void testGetNetworkSuggestionsForScanDetailSuccessWithOneMatchForUpgradableConfig() { 1029 WifiConfiguration upgradableConfig = new WifiConfiguration(); 1030 upgradableConfig.SSID = "\"test\""; 1031 upgradableConfig.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); 1032 upgradableConfig.preSharedKey = "\"PassW0rd\""; 1033 1034 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1035 upgradableConfig, null, false, false, true, true, 1036 DEFAULT_PRIORITY_GROUP); 1037 List<WifiNetworkSuggestion> networkSuggestionList1 = 1038 List.of(networkSuggestion); 1039 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1040 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1041 TEST_PACKAGE_1, TEST_FEATURE)); 1042 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1043 1044 ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion.wifiConfiguration); 1045 1046 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 1047 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 1048 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = 1049 Set.of(networkSuggestion); 1050 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 1051 } 1052 1053 1054 /** 1055 * Verify a successful lookup of a single network suggestion matching the provided scan detail. 1056 * 1057 * The wifi configuration in the network suggestion is a leagcy object, says no security params 1058 * list, and only raw fields are set. 1059 */ 1060 @Test testGetNetworkSuggestionsForScanDetailSuccessWithOneMatchForLegacyConfig()1061 public void testGetNetworkSuggestionsForScanDetailSuccessWithOneMatchForLegacyConfig() { 1062 WifiConfiguration legacyConfig = new WifiConfiguration(); 1063 legacyConfig.SSID = "\"test\""; 1064 legacyConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 1065 legacyConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 1066 legacyConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 1067 legacyConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 1068 legacyConfig.preSharedKey = "\"PassW0rd\""; 1069 1070 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1071 legacyConfig, null, false, false, true, true, 1072 DEFAULT_PRIORITY_GROUP); 1073 List<WifiNetworkSuggestion> networkSuggestionList1 = 1074 List.of(networkSuggestion); 1075 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1076 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1077 TEST_PACKAGE_1, TEST_FEATURE)); 1078 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1079 1080 ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion.wifiConfiguration); 1081 1082 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 1083 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 1084 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = 1085 Set.of(networkSuggestion); 1086 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 1087 } 1088 1089 /** 1090 * Verify a successful lookup of multiple network suggestions matching the provided scan detail. 1091 */ 1092 @Test testGetNetworkSuggestionsForScanDetailSuccessWithMultipleMatch()1093 public void testGetNetworkSuggestionsForScanDetailSuccessWithMultipleMatch() { 1094 WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createOpenNetwork(); 1095 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 1096 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1097 // Reuse the same network credentials to ensure they both match. 1098 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 1099 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1100 1101 List<WifiNetworkSuggestion> networkSuggestionList1 = 1102 List.of(networkSuggestion1); 1103 List<WifiNetworkSuggestion> networkSuggestionList2 = 1104 List.of(networkSuggestion2); 1105 1106 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1107 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1108 TEST_PACKAGE_1, TEST_FEATURE)); 1109 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1110 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 1111 TEST_PACKAGE_2, TEST_FEATURE)); 1112 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1113 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_2, TEST_PACKAGE_2); 1114 1115 ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion1.wifiConfiguration); 1116 1117 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 1118 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 1119 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = Set.of(networkSuggestion1); 1120 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 1121 } 1122 1123 /** 1124 * Verify a successful lookup of a single network suggestion matching the provided scan detail. 1125 */ 1126 @Test testGetNetworkSuggestionsForScanDetailSuccessWithBssidOneMatch()1127 public void testGetNetworkSuggestionsForScanDetailSuccessWithBssidOneMatch() { 1128 WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createOpenNetwork(); 1129 ScanDetail scanDetail = createScanDetailForNetwork(wifiConfiguration); 1130 wifiConfiguration.BSSID = scanDetail.getBSSIDString(); 1131 1132 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1133 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1134 List<WifiNetworkSuggestion> networkSuggestionList1 = 1135 List.of(networkSuggestion); 1136 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1137 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1138 TEST_PACKAGE_1, TEST_FEATURE)); 1139 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1140 1141 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 1142 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 1143 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = 1144 Set.of(networkSuggestion); 1145 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 1146 } 1147 1148 /** 1149 * Verify a successful lookup of multiple network suggestions matching the provided scan detail. 1150 */ 1151 @Test testGetNetworkSuggestionsForScanDetailSuccessWithBssidMultipleMatch()1152 public void testGetNetworkSuggestionsForScanDetailSuccessWithBssidMultipleMatch() { 1153 WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createOpenNetwork(); 1154 ScanDetail scanDetail = createScanDetailForNetwork(wifiConfiguration); 1155 wifiConfiguration.BSSID = scanDetail.getBSSIDString(); 1156 1157 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 1158 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1159 // Reuse the same network credentials to ensure they both match. 1160 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 1161 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1162 1163 List<WifiNetworkSuggestion> networkSuggestionList1 = 1164 List.of(networkSuggestion1); 1165 List<WifiNetworkSuggestion> networkSuggestionList2 = 1166 List.of(networkSuggestion2); 1167 1168 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1169 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1170 TEST_PACKAGE_1, TEST_FEATURE)); 1171 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1172 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 1173 TEST_PACKAGE_2, TEST_FEATURE)); 1174 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1175 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_2, TEST_PACKAGE_2); 1176 1177 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 1178 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 1179 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = Set.of(networkSuggestion1); 1180 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 1181 } 1182 1183 /** 1184 * Verify a successful lookup of multiple network suggestions matching the provided scan detail. 1185 */ 1186 @Test 1187 public void testGetNetworkSuggestionsForScanDetailSuccessWithBssidMultipleMatchFromSamePackage()1188 testGetNetworkSuggestionsForScanDetailSuccessWithBssidMultipleMatchFromSamePackage() { 1189 WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createOpenNetwork(); 1190 ScanDetail scanDetail = createScanDetailForNetwork(wifiConfiguration); 1191 wifiConfiguration.BSSID = scanDetail.getBSSIDString(); 1192 1193 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 1194 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1195 // Reuse the same network credentials to ensure they both match. 1196 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 1197 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1198 1199 List<WifiNetworkSuggestion> networkSuggestionList = 1200 List.of(networkSuggestion1, networkSuggestion2); 1201 1202 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1203 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1204 TEST_PACKAGE_1, TEST_FEATURE)); 1205 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1206 1207 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 1208 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 1209 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = Set.of(networkSuggestion1); 1210 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 1211 } 1212 1213 /** 1214 * Verify a successful lookup of multiple network suggestions matching the provided scan detail. 1215 */ 1216 @Test 1217 public void testGetNetworkSuggestionsForScanDetailSuccessWithBssidAndWithoutBssidMultipleMatch()1218 testGetNetworkSuggestionsForScanDetailSuccessWithBssidAndWithoutBssidMultipleMatch() { 1219 WifiConfiguration wifiConfiguration1 = WifiConfigurationTestUtil.createOpenNetwork(); 1220 ScanDetail scanDetail = createScanDetailForNetwork(wifiConfiguration1); 1221 WifiConfiguration wifiConfiguration2 = new WifiConfiguration(wifiConfiguration1); 1222 wifiConfiguration2.BSSID = scanDetail.getBSSIDString(); 1223 1224 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 1225 wifiConfiguration1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1226 // Reuse the same network credentials to ensure they both match. 1227 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 1228 wifiConfiguration2, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1229 1230 List<WifiNetworkSuggestion> networkSuggestionList1 = 1231 List.of(networkSuggestion1); 1232 List<WifiNetworkSuggestion> networkSuggestionList2 = 1233 List.of(networkSuggestion2); 1234 1235 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1236 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1237 TEST_PACKAGE_1, TEST_FEATURE)); 1238 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1239 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 1240 TEST_PACKAGE_2, TEST_FEATURE)); 1241 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1242 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_2, TEST_PACKAGE_2); 1243 1244 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 1245 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 1246 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = 1247 Set.of(networkSuggestion1, networkSuggestion2); 1248 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 1249 1250 // Now change the bssid of the scan result to a different value, now only the general 1251 // (without bssid) suggestion. 1252 scanDetail.getScanResult().BSSID = MacAddressUtils.createRandomUnicastAddress().toString(); 1253 matchingExtNetworkSuggestions = 1254 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 1255 expectedMatchingNetworkSuggestions = 1256 Set.of(networkSuggestion1); 1257 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 1258 } 1259 1260 /** 1261 * Verify failure to lookup any network suggestion matching the provided scan detail when the 1262 * app providing the suggestion has not been approved. 1263 */ 1264 @Test testGetNetworkSuggestionsForScanDetailFailureOnAppNotApproved()1265 public void testGetNetworkSuggestionsForScanDetailFailureOnAppNotApproved() { 1266 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1267 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 1268 DEFAULT_PRIORITY_GROUP); 1269 List<WifiNetworkSuggestion> networkSuggestionList1 = 1270 List.of(networkSuggestion); 1271 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1272 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1273 TEST_PACKAGE_1, TEST_FEATURE)); 1274 assertFalse(mWifiNetworkSuggestionsManager.hasUserApprovedForApp(TEST_PACKAGE_1)); 1275 1276 ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion.wifiConfiguration); 1277 1278 assertTrue(mWifiNetworkSuggestionsManager 1279 .getNetworkSuggestionsForScanDetail(scanDetail).isEmpty()); 1280 } 1281 1282 /** 1283 * Verify failure to lookup any network suggestion matching the provided scan detail. 1284 */ 1285 @Test testGetNetworkSuggestionsForScanDetailFailureOnSuggestionRemoval()1286 public void testGetNetworkSuggestionsForScanDetailFailureOnSuggestionRemoval() { 1287 WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createOpenNetwork(); 1288 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1289 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 1290 ScanDetail scanDetail = createScanDetailForNetwork(wifiConfiguration); 1291 List<WifiNetworkSuggestion> networkSuggestionList1 = 1292 List.of(networkSuggestion); 1293 1294 // add the suggestion & ensure lookup works. 1295 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1296 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1297 TEST_PACKAGE_1, TEST_FEATURE)); 1298 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1299 assertNotNull(mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 1300 scanDetail)); 1301 1302 // remove the suggestion & ensure lookup fails. 1303 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1304 mWifiNetworkSuggestionsManager.remove(List.of(), TEST_UID_1, 1305 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 1306 assertTrue(mWifiNetworkSuggestionsManager 1307 .getNetworkSuggestionsForScanDetail(scanDetail).isEmpty()); 1308 } 1309 1310 /** 1311 * Verify failure to lookup any network suggestion matching the provided scan detail. 1312 */ 1313 @Test testGetNetworkSuggestionsForScanDetailFailureOnWrongNetwork()1314 public void testGetNetworkSuggestionsForScanDetailFailureOnWrongNetwork() { 1315 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1316 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 1317 DEFAULT_PRIORITY_GROUP); 1318 List<WifiNetworkSuggestion> networkSuggestionList1 = 1319 List.of(networkSuggestion); 1320 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1321 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1322 TEST_PACKAGE_1, TEST_FEATURE)); 1323 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1324 1325 // Create a scan result corresponding to a different network. 1326 ScanDetail scanDetail = createScanDetailForNetwork( 1327 WifiConfigurationTestUtil.createPskNetwork()); 1328 1329 assertTrue(mWifiNetworkSuggestionsManager 1330 .getNetworkSuggestionsForScanDetail(scanDetail).isEmpty()); 1331 } 1332 1333 /** 1334 * Verify a successful lookup of a single network suggestion matching the connected network. 1335 * a) The corresponding network suggestion has the 1336 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. 1337 * b) The app holds location permission. 1338 * This should trigger a broadcast to the app. 1339 * This should not trigger a connection failure callback to the app. 1340 */ 1341 @Test testOnNetworkConnectionSuccessWithOneMatch()1342 public void testOnNetworkConnectionSuccessWithOneMatch() throws Exception { 1343 assertTrue(mWifiNetworkSuggestionsManager.registerSuggestionConnectionStatusListener( 1344 mConnectionStatusListener, TEST_PACKAGE_1, TEST_UID_1)); 1345 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1346 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 1347 DEFAULT_PRIORITY_GROUP); 1348 List<WifiNetworkSuggestion> networkSuggestionList = 1349 List.of(networkSuggestion); 1350 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1351 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1352 TEST_PACKAGE_1, TEST_FEATURE)); 1353 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1354 1355 // Simulate connecting to the network. 1356 WifiConfiguration connectNetwork = 1357 new WifiConfiguration(networkSuggestion.wifiConfiguration); 1358 connectNetwork.fromWifiNetworkSuggestion = true; 1359 connectNetwork.shared = false; 1360 connectNetwork.ephemeral = true; 1361 connectNetwork.creatorName = TEST_PACKAGE_1; 1362 connectNetwork.creatorUid = TEST_UID_1; 1363 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1364 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 1365 1366 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectSuccess(); 1367 1368 // Verify that the correct broadcast was sent out. 1369 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults(eq(TEST_PACKAGE_1), 1370 eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 1371 validatePostConnectionBroadcastSent(TEST_PACKAGE_1, networkSuggestion); 1372 1373 // Verify no more broadcast were sent out. 1374 mInorder.verifyNoMoreInteractions(); 1375 } 1376 1377 @Test testOnNetworkConnectionSuccessWithOneMatchFromCarrierPrivilegedApp()1378 public void testOnNetworkConnectionSuccessWithOneMatchFromCarrierPrivilegedApp() { 1379 assertTrue(mWifiNetworkSuggestionsManager.registerSuggestionConnectionStatusListener( 1380 mConnectionStatusListener, TEST_PACKAGE_1, TEST_UID_1)); 1381 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 1382 .thenReturn(TEST_CARRIER_ID); 1383 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1384 WifiConfigurationTestUtil.createPskNetwork(), null, true, false, true, true, 1385 DEFAULT_PRIORITY_GROUP); 1386 List<WifiNetworkSuggestion> networkSuggestionList = 1387 List.of(networkSuggestion); 1388 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1389 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1390 TEST_PACKAGE_1, TEST_FEATURE)); 1391 assertFalse(mWifiNetworkSuggestionsManager.hasUserApprovedForApp(TEST_PACKAGE_1)); 1392 1393 // Simulate connecting to the network. 1394 WifiConfiguration connectNetwork = 1395 new WifiConfiguration(networkSuggestion.wifiConfiguration); 1396 connectNetwork.fromWifiNetworkSuggestion = true; 1397 connectNetwork.shared = false; 1398 connectNetwork.ephemeral = true; 1399 connectNetwork.creatorName = TEST_PACKAGE_1; 1400 connectNetwork.creatorUid = TEST_UID_1; 1401 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1402 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 1403 1404 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectSuccess(); 1405 1406 // Verify that the correct broadcast was sent out. 1407 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults(eq(TEST_PACKAGE_1), 1408 eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 1409 validatePostConnectionBroadcastSent(TEST_PACKAGE_1, networkSuggestion); 1410 1411 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1412 mWifiNetworkSuggestionsManager.remove(networkSuggestionList, TEST_UID_1, 1413 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 1414 verify(mWifiConfigManager).removeSuggestionConfiguredNetwork( 1415 argThat(new WifiConfigMatcher(networkSuggestion.wifiConfiguration))); 1416 mInorder.verify(mWifiPermissionsUtil) 1417 .doesUidBelongToCurrentUserOrDeviceOwner(eq(TEST_UID_1)); 1418 1419 // Verify no more broadcast were sent out. 1420 mInorder.verifyNoMoreInteractions(); 1421 } 1422 1423 /** 1424 * Verify if a user saved network connected and it can match suggestions. Only the 1425 * carrier-privileged suggestor app can receive the broadcast if 1426 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set to true. 1427 */ 1428 @Test testOnSavedOpenNetworkConnectionSuccessWithMultipleMatch()1429 public void testOnSavedOpenNetworkConnectionSuccessWithMultipleMatch() throws Exception { 1430 assertTrue(mWifiNetworkSuggestionsManager.registerSuggestionConnectionStatusListener( 1431 mConnectionStatusListener, TEST_PACKAGE_1, TEST_UID_1)); 1432 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 1433 .thenReturn(true); 1434 WifiConfiguration config = WifiConfigurationTestUtil.createOpenOweNetwork(); 1435 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 1436 new WifiConfiguration(config), null, true, false, true, true, 1437 DEFAULT_PRIORITY_GROUP); 1438 List<WifiNetworkSuggestion> networkSuggestionList1 = 1439 List.of(networkSuggestion1); 1440 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1441 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1442 TEST_PACKAGE_1, TEST_FEATURE)); 1443 1444 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 1445 new WifiConfiguration(config), null, true, false, true, true, 1446 DEFAULT_PRIORITY_GROUP); 1447 List<WifiNetworkSuggestion> networkSuggestionList2 = 1448 List.of(networkSuggestion2); 1449 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1450 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 1451 TEST_PACKAGE_2, TEST_FEATURE)); 1452 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_2, TEST_PACKAGE_2); 1453 1454 // Simulate connecting to the user saved open network. 1455 WifiConfiguration connectNetwork = new WifiConfiguration(config); 1456 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1457 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 1458 1459 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectSuccess(); 1460 // Verify that the correct broadcast was sent out. 1461 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults(eq(TEST_PACKAGE_1), 1462 eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 1463 validatePostConnectionBroadcastSent(TEST_PACKAGE_1, networkSuggestion1); 1464 1465 // Verify no more broadcast were sent out. 1466 mInorder.verifyNoMoreInteractions(); 1467 } 1468 1469 @Test testOnNetworkConnectionFailureWithOneMatchButCallbackOnBinderDied()1470 public void testOnNetworkConnectionFailureWithOneMatchButCallbackOnBinderDied() 1471 throws Exception { 1472 ArgumentCaptor<IBinder.DeathRecipient> drCaptor = 1473 ArgumentCaptor.forClass(IBinder.DeathRecipient.class); 1474 assertTrue(mWifiNetworkSuggestionsManager.registerSuggestionConnectionStatusListener( 1475 mConnectionStatusListener, TEST_PACKAGE_1, TEST_UID_1)); 1476 verify(mBinder).linkToDeath(drCaptor.capture(), anyInt()); 1477 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1478 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 1479 DEFAULT_PRIORITY_GROUP); 1480 List<WifiNetworkSuggestion> networkSuggestionList = 1481 List.of(networkSuggestion); 1482 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1483 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1484 TEST_PACKAGE_1, TEST_FEATURE)); 1485 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1486 // Simulate binder was died. 1487 drCaptor.getValue().binderDied(); 1488 mLooper.dispatchAll(); 1489 // Simulate connecting to the network. 1490 WifiConfiguration connectNetwork = 1491 new WifiConfiguration(networkSuggestion.wifiConfiguration); 1492 connectNetwork.fromWifiNetworkSuggestion = true; 1493 connectNetwork.shared = false; 1494 connectNetwork.ephemeral = true; 1495 connectNetwork.creatorName = TEST_PACKAGE_1; 1496 connectNetwork.creatorUid = TEST_UID_1; 1497 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1498 WifiMetrics.ConnectionEvent.FAILURE_AUTHENTICATION_FAILURE, 1499 connectNetwork, TEST_BSSID); 1500 1501 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectFailure(); 1502 // Verify no connection failure event was sent out. 1503 mInorder.verify(mWifiPermissionsUtil, never()).enforceCanAccessScanResults( 1504 eq(TEST_PACKAGE_1), eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 1505 verify(mConnectionStatusListener, never()).onConnectionStatus(any(), anyInt()); 1506 1507 // Verify no more broadcast were sent out. 1508 mInorder.verify(mContext, never()).sendBroadcastAsUser( 1509 any(), any()); 1510 } 1511 1512 /** 1513 * Verify a successful lookup of a single network suggestion matching the current network 1514 * connection failure. 1515 * a) The corresponding network suggestion has the 1516 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. 1517 * b) The app holds location permission. 1518 * This should trigger a connection failure callback to the app 1519 */ 1520 @Test testOnNetworkConnectionFailureWithOneMatch()1521 public void testOnNetworkConnectionFailureWithOneMatch() throws Exception { 1522 assertTrue(mWifiNetworkSuggestionsManager.registerSuggestionConnectionStatusListener( 1523 mConnectionStatusListener, TEST_PACKAGE_1, TEST_UID_1)); 1524 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1525 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 1526 DEFAULT_PRIORITY_GROUP); 1527 List<WifiNetworkSuggestion> networkSuggestionList = 1528 List.of(networkSuggestion); 1529 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1530 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1531 TEST_PACKAGE_1, TEST_FEATURE)); 1532 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1533 WifiConfiguration connectNetwork = 1534 new WifiConfiguration(networkSuggestion.wifiConfiguration); 1535 connectNetwork.fromWifiNetworkSuggestion = true; 1536 connectNetwork.shared = false; 1537 connectNetwork.ephemeral = true; 1538 connectNetwork.creatorName = TEST_PACKAGE_1; 1539 connectNetwork.creatorUid = TEST_UID_1; 1540 // Simulate connecting to the network. 1541 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1542 WifiMetrics.ConnectionEvent.FAILURE_DHCP, connectNetwork, TEST_BSSID); 1543 1544 // Verify right callback were sent out. 1545 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults(eq(TEST_PACKAGE_1), 1546 eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 1547 verify(mConnectionStatusListener) 1548 .onConnectionStatus(networkSuggestion, 1549 WifiManager.STATUS_SUGGESTION_CONNECTION_FAILURE_IP_PROVISIONING); 1550 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectFailure(); 1551 1552 // Verify no more broadcast were sent out. 1553 mInorder.verify(mContext, never()).sendBroadcastAsUser( 1554 any(), any()); 1555 } 1556 1557 /** 1558 * Verify a successful lookup of multiple network suggestion matching the connected network. 1559 * a) The corresponding network suggestion has the 1560 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. 1561 * b) The app holds location permission. 1562 * This should trigger a broadcast to all the apps. 1563 */ 1564 @Test testOnNetworkConnectionSuccessWithMultipleMatch()1565 public void testOnNetworkConnectionSuccessWithMultipleMatch() { 1566 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 1567 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 1568 new WifiConfiguration(config), null, true, false, true, true, 1569 DEFAULT_PRIORITY_GROUP); 1570 List<WifiNetworkSuggestion> networkSuggestionList1 = 1571 List.of(networkSuggestion1); 1572 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 1573 new WifiConfiguration(config), null, true, false, true, true, 1574 DEFAULT_PRIORITY_GROUP); 1575 List<WifiNetworkSuggestion> networkSuggestionList2 = 1576 List.of(networkSuggestion2); 1577 1578 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1579 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1580 TEST_PACKAGE_1, TEST_FEATURE)); 1581 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1582 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 1583 TEST_PACKAGE_2, TEST_FEATURE)); 1584 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1585 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_2, TEST_PACKAGE_2); 1586 1587 WifiConfiguration connectNetwork = 1588 new WifiConfiguration(networkSuggestion1.wifiConfiguration); 1589 connectNetwork.fromWifiNetworkSuggestion = true; 1590 connectNetwork.shared = false; 1591 connectNetwork.ephemeral = true; 1592 connectNetwork.creatorName = TEST_PACKAGE_1; 1593 connectNetwork.creatorUid = TEST_UID_1; 1594 1595 // Simulate connecting to the network. 1596 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1597 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 1598 1599 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectSuccess(); 1600 1601 ArgumentCaptor<String> packageNameCaptor = ArgumentCaptor.forClass(String.class); 1602 ArgumentCaptor<String> featureIdCaptor = ArgumentCaptor.forClass(String.class); 1603 ArgumentCaptor<Integer> uidCaptor = ArgumentCaptor.forClass(Integer.class); 1604 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults( 1605 packageNameCaptor.capture(), featureIdCaptor.capture(), uidCaptor.capture(), 1606 nullable(String.class)); 1607 assertEquals(TEST_FEATURE, featureIdCaptor.getValue()); 1608 assertEquals(packageNameCaptor.getValue(), TEST_PACKAGE_1); 1609 assertEquals(Integer.valueOf(TEST_UID_1), uidCaptor.getValue()); 1610 validatePostConnectionBroadcastSent(TEST_PACKAGE_1, networkSuggestion1); 1611 1612 // Verify no more broadcast were sent out. 1613 mInorder.verifyNoMoreInteractions(); 1614 } 1615 1616 /** 1617 * Verify a successful lookup of multiple network suggestion matching the connected network. 1618 * a) The corresponding network suggestion has the 1619 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. 1620 * b) The app holds location permission. 1621 * This should trigger a broadcast to all the apps. 1622 */ 1623 @Test testOnNetworkConnectionSuccessWithBssidMultipleMatch()1624 public void testOnNetworkConnectionSuccessWithBssidMultipleMatch() { 1625 WifiConfiguration config = WifiConfigurationTestUtil.createOpenOweNetwork(); 1626 config.BSSID = TEST_BSSID; 1627 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 1628 new WifiConfiguration(config), null, true, false, true, true, 1629 DEFAULT_PRIORITY_GROUP); 1630 List<WifiNetworkSuggestion> networkSuggestionList1 = 1631 List.of(networkSuggestion1); 1632 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 1633 new WifiConfiguration(config), null, true, false, true, true, 1634 DEFAULT_PRIORITY_GROUP); 1635 List<WifiNetworkSuggestion> networkSuggestionList2 = 1636 List.of(networkSuggestion2); 1637 1638 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1639 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1640 TEST_PACKAGE_1, TEST_FEATURE)); 1641 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1642 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 1643 TEST_PACKAGE_2, TEST_FEATURE)); 1644 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1645 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_2, TEST_PACKAGE_2); 1646 WifiConfiguration connectNetwork = new WifiConfiguration(config); 1647 connectNetwork.fromWifiNetworkSuggestion = true; 1648 connectNetwork.shared = false; 1649 connectNetwork.ephemeral = true; 1650 connectNetwork.creatorName = TEST_PACKAGE_1; 1651 connectNetwork.creatorUid = TEST_UID_1; 1652 // Simulate connecting to the network. 1653 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1654 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 1655 1656 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectSuccess(); 1657 1658 // Verify that the correct broadcasts were sent out. 1659 ArgumentCaptor<String> packageNameCaptor = ArgumentCaptor.forClass(String.class); 1660 ArgumentCaptor<String> featureIdCaptor = ArgumentCaptor.forClass(String.class); 1661 ArgumentCaptor<Integer> uidCaptor = ArgumentCaptor.forClass(Integer.class); 1662 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults( 1663 packageNameCaptor.capture(), featureIdCaptor.capture(), uidCaptor.capture(), 1664 nullable(String.class)); 1665 assertEquals(TEST_FEATURE, featureIdCaptor.getValue()); 1666 assertEquals(packageNameCaptor.getValue(), TEST_PACKAGE_1); 1667 assertEquals(Integer.valueOf(TEST_UID_1), uidCaptor.getValue()); 1668 validatePostConnectionBroadcastSent(TEST_PACKAGE_1, networkSuggestion1); 1669 1670 // Verify no more broadcast were sent out. 1671 mInorder.verifyNoMoreInteractions(); 1672 } 1673 1674 /** 1675 * Verify a successful lookup of multiple network suggestion matching the connected network. 1676 * a) The corresponding network suggestion has the 1677 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. 1678 * b) The app holds location permission. 1679 * This should trigger a broadcast to all the apps. 1680 */ 1681 @Test testOnNetworkConnectionSuccessWithBssidAndWithoutBssidMultipleMatch()1682 public void testOnNetworkConnectionSuccessWithBssidAndWithoutBssidMultipleMatch() { 1683 WifiConfiguration wifiConfiguration1 = WifiConfigurationTestUtil.createOpenNetwork(); 1684 WifiConfiguration wifiConfiguration2 = new WifiConfiguration(wifiConfiguration1); 1685 wifiConfiguration2.BSSID = TEST_BSSID; 1686 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 1687 wifiConfiguration1, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 1688 List<WifiNetworkSuggestion> networkSuggestionList1 = 1689 List.of(networkSuggestion1); 1690 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 1691 wifiConfiguration2, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 1692 List<WifiNetworkSuggestion> networkSuggestionList2 = 1693 List.of(networkSuggestion2); 1694 1695 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1696 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 1697 TEST_PACKAGE_1, TEST_FEATURE)); 1698 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1699 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 1700 TEST_PACKAGE_2, TEST_FEATURE)); 1701 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1702 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_2, TEST_PACKAGE_2); 1703 1704 WifiConfiguration connectNetwork = 1705 new WifiConfiguration(networkSuggestion1.wifiConfiguration); 1706 connectNetwork.fromWifiNetworkSuggestion = true; 1707 connectNetwork.shared = false; 1708 connectNetwork.ephemeral = true; 1709 connectNetwork.creatorName = TEST_PACKAGE_1; 1710 connectNetwork.creatorUid = TEST_UID_1; 1711 1712 // Simulate connecting to the network. 1713 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1714 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 1715 1716 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectSuccess(); 1717 1718 // Verify that the correct broadcasts were sent out. 1719 ArgumentCaptor<String> packageNameCaptor = ArgumentCaptor.forClass(String.class); 1720 ArgumentCaptor<String> featureIdCaptor = ArgumentCaptor.forClass(String.class); 1721 ArgumentCaptor<Integer> uidCaptor = ArgumentCaptor.forClass(Integer.class); 1722 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults( 1723 packageNameCaptor.capture(), featureIdCaptor.capture(), uidCaptor.capture(), 1724 nullable(String.class)); 1725 assertEquals(TEST_FEATURE, featureIdCaptor.getValue()); 1726 assertEquals(packageNameCaptor.getValue(), TEST_PACKAGE_1); 1727 assertEquals(Integer.valueOf(TEST_UID_1), uidCaptor.getValue()); 1728 validatePostConnectionBroadcastSent(TEST_PACKAGE_1, networkSuggestion1); 1729 1730 // Verify no more broadcast were sent out. 1731 mInorder.verifyNoMoreInteractions(); 1732 } 1733 1734 /** 1735 * Verify a successful lookup of a single network suggestion matching the connected network. 1736 * a) The corresponding network suggestion has the 1737 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. 1738 * b) The app holds location permission. 1739 * c) App has not been approved by the user. 1740 * This should not trigger a broadcast to the app. 1741 */ 1742 @Test testOnNetworkConnectionWhenAppNotApproved()1743 public void testOnNetworkConnectionWhenAppNotApproved() { 1744 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1745 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 1746 DEFAULT_PRIORITY_GROUP); 1747 List<WifiNetworkSuggestion> networkSuggestionList = 1748 List.of(networkSuggestion); 1749 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1750 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1751 TEST_PACKAGE_1, TEST_FEATURE)); 1752 verify(mWifiPermissionsUtil, times(2)) 1753 .checkNetworkCarrierProvisioningPermission(TEST_UID_1); 1754 assertFalse(mWifiNetworkSuggestionsManager.hasUserApprovedForApp(TEST_PACKAGE_1)); 1755 1756 WifiConfiguration connectNetwork = 1757 new WifiConfiguration(networkSuggestion.wifiConfiguration); 1758 connectNetwork.fromWifiNetworkSuggestion = true; 1759 connectNetwork.shared = false; 1760 connectNetwork.ephemeral = true; 1761 connectNetwork.creatorName = TEST_PACKAGE_1; 1762 connectNetwork.creatorUid = TEST_UID_1; 1763 1764 // Simulate connecting to the network. 1765 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1766 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 1767 1768 // Verify no broadcast was sent out. 1769 mInorder.verify(mWifiPermissionsUtil, never()).enforceCanAccessScanResults( 1770 anyString(), nullable(String.class), anyInt(), nullable(String.class)); 1771 mInorder.verify(mContext, never()).sendBroadcastAsUser( 1772 any(), any()); 1773 } 1774 1775 /** 1776 * Verify a successful lookup of a single network suggestion matching the connected network. 1777 * a) The corresponding network suggestion does not have the 1778 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. 1779 * b) The app holds location permission. 1780 * This should not trigger a broadcast to the app. 1781 */ 1782 @Test testOnNetworkConnectionWhenIsAppInteractionRequiredNotSet()1783 public void testOnNetworkConnectionWhenIsAppInteractionRequiredNotSet() { 1784 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1785 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 1786 DEFAULT_PRIORITY_GROUP); 1787 List<WifiNetworkSuggestion> networkSuggestionList = 1788 List.of(networkSuggestion); 1789 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1790 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1791 TEST_PACKAGE_1, TEST_FEATURE)); 1792 verify(mWifiPermissionsUtil, times(2)) 1793 .checkNetworkCarrierProvisioningPermission(TEST_UID_1); 1794 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1795 1796 WifiConfiguration connectNetwork = 1797 new WifiConfiguration(networkSuggestion.wifiConfiguration); 1798 connectNetwork.fromWifiNetworkSuggestion = true; 1799 connectNetwork.shared = false; 1800 connectNetwork.ephemeral = true; 1801 connectNetwork.creatorName = TEST_PACKAGE_1; 1802 connectNetwork.creatorUid = TEST_UID_1; 1803 1804 // Simulate connecting to the network. 1805 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1806 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 1807 1808 // Verify no broadcast was sent out. 1809 mInorder.verify(mWifiPermissionsUtil, never()).enforceCanAccessScanResults( 1810 anyString(), nullable(String.class), anyInt(), nullable(String.class)); 1811 mInorder.verify(mContext, never()).sendBroadcastAsUser( 1812 any(), any()); 1813 } 1814 1815 /** 1816 * Verify a successful lookup of a single network suggestion matching the connected network. 1817 * a) The corresponding network suggestion has the 1818 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. 1819 * b) The app does not hold location permission. 1820 * This should not trigger a broadcast to the app. 1821 */ 1822 @Test testOnNetworkConnectionWhenAppDoesNotHoldLocationPermission()1823 public void testOnNetworkConnectionWhenAppDoesNotHoldLocationPermission() { 1824 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1825 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 1826 DEFAULT_PRIORITY_GROUP); 1827 List<WifiNetworkSuggestion> networkSuggestionList = 1828 List.of(networkSuggestion); 1829 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1830 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1831 TEST_PACKAGE_1, TEST_FEATURE)); 1832 verify(mWifiPermissionsUtil, times(2)) 1833 .checkNetworkCarrierProvisioningPermission(TEST_UID_1); 1834 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 1835 1836 doThrow(new SecurityException()).when(mWifiPermissionsUtil).enforceCanAccessScanResults( 1837 eq(TEST_PACKAGE_1), eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 1838 1839 WifiConfiguration connectNetwork = 1840 new WifiConfiguration(networkSuggestion.wifiConfiguration); 1841 connectNetwork.fromWifiNetworkSuggestion = true; 1842 connectNetwork.shared = false; 1843 connectNetwork.ephemeral = true; 1844 connectNetwork.creatorName = TEST_PACKAGE_1; 1845 connectNetwork.creatorUid = TEST_UID_1; 1846 1847 // Simulate connecting to the network. 1848 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 1849 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 1850 1851 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults(eq(TEST_PACKAGE_1), 1852 eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 1853 1854 // Verify no broadcast was sent out. 1855 mInorder.verifyNoMoreInteractions(); 1856 } 1857 1858 /** 1859 * Verify triggering of config store write after successful addition of network suggestions. 1860 */ 1861 @Test testAddNetworkSuggestionsConfigStoreWrite()1862 public void testAddNetworkSuggestionsConfigStoreWrite() { 1863 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1864 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 1865 DEFAULT_PRIORITY_GROUP); 1866 1867 List<WifiNetworkSuggestion> networkSuggestionList = 1868 List.of(networkSuggestion); 1869 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1870 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1871 TEST_PACKAGE_1, TEST_FEATURE)); 1872 1873 // Verify config store interactions. 1874 verify(mWifiConfigManager).saveToStore(); 1875 assertTrue(mDataSource.hasNewDataToSerialize()); 1876 1877 Map<String, PerAppInfo> networkSuggestionsMapToWrite = mDataSource.toSerialize(); 1878 assertEquals(1, networkSuggestionsMapToWrite.size()); 1879 assertTrue(networkSuggestionsMapToWrite.keySet().contains(TEST_PACKAGE_1)); 1880 assertFalse(networkSuggestionsMapToWrite.get(TEST_PACKAGE_1).hasUserApproved); 1881 Collection<ExtendedWifiNetworkSuggestion> extNetworkSuggestionsToWrite = 1882 networkSuggestionsMapToWrite.get(TEST_PACKAGE_1).extNetworkSuggestions.values(); 1883 Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = 1884 Set.of(networkSuggestion); 1885 assertEquals(expectedAllNetworkSuggestions, 1886 extNetworkSuggestionsToWrite 1887 .stream() 1888 .collect(Collectors.mapping( 1889 n -> n.wns, 1890 Collectors.toSet()))); 1891 1892 // Ensure that the new data flag has been reset after read. 1893 assertFalse(mDataSource.hasNewDataToSerialize()); 1894 } 1895 1896 /** 1897 * Verify triggering of config store write after successful addition of network suggestions. 1898 * And store write is failure because out of memory. 1899 */ 1900 @Test testAddNetworkSuggestionsConfigStoreWriteFailedByOOM()1901 public void testAddNetworkSuggestionsConfigStoreWriteFailedByOOM() { 1902 when(mWifiConfigManager.saveToStore()).thenThrow(new OutOfMemoryError()) 1903 .thenReturn(true); 1904 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1905 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 1906 DEFAULT_PRIORITY_GROUP); 1907 1908 List<WifiNetworkSuggestion> networkSuggestionList = 1909 List.of(networkSuggestion); 1910 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_INTERNAL, 1911 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1912 TEST_PACKAGE_1, TEST_FEATURE)); 1913 1914 // Verify config store interactions. 1915 verify(mWifiConfigManager, times(2)).saveToStore(); 1916 assertTrue(mDataSource.hasNewDataToSerialize()); 1917 1918 Map<String, PerAppInfo> networkSuggestionsMapToWrite = mDataSource.toSerialize(); 1919 assertEquals(1, networkSuggestionsMapToWrite.size()); 1920 assertEquals(0, networkSuggestionsMapToWrite.get(TEST_PACKAGE_1) 1921 .extNetworkSuggestions.size()); 1922 1923 // Ensure that the new data flag has been reset after read. 1924 assertFalse(mDataSource.hasNewDataToSerialize()); 1925 } 1926 1927 /** 1928 * Verify triggering of config store write after successful removal of network suggestions. 1929 */ 1930 @Test testRemoveNetworkSuggestionsConfigStoreWrite()1931 public void testRemoveNetworkSuggestionsConfigStoreWrite() { 1932 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1933 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 1934 DEFAULT_PRIORITY_GROUP); 1935 1936 List<WifiNetworkSuggestion> networkSuggestionList = 1937 List.of(networkSuggestion); 1938 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1939 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 1940 TEST_PACKAGE_1, TEST_FEATURE)); 1941 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 1942 mWifiNetworkSuggestionsManager.remove(networkSuggestionList, TEST_UID_1, 1943 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 1944 1945 // Verify config store interactions. 1946 verify(mWifiConfigManager, times(2)).saveToStore(); 1947 assertTrue(mDataSource.hasNewDataToSerialize()); 1948 1949 // Expect a single app entry with no active suggestions. 1950 Map<String, PerAppInfo> networkSuggestionsMapToWrite = mDataSource.toSerialize(); 1951 assertEquals(1, networkSuggestionsMapToWrite.size()); 1952 assertTrue(networkSuggestionsMapToWrite.keySet().contains(TEST_PACKAGE_1)); 1953 assertFalse(networkSuggestionsMapToWrite.get(TEST_PACKAGE_1).hasUserApproved); 1954 assertTrue( 1955 networkSuggestionsMapToWrite.get(TEST_PACKAGE_1).extNetworkSuggestions.isEmpty()); 1956 1957 // Ensure that the new data flag has been reset after read. 1958 assertFalse(mDataSource.hasNewDataToSerialize()); 1959 } 1960 1961 /** 1962 * Verify that the internally used WifiConfiguration created by 1963 * ExtendedWifiNetworkSuggestion#createInternalWifiConfiguration forces MAC randomization off 1964 * if MAC randomization should be disabled for that particular config. 1965 */ 1966 @Test testCarrierConfigSsidListToDisableMacRandomizationDisabled()1967 public void testCarrierConfigSsidListToDisableMacRandomizationDisabled() { 1968 assumeTrue(SdkLevel.isAtLeastS()); 1969 PerAppInfo appInfo = new PerAppInfo(TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 1970 appInfo.hasUserApproved = true; 1971 1972 // Create 2 WifiNetworkSuggestion and mock CarrierConfigManager to include the SSID 1973 // of the first suggetion in the MAC randomization disabled list. 1974 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 1975 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 1976 DEFAULT_PRIORITY_GROUP); 1977 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 1978 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 1979 DEFAULT_PRIORITY_GROUP); 1980 1981 when(mWifiCarrierInfoManager.shouldDisableMacRandomization( 1982 eq(networkSuggestion.getWifiConfiguration().SSID), anyInt(), 1983 anyInt())).thenReturn(true); 1984 ExtendedWifiNetworkSuggestion extendedWifiNetworkSuggestion = 1985 ExtendedWifiNetworkSuggestion.fromWns(networkSuggestion, appInfo, true); 1986 // Verify MAC randomization is disabled for the first suggestion network. 1987 assertEquals(WifiConfiguration.RANDOMIZATION_NONE, 1988 extendedWifiNetworkSuggestion.createInternalWifiConfiguration( 1989 mWifiCarrierInfoManager).macRandomizationSetting); 1990 1991 ExtendedWifiNetworkSuggestion extendedWifiNetworkSuggestion2 = 1992 ExtendedWifiNetworkSuggestion.fromWns(networkSuggestion2, appInfo, true); 1993 assertEquals(WifiConfiguration.RANDOMIZATION_PERSISTENT, 1994 extendedWifiNetworkSuggestion2.createInternalWifiConfiguration( 1995 mWifiCarrierInfoManager).macRandomizationSetting); 1996 } 1997 1998 /** 1999 * Verify handling of initial config store read. 2000 */ 2001 @Test testNetworkSuggestionsConfigStoreLoad()2002 public void testNetworkSuggestionsConfigStoreLoad() { 2003 PasspointConfiguration passpointConfiguration = 2004 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 2005 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 2006 passpointConfiguration.getUniqueId()); 2007 PerAppInfo appInfo = new PerAppInfo(TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 2008 appInfo.hasUserApproved = true; 2009 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2010 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2011 DEFAULT_PRIORITY_GROUP); 2012 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 2013 placeholderConfig, passpointConfiguration, false, false, true, true, 2014 DEFAULT_PRIORITY_GROUP); 2015 ExtendedWifiNetworkSuggestion ewns1 = 2016 ExtendedWifiNetworkSuggestion.fromWns(networkSuggestion, appInfo, true); 2017 appInfo.extNetworkSuggestions.put(ewns1.hashCode(), ewns1); 2018 ExtendedWifiNetworkSuggestion ewns2 = 2019 ExtendedWifiNetworkSuggestion.fromWns(networkSuggestion1, appInfo, true); 2020 appInfo.extNetworkSuggestions.put(ewns2.hashCode(), ewns2); 2021 mDataSource.fromDeserialized(Map.of(TEST_PACKAGE_1, appInfo)); 2022 2023 Set<WifiNetworkSuggestion> allNetworkSuggestions = 2024 mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); 2025 Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = 2026 Set.of(networkSuggestion, networkSuggestion1); 2027 assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); 2028 2029 // Ensure we can lookup the network. 2030 ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion.wifiConfiguration); 2031 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 2032 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 2033 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = 2034 Set.of(networkSuggestion); 2035 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 2036 2037 // Ensure we can lookup the passpoint network. 2038 WifiConfiguration connectNetwork = WifiConfigurationTestUtil.createPasspointNetwork(); 2039 connectNetwork.FQDN = TEST_FQDN; 2040 connectNetwork.providerFriendlyName = TEST_FRIENDLY_NAME; 2041 connectNetwork.setPasspointUniqueId(passpointConfiguration.getUniqueId()); 2042 connectNetwork.fromWifiNetworkSuggestion = true; 2043 connectNetwork.shared = false; 2044 connectNetwork.ephemeral = true; 2045 connectNetwork.creatorName = TEST_PACKAGE_1; 2046 connectNetwork.creatorUid = TEST_UID_1; 2047 2048 matchingExtNetworkSuggestions = 2049 mWifiNetworkSuggestionsManager 2050 .getNetworkSuggestionsForWifiConfiguration(connectNetwork, null); 2051 Set<ExtendedWifiNetworkSuggestion> expectedMatchingExtNetworkSuggestions = Set.of( 2052 ExtendedWifiNetworkSuggestion.fromWns(networkSuggestion1, appInfo, true)); 2053 assertEquals(expectedMatchingExtNetworkSuggestions, matchingExtNetworkSuggestions); 2054 } 2055 2056 /** 2057 * Verify handling of config store read after user switch. 2058 */ 2059 @Test testNetworkSuggestionsConfigStoreLoadAfterUserSwitch()2060 public void testNetworkSuggestionsConfigStoreLoadAfterUserSwitch() { 2061 // Read the store initially. 2062 PerAppInfo appInfo1 = new PerAppInfo(TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 2063 appInfo1.hasUserApproved = true; 2064 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 2065 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2066 DEFAULT_PRIORITY_GROUP); 2067 ExtendedWifiNetworkSuggestion ewns = 2068 ExtendedWifiNetworkSuggestion.fromWns(networkSuggestion1, appInfo1, true); 2069 appInfo1.extNetworkSuggestions.put(ewns.hashCode(), ewns); 2070 mDataSource.fromDeserialized(Map.of(TEST_PACKAGE_1, appInfo1)); 2071 2072 2073 // Now simulate user switch. 2074 mDataSource.reset(); 2075 PerAppInfo appInfo2 = new PerAppInfo(TEST_UID_2, TEST_PACKAGE_2, TEST_FEATURE); 2076 appInfo2.hasUserApproved = true; 2077 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 2078 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2079 DEFAULT_PRIORITY_GROUP); 2080 ExtendedWifiNetworkSuggestion ewns2 = 2081 ExtendedWifiNetworkSuggestion.fromWns(networkSuggestion2, appInfo2, true); 2082 appInfo2.extNetworkSuggestions.put(ewns2.hashCode(), ewns2); 2083 mDataSource.fromDeserialized(Map.of(TEST_PACKAGE_2, appInfo2)); 2084 2085 Set<WifiNetworkSuggestion> allNetworkSuggestions = 2086 mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); 2087 Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = 2088 Set.of(networkSuggestion2); 2089 assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); 2090 2091 // Ensure we can lookup the new network. 2092 ScanDetail scanDetail2 = createScanDetailForNetwork(networkSuggestion2.wifiConfiguration); 2093 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 2094 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail2); 2095 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = 2096 Set.of(networkSuggestion2); 2097 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 2098 2099 // Ensure that the previous network can no longer be looked up. 2100 ScanDetail scanDetail1 = createScanDetailForNetwork(networkSuggestion1.wifiConfiguration); 2101 assertTrue(mWifiNetworkSuggestionsManager 2102 .getNetworkSuggestionsForScanDetail(scanDetail1).isEmpty()); 2103 } 2104 2105 /** 2106 * Verify that we will disconnect from the network if the only network suggestion matching the 2107 * connected network is removed. 2108 */ 2109 @Test 2110 public void testRemoveNetworkSuggestionsMatchingConnectionSuccessWithOneMatch()2111 testRemoveNetworkSuggestionsMatchingConnectionSuccessWithOneMatch() { 2112 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2113 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2114 DEFAULT_PRIORITY_GROUP); 2115 List<WifiNetworkSuggestion> networkSuggestionList = 2116 List.of(networkSuggestion); 2117 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2118 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2119 TEST_PACKAGE_1, TEST_FEATURE)); 2120 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 2121 // Simulate connecting to the network. 2122 WifiConfiguration connectNetwork = 2123 new WifiConfiguration(networkSuggestion.wifiConfiguration); 2124 connectNetwork.fromWifiNetworkSuggestion = true; 2125 connectNetwork.shared = false; 2126 connectNetwork.ephemeral = true; 2127 connectNetwork.creatorName = TEST_PACKAGE_1; 2128 connectNetwork.creatorUid = TEST_UID_1; 2129 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 2130 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 2131 2132 // Now remove the network suggestion and ensure we did trigger a disconnect. 2133 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2134 mWifiNetworkSuggestionsManager.remove(networkSuggestionList, TEST_UID_1, 2135 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 2136 verify(mWifiConfigManager).removeSuggestionConfiguredNetwork( 2137 argThat(new WifiConfigMatcher(connectNetwork))); 2138 } 2139 2140 /** 2141 * Verify that we will disconnect from network when App removed all its suggestions by remove 2142 * empty list. 2143 */ 2144 @Test 2145 public void testRemoveAllNetworkSuggestionsMatchingConnectionSuccessWithOneMatch()2146 testRemoveAllNetworkSuggestionsMatchingConnectionSuccessWithOneMatch() { 2147 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2148 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2149 DEFAULT_PRIORITY_GROUP); 2150 List<WifiNetworkSuggestion> networkSuggestionList = 2151 List.of(networkSuggestion); 2152 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2153 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2154 TEST_PACKAGE_1, TEST_FEATURE)); 2155 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 2156 // Simulate connecting to the network. 2157 WifiConfiguration connectNetwork = 2158 new WifiConfiguration(networkSuggestion.wifiConfiguration); 2159 connectNetwork.fromWifiNetworkSuggestion = true; 2160 connectNetwork.shared = false; 2161 connectNetwork.ephemeral = true; 2162 connectNetwork.creatorName = TEST_PACKAGE_1; 2163 connectNetwork.creatorUid = TEST_UID_1; 2164 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 2165 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 2166 2167 // Now remove all network suggestion and ensure we did trigger a disconnect. 2168 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2169 mWifiNetworkSuggestionsManager.remove(new ArrayList<>(), TEST_UID_1, 2170 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 2171 verify(mWifiConfigManager).removeSuggestionConfiguredNetwork( 2172 argThat(new WifiConfigMatcher(connectNetwork))); 2173 } 2174 2175 2176 /** 2177 * Verify that we remove the profile from WifiConfigManager no matter if it is currently 2178 * connected 2179 */ 2180 @Test testRemoveAppMatchingConnectionSuccessWithMultipleMatch()2181 public void testRemoveAppMatchingConnectionSuccessWithMultipleMatch() { 2182 WifiConfiguration config = WifiConfigurationTestUtil.createOpenOweNetwork(); 2183 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 2184 new WifiConfiguration(config), null, true, false, true, true, 2185 DEFAULT_PRIORITY_GROUP); 2186 List<WifiNetworkSuggestion> networkSuggestionList1 = 2187 List.of(networkSuggestion1); 2188 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 2189 new WifiConfiguration(config), null, true, false, true, true, 2190 DEFAULT_PRIORITY_GROUP); 2191 List<WifiNetworkSuggestion> networkSuggestionList2 = 2192 List.of(networkSuggestion2); 2193 2194 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2195 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 2196 TEST_PACKAGE_1, TEST_FEATURE)); 2197 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2198 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 2199 TEST_PACKAGE_2, TEST_FEATURE)); 2200 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 2201 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_2, TEST_PACKAGE_2); 2202 2203 // Simulate connecting to the network. 2204 WifiConfiguration connectNetwork = new WifiConfiguration(config); 2205 connectNetwork.fromWifiNetworkSuggestion = true; 2206 connectNetwork.shared = false; 2207 connectNetwork.ephemeral = true; 2208 connectNetwork.creatorName = TEST_PACKAGE_1; 2209 connectNetwork.creatorUid = TEST_UID_1; 2210 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 2211 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 2212 2213 // Now remove the current connected app and ensure we remove from WifiConfigManager. 2214 mWifiNetworkSuggestionsManager.removeApp(TEST_PACKAGE_1); 2215 verify(mWifiConfigManager).removeSuggestionConfiguredNetwork( 2216 argThat(new WifiConfigMatcher(connectNetwork))); 2217 2218 // Now remove the other app and ensure we remove from WifiConfigManager. 2219 mWifiNetworkSuggestionsManager.removeApp(TEST_PACKAGE_2); 2220 verify(mWifiConfigManager, times(2)).removeSuggestionConfiguredNetwork( 2221 argThat(new WifiConfigMatcher(networkSuggestion2.wifiConfiguration))); 2222 } 2223 2224 /** 2225 * Verify that we start tracking app-ops on first network suggestion add & stop tracking on the 2226 * last network suggestion remove. 2227 */ 2228 @Test testAddRemoveNetworkSuggestionsStartStopAppOpsWatch()2229 public void testAddRemoveNetworkSuggestionsStartStopAppOpsWatch() { 2230 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 2231 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2232 DEFAULT_PRIORITY_GROUP); 2233 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 2234 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2235 DEFAULT_PRIORITY_GROUP); 2236 2237 List<WifiNetworkSuggestion> networkSuggestionList1 = 2238 List.of(networkSuggestion1); 2239 List<WifiNetworkSuggestion> networkSuggestionList2 = 2240 List.of(networkSuggestion2); 2241 2242 mInorder = inOrder(mAppOpsManager); 2243 2244 // Watch app-ops changes on first add. 2245 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2246 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 2247 TEST_PACKAGE_1, TEST_FEATURE)); 2248 mInorder.verify(mAppOpsManager).startWatchingMode(eq(OPSTR_CHANGE_WIFI_STATE), 2249 eq(TEST_PACKAGE_1), mAppOpChangedListenerCaptor.capture()); 2250 2251 // Nothing happens on second add. 2252 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2253 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_1, 2254 TEST_PACKAGE_1, TEST_FEATURE)); 2255 2256 // Now remove first add, nothing happens. 2257 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2258 mWifiNetworkSuggestionsManager.remove(networkSuggestionList1, TEST_UID_1, 2259 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 2260 // Stop watching app-ops changes on last remove. 2261 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2262 mWifiNetworkSuggestionsManager.remove(networkSuggestionList2, TEST_UID_1, 2263 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 2264 assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); 2265 verify(mAppOpsManager, never()).stopWatchingMode(mAppOpChangedListenerCaptor.getValue()); 2266 2267 mInorder.verifyNoMoreInteractions(); 2268 } 2269 2270 /** 2271 * Verify app-ops disable/enable after suggestions add. 2272 */ 2273 @Test testAppOpsChangeAfterSuggestionsAdd()2274 public void testAppOpsChangeAfterSuggestionsAdd() { 2275 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 2276 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2277 DEFAULT_PRIORITY_GROUP); 2278 List<WifiNetworkSuggestion> networkSuggestionList = 2279 List.of(networkSuggestion1); 2280 2281 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2282 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2283 TEST_PACKAGE_1, TEST_FEATURE)); 2284 2285 Set<WifiNetworkSuggestion> allNetworkSuggestions = 2286 mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); 2287 Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = 2288 Set.of(networkSuggestion1); 2289 assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); 2290 2291 verify(mAppOpsManager).startWatchingMode(eq(OPSTR_CHANGE_WIFI_STATE), eq(TEST_PACKAGE_1), 2292 mAppOpChangedListenerCaptor.capture()); 2293 AppOpsManager.OnOpChangedListener listener = mAppOpChangedListenerCaptor.getValue(); 2294 assertNotNull(listener); 2295 2296 // allow change wifi state. 2297 when(mAppOpsManager.unsafeCheckOpNoThrow( 2298 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, 2299 TEST_PACKAGE_1)) 2300 .thenReturn(MODE_ALLOWED); 2301 listener.onOpChanged(OPSTR_CHANGE_WIFI_STATE, TEST_PACKAGE_1); 2302 mLooper.dispatchAll(); 2303 allNetworkSuggestions = mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); 2304 assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); 2305 2306 // disallow change wifi state & ensure we remove the app from database. 2307 when(mAppOpsManager.unsafeCheckOpNoThrow( 2308 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, 2309 TEST_PACKAGE_1)) 2310 .thenReturn(MODE_IGNORED); 2311 listener.onOpChanged(OPSTR_CHANGE_WIFI_STATE, TEST_PACKAGE_1); 2312 mLooper.dispatchAll(); 2313 verify(mAppOpsManager).stopWatchingMode(mAppOpChangedListenerCaptor.getValue()); 2314 assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); 2315 verify(mWifiMetrics).incrementNetworkSuggestionUserRevokePermission(); 2316 } 2317 2318 /** 2319 * Verify app-ops disable/enable after config store load. 2320 */ 2321 @Test testAppOpsChangeAfterConfigStoreLoad()2322 public void testAppOpsChangeAfterConfigStoreLoad() { 2323 PerAppInfo appInfo = new PerAppInfo(TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 2324 appInfo.hasUserApproved = true; 2325 mDataSource.fromDeserialized(Map.of(TEST_PACKAGE_1, appInfo)); 2326 // Even the app has no suggestion still monitor the ops change 2327 verify(mAppOpsManager).startWatchingMode(eq(OPSTR_CHANGE_WIFI_STATE), eq(TEST_PACKAGE_1), 2328 mAppOpChangedListenerCaptor.capture()); 2329 AppOpsManager.OnOpChangedListener listener = mAppOpChangedListenerCaptor.getValue(); 2330 assertNotNull(listener); 2331 2332 assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); 2333 assertTrue(mWifiNetworkSuggestionsManager.hasUserApprovedForApp(TEST_PACKAGE_1)); 2334 2335 // allow change wifi state. 2336 when(mAppOpsManager.unsafeCheckOpNoThrow( 2337 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, 2338 TEST_PACKAGE_1)) 2339 .thenReturn(MODE_ALLOWED); 2340 listener.onOpChanged(OPSTR_CHANGE_WIFI_STATE, TEST_PACKAGE_1); 2341 mLooper.dispatchAll(); 2342 assertTrue(mWifiNetworkSuggestionsManager.hasUserApprovedForApp(TEST_PACKAGE_1)); 2343 2344 // disallow change wifi state & ensure we remove all the suggestions for that app. 2345 when(mAppOpsManager.unsafeCheckOpNoThrow( 2346 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, 2347 TEST_PACKAGE_1)) 2348 .thenReturn(MODE_IGNORED); 2349 listener.onOpChanged(OPSTR_CHANGE_WIFI_STATE, TEST_PACKAGE_1); 2350 mLooper.dispatchAll(); 2351 assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); 2352 // And user approval is also removed 2353 assertFalse(mWifiNetworkSuggestionsManager.hasUserApprovedForApp(TEST_PACKAGE_1)); 2354 } 2355 2356 /** 2357 * Verify app-ops disable with wrong uid to package mapping. 2358 */ 2359 @Test testAppOpsChangeWrongUid()2360 public void testAppOpsChangeWrongUid() { 2361 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 2362 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2363 DEFAULT_PRIORITY_GROUP); 2364 List<WifiNetworkSuggestion> networkSuggestionList = 2365 List.of(networkSuggestion1); 2366 2367 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2368 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2369 TEST_PACKAGE_1, TEST_FEATURE)); 2370 2371 Set<WifiNetworkSuggestion> allNetworkSuggestions = 2372 mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); 2373 Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = 2374 Set.of(networkSuggestion1); 2375 assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); 2376 2377 verify(mAppOpsManager).startWatchingMode(eq(OPSTR_CHANGE_WIFI_STATE), eq(TEST_PACKAGE_1), 2378 mAppOpChangedListenerCaptor.capture()); 2379 AppOpsManager.OnOpChangedListener listener = mAppOpChangedListenerCaptor.getValue(); 2380 assertNotNull(listener); 2381 2382 // disallow change wifi state & ensure we don't remove all the suggestions for that app. 2383 doThrow(new SecurityException()).when(mAppOpsManager).checkPackage( 2384 eq(TEST_UID_1), eq(TEST_PACKAGE_1)); 2385 when(mAppOpsManager.unsafeCheckOpNoThrow( 2386 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, 2387 TEST_PACKAGE_1)) 2388 .thenReturn(MODE_IGNORED); 2389 listener.onOpChanged(OPSTR_CHANGE_WIFI_STATE, TEST_PACKAGE_1); 2390 mLooper.dispatchAll(); 2391 allNetworkSuggestions = mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); 2392 assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); 2393 } 2394 2395 /** 2396 * Verify that we stop tracking the package on its removal. 2397 */ 2398 @Test testRemoveApp()2399 public void testRemoveApp() { 2400 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 2401 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2402 DEFAULT_PRIORITY_GROUP); 2403 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 2404 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2405 DEFAULT_PRIORITY_GROUP); 2406 2407 List<WifiNetworkSuggestion> networkSuggestionList1 = 2408 List.of(networkSuggestion1); 2409 List<WifiNetworkSuggestion> networkSuggestionList2 = 2410 List.of(networkSuggestion2); 2411 2412 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2413 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 2414 TEST_PACKAGE_1, TEST_FEATURE)); 2415 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2416 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 2417 TEST_PACKAGE_2, TEST_FEATURE)); 2418 2419 // Remove all suggestions from TEST_PACKAGE_1 & TEST_PACKAGE_2, we should continue to track. 2420 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2421 mWifiNetworkSuggestionsManager.remove(networkSuggestionList1, TEST_UID_1, 2422 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 2423 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2424 mWifiNetworkSuggestionsManager.remove(networkSuggestionList2, TEST_UID_2, 2425 TEST_PACKAGE_2, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 2426 2427 assertTrue(mDataSource.hasNewDataToSerialize()); 2428 Map<String, PerAppInfo> networkSuggestionsMapToWrite = mDataSource.toSerialize(); 2429 assertEquals(2, networkSuggestionsMapToWrite.size()); 2430 assertTrue(networkSuggestionsMapToWrite.keySet().contains(TEST_PACKAGE_1)); 2431 assertTrue(networkSuggestionsMapToWrite.keySet().contains(TEST_PACKAGE_2)); 2432 assertTrue( 2433 networkSuggestionsMapToWrite.get(TEST_PACKAGE_1).extNetworkSuggestions.isEmpty()); 2434 assertTrue( 2435 networkSuggestionsMapToWrite.get(TEST_PACKAGE_2).extNetworkSuggestions.isEmpty()); 2436 2437 // Now remove TEST_PACKAGE_1, continue to track TEST_PACKAGE_2. 2438 mWifiNetworkSuggestionsManager.removeApp(TEST_PACKAGE_1); 2439 assertTrue(mDataSource.hasNewDataToSerialize()); 2440 networkSuggestionsMapToWrite = mDataSource.toSerialize(); 2441 assertEquals(1, networkSuggestionsMapToWrite.size()); 2442 assertTrue(networkSuggestionsMapToWrite.keySet().contains(TEST_PACKAGE_2)); 2443 assertTrue( 2444 networkSuggestionsMapToWrite.get(TEST_PACKAGE_2).extNetworkSuggestions.isEmpty()); 2445 2446 // Now remove TEST_PACKAGE_2. 2447 mWifiNetworkSuggestionsManager.removeApp(TEST_PACKAGE_2); 2448 assertTrue(mDataSource.hasNewDataToSerialize()); 2449 networkSuggestionsMapToWrite = mDataSource.toSerialize(); 2450 assertTrue(networkSuggestionsMapToWrite.isEmpty()); 2451 2452 // Verify that we stopped watching these apps for app-ops changes. 2453 verify(mAppOpsManager, times(2)).stopWatchingMode(any()); 2454 } 2455 2456 2457 /** 2458 * Verify that we stop tracking all packages & it's suggestions on network settings reset. 2459 */ 2460 @Test testClear()2461 public void testClear() { 2462 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 2463 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2464 DEFAULT_PRIORITY_GROUP); 2465 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 2466 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2467 DEFAULT_PRIORITY_GROUP); 2468 2469 List<WifiNetworkSuggestion> networkSuggestionList1 = 2470 List.of(networkSuggestion1); 2471 List<WifiNetworkSuggestion> networkSuggestionList2 = 2472 List.of(networkSuggestion2); 2473 2474 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2475 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 2476 TEST_PACKAGE_1, TEST_FEATURE)); 2477 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2478 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 2479 TEST_PACKAGE_2, TEST_FEATURE)); 2480 2481 // Remove all suggestions from TEST_PACKAGE_1 & TEST_PACKAGE_2, we should continue to track. 2482 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2483 mWifiNetworkSuggestionsManager.remove(networkSuggestionList1, TEST_UID_1, 2484 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 2485 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2486 mWifiNetworkSuggestionsManager.remove(networkSuggestionList2, TEST_UID_2, 2487 TEST_PACKAGE_2, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 2488 2489 assertTrue(mDataSource.hasNewDataToSerialize()); 2490 Map<String, PerAppInfo> networkSuggestionsMapToWrite = mDataSource.toSerialize(); 2491 assertEquals(2, networkSuggestionsMapToWrite.size()); 2492 assertTrue(networkSuggestionsMapToWrite.keySet().contains(TEST_PACKAGE_1)); 2493 assertTrue(networkSuggestionsMapToWrite.keySet().contains(TEST_PACKAGE_2)); 2494 assertTrue( 2495 networkSuggestionsMapToWrite.get(TEST_PACKAGE_1).extNetworkSuggestions.isEmpty()); 2496 assertTrue( 2497 networkSuggestionsMapToWrite.get(TEST_PACKAGE_2).extNetworkSuggestions.isEmpty()); 2498 2499 // Now clear everything. 2500 mWifiNetworkSuggestionsManager.clear(); 2501 assertTrue(mDataSource.hasNewDataToSerialize()); 2502 networkSuggestionsMapToWrite = mDataSource.toSerialize(); 2503 assertTrue(networkSuggestionsMapToWrite.isEmpty()); 2504 2505 // Verify that we stopped watching these apps for app-ops changes. 2506 verify(mAppOpsManager, times(2)).stopWatchingMode(any()); 2507 2508 verify(mWifiNotificationManager).cancel(SystemMessage.NOTE_NETWORK_SUGGESTION_AVAILABLE); 2509 } 2510 2511 /** 2512 * Verify user dismissal notification when first time add suggestions and dismissal the user 2513 * approval notification when framework gets scan results. 2514 */ 2515 @Test testUserApprovalNotificationDismissalWhenGetScanResult()2516 public void testUserApprovalNotificationDismissalWhenGetScanResult() { 2517 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2518 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2519 DEFAULT_PRIORITY_GROUP); 2520 List<WifiNetworkSuggestion> networkSuggestionList = 2521 List.of(networkSuggestion); 2522 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2523 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2524 TEST_PACKAGE_1, TEST_FEATURE)); 2525 validateUserApprovalNotification(TEST_APP_NAME_1); 2526 // Simulate user dismissal notification. 2527 sendBroadcastForUserActionOnApp( 2528 NOTIFICATION_USER_DISMISSED_INTENT_ACTION, TEST_PACKAGE_1, TEST_UID_1); 2529 reset(mWifiNotificationManager); 2530 verify(mWifiMetrics).addUserApprovalSuggestionAppUiReaction( 2531 WifiNetworkSuggestionsManager.ACTION_USER_DISMISS, false); 2532 // Simulate finding the network in scan results. 2533 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2534 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2535 2536 validateUserApprovalNotification(TEST_APP_NAME_1); 2537 2538 // Simulate user dismissal notification. 2539 sendBroadcastForUserActionOnApp( 2540 NOTIFICATION_USER_DISMISSED_INTENT_ACTION, TEST_PACKAGE_1, TEST_UID_1); 2541 2542 reset(mWifiNotificationManager); 2543 // We should resend the notification next time the network is found in scan results. 2544 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2545 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2546 2547 validateUserApprovalNotification(TEST_APP_NAME_1); 2548 verifyNoMoreInteractions(mWifiNotificationManager); 2549 } 2550 2551 /** 2552 * Verify user dismissal notification when first time add suggestions and click on allow on 2553 * the user approval notification when framework gets scan results. 2554 */ 2555 @Test testUserApprovalNotificationClickOnAllowWhenGetScanResult()2556 public void testUserApprovalNotificationClickOnAllowWhenGetScanResult() throws RemoteException { 2557 mWifiNetworkSuggestionsManager.addSuggestionUserApprovalStatusListener( 2558 mUserApprovalStatusListener, TEST_PACKAGE_1, TEST_UID_1); 2559 verify(mUserApprovalStatusListener) 2560 .onUserApprovalStatusChange(WifiManager.STATUS_SUGGESTION_APPROVAL_UNKNOWN); 2561 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2562 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2563 DEFAULT_PRIORITY_GROUP); 2564 List<WifiNetworkSuggestion> networkSuggestionList = 2565 List.of(networkSuggestion); 2566 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2567 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2568 TEST_PACKAGE_1, TEST_FEATURE)); 2569 validateUserApprovalNotification(TEST_APP_NAME_1); 2570 verify(mUserApprovalStatusListener) 2571 .onUserApprovalStatusChange(WifiManager.STATUS_SUGGESTION_APPROVAL_PENDING); 2572 2573 // Simulate user dismissal notification. 2574 sendBroadcastForUserActionOnApp( 2575 NOTIFICATION_USER_DISMISSED_INTENT_ACTION, TEST_PACKAGE_1, TEST_UID_1); 2576 reset(mWifiNotificationManager); 2577 2578 // Simulate finding the network in scan results. 2579 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2580 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2581 2582 validateUserApprovalNotification(TEST_APP_NAME_1); 2583 2584 // Simulate user clicking on allow in the notification. 2585 sendBroadcastForUserActionOnApp( 2586 NOTIFICATION_USER_ALLOWED_APP_INTENT_ACTION, TEST_PACKAGE_1, TEST_UID_1); 2587 // Cancel the notification. 2588 verify(mWifiNotificationManager).cancel(SystemMessage.NOTE_NETWORK_SUGGESTION_AVAILABLE); 2589 2590 // Verify config store interactions. 2591 verify(mWifiConfigManager, times(2)).saveToStore(); 2592 assertTrue(mDataSource.hasNewDataToSerialize()); 2593 verify(mWifiMetrics).addUserApprovalSuggestionAppUiReaction( 2594 WifiNetworkSuggestionsManager.ACTION_USER_ALLOWED_APP, false); 2595 2596 reset(mWifiNotificationManager); 2597 // We should not resend the notification next time the network is found in scan results. 2598 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2599 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2600 verifyNoMoreInteractions(mWifiNotificationManager); 2601 verify(mUserApprovalStatusListener).onUserApprovalStatusChange( 2602 WifiManager.STATUS_SUGGESTION_APPROVAL_APPROVED_BY_USER); 2603 } 2604 2605 /** 2606 * Verify user dismissal notification when first time add suggestions and click on disallow on 2607 * the user approval notification when framework gets scan results. 2608 */ 2609 @Test testUserApprovalNotificationClickOnDisallowWhenGetScanResult()2610 public void testUserApprovalNotificationClickOnDisallowWhenGetScanResult() 2611 throws RemoteException { 2612 mWifiNetworkSuggestionsManager.addSuggestionUserApprovalStatusListener( 2613 mUserApprovalStatusListener, TEST_PACKAGE_1, TEST_UID_1); 2614 verify(mUserApprovalStatusListener) 2615 .onUserApprovalStatusChange(WifiManager.STATUS_SUGGESTION_APPROVAL_UNKNOWN); 2616 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2617 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2618 DEFAULT_PRIORITY_GROUP); 2619 List<WifiNetworkSuggestion> networkSuggestionList = 2620 List.of(networkSuggestion); 2621 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2622 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2623 TEST_PACKAGE_1, TEST_FEATURE)); 2624 verify(mAppOpsManager).startWatchingMode(eq(OPSTR_CHANGE_WIFI_STATE), 2625 eq(TEST_PACKAGE_1), mAppOpChangedListenerCaptor.capture()); 2626 validateUserApprovalNotification(TEST_APP_NAME_1); 2627 2628 // Simulate user dismissal notification. 2629 sendBroadcastForUserActionOnApp( 2630 NOTIFICATION_USER_DISMISSED_INTENT_ACTION, TEST_PACKAGE_1, TEST_UID_1); 2631 reset(mWifiNotificationManager); 2632 2633 // Simulate finding the network in scan results. 2634 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2635 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2636 2637 validateUserApprovalNotification(TEST_APP_NAME_1); 2638 verify(mUserApprovalStatusListener) 2639 .onUserApprovalStatusChange(WifiManager.STATUS_SUGGESTION_APPROVAL_PENDING); 2640 2641 // Simulate user clicking on disallow in the notification. 2642 sendBroadcastForUserActionOnApp( 2643 NOTIFICATION_USER_DISALLOWED_APP_INTENT_ACTION, TEST_PACKAGE_1, TEST_UID_1); 2644 // Ensure we turn off CHANGE_WIFI_STATE app-ops. 2645 verify(mAppOpsManager).setMode( 2646 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, TEST_PACKAGE_1, MODE_IGNORED); 2647 // Cancel the notification. 2648 verify(mWifiNotificationManager).cancel(SystemMessage.NOTE_NETWORK_SUGGESTION_AVAILABLE); 2649 2650 // Verify config store interactions. 2651 verify(mWifiConfigManager, times(2)).saveToStore(); 2652 assertTrue(mDataSource.hasNewDataToSerialize()); 2653 verify(mWifiMetrics).addUserApprovalSuggestionAppUiReaction( 2654 WifiNetworkSuggestionsManager.ACTION_USER_DISALLOWED_APP, false); 2655 2656 reset(mWifiNotificationManager); 2657 2658 // Now trigger the app-ops callback to ensure we remove all of their suggestions. 2659 AppOpsManager.OnOpChangedListener listener = mAppOpChangedListenerCaptor.getValue(); 2660 assertNotNull(listener); 2661 when(mAppOpsManager.unsafeCheckOpNoThrow( 2662 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, 2663 TEST_PACKAGE_1)) 2664 .thenReturn(MODE_IGNORED); 2665 listener.onOpChanged(OPSTR_CHANGE_WIFI_STATE, TEST_PACKAGE_1); 2666 mLooper.dispatchAll(); 2667 assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); 2668 2669 // Assuming the user re-enabled the app again & added the same suggestions back. 2670 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2671 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2672 TEST_PACKAGE_1, TEST_FEATURE)); 2673 2674 // We should resend the notification when the network is again found in scan results. 2675 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2676 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2677 verify(mUserApprovalStatusListener).onUserApprovalStatusChange( 2678 WifiManager.STATUS_SUGGESTION_APPROVAL_REJECTED_BY_USER); 2679 validateUserApprovalNotification(TEST_APP_NAME_1); 2680 verifyNoMoreInteractions(mWifiNotificationManager); 2681 } 2682 2683 /** 2684 * Verify that we don't send a new notification when a pending notification is active. 2685 */ 2686 @Test testUserApprovalNotificationWhilePreviousNotificationActive()2687 public void testUserApprovalNotificationWhilePreviousNotificationActive() { 2688 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2689 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2690 DEFAULT_PRIORITY_GROUP); 2691 List<WifiNetworkSuggestion> networkSuggestionList = 2692 List.of(networkSuggestion); 2693 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2694 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2695 TEST_PACKAGE_1, TEST_FEATURE)); 2696 2697 // Simulate finding the network in scan results. 2698 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2699 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2700 2701 validateUserApprovalNotification(TEST_APP_NAME_1); 2702 2703 reset(mWifiNotificationManager); 2704 // We should not resend the notification next time the network is found in scan results. 2705 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2706 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2707 2708 verifyNoMoreInteractions(mWifiNotificationManager); 2709 } 2710 2711 /** 2712 * Verify get network suggestion return the right result 2713 * 1. App never suggested, should return empty list. 2714 * 2. App has network suggestions, return all its suggestion. 2715 * 3. App suggested and remove them all, should return empty list. 2716 */ 2717 @Test testGetNetworkSuggestions()2718 public void testGetNetworkSuggestions() { 2719 // test App never suggested. 2720 List<WifiNetworkSuggestion> storedNetworkSuggestionListPerApp = 2721 mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1); 2722 assertEquals(storedNetworkSuggestionListPerApp.size(), 0); 2723 2724 // App add network suggestions then get stored suggestions. 2725 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 2726 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 2727 DEFAULT_PRIORITY_GROUP); 2728 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 2729 WifiConfigurationTestUtil.createOweNetwork(), null, false, false, true, true, 2730 DEFAULT_PRIORITY_GROUP); 2731 WifiNetworkSuggestion networkSuggestion3 = createWifiNetworkSuggestion( 2732 WifiConfigurationTestUtil.createSaeNetwork(), null, false, false, true, true, 2733 DEFAULT_PRIORITY_GROUP); 2734 WifiNetworkSuggestion networkSuggestion4 = createWifiNetworkSuggestion( 2735 WifiConfigurationTestUtil.createPskNetwork(), null, false, false, true, true, 2736 DEFAULT_PRIORITY_GROUP); 2737 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 2738 networkSuggestionList.add(networkSuggestion1); 2739 networkSuggestionList.add(networkSuggestion2); 2740 networkSuggestionList.add(networkSuggestion3); 2741 networkSuggestionList.add(networkSuggestion4); 2742 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2743 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2744 TEST_PACKAGE_1, TEST_FEATURE)); 2745 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 2746 storedNetworkSuggestionListPerApp = 2747 mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1); 2748 assertEquals(new HashSet<>(networkSuggestionList), 2749 new HashSet<>(storedNetworkSuggestionListPerApp)); 2750 2751 // App remove all network suggestions, expect empty list. 2752 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2753 mWifiNetworkSuggestionsManager.remove(new ArrayList<>(), TEST_UID_1, 2754 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 2755 storedNetworkSuggestionListPerApp = 2756 mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1); 2757 assertEquals(storedNetworkSuggestionListPerApp.size(), 0); 2758 } 2759 2760 /** 2761 * Verify get hidden networks from All user approve network suggestions 2762 */ 2763 @Test testGetHiddenNetworks()2764 public void testGetHiddenNetworks() { 2765 2766 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2767 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2768 DEFAULT_PRIORITY_GROUP); 2769 WifiNetworkSuggestion hiddenNetworkSuggestion1 = createWifiNetworkSuggestion( 2770 WifiConfigurationTestUtil.createPskHiddenNetwork(), null, true, false, true, true, 2771 DEFAULT_PRIORITY_GROUP); 2772 WifiNetworkSuggestion hiddenNetworkSuggestion2 = createWifiNetworkSuggestion( 2773 WifiConfigurationTestUtil.createPskHiddenNetwork(), null, true, false, true, true, 2774 DEFAULT_PRIORITY_GROUP); 2775 WifiNetworkSuggestion hiddenNetworkSuggestion3 = createWifiNetworkSuggestion( 2776 WifiConfigurationTestUtil.createPskHiddenNetwork(), null, true, false, true, false, 2777 DEFAULT_PRIORITY_GROUP); 2778 List<WifiNetworkSuggestion> networkSuggestionList1 = 2779 List.of(networkSuggestion, hiddenNetworkSuggestion1); 2780 List<WifiNetworkSuggestion> networkSuggestionList2 = 2781 List.of(hiddenNetworkSuggestion2); 2782 List<WifiNetworkSuggestion> networkSuggestionList3 = 2783 List.of(hiddenNetworkSuggestion3); 2784 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2785 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 2786 TEST_PACKAGE_1, TEST_FEATURE)); 2787 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2788 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 2789 TEST_PACKAGE_2, TEST_FEATURE)); 2790 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2791 mWifiNetworkSuggestionsManager.add(networkSuggestionList3, TEST_UID_3, 2792 TEST_PACKAGE_3, TEST_FEATURE)); 2793 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 2794 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(false, TEST_UID_2, TEST_PACKAGE_2); 2795 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_3, TEST_PACKAGE_3); 2796 List<WifiScanner.ScanSettings.HiddenNetwork> hiddenNetworks = 2797 mWifiNetworkSuggestionsManager.retrieveHiddenNetworkList(true /* autoJoinOnly */); 2798 assertEquals(3, hiddenNetworks.size()); 2799 hiddenNetworks = 2800 mWifiNetworkSuggestionsManager.retrieveHiddenNetworkList(false /* autoJoinOnly */); 2801 assertEquals(4, hiddenNetworks.size()); 2802 assertEquals(hiddenNetworkSuggestion1.wifiConfiguration.SSID, hiddenNetworks.get(0).ssid); 2803 } 2804 2805 /** 2806 * Verify handling of user clicking allow on the user approval dialog when first time 2807 * add suggestions. 2808 */ 2809 @Test testUserApprovalDialogClickOnAllowDuringAddingSuggestionsFromFgApp()2810 public void testUserApprovalDialogClickOnAllowDuringAddingSuggestionsFromFgApp() { 2811 // Fg app 2812 when(mActivityManager.getPackageImportance(any())).thenReturn(IMPORTANCE_FOREGROUND); 2813 2814 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2815 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2816 DEFAULT_PRIORITY_GROUP); 2817 List<WifiNetworkSuggestion> networkSuggestionList = 2818 List.of(networkSuggestion); 2819 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2820 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2821 TEST_PACKAGE_1, TEST_FEATURE)); 2822 validateUserApprovalDialog(TEST_APP_NAME_1); 2823 2824 // Simulate user clicking on allow in the dialog. 2825 ArgumentCaptor<WifiDialogManager.SimpleDialogCallback> dialogCallbackCaptor = 2826 ArgumentCaptor.forClass(WifiDialogManager.SimpleDialogCallback.class); 2827 verify(mWifiDialogManager).createSimpleDialog( 2828 any(), any(), any(), any(), any(), dialogCallbackCaptor.capture(), any()); 2829 dialogCallbackCaptor.getValue().onPositiveButtonClicked(); 2830 2831 // Verify config store interactions. 2832 verify(mWifiConfigManager, times(2)).saveToStore(); 2833 assertTrue(mDataSource.hasNewDataToSerialize()); 2834 verify(mWifiMetrics).addUserApprovalSuggestionAppUiReaction( 2835 WifiNetworkSuggestionsManager.ACTION_USER_ALLOWED_APP, true); 2836 2837 // We should not resend the notification next time the network is found in scan results. 2838 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2839 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2840 verifyNoMoreInteractions(mWifiNotificationManager); 2841 } 2842 2843 /** 2844 * Verify handling of user clicking Disallow on the user approval dialog when first time 2845 * add suggestions. 2846 */ 2847 @Test testUserApprovalDialogClickOnDisallowWhenAddSuggestionsFromFgApp()2848 public void testUserApprovalDialogClickOnDisallowWhenAddSuggestionsFromFgApp() { 2849 // Fg app 2850 when(mActivityManager.getPackageImportance(any())).thenReturn(IMPORTANCE_FOREGROUND); 2851 2852 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2853 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2854 DEFAULT_PRIORITY_GROUP); 2855 List<WifiNetworkSuggestion> networkSuggestionList = 2856 List.of(networkSuggestion); 2857 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2858 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2859 TEST_PACKAGE_1, TEST_FEATURE)); 2860 verify(mAppOpsManager).startWatchingMode(eq(OPSTR_CHANGE_WIFI_STATE), 2861 eq(TEST_PACKAGE_1), mAppOpChangedListenerCaptor.capture()); 2862 validateUserApprovalDialog(TEST_APP_NAME_1); 2863 2864 // Simulate user clicking on disallow in the dialog. 2865 ArgumentCaptor<WifiDialogManager.SimpleDialogCallback> dialogCallbackCaptor = 2866 ArgumentCaptor.forClass(WifiDialogManager.SimpleDialogCallback.class); 2867 verify(mWifiDialogManager).createSimpleDialog( 2868 any(), any(), any(), any(), any(), dialogCallbackCaptor.capture(), any()); 2869 dialogCallbackCaptor.getValue().onNegativeButtonClicked(); 2870 // Ensure we turn off CHANGE_WIFI_STATE app-ops. 2871 verify(mAppOpsManager).setMode( 2872 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, TEST_PACKAGE_1, MODE_IGNORED); 2873 2874 // Verify config store interactions. 2875 verify(mWifiConfigManager, times(2)).saveToStore(); 2876 assertTrue(mDataSource.hasNewDataToSerialize()); 2877 verify(mWifiMetrics).addUserApprovalSuggestionAppUiReaction( 2878 WifiNetworkSuggestionsManager.ACTION_USER_DISALLOWED_APP, true); 2879 2880 // Now trigger the app-ops callback to ensure we remove all of their suggestions. 2881 AppOpsManager.OnOpChangedListener listener = mAppOpChangedListenerCaptor.getValue(); 2882 assertNotNull(listener); 2883 when(mAppOpsManager.unsafeCheckOpNoThrow( 2884 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, 2885 TEST_PACKAGE_1)) 2886 .thenReturn(MODE_IGNORED); 2887 listener.onOpChanged(OPSTR_CHANGE_WIFI_STATE, TEST_PACKAGE_1); 2888 mLooper.dispatchAll(); 2889 assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); 2890 2891 // Assuming the user re-enabled the app again & added the same suggestions back. 2892 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2893 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2894 TEST_PACKAGE_1, TEST_FEATURE)); 2895 validateUserApprovalDialog(TEST_APP_NAME_1); 2896 verifyNoMoreInteractions(mWifiNotificationManager); 2897 } 2898 2899 /** 2900 * Verify handling of dismissal of the user approval dialog when first time 2901 * add suggestions. 2902 */ 2903 @Test testUserApprovalDialiogDismissDuringAddingSuggestionsFromFgApp()2904 public void testUserApprovalDialiogDismissDuringAddingSuggestionsFromFgApp() { 2905 // Fg app 2906 when(mActivityManager.getPackageImportance(any())).thenReturn(IMPORTANCE_FOREGROUND); 2907 2908 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2909 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2910 DEFAULT_PRIORITY_GROUP); 2911 List<WifiNetworkSuggestion> networkSuggestionList = 2912 List.of(networkSuggestion); 2913 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2914 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2915 TEST_PACKAGE_1, TEST_FEATURE)); 2916 validateUserApprovalDialog(TEST_APP_NAME_1); 2917 2918 // As the dialog is active, notification will not be sent 2919 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2920 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2921 verify(mWifiNotificationManager, never()).notify(anyInt(), any()); 2922 2923 // Simulate user dismissing the dialog via home/back button. 2924 ArgumentCaptor<WifiDialogManager.SimpleDialogCallback> dialogCallbackCaptor = 2925 ArgumentCaptor.forClass(WifiDialogManager.SimpleDialogCallback.class); 2926 verify(mWifiDialogManager).createSimpleDialog( 2927 any(), any(), any(), any(), any(), dialogCallbackCaptor.capture(), any()); 2928 dialogCallbackCaptor.getValue().onCancelled(); 2929 mLooper.dispatchAll(); 2930 2931 // Verify no new config store or app-op interactions. 2932 verify(mWifiConfigManager).saveToStore(); // 1 already done for add 2933 verify(mAppOpsManager, never()).setMode(any(), anyInt(), any(), anyInt()); 2934 verify(mWifiMetrics).addUserApprovalSuggestionAppUiReaction( 2935 WifiNetworkSuggestionsManager.ACTION_USER_DISMISS, true); 2936 2937 // We should resend the notification next time the network is found in scan results. 2938 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2939 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2940 validateUserApprovalNotification(TEST_APP_NAME_1); 2941 } 2942 2943 /** 2944 * Verify handling of user clicking allow on the user approval notification when first time 2945 * add suggestions. 2946 */ 2947 @Test testUserApprovalNotificationClickOnAllowDuringAddingSuggestionsFromNonFgApp()2948 public void testUserApprovalNotificationClickOnAllowDuringAddingSuggestionsFromNonFgApp() { 2949 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2950 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2951 DEFAULT_PRIORITY_GROUP); 2952 List<WifiNetworkSuggestion> networkSuggestionList = 2953 List.of(networkSuggestion); 2954 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2955 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2956 TEST_PACKAGE_1, TEST_FEATURE)); 2957 validateUserApprovalNotification(TEST_APP_NAME_1); 2958 2959 // Simulate user clicking on allow in the notification. 2960 sendBroadcastForUserActionOnApp( 2961 NOTIFICATION_USER_ALLOWED_APP_INTENT_ACTION, TEST_PACKAGE_1, TEST_UID_1); 2962 // Cancel the notification. 2963 verify(mWifiNotificationManager).cancel(SystemMessage.NOTE_NETWORK_SUGGESTION_AVAILABLE); 2964 2965 // Verify config store interactions. 2966 verify(mWifiConfigManager, times(2)).saveToStore(); 2967 assertTrue(mDataSource.hasNewDataToSerialize()); 2968 2969 reset(mWifiNotificationManager); 2970 // We should not resend the notification next time the network is found in scan results. 2971 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail( 2972 createScanDetailForNetwork(networkSuggestion.wifiConfiguration)); 2973 verifyNoMoreInteractions(mWifiNotificationManager); 2974 } 2975 2976 /** 2977 * Verify handling of user clicking Disallow on the user approval notification when first time 2978 * add suggestions. 2979 */ 2980 @Test testUserApprovalNotificationClickOnDisallowWhenAddSuggestionsFromNonFgApp()2981 public void testUserApprovalNotificationClickOnDisallowWhenAddSuggestionsFromNonFgApp() { 2982 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 2983 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 2984 DEFAULT_PRIORITY_GROUP); 2985 List<WifiNetworkSuggestion> networkSuggestionList = 2986 List.of(networkSuggestion); 2987 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 2988 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 2989 TEST_PACKAGE_1, TEST_FEATURE)); 2990 verify(mAppOpsManager).startWatchingMode(eq(OPSTR_CHANGE_WIFI_STATE), 2991 eq(TEST_PACKAGE_1), mAppOpChangedListenerCaptor.capture()); 2992 validateUserApprovalNotification(TEST_APP_NAME_1); 2993 2994 // Simulate user clicking on disallow in the notification. 2995 sendBroadcastForUserActionOnApp( 2996 NOTIFICATION_USER_DISALLOWED_APP_INTENT_ACTION, TEST_PACKAGE_1, TEST_UID_1); 2997 // Ensure we turn off CHANGE_WIFI_STATE app-ops. 2998 verify(mAppOpsManager).setMode( 2999 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, TEST_PACKAGE_1, MODE_IGNORED); 3000 // Cancel the notification. 3001 verify(mWifiNotificationManager).cancel(SystemMessage.NOTE_NETWORK_SUGGESTION_AVAILABLE); 3002 3003 // Verify config store interactions. 3004 verify(mWifiConfigManager, times(2)).saveToStore(); 3005 assertTrue(mDataSource.hasNewDataToSerialize()); 3006 3007 reset(mWifiNotificationManager); 3008 3009 // Now trigger the app-ops callback to ensure we remove all of their suggestions. 3010 AppOpsManager.OnOpChangedListener listener = mAppOpChangedListenerCaptor.getValue(); 3011 assertNotNull(listener); 3012 when(mAppOpsManager.unsafeCheckOpNoThrow( 3013 OPSTR_CHANGE_WIFI_STATE, TEST_UID_1, 3014 TEST_PACKAGE_1)) 3015 .thenReturn(MODE_IGNORED); 3016 listener.onOpChanged(OPSTR_CHANGE_WIFI_STATE, TEST_PACKAGE_1); 3017 mLooper.dispatchAll(); 3018 assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); 3019 3020 // Assuming the user re-enabled the app again & added the same suggestions back. 3021 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 3022 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3023 TEST_PACKAGE_1, TEST_FEATURE)); 3024 validateUserApprovalNotification(TEST_APP_NAME_1); 3025 verifyNoMoreInteractions(mWifiNotificationManager); 3026 } 3027 3028 /** 3029 * Verify a successful lookup of a single passpoint network suggestion matching the 3030 * connected network. 3031 * a) The corresponding network suggestion has the 3032 * {@link WifiNetworkSuggestion#isAppInteractionRequired} flag set. 3033 * b) The app holds location permission. 3034 * This should trigger a broadcast to the app. 3035 */ 3036 @Test testOnPasspointNetworkConnectionSuccessWithOneMatch()3037 public void testOnPasspointNetworkConnectionSuccessWithOneMatch() { 3038 PasspointConfiguration passpointConfiguration = 3039 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 3040 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 3041 passpointConfiguration.getUniqueId()); 3042 placeholderConfig.setPasspointUniqueId(passpointConfiguration.getUniqueId()); 3043 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3044 placeholderConfig, passpointConfiguration, true, false, true, true, 3045 DEFAULT_PRIORITY_GROUP); 3046 List<WifiNetworkSuggestion> networkSuggestionList = 3047 List.of(networkSuggestion); 3048 when(mPasspointManager.addOrUpdateProvider(any(), anyInt(), anyString(), anyBoolean(), 3049 anyBoolean(), eq(false))).thenReturn(true); 3050 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 3051 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3052 TEST_PACKAGE_1, TEST_FEATURE)); 3053 3054 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 3055 3056 // Simulate connecting to the network. 3057 WifiConfiguration connectNetwork = WifiConfigurationTestUtil.createPasspointNetwork(); 3058 connectNetwork.FQDN = TEST_FQDN; 3059 connectNetwork.providerFriendlyName = TEST_FRIENDLY_NAME; 3060 connectNetwork.fromWifiNetworkSuggestion = true; 3061 connectNetwork.shared = false; 3062 connectNetwork.ephemeral = true; 3063 connectNetwork.creatorName = TEST_PACKAGE_1; 3064 connectNetwork.creatorUid = TEST_UID_1; 3065 connectNetwork.setPasspointUniqueId(passpointConfiguration.getUniqueId()); 3066 3067 verify(mWifiMetrics, never()).incrementNetworkSuggestionApiNumConnectSuccess(); 3068 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 3069 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 3070 3071 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectSuccess(); 3072 3073 // Verify that the correct broadcast was sent out. 3074 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults(eq(TEST_PACKAGE_1), 3075 eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 3076 validatePostConnectionBroadcastSent(TEST_PACKAGE_1, networkSuggestion); 3077 3078 // Verify no more broadcast were sent out. 3079 mInorder.verifyNoMoreInteractions(); 3080 } 3081 3082 /** 3083 * Creates a scan detail corresponding to the provided network values. 3084 */ createScanDetailForNetwork(WifiConfiguration configuration)3085 private ScanDetail createScanDetailForNetwork(WifiConfiguration configuration) { 3086 return WifiConfigurationTestUtil.createScanDetailForNetwork(configuration, 3087 MacAddressUtils.createRandomUnicastAddress().toString(), -45, 0, 0, 0); 3088 } 3089 validatePostConnectionBroadcastSent( String expectedPackageName, WifiNetworkSuggestion expectedNetworkSuggestion)3090 private void validatePostConnectionBroadcastSent( 3091 String expectedPackageName, WifiNetworkSuggestion expectedNetworkSuggestion) { 3092 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 3093 ArgumentCaptor<UserHandle> userHandleCaptor = ArgumentCaptor.forClass(UserHandle.class); 3094 mInorder.verify(mContext, calls(1)).sendBroadcastAsUser( 3095 intentCaptor.capture(), userHandleCaptor.capture()); 3096 3097 assertEquals(userHandleCaptor.getValue(), UserHandle.SYSTEM); 3098 3099 Intent intent = intentCaptor.getValue(); 3100 assertEquals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION, 3101 intent.getAction()); 3102 String packageName = intent.getPackage(); 3103 WifiNetworkSuggestion networkSuggestion = 3104 intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_SUGGESTION); 3105 assertEquals(expectedPackageName, packageName); 3106 assertEquals(expectedNetworkSuggestion, networkSuggestion); 3107 } 3108 validateUserApprovalDialog(String... anyOfExpectedAppNames)3109 private void validateUserApprovalDialog(String... anyOfExpectedAppNames) { 3110 verify(mDialogHandle, atLeastOnce()).launchDialog(); 3111 ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); 3112 verify(mWifiDialogManager, atLeastOnce()).createSimpleDialog( 3113 any(), messageCaptor.capture(), any(), any(), any(), any(), any()); 3114 String message = messageCaptor.getValue(); 3115 assertNotNull(message); 3116 3117 boolean foundMatch = false; 3118 for (int i = 0; i < anyOfExpectedAppNames.length; i++) { 3119 foundMatch = message.contains(anyOfExpectedAppNames[i]); 3120 if (foundMatch) break; 3121 } 3122 assertTrue(foundMatch); 3123 } 3124 validateUserApprovalNotification(String... anyOfExpectedAppNames)3125 private void validateUserApprovalNotification(String... anyOfExpectedAppNames) { 3126 verify(mWifiNotificationManager, atLeastOnce()).notify( 3127 eq(SystemMessage.NOTE_NETWORK_SUGGESTION_AVAILABLE), 3128 eq(mNotification)); 3129 ArgumentCaptor<Notification.BigTextStyle> contentCaptor = 3130 ArgumentCaptor.forClass(Notification.BigTextStyle.class); 3131 verify(mNotificationBuilder, atLeastOnce()).setStyle(contentCaptor.capture()); 3132 Notification.BigTextStyle content = contentCaptor.getValue(); 3133 assertNotNull(content); 3134 3135 boolean foundMatch = false; 3136 for (int i = 0; i < anyOfExpectedAppNames.length; i++) { 3137 foundMatch = content.getBigText().toString().contains(anyOfExpectedAppNames[i]); 3138 if (foundMatch) break; 3139 } 3140 assertTrue(foundMatch); 3141 } 3142 sendBroadcastForUserActionOnApp(String action, String packageName, int uid)3143 private void sendBroadcastForUserActionOnApp(String action, String packageName, int uid) { 3144 Intent intent = new Intent() 3145 .setAction(action) 3146 .putExtra(WifiNetworkSuggestionsManager.EXTRA_PACKAGE_NAME, packageName) 3147 .putExtra(WifiNetworkSuggestionsManager.EXTRA_UID, uid); 3148 assertNotNull(mBroadcastReceiverCaptor.getValue()); 3149 mBroadcastReceiverCaptor.getValue().onReceive(mContext, intent); 3150 } 3151 3152 @Test testAddSuggestionWithValidCarrierIdWithCarrierProvisionPermission()3153 public void testAddSuggestionWithValidCarrierIdWithCarrierProvisionPermission() { 3154 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 3155 config.carrierId = VALID_CARRIER_ID; 3156 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3157 config, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3158 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3159 networkSuggestionList.add(networkSuggestion); 3160 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3161 .thenReturn(true); 3162 3163 int status = mWifiNetworkSuggestionsManager 3164 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3165 3166 assertEquals(status, WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS); 3167 verify(mWifiMetrics).incrementNetworkSuggestionApiUsageNumOfAppInType( 3168 WifiNetworkSuggestionsManager.APP_TYPE_NETWORK_PROVISIONING); 3169 } 3170 3171 @Test testAddSuggestionWithValidCarrierIdWithoutCarrierProvisionPermission()3172 public void testAddSuggestionWithValidCarrierIdWithoutCarrierProvisionPermission() { 3173 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 3174 config.carrierId = VALID_CARRIER_ID; 3175 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3176 config, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3177 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3178 networkSuggestionList.add(networkSuggestion); 3179 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3180 .thenReturn(false); 3181 3182 int status = mWifiNetworkSuggestionsManager 3183 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3184 3185 assertEquals(status, 3186 WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED); 3187 } 3188 3189 @Test testAddSuggestionWithDefaultCarrierIdWithoutCarrierProvisionPermission()3190 public void testAddSuggestionWithDefaultCarrierIdWithoutCarrierProvisionPermission() { 3191 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 3192 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3193 config, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3194 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3195 networkSuggestionList.add(networkSuggestion); 3196 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3197 .thenReturn(false); 3198 3199 int status = mWifiNetworkSuggestionsManager 3200 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3201 3202 assertEquals(status, WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS); 3203 } 3204 3205 /** 3206 * Verify we return the network suggestion matches the target FQDN and user already approved. 3207 */ 3208 @Test testGetPasspointSuggestionFromFqdnWithUserApproval()3209 public void testGetPasspointSuggestionFromFqdnWithUserApproval() { 3210 PasspointConfiguration passpointConfiguration = 3211 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 3212 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 3213 passpointConfiguration.getUniqueId()); 3214 placeholderConfig.FQDN = TEST_FQDN; 3215 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion(placeholderConfig, 3216 passpointConfiguration, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3217 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3218 networkSuggestionList.add(networkSuggestion); 3219 when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), 3220 anyInt(), anyString(), eq(true), eq(true), eq(false))).thenReturn(true); 3221 assertEquals(mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3222 TEST_PACKAGE_1, TEST_FEATURE), WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS); 3223 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 3224 Set<ExtendedWifiNetworkSuggestion> ewns = 3225 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForFqdn(TEST_FQDN); 3226 assertEquals(1, ewns.size()); 3227 assertEquals(networkSuggestion, ewns.iterator().next().wns); 3228 } 3229 3230 /** 3231 * Verify we return no network suggestion with matched target FQDN but user not approved. 3232 */ 3233 @Test testGetPasspointSuggestionFromFqdnWithoutUserApproval()3234 public void testGetPasspointSuggestionFromFqdnWithoutUserApproval() { 3235 PasspointConfiguration passpointConfiguration = 3236 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 3237 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 3238 passpointConfiguration.getUniqueId()); 3239 placeholderConfig.FQDN = TEST_FQDN; 3240 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion(placeholderConfig, 3241 passpointConfiguration, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3242 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3243 networkSuggestionList.add(networkSuggestion); 3244 placeholderConfig.creatorUid = TEST_UID_1; 3245 when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), 3246 anyInt(), anyString(), eq(true), eq(true), eq(false))).thenReturn(true); 3247 assertEquals(mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3248 TEST_PACKAGE_1, TEST_FEATURE), WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS); 3249 Set<ExtendedWifiNetworkSuggestion> ewns = 3250 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForFqdn(TEST_FQDN); 3251 assertTrue(ewns.isEmpty()); 3252 } 3253 3254 /** 3255 * Verify return true when allow user manually connect and user approved the app 3256 */ 3257 @Test testIsPasspointSuggestionSharedWithUserSetToTrue()3258 public void testIsPasspointSuggestionSharedWithUserSetToTrue() { 3259 PasspointConfiguration passpointConfiguration = 3260 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 3261 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 3262 passpointConfiguration.getUniqueId()); 3263 placeholderConfig.FQDN = TEST_FQDN; 3264 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion(placeholderConfig, 3265 passpointConfiguration, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3266 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3267 networkSuggestionList.add(networkSuggestion); 3268 placeholderConfig.creatorUid = TEST_UID_1; 3269 when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), 3270 anyInt(), anyString(), eq(true), eq(true), eq(false))).thenReturn(true); 3271 assertEquals(mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3272 TEST_PACKAGE_1, TEST_FEATURE), WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS); 3273 3274 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(false, TEST_UID_1, TEST_PACKAGE_1); 3275 assertFalse(mWifiNetworkSuggestionsManager 3276 .isPasspointSuggestionSharedWithUser(placeholderConfig)); 3277 3278 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 3279 assertTrue(mWifiNetworkSuggestionsManager 3280 .isPasspointSuggestionSharedWithUser(placeholderConfig)); 3281 3282 placeholderConfig.carrierId = TEST_CARRIER_ID; 3283 placeholderConfig.subscriptionId = TEST_SUBID; 3284 when(mWifiCarrierInfoManager.getBestMatchSubscriptionId(placeholderConfig)) 3285 .thenReturn(TEST_SUBID); 3286 when(mWifiCarrierInfoManager.isSimReady(TEST_SUBID)).thenReturn(false); 3287 assertFalse(mWifiNetworkSuggestionsManager 3288 .isPasspointSuggestionSharedWithUser(placeholderConfig)); 3289 3290 when(mWifiCarrierInfoManager.isSimReady(TEST_SUBID)).thenReturn(true); 3291 assertTrue(mWifiNetworkSuggestionsManager 3292 .isPasspointSuggestionSharedWithUser(placeholderConfig)); 3293 3294 placeholderConfig.meteredHint = true; 3295 when(mWifiCarrierInfoManager.isCarrierNetworkFromNonDefaultDataSim(placeholderConfig)) 3296 .thenReturn(true); 3297 assertFalse(mWifiNetworkSuggestionsManager 3298 .isPasspointSuggestionSharedWithUser(placeholderConfig)); 3299 } 3300 3301 /** 3302 * Verify return true when disallow user manually connect and user approved the app 3303 */ 3304 @Test testIsPasspointSuggestionSharedWithUserSetToFalse()3305 public void testIsPasspointSuggestionSharedWithUserSetToFalse() { 3306 PasspointConfiguration passpointConfiguration = 3307 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 3308 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 3309 passpointConfiguration.getUniqueId()); 3310 placeholderConfig.FQDN = TEST_FQDN; 3311 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion(placeholderConfig, 3312 passpointConfiguration, true, false, false, true, DEFAULT_PRIORITY_GROUP); 3313 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3314 networkSuggestionList.add(networkSuggestion); 3315 when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), 3316 anyInt(), anyString(), eq(true), eq(true), eq(false))).thenReturn(true); 3317 assertEquals(mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3318 TEST_PACKAGE_1, TEST_FEATURE), WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS); 3319 3320 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(false, TEST_UID_1, TEST_PACKAGE_1); 3321 assertFalse(mWifiNetworkSuggestionsManager 3322 .isPasspointSuggestionSharedWithUser(placeholderConfig)); 3323 3324 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 3325 assertFalse(mWifiNetworkSuggestionsManager 3326 .isPasspointSuggestionSharedWithUser(placeholderConfig)); 3327 } 3328 3329 /** 3330 * test getWifiConfigForMatchedNetworkSuggestionsSharedWithUser. 3331 * - app without user approval will not be returned. 3332 * - open network will not be returned. 3333 * - suggestion doesn't allow user manually connect will not be return. 3334 * - Multiple duplicate ScanResults will only return single matched config. 3335 */ 3336 @Test testGetWifiConfigForMatchedNetworkSuggestionsSharedWithUser()3337 public void testGetWifiConfigForMatchedNetworkSuggestionsSharedWithUser() { 3338 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 3339 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 3340 DEFAULT_PRIORITY_GROUP); 3341 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 3342 WifiConfigurationTestUtil.createPskNetwork(), null, false, false, false, true, 3343 DEFAULT_PRIORITY_GROUP); 3344 WifiNetworkSuggestion networkSuggestion3 = createWifiNetworkSuggestion( 3345 WifiConfigurationTestUtil.createPskNetwork(), null, false, false, true, true, 3346 DEFAULT_PRIORITY_GROUP); 3347 List<ScanResult> scanResults = new ArrayList<>(); 3348 scanResults.add( 3349 createScanDetailForNetwork(networkSuggestion1.wifiConfiguration).getScanResult()); 3350 scanResults.add( 3351 createScanDetailForNetwork(networkSuggestion2.wifiConfiguration).getScanResult()); 3352 3353 // Create two same ScanResult for networkSuggestion3 3354 ScanResult scanResult1 = createScanDetailForNetwork(networkSuggestion3.wifiConfiguration) 3355 .getScanResult(); 3356 ScanResult scanResult2 = new ScanResult(scanResult1); 3357 scanResults.add(scanResult1); 3358 scanResults.add(scanResult2); 3359 3360 List<WifiNetworkSuggestion> networkSuggestionList = 3361 List.of(networkSuggestion1, networkSuggestion2, networkSuggestion3); 3362 networkSuggestion1.wifiConfiguration.fromWifiNetworkSuggestion = true; 3363 networkSuggestion2.wifiConfiguration.fromWifiNetworkSuggestion = true; 3364 networkSuggestion3.wifiConfiguration.fromWifiNetworkSuggestion = true; 3365 networkSuggestion1.wifiConfiguration.shared = false; 3366 networkSuggestion2.wifiConfiguration.shared = false; 3367 networkSuggestion3.wifiConfiguration.shared = false; 3368 networkSuggestion1.wifiConfiguration.creatorName = TEST_PACKAGE_1; 3369 networkSuggestion2.wifiConfiguration.creatorName = TEST_PACKAGE_1; 3370 networkSuggestion3.wifiConfiguration.creatorName = TEST_PACKAGE_1; 3371 3372 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 3373 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3374 TEST_PACKAGE_1, TEST_FEATURE)); 3375 setupGetConfiguredNetworksFromWcm(networkSuggestion1.wifiConfiguration, 3376 networkSuggestion2.wifiConfiguration, networkSuggestion3.wifiConfiguration); 3377 // When app is not approved, empty list will be returned 3378 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(false, TEST_UID_1, TEST_PACKAGE_1); 3379 List<WifiConfiguration> wifiConfigurationList = mWifiNetworkSuggestionsManager 3380 .getWifiConfigForMatchedNetworkSuggestionsSharedWithUser(scanResults); 3381 assertEquals(0, wifiConfigurationList.size()); 3382 // App is approved, only allow user connect, not open network will return. 3383 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 3384 wifiConfigurationList = mWifiNetworkSuggestionsManager 3385 .getWifiConfigForMatchedNetworkSuggestionsSharedWithUser(scanResults); 3386 assertEquals(1, wifiConfigurationList.size()); 3387 networkSuggestion3.wifiConfiguration.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); 3388 WifiConfigurationTestUtil.assertConfigurationEqual( 3389 networkSuggestion3.wifiConfiguration, wifiConfigurationList.get(0)); 3390 } 3391 3392 /** 3393 * test getWifiConfigForMatchedNetworkSuggestionsSharedWithUser on carrier network. 3394 * - SIM is not present will not be return 3395 */ 3396 @Test testGetWifiConfigForMatchedCarrierNetworkSuggestionsSharedWithUser()3397 public void testGetWifiConfigForMatchedCarrierNetworkSuggestionsSharedWithUser() { 3398 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3399 .thenReturn(true); 3400 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 3401 WifiConfigurationTestUtil.createPskNetwork(), null, false, false, true, true, 3402 DEFAULT_PRIORITY_GROUP); 3403 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 3404 WifiConfigurationTestUtil.createPskNetwork(), null, false, false, true, true, 3405 DEFAULT_PRIORITY_GROUP); 3406 networkSuggestion1.wifiConfiguration.carrierId = TEST_CARRIER_ID; 3407 networkSuggestion2.wifiConfiguration.carrierId = TEST_CARRIER_ID; 3408 networkSuggestion1.wifiConfiguration.shared = false; 3409 networkSuggestion2.wifiConfiguration.shared = false; 3410 WifiConfiguration configuration = networkSuggestion1.wifiConfiguration; 3411 3412 // Suggestion1 has SIM present, suggestion2 has SIM absent 3413 when(mWifiCarrierInfoManager 3414 .getBestMatchSubscriptionId(argThat(new WifiConfigMatcher(configuration)))) 3415 .thenReturn(TEST_SUBID); 3416 when(mWifiCarrierInfoManager.isSimReady(TEST_SUBID)).thenReturn(true); 3417 List<ScanResult> scanResults = new ArrayList<>(); 3418 scanResults.add( 3419 createScanDetailForNetwork(networkSuggestion1.wifiConfiguration).getScanResult()); 3420 scanResults.add( 3421 createScanDetailForNetwork(networkSuggestion2.wifiConfiguration).getScanResult()); 3422 3423 List<WifiNetworkSuggestion> networkSuggestionList = 3424 List.of(networkSuggestion1, networkSuggestion2); 3425 3426 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 3427 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3428 TEST_PACKAGE_1, TEST_FEATURE)); 3429 networkSuggestion1.wifiConfiguration.fromWifiNetworkSuggestion = true; 3430 networkSuggestion2.wifiConfiguration.fromWifiNetworkSuggestion = true; 3431 networkSuggestion1.wifiConfiguration.creatorName = TEST_PACKAGE_1; 3432 networkSuggestion2.wifiConfiguration.creatorName = TEST_PACKAGE_1; 3433 networkSuggestion1.wifiConfiguration.subscriptionId = TEST_SUBID; 3434 setupGetConfiguredNetworksFromWcm(networkSuggestion1.wifiConfiguration, 3435 networkSuggestion2.wifiConfiguration); 3436 List<WifiConfiguration> wifiConfigurationList = mWifiNetworkSuggestionsManager 3437 .getWifiConfigForMatchedNetworkSuggestionsSharedWithUser(scanResults); 3438 assertEquals(1, wifiConfigurationList.size()); 3439 networkSuggestion1.wifiConfiguration.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); 3440 WifiConfigurationTestUtil.assertConfigurationEqual( 3441 networkSuggestion1.wifiConfiguration, wifiConfigurationList.get(0)); 3442 } 3443 3444 class WifiConfigMatcher implements ArgumentMatcher<WifiConfiguration> { 3445 private final WifiConfiguration mConfig; 3446 WifiConfigMatcher(WifiConfiguration config)3447 WifiConfigMatcher(WifiConfiguration config) { 3448 assertNotNull(config); 3449 mConfig = config; 3450 } 3451 3452 @Override matches(WifiConfiguration otherConfig)3453 public boolean matches(WifiConfiguration otherConfig) { 3454 if (otherConfig == null) return false; 3455 return mConfig.SSID.equals(otherConfig.SSID); 3456 } 3457 } 3458 assertSuggestionsEquals(Set<WifiNetworkSuggestion> expectedSuggestions, Set<ExtendedWifiNetworkSuggestion> actualExtSuggestions)3459 private void assertSuggestionsEquals(Set<WifiNetworkSuggestion> expectedSuggestions, 3460 Set<ExtendedWifiNetworkSuggestion> actualExtSuggestions) { 3461 Set<WifiNetworkSuggestion> actualSuggestions = actualExtSuggestions.stream() 3462 .map(ewns -> ewns.wns) 3463 .collect(Collectors.toSet()); 3464 assertEquals(expectedSuggestions, actualSuggestions); 3465 } 3466 setupGetConfiguredNetworksFromWcm(WifiConfiguration...configs)3467 private void setupGetConfiguredNetworksFromWcm(WifiConfiguration...configs) { 3468 List<WifiConfiguration> wcmConfigs = new ArrayList<>(); 3469 for (int i = 0; i < configs.length; i++) { 3470 WifiConfiguration config = new WifiConfiguration(configs[i]); 3471 config.shared = false; 3472 when(mWifiConfigManager.getConfiguredNetwork(config.getProfileKey())) 3473 .thenReturn(config); 3474 wcmConfigs.add(config); 3475 } 3476 when(mWifiConfigManager.getConfiguredNetworks()).thenReturn(wcmConfigs); 3477 } 3478 3479 /** 3480 * Verify error code returns when add SIM-based network from app has no carrier privileges. 3481 */ 3482 @Test testAddSimCredentialNetworkWithoutCarrierPrivileges()3483 public void testAddSimCredentialNetworkWithoutCarrierPrivileges() { 3484 WifiConfiguration config = 3485 WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, 3486 WifiEnterpriseConfig.Phase2.NONE); 3487 if (SdkLevel.isAtLeastS()) { 3488 config.subscriptionId = TEST_SUBID; 3489 } 3490 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3491 config, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3492 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3493 networkSuggestionList.add(networkSuggestion); 3494 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3495 .thenReturn(false); 3496 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3497 .thenReturn(TelephonyManager.UNKNOWN_CARRIER_ID); 3498 int status = mWifiNetworkSuggestionsManager 3499 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3500 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED, status); 3501 verify(mWifiNotificationManager, never()).notify(anyInt(), any()); 3502 assertEquals(0, mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1).size()); 3503 verify(mWifiMetrics, never()).incrementNetworkSuggestionApiUsageNumOfAppInType(anyInt()); 3504 } 3505 3506 /** 3507 * Verify success when add SIM-based network from app has carrier privileges. 3508 */ 3509 @Test testAddSimCredentialNetworkWithCarrierPrivileges()3510 public void testAddSimCredentialNetworkWithCarrierPrivileges() throws RemoteException { 3511 mWifiNetworkSuggestionsManager.addSuggestionUserApprovalStatusListener( 3512 mUserApprovalStatusListener, TEST_PACKAGE_1, TEST_UID_1); 3513 WifiConfiguration config = 3514 WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, 3515 WifiEnterpriseConfig.Phase2.NONE); 3516 if (SdkLevel.isAtLeastS()) { 3517 config.subscriptionId = TEST_SUBID; 3518 } 3519 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3520 new WifiConfiguration(config), null, true, false, true, true, 3521 DEFAULT_PRIORITY_GROUP); 3522 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3523 networkSuggestionList.add(networkSuggestion); 3524 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3525 .thenReturn(false); 3526 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3527 .thenReturn(VALID_CARRIER_ID); 3528 int status = mWifiNetworkSuggestionsManager 3529 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3530 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, status); 3531 verify(mWifiNotificationManager, never()).notify(anyInt(), any()); 3532 assertEquals(1, mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1).size()); 3533 verify(mWifiMetrics).incrementNetworkSuggestionApiUsageNumOfAppInType( 3534 WifiNetworkSuggestionsManager.APP_TYPE_CARRIER_PRIVILEGED); 3535 verify(mUserApprovalStatusListener).onUserApprovalStatusChange( 3536 WifiManager.STATUS_SUGGESTION_APPROVAL_APPROVED_BY_CARRIER_PRIVILEGE); 3537 3538 WifiNetworkSuggestion suggestionToRemove = createWifiNetworkSuggestion( 3539 new WifiConfiguration(config), null, true, false, true, true, 3540 DEFAULT_PRIORITY_GROUP); 3541 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 3542 mWifiNetworkSuggestionsManager.remove(List.of(suggestionToRemove), TEST_UID_1, 3543 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 3544 } 3545 3546 /** 3547 * Verify success when add SIM-based network from app has carrier provision permission. 3548 */ 3549 @Test testAddSimCredentialNetworkWithCarrierProvisionPermission()3550 public void testAddSimCredentialNetworkWithCarrierProvisionPermission() { 3551 WifiConfiguration config = 3552 WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, 3553 WifiEnterpriseConfig.Phase2.NONE); 3554 if (SdkLevel.isAtLeastS()) { 3555 config.subscriptionId = TEST_SUBID; 3556 } 3557 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3558 config, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3559 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3560 networkSuggestionList.add(networkSuggestion); 3561 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3562 .thenReturn(true); 3563 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3564 .thenReturn(TelephonyManager.UNKNOWN_CARRIER_ID); 3565 int status = mWifiNetworkSuggestionsManager 3566 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3567 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, status); 3568 verify(mWifiNotificationManager, never()).notify(anyInt(), any()); 3569 assertEquals(1, mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1).size()); 3570 } 3571 3572 /** 3573 * Verify matched SIM-based network will return when imsi protection is available. 3574 */ 3575 @Test testMatchSimBasedNetworkWithImsiProtection()3576 public void testMatchSimBasedNetworkWithImsiProtection() { 3577 WifiConfiguration config = 3578 WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, 3579 WifiEnterpriseConfig.Phase2.NONE); 3580 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3581 config, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3582 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3583 networkSuggestionList.add(networkSuggestion); 3584 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3585 .thenReturn(false); 3586 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3587 .thenReturn(VALID_CARRIER_ID); 3588 when(mWifiCarrierInfoManager.getMatchingSubId(VALID_CARRIER_ID)).thenReturn(TEST_SUBID); 3589 when(mWifiCarrierInfoManager.isSimReady(TEST_SUBID)).thenReturn(true); 3590 when(mWifiCarrierInfoManager.requiresImsiEncryption(TEST_SUBID)).thenReturn(true); 3591 when(mWifiCarrierInfoManager.isImsiEncryptionInfoAvailable(TEST_SUBID)).thenReturn(true); 3592 ScanDetail scanDetail = createScanDetailForNetwork(config); 3593 int status = mWifiNetworkSuggestionsManager 3594 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3595 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, status); 3596 3597 Set<ExtendedWifiNetworkSuggestion> matchingExtNetworkSuggestions = 3598 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); 3599 Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = 3600 Set.of(networkSuggestion); 3601 verify(mWifiNotificationManager, never()).notify(anyInt(), any()); 3602 assertSuggestionsEquals(expectedMatchingNetworkSuggestions, matchingExtNetworkSuggestions); 3603 } 3604 3605 /** 3606 * Verify when SIM changes, the app loses carrier privilege. Suggestions from this app will be 3607 * removed. If this app suggests again, it will be considered as normal suggestor. 3608 */ 3609 @Test testSimStateChangeWillResetCarrierPrivilegedApp()3610 public void testSimStateChangeWillResetCarrierPrivilegedApp() { 3611 if (SdkLevel.isAtLeastT()) { 3612 return; 3613 } 3614 WifiConfiguration config = 3615 WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, 3616 WifiEnterpriseConfig.Phase2.NONE); 3617 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3618 config, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3619 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3620 networkSuggestionList.add(networkSuggestion); 3621 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3622 .thenReturn(false); 3623 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3624 .thenReturn(VALID_CARRIER_ID); 3625 int status = mWifiNetworkSuggestionsManager 3626 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3627 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, status); 3628 verify(mWifiNotificationManager, never()).notify(anyInt(), any()); 3629 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3630 .thenReturn(TelephonyManager.UNKNOWN_CARRIER_ID); 3631 mWifiNetworkSuggestionsManager.updateCarrierPrivilegedApps(); 3632 assertEquals(0, mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1).size()); 3633 verify(mWifiConfigManager, times(2)).saveToStore(); 3634 status = mWifiNetworkSuggestionsManager 3635 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3636 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED, status); 3637 networkSuggestionList.clear(); 3638 networkSuggestionList.add(createWifiNetworkSuggestion( 3639 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 3640 DEFAULT_PRIORITY_GROUP)); 3641 status = mWifiNetworkSuggestionsManager 3642 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3643 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, status); 3644 verify(mWifiNotificationManager).notify(anyInt(), any()); 3645 } 3646 3647 /** 3648 * Verify when SIM changes, the app loses carrier privilege. Suggestions from this app will be 3649 * removed. If this app suggests again, it will be considered as normal suggestor. 3650 */ 3651 @Test testCarrierPrivilegedChangeWillResetCarrierPrivilegedApp()3652 public void testCarrierPrivilegedChangeWillResetCarrierPrivilegedApp() { 3653 if (!SdkLevel.isAtLeastT()) { 3654 return; 3655 } 3656 WifiConfiguration config = 3657 WifiConfigurationTestUtil.createEapNetwork(WifiEnterpriseConfig.Eap.SIM, 3658 WifiEnterpriseConfig.Phase2.NONE); 3659 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3660 config, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3661 List<WifiNetworkSuggestion> networkSuggestionList = new ArrayList<>(); 3662 networkSuggestionList.add(networkSuggestion); 3663 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 3664 .thenReturn(false); 3665 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3666 .thenReturn(VALID_CARRIER_ID); 3667 int status = mWifiNetworkSuggestionsManager 3668 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3669 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, status); 3670 verify(mWifiNotificationManager, never()).notify(anyInt(), any()); 3671 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3672 .thenReturn(TelephonyManager.UNKNOWN_CARRIER_ID); 3673 mWifiNetworkSuggestionsManager.updateCarrierPrivilegedApps(Collections.emptySet()); 3674 assertEquals(0, mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1).size()); 3675 verify(mWifiConfigManager, times(2)).saveToStore(); 3676 status = mWifiNetworkSuggestionsManager 3677 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3678 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED, status); 3679 networkSuggestionList.clear(); 3680 networkSuggestionList.add(createWifiNetworkSuggestion( 3681 WifiConfigurationTestUtil.createOpenNetwork(), null, true, false, true, true, 3682 DEFAULT_PRIORITY_GROUP)); 3683 status = mWifiNetworkSuggestionsManager 3684 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE); 3685 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, status); 3686 verify(mWifiNotificationManager).notify(anyInt(), any()); 3687 } 3688 3689 /** 3690 * Verify set AllowAutoJoin on suggestion network . 3691 */ 3692 @Test testSetAllowAutoJoinOnSuggestionNetwork()3693 public void testSetAllowAutoJoinOnSuggestionNetwork() { 3694 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3695 WifiConfigurationTestUtil.createOpenOweNetwork(), null, false, false, true, true, 3696 DEFAULT_PRIORITY_GROUP); 3697 List<WifiNetworkSuggestion> networkSuggestionList = 3698 List.of(networkSuggestion); 3699 WifiConfiguration configuration = 3700 new WifiConfiguration(networkSuggestion.wifiConfiguration); 3701 configuration.fromWifiNetworkSuggestion = true; 3702 configuration.shared = false; 3703 configuration.ephemeral = true; 3704 configuration.creatorName = TEST_PACKAGE_1; 3705 configuration.creatorUid = TEST_UID_1; 3706 // No matching will return false. 3707 assertFalse(mWifiNetworkSuggestionsManager 3708 .allowNetworkSuggestionAutojoin(configuration, false)); 3709 verify(mWifiConfigManager, never()).saveToStore(); 3710 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 3711 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3712 TEST_PACKAGE_1, TEST_FEATURE)); 3713 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 3714 verify(mWifiConfigManager, times(2)).saveToStore(); 3715 reset(mWifiConfigManager); 3716 3717 assertTrue(mWifiNetworkSuggestionsManager 3718 .allowNetworkSuggestionAutojoin(configuration, false)); 3719 verify(mWifiConfigManager).saveToStore(); 3720 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = mWifiNetworkSuggestionsManager 3721 .getNetworkSuggestionsForWifiConfiguration(configuration, 3722 TEST_BSSID); 3723 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 3724 assertFalse(ewns.isAutojoinEnabled); 3725 } 3726 } 3727 3728 /** 3729 * Verify set AllowAutoJoin on passpoint suggestion network. 3730 */ 3731 @Test testSetAllowAutoJoinOnPasspointSuggestionNetwork()3732 public void testSetAllowAutoJoinOnPasspointSuggestionNetwork() { 3733 PasspointConfiguration passpointConfiguration = 3734 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 3735 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 3736 passpointConfiguration.getUniqueId()); 3737 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3738 placeholderConfig, passpointConfiguration, false, false, true, true, 3739 DEFAULT_PRIORITY_GROUP); 3740 List<WifiNetworkSuggestion> networkSuggestionList = 3741 List.of(networkSuggestion); 3742 when(mPasspointManager.addOrUpdateProvider(any(), anyInt(), anyString(), anyBoolean(), 3743 anyBoolean(), eq(false))).thenReturn(true); 3744 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 3745 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3746 TEST_PACKAGE_1, TEST_FEATURE)); 3747 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 3748 verify(mWifiConfigManager, times(2)).saveToStore(); 3749 reset(mWifiConfigManager); 3750 // Create WifiConfiguration for Passpoint network. 3751 WifiConfiguration config = WifiConfigurationTestUtil.createPasspointNetwork(); 3752 config.FQDN = TEST_FQDN; 3753 config.providerFriendlyName = TEST_FRIENDLY_NAME; 3754 config.setPasspointUniqueId(passpointConfiguration.getUniqueId()); 3755 config.fromWifiNetworkSuggestion = true; 3756 config.shared = false; 3757 config.ephemeral = true; 3758 config.creatorName = TEST_PACKAGE_1; 3759 config.creatorUid = TEST_UID_1; 3760 // When update PasspointManager is failure, will return false. 3761 when(mPasspointManager.enableAutojoin(anyString(), isNull(), anyBoolean())) 3762 .thenReturn(false); 3763 assertFalse(mWifiNetworkSuggestionsManager 3764 .allowNetworkSuggestionAutojoin(config, false)); 3765 verify(mWifiConfigManager, never()).saveToStore(); 3766 3767 // When update PasspointManager is success, will return true and persist suggestion. 3768 when(mPasspointManager.enableAutojoin(anyString(), isNull(), anyBoolean())) 3769 .thenReturn(true); 3770 assertTrue(mWifiNetworkSuggestionsManager 3771 .allowNetworkSuggestionAutojoin(config, false)); 3772 verify(mWifiConfigManager).saveToStore(); 3773 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = mWifiNetworkSuggestionsManager 3774 .getNetworkSuggestionsForWifiConfiguration(config, TEST_BSSID); 3775 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 3776 assertFalse(ewns.isAutojoinEnabled); 3777 } 3778 } 3779 3780 /** 3781 * Verify that both passpoint configuration and non passpoint configuration could match 3782 * the ScanResults. 3783 */ 3784 @Test getMatchingScanResultsTestWithPasspointAndNonPasspointMatch()3785 public void getMatchingScanResultsTestWithPasspointAndNonPasspointMatch() { 3786 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, null); 3787 PasspointConfiguration mockPasspoint = mock(PasspointConfiguration.class); 3788 WifiNetworkSuggestion passpointSuggestion = createWifiNetworkSuggestion( 3789 placeholderConfig, mockPasspoint, false, false, true, true, DEFAULT_PRIORITY_GROUP); 3790 WifiNetworkSuggestion nonPasspointSuggestion = createWifiNetworkSuggestion( 3791 WifiConfigurationTestUtil.createOpenNetwork(), 3792 null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 3793 List<WifiNetworkSuggestion> suggestions = 3794 List.of(passpointSuggestion, nonPasspointSuggestion); 3795 ScanResult passpointScanResult = new ScanResult(); 3796 passpointScanResult.wifiSsid = WifiSsid.fromUtf8Text("passpoint"); 3797 List<ScanResult> ppSrList = List.of(passpointScanResult); 3798 ScanResult nonPasspointScanResult = new ScanResult(); 3799 nonPasspointScanResult.wifiSsid = WifiSsid.fromUtf8Text( 3800 nonPasspointSuggestion.wifiConfiguration.SSID); 3801 List<ScanResult> nonPpSrList = List.of(nonPasspointScanResult); 3802 List<ScanResult> allSrList = new ArrayList<>(); 3803 allSrList.add(passpointScanResult); 3804 allSrList.add(nonPasspointScanResult); 3805 allSrList.add(null); 3806 when(mPasspointManager.getMatchingScanResults(eq(mockPasspoint), eq(allSrList))) 3807 .thenReturn(ppSrList); 3808 ScanResultMatchInfo mockMatchInfo = mock(ScanResultMatchInfo.class); 3809 ScanResultMatchInfo nonPasspointMi = new ScanResultMatchInfo(); 3810 nonPasspointMi.networkSsid = nonPasspointSuggestion.wifiConfiguration.SSID; 3811 MockitoSession session = ExtendedMockito.mockitoSession().strictness(Strictness.LENIENT) 3812 .mockStatic(ScanResultMatchInfo.class).startMocking(); 3813 try { 3814 doReturn(nonPasspointMi).when( 3815 () -> ScanResultMatchInfo.fromWifiConfiguration( 3816 nonPasspointSuggestion.wifiConfiguration)); 3817 doReturn(nonPasspointMi).when( 3818 () -> ScanResultMatchInfo.fromScanResult(nonPasspointScanResult)); 3819 doReturn(mockMatchInfo).when( 3820 () -> ScanResultMatchInfo.fromScanResult(passpointScanResult)); 3821 Map<WifiNetworkSuggestion, List<ScanResult>> result = 3822 mWifiNetworkSuggestionsManager.getMatchingScanResults(suggestions, allSrList); 3823 assertEquals(2, result.size()); 3824 assertEquals(1, result.get(nonPasspointSuggestion).size()); 3825 } finally { 3826 session.finishMocking(); 3827 } 3828 } 3829 3830 /** 3831 * Verify that the wifi configuration doesn't match anything 3832 */ 3833 @Test getMatchingScanResultsTestWithMatchNothing()3834 public void getMatchingScanResultsTestWithMatchNothing() { 3835 WifiNetworkSuggestion nonPasspointSuggestion = createWifiNetworkSuggestion( 3836 WifiConfigurationTestUtil.createOpenNetwork(), 3837 null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 3838 List<WifiNetworkSuggestion> suggestions = List.of(nonPasspointSuggestion); 3839 ScanResult nonPasspointScanResult = new ScanResult(); 3840 nonPasspointScanResult.wifiSsid = WifiSsid.fromUtf8Text("non-passpoint"); 3841 List<ScanResult> allSrList = List.of(nonPasspointScanResult); 3842 3843 MockitoSession session = ExtendedMockito.mockitoSession().mockStatic( 3844 ScanResultMatchInfo.class).startMocking(); 3845 ScanResultMatchInfo mockMatchInfo = mock(ScanResultMatchInfo.class); 3846 ScanResultMatchInfo miFromConfig = new ScanResultMatchInfo(); 3847 miFromConfig.networkSsid = nonPasspointSuggestion.wifiConfiguration.SSID; 3848 try { 3849 when(ScanResultMatchInfo.fromWifiConfiguration(any(WifiConfiguration.class))) 3850 .thenReturn(miFromConfig); 3851 when(ScanResultMatchInfo.fromScanResult(eq(nonPasspointScanResult))) 3852 .thenReturn(mockMatchInfo); 3853 Map<WifiNetworkSuggestion, List<ScanResult>> result = 3854 mWifiNetworkSuggestionsManager.getMatchingScanResults(suggestions, allSrList); 3855 assertEquals(1, result.size()); 3856 assertEquals(0, result.get(nonPasspointSuggestion).size()); 3857 } finally { 3858 session.finishMocking(); 3859 } 3860 } 3861 3862 /** 3863 * Verify that the wifi configuration doesn't match anything if the Wificonfiguration 3864 * is invalid. 3865 */ 3866 @Test getMatchingScanResultsTestWithInvalidWifiConfiguration()3867 public void getMatchingScanResultsTestWithInvalidWifiConfiguration() { 3868 WifiNetworkSuggestion nonPasspointSuggestion = createWifiNetworkSuggestion( 3869 WifiConfigurationTestUtil.createOpenNetwork(), 3870 null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 3871 List<WifiNetworkSuggestion> suggestions = List.of(nonPasspointSuggestion); 3872 ScanResult nonPasspointScanResult = new ScanResult(); 3873 nonPasspointScanResult.wifiSsid = WifiSsid.fromUtf8Text("non-passpoint"); 3874 List<ScanResult> allSrList = List.of(nonPasspointScanResult); 3875 3876 MockitoSession session = ExtendedMockito.mockitoSession().mockStatic( 3877 ScanResultMatchInfo.class).startMocking(); 3878 try { 3879 when(ScanResultMatchInfo.fromWifiConfiguration(any(WifiConfiguration.class))) 3880 .thenReturn(null); 3881 3882 Map<WifiNetworkSuggestion, List<ScanResult>> result = 3883 mWifiNetworkSuggestionsManager.getMatchingScanResults(suggestions, allSrList); 3884 assertEquals(1, result.size()); 3885 assertEquals(0, result.get(nonPasspointSuggestion).size()); 3886 } finally { 3887 session.finishMocking(); 3888 } 3889 } 3890 3891 @Test testUpdateAutoJoinIfImsiProtectionIsEnabledAndDisabled()3892 public void testUpdateAutoJoinIfImsiProtectionIsEnabledAndDisabled() { 3893 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3894 .thenReturn(TEST_CARRIER_ID); 3895 when(mWifiCarrierInfoManager.getMatchingSubId(TEST_CARRIER_ID)).thenReturn(TEST_SUBID); 3896 when(mWifiCarrierInfoManager.getCarrierNameForSubId(TEST_SUBID)) 3897 .thenReturn(TEST_CARRIER_NAME); 3898 when(mWifiCarrierInfoManager.requiresImsiEncryption(TEST_SUBID)).thenReturn(false); 3899 when(mWifiCarrierInfoManager.hasUserApprovedImsiPrivacyExemptionForCarrier(TEST_CARRIER_ID)) 3900 .thenReturn(false); 3901 when(mWifiCarrierInfoManager.isOobPseudonymFeatureEnabled(TEST_CARRIER_ID)) 3902 .thenReturn(false); 3903 when(mPasspointManager.addOrUpdateProvider(any(), anyInt(), anyString(), anyBoolean(), 3904 anyBoolean(), eq(false))).thenReturn(true); 3905 3906 WifiConfiguration eapSimConfig = WifiConfigurationTestUtil.createEapNetwork( 3907 WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); 3908 PasspointConfiguration passpointConfiguration = 3909 createTestConfigWithSimCredential(TEST_FQDN, TEST_IMSI, TEST_REALM); 3910 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 3911 passpointConfiguration.getUniqueId()); 3912 placeholderConfig.setPasspointUniqueId(passpointConfiguration.getUniqueId()); 3913 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3914 eapSimConfig, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3915 WifiNetworkSuggestion passpointSuggestion = createWifiNetworkSuggestion( 3916 placeholderConfig, passpointConfiguration, true, false, true, true, 3917 DEFAULT_PRIORITY_GROUP); 3918 List<WifiNetworkSuggestion> networkSuggestionList = 3919 Arrays.asList(networkSuggestion, passpointSuggestion); 3920 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 3921 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3922 TEST_PACKAGE_1, TEST_FEATURE)); 3923 3924 mUserApproveCarrierListenerArgumentCaptor.getValue() 3925 .onImsiProtectedOrUserApprovalChanged(TEST_CARRIER_ID, true); 3926 3927 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = mWifiNetworkSuggestionsManager 3928 .getNetworkSuggestionsForScanDetail(createScanDetailForNetwork(eapSimConfig)); 3929 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 3930 assertTrue(ewns.isAutojoinEnabled); 3931 } 3932 3933 mUserApproveCarrierListenerArgumentCaptor.getValue() 3934 .onImsiProtectedOrUserApprovalChanged(TEST_CARRIER_ID, false); 3935 3936 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 3937 assertFalse(ewns.isAutojoinEnabled); 3938 } 3939 } 3940 3941 /** 3942 * Verify when matching a SIM-Based network without IMSI protection, framework will mark it 3943 * auto-join disable and send notification. If user click on allow, will restore the auto-join 3944 * config. 3945 */ 3946 @Test testSendImsiProtectionNotificationOnUserAllowed()3947 public void testSendImsiProtectionNotificationOnUserAllowed() { 3948 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 3949 .thenReturn(TEST_CARRIER_ID); 3950 when(mWifiCarrierInfoManager.getMatchingSubId(TEST_CARRIER_ID)).thenReturn(TEST_SUBID); 3951 when(mWifiCarrierInfoManager.getCarrierNameForSubId(TEST_SUBID)) 3952 .thenReturn(TEST_CARRIER_NAME); 3953 when(mWifiCarrierInfoManager.requiresImsiEncryption(TEST_SUBID)).thenReturn(false); 3954 when(mWifiCarrierInfoManager.hasUserApprovedImsiPrivacyExemptionForCarrier(TEST_CARRIER_ID)) 3955 .thenReturn(false); 3956 when(mPasspointManager.addOrUpdateProvider(any(), anyInt(), anyString(), anyBoolean(), 3957 anyBoolean(), eq(false))).thenReturn(true); 3958 3959 WifiConfiguration eapSimConfig = WifiConfigurationTestUtil.createEapNetwork( 3960 WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); 3961 PasspointConfiguration passpointConfiguration = 3962 createTestConfigWithSimCredential(TEST_FQDN, TEST_IMSI, TEST_REALM); 3963 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 3964 passpointConfiguration.getUniqueId()); 3965 placeholderConfig.setPasspointUniqueId(passpointConfiguration.getUniqueId()); 3966 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 3967 eapSimConfig, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 3968 WifiNetworkSuggestion passpointSuggestion = createWifiNetworkSuggestion( 3969 placeholderConfig, passpointConfiguration, true, false, true, true, 3970 DEFAULT_PRIORITY_GROUP); 3971 List<WifiNetworkSuggestion> networkSuggestionList = 3972 Arrays.asList(networkSuggestion, passpointSuggestion); 3973 3974 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 3975 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 3976 TEST_PACKAGE_1, TEST_FEATURE)); 3977 3978 verifyNoMoreInteractions(mWifiNotificationManager); 3979 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = mWifiNetworkSuggestionsManager 3980 .getNetworkSuggestionsForScanDetail(createScanDetailForNetwork(eapSimConfig)); 3981 verify(mWifiCarrierInfoManager) 3982 .sendImsiProtectionExemptionNotificationIfRequired(TEST_CARRIER_ID); 3983 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 3984 assertFalse(ewns.isAutojoinEnabled); 3985 } 3986 3987 // Simulate user approved carrier 3988 eapSimConfig.fromWifiNetworkSuggestion = true; 3989 eapSimConfig.shared = false; 3990 eapSimConfig.creatorUid = TEST_UID_1; 3991 eapSimConfig.creatorName = TEST_PACKAGE_1; 3992 when(mWifiConfigManager.getConfiguredNetwork(anyString())).thenReturn(eapSimConfig); 3993 when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), anyString(), eq(false))) 3994 .thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)); 3995 mUserApproveCarrierListenerArgumentCaptor.getValue() 3996 .onImsiProtectedOrUserApprovalChanged(TEST_CARRIER_ID, true); 3997 when(mWifiCarrierInfoManager.hasUserApprovedImsiPrivacyExemptionForCarrier(TEST_CARRIER_ID)) 3998 .thenReturn(true); 3999 verify(mPasspointManager).enableAutojoin(anyString(), any(), eq(true)); 4000 verify(mWifiConfigManager).addOrUpdateNetwork(any(), anyInt(), anyString(), eq(false)); 4001 matchedSuggestions = mWifiNetworkSuggestionsManager 4002 .getNetworkSuggestionsForScanDetail(createScanDetailForNetwork(eapSimConfig)); 4003 4004 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4005 assertTrue(ewns.isAutojoinEnabled); 4006 } 4007 verify(mWifiConfigManager, atLeastOnce()).saveToStore(); 4008 } 4009 4010 @Test testAutoJoinIsNotChangedWhenOobPseudonymIsEnabled()4011 public void testAutoJoinIsNotChangedWhenOobPseudonymIsEnabled() { 4012 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 4013 .thenReturn(TEST_CARRIER_ID); 4014 when(mWifiCarrierInfoManager.getMatchingSubId(TEST_CARRIER_ID)).thenReturn(TEST_SUBID); 4015 when(mWifiCarrierInfoManager.getCarrierNameForSubId(TEST_SUBID)) 4016 .thenReturn(TEST_CARRIER_NAME); 4017 when(mWifiCarrierInfoManager.requiresImsiEncryption(TEST_SUBID)).thenReturn(false); 4018 when(mWifiCarrierInfoManager.hasUserApprovedImsiPrivacyExemptionForCarrier(TEST_CARRIER_ID)) 4019 .thenReturn(false); 4020 when(mWifiCarrierInfoManager.isOobPseudonymFeatureEnabled(TEST_CARRIER_ID)) 4021 .thenReturn(true); 4022 when(mPasspointManager.addOrUpdateProvider(any(), anyInt(), anyString(), anyBoolean(), 4023 anyBoolean(), eq(false))).thenReturn(true); 4024 4025 WifiConfiguration eapSimConfig = WifiConfigurationTestUtil.createEapNetwork( 4026 WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); 4027 PasspointConfiguration passpointConfiguration = 4028 createTestConfigWithSimCredential(TEST_FQDN, TEST_IMSI, TEST_REALM); 4029 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 4030 passpointConfiguration.getUniqueId()); 4031 placeholderConfig.setPasspointUniqueId(passpointConfiguration.getUniqueId()); 4032 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 4033 eapSimConfig, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 4034 WifiNetworkSuggestion passpointSuggestion = createWifiNetworkSuggestion( 4035 placeholderConfig, passpointConfiguration, true, false, true, true, 4036 DEFAULT_PRIORITY_GROUP); 4037 List<WifiNetworkSuggestion> networkSuggestionList = 4038 Arrays.asList(networkSuggestion, passpointSuggestion); 4039 4040 4041 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4042 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 4043 TEST_PACKAGE_1, TEST_FEATURE)); 4044 4045 verifyNoMoreInteractions(mWifiNotificationManager); 4046 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = mWifiNetworkSuggestionsManager 4047 .getNetworkSuggestionsForScanDetail(createScanDetailForNetwork(eapSimConfig)); 4048 verify(mWifiCarrierInfoManager) 4049 .sendImsiProtectionExemptionNotificationIfRequired(TEST_CARRIER_ID); 4050 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4051 assertTrue(ewns.isAutojoinEnabled); 4052 } 4053 } 4054 4055 @Test testNotSendImsiProtectionNotificationOnPeapNetwork()4056 public void testNotSendImsiProtectionNotificationOnPeapNetwork() { 4057 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(TEST_PACKAGE_1)) 4058 .thenReturn(TEST_CARRIER_ID); 4059 when(mWifiCarrierInfoManager.getMatchingSubId(TEST_CARRIER_ID)).thenReturn(TEST_SUBID); 4060 when(mWifiCarrierInfoManager.getCarrierNameForSubId(TEST_SUBID)) 4061 .thenReturn(TEST_CARRIER_NAME); 4062 when(mWifiCarrierInfoManager.requiresImsiEncryption(TEST_SUBID)).thenReturn(false); 4063 when(mWifiCarrierInfoManager.hasUserApprovedImsiPrivacyExemptionForCarrier(TEST_CARRIER_ID)) 4064 .thenReturn(false); 4065 when(mPasspointManager.addOrUpdateProvider(any(), anyInt(), anyString(), anyBoolean(), 4066 anyBoolean(), eq(false))).thenReturn(true); 4067 4068 WifiConfiguration eapSimConfig = WifiConfigurationTestUtil.createEapNetwork( 4069 WifiEnterpriseConfig.Eap.PEAP, WifiEnterpriseConfig.Phase2.AKA); 4070 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 4071 eapSimConfig, null, true, false, true, true, DEFAULT_PRIORITY_GROUP); 4072 List<WifiNetworkSuggestion> networkSuggestionList = 4073 Arrays.asList(networkSuggestion); 4074 4075 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4076 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 4077 TEST_PACKAGE_1, TEST_FEATURE)); 4078 4079 verifyNoMoreInteractions(mWifiNotificationManager); 4080 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = mWifiNetworkSuggestionsManager 4081 .getNetworkSuggestionsForScanDetail(createScanDetailForNetwork(eapSimConfig)); 4082 verify(mWifiCarrierInfoManager, never()) 4083 .sendImsiProtectionExemptionNotificationIfRequired(TEST_CARRIER_ID); 4084 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4085 assertTrue(ewns.isAutojoinEnabled); 4086 } 4087 } 4088 4089 /** 4090 * Verify adding invalid suggestions will return right error reason code. 4091 */ 4092 @Test testAddInvalidNetworkSuggestions()4093 public void testAddInvalidNetworkSuggestions() { 4094 WifiConfiguration invalidConfig = WifiConfigurationTestUtil.createOpenNetwork(); 4095 invalidConfig.SSID = ""; 4096 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion(invalidConfig, 4097 null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4098 List<WifiNetworkSuggestion> networkSuggestionList = 4099 List.of(networkSuggestion); 4100 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 4101 mWifiNetworkSuggestionsManager 4102 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); 4103 } 4104 4105 /** 4106 * Verify adding invalid passpoint suggestions will return right error reason code. 4107 */ 4108 @Test testAddInvalidPasspointNetworkSuggestions()4109 public void testAddInvalidPasspointNetworkSuggestions() { 4110 PasspointConfiguration passpointConfiguration = new PasspointConfiguration(); 4111 HomeSp homeSp = new HomeSp(); 4112 homeSp.setFqdn(TEST_FQDN); 4113 passpointConfiguration.setHomeSp(homeSp); 4114 WifiConfiguration placeholderConfig = new WifiConfiguration(); 4115 placeholderConfig.FQDN = TEST_FQDN; 4116 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion(placeholderConfig, 4117 passpointConfiguration, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4118 List<WifiNetworkSuggestion> networkSuggestionList = 4119 List.of(networkSuggestion); 4120 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 4121 mWifiNetworkSuggestionsManager 4122 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); 4123 } 4124 setupAndGetPnoAvailableSuggestions()4125 private List<WifiNetworkSuggestion> setupAndGetPnoAvailableSuggestions() { 4126 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4127 PasspointConfiguration passpointConfiguration = 4128 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 4129 WifiConfiguration placeholderConfig = new WifiConfiguration(); 4130 placeholderConfig.FQDN = TEST_FQDN; 4131 WifiNetworkSuggestion networkSuggestion = 4132 createWifiNetworkSuggestion(network1, null, false, false, true, true, 4133 DEFAULT_PRIORITY_GROUP); 4134 WifiNetworkSuggestion passpointSuggestion = createWifiNetworkSuggestion(placeholderConfig, 4135 passpointConfiguration, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4136 List<WifiNetworkSuggestion> networkSuggestionList = 4137 List.of(networkSuggestion, passpointSuggestion); 4138 when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), 4139 anyInt(), anyString(), eq(true), eq(true), eq(false))).thenReturn(true); 4140 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4141 mWifiNetworkSuggestionsManager 4142 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); 4143 return networkSuggestionList; 4144 } 4145 4146 /** 4147 * Verify getAllScanOptimizationSuggestionNetworks will only return user approved, 4148 * non-passpoint network. 4149 */ 4150 @Test testGetPnoAvailableSuggestions()4151 public void testGetPnoAvailableSuggestions() { 4152 List<WifiNetworkSuggestion> suggestions = setupAndGetPnoAvailableSuggestions(); 4153 WifiConfiguration network1 = suggestions.get(0).wifiConfiguration; 4154 assertTrue(mWifiNetworkSuggestionsManager 4155 .getAllScanOptimizationSuggestionNetworks().isEmpty()); 4156 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 4157 List<WifiConfiguration> pnoNetwork = 4158 mWifiNetworkSuggestionsManager.getAllScanOptimizationSuggestionNetworks(); 4159 assertEquals(1, pnoNetwork.size()); 4160 assertEquals(network1.SSID, pnoNetwork.get(0).SSID); 4161 } 4162 4163 /** 4164 * Verifies that getAllPasspointScanOptimizationSuggestionNetworks will return the expected 4165 * user-approved Passpoint networks. 4166 */ 4167 @Test testGetPasspointPnoAvailableSuggestions()4168 public void testGetPasspointPnoAvailableSuggestions() { 4169 setupAndGetPnoAvailableSuggestions(); 4170 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 4171 4172 // Suggestion should be available in the full list of results, but not 4173 // in the list of Passpoint suggestions that include an SSID. 4174 assertFalse( 4175 mWifiNetworkSuggestionsManager 4176 .getAllPasspointScanOptimizationSuggestionNetworks(false) 4177 .isEmpty()); 4178 assertTrue( 4179 mWifiNetworkSuggestionsManager 4180 .getAllPasspointScanOptimizationSuggestionNetworks(true) 4181 .isEmpty()); 4182 4183 // Assign an SSID to the Passpoint suggestion. It should now be retrievable when 4184 // requesting the list of Passpoint suggestions that include an SSID. 4185 final String ssid = "my-passpoint-network"; 4186 when(mPasspointManager.getMostRecentSsidForProfile(any())).thenReturn(ssid); 4187 List<WifiConfiguration> configs = 4188 mWifiNetworkSuggestionsManager.getAllPasspointScanOptimizationSuggestionNetworks( 4189 true); 4190 assertEquals(1, configs.size()); 4191 assertEquals(ssid, configs.get(0).SSID); 4192 } 4193 4194 /** 4195 * Verify if a suggestion is mostRecently connected, flag will be persist. 4196 */ 4197 @Test testIsMostRecentlyConnectedSuggestion()4198 public void testIsMostRecentlyConnectedSuggestion() { 4199 WifiConfiguration network = WifiConfigurationTestUtil.createOpenNetwork(); 4200 WifiNetworkSuggestion networkSuggestion = 4201 createWifiNetworkSuggestion(network, null, false, false, true, true, 4202 DEFAULT_PRIORITY_GROUP); 4203 List<WifiNetworkSuggestion> networkSuggestionList = Arrays.asList(networkSuggestion); 4204 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4205 mWifiNetworkSuggestionsManager 4206 .add(networkSuggestionList, TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); 4207 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 4208 when(mLruConnectionTracker.isMostRecentlyConnected(any())).thenReturn(true); 4209 Map<String, PerAppInfo> suggestionStore = new HashMap<>(mDataSource.toSerialize()); 4210 PerAppInfo perAppInfo = suggestionStore.get(TEST_PACKAGE_1); 4211 ExtendedWifiNetworkSuggestion ewns = 4212 perAppInfo.extNetworkSuggestions.values().iterator().next(); 4213 assertTrue(ewns.wns.wifiConfiguration.isMostRecentlyConnected); 4214 mDataSource.fromDeserialized(suggestionStore); 4215 verify(mLruConnectionTracker).addNetwork(any()); 4216 reset(mLruConnectionTracker); 4217 4218 when(mLruConnectionTracker.isMostRecentlyConnected(any())).thenReturn(false); 4219 suggestionStore = mDataSource.toSerialize(); 4220 perAppInfo = suggestionStore.get(TEST_PACKAGE_1); 4221 ewns = perAppInfo.extNetworkSuggestions.values().iterator().next(); 4222 assertFalse(ewns.wns.wifiConfiguration.isMostRecentlyConnected); 4223 mDataSource.fromDeserialized(suggestionStore); 4224 verify(mLruConnectionTracker, never()).addNetwork(any()); 4225 } 4226 4227 @Test testOnSuggestionUpdateListener()4228 public void testOnSuggestionUpdateListener() { 4229 WifiNetworkSuggestionsManager.OnSuggestionUpdateListener listener = 4230 mock(WifiNetworkSuggestionsManager.OnSuggestionUpdateListener.class); 4231 mWifiNetworkSuggestionsManager.addOnSuggestionUpdateListener(listener); 4232 4233 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 4234 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 4235 DEFAULT_PRIORITY_GROUP); 4236 4237 List<WifiNetworkSuggestion> networkSuggestionList1 = 4238 List.of(networkSuggestion1); 4239 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4240 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 4241 TEST_PACKAGE_1, TEST_FEATURE)); 4242 verify(listener).onSuggestionsAddedOrUpdated(networkSuggestionList1); 4243 4244 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4245 mWifiNetworkSuggestionsManager.remove(networkSuggestionList1, TEST_UID_1, 4246 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 4247 verify(listener).onSuggestionsRemoved(networkSuggestionList1); 4248 } 4249 4250 @Test testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithoutSameOpenSuggestion()4251 public void testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithoutSameOpenSuggestion() { 4252 when(mResources.getBoolean( 4253 R.bool.config_wifiIgnoreOpenSavedNetworkWhenSecureSuggestionAvailable)) 4254 .thenReturn(true); 4255 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())) 4256 .thenReturn(true); 4257 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4258 ScanDetail scanDetail1 = createScanDetailForNetwork(network1); 4259 network1.carrierId = TEST_CARRIER_ID; 4260 WifiNetworkSuggestion suggestion1 = createWifiNetworkSuggestion( 4261 network1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4262 WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); 4263 ScanDetail scanDetail2 = createScanDetailForNetwork(network2); 4264 network2.carrierId = TEST_CARRIER_ID; 4265 WifiNetworkSuggestion suggestion2 = createWifiNetworkSuggestion( 4266 network2, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4267 4268 List<ScanDetail> scanDetails = Arrays.asList(scanDetail1, scanDetail2); 4269 // Without same open suggestion in the framework, should not be ignored. 4270 List<WifiNetworkSuggestion> suggestionList = Arrays.asList(suggestion2); 4271 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4272 mWifiNetworkSuggestionsManager.add(suggestionList, TEST_UID_1, 4273 TEST_PACKAGE_1, TEST_FEATURE)); 4274 assertFalse(mWifiNetworkSuggestionsManager 4275 .shouldBeIgnoredBySecureSuggestionFromSameCarrier(network1, scanDetails)); 4276 } 4277 4278 @Test testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithoutSecureSuggestion()4279 public void testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithoutSecureSuggestion() { 4280 when(mResources.getBoolean( 4281 R.bool.config_wifiIgnoreOpenSavedNetworkWhenSecureSuggestionAvailable)) 4282 .thenReturn(true); 4283 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())) 4284 .thenReturn(true); 4285 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4286 ScanDetail scanDetail1 = createScanDetailForNetwork(network1); 4287 network1.carrierId = TEST_CARRIER_ID; 4288 WifiNetworkSuggestion suggestion1 = createWifiNetworkSuggestion( 4289 network1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4290 WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); 4291 ScanDetail scanDetail2 = createScanDetailForNetwork(network2); 4292 network2.carrierId = TEST_CARRIER_ID; 4293 WifiNetworkSuggestion suggestion2 = createWifiNetworkSuggestion( 4294 network2, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4295 4296 List<ScanDetail> scanDetails = Arrays.asList(scanDetail1, scanDetail2); 4297 // Without secure suggestion in the framework, should not be ignored. 4298 List<WifiNetworkSuggestion> suggestionList = Arrays.asList(suggestion1); 4299 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4300 mWifiNetworkSuggestionsManager.add(suggestionList, TEST_UID_1, 4301 TEST_PACKAGE_1, TEST_FEATURE)); 4302 assertFalse(mWifiNetworkSuggestionsManager 4303 .shouldBeIgnoredBySecureSuggestionFromSameCarrier(network1, scanDetails)); 4304 } 4305 4306 @Test testShouldNotBeIgnoredWithoutCarrierProvisioningPermission()4307 public void testShouldNotBeIgnoredWithoutCarrierProvisioningPermission() { 4308 when(mResources.getBoolean( 4309 R.bool.config_wifiIgnoreOpenSavedNetworkWhenSecureSuggestionAvailable)) 4310 .thenReturn(true); 4311 when(mWifiCarrierInfoManager.getCarrierIdForPackageWithCarrierPrivileges(anyString())) 4312 .thenReturn(TEST_CARRIER_ID); 4313 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4314 ScanDetail scanDetail1 = createScanDetailForNetwork(network1); 4315 network1.carrierId = TEST_CARRIER_ID; 4316 WifiNetworkSuggestion suggestion1 = createWifiNetworkSuggestion( 4317 network1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4318 WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); 4319 ScanDetail scanDetail2 = createScanDetailForNetwork(network2); 4320 network2.carrierId = TEST_CARRIER_ID; 4321 WifiNetworkSuggestion suggestion2 = createWifiNetworkSuggestion( 4322 network2, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4323 4324 List<ScanDetail> scanDetails = Arrays.asList(scanDetail1, scanDetail2); 4325 // Without CarrierProvisioningPermission, should not be ignored. 4326 List<WifiNetworkSuggestion> suggestionList = Arrays.asList(suggestion1, suggestion2); 4327 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4328 mWifiNetworkSuggestionsManager.add(suggestionList, TEST_UID_1, 4329 TEST_PACKAGE_1, TEST_FEATURE)); 4330 assertFalse(mWifiNetworkSuggestionsManager 4331 .shouldBeIgnoredBySecureSuggestionFromSameCarrier(network1, scanDetails)); 4332 } 4333 4334 @Test testShouldNotBeIgnoredBySecureSuggestionFromDifferentCarrierId()4335 public void testShouldNotBeIgnoredBySecureSuggestionFromDifferentCarrierId() { 4336 when(mResources.getBoolean( 4337 R.bool.config_wifiIgnoreOpenSavedNetworkWhenSecureSuggestionAvailable)) 4338 .thenReturn(true); 4339 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())) 4340 .thenReturn(true); 4341 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4342 ScanDetail scanDetail1 = createScanDetailForNetwork(network1); 4343 network1.carrierId = VALID_CARRIER_ID; 4344 WifiNetworkSuggestion suggestion1 = createWifiNetworkSuggestion( 4345 network1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4346 WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); 4347 ScanDetail scanDetail2 = createScanDetailForNetwork(network2); 4348 network2.carrierId = TEST_CARRIER_ID; 4349 WifiNetworkSuggestion suggestion2 = createWifiNetworkSuggestion( 4350 network2, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4351 4352 List<ScanDetail> scanDetails = Arrays.asList(scanDetail1, scanDetail2); 4353 // Open and secure suggestions have different carrierId, should not be ignored. 4354 List<WifiNetworkSuggestion> suggestionList = Arrays.asList(suggestion1, suggestion2); 4355 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4356 mWifiNetworkSuggestionsManager.add(suggestionList, TEST_UID_1, 4357 TEST_PACKAGE_1, TEST_FEATURE)); 4358 assertFalse(mWifiNetworkSuggestionsManager 4359 .shouldBeIgnoredBySecureSuggestionFromSameCarrier(network1, scanDetails)); 4360 } 4361 4362 @Test testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithAutojoinDisabled()4363 public void testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithAutojoinDisabled() { 4364 when(mResources.getBoolean( 4365 R.bool.config_wifiIgnoreOpenSavedNetworkWhenSecureSuggestionAvailable)) 4366 .thenReturn(true); 4367 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())) 4368 .thenReturn(true); 4369 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4370 ScanDetail scanDetail1 = createScanDetailForNetwork(network1); 4371 network1.carrierId = TEST_CARRIER_ID; 4372 WifiNetworkSuggestion suggestion1 = createWifiNetworkSuggestion( 4373 network1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4374 WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); 4375 ScanDetail scanDetail2 = createScanDetailForNetwork(network2); 4376 network2.carrierId = TEST_CARRIER_ID; 4377 WifiNetworkSuggestion suggestion2 = createWifiNetworkSuggestion( 4378 network2, null, false, false, true, false, DEFAULT_PRIORITY_GROUP); 4379 4380 List<ScanDetail> scanDetails = Arrays.asList(scanDetail1, scanDetail2); 4381 // Secure suggestions is auto-join disabled, should not be ignored. 4382 List<WifiNetworkSuggestion> suggestionList = Arrays.asList(suggestion1, suggestion2); 4383 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4384 mWifiNetworkSuggestionsManager.add(suggestionList, TEST_UID_1, 4385 TEST_PACKAGE_1, TEST_FEATURE)); 4386 assertFalse(mWifiNetworkSuggestionsManager 4387 .shouldBeIgnoredBySecureSuggestionFromSameCarrier(network1, scanDetails)); 4388 } 4389 4390 @Test testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithDifferentMeterness()4391 public void testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithDifferentMeterness() { 4392 when(mResources.getBoolean( 4393 R.bool.config_wifiIgnoreOpenSavedNetworkWhenSecureSuggestionAvailable)) 4394 .thenReturn(true); 4395 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())) 4396 .thenReturn(true); 4397 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4398 ScanDetail scanDetail1 = createScanDetailForNetwork(network1); 4399 network1.carrierId = TEST_CARRIER_ID; 4400 WifiNetworkSuggestion suggestion1 = createWifiNetworkSuggestion( 4401 network1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4402 WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); 4403 network2.meteredOverride = WifiConfiguration.METERED_OVERRIDE_METERED; 4404 ScanDetail scanDetail2 = createScanDetailForNetwork(network2); 4405 network2.carrierId = TEST_CARRIER_ID; 4406 WifiNetworkSuggestion suggestion2 = createWifiNetworkSuggestion( 4407 network2, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4408 4409 List<ScanDetail> scanDetails = Arrays.asList(scanDetail1, scanDetail2); 4410 // Secure suggestions is auto-join disabled, should not be ignored. 4411 List<WifiNetworkSuggestion> suggestionList = Arrays.asList(suggestion1, suggestion2); 4412 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4413 mWifiNetworkSuggestionsManager.add(suggestionList, TEST_UID_1, 4414 TEST_PACKAGE_1, TEST_FEATURE)); 4415 assertFalse(mWifiNetworkSuggestionsManager 4416 .shouldBeIgnoredBySecureSuggestionFromSameCarrier(network1, scanDetails)); 4417 } 4418 4419 @Test testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithNetworkDisabled()4420 public void testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithNetworkDisabled() { 4421 when(mResources.getBoolean( 4422 R.bool.config_wifiIgnoreOpenSavedNetworkWhenSecureSuggestionAvailable)) 4423 .thenReturn(true); 4424 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())) 4425 .thenReturn(true); 4426 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4427 ScanDetail scanDetail1 = createScanDetailForNetwork(network1); 4428 network1.carrierId = TEST_CARRIER_ID; 4429 WifiNetworkSuggestion suggestion1 = createWifiNetworkSuggestion( 4430 network1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4431 WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); 4432 ScanDetail scanDetail2 = createScanDetailForNetwork(network2); 4433 network2.carrierId = TEST_CARRIER_ID; 4434 WifiNetworkSuggestion suggestion2 = createWifiNetworkSuggestion( 4435 network2, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4436 4437 WifiConfiguration wcmConfig = new WifiConfiguration(network2); 4438 wcmConfig.fromWifiNetworkSuggestion = true; 4439 wcmConfig.creatorName = TEST_PACKAGE_1; 4440 wcmConfig.creatorUid = TEST_UID_1; 4441 WifiConfiguration.NetworkSelectionStatus status = 4442 mock(WifiConfiguration.NetworkSelectionStatus.class); 4443 when(status.isNetworkEnabled()).thenReturn(false); 4444 wcmConfig.setNetworkSelectionStatus(status); 4445 when(mWifiConfigManager.getConfiguredNetwork(wcmConfig.getProfileKey())) 4446 .thenReturn(wcmConfig); 4447 4448 List<ScanDetail> scanDetails = Arrays.asList(scanDetail1, scanDetail2); 4449 // Secure suggestions is auto-join disabled, should not be ignored. 4450 List<WifiNetworkSuggestion> suggestionList = Arrays.asList(suggestion1, suggestion2); 4451 when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), anyString(), eq(false))) 4452 .thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)); 4453 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4454 mWifiNetworkSuggestionsManager.add(suggestionList, TEST_UID_1, 4455 TEST_PACKAGE_1, TEST_FEATURE)); 4456 assertFalse(mWifiNetworkSuggestionsManager 4457 .shouldBeIgnoredBySecureSuggestionFromSameCarrier(network1, scanDetails)); 4458 } 4459 4460 @Test testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithOverlayFalse()4461 public void testShouldNotBeIgnoredBySecureSuggestionFromSameCarrierWithOverlayFalse() { 4462 when(mResources.getBoolean( 4463 R.bool.config_wifiIgnoreOpenSavedNetworkWhenSecureSuggestionAvailable)) 4464 .thenReturn(false); 4465 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())) 4466 .thenReturn(true); 4467 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4468 ScanDetail scanDetail1 = createScanDetailForNetwork(network1); 4469 network1.carrierId = TEST_CARRIER_ID; 4470 WifiNetworkSuggestion suggestion1 = createWifiNetworkSuggestion( 4471 network1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4472 WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); 4473 ScanDetail scanDetail2 = createScanDetailForNetwork(network2); 4474 network2.carrierId = TEST_CARRIER_ID; 4475 WifiNetworkSuggestion suggestion2 = createWifiNetworkSuggestion( 4476 network2, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4477 4478 List<ScanDetail> scanDetails = Arrays.asList(scanDetail1, scanDetail2); 4479 // Both open and secure suggestions with same carrierId, 4480 List<WifiNetworkSuggestion> suggestionList = Arrays.asList(suggestion1, suggestion2); 4481 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4482 mWifiNetworkSuggestionsManager.add(suggestionList, TEST_UID_1, 4483 TEST_PACKAGE_1, TEST_FEATURE)); 4484 assertFalse(mWifiNetworkSuggestionsManager 4485 .shouldBeIgnoredBySecureSuggestionFromSameCarrier(network1, scanDetails)); 4486 } 4487 4488 @Test testShouldBeIgnoredBySecureSuggestionFromSameCarrier()4489 public void testShouldBeIgnoredBySecureSuggestionFromSameCarrier() { 4490 when(mResources.getBoolean( 4491 R.bool.config_wifiIgnoreOpenSavedNetworkWhenSecureSuggestionAvailable)) 4492 .thenReturn(true); 4493 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(anyInt())) 4494 .thenReturn(true); 4495 WifiConfiguration network1 = WifiConfigurationTestUtil.createOpenNetwork(); 4496 ScanDetail scanDetail1 = createScanDetailForNetwork(network1); 4497 network1.carrierId = TEST_CARRIER_ID; 4498 WifiNetworkSuggestion suggestion1 = createWifiNetworkSuggestion( 4499 network1, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4500 WifiConfiguration network2 = WifiConfigurationTestUtil.createPskNetwork(); 4501 ScanDetail scanDetail2 = createScanDetailForNetwork(network2); 4502 network2.carrierId = TEST_CARRIER_ID; 4503 WifiNetworkSuggestion suggestion2 = createWifiNetworkSuggestion( 4504 network2, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4505 4506 List<ScanDetail> scanDetails = Arrays.asList(scanDetail1, scanDetail2); 4507 // Both open and secure suggestions with same carrierId, 4508 List<WifiNetworkSuggestion> suggestionList = Arrays.asList(suggestion1, suggestion2); 4509 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4510 mWifiNetworkSuggestionsManager.add(suggestionList, TEST_UID_1, 4511 TEST_PACKAGE_1, TEST_FEATURE)); 4512 assertTrue(mWifiNetworkSuggestionsManager 4513 .shouldBeIgnoredBySecureSuggestionFromSameCarrier(network1, scanDetails)); 4514 } 4515 4516 @Test testUnregisterSuggestionConnectionStatusListenerNeverRegistered()4517 public void testUnregisterSuggestionConnectionStatusListenerNeverRegistered() { 4518 mWifiNetworkSuggestionsManager.unregisterSuggestionConnectionStatusListener( 4519 mConnectionStatusListener, TEST_PACKAGE_1, TEST_UID_1); 4520 } 4521 4522 /** 4523 * Verify that we only return user approved suggestions. 4524 */ 4525 @Test testGetApprovedNetworkSuggestions()4526 public void testGetApprovedNetworkSuggestions() { 4527 WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createOpenNetwork(); 4528 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 4529 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4530 // Reuse the same network credentials to ensure they both match. 4531 WifiNetworkSuggestion networkSuggestion2 = createWifiNetworkSuggestion( 4532 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4533 4534 List<WifiNetworkSuggestion> networkSuggestionList1 = 4535 List.of(networkSuggestion1); 4536 List<WifiNetworkSuggestion> networkSuggestionList2 = 4537 List.of(networkSuggestion2); 4538 4539 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4540 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 4541 TEST_PACKAGE_1, TEST_FEATURE)); 4542 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4543 mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_UID_2, 4544 TEST_PACKAGE_2, TEST_FEATURE)); 4545 4546 // nothing approved, return empty. 4547 assertTrue(mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions().isEmpty()); 4548 4549 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 4550 // only app 1 approved. 4551 assertEquals(Set.of(networkSuggestion1), 4552 mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()); 4553 4554 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_2); 4555 // both app 1 & 2 approved, but they're equal so the set only has 1 element 4556 assertEquals(Set.of(networkSuggestion1), 4557 mWifiNetworkSuggestionsManager.getAllApprovedNetworkSuggestions()); 4558 } 4559 4560 /** 4561 * Verify only carrier privileged app can suggest carrier merged network. A valid carrier 4562 * merged network must be metered enterprise network with a valid subscription Id. 4563 */ 4564 @Test testAddCarrierMergedNetwork()4565 public void testAddCarrierMergedNetwork() { 4566 assumeTrue(SdkLevel.isAtLeastS()); 4567 WifiConfiguration eapSimConfig = WifiConfigurationTestUtil.createEapNetwork( 4568 WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); 4569 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 4570 eapSimConfig.carrierMerged = true; 4571 eapSimConfig.meteredOverride = WifiConfiguration.METERED_OVERRIDE_METERED; 4572 eapSimConfig.subscriptionId = TEST_SUBID; 4573 config.carrierMerged = true; 4574 config.meteredOverride = WifiConfiguration.METERED_OVERRIDE_METERED; 4575 config.subscriptionId = TEST_SUBID; 4576 4577 // Adding carrier merged network < EAP will fail. 4578 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 4579 config, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4580 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 4581 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4582 TEST_PACKAGE_1, TEST_FEATURE)); 4583 4584 // Adding carrier merged network is not metered will fail. 4585 eapSimConfig.meteredOverride = WifiConfiguration.METERED_OVERRIDE_NONE; 4586 networkSuggestion = createWifiNetworkSuggestion( 4587 eapSimConfig, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4588 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 4589 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4590 TEST_PACKAGE_1, TEST_FEATURE)); 4591 4592 // Adding carrier merged network without a valid SubID will fail. 4593 eapSimConfig.meteredOverride = WifiConfiguration.METERED_OVERRIDE_METERED; 4594 eapSimConfig.subscriptionId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 4595 networkSuggestion = createWifiNetworkSuggestion( 4596 eapSimConfig, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4597 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 4598 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4599 TEST_PACKAGE_1, TEST_FEATURE)); 4600 4601 // Adding carrier merged network from a non carrier privileged app will not be allowed. 4602 eapSimConfig.subscriptionId = TEST_SUBID; 4603 networkSuggestion = createWifiNetworkSuggestion( 4604 eapSimConfig, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4605 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED, 4606 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4607 TEST_PACKAGE_1, TEST_FEATURE)); 4608 4609 // Adding a carrier merged network when the carrier configuration doesn't indicate it will 4610 // provision such networks is not allowed 4611 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 4612 .thenReturn(true); 4613 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_NOT_ALLOWED, 4614 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4615 TEST_PACKAGE_1, TEST_FEATURE)); 4616 4617 when(mWifiCarrierInfoManager.areMergedCarrierWifiNetworksAllowed(TEST_SUBID)).thenReturn( 4618 true); 4619 eapSimConfig.carrierId = VALID_CARRIER_ID; 4620 networkSuggestion = createWifiNetworkSuggestion( 4621 eapSimConfig, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4622 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4623 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4624 TEST_PACKAGE_1, TEST_FEATURE)); 4625 } 4626 4627 /** 4628 * Verify when calling API from background user will fail. 4629 */ 4630 @Test testCallingFromBackgroundUserWillFailed()4631 public void testCallingFromBackgroundUserWillFailed() { 4632 WifiConfiguration wifiConfiguration = WifiConfigurationTestUtil.createOpenNetwork(); 4633 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 4634 wifiConfiguration, null, false, false, true, true, DEFAULT_PRIORITY_GROUP); 4635 4636 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4637 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4638 TEST_PACKAGE_1, TEST_FEATURE)); 4639 // When switch the user to background 4640 when(mWifiPermissionsUtil.doesUidBelongToCurrentUserOrDeviceOwner(TEST_UID_1)) 4641 .thenReturn(false); 4642 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_INTERNAL, 4643 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4644 TEST_PACKAGE_1, TEST_FEATURE)); 4645 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_INTERNAL, 4646 mWifiNetworkSuggestionsManager.remove(Arrays.asList(networkSuggestion), TEST_UID_1, 4647 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 4648 assertTrue(mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1).isEmpty()); 4649 assertFalse(mWifiNetworkSuggestionsManager.registerSuggestionConnectionStatusListener( 4650 mConnectionStatusListener, TEST_PACKAGE_1, TEST_UID_1)); 4651 4652 // When switch the user back to foreground 4653 when(mWifiPermissionsUtil.doesUidBelongToCurrentUserOrDeviceOwner(TEST_UID_1)) 4654 .thenReturn(true); 4655 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4656 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4657 TEST_PACKAGE_1, TEST_FEATURE)); 4658 assertFalse(mWifiNetworkSuggestionsManager.get(TEST_PACKAGE_1, TEST_UID_1).isEmpty()); 4659 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4660 mWifiNetworkSuggestionsManager.remove(Arrays.asList(networkSuggestion), TEST_UID_1, 4661 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 4662 assertTrue(mWifiNetworkSuggestionsManager.registerSuggestionConnectionStatusListener( 4663 mConnectionStatusListener, TEST_PACKAGE_1, TEST_UID_1)); 4664 } 4665 4666 @Test testSuggestionCarrierNetworkConnectionBroadcastAndDisconnectWithCarrierIdOnly()4667 public void testSuggestionCarrierNetworkConnectionBroadcastAndDisconnectWithCarrierIdOnly() { 4668 when(mWifiCarrierInfoManager.getBestMatchSubscriptionId(any())).thenReturn(TEST_SUBID); 4669 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 4670 .thenReturn(true); 4671 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 4672 WifiConfigurationTestUtil.createPskNetwork(), null, true, false, true, true, 4673 DEFAULT_PRIORITY_GROUP); 4674 networkSuggestion.wifiConfiguration.carrierId = TEST_CARRIER_ID; 4675 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4676 mWifiNetworkSuggestionsManager.add(Arrays.asList(networkSuggestion), TEST_UID_1, 4677 TEST_PACKAGE_1, TEST_FEATURE)); 4678 mInorder.verify(mWifiPermissionsUtil).doesUidBelongToCurrentUserOrDeviceOwner(anyInt()); 4679 assertTrue(mWifiNetworkSuggestionsManager.hasUserApprovedForApp(TEST_PACKAGE_1)); 4680 4681 // Simulate connecting to the network. 4682 WifiConfiguration connectNetwork = 4683 new WifiConfiguration(networkSuggestion.wifiConfiguration); 4684 connectNetwork.fromWifiNetworkSuggestion = true; 4685 connectNetwork.shared = false; 4686 connectNetwork.ephemeral = true; 4687 connectNetwork.creatorName = TEST_PACKAGE_1; 4688 connectNetwork.creatorUid = TEST_UID_1; 4689 connectNetwork.subscriptionId = TEST_SUBID; 4690 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 4691 WifiMetrics.ConnectionEvent.FAILURE_NONE, connectNetwork, TEST_BSSID); 4692 4693 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectSuccess(); 4694 4695 // Verify that the correct broadcast was sent out. 4696 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults(eq(TEST_PACKAGE_1), 4697 eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 4698 validatePostConnectionBroadcastSent(TEST_PACKAGE_1, networkSuggestion); 4699 4700 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4701 mWifiNetworkSuggestionsManager.remove(Arrays.asList(networkSuggestion), TEST_UID_1, 4702 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 4703 verify(mWifiConfigManager).removeSuggestionConfiguredNetwork( 4704 argThat(new WifiConfigMatcher(connectNetwork))); 4705 mInorder.verify(mWifiPermissionsUtil).doesUidBelongToCurrentUserOrDeviceOwner(anyInt()); 4706 4707 // Verify no more broadcast were sent out. 4708 mInorder.verifyNoMoreInteractions(); 4709 } 4710 4711 @Test testTreatAppAsCarrierProvider()4712 public void testTreatAppAsCarrierProvider() { 4713 assertFalse(mWifiNetworkSuggestionsManager 4714 .isAppWorkingAsCrossCarrierProvider(TEST_APP_NAME_1)); 4715 mWifiNetworkSuggestionsManager.setAppWorkingAsCrossCarrierProvider(TEST_APP_NAME_1, true); 4716 assertTrue(mWifiNetworkSuggestionsManager 4717 .isAppWorkingAsCrossCarrierProvider(TEST_APP_NAME_1)); 4718 mWifiNetworkSuggestionsManager.setAppWorkingAsCrossCarrierProvider(TEST_APP_NAME_1, false); 4719 assertFalse(mWifiNetworkSuggestionsManager 4720 .isAppWorkingAsCrossCarrierProvider(TEST_APP_NAME_1)); 4721 } 4722 4723 @Test testSetAnonymousIdentityOnSuggestionNetwork()4724 public void testSetAnonymousIdentityOnSuggestionNetwork() { 4725 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 4726 .thenReturn(true); 4727 WifiConfiguration eapSimConfig = WifiConfigurationTestUtil.createWpa2Wpa3EnterpriseNetwork( 4728 WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); 4729 eapSimConfig.enterpriseConfig.setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY_1); 4730 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 4731 new WifiConfiguration(eapSimConfig), null, false, false, true, true, 4732 DEFAULT_PRIORITY_GROUP); 4733 List<WifiNetworkSuggestion> networkSuggestionList = 4734 List.of(networkSuggestion); 4735 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4736 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 4737 TEST_PACKAGE_1, TEST_FEATURE)); 4738 WifiConfiguration configuration = 4739 new WifiConfiguration(eapSimConfig); 4740 configuration.fromWifiNetworkSuggestion = true; 4741 configuration.shared = false; 4742 configuration.ephemeral = true; 4743 configuration.creatorName = TEST_PACKAGE_1; 4744 configuration.creatorUid = TEST_UID_1; 4745 configuration.enterpriseConfig.setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY_1); 4746 4747 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = mWifiNetworkSuggestionsManager 4748 .getNetworkSuggestionsForWifiConfiguration(configuration, 4749 TEST_BSSID); 4750 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4751 assertNull(ewns.anonymousIdentity); 4752 assertEquals(TEST_ANONYMOUS_IDENTITY_1, 4753 ewns.createInternalWifiConfiguration(mWifiCarrierInfoManager) 4754 .enterpriseConfig.getAnonymousIdentity()); 4755 } 4756 4757 configuration.enterpriseConfig.setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY_2); 4758 mWifiNetworkSuggestionsManager.setAnonymousIdentity(configuration); 4759 4760 matchedSuggestions = mWifiNetworkSuggestionsManager 4761 .getNetworkSuggestionsForWifiConfiguration(configuration, 4762 TEST_BSSID); 4763 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4764 assertEquals(TEST_ANONYMOUS_IDENTITY_2, ewns.anonymousIdentity); 4765 } 4766 4767 // Reset SIM network suggestion, Anonymous Identity should gone. 4768 mWifiNetworkSuggestionsManager.resetSimNetworkSuggestions(); 4769 matchedSuggestions = mWifiNetworkSuggestionsManager 4770 .getNetworkSuggestionsForWifiConfiguration(configuration, 4771 TEST_BSSID); 4772 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4773 assertEquals(null, ewns.anonymousIdentity); 4774 assertEquals(TEST_ANONYMOUS_IDENTITY_1, 4775 ewns.createInternalWifiConfiguration(mWifiCarrierInfoManager) 4776 .enterpriseConfig.getAnonymousIdentity()); 4777 } 4778 verify(mWifiConfigManager, times(3)).saveToStore(); 4779 } 4780 4781 @Test testSetAnonymousIdentityNullOnSuggestionNetwork()4782 public void testSetAnonymousIdentityNullOnSuggestionNetwork() { 4783 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 4784 .thenReturn(true); 4785 WifiConfiguration eapSimConfig = WifiConfigurationTestUtil.createWpa2Wpa3EnterpriseNetwork( 4786 WifiEnterpriseConfig.Eap.SIM, WifiEnterpriseConfig.Phase2.NONE); 4787 eapSimConfig.enterpriseConfig.setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY_1); 4788 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 4789 new WifiConfiguration(eapSimConfig), null, false, false, true, true, 4790 DEFAULT_PRIORITY_GROUP); 4791 List<WifiNetworkSuggestion> networkSuggestionList = List.of(networkSuggestion); 4792 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4793 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 4794 TEST_PACKAGE_1, TEST_FEATURE)); 4795 WifiConfiguration configuration = 4796 new WifiConfiguration(eapSimConfig); 4797 configuration.fromWifiNetworkSuggestion = true; 4798 configuration.shared = false; 4799 configuration.ephemeral = true; 4800 configuration.creatorName = TEST_PACKAGE_1; 4801 configuration.creatorUid = TEST_UID_1; 4802 configuration.enterpriseConfig.setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY_1); 4803 4804 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = mWifiNetworkSuggestionsManager 4805 .getNetworkSuggestionsForWifiConfiguration(configuration, 4806 TEST_BSSID); 4807 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4808 assertNull(ewns.anonymousIdentity); 4809 assertEquals(TEST_ANONYMOUS_IDENTITY_1, 4810 ewns.createInternalWifiConfiguration(mWifiCarrierInfoManager) 4811 .enterpriseConfig.getAnonymousIdentity()); 4812 } 4813 4814 configuration.enterpriseConfig.setAnonymousIdentity(TEST_ANONYMOUS_IDENTITY_2); 4815 mWifiNetworkSuggestionsManager.setAnonymousIdentity(configuration); 4816 4817 matchedSuggestions = mWifiNetworkSuggestionsManager 4818 .getNetworkSuggestionsForWifiConfiguration(configuration, 4819 TEST_BSSID); 4820 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4821 assertEquals(TEST_ANONYMOUS_IDENTITY_2, ewns.anonymousIdentity); 4822 } 4823 4824 when(mWifiConfigManager.getConfiguredNetwork(anyString())).thenReturn(configuration); 4825 when(mWifiConfigManager.addOrUpdateNetwork(any(), eq(TEST_UID_1), eq(TEST_PACKAGE_1), 4826 eq(false))).thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)); 4827 configuration.enterpriseConfig.setAnonymousIdentity(null); 4828 mWifiNetworkSuggestionsManager.setAnonymousIdentity(configuration); 4829 matchedSuggestions = mWifiNetworkSuggestionsManager 4830 .getNetworkSuggestionsForWifiConfiguration(configuration, 4831 TEST_BSSID); 4832 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4833 assertTrue(TextUtils.isEmpty(ewns.anonymousIdentity)); 4834 assertEquals(TEST_ANONYMOUS_IDENTITY_1, 4835 ewns.createInternalWifiConfiguration(mWifiCarrierInfoManager) 4836 .enterpriseConfig.getAnonymousIdentity()); 4837 } 4838 verify(mWifiConfigManager).addOrUpdateNetwork(any(), eq(TEST_UID_1), eq(TEST_PACKAGE_1), 4839 eq(false)); 4840 verify(mWifiConfigManager, times(3)).saveToStore(); 4841 } 4842 4843 @Test testSetUserConnectChoice()4844 public void testSetUserConnectChoice() { 4845 WifiConfigManager.OnNetworkUpdateListener listener = mNetworkListenerCaptor.getValue(); 4846 WifiConfiguration config = WifiConfigurationTestUtil.createOpenOweNetwork(); 4847 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 4848 new WifiConfiguration(config), null, false, false, true, true, 4849 DEFAULT_PRIORITY_GROUP); 4850 List<WifiNetworkSuggestion> networkSuggestionList = 4851 List.of(networkSuggestion); 4852 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 4853 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 4854 TEST_PACKAGE_1, TEST_FEATURE)); 4855 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 4856 WifiConfiguration configuration = 4857 new WifiConfiguration(config); 4858 configuration.fromWifiNetworkSuggestion = true; 4859 configuration.shared = false; 4860 configuration.ephemeral = true; 4861 configuration.creatorName = TEST_PACKAGE_1; 4862 configuration.creatorUid = TEST_UID_1; 4863 4864 listener.onConnectChoiceSet(List.of(configuration), 4865 USER_CONNECT_CHOICE, TEST_RSSI); 4866 Set<ExtendedWifiNetworkSuggestion> matchedSuggestions = mWifiNetworkSuggestionsManager 4867 .getNetworkSuggestionsForWifiConfiguration(configuration, 4868 TEST_BSSID); 4869 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4870 assertEquals(USER_CONNECT_CHOICE, ewns.connectChoice); 4871 assertEquals(TEST_RSSI, ewns.connectChoiceRssi); 4872 } 4873 4874 listener.onConnectChoiceRemoved(USER_CONNECT_CHOICE); 4875 matchedSuggestions = mWifiNetworkSuggestionsManager 4876 .getNetworkSuggestionsForWifiConfiguration(configuration, 4877 TEST_BSSID); 4878 for (ExtendedWifiNetworkSuggestion ewns : matchedSuggestions) { 4879 assertEquals(null, ewns.connectChoice); 4880 assertEquals(0, ewns.connectChoiceRssi); 4881 } 4882 4883 // Add suggestion and change user approval have 2, set and remove user choice have 2. 4884 verify(mWifiConfigManager, times(4)).saveToStore(); 4885 4886 reset(mWifiConfigManager); 4887 listener.onConnectChoiceRemoved(USER_CONNECT_CHOICE); 4888 listener.onConnectChoiceRemoved(null); 4889 verify(mWifiConfigManager, never()).saveToStore(); 4890 } 4891 4892 /** 4893 * Helper function for creating a test configuration with user credential. 4894 * 4895 * @return {@link PasspointConfiguration} 4896 */ createTestConfigWithUserCredential(String fqdn, String friendlyName)4897 private PasspointConfiguration createTestConfigWithUserCredential(String fqdn, 4898 String friendlyName) { 4899 PasspointConfiguration config = new PasspointConfiguration(); 4900 HomeSp homeSp = new HomeSp(); 4901 homeSp.setFqdn(fqdn); 4902 homeSp.setFriendlyName(friendlyName); 4903 config.setHomeSp(homeSp); 4904 Credential credential = new Credential(); 4905 credential.setRealm(TEST_REALM); 4906 credential.setCaCertificate(FakeKeys.CA_CERT0); 4907 Credential.UserCredential userCredential = new Credential.UserCredential(); 4908 userCredential.setUsername("username"); 4909 userCredential.setPassword("password"); 4910 userCredential.setEapType(EAPConstants.EAP_TTLS); 4911 userCredential.setNonEapInnerMethod(Credential.UserCredential.AUTH_METHOD_MSCHAP); 4912 credential.setUserCredential(userCredential); 4913 config.setCredential(credential); 4914 return config; 4915 } 4916 4917 /** 4918 * Helper function for creating a test configuration with SIM credential. 4919 * 4920 * @return {@link PasspointConfiguration} 4921 */ createTestConfigWithSimCredential(String fqdn, String imsi, String realm)4922 private PasspointConfiguration createTestConfigWithSimCredential(String fqdn, String imsi, 4923 String realm) { 4924 PasspointConfiguration config = new PasspointConfiguration(); 4925 HomeSp homeSp = new HomeSp(); 4926 homeSp.setFqdn(fqdn); 4927 homeSp.setFriendlyName(TEST_FRIENDLY_NAME); 4928 config.setHomeSp(homeSp); 4929 Credential credential = new Credential(); 4930 credential.setRealm(realm); 4931 Credential.SimCredential simCredential = new Credential.SimCredential(); 4932 simCredential.setImsi(imsi); 4933 simCredential.setEapType(EAPConstants.EAP_SIM); 4934 credential.setSimCredential(simCredential); 4935 config.setCredential(credential); 4936 return config; 4937 } 4938 createPlaceholderConfigForPasspoint(String fqdn, String uniqueId)4939 private WifiConfiguration createPlaceholderConfigForPasspoint(String fqdn, 4940 String uniqueId) { 4941 WifiConfiguration config = new WifiConfiguration(); 4942 config.FQDN = fqdn; 4943 config.setPasspointUniqueId(uniqueId); 4944 return config; 4945 } 4946 4947 @Test testResetNotification()4948 public void testResetNotification() { 4949 mWifiNetworkSuggestionsManager.resetNotification(); 4950 verify(mWifiNotificationManager).cancel(SystemMessage.NOTE_NETWORK_SUGGESTION_AVAILABLE); 4951 } 4952 4953 /** 4954 * Verify we return the merged network suggestion matches the target FQDN when merged network is 4955 * allowed . 4956 */ 4957 @Test testGetMergedPasspointSuggestionFromFqdn()4958 public void testGetMergedPasspointSuggestionFromFqdn() { 4959 assumeTrue(SdkLevel.isAtLeastS()); 4960 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 4961 .thenReturn(true); 4962 when(mWifiCarrierInfoManager.areMergedCarrierWifiNetworksAllowed(TEST_SUBID)) 4963 .thenReturn(true); 4964 PasspointConfiguration passpointConfiguration = 4965 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 4966 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 4967 passpointConfiguration.getUniqueId()); 4968 placeholderConfig.carrierMerged = true; 4969 placeholderConfig.meteredOverride = WifiConfiguration.METERED_OVERRIDE_METERED; 4970 placeholderConfig.FQDN = TEST_FQDN; 4971 placeholderConfig.subscriptionId = TEST_SUBID; 4972 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion(placeholderConfig, 4973 passpointConfiguration, true, false, true, true, DEFAULT_PRIORITY_GROUP); 4974 4975 when(mPasspointManager.addOrUpdateProvider(any(PasspointConfiguration.class), 4976 anyInt(), anyString(), eq(true), eq(true), eq(false))).thenReturn(true); 4977 assertEquals(mWifiNetworkSuggestionsManager.add(List.of(networkSuggestion), TEST_UID_1, 4978 TEST_PACKAGE_1, TEST_FEATURE), WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS); 4979 4980 Set<ExtendedWifiNetworkSuggestion> ewns = 4981 mWifiNetworkSuggestionsManager.getNetworkSuggestionsForFqdn(TEST_FQDN); 4982 assertEquals(1, ewns.size()); 4983 assertEquals(networkSuggestion, ewns.iterator().next().wns); 4984 4985 // Change to disallow merged network, no matching suggestion should return. 4986 when(mWifiCarrierInfoManager.areMergedCarrierWifiNetworksAllowed(TEST_SUBID)) 4987 .thenReturn(false); 4988 ewns = mWifiNetworkSuggestionsManager.getNetworkSuggestionsForFqdn(TEST_FQDN); 4989 assertEquals(0, ewns.size()); 4990 } 4991 4992 /** 4993 * Verify we return the merged network suggestion matches the target ScanDetail when merged 4994 * network is allowed . 4995 */ 4996 @Test testGetMergedNetworkSuggestionsForScanDetail()4997 public void testGetMergedNetworkSuggestionsForScanDetail() { 4998 assumeTrue(SdkLevel.isAtLeastS()); 4999 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 5000 .thenReturn(true); 5001 when(mWifiCarrierInfoManager.areMergedCarrierWifiNetworksAllowed(TEST_SUBID)) 5002 .thenReturn(true); 5003 WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork(); 5004 ScanDetail scanDetail = createScanDetailForNetwork(config); 5005 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 5006 config, null, false, false, true, true, 5007 DEFAULT_PRIORITY_GROUP); 5008 config.subscriptionId = TEST_SUBID; 5009 config.carrierMerged = true; 5010 config.meteredOverride = WifiConfiguration.METERED_OVERRIDE_METERED; 5011 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 5012 mWifiNetworkSuggestionsManager.add(List.of(networkSuggestion), TEST_UID_1, 5013 TEST_PACKAGE_1, TEST_FEATURE)); 5014 5015 Set<ExtendedWifiNetworkSuggestion> ewns = mWifiNetworkSuggestionsManager 5016 .getNetworkSuggestionsForScanDetail(scanDetail); 5017 assertEquals(1, ewns.size()); 5018 5019 // Change to disallow merged network, no matching suggestion should return. 5020 when(mWifiCarrierInfoManager.areMergedCarrierWifiNetworksAllowed(TEST_SUBID)) 5021 .thenReturn(false); 5022 ewns = mWifiNetworkSuggestionsManager 5023 .getNetworkSuggestionsForScanDetail(scanDetail); 5024 assertEquals(0, ewns.size()); 5025 } 5026 5027 /** 5028 * Verify we return the merged network suggestion matches the target ScanDetail when merged 5029 * network is allowed . 5030 */ 5031 @Test testGetMergedNetworkSuggestionsWithSubscriptionGroupForScanDetail()5032 public void testGetMergedNetworkSuggestionsWithSubscriptionGroupForScanDetail() { 5033 assumeTrue(SdkLevel.isAtLeastT()); 5034 when(mWifiPermissionsUtil.checkNetworkCarrierProvisioningPermission(TEST_UID_1)) 5035 .thenReturn(true); 5036 when(mWifiCarrierInfoManager.areMergedCarrierWifiNetworksAllowed(TEST_SUBID)) 5037 .thenReturn(true); 5038 when(mWifiCarrierInfoManager.getBestMatchSubscriptionId(any())) 5039 .thenReturn(TEST_SUBID); 5040 WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork(); 5041 ScanDetail scanDetail = createScanDetailForNetwork(config); 5042 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 5043 config, null, false, false, true, true, 5044 DEFAULT_PRIORITY_GROUP); 5045 config.setSubscriptionGroup(GROUP_UUID); 5046 config.carrierMerged = true; 5047 config.meteredOverride = WifiConfiguration.METERED_OVERRIDE_METERED; 5048 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 5049 mWifiNetworkSuggestionsManager.add(List.of(networkSuggestion), TEST_UID_1, 5050 TEST_PACKAGE_1, TEST_FEATURE)); 5051 5052 Set<ExtendedWifiNetworkSuggestion> ewns = mWifiNetworkSuggestionsManager 5053 .getNetworkSuggestionsForScanDetail(scanDetail); 5054 assertEquals(1, ewns.size()); 5055 assertEquals(TEST_SUBID, ewns.iterator().next() 5056 .createInternalWifiConfiguration(mWifiCarrierInfoManager).subscriptionId); 5057 5058 // Change to disallow merged network, no matching suggestion should return. 5059 when(mWifiCarrierInfoManager.areMergedCarrierWifiNetworksAllowed(TEST_SUBID)) 5060 .thenReturn(false); 5061 ewns = mWifiNetworkSuggestionsManager 5062 .getNetworkSuggestionsForScanDetail(scanDetail); 5063 assertEquals(0, ewns.size()); 5064 } 5065 5066 @Test testAddNonRandomizedNetworkSuggestionForcedToRandomized()5067 public void testAddNonRandomizedNetworkSuggestionForcedToRandomized() { 5068 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 5069 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 5070 DEFAULT_PRIORITY_GROUP); 5071 networkSuggestion.wifiConfiguration.macRandomizationSetting = 5072 WifiConfiguration.RANDOMIZATION_NONE; 5073 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 5074 mWifiNetworkSuggestionsManager.add(List.of(networkSuggestion), 5075 TEST_UID_1, TEST_PACKAGE_1, TEST_FEATURE)); 5076 assertEquals(WifiConfiguration.RANDOMIZATION_PERSISTENT, 5077 networkSuggestion.wifiConfiguration.macRandomizationSetting); 5078 } 5079 5080 @Test testAddNonRandomizedPasspointSuggestionForcedToRandomized()5081 public void testAddNonRandomizedPasspointSuggestionForcedToRandomized() { 5082 PasspointConfiguration passpointConfiguration = 5083 createTestConfigWithUserCredential(TEST_FQDN, TEST_FRIENDLY_NAME); 5084 WifiConfiguration placeholderConfig = createPlaceholderConfigForPasspoint(TEST_FQDN, 5085 passpointConfiguration.getUniqueId()); 5086 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 5087 placeholderConfig, passpointConfiguration, false, false, true, true, 5088 DEFAULT_PRIORITY_GROUP); 5089 networkSuggestion.passpointConfiguration.setMacRandomizationEnabled(false); 5090 networkSuggestion.passpointConfiguration.setNonPersistentMacRandomizationEnabled(false); 5091 List<WifiNetworkSuggestion> networkSuggestionList = 5092 List.of(networkSuggestion); 5093 when(mPasspointManager.addOrUpdateProvider(any(), anyInt(), anyString(), anyBoolean(), 5094 anyBoolean(), eq(false))).thenReturn(true); 5095 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 5096 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 5097 TEST_PACKAGE_1, TEST_FEATURE)); 5098 5099 ArgumentCaptor<PasspointConfiguration> passpointConfigCaptor = 5100 ArgumentCaptor.forClass(PasspointConfiguration.class); 5101 verify(mPasspointManager).addOrUpdateProvider(passpointConfigCaptor.capture(), 5102 anyInt(), anyString(), anyBoolean(), anyBoolean(), eq(false)); 5103 assertTrue(passpointConfigCaptor.getValue().isMacRandomizationEnabled()); 5104 assertFalse(passpointConfigCaptor.getValue().isNonPersistentMacRandomizationEnabled()); 5105 } 5106 5107 @Test testIncompleteEnterpriseNetworkSuggestion()5108 public void testIncompleteEnterpriseNetworkSuggestion() { 5109 WifiConfiguration config = new WifiConfiguration(); 5110 config.SSID = "\"someNetwork\""; 5111 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP); 5112 // EAP method is kept as Eap.NONE - should not crash, but return invalid 5113 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 5114 config, null, false, false, true, true, 5115 DEFAULT_PRIORITY_GROUP); 5116 5117 List<WifiNetworkSuggestion> networkSuggestionList = 5118 List.of(networkSuggestion1); 5119 when(mWifiKeyStore.updateNetworkKeys(eq(networkSuggestion1.wifiConfiguration), any())) 5120 .thenReturn(true); 5121 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 5122 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 5123 TEST_PACKAGE_1, TEST_FEATURE)); 5124 } 5125 5126 /** 5127 * Verify successful removal of network suggestions with linger action when suggestion is 5128 * currently connected. Should trigger linger and delay remove WifiConfiguration. 5129 */ 5130 @Test testRemoveNetworkSuggestionsWithActionCurrentlyConnected()5131 public void testRemoveNetworkSuggestionsWithActionCurrentlyConnected() { 5132 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 5133 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 5134 DEFAULT_PRIORITY_GROUP); 5135 5136 List<WifiNetworkSuggestion> networkSuggestionList1 = 5137 List.of(networkSuggestion1); 5138 5139 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 5140 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 5141 TEST_PACKAGE_1, TEST_FEATURE)); 5142 5143 // Now remove suggestion with lingering which is currently connected. 5144 when(mClock.getElapsedSinceBootMillis()).thenReturn(0L); 5145 when(mPrimaryClientModeManager.getConnectedWifiConfiguration()) 5146 .thenReturn(networkSuggestion1.wifiConfiguration); 5147 when(mWifiConfigManager.getConfiguredNetwork(any())) 5148 .thenReturn(networkSuggestion1.wifiConfiguration); 5149 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 5150 mWifiNetworkSuggestionsManager.remove(List.of(networkSuggestion1), 5151 TEST_UID_1, TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_LINGER)); 5152 verify(mWifiScoreCard).removeNetwork(anyString()); 5153 verify(mLruConnectionTracker).removeNetwork(any()); 5154 verify(mPrimaryClientModeManager).setShouldReduceNetworkScore(true); 5155 verify(mWifiConfigManager, never()).removeSuggestionConfiguredNetwork(any()); 5156 when(mClock.getElapsedSinceBootMillis()).thenReturn((long) DEFAULT_LINGER_DELAY_MS + 1); 5157 mLooper.moveTimeForward(DEFAULT_LINGER_DELAY_MS + 1); 5158 mLooper.dispatchAll(); 5159 verify(mWifiConfigManager).removeSuggestionConfiguredNetwork( 5160 argThat(new WifiConfigMatcher(networkSuggestion1.wifiConfiguration))); 5161 5162 verify(mWifiMetrics, times(2)).incrementNetworkSuggestionApiNumModification(); 5163 verify(mWifiMetrics, times(1)).addNetworkSuggestionPriorityGroup(anyInt()); 5164 ArgumentCaptor<List<Integer>> maxSizesCaptor = ArgumentCaptor.forClass(List.class); 5165 verify(mWifiMetrics, times(2)).noteNetworkSuggestionApiListSizeHistogram( 5166 maxSizesCaptor.capture()); 5167 verify(mWifiConfigManager).removeConnectChoiceFromAllNetworks(anyString()); 5168 assertNotNull(maxSizesCaptor.getValue()); 5169 assertEquals(maxSizesCaptor.getValue(), List.of(1)); 5170 } 5171 5172 /** 5173 * Verify successful removal of network suggestions with linger action when suggestion is not 5174 * currently connected. Should remove WifiConfiguration immeditaly. 5175 */ 5176 @Test testRemoveNetworkSuggestionsWithActionNotConnected()5177 public void testRemoveNetworkSuggestionsWithActionNotConnected() { 5178 WifiNetworkSuggestion networkSuggestion1 = createWifiNetworkSuggestion( 5179 WifiConfigurationTestUtil.createOpenNetwork(), null, false, false, true, true, 5180 DEFAULT_PRIORITY_GROUP); 5181 5182 List<WifiNetworkSuggestion> networkSuggestionList1 = 5183 List.of(networkSuggestion1); 5184 5185 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 5186 mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_UID_1, 5187 TEST_PACKAGE_1, TEST_FEATURE)); 5188 5189 // Now remove suggestion with lingering which is currently connected. 5190 when(mWifiConfigManager.getConfiguredNetwork(any())) 5191 .thenReturn(networkSuggestion1.wifiConfiguration); 5192 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 5193 mWifiNetworkSuggestionsManager.remove(List.of(networkSuggestion1), 5194 TEST_UID_1, TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_LINGER)); 5195 verify(mWifiScoreCard).removeNetwork(anyString()); 5196 verify(mLruConnectionTracker).removeNetwork(any()); 5197 verify(mPrimaryClientModeManager, never()).setShouldReduceNetworkScore(true); 5198 verify(mWifiConfigManager).removeSuggestionConfiguredNetwork( 5199 argThat(new WifiConfigMatcher(networkSuggestion1.wifiConfiguration))); 5200 5201 verify(mWifiMetrics, times(2)).incrementNetworkSuggestionApiNumModification(); 5202 verify(mWifiMetrics, times(1)).addNetworkSuggestionPriorityGroup(anyInt()); 5203 ArgumentCaptor<List<Integer>> maxSizesCaptor = ArgumentCaptor.forClass(List.class); 5204 verify(mWifiMetrics, times(2)).noteNetworkSuggestionApiListSizeHistogram( 5205 maxSizesCaptor.capture()); 5206 verify(mWifiConfigManager).removeConnectChoiceFromAllNetworks(anyString()); 5207 assertNotNull(maxSizesCaptor.getValue()); 5208 assertEquals(maxSizesCaptor.getValue(), List.of(1)); 5209 } 5210 createWifiNetworkSuggestion(WifiConfiguration config, PasspointConfiguration passpointConfiguration, boolean isAppInteractionRequired, boolean isUserInteractionRequired, boolean isUserAllowedToManuallyConnect, boolean isInitialAutoJoinEnabled, int priorityGroup)5211 private static WifiNetworkSuggestion createWifiNetworkSuggestion(WifiConfiguration config, 5212 PasspointConfiguration passpointConfiguration, 5213 boolean isAppInteractionRequired, 5214 boolean isUserInteractionRequired, 5215 boolean isUserAllowedToManuallyConnect, 5216 boolean isInitialAutoJoinEnabled, int priorityGroup) { 5217 config.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_PERSISTENT; 5218 return new WifiNetworkSuggestion(config, passpointConfiguration, isAppInteractionRequired, 5219 isUserInteractionRequired, isUserAllowedToManuallyConnect, isInitialAutoJoinEnabled, 5220 priorityGroup); 5221 } 5222 5223 @Test testSuggestionWithBothSubscriptionIdAndSubscriptionGroupSetIsInvalid()5224 public void testSuggestionWithBothSubscriptionIdAndSubscriptionGroupSetIsInvalid() { 5225 assumeTrue(SdkLevel.isAtLeastT()); 5226 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 5227 config.setSubscriptionGroup(GROUP_UUID); 5228 config.subscriptionId = TEST_SUBID; 5229 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 5230 config, null, false, false, true, true, 5231 DEFAULT_PRIORITY_GROUP); 5232 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_INVALID, 5233 mWifiNetworkSuggestionsManager.add(List.of(networkSuggestion), TEST_UID_1, 5234 TEST_PACKAGE_1, TEST_FEATURE)); 5235 } 5236 5237 @Test testOnNetworkConnectionSuccessWithMatchAndNetworkTransitionDisable()5238 public void testOnNetworkConnectionSuccessWithMatchAndNetworkTransitionDisable() 5239 throws Exception { 5240 assertTrue(mWifiNetworkSuggestionsManager.registerSuggestionConnectionStatusListener( 5241 mConnectionStatusListener, TEST_PACKAGE_1, TEST_UID_1)); 5242 WifiNetworkSuggestion networkSuggestion = createWifiNetworkSuggestion( 5243 WifiConfigurationTestUtil.createPskNetwork(), null, true, false, true, true, 5244 DEFAULT_PRIORITY_GROUP); 5245 List<WifiNetworkSuggestion> networkSuggestionList = 5246 List.of(networkSuggestion); 5247 5248 // Verify no crash with unmatched update 5249 mNetworkListenerCaptor.getValue().onSecurityParamsUpdate(networkSuggestion 5250 .wifiConfiguration, networkSuggestion.wifiConfiguration.getSecurityParamsList()); 5251 mNetworkListenerCaptor.getValue().onSecurityParamsUpdate(null, Collections.EMPTY_LIST); 5252 5253 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS, 5254 mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_UID_1, 5255 TEST_PACKAGE_1, TEST_FEATURE)); 5256 mWifiNetworkSuggestionsManager.setHasUserApprovedForApp(true, TEST_UID_1, TEST_PACKAGE_1); 5257 5258 // Simulate connecting to the network. 5259 WifiConfiguration connectNetwork = 5260 new WifiConfiguration(networkSuggestion.wifiConfiguration); 5261 connectNetwork.fromWifiNetworkSuggestion = true; 5262 connectNetwork.shared = false; 5263 connectNetwork.ephemeral = true; 5264 connectNetwork.creatorName = TEST_PACKAGE_1; 5265 connectNetwork.creatorUid = TEST_UID_1; 5266 WifiConfiguration updated = new WifiConfiguration(connectNetwork); 5267 updated.setSecurityParamsEnabled(WifiConfiguration.SECURITY_TYPE_PSK, false); 5268 5269 // Update to handle Network Transition Disable 5270 mNetworkListenerCaptor.getValue().onSecurityParamsUpdate(connectNetwork, 5271 updated.getSecurityParamsList()); 5272 mWifiNetworkSuggestionsManager.handleConnectionAttemptEnded( 5273 WifiMetrics.ConnectionEvent.FAILURE_NONE, updated, TEST_BSSID); 5274 5275 verify(mWifiMetrics).incrementNetworkSuggestionApiNumConnectSuccess(); 5276 5277 // Verify that the correct broadcast was sent out. 5278 mInorder.verify(mWifiPermissionsUtil).enforceCanAccessScanResults(eq(TEST_PACKAGE_1), 5279 eq(TEST_FEATURE), eq(TEST_UID_1), nullable(String.class)); 5280 validatePostConnectionBroadcastSent(TEST_PACKAGE_1, networkSuggestion); 5281 5282 // Verify no more broadcast were sent out. 5283 mInorder.verifyNoMoreInteractions(); 5284 } 5285 5286 @Test testHandleShutDown()5287 public void testHandleShutDown() { 5288 mWifiNetworkSuggestionsManager.handleShutDown(); 5289 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_INTERNAL, 5290 mWifiNetworkSuggestionsManager.add(Collections.emptyList(), TEST_UID_1, 5291 TEST_PACKAGE_1, TEST_FEATURE)); 5292 assertEquals(WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_INTERNAL, 5293 mWifiNetworkSuggestionsManager.remove(Collections.emptyList(), TEST_UID_1, 5294 TEST_PACKAGE_1, ACTION_REMOVE_SUGGESTION_DISCONNECT)); 5295 } 5296 } 5297