diff options
author | Roshan Pius <rpius@google.com> | 2016-06-15 17:36:20 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2016-06-15 17:36:20 +0000 |
commit | 6ce93571aaab3f258789fcb9bb2c667d0c2e0bd5 (patch) | |
tree | 4d120007de115832716f34c07cbb911b86c246cd /service | |
parent | f89e647e16291557f8c882a89125e53b7f7af004 (diff) | |
parent | 642b0bb43ed856bac0503d3169d67026de2c1b02 (diff) |
Merge changes I83266631,I5bb152e9
* changes:
XmlUtil: Add WifiEnterpriseConfigXmlUtil
XmlUtil: Report all parsing errors
Diffstat (limited to 'service')
-rw-r--r-- | service/java/com/android/server/wifi/WifiBackupRestore.java | 51 | ||||
-rw-r--r-- | service/java/com/android/server/wifi/util/XmlUtil.java | 276 |
2 files changed, 256 insertions, 71 deletions
diff --git a/service/java/com/android/server/wifi/WifiBackupRestore.java b/service/java/com/android/server/wifi/WifiBackupRestore.java index 02954d4a8..b902ffd6b 100644 --- a/service/java/com/android/server/wifi/WifiBackupRestore.java +++ b/service/java/com/android/server/wifi/WifiBackupRestore.java @@ -177,14 +177,11 @@ public class WifiBackupRestore { private void writeNetworkConfigurationToXml(XmlSerializer out, WifiConfiguration configuration) throws XmlPullParserException, IOException { XmlUtil.writeNextSectionStart(out, XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION); - WifiConfigurationXmlUtil.writeWifiConfigurationToXmlForBackup(out, configuration); + WifiConfigurationXmlUtil.writeToXmlForBackup(out, configuration); XmlUtil.writeNextSectionEnd(out, XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION); - if (configuration.getIpConfiguration() != null) { - XmlUtil.writeNextSectionStart(out, XML_TAG_SECTION_HEADER_IP_CONFIGURATION); - IpConfigurationXmlUtil.writeIpConfigurationToXml( - out, configuration.getIpConfiguration()); - XmlUtil.writeNextSectionEnd(out, XML_TAG_SECTION_HEADER_IP_CONFIGURATION); - } + XmlUtil.writeNextSectionStart(out, XML_TAG_SECTION_HEADER_IP_CONFIGURATION); + IpConfigurationXmlUtil.writeToXml(out, configuration.getIpConfiguration()); + XmlUtil.writeNextSectionEnd(out, XML_TAG_SECTION_HEADER_IP_CONFIGURATION); } /** @@ -239,16 +236,12 @@ public class WifiBackupRestore { XmlPullParser in, int outerTagDepth, int dataVersion) throws XmlPullParserException, IOException { // Find the configuration list section. - if (!XmlUtil.gotoNextSection( - in, XML_TAG_SECTION_HEADER_NETWORK_LIST, outerTagDepth)) { - Log.e(TAG, "Error parsing the backup data. Did not find network list"); - // Malformed XML input, bail out. - return null; - } + XmlUtil.gotoNextSectionWithName(in, XML_TAG_SECTION_HEADER_NETWORK_LIST, outerTagDepth); // Find all the configurations within the configuration list section. int networkListTagDepth = outerTagDepth + 1; List<WifiConfiguration> configurations = new ArrayList<>(); - while (XmlUtil.gotoNextSection(in, XML_TAG_SECTION_HEADER_NETWORK, networkListTagDepth)) { + while (XmlUtil.gotoNextSectionWithNameOrEnd( + in, XML_TAG_SECTION_HEADER_NETWORK, networkListTagDepth)) { WifiConfiguration configuration = parseNetworkConfigurationFromXml(in, dataVersion, networkListTagDepth); if (configuration != null) { @@ -266,7 +259,7 @@ public class WifiBackupRestore { XmlPullParser in, int outerTagDepth) throws XmlPullParserException, IOException { Pair<String, WifiConfiguration> parsedConfig = - WifiConfigurationXmlUtil.parseWifiConfigurationFromXml(in, outerTagDepth); + WifiConfigurationXmlUtil.parseFromXml(in, outerTagDepth); if (parsedConfig == null || parsedConfig.first == null || parsedConfig.second == null) { return null; } @@ -305,23 +298,19 @@ public class WifiBackupRestore { WifiConfiguration configuration = null; int networkTagDepth = outerTagDepth + 1; // Retrieve WifiConfiguration object first. - if (XmlUtil.gotoNextSection( - in, XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION, networkTagDepth)) { - int configTagDepth = networkTagDepth + 1; - configuration = - parseWifiConfigurationFromXmlAndValidateConfigKey(in, configTagDepth); - if (configuration == null) { - return null; - } - } - // Now retrieve any IP configuration info if present. - if (XmlUtil.gotoNextSection( - in, XML_TAG_SECTION_HEADER_IP_CONFIGURATION, networkTagDepth)) { - int configTagDepth = networkTagDepth + 1; - IpConfiguration ipConfiguration = - IpConfigurationXmlUtil.parseIpConfigurationFromXml(in, configTagDepth); - configuration.setIpConfiguration(ipConfiguration); + XmlUtil.gotoNextSectionWithName( + in, XML_TAG_SECTION_HEADER_WIFI_CONFIGURATION, networkTagDepth); + int configTagDepth = networkTagDepth + 1; + configuration = parseWifiConfigurationFromXmlAndValidateConfigKey(in, configTagDepth); + if (configuration == null) { + return null; } + // Now retrieve any IP configuration info. + XmlUtil.gotoNextSectionWithName( + in, XML_TAG_SECTION_HEADER_IP_CONFIGURATION, networkTagDepth); + IpConfiguration ipConfiguration = + IpConfigurationXmlUtil.parseFromXml(in, configTagDepth); + configuration.setIpConfiguration(ipConfiguration); return configuration; } return null; diff --git a/service/java/com/android/server/wifi/util/XmlUtil.java b/service/java/com/android/server/wifi/util/XmlUtil.java index 29cb21dee..dffe60205 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.net.wifi.WifiConfiguration.NetworkSelectionStatus; +import android.net.wifi.WifiEnterpriseConfig; import android.util.Log; import android.util.Pair; @@ -100,26 +101,78 @@ public class XmlUtil { } /** - * Move the XML stream to the next section header. The provided outerDepth is used to find - * sub sections within that depth. + * Move the XML stream to the next section header or indicate if there are no more sections. + * The provided outerDepth is used to find sub sections within that depth. + * + * Use this to move across sections if the ordering of sections are variable. The returned name + * can be used to decide what section is next. * * @param in XmlPullParser instance pointing to the XML stream. - * @param headerName expected name for the start tag. + * @param headerName An array of one string, used to return the name of the next section. * @param outerDepth Find section within this depth. - * @return {@code true} if a start tag with the provided name is found, {@code false} otherwise + * @return {@code true} if a next section is found, {@code false} if there are no more sections. * @throws XmlPullParserException if parsing errors occur. */ - public static boolean gotoNextSection(XmlPullParser in, String headerName, int outerDepth) + public static boolean gotoNextSectionOrEnd( + XmlPullParser in, String[] headerName, int outerDepth) throws XmlPullParserException, IOException { - while (XmlUtils.nextElementWithin(in, outerDepth)) { - if (in.getName().equals(headerName)) { + if (XmlUtils.nextElementWithin(in, outerDepth)) { + headerName[0] = in.getName(); + return true; + } + return false; + } + + /** + * Move the XML stream to the next section header or indicate if there are no more sections. + * If a section, exists ensure that the name matches the provided name. + * The provided outerDepth is used to find sub sections within that depth. + * + * Use this to move across repeated sections until the end. + * + * @param in XmlPullParser instance pointing to the XML stream. + * @param expectedName expected name for the section header. + * @param outerDepth Find section within this depth. + * @return {@code true} if a next section is found, {@code false} if there are no more sections. + * @throws XmlPullParserException if the section header name does not match |expectedName|, + * or if parsing errors occur. + */ + public static boolean gotoNextSectionWithNameOrEnd( + XmlPullParser in, String expectedName, int outerDepth) + throws XmlPullParserException, IOException { + String[] headerName = new String[1]; + if (gotoNextSectionOrEnd(in, headerName, outerDepth)) { + if (headerName[0].equals(expectedName)) { return true; } + throw new XmlPullParserException( + "Next section name does not match expected name: " + expectedName); } return false; } /** + * Move the XML stream to the next section header and ensure that the name matches the provided + * name. + * The provided outerDepth is used to find sub sections within that depth. + * + * Use this to move across sections if the ordering of sections are fixed. + * + * @param in XmlPullParser instance pointing to the XML stream. + * @param expectedName expected name for the section header. + * @param outerDepth Find section within this depth. + * @throws XmlPullParserException if the section header name does not match |expectedName|, + * there are no more sections or if parsing errors occur. + */ + public static void gotoNextSectionWithName( + XmlPullParser in, String expectedName, int outerDepth) + throws XmlPullParserException, IOException { + if (!gotoNextSectionWithNameOrEnd(in, expectedName, outerDepth)) { + throw new XmlPullParserException("Section not found. Expected: " + expectedName); + } + } + + /** * Checks if the stream is at the end of a section of values. This moves the stream to next tag * and checks if it finds an end tag at the specified depth. * @@ -130,7 +183,7 @@ public class XmlUtil { */ public static boolean isNextSectionEnd(XmlPullParser in, int sectionDepth) throws XmlPullParserException, IOException { - return (in.nextTag() == XmlPullParser.END_TAG && in.getDepth() == sectionDepth); + return !XmlUtils.nextElementWithin(in, sectionDepth); } /** @@ -313,8 +366,8 @@ public class XmlUtil { * @param out XmlSerializer instance pointing to the XML stream. * @param configuration WifiConfiguration object to be serialized. */ - public static void writeCommonWifiConfigurationElementsToXml(XmlSerializer out, - WifiConfiguration configuration) + public static void writeCommonElementsToXml( + XmlSerializer out, WifiConfiguration configuration) throws XmlPullParserException, IOException { XmlUtil.writeNextValue(out, XML_TAG_CONFIG_KEY, configuration.configKey()); XmlUtil.writeNextValue(out, XML_TAG_SSID, configuration.SSID); @@ -343,21 +396,22 @@ public class XmlUtil { * @param out XmlSerializer instance pointing to the XML stream. * @param configuration WifiConfiguration object to be serialized. */ - public static void writeWifiConfigurationToXmlForBackup(XmlSerializer out, - WifiConfiguration configuration) + public static void writeToXmlForBackup(XmlSerializer out, WifiConfiguration configuration) throws XmlPullParserException, IOException { - writeCommonWifiConfigurationElementsToXml(out, configuration); + writeCommonElementsToXml(out, configuration); } /** * Write the Configuration data elements for config store from the provided Configuration * to the XML stream. + * + * @param out XmlSerializer instance pointing to the XML stream. + * @param configuration WifiConfiguration object to be serialized. */ - public static void writeWifiConfigurationToXmlForConfigStore(XmlSerializer out, - WifiConfiguration configuration) + public static void writeToXmlForConfigStore( + XmlSerializer out, WifiConfiguration configuration) throws XmlPullParserException, IOException { - writeCommonWifiConfigurationElementsToXml(out, configuration); - // TODO: Will need to add more elements which needs to be persisted. + writeCommonElementsToXml(out, configuration); XmlUtil.writeNextValue(out, XML_TAG_FQDN, configuration.FQDN); XmlUtil.writeNextValue( out, XML_TAG_PROVIDER_FRIENDLY_NAME, configuration.providerFriendlyName); @@ -418,7 +472,7 @@ public class XmlUtil { * @return Pair<Config key, WifiConfiguration object> if parsing is successful, * null otherwise. */ - public static Pair<String, WifiConfiguration> parseWifiConfigurationFromXml( + public static Pair<String, WifiConfiguration> parseFromXml( XmlPullParser in, int outerTagDepth) throws XmlPullParserException, IOException { WifiConfiguration configuration = new WifiConfiguration(); @@ -429,8 +483,7 @@ public class XmlUtil { String[] valueName = new String[1]; Object value = XmlUtil.readCurrentValue(in, valueName); if (valueName[0] == null) { - Log.e(TAG, "Missing value name"); - return null; + throw new XmlPullParserException("Missing value name"); } switch (valueName[0]) { case XML_TAG_CONFIG_KEY: @@ -518,8 +571,8 @@ public class XmlUtil { configuration.lastConnectUid = (int) value; break; default: - Log.e(TAG, "Unknown value name found: " + valueName[0]); - return null; + throw new XmlPullParserException( + "Unknown value name found: " + valueName[0]); } } return Pair.create(configKeyInData, configuration); @@ -550,8 +603,8 @@ public class XmlUtil { /** * Write the static IP configuration data elements to XML stream. */ - private static void writeStaticIpConfigurationToXml(XmlSerializer out, - StaticIpConfiguration staticIpConfiguration) + private static void writeStaticIpConfigurationToXml( + XmlSerializer out, StaticIpConfiguration staticIpConfiguration) throws XmlPullParserException, IOException { if (staticIpConfiguration.ipAddress != null) { XmlUtil.writeNextValue( @@ -593,9 +646,11 @@ public class XmlUtil { /** * Write the IP configuration data elements from the provided Configuration to the XML * stream. + * + * @param out XmlSerializer instance pointing to the XML stream. + * @param ipConfiguration IpConfiguration object to be serialized. */ - public static void writeIpConfigurationToXml(XmlSerializer out, - IpConfiguration ipConfiguration) + public static void writeToXml(XmlSerializer out, IpConfiguration ipConfiguration) throws XmlPullParserException, IOException { // Write IP assignment settings XmlUtil.writeNextValue(out, XML_TAG_IP_ASSIGNMENT, @@ -689,8 +744,7 @@ public class XmlUtil { * @param outerTagDepth depth of the outer tag in the XML document. * @return IpConfiguration object if parsing is successful, null otherwise. */ - public static IpConfiguration parseIpConfigurationFromXml(XmlPullParser in, - int outerTagDepth) + public static IpConfiguration parseFromXml(XmlPullParser in, int outerTagDepth) throws XmlPullParserException, IOException { IpConfiguration ipConfiguration = new IpConfiguration(); @@ -707,8 +761,7 @@ public class XmlUtil { case UNASSIGNED: break; default: - Log.wtf(TAG, "Unknown ip assignment type: " + ipAssignment); - return null; + throw new XmlPullParserException("Unknown ip assignment type: " + ipAssignment); } // Parse out the proxy settings next. @@ -737,8 +790,8 @@ public class XmlUtil { case UNASSIGNED: break; default: - Log.wtf(TAG, "Unknown proxy settings type: " + proxySettings); - return null; + throw new XmlPullParserException( + "Unknown proxy settings type: " + proxySettings); } return ipConfiguration; } @@ -762,9 +815,11 @@ public class XmlUtil { /** * Write the NetworkSelectionStatus data elements from the provided status to the XML * stream. + * + * @param out XmlSerializer instance pointing to the XML stream. + * @param status NetworkSelectionStatus object to be serialized. */ - public static void writeNetworkSelectionStatusToXml(XmlSerializer out, - NetworkSelectionStatus status) + public static void writeToXml(XmlSerializer out, NetworkSelectionStatus status) throws XmlPullParserException, IOException { // Don't persist blacklists across reboots. So, if the status is temporarily disabled, // store the status as enabled. This will ensure that when the device reboots, it is @@ -790,8 +845,7 @@ public class XmlUtil { * @param outerTagDepth depth of the outer tag in the XML document. * @return NetworkSelectionStatus object if parsing is successful, null otherwise. */ - public static NetworkSelectionStatus parseNetworkSelectionStatusFromXml(XmlPullParser in, - int outerTagDepth) + public static NetworkSelectionStatus parseFromXml(XmlPullParser in, int outerTagDepth) throws XmlPullParserException, IOException { NetworkSelectionStatus status = new NetworkSelectionStatus(); @@ -800,8 +854,7 @@ public class XmlUtil { String[] valueName = new String[1]; Object value = XmlUtil.readCurrentValue(in, valueName); if (valueName[0] == null) { - Log.e(TAG, "Missing value name"); - return null; + throw new XmlPullParserException("Missing value name"); } switch (valueName[0]) { case XML_TAG_SELECTION_STATUS: @@ -820,12 +873,155 @@ public class XmlUtil { status.setHasEverConnected((boolean) value); break; default: - Log.e(TAG, "Unknown value name found: " + valueName[0]); - return null; + throw new XmlPullParserException( + "Unknown value name found: " + valueName[0]); } } return status; } } + + /** + * Utility class to serialize and deseriaize {@link WifiEnterpriseConfig} object to XML & + * vice versa. This is used by {@link com.android.server.wifi.WifiConfigStore} module. + */ + public static class WifiEnterpriseConfigXmlUtil { + + /** + * List of XML tags corresponding to WifiEnterpriseConfig object elements. + */ + public static final String XML_TAG_IDENTITY = "Identity"; + public static final String XML_TAG_ANON_IDENTITY = "AnonIdentity"; + public static final String XML_TAG_PASSWORD = "Password"; + public static final String XML_TAG_CLIENT_CERT = "ClientCert"; + public static final String XML_TAG_CA_CERT = "CaCert"; + public static final String XML_TAG_SUBJECT_MATCH = "SubjectMatch"; + public static final String XML_TAG_ENGINE = "Engine"; + public static final String XML_TAG_ENGINE_ID = "EngineId"; + public static final String XML_TAG_PRIVATE_KEY_ID = "PrivateKeyId"; + public static final String XML_TAG_ALT_SUBJECT_MATCH = "AltSubjectMatch"; + public static final String XML_TAG_DOM_SUFFIX_MATCH = "DomSuffixMatch"; + public static final String XML_TAG_CA_PATH = "CaPath"; + public static final String XML_TAG_EAP_METHOD = "EapMethod"; + public static final String XML_TAG_PHASE2_METHOD = "Phase2Method"; + + /** + * Write the WifiEnterpriseConfig data elements from the provided config to the XML + * stream. + * + * @param out XmlSerializer instance pointing to the XML stream. + * @param enterpriseConfig WifiEnterpriseConfig object to be serialized. + */ + public static void writeToXml(XmlSerializer out, WifiEnterpriseConfig enterpriseConfig) + throws XmlPullParserException, IOException { + XmlUtil.writeNextValue(out, XML_TAG_IDENTITY, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_ANON_IDENTITY, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_PASSWORD, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_CLIENT_CERT, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_CA_CERT, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_SUBJECT_MATCH, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_ENGINE, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_ENGINE_ID, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_PRIVATE_KEY_ID, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_ALT_SUBJECT_MATCH, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_DOM_SUFFIX_MATCH, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_CA_PATH, + enterpriseConfig.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY, "")); + XmlUtil.writeNextValue(out, XML_TAG_EAP_METHOD, enterpriseConfig.getEapMethod()); + XmlUtil.writeNextValue(out, XML_TAG_PHASE2_METHOD, enterpriseConfig.getPhase2Method()); + } + + /** + * Parses the data elements from the provided XML stream to a WifiEnterpriseConfig object. + * + * @param in XmlPullParser instance pointing to the XML stream. + * @param outerTagDepth depth of the outer tag in the XML document. + * @return WifiEnterpriseConfig object if parsing is successful, null otherwise. + */ + public static WifiEnterpriseConfig parseFromXml(XmlPullParser in, int outerTagDepth) + throws XmlPullParserException, IOException { + WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig(); + + // Loop through and parse out all the elements from the stream within this section. + while (!XmlUtil.isNextSectionEnd(in, outerTagDepth)) { + String[] valueName = new String[1]; + Object value = XmlUtil.readCurrentValue(in, valueName); + if (valueName[0] == null) { + throw new XmlPullParserException("Missing value name"); + } + switch (valueName[0]) { + case XML_TAG_IDENTITY: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.IDENTITY_KEY, (String) value); + break; + case XML_TAG_ANON_IDENTITY: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.ANON_IDENTITY_KEY, (String) value); + break; + case XML_TAG_PASSWORD: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.PASSWORD_KEY, (String) value); + break; + case XML_TAG_CLIENT_CERT: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.CLIENT_CERT_KEY, (String) value); + break; + case XML_TAG_CA_CERT: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.CA_CERT_KEY, (String) value); + break; + case XML_TAG_SUBJECT_MATCH: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.SUBJECT_MATCH_KEY, (String) value); + break; + case XML_TAG_ENGINE: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.ENGINE_KEY, (String) value); + break; + case XML_TAG_ENGINE_ID: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.ENGINE_ID_KEY, (String) value); + break; + case XML_TAG_PRIVATE_KEY_ID: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY, (String) value); + break; + case XML_TAG_ALT_SUBJECT_MATCH: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY, (String) value); + break; + case XML_TAG_DOM_SUFFIX_MATCH: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY, (String) value); + break; + case XML_TAG_CA_PATH: + enterpriseConfig.setFieldValue( + WifiEnterpriseConfig.CA_PATH_KEY, (String) value); + break; + case XML_TAG_EAP_METHOD: + enterpriseConfig.setEapMethod((int) value); + break; + case XML_TAG_PHASE2_METHOD: + enterpriseConfig.setPhase2Method((int) value); + break; + default: + throw new XmlPullParserException( + "Unknown value name found: " + valueName[0]); + } + } + return enterpriseConfig; + } + } } |