summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlesl <lesl@google.com>2019-12-19 20:38:41 +0800
committerAhmed ElArabawy <arabawy@google.com>2019-12-19 10:51:56 -0800
commit344fbea264873c52a4b1ceadf699d727d3c6df38 (patch)
tree8bfc9e71e4fa7acff32047390f3803278236d1d0
parent919df467f922b8e05bfd710aae48d415b2c76132 (diff)
SoftAp: fix backup restore fail.
1. load from configuration .txt file should be backup from WifiConfiguration. The security type should use WifiConfiguration 2. Add band convert for old configuration 3. Fix unit test 4. Add catch exception since old configuration might invalid. Bug: 146547601 Test: atest frameworks/opt/net/wifi/tests/wifitests Change-Id: I6ddaad9b19181360612e06e2963316c5bc83b2c7
-rw-r--r--service/java/com/android/server/wifi/SoftApBackupRestore.java3
-rw-r--r--service/java/com/android/server/wifi/SoftApStoreData.java102
-rw-r--r--service/java/com/android/server/wifi/WifiApConfigStore.java12
-rw-r--r--tests/wifitests/src/com/android/server/wifi/SoftApBackupRestoreTest.java25
-rw-r--r--tests/wifitests/src/com/android/server/wifi/SoftApStoreDataTest.java40
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java78
6 files changed, 194 insertions, 66 deletions
diff --git a/service/java/com/android/server/wifi/SoftApBackupRestore.java b/service/java/com/android/server/wifi/SoftApBackupRestore.java
index 477c1a06d..790a5cb69 100644
--- a/service/java/com/android/server/wifi/SoftApBackupRestore.java
+++ b/service/java/com/android/server/wifi/SoftApBackupRestore.java
@@ -129,6 +129,9 @@ public class SoftApBackupRestore {
} catch (BackupUtils.BadVersionException badVersion) {
Log.e(TAG, "Invalid backup data received, BadVersionException: " + badVersion);
return null;
+ } catch (IllegalArgumentException ie) {
+ Log.e(TAG, "Invalid backup data received, IllegalArgumentException " + ie);
+ return null;
}
return configBuilder.build();
}
diff --git a/service/java/com/android/server/wifi/SoftApStoreData.java b/service/java/com/android/server/wifi/SoftApStoreData.java
index be8dcd891..e48ce94a7 100644
--- a/service/java/com/android/server/wifi/SoftApStoreData.java
+++ b/service/java/com/android/server/wifi/SoftApStoreData.java
@@ -21,6 +21,7 @@ import android.net.wifi.SoftApConfiguration;
import android.text.TextUtils;
import android.util.Log;
+import com.android.server.wifi.util.ApConfigUtil;
import com.android.server.wifi.util.WifiConfigStoreEncryptionUtil;
import com.android.server.wifi.util.XmlUtil;
@@ -42,6 +43,7 @@ public class SoftApStoreData implements WifiConfigStore.StoreData {
private static final String XML_TAG_HIDDEN_SSID = "HiddenSSID";
private static final String XML_TAG_SECURITY_TYPE = "SecurityType";
private static final String XML_TAG_WPA2_PASSPHRASE = "Wpa2Passphrase";
+ private static final String XML_TAG_AP_BAND = "ApBand";
private final DataSource mDataSource;
@@ -90,7 +92,7 @@ public class SoftApStoreData implements WifiConfigStore.StoreData {
SoftApConfiguration softApConfig = mDataSource.toSerialize();
if (softApConfig != null) {
XmlUtil.writeNextValue(out, XML_TAG_SSID, softApConfig.getSsid());
- XmlUtil.writeNextValue(out, XML_TAG_BAND, softApConfig.getBand());
+ XmlUtil.writeNextValue(out, XML_TAG_AP_BAND, softApConfig.getBand());
XmlUtil.writeNextValue(out, XML_TAG_CHANNEL, softApConfig.getChannel());
XmlUtil.writeNextValue(out, XML_TAG_HIDDEN_SSID, softApConfig.isHiddenSsid());
XmlUtil.writeNextValue(out, XML_TAG_SECURITY_TYPE, softApConfig.getSecurityType());
@@ -114,58 +116,66 @@ public class SoftApStoreData implements WifiConfigStore.StoreData {
int securityType = SoftApConfiguration.SECURITY_TYPE_OPEN;
String wpa2Passphrase = null;
String ssid = null;
- int band = -1;
+ // Note that, during deserializaion, we may read the old band encoding (XML_TAG_BAND)
+ // or the new band encoding (XML_TAG_AP_BAND) that is used after the introduction of the
+ // 6GHz band. If the old encoding is found, a conversion is done.
int channel = -1;
- while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
- String[] valueName = new String[1];
- Object value = XmlUtil.readCurrentValue(in, valueName);
- if (TextUtils.isEmpty(valueName[0])) {
- throw new XmlPullParserException("Missing value name");
+ int apBand = -1;
+ try {
+ while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) {
+ String[] valueName = new String[1];
+ Object value = XmlUtil.readCurrentValue(in, valueName);
+ if (TextUtils.isEmpty(valueName[0])) {
+ throw new XmlPullParserException("Missing value name");
+ }
+ switch (valueName[0]) {
+ case XML_TAG_SSID:
+ ssid = (String) value;
+ softApConfigBuilder.setSsid((String) value);
+ break;
+ case XML_TAG_BAND:
+ apBand = ApConfigUtil.convertWifiConfigBandToSoftApConfigBand((int) value);
+ break;
+ case XML_TAG_AP_BAND:
+ apBand = (int) value;
+ break;
+ case XML_TAG_CHANNEL:
+ channel = (int) value;
+ break;
+ case XML_TAG_HIDDEN_SSID:
+ softApConfigBuilder.setHiddenSsid((boolean) value);
+ break;
+ case XML_TAG_SECURITY_TYPE:
+ securityType = (int) value;
+ break;
+ case XML_TAG_WPA2_PASSPHRASE:
+ wpa2Passphrase = (String) value;
+ break;
+ default:
+ Log.w(TAG, "Ignoring unknown value name " + valueName[0]);
+ break;
+ }
}
- switch (valueName[0]) {
- case XML_TAG_SSID:
- ssid = (String) value;
- softApConfigBuilder.setSsid((String) value);
- break;
- case XML_TAG_BAND:
- band = (int) value;
- break;
- case XML_TAG_CHANNEL:
- channel = (int) value;
- break;
- case XML_TAG_HIDDEN_SSID:
- softApConfigBuilder.setHiddenSsid((boolean) value);
- break;
- case XML_TAG_SECURITY_TYPE:
- securityType = (int) value;
- break;
- case XML_TAG_WPA2_PASSPHRASE:
- wpa2Passphrase = (String) value;
- break;
- default:
- Log.w(TAG, "Ignoring unknown value name " + valueName[0]);
- break;
- }
- }
- // Set channel and band
- if (channel == 0) {
- if (band != -1) {
- softApConfigBuilder.setBand(band);
+ // Set channel and band
+ if (channel == 0) {
+ softApConfigBuilder.setBand(apBand);
+ } else {
+ softApConfigBuilder.setChannel(channel, apBand);
}
- } else if ((channel != -1) && (band != -1)) {
- softApConfigBuilder.setChannel(channel, band);
- }
- // We should at-least have SSID restored from store.
- if (ssid == null) {
- Log.e(TAG, "Failed to parse SSID");
+ // We should at-least have SSID restored from store.
+ if (ssid == null) {
+ Log.e(TAG, "Failed to parse SSID");
+ return;
+ }
+ if (securityType == SoftApConfiguration.SECURITY_TYPE_WPA2_PSK) {
+ softApConfigBuilder.setWpa2Passphrase(wpa2Passphrase);
+ }
+ } catch (IllegalArgumentException e) {
+ Log.e(TAG, "Failed to parse configuration" + e);
return;
}
- if (securityType == SoftApConfiguration.SECURITY_TYPE_WPA2_PSK) {
- softApConfigBuilder.setWpa2Passphrase(wpa2Passphrase);
- }
-
mDataSource.fromDeserialized(softApConfigBuilder.setSsid(ssid).build());
}
diff --git a/service/java/com/android/server/wifi/WifiApConfigStore.java b/service/java/com/android/server/wifi/WifiApConfigStore.java
index 8b7d94697..a42dbebd7 100644
--- a/service/java/com/android/server/wifi/WifiApConfigStore.java
+++ b/service/java/com/android/server/wifi/WifiApConfigStore.java
@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.IntentFilter;
import android.net.MacAddress;
import android.net.wifi.SoftApConfiguration;
+import android.net.wifi.WifiConfiguration;
import android.os.Environment;
import android.os.Handler;
import android.os.Process;
@@ -293,9 +294,11 @@ public class WifiApConfigStore {
int channel = in.readInt();
if (channel == 0) {
- configBuilder.setBand(band);
+ configBuilder.setBand(
+ ApConfigUtil.convertWifiConfigBandToSoftApConfigBand(band));
} else {
- configBuilder.setChannel(channel, band);
+ configBuilder.setChannel(channel,
+ ApConfigUtil.convertWifiConfigBandToSoftApConfigBand(band));
}
}
@@ -304,13 +307,16 @@ public class WifiApConfigStore {
}
int authType = in.readInt();
- if (authType == SoftApConfiguration.SECURITY_TYPE_WPA2_PSK) {
+ if (authType == WifiConfiguration.KeyMgmt.WPA2_PSK) {
configBuilder.setWpa2Passphrase(in.readUTF());
}
config = configBuilder.build();
} catch (IOException e) {
Log.e(TAG, "Error reading hotspot configuration " + e);
config = null;
+ } catch (IllegalArgumentException ie) {
+ Log.e(TAG, "Invalid hotspot configuration " + ie);
+ config = null;
} finally {
if (in != null) {
try {
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();