Merge "WifiConfigManager: Prevent store writes before a read is triggered"
diff --git a/service/java/com/android/server/wifi/WifiConfigManager.java b/service/java/com/android/server/wifi/WifiConfigManager.java
index ebd18cd..8855d6d 100644
--- a/service/java/com/android/server/wifi/WifiConfigManager.java
+++ b/service/java/com/android/server/wifi/WifiConfigManager.java
@@ -2755,6 +2755,10 @@
      * @return Whether the write was successful or not, this is applicable only for force writes.
      */
     public boolean saveToStore(boolean forceWrite) {
+        if (mPendingStoreRead) {
+            Log.e(TAG, "Cannot save to store before store is read!");
+            return false;
+        }
         ArrayList<WifiConfiguration> sharedConfigurations = new ArrayList<>();
         ArrayList<WifiConfiguration> userConfigurations = new ArrayList<>();
         // List of network IDs for legacy Passpoint configuration to be removed.
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
index 5d6b14d..2baae11 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java
@@ -210,13 +210,29 @@
      * yet loaded data from store.
      */
     @Test
-    public void testAddNetworkBeforeLoadFromStore() {
+    public void testAddNetworkIsRejectedBeforeLoadFromStore() {
         WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork();
         assertFalse(
                 mWifiConfigManager.addOrUpdateNetwork(openNetwork, TEST_CREATOR_UID).isSuccess());
     }
 
     /**
+     * Verifies the {@link WifiConfigManager#saveToStore(boolean)} is rejected until the store has
+     * been read first using {@link WifiConfigManager#loadFromStore()}.
+     */
+    @Test
+    public void testSaveToStoreIsRejectedBeforeLoadFromStore() throws Exception {
+        assertFalse(mWifiConfigManager.saveToStore(true));
+        mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()).write(anyBoolean());
+
+        assertTrue(mWifiConfigManager.loadFromStore());
+        mContextConfigStoreMockOrder.verify(mWifiConfigStore).read();
+
+        assertTrue(mWifiConfigManager.saveToStore(true));
+        mContextConfigStoreMockOrder.verify(mWifiConfigStore).write(anyBoolean());
+    }
+
+    /**
      * Verifies the addition of a single network using
      * {@link WifiConfigManager#addOrUpdateNetwork(WifiConfiguration, int)}
      */
@@ -2370,6 +2386,7 @@
      * and {@link WifiConfigManager#handleUserUnlock(int)} and ensures that the new store is not
      * read until the user is unlocked.
      */
+    @Test
     public void testHandleUserSwitchWhenLocked() throws Exception {
         int user1 = TEST_DEFAULT_USER;
         int user2 = TEST_DEFAULT_USER + 1;
@@ -2408,6 +2425,9 @@
         int user2 = TEST_DEFAULT_USER + 1;
         setupUserProfiles(user2);
 
+        // Set up the internal data first.
+        assertTrue(mWifiConfigManager.loadFromStore());
+
         // Try stopping background user2 first, this should not do anything.
         when(mUserManager.isUserUnlockingOrUnlocked(user2)).thenReturn(false);
         mWifiConfigManager.handleUserStop(user2);