diff options
author | Roshan Pius <rpius@google.com> | 2016-06-08 14:04:53 -0700 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2016-06-09 13:35:41 -0700 |
commit | 2fafcc56fda54b1adf8b6743beaac59dbb84dfec (patch) | |
tree | c1a4dd8874f269c2893f7c1fdd32c5e63e1687f8 | |
parent | d8ee2a06475a6d437df11a8c6eeb3138dc384afe (diff) |
WifiBackupRestore: Handle network UIDs
Don't save creatorUID element for backup/restore. All networks will be
restored with the System UID set as the creatorUID. The creator UID is
hard to preserve across backup/restore.
Also, since the creatorUID is not being backed, there are genuine cases
where the configKey mismatch might occur. So, move the |configKey|
mismatch check out of |XmlUtil| and let backup/restore & config store
handle it differently as needed.
PS: Need to also figure out if we should stop backing up all networks
not saved by settings agent (creatorUid != SYSTEM_UD). Will wait for
discussions in b/29201888 to decide on that.
BUG: 29201888
TEST: Unit-tests
Change-Id: I668281893de9260a803872c04646b5af28ee45e5
5 files changed, 83 insertions, 28 deletions
diff --git a/service/java/com/android/server/wifi/WifiBackupRestore.java b/service/java/com/android/server/wifi/WifiBackupRestore.java index d17b777d0..02954d4a8 100644 --- a/service/java/com/android/server/wifi/WifiBackupRestore.java +++ b/service/java/com/android/server/wifi/WifiBackupRestore.java @@ -20,6 +20,7 @@ import android.net.IpConfiguration; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiEnterpriseConfig; import android.util.Log; +import android.util.Pair; import android.util.SparseArray; import android.util.Xml; @@ -259,6 +260,36 @@ public class WifiBackupRestore { } /** + * Helper method to parse the WifiConfiguration object and validate the configKey parsed. + */ + private WifiConfiguration parseWifiConfigurationFromXmlAndValidateConfigKey( + XmlPullParser in, int outerTagDepth) + throws XmlPullParserException, IOException { + Pair<String, WifiConfiguration> parsedConfig = + WifiConfigurationXmlUtil.parseWifiConfigurationFromXml(in, outerTagDepth); + if (parsedConfig == null || parsedConfig.first == null || parsedConfig.second == null) { + return null; + } + String configKeyParsed = parsedConfig.first; + WifiConfiguration configuration = parsedConfig.second; + String configKeyCalculated = configuration.configKey(); + if (!configKeyParsed.equals(configKeyCalculated)) { + String configKeyMismatchLog = + "Configuration key does not match. Retrieved: " + configKeyParsed + + ", Calculated: " + configKeyCalculated; + if (configuration.shared) { + Log.e(TAG, configKeyMismatchLog); + return null; + } else { + // ConfigKey mismatches are expected for private networks because the + // UID is not preserved across backup/restore. + Log.w(TAG, configKeyMismatchLog); + } + } + return configuration; + } + + /** * Parses the configuration data elements from the provided XML stream to a Configuration. * * @param in XmlPullParser instance pointing to the XML stream. @@ -278,7 +309,7 @@ public class WifiBackupRestore { in, XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION, networkTagDepth)) { int configTagDepth = networkTagDepth + 1; configuration = - WifiConfigurationXmlUtil.parseWifiConfigurationFromXml(in, configTagDepth); + parseWifiConfigurationFromXmlAndValidateConfigKey(in, configTagDepth); if (configuration == null) { return null; } @@ -661,13 +692,12 @@ public class WifiBackupRestore { String idString = mParsedIdStrLine.substring(mParsedIdStrLine.indexOf('=') + 1); Map<String, String> extras = WifiNative.parseNetworkExtra(idString); - configuration.creatorUid = - Integer.valueOf(extras.get(WifiConfigStore.ID_STRING_KEY_CREATOR_UID)); String configKey = extras.get(WifiConfigStore.ID_STRING_KEY_CONFIG_KEY); if (!configKey.equals(configuration.configKey())) { - Log.e(TAG, "Configuration key does not match. Retrieved: " + configKey + // ConfigKey mismatches are expected for private networks because the + // UID is not preserved across backup/restore. + Log.w(TAG, "Configuration key does not match. Retrieved: " + configKey + ", Calculated: " + configuration.configKey()); - return null; } } return configuration; diff --git a/service/java/com/android/server/wifi/util/XmlUtil.java b/service/java/com/android/server/wifi/util/XmlUtil.java index 4b8fec308..c5f7a9eed 100644 --- a/service/java/com/android/server/wifi/util/XmlUtil.java +++ b/service/java/com/android/server/wifi/util/XmlUtil.java @@ -26,6 +26,7 @@ import android.net.RouteInfo; import android.net.StaticIpConfiguration; import android.net.wifi.WifiConfiguration; import android.util.Log; +import android.util.Pair; import com.android.internal.util.XmlUtils; @@ -259,6 +260,7 @@ public class XmlUtil { public static final String XML_TAG_ALLOWED_AUTH_ALGOS = "AllowedAuthAlgos"; public static final String XML_TAG_SHARED = "Shared"; public static final String XML_TAG_CREATOR_UID = "CreatorUid"; + public static final String XML_TAG_CREATOR_NAME = "CreatorName"; /** * Write WepKeys to the XML stream. @@ -314,7 +316,6 @@ public class XmlUtil { out, XML_TAG_ALLOWED_AUTH_ALGOS, configuration.allowedAuthAlgorithms.toByteArray()); XmlUtil.writeNextValue(out, XML_TAG_SHARED, configuration.shared); - XmlUtil.writeNextValue(out, XML_TAG_CREATOR_UID, configuration.creatorUid); } /** @@ -340,10 +341,13 @@ public class XmlUtil { throws XmlPullParserException, IOException { writeCommonWifiConfigurationElementsToXml(out, configuration); // TODO: Will need to add more elements which needs to be persisted. + XmlUtil.writeNextValue(out, XML_TAG_CREATOR_UID, configuration.creatorUid); + XmlUtil.writeNextValue(out, XML_TAG_CREATOR_NAME, configuration.creatorName); } /** * Populate wepKeys array elements only if they were non-empty in the backup data. + * * @throws XmlPullParserException if parsing errors occur. */ private static void populateWepKeysFromXmlValue(Object value, String[] wepKeys) @@ -372,10 +376,11 @@ public class XmlUtil { * * @param in XmlPullParser instance pointing to the XML stream. * @param outerTagDepth depth of the outer tag in the XML document. - * @return WifiConfiguration object if parsing is successful, null otherwise. + * @return Pair<Config key, WifiConfiguration object> if parsing is successful, + * null otherwise. */ - public static WifiConfiguration parseWifiConfigurationFromXml(XmlPullParser in, - int outerTagDepth) + public static Pair<String, WifiConfiguration> parseWifiConfigurationFromXml( + XmlPullParser in, int outerTagDepth) throws XmlPullParserException, IOException { WifiConfiguration configuration = new WifiConfiguration(); String configKeyInData = null; @@ -428,20 +433,15 @@ public class XmlUtil { case XML_TAG_CREATOR_UID: configuration.creatorUid = (int) value; break; + case XML_TAG_CREATOR_NAME: + configuration.creatorName = (String) value; + break; default: Log.e(TAG, "Unknown value name found: " + valueName[0]); return null; } } - // We should now have all the data to calculate the configKey. Compare it against the - // configKey stored in the XML data. - String configKeyCalculated = configuration.configKey(); - if (configKeyInData == null || !configKeyInData.equals(configKeyCalculated)) { - Log.e(TAG, "Configuration key does not match. Retrieved: " + configKeyInData - + ", Calculated: " + configKeyCalculated); - return null; - } - return configuration; + return Pair.create(configKeyInData, configuration); } } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiBackupRestoreTest.java b/tests/wifitests/src/com/android/server/wifi/WifiBackupRestoreTest.java index 0ca9eab15..c813cfaf6 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiBackupRestoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiBackupRestoreTest.java @@ -775,7 +775,7 @@ public class WifiBackupRestoreTest { for (WifiConfiguration actualConfiguration : actual) { String actualConfigKey = actualConfiguration.configKey(); if (actualConfigKey.equals(expectedConfigKey)) { - WifiConfigurationTestUtil.assertConfigurationEqual( + WifiConfigurationTestUtil.assertConfigurationEqualForBackup( expectedConfiguration, actualConfiguration); didCompare = true; } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationTestUtil.java b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationTestUtil.java index 85fe81b58..a7e47ffae 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConfigurationTestUtil.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConfigurationTestUtil.java @@ -96,9 +96,10 @@ public class WifiConfigurationTestUtil { } /** - * Asserts that the 2 WifiConfigurations are equal + * Asserts that the 2 WifiConfigurations are equal in the elements saved for both backup/restore + * and config store. */ - public static void assertConfigurationEqual( + private static void assertCommonConfigurationElementsEqual( WifiConfiguration expected, WifiConfiguration actual) { assertEquals(expected.SSID, actual.SSID); assertEquals(expected.BSSID, actual.BSSID); @@ -110,7 +111,26 @@ public class WifiConfigurationTestUtil { assertEquals(expected.allowedProtocols, actual.allowedProtocols); assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms); assertEquals(expected.shared, actual.shared); - assertEquals(expected.creatorUid, actual.creatorUid); assertEquals(expected.getIpConfiguration(), actual.getIpConfiguration()); } + + /** + * Asserts that the 2 WifiConfigurations are equal. This only compares the elements saved + * fpr backup/restore. + */ + public static void assertConfigurationEqualForBackup( + WifiConfiguration expected, WifiConfiguration actual) { + assertCommonConfigurationElementsEqual(expected, actual); + } + + /** + * Asserts that the 2 WifiConfigurations are equal. This compares all the elements saved for + * config store. + */ + public static void assertConfigurationEqualForConfigStore( + WifiConfiguration expected, WifiConfiguration actual) { + assertCommonConfigurationElementsEqual(expected, actual); + assertEquals(expected.creatorUid, actual.creatorUid); + assertEquals(expected.creatorName, actual.creatorName); + } } diff --git a/tests/wifitests/src/com/android/server/wifi/util/XmlUtilTest.java b/tests/wifitests/src/com/android/server/wifi/util/XmlUtilTest.java index fd07ab2c1..d53c51384 100644 --- a/tests/wifitests/src/com/android/server/wifi/util/XmlUtilTest.java +++ b/tests/wifitests/src/com/android/server/wifi/util/XmlUtilTest.java @@ -23,6 +23,7 @@ import android.net.ProxyInfo; import android.net.StaticIpConfiguration; import android.net.wifi.WifiConfiguration; import android.test.suitebuilder.annotation.SmallTest; +import android.util.Pair; import android.util.Xml; import com.android.internal.util.FastXmlSerializer; @@ -286,7 +287,7 @@ public class XmlUtilTest { return outputStream.toByteArray(); } - private WifiConfiguration deserializeWifiConfiguration(byte[] data) + private Pair<String, WifiConfiguration> deserializeWifiConfiguration(byte[] data) throws IOException, XmlPullParserException { // Deserialize the configuration object. final XmlPullParser in = Xml.newPullParser(); @@ -301,18 +302,22 @@ public class XmlUtilTest { */ private void serializeDeserializeWifiConfiguration(WifiConfiguration configuration) throws IOException, XmlPullParserException { - WifiConfiguration retrievedConfiguration; + Pair<String, WifiConfiguration> retrieved; // Test serialization/deserialization for backup first. - retrievedConfiguration = + retrieved = deserializeWifiConfiguration( serializeWifiConfigurationForBackup(configuration)); - WifiConfigurationTestUtil.assertConfigurationEqual(configuration, retrievedConfiguration); + assertEquals(retrieved.first, retrieved.second.configKey()); + WifiConfigurationTestUtil.assertConfigurationEqualForBackup( + configuration, retrieved.second); // Test serialization/deserialization for config store. - retrievedConfiguration = + retrieved = deserializeWifiConfiguration( serializeWifiConfigurationForConfigStore(configuration)); - WifiConfigurationTestUtil.assertConfigurationEqual(configuration, retrievedConfiguration); + assertEquals(retrieved.first, retrieved.second.configKey()); + WifiConfigurationTestUtil.assertConfigurationEqualForConfigStore( + configuration, retrieved.second); } private void serializeDeserializeIpConfiguration(IpConfiguration configuration) |