summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRoshan Pius <rpius@google.com>2016-08-25 14:51:48 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2016-08-25 14:51:49 +0000
commitc13a0778c3d9e081bb328bcea0bc64914de9b02e (patch)
tree9b751d922ea8bad8703b3b23e241b384560bdd56 /tests
parent35a7bf94dc19d7b9569a4618520d0960d0d1d9ec (diff)
parentd58768eee13969fda1c4dd465b09aef6db30c14c (diff)
Merge changes Ia69acca0,Ib2ebe427,I35d626ed,I143a4271
* changes: WifiConfigStoreNew: Change location of user store files WifiConfigManagerNew: Keep track of user selected network WifiConfigManagerNew: Migration from legacy stores WifiConfigStoreLegacy: Migration from legacy store
Diffstat (limited to 'tests')
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiConfigManagerNewTest.java121
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiConfigStoreLegacyTest.java248
2 files changed, 359 insertions, 10 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerNewTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerNewTest.java
index 48fd05429..43c446900 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerNewTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigManagerNewTest.java
@@ -38,7 +38,9 @@ import android.test.suitebuilder.annotation.SmallTest;
import android.text.TextUtils;
import com.android.internal.R;
+import com.android.server.wifi.WifiConfigStoreLegacy.WifiConfigStoreDataLegacy;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
@@ -77,6 +79,7 @@ public class WifiConfigManagerNewTest {
@Mock private UserManager mUserManager;
@Mock private WifiKeyStore mWifiKeyStore;
@Mock private WifiConfigStoreNew mWifiConfigStore;
+ @Mock private WifiConfigStoreLegacy mWifiConfigStoreLegacy;
@Mock private PackageManager mPackageManager;
private MockResources mResources;
@@ -145,10 +148,20 @@ public class WifiConfigManagerNewTest {
.updateNetworkKeys(any(WifiConfiguration.class), any(WifiConfiguration.class)))
.thenReturn(true);
+ when(mWifiConfigStore.areStoresPresent()).thenReturn(true);
+
createWifiConfigManager();
}
/**
+ * Called after each test
+ */
+ @After
+ public void cleanup() {
+ validateMockitoUsage();
+ }
+
+ /**
* Verifies the addition of a single network using
* {@link WifiConfigManagerNew#addOrUpdateNetwork(WifiConfiguration, int)}
*/
@@ -541,7 +554,7 @@ public class WifiConfigManagerNewTest {
/**
* Verifies the enabling of network using
- * {@link WifiConfigManagerNew#enableNetwork(int, int)} and
+ * {@link WifiConfigManagerNew#enableNetwork(int, boolean, int)} and
* {@link WifiConfigManagerNew#disableNetwork(int, int)}.
*/
@Test
@@ -550,7 +563,8 @@ public class WifiConfigManagerNewTest {
NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(openNetwork);
- assertTrue(mWifiConfigManager.enableNetwork(result.getNetworkId(), TEST_CREATOR_UID));
+ assertTrue(mWifiConfigManager.enableNetwork(
+ result.getNetworkId(), false, TEST_CREATOR_UID));
WifiConfiguration retrievedNetwork =
mWifiConfigManager.getConfiguredNetwork(result.getNetworkId());
NetworkSelectionStatus retrievedStatus = retrievedNetwork.getNetworkSelectionStatus();
@@ -567,7 +581,7 @@ public class WifiConfigManagerNewTest {
/**
* Verifies the enabling of network using
- * {@link WifiConfigManagerNew#enableNetwork(int, int)} with a UID which
+ * {@link WifiConfigManagerNew#enableNetwork(int, boolean, int)} with a UID which
* has no permission to modify the network fails..
*/
@Test
@@ -576,7 +590,8 @@ public class WifiConfigManagerNewTest {
NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(openNetwork);
- assertTrue(mWifiConfigManager.enableNetwork(result.getNetworkId(), TEST_CREATOR_UID));
+ assertTrue(mWifiConfigManager.enableNetwork(
+ result.getNetworkId(), false, TEST_CREATOR_UID));
WifiConfiguration retrievedNetwork =
mWifiConfigManager.getConfiguredNetwork(result.getNetworkId());
NetworkSelectionStatus retrievedStatus = retrievedNetwork.getNetworkSelectionStatus();
@@ -669,7 +684,7 @@ public class WifiConfigManagerNewTest {
/**
* Verifies that any configuration update attempt with an invalid networkID is gracefully
* handled.
- * This invokes {@link WifiConfigManagerNew#enableNetwork(int, int)},
+ * This invokes {@link WifiConfigManagerNew#enableNetwork(int, boolean, int)},
* {@link WifiConfigManagerNew#disableNetwork(int, int)},
* {@link WifiConfigManagerNew#updateNetworkSelectionStatus(int, int)} and
* {@link WifiConfigManagerNew#checkAndUpdateLastConnectUid(int, int)}.
@@ -680,7 +695,8 @@ public class WifiConfigManagerNewTest {
NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(openNetwork);
- assertFalse(mWifiConfigManager.enableNetwork(result.getNetworkId() + 1, TEST_CREATOR_UID));
+ assertFalse(mWifiConfigManager.enableNetwork(
+ result.getNetworkId() + 1, false, TEST_CREATOR_UID));
assertFalse(mWifiConfigManager.disableNetwork(result.getNetworkId() + 1, TEST_CREATOR_UID));
assertFalse(mWifiConfigManager.updateNetworkSelectionStatus(
result.getNetworkId() + 1, NetworkSelectionStatus.DISABLED_BY_WIFI_MANAGER));
@@ -1165,9 +1181,9 @@ public class WifiConfigManagerNewTest {
verifyAddNetworkToWifiConfigManager(network3);
// Enable all of them.
- assertTrue(mWifiConfigManager.enableNetwork(network1.networkId, TEST_CREATOR_UID));
- assertTrue(mWifiConfigManager.enableNetwork(network2.networkId, TEST_CREATOR_UID));
- assertTrue(mWifiConfigManager.enableNetwork(network3.networkId, TEST_CREATOR_UID));
+ assertTrue(mWifiConfigManager.enableNetwork(network1.networkId, false, TEST_CREATOR_UID));
+ assertTrue(mWifiConfigManager.enableNetwork(network2.networkId, false, TEST_CREATOR_UID));
+ assertTrue(mWifiConfigManager.enableNetwork(network3.networkId, false, TEST_CREATOR_UID));
// Now set scan results in 2 of them to set the corresponding
// {@link NetworkSelectionStatus#mSeenInLastQualifiedNetworkSelection} field.
@@ -1782,11 +1798,96 @@ public class WifiConfigManagerNewTest {
assertTrue(result.isSuccess());
}
+ /**
+ * Verifies the loading of networks using {@link WifiConfigManagerNew#loadFromStore()} attempts
+ * to migrate data from legacy stores when the new store files are absent.
+ */
+ @Test
+ public void testMigrationFromLegacyStore() throws Exception {
+ // Create the store data to be returned from legacy stores.
+ List<WifiConfiguration> networks = new ArrayList<>();
+ networks.add(WifiConfigurationTestUtil.createPskNetwork());
+ networks.add(WifiConfigurationTestUtil.createEapNetwork());
+ networks.add(WifiConfigurationTestUtil.createWepNetwork());
+ WifiConfigStoreDataLegacy storeData =
+ new WifiConfigStoreDataLegacy(networks, new HashSet<String>());
+
+ // New store files not present, so migrate from the old store.
+ when(mWifiConfigStore.areStoresPresent()).thenReturn(false);
+ when(mWifiConfigStoreLegacy.areStoresPresent()).thenReturn(true);
+ when(mWifiConfigStoreLegacy.read()).thenReturn(storeData);
+
+ // Now trigger a load from store. This should populate the in memory list with all the
+ // networks above from the legacy store.
+ mWifiConfigManager.loadFromStore();
+
+ verify(mWifiConfigStore, never()).read();
+ verify(mWifiConfigStoreLegacy).read();
+
+ List<WifiConfiguration> retrievedNetworks =
+ mWifiConfigManager.getConfiguredNetworksWithPasswords();
+ WifiConfigurationTestUtil.assertConfigurationsEqualForConfigManagerAddOrUpdate(
+ networks, retrievedNetworks);
+ }
+
+ /**
+ * Verifies the loading of networks using {@link WifiConfigManagerNew#loadFromStore()} does
+ * not attempt to read from any of the stores (new or legacy) when the store files are
+ * not present.
+ */
+ @Test
+ public void testFreshInstallDoesNotLoadFromStore() throws Exception {
+ // New store files not present, so migrate from the old store.
+ when(mWifiConfigStore.areStoresPresent()).thenReturn(false);
+ when(mWifiConfigStoreLegacy.areStoresPresent()).thenReturn(false);
+
+ // Now trigger a load from store. This should populate the in memory list with all the
+ // networks above.
+ mWifiConfigManager.loadFromStore();
+
+ verify(mWifiConfigStore, never()).read();
+ verify(mWifiConfigStoreLegacy, never()).read();
+
+ assertTrue(mWifiConfigManager.getConfiguredNetworksWithPasswords().isEmpty());
+ }
+
+ /**
+ * Verifies that the last user selected network parameter is set when
+ * {@link WifiConfigManagerNew#enableNetwork(int, boolean, int)} with disableOthers flag is set
+ * to true and cleared when either {@link WifiConfigManagerNew#disableNetwork(int, int)} or
+ * {@link WifiConfigManagerNew#removeNetwork(int, int)} is invoked using the same network ID.
+ */
+ @Test
+ public void testLastSelectedNetwork() throws Exception {
+ WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork();
+ NetworkUpdateResult result = verifyAddNetworkToWifiConfigManager(openNetwork);
+
+ when(mClock.getElapsedSinceBootMillis()).thenReturn(67L);
+ assertTrue(mWifiConfigManager.enableNetwork(
+ result.getNetworkId(), true, TEST_CREATOR_UID));
+ assertEquals(result.getNetworkId(), mWifiConfigManager.getLastSelectedNetwork());
+ assertEquals(67, mWifiConfigManager.getLastSelectedTimeStamp());
+
+ // Now disable the network and ensure that the last selected flag is cleared.
+ assertTrue(mWifiConfigManager.disableNetwork(result.getNetworkId(), TEST_CREATOR_UID));
+ assertEquals(
+ WifiConfiguration.INVALID_NETWORK_ID, mWifiConfigManager.getLastSelectedNetwork());
+
+ // Enable it again and remove the network to ensure that the last selected flag was cleared.
+ assertTrue(mWifiConfigManager.enableNetwork(
+ result.getNetworkId(), true, TEST_CREATOR_UID));
+ assertEquals(result.getNetworkId(), mWifiConfigManager.getLastSelectedNetwork());
+
+ assertTrue(mWifiConfigManager.removeNetwork(result.getNetworkId(), TEST_CREATOR_UID));
+ assertEquals(
+ WifiConfiguration.INVALID_NETWORK_ID, mWifiConfigManager.getLastSelectedNetwork());
+ }
+
private void createWifiConfigManager() {
mWifiConfigManager =
new WifiConfigManagerNew(
mContext, mFrameworkFacade, mClock, mUserManager, mWifiKeyStore,
- mWifiConfigStore);
+ mWifiConfigStore, mWifiConfigStoreLegacy);
mWifiConfigManager.enableVerboseLogging(1);
}
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreLegacyTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreLegacyTest.java
new file mode 100644
index 000000000..060724a03
--- /dev/null
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigStoreLegacyTest.java
@@ -0,0 +1,248 @@
+/*
+ * Copyright (C) 2016 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;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import android.app.test.MockAnswerUtil.AnswerWithArguments;
+import android.net.IpConfiguration;
+import android.net.wifi.WifiConfiguration;
+import android.net.wifi.WifiEnterpriseConfig;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.text.TextUtils;
+import android.util.SparseArray;
+
+import com.android.server.net.IpConfigStore;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Unit tests for {@link com.android.server.wifi.WifiConfigStoreLegacy}.
+ */
+@SmallTest
+public class WifiConfigStoreLegacyTest {
+ private static final String MASKED_FIELD_VALUE = "*";
+
+ // Test mocks
+ @Mock private WifiSupplicantControl mWifiSupplicantControl;
+ @Mock private WifiNetworkHistory mWifiNetworkHistory;
+ @Mock private IpConfigStore mIpconfigStore;
+
+ /**
+ * Test instance of WifiConfigStore.
+ */
+ private WifiConfigStoreLegacy mWifiConfigStore;
+
+
+ /**
+ * Setup the test environment.
+ */
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+
+ mWifiConfigStore =
+ new WifiConfigStoreLegacy(
+ mWifiNetworkHistory, mWifiSupplicantControl, mIpconfigStore);
+ }
+
+ /**
+ * Called after each test
+ */
+ @After
+ public void cleanup() {
+ validateMockitoUsage();
+ }
+
+ /**
+ * Verify loading of network configurations from legacy stores. This is verifying the population
+ * of the masked wpa_supplicant fields using wpa_supplicant.conf file.
+ */
+ @Test
+ public void testLoadFromStores() {
+ WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork();
+ WifiConfiguration wepNetwork = WifiConfigurationTestUtil.createWepNetwork();
+ WifiConfiguration eapNetwork = WifiConfigurationTestUtil.createEapNetwork();
+ eapNetwork.enterpriseConfig.setPassword("EapPassword");
+
+ final List<WifiConfiguration> networks = new ArrayList<>();
+ networks.add(pskNetwork);
+ networks.add(wepNetwork);
+ networks.add(eapNetwork);
+
+ // Return the config data with passwords masked from wpa_supplicant control interface.
+ doAnswer(new AnswerWithArguments() {
+ public int answer(Map<String, WifiConfiguration> configs,
+ SparseArray<Map<String, String>> networkExtras) {
+ for (Map.Entry<String, WifiConfiguration> entry:
+ createWpaSupplicantLoadData(networks).entrySet()) {
+ configs.put(entry.getKey(), entry.getValue());
+ }
+ return 0;
+ }
+ }).when(mWifiSupplicantControl).loadNetworks(any(Map.class), any(SparseArray.class));
+
+ // Return the unmasked values during file parsing.
+ doAnswer(new AnswerWithArguments() {
+ public Map<String, String> answer(String fieldName) {
+ if (fieldName.equals(WifiConfiguration.pskVarName)) {
+ return createPskMap(networks);
+ } else if (fieldName.equals(WifiConfiguration.wepKeyVarNames[0])) {
+ return createWepKey0Map(networks);
+ } else if (fieldName.equals(WifiConfiguration.wepKeyVarNames[1])) {
+ return createWepKey1Map(networks);
+ } else if (fieldName.equals(WifiConfiguration.wepKeyVarNames[2])) {
+ return createWepKey2Map(networks);
+ } else if (fieldName.equals(WifiConfiguration.wepKeyVarNames[3])) {
+ return createWepKey3Map(networks);
+ } else if (fieldName.equals(WifiEnterpriseConfig.PASSWORD_KEY)) {
+ return createEapPasswordMap(networks);
+ }
+ return new HashMap<>();
+ }
+ }).when(mWifiSupplicantControl).readNetworkVariablesFromSupplicantFile(anyString());
+
+ WifiConfigStoreLegacy.WifiConfigStoreDataLegacy storeData = mWifiConfigStore.read();
+
+ WifiConfigurationTestUtil.assertConfigurationsEqualForConfigStore(
+ networks, storeData.getConfigurations());
+ }
+
+ private SparseArray<IpConfiguration> createIpConfigStoreLoadData(
+ List<WifiConfiguration> configurations) {
+ SparseArray<IpConfiguration> newIpConfigurations = new SparseArray<>();
+ for (WifiConfiguration config : configurations) {
+ newIpConfigurations.put(
+ config.configKey().hashCode(),
+ new IpConfiguration(config.getIpConfiguration()));
+ }
+ return newIpConfigurations;
+ }
+
+ private Map<String, String> createPskMap(List<WifiConfiguration> configurations) {
+ Map<String, String> pskMap = new HashMap<>();
+ for (WifiConfiguration config : configurations) {
+ if (!TextUtils.isEmpty(config.preSharedKey)) {
+ pskMap.put(config.configKey(), config.preSharedKey);
+ }
+ }
+ return pskMap;
+ }
+
+ private Map<String, String> createWepKey0Map(List<WifiConfiguration> configurations) {
+ Map<String, String> wepKeyMap = new HashMap<>();
+ for (WifiConfiguration config : configurations) {
+ if (!TextUtils.isEmpty(config.wepKeys[0])) {
+ wepKeyMap.put(config.configKey(), config.wepKeys[0]);
+ }
+ }
+ return wepKeyMap;
+ }
+
+ private Map<String, String> createWepKey1Map(List<WifiConfiguration> configurations) {
+ Map<String, String> wepKeyMap = new HashMap<>();
+ for (WifiConfiguration config : configurations) {
+ if (!TextUtils.isEmpty(config.wepKeys[1])) {
+ wepKeyMap.put(config.configKey(), config.wepKeys[1]);
+ }
+ }
+ return wepKeyMap;
+ }
+
+ private Map<String, String> createWepKey2Map(List<WifiConfiguration> configurations) {
+ Map<String, String> wepKeyMap = new HashMap<>();
+ for (WifiConfiguration config : configurations) {
+ if (!TextUtils.isEmpty(config.wepKeys[2])) {
+ wepKeyMap.put(config.configKey(), config.wepKeys[2]);
+ }
+ }
+ return wepKeyMap;
+ }
+
+ private Map<String, String> createWepKey3Map(List<WifiConfiguration> configurations) {
+ Map<String, String> wepKeyMap = new HashMap<>();
+ for (WifiConfiguration config : configurations) {
+ if (!TextUtils.isEmpty(config.wepKeys[3])) {
+ wepKeyMap.put(config.configKey(), config.wepKeys[3]);
+ }
+ }
+ return wepKeyMap;
+ }
+
+ private Map<String, String> createEapPasswordMap(List<WifiConfiguration> configurations) {
+ Map<String, String> eapPasswordMap = new HashMap<>();
+ for (WifiConfiguration config : configurations) {
+ if (!TextUtils.isEmpty(config.enterpriseConfig.getPassword())) {
+ eapPasswordMap.put(config.configKey(), config.enterpriseConfig.getPassword());
+ }
+ }
+ return eapPasswordMap;
+ }
+
+ private Map<String, WifiConfiguration> createWpaSupplicantLoadData(
+ List<WifiConfiguration> configurations) {
+ List<WifiConfiguration> newConfigurations = createMaskedWifiConfigurations(configurations);
+ Map<String, WifiConfiguration> configurationMap = new HashMap<>();
+ for (WifiConfiguration config : newConfigurations) {
+ configurationMap.put(config.configKey(true), config);
+ }
+ return configurationMap;
+ }
+
+ private List<WifiConfiguration> createMaskedWifiConfigurations(
+ List<WifiConfiguration> configurations) {
+ List<WifiConfiguration> newConfigurations = new ArrayList<>();
+ for (WifiConfiguration config : configurations) {
+ newConfigurations.add(createMaskedWifiConfiguration(config));
+ }
+ return newConfigurations;
+ }
+
+ private WifiConfiguration createMaskedWifiConfiguration(WifiConfiguration configuration) {
+ WifiConfiguration newConfig = new WifiConfiguration(configuration);
+ if (!TextUtils.isEmpty(configuration.preSharedKey)) {
+ newConfig.preSharedKey = MASKED_FIELD_VALUE;
+ }
+ if (!TextUtils.isEmpty(configuration.wepKeys[0])) {
+ newConfig.wepKeys[0] = MASKED_FIELD_VALUE;
+ }
+ if (!TextUtils.isEmpty(configuration.wepKeys[1])) {
+ newConfig.wepKeys[1] = MASKED_FIELD_VALUE;
+ }
+ if (!TextUtils.isEmpty(configuration.wepKeys[2])) {
+ newConfig.wepKeys[2] = MASKED_FIELD_VALUE;
+ }
+ if (!TextUtils.isEmpty(configuration.wepKeys[3])) {
+ newConfig.wepKeys[3] = MASKED_FIELD_VALUE;
+ }
+ if (!TextUtils.isEmpty(configuration.enterpriseConfig.getPassword())) {
+ newConfig.enterpriseConfig.setPassword(MASKED_FIELD_VALUE);
+ }
+ return newConfig;
+ }
+
+}