This source file includes following definitions.
- createTestXML
- createUpdateXML
- runSuccessTest
- runFailureTest
- Feature
- testValidAllTypes
- Feature
- testValidNoInstall
- Feature
- testValidNoPing
- Feature
- testValidNoUpdatecheck
- Feature
- testValidUpdatecheckNoUpdate
- Feature
- testValidUpdatecheckError
- Feature
- testValidUpdatecheckUnknown
- Feature
- testValidAppStatusRestricted
- Feature
- testFailBogusResponse
- Feature
- testBadResponseProtocol
- Feature
- testFailMissingDaystart
- Feature
- testAppTagMissingUpdatecheck
- Feature
- testAppTagUnexpectedUpdatecheck
- Feature
- testAppTagMissingPing
- Feature
- testAppTagUnexpectedPing
- Feature
- testAppTagMissingInstall
- Feature
- testAppTagUnexpectedInstall
- Feature
- testAppTagStatusError
- Feature
- testUpdatecheckMissingUrl
package org.chromium.chrome.browser.omaha;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Xml;
import org.chromium.base.test.util.Feature;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.StringWriter;
public class ResponseParserTest extends InstrumentationTestCase {
private static final String STRIPPED_URL =
"https://play.google.com/store/apps/details?id=com.google.android.apps.chrome";
private static final String URL = STRIPPED_URL + "/";
private static final String NEXT_VERSION = "1.2.3.4";
private static final String APP_STATUS_OK = "ok";
private static final String APP_STATUS_RESTRICTED = "restricted";
private static final String APP_STATUS_ERROR = "error-whatever-else";
private static final String UPDATE_STATUS_OK = "ok";
private static final String UPDATE_STATUS_NOUPDATE = "noupdate";
private static final String UPDATE_STATUS_ERROR = "error-osnotsupported";
private static final String UPDATE_STATUS_WTF = "omgwtfbbq";
private static String createTestXML(String xmlProtocol, String elapsedSeconds,
String appStatus, boolean addInstall, boolean addPing, String updateStatus,
String updateUrl) {
StringWriter writer = new StringWriter();
try {
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(writer);
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "response");
serializer.attribute(null, "server", "prod");
if (xmlProtocol != null) {
serializer.attribute(null, "protocol", xmlProtocol);
}
if (elapsedSeconds != null) {
serializer.startTag(null, "daystart");
serializer.attribute(null, "elapsed_seconds", elapsedSeconds);
serializer.endTag(null, "daystart");
}
serializer.startTag(null, "app");
serializer.attribute(null, "appid", "{APP_ID}");
serializer.attribute(null, "status", appStatus);
serializer.attribute(null, "unused", "attribute");
if (addInstall) {
serializer.startTag(null, "event");
serializer.attribute(null, "status", "ok");
serializer.endTag(null, "event");
}
if (addPing) {
serializer.startTag(null, "ping");
serializer.attribute(null, "status", "ok");
serializer.endTag(null, "ping");
}
if (updateStatus != null) {
serializer.startTag(null, "updatecheck");
serializer.attribute(null, "status", updateStatus);
if (UPDATE_STATUS_OK.equals(updateStatus)) {
createUpdateXML(serializer, updateUrl);
}
serializer.endTag(null, "updatecheck");
}
serializer.endTag(null, "app");
serializer.startTag(null, "extraneous");
serializer.attribute(null, "useless", "yes");
serializer.endTag(null, "extraneous");
serializer.endTag(null, "response");
serializer.endDocument();
} catch (IOException e) {
fail("Caught an IOException creating the XML: " + e);
} catch (IllegalArgumentException e) {
fail("Caught an IllegalArgumentException creating the XML: " + e);
} catch (IllegalStateException e) {
fail("Caught an IllegalStateException creating the XML: " + e);
}
return writer.toString();
}
private static void createUpdateXML(XmlSerializer serializer, String updateUrl)
throws IOException {
serializer.startTag(null, "urls");
if (updateUrl != null) {
serializer.startTag(null, "url");
serializer.attribute(null, "codebase", updateUrl);
serializer.endTag(null, "url");
}
serializer.endTag(null, "urls");
serializer.startTag(null, "manifest");
serializer.attribute(null, "garbage", "attribute");
serializer.attribute(null, "version", NEXT_VERSION);
serializer.startTag(null, "packages");
serializer.startTag(null, "package");
serializer.attribute(null, "hash", "0");
serializer.attribute(null, "name", "dummy.apk");
serializer.attribute(null, "required", "true");
serializer.attribute(null, "size", "0");
serializer.endTag(null, "package");
serializer.endTag(null, "packages");
serializer.startTag(null, "actions");
serializer.startTag(null, "action");
serializer.attribute(null, "event", "install");
serializer.attribute(null, "run", "dummy.apk");
serializer.endTag(null, "action");
serializer.startTag(null, "action");
serializer.attribute(null, "event", "postinstall");
serializer.endTag(null, "action");
serializer.endTag(null, "actions");
serializer.endTag(null, "manifest");
serializer.startTag(null, "dummy");
serializer.attribute(null, "hopefully", "ignored");
serializer.endTag(null, "dummy");
}
private static void runSuccessTest(String appStatus, boolean addInstall, boolean addPing,
String updateStatus) throws RequestFailureException {
String xml =
createTestXML("3.0", "12345", appStatus, addInstall, addPing, updateStatus, URL);
ResponseParser parser =
new ResponseParser(true, "{APP_ID}", addInstall, addPing, updateStatus != null);
parser.parseResponse(xml);
assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystartSeconds());
assertEquals("<app> status doesn't match.", appStatus, parser.getAppStatus());
assertEquals("<updatecheck> status doesn't match.", updateStatus, parser.getUpdateStatus());
if (UPDATE_STATUS_OK.equals(updateStatus)) {
assertEquals("Version number doesn't match.", "1.2.3.4", parser.getNewVersion());
assertEquals("Market URL doesn't match.", STRIPPED_URL, parser.getURL());
} else {
assertEquals("Version number doesn't match.", null, parser.getNewVersion());
assertEquals("Market URL doesn't match.", null, parser.getURL());
}
}
private static void runFailureTest(String xml, int expectedErrorCode,
boolean expectInstall, boolean expectPing, boolean expectUpdate) {
ResponseParser parser =
new ResponseParser(true, "{APP_ID}", expectInstall, expectPing, expectUpdate);
try {
parser.parseResponse(xml);
} catch (RequestFailureException e) {
assertEquals("Incorrect error code received.", expectedErrorCode, e.errorCode);
return;
}
fail("Failed to throw RequestFailureException for bad XML.");
}
@SmallTest
@Feature({"Omaha"})
public void testValidAllTypes() throws RequestFailureException {
runSuccessTest(APP_STATUS_OK, true, true, UPDATE_STATUS_OK);
}
@SmallTest
@Feature({"Omaha"})
public void testValidNoInstall() throws RequestFailureException {
runSuccessTest(APP_STATUS_OK, false, true, UPDATE_STATUS_OK);
}
@SmallTest
@Feature({"Omaha"})
public void testValidNoPing() throws RequestFailureException {
runSuccessTest(APP_STATUS_OK, true, false, UPDATE_STATUS_OK);
}
@SmallTest
@Feature({"Omaha"})
public void testValidNoUpdatecheck() throws RequestFailureException {
runSuccessTest(APP_STATUS_OK, true, true, null);
}
@SmallTest
@Feature({"Omaha"})
public void testValidUpdatecheckNoUpdate() throws RequestFailureException {
runSuccessTest(APP_STATUS_OK, false, false, UPDATE_STATUS_NOUPDATE);
}
@SmallTest
@Feature({"Omaha"})
public void testValidUpdatecheckError() throws RequestFailureException {
runSuccessTest(APP_STATUS_OK, false, false, UPDATE_STATUS_ERROR);
}
@SmallTest
@Feature({"Omaha"})
public void testValidUpdatecheckUnknown() throws RequestFailureException {
runSuccessTest(APP_STATUS_OK, false, false, UPDATE_STATUS_WTF);
}
@SmallTest
@Feature({"Omaha"})
public void testValidAppStatusRestricted() throws RequestFailureException {
runSuccessTest(APP_STATUS_RESTRICTED, false, false, null);
}
@SmallTest
@Feature({"Omaha"})
public void testFailBogusResponse() {
String xml = "Bogus";
runFailureTest(xml, RequestFailureException.ERROR_MALFORMED_XML, false, false, false);
}
@SmallTest
@Feature({"Omaha"})
public void testBadResponseProtocol() {
String xml =
createTestXML("2.0", "12345", APP_STATUS_OK, false, false, UPDATE_STATUS_OK, URL);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_RESPONSE, false, false, false);
}
@SmallTest
@Feature({"Omaha"})
public void testFailMissingDaystart() {
String xml =
createTestXML("3.0", null, APP_STATUS_OK, false, false, UPDATE_STATUS_OK, URL);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_DAYSTART, false, false, true);
}
@SmallTest
@Feature({"Omaha"})
public void testAppTagMissingUpdatecheck() {
String xml =
createTestXML("3.0", "12345", APP_STATUS_OK, true, false, null, URL);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_UPDATECHECK, true, false, true);
}
@SmallTest
@Feature({"Omaha"})
public void testAppTagUnexpectedUpdatecheck() {
String xml =
createTestXML("3.0", "12345", APP_STATUS_OK, true, false, UPDATE_STATUS_OK, URL);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_UPDATECHECK, true, false, false);
}
@SmallTest
@Feature({"Omaha"})
public void testAppTagMissingPing() {
String xml =
createTestXML("3.0", "12345", APP_STATUS_OK, false, false, UPDATE_STATUS_OK, URL);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_PING, false, true, true);
}
@SmallTest
@Feature({"Omaha"})
public void testAppTagUnexpectedPing() {
String xml =
createTestXML("3.0", "12345", APP_STATUS_OK, false, true, UPDATE_STATUS_OK, URL);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_PING, false, false, true);
}
@SmallTest
@Feature({"Omaha"})
public void testAppTagMissingInstall() {
String xml =
createTestXML("3.0", "12345", APP_STATUS_OK, false, false, UPDATE_STATUS_OK, URL);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_EVENT, true, false, true);
}
@SmallTest
@Feature({"Omaha"})
public void testAppTagUnexpectedInstall() {
String xml =
createTestXML("3.0", "12345", APP_STATUS_OK, true, false, UPDATE_STATUS_OK, URL);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_EVENT, false, false, true);
}
@SmallTest
@Feature({"Omaha"})
public void testAppTagStatusError() {
String xml =
createTestXML("3.0", "12345", APP_STATUS_ERROR, false, false, null, URL);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_APP, false, false, false);
}
@SmallTest
@Feature({"Omaha"})
public void testUpdatecheckMissingUrl() {
String xml = createTestXML(
"3.0", "12345", APP_STATUS_OK, false, false, UPDATE_STATUS_OK, null);
runFailureTest(xml, RequestFailureException.ERROR_PARSE_URLS, false, false, true);
}
}