diff options
-rw-r--r-- | service/java/com/android/server/wifi/WifiConfigManager.java | 10 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java | 61 |
2 files changed, 70 insertions, 1 deletions
diff --git a/service/java/com/android/server/wifi/WifiConfigManager.java b/service/java/com/android/server/wifi/WifiConfigManager.java index 6d04b7a68..5b5c968c1 100644 --- a/service/java/com/android/server/wifi/WifiConfigManager.java +++ b/service/java/com/android/server/wifi/WifiConfigManager.java @@ -2927,14 +2927,22 @@ public class WifiConfigManager { Set<Integer> removedNetworkIds = new HashSet<>(); // Remove any private networks of the old user before switching the userId. for (WifiConfiguration config : getConfiguredNetworks()) { - if (!config.shared && doesUidBelongToCurrentUser(config.creatorUid)) { + if ((!config.shared && doesUidBelongToCurrentUser(config.creatorUid)) + || config.ephemeral) { removedNetworkIds.add(config.networkId); localLog("clearInternalUserData: removed config." + " netId=" + config.networkId + " configKey=" + config.getKey()); mConfiguredNetworks.remove(config.networkId); + for (OnNetworkUpdateListener listener : mListeners) { + listener.onNetworkRemoved( + createExternalWifiConfiguration(config, true, Process.WIFI_UID)); + } } } + if (!removedNetworkIds.isEmpty()) { + sendConfiguredNetworkChangedBroadcast(WifiManager.CHANGE_REASON_REMOVED); + } mUserTemporarilyDisabledList.clear(); mScanDetailCaches.clear(); clearLastSelectedNetwork(); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index d2584adbc..3de8cb98b 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -2915,6 +2915,7 @@ public class WifiConfigManagerTest extends WifiBaseTest { Set<Integer> removedNetworks = mWifiConfigManager.handleUserSwitch(user2); verify(mWifiConfigStore).switchUserStoresAndRead(any(List.class)); assertTrue((removedNetworks.size() == 1) && (removedNetworks.contains(user1NetworkId))); + verify(mWcmListener).onNetworkRemoved(any()); // Set the expected networks to be |sharedNetwork| and |user2Network|. List<WifiConfiguration> expectedNetworks = new ArrayList<WifiConfiguration>() { @@ -2933,6 +2934,66 @@ public class WifiConfigManagerTest extends WifiBaseTest { assertTrue(removedNetworks.isEmpty()); } + @Test + public void testHandleUserSwitchRemovesOldUserEphemeralNetworks() throws Exception { + int user1 = TEST_DEFAULT_USER; + int user2 = TEST_DEFAULT_USER + 1; + setupUserProfiles(user2); + + int appId = 674; + + // Create 2 networks. 1 ephemeral network for user1 and 1 shared. + final WifiConfiguration sharedNetwork = WifiConfigurationTestUtil.createPskNetwork(); + + // Set up the store data that is loaded initially. + List<WifiConfiguration> sharedNetworks = new ArrayList<WifiConfiguration>() { + { + add(sharedNetwork); + } + }; + setupStoreDataForRead(sharedNetworks, Collections.EMPTY_LIST); + assertTrue(mWifiConfigManager.loadFromStore()); + verify(mWifiConfigStore).read(); + + WifiConfiguration ephemeralNetwork = WifiConfigurationTestUtil.createEphemeralNetwork(); + verifyAddEphemeralNetworkToWifiConfigManager(ephemeralNetwork); + + // Fetch the network ID assigned to the user 1 network initially. + int ephemeralNetworkId = WifiConfiguration.INVALID_NETWORK_ID; + List<WifiConfiguration> retrievedNetworks = + mWifiConfigManager.getConfiguredNetworksWithPasswords(); + for (WifiConfiguration network : retrievedNetworks) { + if (network.getKey().equals(ephemeralNetwork.getKey())) { + ephemeralNetworkId = network.networkId; + } + } + + // Now switch the user to user 2 and ensure that user 1's private network has been removed. + when(mUserManager.isUserUnlockingOrUnlocked(UserHandle.of(user2))).thenReturn(true); + Set<Integer> removedNetworks = mWifiConfigManager.handleUserSwitch(user2); + verify(mWifiConfigStore).switchUserStoresAndRead(any(List.class)); + assertTrue((removedNetworks.size() == 1)); + assertTrue(removedNetworks.contains(ephemeralNetworkId)); + verifyNetworkRemoveBroadcast(); + verify(mWcmListener).onNetworkRemoved(any()); + + + // Set the expected networks to be |sharedNetwork|. + List<WifiConfiguration> expectedNetworks = new ArrayList<WifiConfiguration>() { + { + add(sharedNetwork); + } + }; + WifiConfigurationTestUtil.assertConfigurationsEqualForConfigManagerAddOrUpdate( + expectedNetworks, mWifiConfigManager.getConfiguredNetworksWithPasswords()); + + // Send another user switch indication with the same user 2. This should be ignored and + // hence should not remove any new networks. + when(mUserManager.isUserUnlockingOrUnlocked(UserHandle.of(user2))).thenReturn(true); + removedNetworks = mWifiConfigManager.handleUserSwitch(user2); + assertTrue(removedNetworks.isEmpty()); + } + /** * Verifies the foreground user switch using {@link WifiConfigManager#handleUserSwitch(int)} * and ensures that user switch from a user with no private networks is handled. |