diff options
author | Roshan Pius <rpius@google.com> | 2020-03-19 01:26:25 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-03-19 01:26:25 +0000 |
commit | c5a7be7a8ff56a03a1bc15ae0b15dd8ad1091cca (patch) | |
tree | 9de410c191c0b028bafa0327cbee8fe474d4582d /tests | |
parent | 27d2604a9a7c5673ba9c8779400d8a660093d7a9 (diff) | |
parent | d9f6a328b3f97cdd365fc434cad43a1f222f2faf (diff) |
Merge "WifiSoftApConfigStore: Move softap.conf conversion to WifiMigration" into rvc-dev
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java | 131 | ||||
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiConfigStoreTest.java | 138 |
2 files changed, 141 insertions, 128 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java b/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java index 14cb6043a..54761e2be 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java @@ -24,7 +24,6 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.mockito.Mockito.any; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; @@ -36,15 +35,12 @@ import android.content.pm.ApplicationInfo; import android.net.MacAddress; import android.net.wifi.SoftApConfiguration; import android.net.wifi.SoftApConfiguration.Builder; -import android.net.wifi.WifiConfiguration; -import android.net.wifi.WifiConfiguration.KeyMgmt; import android.os.Build; import android.os.Handler; import android.os.test.TestLooper; import androidx.test.filters.SmallTest; -import com.android.server.wifi.util.ApConfigUtil; import com.android.wifi.resources.R; import org.junit.Before; @@ -53,12 +49,6 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import java.io.BufferedOutputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; @@ -71,11 +61,9 @@ public class WifiApConfigStoreTest extends WifiBaseTest { private static final String TAG = "WifiApConfigStoreTest"; - private static final String TEST_AP_CONFIG_FILE_PREFIX = "APConfig_"; private static final String TEST_DEFAULT_2G_CHANNEL_LIST = "1,2,3,4,5,6"; private static final String TEST_DEFAULT_AP_SSID = "TestAP"; private static final String TEST_DEFAULT_HOTSPOT_SSID = "TestShare"; - private static final String TEST_DEFAULT_HOTSPOT_PSK = "TestPassword"; private static final int RAND_SSID_INT_MIN = 1000; private static final int RAND_SSID_INT_MAX = 9999; private static final String TEST_CHAR_SET_AS_STRING = "abcdefghijklmnopqrstuvwxyz0123456789"; @@ -96,7 +84,6 @@ public class WifiApConfigStoreTest extends WifiBaseTest { @Mock private WifiConfigStore mWifiConfigStore; @Mock private WifiConfigManager mWifiConfigManager; @Mock private ActiveModeWarden mActiveModeWarden; - private File mLegacyApConfigFile; private Random mRandom; private MockResources mResources; @Mock private ApplicationInfo mMockApplInfo; @@ -136,19 +123,10 @@ public class WifiApConfigStoreTest extends WifiBaseTest { /** * Helper method to create and verify actions for the ApConfigStore used in the following tests. */ - private WifiApConfigStore createWifiApConfigStore(File legacyFile) throws Exception { - WifiApConfigStore store; - if (legacyFile == null) { - store = new WifiApConfigStore( - mContext, mWifiInjector, mHandler, mBackupManagerProxy, - mWifiConfigStore, mWifiConfigManager, mActiveModeWarden); - } else { - store = new WifiApConfigStore( - mContext, mWifiInjector, mHandler, mBackupManagerProxy, - mWifiConfigStore, mWifiConfigManager, mActiveModeWarden, - new FileInputStream(legacyFile)); - } - + private WifiApConfigStore createWifiApConfigStore() throws Exception { + WifiApConfigStore store = new WifiApConfigStore( + mContext, mWifiInjector, mHandler, mBackupManagerProxy, + mWifiConfigStore, mWifiConfigManager, mActiveModeWarden); verify(mWifiConfigStore).registerStoreData(any()); ArgumentCaptor<SoftApStoreData.DataSource> dataStoreSourceArgumentCaptor = ArgumentCaptor.forClass(SoftApStoreData.DataSource.class); @@ -158,10 +136,6 @@ public class WifiApConfigStoreTest extends WifiBaseTest { return store; } - private WifiApConfigStore createWifiApConfigStore() throws Exception { - return createWifiApConfigStore(null); - } - /** * Generate a SoftApConfiguration based on the specified parameters. */ @@ -180,62 +154,6 @@ public class WifiApConfigStoreTest extends WifiBaseTest { return configBuilder.build(); } - /** - * Generate a WifiConfiguration based on the specified parameters. - */ - private WifiConfiguration setupWifiConfigurationApConfig( - String ssid, String preSharedKey, int keyManagement, int band, int channel, - boolean hiddenSSID) { - WifiConfiguration config = new WifiConfiguration(); - config.SSID = ssid; - config.preSharedKey = preSharedKey; - config.allowedKeyManagement.set(keyManagement); - config.apBand = band; - config.apChannel = channel; - config.hiddenSSID = hiddenSSID; - return config; - } - - private void writeLegacyApConfigFile(WifiConfiguration config) throws Exception { - try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream( - new FileOutputStream(mLegacyApConfigFile)))) { - out.writeInt(WifiApConfigStore.AP_CONFIG_FILE_VERSION); - out.writeUTF(config.SSID); - out.writeInt(config.apBand); - out.writeInt(config.apChannel); - out.writeBoolean(config.hiddenSSID); - int authType = config.getAuthType(); - out.writeInt(authType); - if (authType != KeyMgmt.NONE) { - out.writeUTF(config.preSharedKey); - } - } catch (IOException e) { - fail("Error writing hotspot configuration" + e); - } - } - - /** - * Asserts that the WifiConfigurations equal to SoftApConfiguration. - * This only compares the elements saved - * for softAp used. - */ - public static void assertWifiConfigurationEqualSoftApConfiguration( - WifiConfiguration backup, SoftApConfiguration restore) { - assertEquals(backup.SSID, restore.getSsid()); - assertEquals(backup.BSSID, restore.getBssid()); - assertEquals(ApConfigUtil.convertWifiConfigBandToSoftApConfigBand(backup.apBand), - restore.getBand()); - assertEquals(backup.apChannel, restore.getChannel()); - assertEquals(backup.preSharedKey, restore.getPassphrase()); - int authType = backup.getAuthType(); - if (backup.getAuthType() == WifiConfiguration.KeyMgmt.WPA2_PSK) { - assertEquals(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK, restore.getSecurityType()); - } else { - assertEquals(SoftApConfiguration.SECURITY_TYPE_OPEN, restore.getSecurityType()); - } - assertEquals(backup.hiddenSSID, restore.isHiddenSsid()); - } - private void verifyApConfig(SoftApConfiguration config1, SoftApConfiguration config2) { assertEquals(config1.getSsid(), config2.getSsid()); assertEquals(config1.getPassphrase(), config2.getPassphrase()); @@ -299,47 +217,6 @@ public class WifiApConfigStoreTest extends WifiBaseTest { verify(mWifiConfigManager).saveToStore(true); } - /** - * Verify WifiApConfigStore can correctly load the existing configuration - * from the legacy config file and migrate it to the new config store. - */ - @Test - public void initWithExistingConfigurationInLegacyFile() throws Exception { - WifiConfiguration backupConfig = setupWifiConfigurationApConfig( - "ConfiguredAP", /* SSID */ - "randomKey", /* preshared key */ - KeyMgmt.WPA2_PSK, /* key management */ - 1, /* AP band (5GHz) */ - 40, /* AP channel */ - true /* Hidden SSID */); - - /* Create a temporary file for AP config file storage. */ - mLegacyApConfigFile = File.createTempFile(TEST_AP_CONFIG_FILE_PREFIX, ""); - - SoftApConfiguration expectedConfig = setupApConfig( - "ConfiguredAP", /* SSID */ - "randomKey", /* preshared key */ - SECURITY_TYPE_WPA2_PSK, /* security type */ - SoftApConfiguration.BAND_5GHZ, /* AP band (5GHz) */ - 40, /* AP channel */ - true /* Hidden SSID */); - - assertWifiConfigurationEqualSoftApConfiguration(backupConfig, expectedConfig); - - writeLegacyApConfigFile(backupConfig); - WifiApConfigStore store = createWifiApConfigStore(mLegacyApConfigFile); - verify(mWifiConfigManager).saveToStore(true); - verify(mBackupManagerProxy).notifyDataChanged(); - verifyApConfig(expectedConfig, store.getApConfiguration()); - verifyApConfig(expectedConfig, mDataStoreSource.toSerialize()); - // Simulate the config store read to trigger the write to new config store. - mDataStoreSource.reset(); - mLooper.dispatchAll(); - // Triggers write twice: - // a) On reading the legacy file (new config store not ready yet) - // b) When the new config store is ready. - verify(mWifiConfigManager, times(2)).saveToStore(true); - } /** * Verify the handling of setting a null ap configuration. diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreTest.java index 967c029ba..ca368733a 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreTest.java @@ -19,6 +19,7 @@ package com.android.server.wifi; import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import android.app.test.MockAnswerUtil; import android.app.test.TestAlarmManager; import android.content.Context; import android.content.pm.PackageManager; @@ -46,12 +47,14 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.MockitoSession; +import org.mockito.stubbing.Answer; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; @@ -153,6 +156,12 @@ public class WifiConfigStoreTest extends WifiBaseTest { + "</Integrity>\n" + "<%s />\n" + "</WifiConfigStoreData>\n"; + private static final String TEST_DATA_XML_STRING_FORMAT_V3_WITH_ONE_DATA_SOURCE = + "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" + + "<WifiConfigStoreData>\n" + + "<int name=\"Version\" value=\"3\" />\n" + + "<%s />\n" + + "</WifiConfigStoreData>\n"; // Test mocks @Mock private Context mContext; @Mock private PackageManager mPackageManager; @@ -162,6 +171,7 @@ public class WifiConfigStoreTest extends WifiBaseTest { @Mock private WifiMetrics mWifiMetrics; @Mock private WifiConfigStoreEncryptionUtil mEncryptionUtil; private MockStoreFile mSharedStore; + private MockStoreFile mSharedSoftApStore; private MockStoreFile mUserStore; private MockStoreFile mUserNetworkSuggestionsStore; private List<StoreFile> mUserStores = new ArrayList<StoreFile>(); @@ -190,6 +200,7 @@ public class WifiConfigStoreTest extends WifiBaseTest { when(mEncryptionUtil.decrypt(any(EncryptedData.class))) .thenReturn(new byte[0]); mSharedStore = new MockStoreFile(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + mSharedSoftApStore = new MockStoreFile(WifiConfigStore.STORE_FILE_SHARED_SOFTAP); mUserStore = new MockStoreFile(WifiConfigStore.STORE_FILE_USER_GENERAL); mUserNetworkSuggestionsStore = new MockStoreFile(WifiConfigStore.STORE_FILE_USER_NETWORK_SUGGESTIONS); @@ -214,7 +225,7 @@ public class WifiConfigStoreTest extends WifiBaseTest { setupMocks(); mWifiConfigStore = new WifiConfigStore(mContext, new Handler(mLooper.getLooper()), mClock, - mWifiMetrics, Arrays.asList(mSharedStore)); + mWifiMetrics, Arrays.asList(mSharedStore, mSharedSoftApStore)); // Enable verbose logging before tests. mWifiConfigStore.enableVerboseLogging(true); } @@ -847,6 +858,131 @@ public class WifiConfigStoreTest extends WifiBaseTest { } /** + * Tests the complete migration path all the way from reading from the migration stream to + * parsing the XML data and sending it to the appropriate registered data sources. + */ + @Test + public void testMigration() throws Exception { + // Setup both shared & user store migrations. + StoreFile sharedStoreFile1 = mock(StoreFile.class); + when(sharedStoreFile1.getFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + StoreFile sharedStoreFile2 = mock(StoreFile.class); + when(sharedStoreFile2.getFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_SOFTAP); + StoreFile userStoreFile1 = mock(StoreFile.class); + when(userStoreFile1.getFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_GENERAL); + StoreFile userStoreFile2 = mock(StoreFile.class); + when(userStoreFile2.getFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_NETWORK_SUGGESTIONS); + mWifiConfigStore = new WifiConfigStore(mContext, new Handler(mLooper.getLooper()), mClock, + mWifiMetrics, Arrays.asList(sharedStoreFile1, sharedStoreFile2)); + mWifiConfigStore.setUserStores(Arrays.asList(userStoreFile1, userStoreFile2)); + + // Register data container. + StoreData sharedStoreData = mock(StoreData.class); + when(sharedStoreData.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_SHARED_GENERAL); + when(sharedStoreData.getName()).thenReturn(TEST_SHARE_DATA); + StoreData userStoreData = mock(StoreData.class); + when(userStoreData.getStoreFileId()) + .thenReturn(WifiConfigStore.STORE_FILE_USER_GENERAL); + when(userStoreData.getName()).thenReturn(TEST_USER_DATA); + mWifiConfigStore.registerStoreData(sharedStoreData); + mWifiConfigStore.registerStoreData(userStoreData); + + // Migration data + InputStream sharedStream1 = mock(InputStream.class); + InputStream sharedStream2 = mock(InputStream.class); + InputStream userStream1 = mock(InputStream.class); + InputStream userStream2 = mock(InputStream.class); + when(WifiMigration.convertAndRetrieveSharedConfigStoreFile( + WifiMigration.STORE_FILE_SHARED_GENERAL)) + .thenReturn(sharedStream1); + when(WifiMigration.convertAndRetrieveSharedConfigStoreFile( + WifiMigration.STORE_FILE_SHARED_SOFTAP)) + .thenReturn(sharedStream2); + when(WifiMigration.convertAndRetrieveUserConfigStoreFile( + eq(WifiMigration.STORE_FILE_USER_GENERAL), any())) + .thenReturn(userStream1); + when(WifiMigration.convertAndRetrieveUserConfigStoreFile( + eq(WifiMigration.STORE_FILE_USER_NETWORK_SUGGESTIONS), any())) + .thenReturn(userStream2); + + byte[] sharedStoreXmlBytes = + String.format(TEST_DATA_XML_STRING_FORMAT_V3_WITH_ONE_DATA_SOURCE, + TEST_SHARE_DATA).getBytes(); + byte[] userStoreXmlBytes = + String.format(TEST_DATA_XML_STRING_FORMAT_V3_WITH_ONE_DATA_SOURCE, + TEST_USER_DATA).getBytes(); + when(sharedStream1.available()) + .thenReturn(sharedStoreXmlBytes.length) // first time return file contents, then 0. + .thenReturn(0); + when(sharedStream2.available()) + .thenReturn(sharedStoreXmlBytes.length) // first time return file contents, then 0. + .thenReturn(0); + when(userStream1.available()) + .thenReturn(userStoreXmlBytes.length) // first time return file contents, then 0. + .thenReturn(0); + when(userStream2.available()) + .thenReturn(userStoreXmlBytes.length) // first time return file contents, then 0. + .thenReturn(0); + Answer sharedStreamReadAnswer = new MockAnswerUtil.AnswerWithArguments() { + public int answer(byte[] b, int off, int len) { + System.arraycopy(sharedStoreXmlBytes, 0, b, 0, sharedStoreXmlBytes.length); + return sharedStoreXmlBytes.length; + } + }; + Answer userStreamReadAnswer = new MockAnswerUtil.AnswerWithArguments() { + public int answer(byte[] b, int off, int len) { + System.arraycopy(userStoreXmlBytes, 0, b, 0, userStoreXmlBytes.length); + return userStoreXmlBytes.length; + } + }; + when(sharedStream1.read(any(byte[].class), anyInt(), anyInt())) + .thenAnswer(sharedStreamReadAnswer) // first time return file contents, then 0. + .thenReturn(0); + when(sharedStream2.read(any(byte[].class), anyInt(), anyInt())) + .thenAnswer(sharedStreamReadAnswer) // first time return file contents, then 0. + .thenReturn(0); + when(userStream1.read(any(byte[].class), anyInt(), anyInt())) + .thenAnswer(userStreamReadAnswer) // first time return file contents, then 0. + .thenReturn(0); + when(userStream2.read(any(byte[].class), anyInt(), anyInt())) + .thenAnswer(userStreamReadAnswer) // first time return file contents, then 0. + .thenReturn(0); + + // Trigger read. + mWifiConfigStore.read(); + + // Verify that we read the data out of all the migration streams & we didn't read + // from the files on disk. + verify(sharedStream1, times(2)).available(); + verify(sharedStream1, times(2)).read(any(), anyInt(), anyInt()); + verify(sharedStream2, times(2)).available(); + verify(sharedStream2, times(2)).read(any(), anyInt(), anyInt()); + verify(userStream1, times(2)).available(); + verify(userStream1, times(2)).read(any(), anyInt(), anyInt()); + verify(userStream2, times(2)).available(); + verify(userStream2, times(2)).read(any(), anyInt(), anyInt()); + + // Verify that we correctly deserialized the data and sent it to the corresponding sources. + verify(sharedStoreData, times(1)) + .deserializeData(any(XmlPullParser.class), anyInt(), + eq(WifiConfigStore.ENCRYPT_CREDENTIALS_CONFIG_STORE_DATA_VERSION), any()); + verify(userStoreData, times(1)) + .deserializeData(any(XmlPullParser.class), anyInt(), + eq(WifiConfigStore.ENCRYPT_CREDENTIALS_CONFIG_STORE_DATA_VERSION), any()); + + // Verify we did not read from the real store files. + verify(sharedStoreFile1, never()).readRawData(); + verify(sharedStoreFile2, never()).readRawData(); + verify(userStoreFile1, never()).readRawData(); + verify(userStoreFile2, never()).readRawData(); + } + + /** * Mock Store File to redirect all file writes from WifiConfigStore to local buffers. * This can be used to examine the data output by WifiConfigStore. */ |