From d72ccc6880a1a624926b97417047d42a5aa182ef Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Wed, 27 Sep 2017 12:59:59 -0700 Subject: WifiConfigManager: Don't reset the userId on user stop Couple of fixes in user stop handling: a) Only clear the network data belonging to the user that is stopped. b) Don't reset the user Id on stop. It will be done in the user switch handling. Bug: 65939780 Test: Unit tests Change-Id: Ia3ab033d893009cfd8019ee27d52a490a8040438 --- .../com/android/server/wifi/WifiConfigManager.java | 11 +++++- .../android/server/wifi/WifiConfigManagerTest.java | 45 +++++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/service/java/com/android/server/wifi/WifiConfigManager.java b/service/java/com/android/server/wifi/WifiConfigManager.java index 75b39b261..d9abb94b2 100644 --- a/service/java/com/android/server/wifi/WifiConfigManager.java +++ b/service/java/com/android/server/wifi/WifiConfigManager.java @@ -2571,10 +2571,12 @@ public class WifiConfigManager { * @param userId The identifier of the user that stopped. */ public void handleUserStop(int userId) { + if (mVerboseLoggingEnabled) { + Log.v(TAG, "Handling user stop for " + userId); + } if (userId == mCurrentUserId && mUserManager.isUserUnlockingOrUnlocked(mCurrentUserId)) { saveToStore(true); - clearInternalData(); - mCurrentUserId = UserHandle.USER_SYSTEM; + clearInternalUserData(mCurrentUserId); } } @@ -2586,6 +2588,7 @@ public class WifiConfigManager { * - List of deleted ephemeral networks. */ private void clearInternalData() { + localLog("clearInternalData: Clearing all internal data"); mConfiguredNetworks.clear(); mDeletedEphemeralSSIDs.clear(); mScanDetailCaches.clear(); @@ -2604,12 +2607,16 @@ public class WifiConfigManager { * removed from memory. */ private Set clearInternalUserData(int userId) { + localLog("clearInternalUserData: Clearing user internal data for " + userId); Set removedNetworkIds = new HashSet<>(); // Remove any private networks of the old user before switching the userId. for (WifiConfiguration config : getInternalConfiguredNetworks()) { if (!config.shared && WifiConfigurationUtil.doesUidBelongToAnyProfile( config.creatorUid, mUserManager.getProfiles(userId))) { removedNetworkIds.add(config.networkId); + localLog("clearInternalUserData: removed config." + + " netId=" + config.networkId + + " configKey=" + config.configKey()); mConfiguredNetworks.remove(config.networkId); } } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index d14c8fc9f..57638a33d 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -2367,7 +2367,7 @@ public class WifiConfigManagerTest { } /** - * Verifies that the foreground user stop using {@link WifiConfigManager#handleUserStop(int)} + * Verifies that the user stop handling using {@link WifiConfigManager#handleUserStop(int)} * and ensures that the store is written only when the foreground user is stopped. */ @Test @@ -2389,6 +2389,49 @@ public class WifiConfigManagerTest { mContextConfigStoreMockOrder.verify(mWifiConfigStore).write(anyBoolean()); } + /** + * Verifies that the user stop handling using {@link WifiConfigManager#handleUserStop(int)} + * and ensures that the shared data is not lost when the foreground user is stopped. + */ + @Test + public void testHandleUserStopDoesNotClearSharedData() throws Exception { + int user1 = TEST_DEFAULT_USER; + + // + // Setup the database for the user before initiating stop. + // + int appId = 674; + // Create 2 networks. 1 for user1, and 1 shared. + final WifiConfiguration user1Network = WifiConfigurationTestUtil.createPskNetwork(); + user1Network.shared = false; + user1Network.creatorUid = UserHandle.getUid(user1, appId); + final WifiConfiguration sharedNetwork = WifiConfigurationTestUtil.createPskNetwork(); + + // Set up the store data that is loaded initially. + List sharedNetworks = new ArrayList() { + { + add(sharedNetwork); + } + }; + List user1Networks = new ArrayList() { + { + add(user1Network); + } + }; + setupStoreDataForRead(sharedNetworks, user1Networks, new HashSet()); + assertTrue(mWifiConfigManager.loadFromStore()); + verify(mWifiConfigStore).read(); + + // Ensure that we have 2 networks in the database before the stop. + assertEquals(2, mWifiConfigManager.getConfiguredNetworks().size()); + + mWifiConfigManager.handleUserStop(user1); + + // Ensure that we only have 1 shared network in the database after the stop. + assertEquals(1, mWifiConfigManager.getConfiguredNetworks().size()); + assertEquals(sharedNetwork.SSID, mWifiConfigManager.getConfiguredNetworks().get(0).SSID); + } + /** * Verifies the foreground user unlock via {@link WifiConfigManager#handleUserUnlock(int)} * results in a store read after bootup. -- cgit v1.2.3