diff options
author | Ahmed ElArabawy <arabawy@google.com> | 2019-12-19 20:23:14 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-12-19 20:23:14 +0000 |
commit | b34478d20555aa46b3c39218feff83b65b83fe22 (patch) | |
tree | d8c2dbfe5c805d023a50c1c73fb46cc957672b47 /tests | |
parent | da46e1a0bf8514edd615707dd458d22fb2842137 (diff) | |
parent | 344fbea264873c52a4b1ceadf699d727d3c6df38 (diff) |
Merge "SoftAp: fix backup restore fail."
Diffstat (limited to 'tests')
3 files changed, 126 insertions, 17 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/SoftApBackupRestoreTest.java b/tests/wifitests/src/com/android/server/wifi/SoftApBackupRestoreTest.java index 9a275fe32..968ea12fb 100644 --- a/tests/wifitests/src/com/android/server/wifi/SoftApBackupRestoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SoftApBackupRestoreTest.java @@ -45,6 +45,29 @@ public class SoftApBackupRestoreTest extends WifiBaseTest { private SoftApBackupRestore mSoftApBackupRestore; private static final int LAST_WIFICOFIGURATION_BACKUP_VERSION = 3; + /** + * 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.getWpa2Passphrase()); + 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()); + } + + @Before public void setUp() throws Exception { mSoftApBackupRestore = new SoftApBackupRestore(); @@ -120,6 +143,6 @@ public class SoftApBackupRestoreTest extends WifiBaseTest { SoftApConfiguration restoredConfig = mSoftApBackupRestore.retrieveSoftApConfigurationFromBackupData(data); - assertThat(ApConfigUtil.fromWifiConfiguration(wifiConfig)).isEqualTo(restoredConfig); + assertWifiConfigurationEqualSoftApConfiguration(wifiConfig, restoredConfig); } } diff --git a/tests/wifitests/src/com/android/server/wifi/SoftApStoreDataTest.java b/tests/wifitests/src/com/android/server/wifi/SoftApStoreDataTest.java index ee6821528..67f66184a 100644 --- a/tests/wifitests/src/com/android/server/wifi/SoftApStoreDataTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SoftApStoreDataTest.java @@ -25,6 +25,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.net.wifi.SoftApConfiguration; +import android.net.wifi.WifiConfiguration; import android.util.Xml; import androidx.test.filters.SmallTest; @@ -52,14 +53,24 @@ public class SoftApStoreDataTest extends WifiBaseTest { private static final String TEST_SSID = "SSID"; private static final String TEST_WPA2_PASSPHRASE = "Test"; private static final boolean TEST_HIDDEN = false; - private static final int TEST_BAND = SoftApConfiguration.BAND_ANY; + private static final int TEST_BAND = SoftApConfiguration.BAND_2GHZ + | SoftApConfiguration.BAND_5GHZ; + private static final int TEST_OLD_BAND = WifiConfiguration.AP_BAND_ANY; private static final int TEST_CHANNEL = 0; private static final int TEST_SECURITY = SoftApConfiguration.SECURITY_TYPE_WPA2_PSK; private static final String TEST_SOFTAP_CONFIG_XML_STRING = "<string name=\"SSID\">" + TEST_SSID + "</string>\n" - + "<int name=\"Band\" value=\"" + TEST_BAND + "\" />\n" + + "<int name=\"Band\" value=\"" + TEST_OLD_BAND + "\" />\n" + + "<int name=\"Channel\" value=\"" + TEST_CHANNEL + "\" />\n" + + "<boolean name=\"HiddenSSID\" value=\"" + TEST_HIDDEN + "\" />\n" + + "<int name=\"SecurityType\" value=\"" + TEST_SECURITY + "\" />\n" + + "<string name=\"Wpa2Passphrase\">" + TEST_WPA2_PASSPHRASE + "</string>\n"; + + private static final String TEST_SOFTAP_CONFIG_XML_STRING_WITH_NEW_BAND_DESIGN = + "<string name=\"SSID\">" + TEST_SSID + "</string>\n" + + "<int name=\"ApBand\" value=\"" + TEST_BAND + "\" />\n" + "<int name=\"Channel\" value=\"" + TEST_CHANNEL + "\" />\n" + "<boolean name=\"HiddenSSID\" value=\"" + TEST_HIDDEN + "\" />\n" + "<int name=\"SecurityType\" value=\"" + TEST_SECURITY + "\" />\n" @@ -142,7 +153,7 @@ public class SoftApStoreDataTest extends WifiBaseTest { when(mDataSource.toSerialize()).thenReturn(softApConfigBuilder.build()); byte[] actualData = serializeData(); - assertEquals(TEST_SOFTAP_CONFIG_XML_STRING, new String(actualData)); + assertEquals(TEST_SOFTAP_CONFIG_XML_STRING_WITH_NEW_BAND_DESIGN, new String(actualData)); } /** @@ -152,6 +163,29 @@ public class SoftApStoreDataTest extends WifiBaseTest { */ @Test public void deserializeSoftAp() throws Exception { + deserializeData(TEST_SOFTAP_CONFIG_XML_STRING_WITH_NEW_BAND_DESIGN.getBytes()); + + ArgumentCaptor<SoftApConfiguration> softapConfigCaptor = + ArgumentCaptor.forClass(SoftApConfiguration.class); + verify(mDataSource).fromDeserialized(softapConfigCaptor.capture()); + SoftApConfiguration softApConfig = softapConfigCaptor.getValue(); + assertNotNull(softApConfig); + assertEquals(softApConfig.getSsid(), TEST_SSID); + assertEquals(softApConfig.getWpa2Passphrase(), TEST_WPA2_PASSPHRASE); + assertEquals(softApConfig.getSecurityType(), SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); + assertEquals(softApConfig.isHiddenSsid(), TEST_HIDDEN); + assertEquals(softApConfig.getBand(), TEST_BAND); + assertEquals(softApConfig.getChannel(), TEST_CHANNEL); + } + + /** + * Verify that the old format is deserialized correctly. + * + * @throws Exception + */ + @Test + public void deserializeOldBandSoftAp() throws Exception { + // Start with the old serialized data deserializeData(TEST_SOFTAP_CONFIG_XML_STRING.getBytes()); ArgumentCaptor<SoftApConfiguration> softapConfigCaptor = diff --git a/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java b/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java index b476589cc..df99b179d 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java @@ -35,12 +35,15 @@ 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; @@ -170,24 +173,62 @@ public class WifiApConfigStoreTest extends WifiBaseTest { return configBuilder.build(); } - private void writeLegacyApConfigFile(SoftApConfiguration config) throws Exception { + /** + * 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.getSsid()); - out.writeInt(config.getBand()); - out.writeInt(config.getChannel()); - out.writeBoolean(config.isHiddenSsid()); - int securityType = config.getSecurityType(); - out.writeInt(securityType); - if (securityType == SECURITY_TYPE_WPA2_PSK) { - out.writeUTF(config.getWpa2Passphrase()); + 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.getWpa2Passphrase()); + 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.getWpa2Passphrase(), config2.getWpa2Passphrase()); @@ -239,17 +280,28 @@ public class WifiApConfigStoreTest extends WifiBaseTest { */ @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) */ + SoftApConfiguration.BAND_5GHZ, /* 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, ""); - writeLegacyApConfigFile(expectedConfig); + assertWifiConfigurationEqualSoftApConfiguration(backupConfig, expectedConfig); + + writeLegacyApConfigFile(backupConfig); WifiApConfigStore store = createWifiApConfigStore(mLegacyApConfigFile.getPath()); verify(mWifiConfigManager).saveToStore(true); verify(mBackupManagerProxy).notifyDataChanged(); |