From 0b86079855b2bc120a6c7790c53c927d95485b62 Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Fri, 19 Oct 2018 08:20:30 -0700 Subject: WifiConfigStore Refactor (1/2) New WifiConfigStore Architecture: a) WifiConfigStore can now manage multiple user store files (StoreFile). It now manages a total of 3 files: 1 shared & 1 user (both existing) & 1 new one specifically for network suggestions. b) Clients(StoreData) should now register themselves to write/read from any one of the store files. c) |StoreFile|'s are only written if any one of the |StoreData|'s registered for that file indicates that it has new data to write. PS: Please refer to the bug for a summary of problems with the previous architecture. Bug: 117599800 Test: Unit tests Test: Verified upgrading device with some store data to the new architecture and ensured that we don't lose any data. Test: Will send for integration tests. Change-Id: Iebe54b6bef4d203540e7da490eae9851131d8827 --- .../android/server/wifi/WifiConfigStoreTest.java | 387 ++++++++++++++------- 1 file changed, 254 insertions(+), 133 deletions(-) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreTest.java index 1741acc4b..0240a67ab 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreTest.java @@ -27,6 +27,8 @@ import android.net.wifi.WifiConfiguration; import android.os.test.TestLooper; import android.support.test.filters.SmallTest; +import com.android.internal.util.ArrayUtils; +import com.android.server.wifi.WifiConfigStore.StoreData; import com.android.server.wifi.WifiConfigStore.StoreFile; import com.android.server.wifi.util.XmlUtil; @@ -53,13 +55,6 @@ import java.util.Set; */ @SmallTest public class WifiConfigStoreTest { - // Store file content without any data. - private static final String EMPTY_FILE_CONTENT = - "\n" - + "\n" - + "\n" - + "\n"; - private static final String TEST_USER_DATA = "UserData"; private static final String TEST_SHARE_DATA = "ShareData"; private static final String TEST_CREATOR_NAME = "CreatorName"; @@ -150,7 +145,10 @@ public class WifiConfigStoreTest { @Mock private Clock mClock; private MockStoreFile mSharedStore; private MockStoreFile mUserStore; - private MockStoreData mStoreData; + private MockStoreFile mUserNetworkSuggestionsStore; + private List mUserStores = new ArrayList(); + private MockStoreData mSharedStoreData; + private MockStoreData mUserStoreData; /** * Test instance of WifiConfigStore. @@ -168,9 +166,15 @@ public class WifiConfigStoreTest { .thenReturn(mAlarmManager.getAlarmManager()); when(mContext.getPackageManager()).thenReturn(mPackageManager); when(mPackageManager.getNameForUid(anyInt())).thenReturn(TEST_CREATOR_NAME); - mUserStore = new MockStoreFile(); - mSharedStore = new MockStoreFile(); - mStoreData = new MockStoreData(); + mSharedStore = new MockStoreFile(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + mUserStore = new MockStoreFile(WifiConfigStore.STORE_FILE_USER_GENERAL); + mUserNetworkSuggestionsStore = + new MockStoreFile(WifiConfigStore.STORE_FILE_USER_NETWORK_SUGGESTIONS); + mUserStores.add(mUserStore); + mUserStores.add(mUserNetworkSuggestionsStore); + + mSharedStoreData = new MockStoreData(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + mUserStoreData = new MockStoreData(WifiConfigStore.STORE_FILE_USER_GENERAL); } /** @@ -194,24 +198,20 @@ public class WifiConfigStoreTest { } /** - * Verify the contents of the config file with empty data. The data content should be the - * same as {@link #EMPTY_FILE_CONTENT}. + * Verify that no write occurs if there is {@link StoreData} registered for any + * {@link StoreFile}. * * @throws Exception */ @Test - public void testWriteWithEmptyData() throws Exception { + public void testWriteWithNoStoreData() throws Exception { // Perform force write to both share and user store file. - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + mWifiConfigStore.setUserStores(mUserStores); mWifiConfigStore.write(true); - assertFalse(mAlarmManager.isPending(WifiConfigStore.BUFFERED_WRITE_ALARM_TAG)); - assertTrue(mSharedStore.isStoreWritten()); - assertTrue(mUserStore.isStoreWritten()); - assertTrue(Arrays.equals(EMPTY_FILE_CONTENT.getBytes(StandardCharsets.UTF_8), - mSharedStore.getStoreBytes())); - assertTrue(Arrays.equals(EMPTY_FILE_CONTENT.getBytes(StandardCharsets.UTF_8), - mUserStore.getStoreBytes())); + assertFalse(mSharedStore.isStoreWritten()); + assertFalse(mUserStore.isStoreWritten()); + assertFalse(mUserNetworkSuggestionsStore.isStoreWritten()); } /** @@ -221,12 +221,17 @@ public class WifiConfigStoreTest { */ @Test public void testForceWrite() throws Exception { - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + // Register data container. + mWifiConfigStore.registerStoreData(mSharedStoreData); + mWifiConfigStore.registerStoreData(mUserStoreData); + + mWifiConfigStore.switchUserStoresAndRead(mUserStores); mWifiConfigStore.write(true); assertFalse(mAlarmManager.isPending(WifiConfigStore.BUFFERED_WRITE_ALARM_TAG)); assertTrue(mSharedStore.isStoreWritten()); assertTrue(mUserStore.isStoreWritten()); + assertFalse(mUserNetworkSuggestionsStore.isStoreWritten()); } /** @@ -235,18 +240,24 @@ public class WifiConfigStoreTest { */ @Test public void testBufferedWrite() throws Exception { - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + // Register data container. + mWifiConfigStore.registerStoreData(mSharedStoreData); + mWifiConfigStore.registerStoreData(mUserStoreData); + + mWifiConfigStore.switchUserStoresAndRead(mUserStores); mWifiConfigStore.write(false); assertTrue(mAlarmManager.isPending(WifiConfigStore.BUFFERED_WRITE_ALARM_TAG)); assertFalse(mSharedStore.isStoreWritten()); assertFalse(mUserStore.isStoreWritten()); + assertFalse(mUserNetworkSuggestionsStore.isStoreWritten()); // Now send the alarm and ensure that the writes happen. mAlarmManager.dispatch(WifiConfigStore.BUFFERED_WRITE_ALARM_TAG); mLooper.dispatchAll(); assertTrue(mSharedStore.isStoreWritten()); assertTrue(mUserStore.isStoreWritten()); + assertFalse(mUserNetworkSuggestionsStore.isStoreWritten()); } /** @@ -257,12 +268,14 @@ public class WifiConfigStoreTest { @Test public void testForceWriteAfterBufferedWrite() throws Exception { // Register a test data container with bogus data. - mWifiConfigStore.registerStoreData(mStoreData); - mStoreData.setShareData("abcds"); - mStoreData.setUserData("asdfa"); + mWifiConfigStore.registerStoreData(mSharedStoreData); + mWifiConfigStore.registerStoreData(mUserStoreData); + + mSharedStoreData.setData("abcds"); + mUserStoreData.setData("asdfa"); // Perform buffered write for both user and share store file. - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + mWifiConfigStore.switchUserStoresAndRead(mUserStores); mWifiConfigStore.write(false); assertTrue(mAlarmManager.isPending(WifiConfigStore.BUFFERED_WRITE_ALARM_TAG)); @@ -271,8 +284,8 @@ public class WifiConfigStoreTest { // Update the container with new set of data. The send a force write and ensure that the // writes have been performed and alarms have been stopped and updated data are written. - mStoreData.setUserData(TEST_USER_DATA); - mStoreData.setShareData(TEST_SHARE_DATA); + mUserStoreData.setData(TEST_USER_DATA); + mSharedStoreData.setData(TEST_SHARE_DATA); mWifiConfigStore.write(true); assertFalse(mAlarmManager.isPending(WifiConfigStore.BUFFERED_WRITE_ALARM_TAG)); @@ -281,10 +294,48 @@ public class WifiConfigStoreTest { // Verify correct data are loaded to the data container after a read. mWifiConfigStore.read(); - assertEquals(TEST_USER_DATA, mStoreData.getUserData()); - assertEquals(TEST_SHARE_DATA, mStoreData.getShareData()); + assertEquals(TEST_USER_DATA, mUserStoreData.getData()); + assertEquals(TEST_SHARE_DATA, mSharedStoreData.getData()); } + /** + * Tests the force write with no new data after a buffered write. + * Expected behaviour: The force write should flush the previous buffered write and stop the + * buffer write alarms. + */ + @Test + public void testForceWriteWithNoNewDataAfterBufferedWrite() throws Exception { + // Register a test data container with bogus data. + mWifiConfigStore.registerStoreData(mSharedStoreData); + mWifiConfigStore.registerStoreData(mUserStoreData); + + mSharedStoreData.setData("abcds"); + mUserStoreData.setData("asdfa"); + + // Perform buffered write for both user and share store file. + mWifiConfigStore.switchUserStoresAndRead(mUserStores); + mWifiConfigStore.write(false); + + assertTrue(mAlarmManager.isPending(WifiConfigStore.BUFFERED_WRITE_ALARM_TAG)); + assertFalse(mSharedStore.isStoreWritten()); + assertFalse(mUserStore.isStoreWritten()); + + // Containers have no new data. + mUserStoreData.setHasAnyNewData(false); + mSharedStoreData.setHasAnyNewData(false); + mWifiConfigStore.write(true); + + assertFalse(mAlarmManager.isPending(WifiConfigStore.BUFFERED_WRITE_ALARM_TAG)); + assertTrue(mSharedStore.isStoreWritten()); + assertTrue(mUserStore.isStoreWritten()); + + // Verify correct data are loaded to the data container after a read. + mWifiConfigStore.read(); + assertEquals("abcds", mSharedStoreData.getData()); + assertEquals("asdfa", mUserStoreData.getData()); + } + + /** * Tests the read API behaviour after a write to the store files. * Expected behaviour: The read should return the same data that was last written. @@ -292,24 +343,25 @@ public class WifiConfigStoreTest { @Test public void testReadAfterWrite() throws Exception { // Register data container. - mWifiConfigStore.registerStoreData(mStoreData); + mWifiConfigStore.registerStoreData(mSharedStoreData); + mWifiConfigStore.registerStoreData(mUserStoreData); // Read both share and user config store. - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + mWifiConfigStore.switchUserStoresAndRead(mUserStores); // Verify no data is read. - assertNull(mStoreData.getUserData()); - assertNull(mStoreData.getShareData()); + assertNull(mUserStoreData.getData()); + assertNull(mSharedStoreData.getData()); // Write share and user data. - mStoreData.setUserData(TEST_USER_DATA); - mStoreData.setShareData(TEST_SHARE_DATA); + mUserStoreData.setData(TEST_USER_DATA); + mSharedStoreData.setData(TEST_SHARE_DATA); mWifiConfigStore.write(true); // Read and verify the data content in the data container. mWifiConfigStore.read(); - assertEquals(TEST_USER_DATA, mStoreData.getUserData()); - assertEquals(TEST_SHARE_DATA, mStoreData.getShareData()); + assertEquals(TEST_USER_DATA, mUserStoreData.getData()); + assertEquals(TEST_SHARE_DATA, mSharedStoreData.getData()); } /** @@ -320,19 +372,25 @@ public class WifiConfigStoreTest { */ @Test public void testReadWithNoSharedStoreFileAndUserStoreNotVisible() throws Exception { - WifiConfigStore.StoreData storeData = mock(WifiConfigStore.StoreData.class); + StoreData sharedStoreData = mock(StoreData.class); + when(sharedStoreData.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + StoreData userStoreData = mock(StoreData.class); + when(userStoreData.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_GENERAL); // Reading the mock store without a write should simulate the file not found case because // |readRawData| would return null. - mWifiConfigStore.registerStoreData(storeData); + mWifiConfigStore.registerStoreData(sharedStoreData); + mWifiConfigStore.registerStoreData(userStoreData); assertFalse(mWifiConfigStore.areStoresPresent()); mWifiConfigStore.read(); // Ensure that we got the call to deserialize empty shared data, but no user data. - verify(storeData).resetData(eq(true)); - verify(storeData).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData, never()).resetData(eq(false)); - verify(storeData, never()).deserializeData(eq(null), anyInt(), eq(false)); + verify(sharedStoreData).resetData(); + verify(sharedStoreData).deserializeData(eq(null), anyInt()); + verify(userStoreData, never()).resetData(); + verify(userStoreData, never()).deserializeData(any(), anyInt()); } /** @@ -342,21 +400,27 @@ public class WifiConfigStoreTest { */ @Test public void testReadWithNoStoreFiles() throws Exception { - WifiConfigStore.StoreData storeData = mock(WifiConfigStore.StoreData.class); + StoreData sharedStoreData = mock(StoreData.class); + when(sharedStoreData.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + StoreData userStoreData = mock(StoreData.class); + when(userStoreData.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_GENERAL); // Reading the mock store without a write should simulate the file not found case because // |readRawData| would return null. - mWifiConfigStore.registerStoreData(storeData); + mWifiConfigStore.registerStoreData(sharedStoreData); + mWifiConfigStore.registerStoreData(userStoreData); // Read both share and user config store. - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + mWifiConfigStore.setUserStores(mUserStores); assertFalse(mWifiConfigStore.areStoresPresent()); mWifiConfigStore.read(); // Ensure that we got the call to deserialize empty shared & user data. - verify(storeData).resetData(eq(true)); - verify(storeData).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData, times(2)).resetData(eq(false)); - verify(storeData, times(2)).deserializeData(eq(null), anyInt(), eq(false)); + verify(userStoreData).resetData(); + verify(userStoreData).deserializeData(eq(null), anyInt()); + verify(sharedStoreData).resetData(); + verify(sharedStoreData).deserializeData(eq(null), anyInt()); } /** @@ -367,14 +431,14 @@ public class WifiConfigStoreTest { @Test public void testReadAfterWriteWithNoUserStore() throws Exception { // Setup data container. - mWifiConfigStore.registerStoreData(mStoreData); - mStoreData.setShareData(TEST_SHARE_DATA); + mWifiConfigStore.registerStoreData(mSharedStoreData); + mSharedStoreData.setData(TEST_SHARE_DATA); // Perform write for the share store file. mWifiConfigStore.write(true); mWifiConfigStore.read(); // Verify data content for both user and share data. - assertEquals(TEST_SHARE_DATA, mStoreData.getShareData()); + assertEquals(TEST_SHARE_DATA, mSharedStoreData.getData()); } /** @@ -386,21 +450,22 @@ public class WifiConfigStoreTest { @Test public void testReadWillResetStoreData() throws Exception { // Register and setup store data. - mWifiConfigStore.registerStoreData(mStoreData); + mWifiConfigStore.registerStoreData(mSharedStoreData); + mWifiConfigStore.registerStoreData(mUserStoreData); // Perform force write with empty data content to both user and share store file. - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + mWifiConfigStore.switchUserStoresAndRead(mUserStores); mWifiConfigStore.write(true); // Setup data container with some value. - mStoreData.setUserData(TEST_USER_DATA); - mStoreData.setShareData(TEST_SHARE_DATA); + mUserStoreData.setData(TEST_USER_DATA); + mSharedStoreData.setData(TEST_SHARE_DATA); // Perform read of both user and share store file and verify data in the data container // is in sync (empty) with what is in the file. mWifiConfigStore.read(); - assertNull(mStoreData.getShareData()); - assertNull(mStoreData.getUserData()); + assertNull(mSharedStoreData.getData()); + assertNull(mUserStoreData.getData()); } /** @@ -413,7 +478,7 @@ public class WifiConfigStoreTest { @Test public void testReadWifiConfigStoreData() throws Exception { // Setup network list. - NetworkListStoreData networkList = new NetworkListStoreData(mContext); + NetworkListStoreData networkList = new NetworkListUserStoreData(mContext); mWifiConfigStore.registerStoreData(networkList); WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); openNetwork.creatorName = TEST_CREATOR_NAME; @@ -440,9 +505,9 @@ public class WifiConfigStoreTest { byte[] xmlBytes = xmlString.getBytes(StandardCharsets.UTF_8); mUserStore.storeRawDataToWrite(xmlBytes); - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + mWifiConfigStore.switchUserStoresAndRead(mUserStores); WifiConfigurationTestUtil.assertConfigurationsEqualForConfigStore( - userConfigs, networkList.getUserConfigurations()); + userConfigs, networkList.getConfigurations()); assertEquals(ssidList, deletedEphemeralSsids.getSsidList()); } @@ -455,10 +520,10 @@ public class WifiConfigStoreTest { @Test public void testWriteWifiConfigStoreData() throws Exception { // Setup user store. - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + mWifiConfigStore.switchUserStoresAndRead(mUserStores); // Setup network list store data. - NetworkListStoreData networkList = new NetworkListStoreData(mContext); + NetworkListStoreData networkList = new NetworkListUserStoreData(mContext); mWifiConfigStore.registerStoreData(networkList); WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); openNetwork.creatorName = TEST_CREATOR_NAME; @@ -467,7 +532,7 @@ public class WifiConfigStoreTest { openNetwork.setRandomizedMacAddress(TEST_RANDOMIZED_MAC); List userConfigs = new ArrayList<>(); userConfigs.add(openNetwork); - networkList.setUserConfigurations(userConfigs); + networkList.setConfigurations(userConfigs); // Setup deleted ephemeral SSID list store data. DeletedEphemeralSsidsStoreData deletedEphemeralSsids = @@ -503,14 +568,12 @@ public class WifiConfigStoreTest { public void testReadWifiConfigStoreDataIndicateClientsThatThereIsNoDataForThem() throws Exception { // Set both the user store & shared store files. - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + mWifiConfigStore.switchUserStoresAndRead(mUserStores); String storeData1Name = "test1"; String storeData2Name = "test2"; - WifiConfigStore.StoreData storeData1 = mock(WifiConfigStore.StoreData.class); - WifiConfigStore.StoreData storeData2 = mock(WifiConfigStore.StoreData.class); - when(storeData1.getName()).thenReturn(storeData1Name); - when(storeData2.getName()).thenReturn(storeData2Name); + StoreData storeData1 = mock(StoreData.class); + StoreData storeData2 = mock(StoreData.class); assertTrue(mWifiConfigStore.registerStoreData(storeData1)); assertTrue(mWifiConfigStore.registerStoreData(storeData2)); @@ -524,57 +587,121 @@ public class WifiConfigStoreTest { storeData1Name, storeData2Name); // Scenario 1: StoreData1 in shared store file. + when(storeData1.getName()).thenReturn(storeData1Name); + when(storeData2.getName()).thenReturn(storeData2Name); + when(storeData1.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + when(storeData2.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); mSharedStore.storeRawDataToWrite(fileContentsXmlStringWithOnlyStoreData1.getBytes()); mUserStore.storeRawDataToWrite(null); mWifiConfigStore.read(); - verify(storeData1).deserializeData(notNull(), anyInt(), eq(true)); - verify(storeData1, never()).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData1).deserializeData(eq(null), anyInt(), eq(false)); - verify(storeData2).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData2).deserializeData(eq(null), anyInt(), eq(false)); + verify(storeData1).deserializeData(notNull(), anyInt()); + verify(storeData1, never()).deserializeData(eq(null), anyInt()); + verify(storeData2).deserializeData(eq(null), anyInt()); reset(storeData1, storeData2); // Scenario 2: StoreData2 in user store file. + when(storeData1.getName()).thenReturn(storeData1Name); + when(storeData2.getName()).thenReturn(storeData2Name); + when(storeData1.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_GENERAL); + when(storeData2.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_GENERAL); mSharedStore.storeRawDataToWrite(null); mUserStore.storeRawDataToWrite(fileContentsXmlStringWithOnlyStoreData2.getBytes()); mWifiConfigStore.read(); - verify(storeData1).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData1).deserializeData(eq(null), anyInt(), eq(false)); - verify(storeData2).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData2).deserializeData(notNull(), anyInt(), eq(false)); - verify(storeData2, never()).deserializeData(eq(null), anyInt(), eq(false)); + verify(storeData1).deserializeData(eq(null), anyInt()); + verify(storeData2).deserializeData(notNull(), anyInt()); + verify(storeData2, never()).deserializeData(eq(null), anyInt()); reset(storeData1, storeData2); // Scenario 3: StoreData1 in shared store file & StoreData2 in user store file. + when(storeData1.getName()).thenReturn(storeData1Name); + when(storeData2.getName()).thenReturn(storeData2Name); + when(storeData1.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + when(storeData2.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_GENERAL); mSharedStore.storeRawDataToWrite(fileContentsXmlStringWithOnlyStoreData1.getBytes()); mUserStore.storeRawDataToWrite(fileContentsXmlStringWithOnlyStoreData2.getBytes()); mWifiConfigStore.read(); - verify(storeData1).deserializeData(notNull(), anyInt(), eq(true)); - verify(storeData1, never()).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData1).deserializeData(eq(null), anyInt(), eq(false)); - verify(storeData2).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData2).deserializeData(notNull(), anyInt(), eq(false)); - verify(storeData2, never()).deserializeData(eq(null), anyInt(), eq(false)); + verify(storeData1).deserializeData(notNull(), anyInt()); + verify(storeData1, never()).deserializeData(eq(null), anyInt()); + verify(storeData2).deserializeData(notNull(), anyInt()); + verify(storeData2, never()).deserializeData(eq(null), anyInt()); reset(storeData1, storeData2); // Scenario 4: StoreData1 & StoreData2 in shared store file. + when(storeData1.getName()).thenReturn(storeData1Name); + when(storeData2.getName()).thenReturn(storeData2Name); + when(storeData1.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + when(storeData2.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); mSharedStore.storeRawDataToWrite( fileContentsXmlStringWithStoreData1AndStoreData2.getBytes()); mUserStore.storeRawDataToWrite(null); mWifiConfigStore.read(); - verify(storeData1).deserializeData(notNull(), anyInt(), eq(true)); - verify(storeData1, never()).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData1).deserializeData(eq(null), anyInt(), eq(false)); - verify(storeData2).deserializeData(notNull(), anyInt(), eq(true)); - verify(storeData2, never()).deserializeData(eq(null), anyInt(), eq(true)); - verify(storeData2).deserializeData(eq(null), anyInt(), eq(false)); + verify(storeData1).deserializeData(notNull(), anyInt()); + verify(storeData1, never()).deserializeData(eq(null), anyInt()); + verify(storeData2).deserializeData(notNull(), anyInt()); + verify(storeData2, never()).deserializeData(eq(null), anyInt()); reset(storeData1, storeData2); } + /** + * Tests the write API behavior when all the store data's registered for a given store file + * has no new data to write. + * Expected behaviour: The write should not trigger a new file write for that specific store + * file. + */ + @Test + public void testWriteWithNoNewData() throws Exception { + StoreData sharedStoreData = mock(StoreData.class); + when(sharedStoreData.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + when(sharedStoreData.hasNewDataToSerialize()).thenReturn(true); + when(sharedStoreData.getName()).thenReturn("sharedStoreData"); + + StoreData userStoreData = mock(StoreData.class); + when(userStoreData.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_GENERAL); + when(userStoreData.hasNewDataToSerialize()).thenReturn(true); + when(userStoreData.getName()).thenReturn("userStoreData"); + + StoreData userStoreNetworkSuggestionsData = + mock(StoreData.class); + when(userStoreNetworkSuggestionsData.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_NETWORK_SUGGESTIONS); + when(userStoreNetworkSuggestionsData.hasNewDataToSerialize()).thenReturn(false); + when(userStoreNetworkSuggestionsData.getName()) + .thenReturn("userStoreNetworkSuggestionsData"); + + assertTrue(mWifiConfigStore.registerStoreData(sharedStoreData)); + assertTrue(mWifiConfigStore.registerStoreData(userStoreData)); + assertTrue(mWifiConfigStore.registerStoreData(userStoreNetworkSuggestionsData)); + + // Write both share and user config store. + mWifiConfigStore.setUserStores(mUserStores); + + // Now trigger a write. + mWifiConfigStore.write(true); + + verify(sharedStoreData).hasNewDataToSerialize(); + verify(userStoreData).hasNewDataToSerialize(); + verify(userStoreNetworkSuggestionsData).hasNewDataToSerialize(); + + // Verify that we serialized data from the first 2 data source, but not from the last one. + verify(sharedStoreData).serializeData(any()); + verify(userStoreData).serializeData(any()); + verify(userStoreNetworkSuggestionsData, never()).serializeData(any()); + } + /** * Verify that a XmlPullParserException will be thrown when reading an user store file * containing unknown data. @@ -591,7 +718,7 @@ public class WifiConfigStoreTest { + "\n" + "\n"; mUserStore.storeRawDataToWrite(storeFileData.getBytes(StandardCharsets.UTF_8)); - mWifiConfigStore.switchUserStoreAndRead(mUserStore); + mWifiConfigStore.switchUserStoresAndRead(mUserStores); } /** @@ -621,8 +748,8 @@ public class WifiConfigStoreTest { private byte[] mStoreBytes; private boolean mStoreWritten; - public MockStoreFile() { - super(new File("MockStoreFile")); + MockStoreFile(@WifiConfigStore.StoreFileId int fileId) { + super(new File("MockStoreFile"), fileId); } @Override @@ -643,7 +770,9 @@ public class WifiConfigStoreTest { @Override public void writeBufferedRawData() { - mStoreWritten = true; + if (!ArrayUtils.isEmpty(mStoreBytes)) { + mStoreWritten = true; + } } public byte[] getStoreBytes() { @@ -658,71 +787,63 @@ public class WifiConfigStoreTest { /** * Mock data container for providing test data for the store file. */ - private class MockStoreData implements WifiConfigStore.StoreData { + private class MockStoreData implements StoreData { private static final String XML_TAG_TEST_HEADER = "TestHeader"; private static final String XML_TAG_TEST_DATA = "TestData"; - private String mShareData; - private String mUserData; + private @WifiConfigStore.StoreFileId int mFileId; + private String mData; + private boolean mHasAnyNewData = true; - MockStoreData() {} + MockStoreData(@WifiConfigStore.StoreFileId int fileId) { + mFileId = fileId; + } @Override - public void serializeData(XmlSerializer out, boolean shared) + public void serializeData(XmlSerializer out) throws XmlPullParserException, IOException { - if (shared) { - XmlUtil.writeNextValue(out, XML_TAG_TEST_DATA, mShareData); - } else { - XmlUtil.writeNextValue(out, XML_TAG_TEST_DATA, mUserData); - } + XmlUtil.writeNextValue(out, XML_TAG_TEST_DATA, mData); } @Override - public void deserializeData(XmlPullParser in, int outerTagDepth, boolean shared) + public void deserializeData(XmlPullParser in, int outerTagDepth) throws XmlPullParserException, IOException { if (in == null) { return; } - if (shared) { - mShareData = (String) XmlUtil.readNextValueWithName(in, XML_TAG_TEST_DATA); - } else { - mUserData = (String) XmlUtil.readNextValueWithName(in, XML_TAG_TEST_DATA); - } + mData = (String) XmlUtil.readNextValueWithName(in, XML_TAG_TEST_DATA); } @Override - public void resetData(boolean shared) { - if (shared) { - mShareData = null; - } else { - mUserData = null; - } + public void resetData() { + mData = null; } @Override - public String getName() { - return XML_TAG_TEST_HEADER; + public boolean hasNewDataToSerialize() { + return mHasAnyNewData; } @Override - public boolean supportShareData() { - return true; + public String getName() { + return XML_TAG_TEST_HEADER; } - public String getShareData() { - return mShareData; + @Override + public @WifiConfigStore.StoreFileId int getStoreFileId() { + return mFileId; } - public void setShareData(String shareData) { - mShareData = shareData; + public String getData() { + return mData; } - public String getUserData() { - return mUserData; + public void setData(String data) { + mData = data; } - public void setUserData(String userData) { - mUserData = userData; + public void setHasAnyNewData(boolean hasAnyNewData) { + mHasAnyNewData = hasAnyNewData; } } } -- cgit v1.2.3 From 697799b05b333bab62965d29f7b4fcb91b14fed9 Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Fri, 19 Oct 2018 08:29:52 -0700 Subject: WifiConfigStore Refactor (2/2) Change all the WifiConfigStore clients to use the new architecture: a) Any clients (StoreData) that was previously writing data to both the user & shared store files needs to split into 2 separate |StoreData| instances. b) All clients (StoreData) currently always ask for data to persisted. This can potentially be improved in the future (only set the flag when it has new data), but it preserves current state for both the general shared & user store files. Bug: 117599800 Test: Unit tests Test: Verified upgrading device with some store data to the new architecture and ensured that we don't lose any data. Test: Will send for integration tests. Change-Id: Ibc07a7adba855fa858ddcf27c8a787677fcb3d3d --- .../wifi/DeletedEphemeralSsidsStoreDataTest.java | 51 +--- .../server/wifi/NetworkListStoreDataTest.java | 89 +++--- .../android/server/wifi/SsidSetStoreDataTest.java | 51 +--- .../server/wifi/WakeupConfigStoreDataTest.java | 53 ++-- .../android/server/wifi/WakeupControllerTest.java | 2 +- .../android/server/wifi/WifiConfigManagerTest.java | 76 +++--- .../PasspointConfigSharedStoreDataTest.java | 125 +++++++++ .../hotspot2/PasspointConfigStoreDataTest.java | 299 --------------------- .../hotspot2/PasspointConfigUserStoreDataTest.java | 278 +++++++++++++++++++ .../server/wifi/hotspot2/PasspointManagerTest.java | 47 ++-- 10 files changed, 546 insertions(+), 525 deletions(-) create mode 100644 tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigSharedStoreDataTest.java delete mode 100644 tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigStoreDataTest.java create mode 100644 tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigUserStoreDataTest.java (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/DeletedEphemeralSsidsStoreDataTest.java b/tests/wifitests/src/com/android/server/wifi/DeletedEphemeralSsidsStoreDataTest.java index 289a3beab..2af8b557c 100644 --- a/tests/wifitests/src/com/android/server/wifi/DeletedEphemeralSsidsStoreDataTest.java +++ b/tests/wifitests/src/com/android/server/wifi/DeletedEphemeralSsidsStoreDataTest.java @@ -27,7 +27,6 @@ import com.android.internal.util.FastXmlSerializer; import org.junit.Before; import org.junit.Test; import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import java.io.ByteArrayInputStream; @@ -61,15 +60,14 @@ public class DeletedEphemeralSsidsStoreDataTest { /** * Helper function for serializing configuration data to a XML block. * - * @param shared Flag indicating serializing shared or user configurations * @return byte[] of the XML data * @throws Exception */ - private byte[] serializeData(boolean shared) throws Exception { + private byte[] serializeData() throws Exception { final XmlSerializer out = new FastXmlSerializer(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); out.setOutput(outputStream, StandardCharsets.UTF_8.name()); - mDeletedEphemeralSsidsStoreData.serializeData(out, shared); + mDeletedEphemeralSsidsStoreData.serializeData(out); out.flush(); return outputStream.toByteArray(); } @@ -78,42 +76,17 @@ public class DeletedEphemeralSsidsStoreDataTest { * Helper function for parsing configuration data from a XML block. * * @param data XML data to parse from - * @param shared Flag indicating parsing of shared or user configurations * @return SSID list * @throws Exception */ - private Set deserializeData(byte[] data, boolean shared) throws Exception { + private Set deserializeData(byte[] data) throws Exception { final XmlPullParser in = Xml.newPullParser(); final ByteArrayInputStream inputStream = new ByteArrayInputStream(data); in.setInput(inputStream, StandardCharsets.UTF_8.name()); - mDeletedEphemeralSsidsStoreData.deserializeData(in, in.getDepth(), shared); + mDeletedEphemeralSsidsStoreData.deserializeData(in, in.getDepth()); return mDeletedEphemeralSsidsStoreData.getSsidList(); } - /** - * Verify that a XmlPullParserException will be thrown when attempting to serialize SSID list - * to the share store, since the deleted ephemeral SSID list should never be persist - * to the share store. - * - * @throws Exception - */ - @Test(expected = XmlPullParserException.class) - public void serializeShareData() throws Exception { - serializeData(true /* shared */); - } - - /** - * Verify that a XmlPullParserException will be thrown when attempting to parse SSID list - * from the share store, since the deleted ephemeral SSID list should never be persist - * to the share store. - * - * @throws Exception - */ - @Test(expected = XmlPullParserException.class) - public void deserializeShareData() throws Exception { - deserializeData(new byte[0], true /* shared */); - } - /** * Verify that serializing the user store data without any configuration doesn't cause any * crash and no data should be serialized. @@ -122,7 +95,7 @@ public class DeletedEphemeralSsidsStoreDataTest { */ @Test public void serializeEmptyConfigs() throws Exception { - assertEquals(0, serializeData(false /* shared */).length); + assertEquals(0, serializeData().length); } /** @@ -133,17 +106,19 @@ public class DeletedEphemeralSsidsStoreDataTest { */ @Test public void deserializeEmptyData() throws Exception { - assertTrue(deserializeData(new byte[0], false /* shared */).isEmpty()); + assertTrue(deserializeData(new byte[0]).isEmpty()); } /** - * Verify that DeletedEphemeralSsidsStoreData does not support share data. + * Verify that DeletedEphemeralSsidsStoreData is written to + * {@link WifiConfigStore#STORE_FILE_NAME_USER_GENERAL}. * * @throws Exception */ @Test - public void supportShareData() throws Exception { - assertFalse(mDeletedEphemeralSsidsStoreData.supportShareData()); + public void getUserStoreFileId() throws Exception { + assertEquals(WifiConfigStore.STORE_FILE_USER_GENERAL, + mDeletedEphemeralSsidsStoreData.getStoreFileId()); } /** @@ -158,7 +133,7 @@ public class DeletedEphemeralSsidsStoreDataTest { ssidList.add(TEST_SSID1); ssidList.add(TEST_SSID2); mDeletedEphemeralSsidsStoreData.setSsidList(ssidList); - byte[] actualData = serializeData(false /* shared */); + byte[] actualData = serializeData(); assertTrue(Arrays.equals(TEST_SSID_LIST_XML_BYTES, actualData)); } @@ -173,6 +148,6 @@ public class DeletedEphemeralSsidsStoreDataTest { Set ssidList = new HashSet<>(); ssidList.add(TEST_SSID1); ssidList.add(TEST_SSID2); - assertEquals(ssidList, deserializeData(TEST_SSID_LIST_XML_BYTES, false /* shared */)); + assertEquals(ssidList, deserializeData(TEST_SSID_LIST_XML_BYTES)); } } diff --git a/tests/wifitests/src/com/android/server/wifi/NetworkListStoreDataTest.java b/tests/wifitests/src/com/android/server/wifi/NetworkListStoreDataTest.java index caed9445e..5fa15e81b 100644 --- a/tests/wifitests/src/com/android/server/wifi/NetworkListStoreDataTest.java +++ b/tests/wifitests/src/com/android/server/wifi/NetworkListStoreDataTest.java @@ -48,7 +48,7 @@ import java.util.Arrays; import java.util.List; /** - * Unit tests for {@link com.android.server.wifi.NetworksListStoreData}. + * Unit tests for {@link com.android.server.wifi.NetworkListStoreData}. */ @SmallTest public class NetworkListStoreDataTest { @@ -182,7 +182,9 @@ public class NetworkListStoreDataTest { + "\n" + "\n"; - private NetworkListStoreData mNetworkListStoreData; + // We use {@link NetworkListSharedStoreData} instance because {@link NetworkListStoreData} is + // abstract. + private NetworkListSharedStoreData mNetworkListSharedStoreData; @Mock private Context mContext; @Mock private PackageManager mPackageManager; @@ -191,21 +193,20 @@ public class NetworkListStoreDataTest { MockitoAnnotations.initMocks(this); when(mContext.getPackageManager()).thenReturn(mPackageManager); when(mPackageManager.getNameForUid(anyInt())).thenReturn(TEST_CREATOR_NAME); - mNetworkListStoreData = new NetworkListStoreData(mContext); + mNetworkListSharedStoreData = new NetworkListSharedStoreData(mContext); } /** * Helper function for serializing configuration data to a XML block. * - * @param shared Flag indicating serializing shared or user configurations * @return byte[] of the XML data * @throws Exception */ - private byte[] serializeData(boolean shared) throws Exception { + private byte[] serializeData() throws Exception { final XmlSerializer out = new FastXmlSerializer(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); out.setOutput(outputStream, StandardCharsets.UTF_8.name()); - mNetworkListStoreData.serializeData(out, shared); + mNetworkListSharedStoreData.serializeData(out); out.flush(); return outputStream.toByteArray(); } @@ -214,20 +215,15 @@ public class NetworkListStoreDataTest { * Helper function for parsing configuration data from a XML block. * * @param data XML data to parse from - * @param shared Flag indicating parsing of shared or user configurations * @return List of WifiConfiguration parsed * @throws Exception */ - private List deserializeData(byte[] data, boolean shared) throws Exception { + private List deserializeData(byte[] data) throws Exception { final XmlPullParser in = Xml.newPullParser(); final ByteArrayInputStream inputStream = new ByteArrayInputStream(data); in.setInput(inputStream, StandardCharsets.UTF_8.name()); - mNetworkListStoreData.deserializeData(in, in.getDepth(), shared); - if (shared) { - return mNetworkListStoreData.getSharedConfigurations(); - } else { - return mNetworkListStoreData.getUserConfigurations(); - } + mNetworkListSharedStoreData.deserializeData(in, in.getDepth()); + return mNetworkListSharedStoreData.getConfigurations(); } /** @@ -288,8 +284,7 @@ public class NetworkListStoreDataTest { */ @Test public void serializeEmptyConfigs() throws Exception { - assertEquals(0, serializeData(true /* shared */).length); - assertEquals(0, serializeData(false /* shared */).length); + assertEquals(0, serializeData().length); } /** @@ -300,18 +295,23 @@ public class NetworkListStoreDataTest { */ @Test public void deserializeEmptyData() throws Exception { - assertTrue(deserializeData(new byte[0], true /* shared */).isEmpty()); - assertTrue(deserializeData(new byte[0], false /* shared */).isEmpty()); + assertTrue(deserializeData(new byte[0]).isEmpty()); } /** - * Verify that NetworkListStoreData does support share data. + * Verify that {@link NetworkListSharedStoreData} is written to + * {@link WifiConfigStore#STORE_FILE_NAME_SHARED_GENERAL}. + * Verify that {@link NetworkListUserStoreData} is written to + * {@link WifiConfigStore#STORE_FILE_NAME_USER_GENERAL}. * * @throws Exception */ @Test - public void supportShareData() throws Exception { - assertTrue(mNetworkListStoreData.supportShareData()); + public void getUserStoreFileId() throws Exception { + assertEquals(WifiConfigStore.STORE_FILE_SHARED_GENERAL, + mNetworkListSharedStoreData.getStoreFileId()); + assertEquals(WifiConfigStore.STORE_FILE_USER_GENERAL, + new NetworkListUserStoreData(mContext).getStoreFileId()); } /** @@ -323,9 +323,9 @@ public class NetworkListStoreDataTest { @Test public void serializeSharedConfigurations() throws Exception { List networkList = getTestNetworksConfig(true /* shared */); - mNetworkListStoreData.setSharedConfigurations(networkList); + mNetworkListSharedStoreData.setConfigurations(networkList); byte[] expectedData = getTestNetworksXmlBytes(networkList.get(0), networkList.get(1)); - assertTrue(Arrays.equals(expectedData, serializeData(true /* shared */))); + assertTrue(Arrays.equals(expectedData, serializeData())); } /** @@ -338,34 +338,7 @@ public class NetworkListStoreDataTest { List networkList = getTestNetworksConfig(true /* shared */); byte[] xmlData = getTestNetworksXmlBytes(networkList.get(0), networkList.get(1)); WifiConfigurationTestUtil.assertConfigurationsEqualForConfigStore( - networkList, deserializeData(xmlData, true /* shared */)); - } - - /** - * Verify that the user configurations (containing an open and an EAP network) are serialized - * correctly, matching the expected XML string. - * - * @throws Exception - */ - @Test - public void serializeUserConfigurations() throws Exception { - List networkList = getTestNetworksConfig(false /* shared */); - mNetworkListStoreData.setUserConfigurations(networkList); - byte[] expectedData = getTestNetworksXmlBytes(networkList.get(0), networkList.get(1)); - assertTrue(Arrays.equals(expectedData, serializeData(false /* shared */))); - } - - /** - * Verify that the user configurations are parsed correctly from a XML string containing - * test networks (an open and an EAP network). - * @throws Exception - */ - @Test - public void deserializeUserConfigurations() throws Exception { - List networkList = getTestNetworksConfig(false /* shared */); - byte[] xmlData = getTestNetworksXmlBytes(networkList.get(0), networkList.get(1)); - WifiConfigurationTestUtil.assertConfigurationsEqualForConfigStore( - networkList, deserializeData(xmlData, false /* shared */)); + networkList, deserializeData(xmlData)); } /** @@ -432,7 +405,7 @@ public class NetworkListStoreDataTest { openNetwork.SSID.replaceAll("\"", """), openNetwork.shared, openNetwork.creatorUid, openNetwork.getRandomizedMacAddress()) .getBytes(StandardCharsets.UTF_8); - deserializeData(xmlData, true); + deserializeData(xmlData); } /** @@ -450,7 +423,7 @@ public class NetworkListStoreDataTest { openNetwork.shared, openNetwork.creatorUid, openNetwork.creatorName, openNetwork.getRandomizedMacAddress()) .getBytes(StandardCharsets.UTF_8); - deserializeData(xmlData, true); + deserializeData(xmlData); } /** @@ -470,7 +443,7 @@ public class NetworkListStoreDataTest { String.format(XmlUtilTest.XML_STRING_EAP_METHOD_REPLACE_FORMAT, WifiEnterpriseConfig.Eap.NONE)); List retrievedNetworkList = - deserializeData(xmlString.getBytes(StandardCharsets.UTF_8), true /* shared */); + deserializeData(xmlString.getBytes(StandardCharsets.UTF_8)); // Retrieved network should not contain the eap network. assertEquals(1, retrievedNetworkList.size()); for (WifiConfiguration network : retrievedNetworkList) { @@ -495,7 +468,7 @@ public class NetworkListStoreDataTest { openNetwork.shared, openNetwork.creatorUid, openNetwork.creatorName, openNetwork.getRandomizedMacAddress()) .getBytes(StandardCharsets.UTF_8); - List deserializedNetworks = deserializeData(xmlData, true); + List deserializedNetworks = deserializeData(xmlData); assertEquals(1, deserializedNetworks.size()); assertEquals(openNetwork.configKey(), deserializedNetworks.get(0).configKey()); assertEquals(SYSTEM_UID, deserializedNetworks.get(0).creatorUid); @@ -523,7 +496,7 @@ public class NetworkListStoreDataTest { openNetwork.shared, openNetwork.creatorUid, openNetwork.creatorName, openNetwork.getRandomizedMacAddress()) .getBytes(StandardCharsets.UTF_8); - List deserializedNetworks = deserializeData(xmlData, true); + List deserializedNetworks = deserializeData(xmlData); assertEquals(1, deserializedNetworks.size()); assertEquals(openNetwork.configKey(), deserializedNetworks.get(0).configKey()); assertEquals(openNetwork.creatorUid, deserializedNetworks.get(0).creatorUid); @@ -550,7 +523,7 @@ public class NetworkListStoreDataTest { openNetwork.shared, openNetwork.creatorUid, openNetwork.creatorName, openNetwork.getRandomizedMacAddress()) .getBytes(StandardCharsets.UTF_8); - List deserializedNetworks = deserializeData(xmlData, true); + List deserializedNetworks = deserializeData(xmlData); assertEquals(1, deserializedNetworks.size()); assertEquals(openNetwork.configKey(), deserializedNetworks.get(0).configKey()); assertEquals(openNetwork.creatorUid, deserializedNetworks.get(0).creatorUid); @@ -571,7 +544,7 @@ public class NetworkListStoreDataTest { openNetwork.shared, openNetwork.creatorUid, openNetwork.creatorName, openNetwork.getRandomizedMacAddress()) .getBytes(StandardCharsets.UTF_8); - List deserializedNetworks = deserializeData(xmlData, true); + List deserializedNetworks = deserializeData(xmlData); assertEquals(1, deserializedNetworks.size()); assertEquals(openNetwork.configKey(), deserializedNetworks.get(0).configKey()); assertEquals(openNetwork.creatorUid, deserializedNetworks.get(0).creatorUid); diff --git a/tests/wifitests/src/com/android/server/wifi/SsidSetStoreDataTest.java b/tests/wifitests/src/com/android/server/wifi/SsidSetStoreDataTest.java index 8a9d9bc80..86ebb875f 100644 --- a/tests/wifitests/src/com/android/server/wifi/SsidSetStoreDataTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SsidSetStoreDataTest.java @@ -17,7 +17,6 @@ package com.android.server.wifi; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.any; import static org.mockito.Mockito.eq; @@ -73,15 +72,14 @@ public class SsidSetStoreDataTest { /** * Helper function for serializing configuration data to a XML block. * - * @param shared Flag indicating serializing shared or user configurations * @return byte[] of the XML data * @throws Exception */ - private byte[] serializeData(boolean shared) throws Exception { + private byte[] serializeData() throws Exception { final XmlSerializer out = new FastXmlSerializer(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); out.setOutput(outputStream, StandardCharsets.UTF_8.name()); - mSsidSetStoreData.serializeData(out, shared); + mSsidSetStoreData.serializeData(out); out.flush(); return outputStream.toByteArray(); } @@ -90,36 +88,13 @@ public class SsidSetStoreDataTest { * Helper function for parsing configuration data from a XML block. * * @param data XML data to parse from - * @param shared Flag indicating parsing of shared or user configurations * @throws Exception */ - private void deserializeData(byte[] data, boolean shared) throws Exception { + private void deserializeData(byte[] data) throws Exception { final XmlPullParser in = Xml.newPullParser(); final ByteArrayInputStream inputStream = new ByteArrayInputStream(data); in.setInput(inputStream, StandardCharsets.UTF_8.name()); - mSsidSetStoreData.deserializeData(in, in.getDepth(), shared); - } - - /** - * Verify that a XmlPullParserException will be thrown when attempting to serialize data - * to the share store. - * - * @throws Exception - */ - @Test(expected = XmlPullParserException.class) - public void serializeShareData() throws Exception { - serializeData(true /* shared */); - } - - /** - * Verify that a XmlPullParserException will be thrown when attempting to deserialize - * data from the share store. - * - * @throws Exception - */ - @Test(expected = XmlPullParserException.class) - public void deserializeShareData() throws Exception { - deserializeData(new byte[0], true /* shared */); + mSsidSetStoreData.deserializeData(in, in.getDepth()); } /** @@ -131,7 +106,7 @@ public class SsidSetStoreDataTest { @Test public void serializeEmptyConfigs() throws Exception { when(mDataSource.getSsids()).thenReturn(new HashSet()); - assertEquals(0, serializeData(false /* shared */).length); + assertEquals(0, serializeData().length); } /** @@ -142,18 +117,20 @@ public class SsidSetStoreDataTest { */ @Test public void deserializeEmptyStoreData() throws Exception { - deserializeData(new byte[0], false /* shared */); + deserializeData(new byte[0]); verify(mDataSource, never()).setSsids(any(Set.class)); } /** - * Verify that {@link SsidSetStoreData} does not support share data. + * Verify that SsidSetStoreData is written to + * {@link WifiConfigStore#STORE_FILE_NAME_USER_GENERAL}. * * @throws Exception */ @Test - public void supportShareData() throws Exception { - assertFalse(mSsidSetStoreData.supportShareData()); + public void getUserStoreFileId() throws Exception { + assertEquals(WifiConfigStore.STORE_FILE_USER_GENERAL, + mSsidSetStoreData.getStoreFileId()); } /** @@ -167,7 +144,7 @@ public class SsidSetStoreDataTest { ssidSet.add(TEST_SSID1); ssidSet.add(TEST_SSID2); when(mDataSource.getSsids()).thenReturn(ssidSet); - byte[] actualData = serializeData(false /* shared */); + byte[] actualData = serializeData(); assertTrue(Arrays.equals(TEST_SSID_SET_XML_BYTES, actualData)); } @@ -181,7 +158,7 @@ public class SsidSetStoreDataTest { Set ssidSet = new HashSet<>(); ssidSet.add(TEST_SSID1); ssidSet.add(TEST_SSID2); - deserializeData(TEST_SSID_SET_XML_BYTES, false /* shared */); + deserializeData(TEST_SSID_SET_XML_BYTES); verify(mDataSource).setSsids(eq(ssidSet)); } @@ -199,6 +176,6 @@ public class SsidSetStoreDataTest { + "" + TEST_SSID2 + "\n" + "" + "badInput" + "" // Unknown tag. + "\n"; - deserializeData(ssidSet.getBytes(StandardCharsets.UTF_8), false /* shared */); + deserializeData(ssidSet.getBytes(StandardCharsets.UTF_8)); } } diff --git a/tests/wifitests/src/com/android/server/wifi/WakeupConfigStoreDataTest.java b/tests/wifitests/src/com/android/server/wifi/WakeupConfigStoreDataTest.java index 33f1b42fe..cfb0e1c80 100644 --- a/tests/wifitests/src/com/android/server/wifi/WakeupConfigStoreDataTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WakeupConfigStoreDataTest.java @@ -16,6 +16,7 @@ package com.android.server.wifi; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.eq; @@ -34,7 +35,6 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import java.io.ByteArrayInputStream; @@ -67,14 +67,13 @@ public class WakeupConfigStoreDataTest { /** * Helper function for serializing configuration data to a XML block. * - * @param shared Flag indicating serializing shared or user configurations * @return byte[] of the XML data */ - private byte[] serializeData(boolean shared) throws Exception { + private byte[] serializeData() throws Exception { final XmlSerializer out = new FastXmlSerializer(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); out.setOutput(outputStream, StandardCharsets.UTF_8.name()); - mWakeupConfigData.serializeData(out, shared); + mWakeupConfigData.serializeData(out); out.flush(); return outputStream.toByteArray(); } @@ -83,31 +82,12 @@ public class WakeupConfigStoreDataTest { * Helper function for parsing configuration data from a XML block. * * @param data XML data to parse from - * @param shared Flag indicating parsing of shared or user configurations */ - private void deserializeData(byte[] data, boolean shared) throws Exception { + private void deserializeData(byte[] data) throws Exception { final XmlPullParser in = Xml.newPullParser(); final ByteArrayInputStream inputStream = new ByteArrayInputStream(data); in.setInput(inputStream, StandardCharsets.UTF_8.name()); - mWakeupConfigData.deserializeData(in, in.getDepth(), shared); - } - - /** - * Verify that a XmlPullParserException will be thrown when attempting to serialize data - * to the share store. - */ - @Test(expected = XmlPullParserException.class) - public void serializeShareDataThrowsException() throws Exception { - serializeData(true /* shared */); - } - - /** - * Verify that a XmlPullParserException will be thrown when attempting to deserialize - * data from the share store. - */ - @Test(expected = XmlPullParserException.class) - public void deserializeShareDataThrowsException() throws Exception { - deserializeData(new byte[0], true /* shared */); + mWakeupConfigData.deserializeData(in, in.getDepth()); } /** @@ -125,8 +105,8 @@ public class WakeupConfigStoreDataTest { when(mNotificationsDataSource.getData()).thenReturn(notificationsShown); when(mNetworkDataSource.getData()).thenReturn(networks); - byte[] bytes = serializeData(false /* shared */); - deserializeData(bytes, false /* shared */); + byte[] bytes = serializeData(); + deserializeData(bytes); verify(mActiveDataSource).setData(eq(isActive)); verify(mIsOnboardedDataSource).setData(eq(isOnboarded)); @@ -161,8 +141,8 @@ public class WakeupConfigStoreDataTest { when(mNotificationsDataSource.getData()).thenReturn(notificationsShown); when(mNetworkDataSource.getData()).thenReturn(networks); - byte[] bytes = serializeData(false /* shared */); - deserializeData(bytes, false /* shared */); + byte[] bytes = serializeData(); + deserializeData(bytes); verify(mActiveDataSource).setData(eq(isActive)); verify(mIsOnboardedDataSource).setData(eq(isOnboarded)); @@ -175,7 +155,7 @@ public class WakeupConfigStoreDataTest { */ @Test public void resetDataWipesDataSources() { - mWakeupConfigData.resetData(false /* shared */); + mWakeupConfigData.resetData(); verify(mActiveDataSource).setData(false); verify(mIsOnboardedDataSource).setData(false); @@ -196,16 +176,19 @@ public class WakeupConfigStoreDataTest { */ @Test public void hasBeenReadIsTrueWhenUserStoreIsLoaded() throws Exception { - mWakeupConfigData.deserializeData(null /* in */, 0 /* outerTagDepth */, false /* shared */); + mWakeupConfigData.deserializeData(null /* in */, 0 /* outerTagDepth */); assertTrue(mWakeupConfigData.hasBeenRead()); } /** - * Verify that hasBeenRead returns false if only the shared store has been read. + * Verify that WakeUpConfigStoreData is written to + * {@link WifiConfigStore#STORE_FILE_NAME_USER_GENERAL}. + * + * @throws Exception */ @Test - public void hasBeenReadIsFalseWhenSharedStoreIsLoaded() throws Exception { - mWakeupConfigData.deserializeData(null /* in */, 0 /* outerTagDepth */, true /* shared */); - assertFalse(mWakeupConfigData.hasBeenRead()); + public void getUserStoreFileId() throws Exception { + assertEquals(WifiConfigStore.STORE_FILE_USER_GENERAL, + mWakeupConfigData.getStoreFileId()); } } diff --git a/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java b/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java index 3734ba786..4d8f03481 100644 --- a/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WakeupControllerTest.java @@ -142,7 +142,7 @@ public class WakeupControllerTest { private void readUserStore() { try { - mWakeupConfigStoreData.deserializeData(null, 0, false); + mWakeupConfigStoreData.deserializeData(null, 0); } catch (XmlPullParserException | IOException e) { // unreachable } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java index 6fd39a4a5..40f9f459d 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerTest.java @@ -109,7 +109,8 @@ public class WifiConfigManagerTest { @Mock private DevicePolicyManagerInternal mDevicePolicyManagerInternal; @Mock private WifiPermissionsUtil mWifiPermissionsUtil; @Mock private WifiPermissionsWrapper mWifiPermissionsWrapper; - @Mock private NetworkListStoreData mNetworkListStoreData; + @Mock private NetworkListSharedStoreData mNetworkListSharedStoreData; + @Mock private NetworkListUserStoreData mNetworkListUserStoreData; @Mock private DeletedEphemeralSsidsStoreData mDeletedEphemeralSsidsStoreData; @Mock private WifiConfigManager.OnSavedNetworkUpdateListener mWcmListener; @@ -129,7 +130,8 @@ public class WifiConfigManagerTest { // Set up the inorder for verifications. This is needed to verify that the broadcasts, // store writes for network updates followed by network additions are in the expected order. mContextConfigStoreMockOrder = inOrder(mContext, mWifiConfigStore); - mNetworkListStoreDataMockOrder = inOrder(mNetworkListStoreData); + mNetworkListStoreDataMockOrder = + inOrder(mNetworkListSharedStoreData, mNetworkListUserStoreData); // Set up the package name stuff & permission override. when(mContext.getPackageManager()).thenReturn(mPackageManager); @@ -2177,7 +2179,7 @@ public class WifiConfigManagerTest { // Now switch the user to user 2 and ensure that shared network's IDs have not changed. when(mUserManager.isUserUnlockingOrUnlocked(user2)).thenReturn(true); mWifiConfigManager.handleUserSwitch(user2); - verify(mWifiConfigStore).switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + verify(mWifiConfigStore).switchUserStoresAndRead(any(List.class)); // Again fetch the network ID's assigned to the shared networks and ensure they have not // changed. @@ -2254,7 +2256,7 @@ public class WifiConfigManagerTest { // Now switch the user to user 2 and ensure that user 1's private network has been removed. when(mUserManager.isUserUnlockingOrUnlocked(user2)).thenReturn(true); Set removedNetworks = mWifiConfigManager.handleUserSwitch(user2); - verify(mWifiConfigStore).switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + verify(mWifiConfigStore).switchUserStoresAndRead(any(List.class)); assertTrue((removedNetworks.size() == 1) && (removedNetworks.contains(user1NetworkId))); // Set the expected networks to be |sharedNetwork| and |user2Network|. @@ -2316,7 +2318,7 @@ public class WifiConfigManagerTest { // Now switch the user to user 2 and ensure that no private network has been removed. when(mUserManager.isUserUnlockingOrUnlocked(user2)).thenReturn(true); Set removedNetworks = mWifiConfigManager.handleUserSwitch(user2); - verify(mWifiConfigStore).switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + verify(mWifiConfigStore).switchUserStoresAndRead(any(List.class)); assertTrue(removedNetworks.isEmpty()); } @@ -2367,7 +2369,7 @@ public class WifiConfigManagerTest { }; setupStoreDataForUserRead(userNetworks, new HashSet()); mWifiConfigManager.handleUserUnlock(user1); - verify(mWifiConfigStore).switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + verify(mWifiConfigStore).switchUserStoresAndRead(any(List.class)); // Capture the written data for the user 1 and ensure that it corresponds to what was // setup. Pair, List> writtenNetworkList = @@ -2446,7 +2448,7 @@ public class WifiConfigManagerTest { // the configured networks (migrated to PasspointManager). setupStoreDataForUserRead(new ArrayList(), new HashSet()); mWifiConfigManager.handleUserUnlock(user1); - verify(mWifiConfigStore).switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + verify(mWifiConfigStore).switchUserStoresAndRead(any(List.class)); Pair, List> writtenNetworkList = captureWriteNetworksListStoreData(); assertTrue(writtenNetworkList.first.isEmpty()); @@ -2474,7 +2476,7 @@ public class WifiConfigManagerTest { mWifiConfigManager.handleUserSwitch(user2); // Ensure that the read was invoked. mContextConfigStoreMockOrder.verify(mWifiConfigStore) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); } /** @@ -2497,18 +2499,18 @@ public class WifiConfigManagerTest { // Ensure that the read was not invoked. mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); // Now try unlocking some other user (user1), this should be ignored. mWifiConfigManager.handleUserUnlock(user1); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); setupStoreDataForUserRead(new ArrayList(), new HashSet()); // Unlock the user2 and ensure that we read the data now. mWifiConfigManager.handleUserUnlock(user2); mContextConfigStoreMockOrder.verify(mWifiConfigStore) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); } /** @@ -2528,12 +2530,12 @@ public class WifiConfigManagerTest { when(mUserManager.isUserUnlockingOrUnlocked(user2)).thenReturn(false); mWifiConfigManager.handleUserStop(user2); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); // Now try stopping the foreground user1, this should trigger a write to store. mWifiConfigManager.handleUserStop(user1); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); mContextConfigStoreMockOrder.verify(mWifiConfigStore).write(anyBoolean()); } @@ -2593,14 +2595,14 @@ public class WifiConfigManagerTest { mContextConfigStoreMockOrder.verify(mWifiConfigStore).read(); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()).write(anyBoolean()); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); setupStoreDataForUserRead(new ArrayList(), new HashSet()); // Unlock the user1 (default user) for the first time and ensure that we read the data. mWifiConfigManager.handleUserUnlock(user1); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()).read(); mContextConfigStoreMockOrder.verify(mWifiConfigStore) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); mContextConfigStoreMockOrder.verify(mWifiConfigStore).write(anyBoolean()); } @@ -2619,13 +2621,13 @@ public class WifiConfigManagerTest { mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()).read(); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()).write(anyBoolean()); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); setupStoreDataForUserRead(new ArrayList(), new HashSet()); // Read from store now. assertTrue(mWifiConfigManager.loadFromStore()); mContextConfigStoreMockOrder.verify(mWifiConfigStore) - .setUserStore(any(WifiConfigStore.StoreFile.class)); + .setUserStores(any(List.class)); mContextConfigStoreMockOrder.verify(mWifiConfigStore).read(); } @@ -2646,7 +2648,7 @@ public class WifiConfigManagerTest { mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()).read(); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()).write(anyBoolean()); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); // Now load from the store. assertTrue(mWifiConfigManager.loadFromStore()); @@ -2656,7 +2658,7 @@ public class WifiConfigManagerTest { setupStoreDataForUserRead(new ArrayList<>(), new HashSet<>()); mWifiConfigManager.handleUserUnlock(user2); mContextConfigStoreMockOrder.verify(mWifiConfigStore) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); } /** @@ -2678,12 +2680,12 @@ public class WifiConfigManagerTest { mWifiConfigManager.handleUserSwitch(user2); // Ensure that the read was invoked. mContextConfigStoreMockOrder.verify(mWifiConfigStore) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); // Unlock the user2 again and ensure that we don't read the data now. mWifiConfigManager.handleUserUnlock(user2); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); } /** @@ -2699,7 +2701,7 @@ public class WifiConfigManagerTest { when(mUserManager.isUserUnlockingOrUnlocked(user2)).thenReturn(false); mWifiConfigManager.handleUserSwitch(user2); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()).write(anyBoolean()); } @@ -2715,7 +2717,7 @@ public class WifiConfigManagerTest { // write the store files. mWifiConfigManager.handleUserUnlock(user1); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); mContextConfigStoreMockOrder.verify(mWifiConfigStore, never()).write(anyBoolean()); } @@ -2863,7 +2865,7 @@ public class WifiConfigManagerTest { mWifiConfigManager.handleUserSwitch(user2); // Ensure that the read was invoked. mContextConfigStoreMockOrder.verify(mWifiConfigStore) - .switchUserStoreAndRead(any(WifiConfigStore.StoreFile.class)); + .switchUserStoresAndRead(any(List.class)); } /** @@ -3753,8 +3755,8 @@ public class WifiConfigManagerTest { new WifiConfigManager( mContext, mClock, mUserManager, mTelephonyManager, mWifiKeyStore, mWifiConfigStore, mWifiConfigStoreLegacy, - mWifiPermissionsUtil, mWifiPermissionsWrapper, mNetworkListStoreData, - mDeletedEphemeralSsidsStoreData); + mWifiPermissionsUtil, mWifiPermissionsWrapper, mNetworkListSharedStoreData, + mNetworkListUserStoreData, mDeletedEphemeralSsidsStoreData); mWifiConfigManager.enableVerboseLogging(1); } @@ -3889,10 +3891,10 @@ public class WifiConfigManagerTest { ArgumentCaptor.forClass(ArrayList.class); ArgumentCaptor userConfigsCaptor = ArgumentCaptor.forClass(ArrayList.class); - mNetworkListStoreDataMockOrder.verify(mNetworkListStoreData) - .setSharedConfigurations(sharedConfigsCaptor.capture()); - mNetworkListStoreDataMockOrder.verify(mNetworkListStoreData) - .setUserConfigurations(userConfigsCaptor.capture()); + mNetworkListStoreDataMockOrder.verify(mNetworkListSharedStoreData) + .setConfigurations(sharedConfigsCaptor.capture()); + mNetworkListStoreDataMockOrder.verify(mNetworkListUserStoreData) + .setConfigurations(userConfigsCaptor.capture()); mContextConfigStoreMockOrder.verify(mWifiConfigStore).write(anyBoolean()); return Pair.create(sharedConfigsCaptor.getValue(), userConfigsCaptor.getValue()); } catch (Exception e) { @@ -3937,19 +3939,19 @@ public class WifiConfigManagerTest { */ private void setupStoreDataForRead(List sharedConfigurations, List userConfigurations, Set deletedEphemeralSsids) { - when(mNetworkListStoreData.getSharedConfigurations()) + when(mNetworkListSharedStoreData.getConfigurations()) .thenReturn(sharedConfigurations); - when(mNetworkListStoreData.getUserConfigurations()).thenReturn(userConfigurations); + when(mNetworkListUserStoreData.getConfigurations()).thenReturn(userConfigurations); when(mDeletedEphemeralSsidsStoreData.getSsidList()).thenReturn(deletedEphemeralSsids); } /** * Setup expectations for WifiNetworksListStoreData and DeletedEphemeralSsidsStoreData - * after WifiConfigStore#switchUserStoreAndRead. + * after WifiConfigStore#switchUserStoresAndRead. */ private void setupStoreDataForUserRead(List userConfigurations, Set deletedEphemeralSsids) { - when(mNetworkListStoreData.getUserConfigurations()).thenReturn(userConfigurations); + when(mNetworkListUserStoreData.getConfigurations()).thenReturn(userConfigurations); when(mDeletedEphemeralSsidsStoreData.getSsidList()).thenReturn(deletedEphemeralSsids); } @@ -4076,7 +4078,8 @@ public class WifiConfigManagerTest { */ private NetworkUpdateResult addNetworkToWifiConfigManager(WifiConfiguration configuration, int uid) { - clearInvocations(mContext, mWifiConfigStore, mNetworkListStoreData); + clearInvocations(mContext, mWifiConfigStore, mNetworkListSharedStoreData, + mNetworkListUserStoreData); triggerStoreReadIfNeeded(); when(mClock.getWallClockMillis()).thenReturn(TEST_WALLCLOCK_CREATION_TIME_MILLIS); NetworkUpdateResult result = @@ -4148,7 +4151,8 @@ public class WifiConfigManagerTest { * to modify the configuration before we compare the added network with the retrieved network. */ private NetworkUpdateResult updateNetworkToWifiConfigManager(WifiConfiguration configuration) { - clearInvocations(mContext, mWifiConfigStore, mNetworkListStoreData); + clearInvocations(mContext, mWifiConfigStore, mNetworkListSharedStoreData, + mNetworkListUserStoreData); when(mClock.getWallClockMillis()).thenReturn(TEST_WALLCLOCK_UPDATE_TIME_MILLIS); NetworkUpdateResult result = mWifiConfigManager.addOrUpdateNetwork(configuration, TEST_UPDATE_UID); diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigSharedStoreDataTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigSharedStoreDataTest.java new file mode 100644 index 000000000..6c2ab7bd5 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigSharedStoreDataTest.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi.hotspot2; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import android.support.test.filters.SmallTest; +import android.util.Xml; + +import com.android.internal.util.FastXmlSerializer; +import com.android.server.wifi.WifiConfigStore; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlSerializer; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.PasspointConfigSharedStoreData}. + */ +@SmallTest +public class PasspointConfigSharedStoreDataTest { + @Mock PasspointConfigSharedStoreData.DataSource mDataSource; + PasspointConfigSharedStoreData mConfigStoreData; + + /** Sets up test. */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + mConfigStoreData = new PasspointConfigSharedStoreData(mDataSource); + } + + /** + * Helper function for serializing store data to a XML block. + * + * @return byte[] + * @throws Exception + */ + private byte[] serializeData() throws Exception { + final XmlSerializer out = new FastXmlSerializer(); + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + out.setOutput(outputStream, StandardCharsets.UTF_8.name()); + mConfigStoreData.serializeData(out); + out.flush(); + return outputStream.toByteArray(); + } + + /** + * Helper function for deserializing store data from a XML block. + * + * @param data The XML block data bytes + * @throws Exception + */ + private void deserializeData(byte[] data) throws Exception { + final XmlPullParser in = Xml.newPullParser(); + final ByteArrayInputStream inputStream = new ByteArrayInputStream(data); + in.setInput(inputStream, StandardCharsets.UTF_8.name()); + mConfigStoreData.deserializeData(in, in.getDepth()); + } + + /** + * Verify that the serialization and deserialization of share store data works as expected. + * The data used for serialization matches the result of the deserialization. + * + * @throws Exception + */ + @Test + public void serializeAndDeserializeShareStoreData() throws Exception { + // Setup expected data. + long providerIndex = 412; + + // Serialize data for share store. + when(mDataSource.getProviderIndex()).thenReturn(providerIndex); + byte[] data = serializeData(); + + // Deserialize data for share store and verify the content. + deserializeData(data); + verify(mDataSource).setProviderIndex(providerIndex); + } + + /** + * Verify that deserialization of an empty share store data doesn't cause any exception + * and the corresponding data source should not be updated. + * + * @throws Exception + */ + @Test + public void deserializeEmptyShareStoreData() throws Exception { + deserializeData(new byte[0]); + verify(mDataSource, never()).setProviderIndex(anyLong()); + } + + /** + * Verify that PasspointConfigUserStoreData is written to + * {@link WifiConfigStore#STORE_FILE_NAME_SHARED_GENERAL}. + * + * @throws Exception + */ + @Test + public void getUserStoreFileId() throws Exception { + assertEquals(WifiConfigStore.STORE_FILE_SHARED_GENERAL, + mConfigStoreData.getStoreFileId()); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigStoreDataTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigStoreDataTest.java deleted file mode 100644 index 35f80a1f2..000000000 --- a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigStoreDataTest.java +++ /dev/null @@ -1,299 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.server.wifi.hotspot2; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - -import android.net.wifi.hotspot2.PasspointConfiguration; -import android.net.wifi.hotspot2.pps.Credential; -import android.net.wifi.hotspot2.pps.HomeSp; -import android.net.wifi.hotspot2.pps.Policy; -import android.net.wifi.hotspot2.pps.UpdateParameter; -import android.support.test.filters.SmallTest; -import android.util.Xml; - -import com.android.internal.util.FastXmlSerializer; -import com.android.server.wifi.SIMAccessor; -import com.android.server.wifi.WifiKeyStore; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlSerializer; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.nio.charset.StandardCharsets; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Unit tests for {@link com.android.server.wifi.hotspot2.PasspointConfigStoreData}. - */ -@SmallTest -public class PasspointConfigStoreDataTest { - private static final String TEST_CA_CERTIFICATE_ALIAS = "CaCert"; - private static final String TEST_CLIENT_CERTIFICATE_ALIAS = "ClientCert"; - private static final String TEST_CLIENT_PRIVATE_KEY_ALIAS = "ClientPrivateKey"; - private static final long TEST_PROVIDER_ID = 1; - private static final int TEST_CREATOR_UID = 1234; - private static final boolean TEST_HAS_EVER_CONNECTED = true; - private static final boolean TEST_SHARED = false; - - @Mock WifiKeyStore mKeyStore; - @Mock SIMAccessor mSimAccessor; - @Mock PasspointConfigStoreData.DataSource mDataSource; - PasspointConfigStoreData mConfigStoreData; - - /** Sets up test. */ - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - mConfigStoreData = new PasspointConfigStoreData(mKeyStore, mSimAccessor, mDataSource); - } - - /** - * Helper function for generating a {@link PasspointConfiguration} for testing the XML - * serialization/deserialization logic. - * - * @return {@link PasspointConfiguration} - * @throws Exception - */ - private PasspointConfiguration createFullPasspointConfiguration() throws Exception { - DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); - byte[] certFingerprint = new byte[32]; - Arrays.fill(certFingerprint, (byte) 0x1f); - - PasspointConfiguration config = new PasspointConfiguration(); - config.setUpdateIdentifier(12); - config.setCredentialPriority(99); - - // AAA Server trust root. - Map trustRootCertList = new HashMap<>(); - trustRootCertList.put("server1.trust.root.com", certFingerprint); - config.setTrustRootCertList(trustRootCertList); - - // Subscription update. - UpdateParameter subscriptionUpdate = new UpdateParameter(); - subscriptionUpdate.setUpdateIntervalInMinutes(120); - subscriptionUpdate.setUpdateMethod(UpdateParameter.UPDATE_METHOD_SSP); - subscriptionUpdate.setRestriction(UpdateParameter.UPDATE_RESTRICTION_ROAMING_PARTNER); - subscriptionUpdate.setServerUri("subscription.update.com"); - subscriptionUpdate.setUsername("subscriptionUser"); - subscriptionUpdate.setBase64EncodedPassword("subscriptionPass"); - subscriptionUpdate.setTrustRootCertUrl("subscription.update.cert.com"); - subscriptionUpdate.setTrustRootCertSha256Fingerprint(certFingerprint); - config.setSubscriptionUpdate(subscriptionUpdate); - - // Subscription parameters. - config.setSubscriptionCreationTimeInMillis(format.parse("2016-02-01T10:00:00Z").getTime()); - config.setSubscriptionExpirationTimeInMillis( - format.parse("2016-03-01T10:00:00Z").getTime()); - config.setSubscriptionType("Gold"); - config.setUsageLimitDataLimit(921890); - config.setUsageLimitStartTimeInMillis(format.parse("2016-12-01T10:00:00Z").getTime()); - config.setUsageLimitTimeLimitInMinutes(120); - config.setUsageLimitUsageTimePeriodInMinutes(99910); - - // HomeSP configuration. - HomeSp homeSp = new HomeSp(); - homeSp.setFriendlyName("Century House"); - homeSp.setFqdn("mi6.co.uk"); - homeSp.setRoamingConsortiumOis(new long[] {0x112233L, 0x445566L}); - homeSp.setIconUrl("icon.test.com"); - Map homeNetworkIds = new HashMap<>(); - homeNetworkIds.put("TestSSID", 0x12345678L); - homeNetworkIds.put("NullHESSID", null); - homeSp.setHomeNetworkIds(homeNetworkIds); - homeSp.setMatchAllOis(new long[] {0x11223344}); - homeSp.setMatchAnyOis(new long[] {0x55667788}); - homeSp.setOtherHomePartners(new String[] {"other.fqdn.com"}); - config.setHomeSp(homeSp); - - // Credential configuration. - Credential credential = new Credential(); - credential.setCreationTimeInMillis(format.parse("2016-01-01T10:00:00Z").getTime()); - credential.setExpirationTimeInMillis(format.parse("2016-02-01T10:00:00Z").getTime()); - credential.setRealm("shaken.stirred.com"); - credential.setCheckAaaServerCertStatus(true); - Credential.UserCredential userCredential = new Credential.UserCredential(); - userCredential.setUsername("james"); - userCredential.setPassword("Ym9uZDAwNw=="); - userCredential.setMachineManaged(true); - userCredential.setSoftTokenApp("TestApp"); - userCredential.setAbleToShare(true); - userCredential.setEapType(21); - userCredential.setNonEapInnerMethod("MS-CHAP-V2"); - credential.setUserCredential(userCredential); - Credential.CertificateCredential certCredential = new Credential.CertificateCredential(); - certCredential.setCertType("x509v3"); - certCredential.setCertSha256Fingerprint(certFingerprint); - credential.setCertCredential(certCredential); - Credential.SimCredential simCredential = new Credential.SimCredential(); - simCredential.setImsi("imsi"); - simCredential.setEapType(24); - credential.setSimCredential(simCredential); - config.setCredential(credential); - - // Policy configuration. - Policy policy = new Policy(); - List preferredRoamingPartnerList = new ArrayList<>(); - Policy.RoamingPartner partner1 = new Policy.RoamingPartner(); - partner1.setFqdn("test1.fqdn.com"); - partner1.setFqdnExactMatch(true); - partner1.setPriority(127); - partner1.setCountries("us,fr"); - Policy.RoamingPartner partner2 = new Policy.RoamingPartner(); - partner2.setFqdn("test2.fqdn.com"); - partner2.setFqdnExactMatch(false); - partner2.setPriority(200); - partner2.setCountries("*"); - preferredRoamingPartnerList.add(partner1); - preferredRoamingPartnerList.add(partner2); - policy.setPreferredRoamingPartnerList(preferredRoamingPartnerList); - policy.setMinHomeDownlinkBandwidth(23412); - policy.setMinHomeUplinkBandwidth(9823); - policy.setMinRoamingDownlinkBandwidth(9271); - policy.setMinRoamingUplinkBandwidth(2315); - policy.setExcludedSsidList(new String[] {"excludeSSID"}); - Map requiredProtoPortMap = new HashMap<>(); - requiredProtoPortMap.put(12, "34,92,234"); - policy.setRequiredProtoPortMap(requiredProtoPortMap); - policy.setMaximumBssLoadValue(23); - UpdateParameter policyUpdate = new UpdateParameter(); - policyUpdate.setUpdateIntervalInMinutes(120); - policyUpdate.setUpdateMethod(UpdateParameter.UPDATE_METHOD_OMADM); - policyUpdate.setRestriction(UpdateParameter.UPDATE_RESTRICTION_HOMESP); - policyUpdate.setServerUri("policy.update.com"); - policyUpdate.setUsername("updateUser"); - policyUpdate.setBase64EncodedPassword("updatePass"); - policyUpdate.setTrustRootCertUrl("update.cert.com"); - policyUpdate.setTrustRootCertSha256Fingerprint(certFingerprint); - policy.setPolicyUpdate(policyUpdate); - config.setPolicy(policy); - return config; - } - - /** - * Helper function for serializing store data to a XML block. - * - * @param share Flag indicating share or user data - * @return byte[] - * @throws Exception - */ - private byte[] serializeData(boolean share) throws Exception { - final XmlSerializer out = new FastXmlSerializer(); - final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - out.setOutput(outputStream, StandardCharsets.UTF_8.name()); - mConfigStoreData.serializeData(out, share); - out.flush(); - return outputStream.toByteArray(); - } - - /** - * Helper function for deserializing store data from a XML block. - * - * @param data The XML block data bytes - * @param share Flag indicating share or user data - * @throws Exception - */ - private void deserializeData(byte[] data, boolean share) throws Exception { - final XmlPullParser in = Xml.newPullParser(); - final ByteArrayInputStream inputStream = new ByteArrayInputStream(data); - in.setInput(inputStream, StandardCharsets.UTF_8.name()); - mConfigStoreData.deserializeData(in, in.getDepth(), share); - } - - /** - * Verify that the serialization and deserialization of user store data works as expected. - * The data used for serialization matches the result of the deserialization. - * - * @throws Exception - */ - @Test - public void serializeAndDeserializeUserStoreData() throws Exception { - // Setup expected data. - List providerList = new ArrayList<>(); - providerList.add(new PasspointProvider(createFullPasspointConfiguration(), - mKeyStore, mSimAccessor, TEST_PROVIDER_ID, TEST_CREATOR_UID, - TEST_CA_CERTIFICATE_ALIAS, TEST_CLIENT_CERTIFICATE_ALIAS, - TEST_CLIENT_PRIVATE_KEY_ALIAS, TEST_HAS_EVER_CONNECTED, TEST_SHARED)); - - // Serialize data for user store. - when(mDataSource.getProviders()).thenReturn(providerList); - byte[] data = serializeData(false); - - // Deserialize data for user store and verify the content. - ArgumentCaptor providersCaptor = ArgumentCaptor.forClass(ArrayList.class); - deserializeData(data, false); - verify(mDataSource).setProviders(providersCaptor.capture()); - assertEquals(providerList, providersCaptor.getValue()); - } - - /** - * Verify that the serialization and deserialization of share store data works as expected. - * The data used for serialization matches the result of the deserialization. - * - * @throws Exception - */ - @Test - public void serializeAndDeserializeShareStoreData() throws Exception { - // Setup expected data. - long providerIndex = 412; - - // Serialize data for share store. - when(mDataSource.getProviderIndex()).thenReturn(providerIndex); - byte[] data = serializeData(true); - - // Deserialize data for share store and verify the content. - deserializeData(data, true); - verify(mDataSource).setProviderIndex(providerIndex); - } - - /** - * Verify that deserialization of an empty user store data doesn't cause any exception and - * the corresponding data source should not be updated. - * - * @throws Exception - */ - @Test - public void deserializeEmptyUserStoreData() throws Exception { - deserializeData(new byte[0], false); - verify(mDataSource, never()).setProviders(any(ArrayList.class)); - } - - /** - * Verify that deserialization of an empty share store data doesn't cause any exception - * and the corresponding data source should not be updated. - * - * @throws Exception - */ - @Test - public void deserializeEmptyShareStoreData() throws Exception { - deserializeData(new byte[0], true); - verify(mDataSource, never()).setProviderIndex(anyLong()); - } -} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigUserStoreDataTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigUserStoreDataTest.java new file mode 100644 index 000000000..7ab29c972 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointConfigUserStoreDataTest.java @@ -0,0 +1,278 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi.hotspot2; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import android.net.wifi.hotspot2.PasspointConfiguration; +import android.net.wifi.hotspot2.pps.Credential; +import android.net.wifi.hotspot2.pps.HomeSp; +import android.net.wifi.hotspot2.pps.Policy; +import android.net.wifi.hotspot2.pps.UpdateParameter; +import android.support.test.filters.SmallTest; +import android.util.Xml; + +import com.android.internal.util.FastXmlSerializer; +import com.android.server.wifi.SIMAccessor; +import com.android.server.wifi.WifiConfigStore; +import com.android.server.wifi.WifiKeyStore; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlSerializer; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.PasspointConfigUserStoreData}. + */ +@SmallTest +public class PasspointConfigUserStoreDataTest { + private static final String TEST_CA_CERTIFICATE_ALIAS = "CaCert"; + private static final String TEST_CLIENT_CERTIFICATE_ALIAS = "ClientCert"; + private static final String TEST_CLIENT_PRIVATE_KEY_ALIAS = "ClientPrivateKey"; + private static final long TEST_PROVIDER_ID = 1; + private static final int TEST_CREATOR_UID = 1234; + private static final boolean TEST_HAS_EVER_CONNECTED = true; + private static final boolean TEST_SHARED = false; + + @Mock WifiKeyStore mKeyStore; + @Mock SIMAccessor mSimAccessor; + @Mock PasspointConfigUserStoreData.DataSource mDataSource; + PasspointConfigUserStoreData mConfigStoreData; + + /** Sets up test. */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + mConfigStoreData = new PasspointConfigUserStoreData(mKeyStore, mSimAccessor, mDataSource); + } + + /** + * Helper function for generating a {@link PasspointConfiguration} for testing the XML + * serialization/deserialization logic. + * + * @return {@link PasspointConfiguration} + * @throws Exception + */ + private PasspointConfiguration createFullPasspointConfiguration() throws Exception { + DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); + byte[] certFingerprint = new byte[32]; + Arrays.fill(certFingerprint, (byte) 0x1f); + + PasspointConfiguration config = new PasspointConfiguration(); + config.setUpdateIdentifier(12); + config.setCredentialPriority(99); + + // AAA Server trust root. + Map trustRootCertList = new HashMap<>(); + trustRootCertList.put("server1.trust.root.com", certFingerprint); + config.setTrustRootCertList(trustRootCertList); + + // Subscription update. + UpdateParameter subscriptionUpdate = new UpdateParameter(); + subscriptionUpdate.setUpdateIntervalInMinutes(120); + subscriptionUpdate.setUpdateMethod(UpdateParameter.UPDATE_METHOD_SSP); + subscriptionUpdate.setRestriction(UpdateParameter.UPDATE_RESTRICTION_ROAMING_PARTNER); + subscriptionUpdate.setServerUri("subscription.update.com"); + subscriptionUpdate.setUsername("subscriptionUser"); + subscriptionUpdate.setBase64EncodedPassword("subscriptionPass"); + subscriptionUpdate.setTrustRootCertUrl("subscription.update.cert.com"); + subscriptionUpdate.setTrustRootCertSha256Fingerprint(certFingerprint); + config.setSubscriptionUpdate(subscriptionUpdate); + + // Subscription parameters. + config.setSubscriptionCreationTimeInMillis(format.parse("2016-02-01T10:00:00Z").getTime()); + config.setSubscriptionExpirationTimeInMillis( + format.parse("2016-03-01T10:00:00Z").getTime()); + config.setSubscriptionType("Gold"); + config.setUsageLimitDataLimit(921890); + config.setUsageLimitStartTimeInMillis(format.parse("2016-12-01T10:00:00Z").getTime()); + config.setUsageLimitTimeLimitInMinutes(120); + config.setUsageLimitUsageTimePeriodInMinutes(99910); + + // HomeSP configuration. + HomeSp homeSp = new HomeSp(); + homeSp.setFriendlyName("Century House"); + homeSp.setFqdn("mi6.co.uk"); + homeSp.setRoamingConsortiumOis(new long[] {0x112233L, 0x445566L}); + homeSp.setIconUrl("icon.test.com"); + Map homeNetworkIds = new HashMap<>(); + homeNetworkIds.put("TestSSID", 0x12345678L); + homeNetworkIds.put("NullHESSID", null); + homeSp.setHomeNetworkIds(homeNetworkIds); + homeSp.setMatchAllOis(new long[] {0x11223344}); + homeSp.setMatchAnyOis(new long[] {0x55667788}); + homeSp.setOtherHomePartners(new String[] {"other.fqdn.com"}); + config.setHomeSp(homeSp); + + // Credential configuration. + Credential credential = new Credential(); + credential.setCreationTimeInMillis(format.parse("2016-01-01T10:00:00Z").getTime()); + credential.setExpirationTimeInMillis(format.parse("2016-02-01T10:00:00Z").getTime()); + credential.setRealm("shaken.stirred.com"); + credential.setCheckAaaServerCertStatus(true); + Credential.UserCredential userCredential = new Credential.UserCredential(); + userCredential.setUsername("james"); + userCredential.setPassword("Ym9uZDAwNw=="); + userCredential.setMachineManaged(true); + userCredential.setSoftTokenApp("TestApp"); + userCredential.setAbleToShare(true); + userCredential.setEapType(21); + userCredential.setNonEapInnerMethod("MS-CHAP-V2"); + credential.setUserCredential(userCredential); + Credential.CertificateCredential certCredential = new Credential.CertificateCredential(); + certCredential.setCertType("x509v3"); + certCredential.setCertSha256Fingerprint(certFingerprint); + credential.setCertCredential(certCredential); + Credential.SimCredential simCredential = new Credential.SimCredential(); + simCredential.setImsi("imsi"); + simCredential.setEapType(24); + credential.setSimCredential(simCredential); + config.setCredential(credential); + + // Policy configuration. + Policy policy = new Policy(); + List preferredRoamingPartnerList = new ArrayList<>(); + Policy.RoamingPartner partner1 = new Policy.RoamingPartner(); + partner1.setFqdn("test1.fqdn.com"); + partner1.setFqdnExactMatch(true); + partner1.setPriority(127); + partner1.setCountries("us,fr"); + Policy.RoamingPartner partner2 = new Policy.RoamingPartner(); + partner2.setFqdn("test2.fqdn.com"); + partner2.setFqdnExactMatch(false); + partner2.setPriority(200); + partner2.setCountries("*"); + preferredRoamingPartnerList.add(partner1); + preferredRoamingPartnerList.add(partner2); + policy.setPreferredRoamingPartnerList(preferredRoamingPartnerList); + policy.setMinHomeDownlinkBandwidth(23412); + policy.setMinHomeUplinkBandwidth(9823); + policy.setMinRoamingDownlinkBandwidth(9271); + policy.setMinRoamingUplinkBandwidth(2315); + policy.setExcludedSsidList(new String[] {"excludeSSID"}); + Map requiredProtoPortMap = new HashMap<>(); + requiredProtoPortMap.put(12, "34,92,234"); + policy.setRequiredProtoPortMap(requiredProtoPortMap); + policy.setMaximumBssLoadValue(23); + UpdateParameter policyUpdate = new UpdateParameter(); + policyUpdate.setUpdateIntervalInMinutes(120); + policyUpdate.setUpdateMethod(UpdateParameter.UPDATE_METHOD_OMADM); + policyUpdate.setRestriction(UpdateParameter.UPDATE_RESTRICTION_HOMESP); + policyUpdate.setServerUri("policy.update.com"); + policyUpdate.setUsername("updateUser"); + policyUpdate.setBase64EncodedPassword("updatePass"); + policyUpdate.setTrustRootCertUrl("update.cert.com"); + policyUpdate.setTrustRootCertSha256Fingerprint(certFingerprint); + policy.setPolicyUpdate(policyUpdate); + config.setPolicy(policy); + return config; + } + + /** + * Helper function for serializing store data to a XML block. + * + * @return byte[] + * @throws Exception + */ + private byte[] serializeData() throws Exception { + final XmlSerializer out = new FastXmlSerializer(); + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + out.setOutput(outputStream, StandardCharsets.UTF_8.name()); + mConfigStoreData.serializeData(out); + out.flush(); + return outputStream.toByteArray(); + } + + /** + * Helper function for deserializing store data from a XML block. + * + * @param data The XML block data bytes + * @throws Exception + */ + private void deserializeData(byte[] data) throws Exception { + final XmlPullParser in = Xml.newPullParser(); + final ByteArrayInputStream inputStream = new ByteArrayInputStream(data); + in.setInput(inputStream, StandardCharsets.UTF_8.name()); + mConfigStoreData.deserializeData(in, in.getDepth()); + } + + /** + * Verify that the serialization and deserialization of user store data works as expected. + * The data used for serialization matches the result of the deserialization. + * + * @throws Exception + */ + @Test + public void serializeAndDeserializeUserStoreData() throws Exception { + // Setup expected data. + List providerList = new ArrayList<>(); + providerList.add(new PasspointProvider(createFullPasspointConfiguration(), + mKeyStore, mSimAccessor, TEST_PROVIDER_ID, TEST_CREATOR_UID, + TEST_CA_CERTIFICATE_ALIAS, TEST_CLIENT_CERTIFICATE_ALIAS, + TEST_CLIENT_PRIVATE_KEY_ALIAS, TEST_HAS_EVER_CONNECTED, TEST_SHARED)); + + // Serialize data for user store. + when(mDataSource.getProviders()).thenReturn(providerList); + byte[] data = serializeData(); + + // Deserialize data for user store and verify the content. + ArgumentCaptor providersCaptor = ArgumentCaptor.forClass(ArrayList.class); + deserializeData(data); + verify(mDataSource).setProviders(providersCaptor.capture()); + assertEquals(providerList, providersCaptor.getValue()); + } + + /** + * Verify that deserialization of an empty user store data doesn't cause any exception and + * the corresponding data source should not be updated. + * + * @throws Exception + */ + @Test + public void deserializeEmptyUserStoreData() throws Exception { + deserializeData(new byte[0]); + verify(mDataSource, never()).setProviders(any(ArrayList.class)); + } + + /** + * Verify that PasspointConfigUserStoreData is written to + * {@link WifiConfigStore#STORE_FILE_NAME_USER_GENERAL}. + * + * @throws Exception + */ + @Test + public void getUserStoreFileId() throws Exception { + assertEquals(WifiConfigStore.STORE_FILE_USER_GENERAL, + mConfigStoreData.getStoreFileId()); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointManagerTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointManagerTest.java index 8d60bbbe9..415332964 100644 --- a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointManagerTest.java @@ -136,7 +136,8 @@ public class PasspointManagerTest { @Mock CertificateVerifier mCertVerifier; @Mock WifiConfigManager mWifiConfigManager; @Mock WifiConfigStore mWifiConfigStore; - @Mock PasspointConfigStoreData.DataSource mDataSource; + @Mock PasspointConfigSharedStoreData.DataSource mSharedDataSource; + @Mock PasspointConfigUserStoreData.DataSource mUserDataSource; @Mock WifiMetrics mWifiMetrics; @Mock OsuNetworkConnection mOsuNetworkConnection; @Mock OsuServerConnection mOsuServerConnection; @@ -170,12 +171,16 @@ public class PasspointManagerTest { ArgumentCaptor.forClass(PasspointEventHandler.Callbacks.class); verify(mObjectFactory).makePasspointEventHandler(any(WifiNative.class), callbacks.capture()); - ArgumentCaptor dataSource = - ArgumentCaptor.forClass(PasspointConfigStoreData.DataSource.class); - verify(mObjectFactory).makePasspointConfigStoreData( - any(WifiKeyStore.class), any(SIMAccessor.class), dataSource.capture()); + ArgumentCaptor sharedDataSource = + ArgumentCaptor.forClass(PasspointConfigSharedStoreData.DataSource.class); + verify(mObjectFactory).makePasspointConfigSharedStoreData(sharedDataSource.capture()); + ArgumentCaptor userDataSource = + ArgumentCaptor.forClass(PasspointConfigUserStoreData.DataSource.class); + verify(mObjectFactory).makePasspointConfigUserStoreData( + any(WifiKeyStore.class), any(SIMAccessor.class), userDataSource.capture()); mCallbacks = callbacks.getValue(); - mDataSource = dataSource.getValue(); + mSharedDataSource = sharedDataSource.getValue(); + mUserDataSource = userDataSource.getValue(); mLooper = new TestLooper(); } @@ -491,11 +496,11 @@ public class PasspointManagerTest { reset(mWifiConfigManager); // Verify content in the data source. - List providers = mDataSource.getProviders(); + List providers = mUserDataSource.getProviders(); assertEquals(1, providers.size()); assertEquals(config, providers.get(0).getConfig()); // Provider index start with 0, should be 1 after adding a provider. - assertEquals(1, mDataSource.getProviderIndex()); + assertEquals(1, mSharedDataSource.getProviderIndex()); // Remove the provider. assertTrue(mManager.removeProvider(TEST_FQDN)); @@ -506,9 +511,9 @@ public class PasspointManagerTest { assertTrue(mManager.getProviderConfigs().isEmpty()); // Verify content in the data source. - assertTrue(mDataSource.getProviders().isEmpty()); + assertTrue(mUserDataSource.getProviders().isEmpty()); // Removing a provider should not change the provider index. - assertEquals(1, mDataSource.getProviderIndex()); + assertEquals(1, mSharedDataSource.getProviderIndex()); } /** @@ -531,11 +536,11 @@ public class PasspointManagerTest { reset(mWifiConfigManager); // Verify content in the data source. - List providers = mDataSource.getProviders(); + List providers = mUserDataSource.getProviders(); assertEquals(1, providers.size()); assertEquals(config, providers.get(0).getConfig()); // Provider index start with 0, should be 1 after adding a provider. - assertEquals(1, mDataSource.getProviderIndex()); + assertEquals(1, mSharedDataSource.getProviderIndex()); // Remove the provider. assertTrue(mManager.removeProvider(TEST_FQDN)); @@ -546,9 +551,9 @@ public class PasspointManagerTest { assertTrue(mManager.getProviderConfigs().isEmpty()); // Verify content in the data source. - assertTrue(mDataSource.getProviders().isEmpty()); + assertTrue(mUserDataSource.getProviders().isEmpty()); // Removing a provider should not change the provider index. - assertEquals(1, mDataSource.getProviderIndex()); + assertEquals(1, mSharedDataSource.getProviderIndex()); } /** @@ -574,10 +579,10 @@ public class PasspointManagerTest { reset(mWifiConfigManager); // Verify data source content. - List origProviders = mDataSource.getProviders(); + List origProviders = mUserDataSource.getProviders(); assertEquals(1, origProviders.size()); assertEquals(origConfig, origProviders.get(0).getConfig()); - assertEquals(1, mDataSource.getProviderIndex()); + assertEquals(1, mSharedDataSource.getProviderIndex()); // Add another provider with the same base domain as the existing provider. // This should replace the existing provider with the new configuration. @@ -592,10 +597,10 @@ public class PasspointManagerTest { verify(mWifiMetrics).incrementNumPasspointProviderInstallSuccess(); // Verify data source content. - List newProviders = mDataSource.getProviders(); + List newProviders = mUserDataSource.getProviders(); assertEquals(1, newProviders.size()); assertEquals(newConfig, newProviders.get(0).getConfig()); - assertEquals(2, mDataSource.getProviderIndex()); + assertEquals(2, mSharedDataSource.getProviderIndex()); } /** @@ -1084,7 +1089,7 @@ public class PasspointManagerTest { PasspointProvider provider = createMockProvider(config); List providers = new ArrayList<>(); providers.add(provider); - mDataSource.setProviders(providers); + mUserDataSource.setProviders(providers); // Verify the providers maintained by PasspointManager. assertEquals(1, mManager.getProviderConfigs().size()); @@ -1100,8 +1105,8 @@ public class PasspointManagerTest { @Test public void verifyProviderIndexAfterDataSourceUpdate() throws Exception { long providerIndex = 9; - mDataSource.setProviderIndex(providerIndex); - assertEquals(providerIndex, mDataSource.getProviderIndex()); + mSharedDataSource.setProviderIndex(providerIndex); + assertEquals(providerIndex, mSharedDataSource.getProviderIndex()); // Add a provider. PasspointConfiguration config = createTestConfigWithUserCredential(TEST_FQDN); -- cgit v1.2.3