Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.omaha; | |
| 6 | |
| 7 import android.test.InstrumentationTestCase; | |
| 8 import android.test.suitebuilder.annotation.SmallTest; | |
| 9 | |
| 10 import org.chromium.base.test.util.Feature; | |
| 11 | |
| 12 public class ResponseParserTest extends InstrumentationTestCase { | |
| 13 // Note that the Omaha server appends "/" to the end of the URL codebase. | |
| 14 private static final String STRIPPED_MARKET_URL = | |
| 15 "https://market.android.com/details?id=com.google.android.apps.chrom e"; | |
|
nyquist
2013/11/11 17:43:41
Is this still the URL we want?
Could we use https:
gone
2013/11/11 18:22:34
Done.
| |
| 16 private static final String MARKET_URL = STRIPPED_MARKET_URL + "/"; | |
| 17 | |
| 18 private static final String UPDATE_STATUS_OK = "ok"; | |
| 19 private static final String UPDATE_STATUS_NOUPDATE = "noupdate"; | |
| 20 private static final String UPDATE_STATUS_ERROR = "error-osnotsupported"; | |
| 21 private static final String UPDATE_STATUS_WTF = "omgwtfbbq"; | |
| 22 | |
| 23 private static final String VALID_XML = | |
| 24 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
| 25 "<response protocol=\"3.0\" server=\"prod\">" + | |
| 26 " <daystart elapsed_seconds=\"12345\"/>" + | |
| 27 " <app appid=\"{APP_ID}\" status=\"ok\">" + | |
| 28 " <updatecheck status=\"" + UPDATE_STATUS_OK + "\">" + | |
| 29 " <urls>" + | |
| 30 " <url codebase=\"" + MARKET_URL + "\"/>" + | |
| 31 " </urls>" + | |
| 32 " <manifest version=\"1.2.3.4\">" + | |
| 33 " <packages>" + | |
| 34 " <package hash=\"0\" name=\"dummy.apk\" required=\"true\" size =\"0\"/>" + | |
| 35 " </packages>" + | |
| 36 " <actions>" + | |
| 37 " <action event=\"install\" run=\"dummy.apk\"/>" + | |
| 38 " <action event=\"postinstall\"/>" + | |
| 39 " </actions>" + | |
| 40 " </manifest>" + | |
| 41 " </updatecheck>" + | |
| 42 " <ping status=\"ok\"/>" + | |
| 43 " </app>" + | |
| 44 "</response>"; | |
| 45 | |
| 46 private static final String VALID_XML_WITH_INSTALL = | |
| 47 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
| 48 "<response protocol=\"3.0\" server=\"prod\">" + | |
| 49 " <daystart elapsed_seconds=\"12345\"/>" + | |
| 50 " <app appid=\"{APP_ID}\" status=\"ok\">" + | |
| 51 " <event status=\"ok\"/>" + | |
| 52 " </app>" + | |
| 53 "</response>"; | |
| 54 | |
| 55 private static final String VALID_XML_UP_TO_DATE = | |
| 56 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
| 57 "<response protocol=\"3.0\" server=\"prod\">" + | |
| 58 " <daystart elapsed_seconds=\"12345\"/>" + | |
| 59 " <app appid=\"{APP_ID}\" status=\"ok\">" + | |
| 60 " <updatecheck status=\"" + UPDATE_STATUS_NOUPDATE + "\"/>" + | |
| 61 " <ping status=\"ok\"/>" + | |
| 62 " </app>" + | |
| 63 "</response>"; | |
| 64 | |
| 65 private static final String UPDATE_NO_PING = | |
| 66 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
| 67 "<response protocol=\"3.0\" server=\"prod\">" + | |
| 68 " <daystart elapsed_seconds=\"12345\"/>" + | |
| 69 " <app appid=\"{APP_ID}\" status=\"ok\">" + | |
| 70 " <updatecheck status=\"" + UPDATE_STATUS_OK + "\">" + | |
| 71 " <urls>" + | |
| 72 " <url codebase=\"" + MARKET_URL + "\"/>" + | |
| 73 " </urls>" + | |
| 74 " <manifest version=\"1.2.3.4\">" + | |
| 75 " <packages>" + | |
| 76 " <package hash=\"0\" name=\"dummy.apk\" required=\"true\" size =\"0\"/>" + | |
| 77 " </packages>" + | |
| 78 " <actions>" + | |
| 79 " <action event=\"install\" run=\"dummy.apk\"/>" + | |
| 80 " <action event=\"postinstall\"/>" + | |
| 81 " </actions>" + | |
| 82 " </manifest>" + | |
| 83 " </updatecheck>" + | |
| 84 " </app>" + | |
| 85 "</response>"; | |
| 86 | |
| 87 private static final String PING_NO_UPDATE = | |
| 88 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
| 89 "<response protocol=\"3.0\" server=\"prod\">" + | |
| 90 " <daystart elapsed_seconds=\"12345\"/>" + | |
| 91 " <app appid=\"{APP_ID}\" status=\"ok\">" + | |
| 92 " <ping status=\"ok\"/>" + | |
| 93 " </app>" + | |
| 94 "</response>"; | |
| 95 | |
| 96 private static final String ERROR_UPDATE_STATUS = | |
| 97 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
| 98 "<response protocol=\"3.0\" server=\"prod\">" + | |
| 99 " <daystart elapsed_seconds=\"12345\"/>" + | |
| 100 " <app appid=\"{APP_ID}\" status=\"ok\">" + | |
| 101 " <updatecheck status=\"" + UPDATE_STATUS_ERROR + "\"/>" + | |
| 102 " <ping status=\"ok\"/>" + | |
| 103 " </app>" + | |
| 104 "</response>"; | |
| 105 | |
| 106 private static final String BAD_UPDATE_STATUS = | |
| 107 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
| 108 "<response protocol=\"3.0\" server=\"prod\">" + | |
| 109 " <daystart elapsed_seconds=\"12345\"/>" + | |
| 110 " <app appid=\"{APP_ID}\" status=\"ok\">" + | |
| 111 " <updatecheck status=\"" + UPDATE_STATUS_WTF + "\"/>" + | |
| 112 " <ping status=\"ok\"/>" + | |
| 113 " </app>" + | |
| 114 "</response>"; | |
| 115 | |
| 116 private static final String NO_DAYSTART = | |
| 117 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
| 118 "<response protocol=\"3.0\" server=\"prod\">" + | |
| 119 " <app appid=\"{APP_ID}\" status=\"ok\">" + | |
| 120 " <updatecheck status=\"" + UPDATE_STATUS_NOUPDATE + "\"/>" + | |
| 121 " <ping status=\"ok\"/>" + | |
| 122 " </app>" + | |
| 123 "</response>"; | |
| 124 | |
| 125 private static final String INVALID_TAGS = | |
| 126 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + | |
| 127 "<response protocol=\"3.0\" server=\"prod\">" + | |
| 128 " <app appid=\"{APP_ID}\" status=\"ok\">" + | |
| 129 " <dummy/>" + | |
| 130 " </app>" + | |
| 131 "</response>"; | |
| 132 | |
| 133 private static final String BOGUS_RESPONSE = "Bogus"; | |
| 134 | |
| 135 @SmallTest | |
| 136 @Feature({"Omaha"}) | |
| 137 public void testValidXML() throws RequestFailureException { | |
| 138 ResponseParser parser = new ResponseParser(VALID_XML, "{APP_ID}", false) ; | |
| 139 assertEquals("Version number doesn't match.", "1.2.3.4", parser.getMarke tVersion()); | |
| 140 assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystart Seconds()); | |
| 141 assertEquals("Market URL doesn't match.", STRIPPED_MARKET_URL, parser.ge tURL()); | |
| 142 assertEquals("Update status doesn't match.", UPDATE_STATUS_OK, parser.ge tUpdateStatus()); | |
| 143 } | |
| 144 | |
| 145 @SmallTest | |
| 146 @Feature({"Omaha"}) | |
| 147 public void testValidXMLUpdateError() throws RequestFailureException { | |
| 148 ResponseParser parser = new ResponseParser(ERROR_UPDATE_STATUS, "{APP_ID }", false); | |
| 149 assertEquals("Version number doesn't match.", null, parser.getMarketVers ion()); | |
| 150 assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystart Seconds()); | |
| 151 assertEquals("Market URL doesn't match.", null, parser.getURL()); | |
| 152 assertEquals("Update status doesn't match.", UPDATE_STATUS_ERROR, parser .getUpdateStatus()); | |
| 153 } | |
| 154 | |
| 155 @SmallTest | |
| 156 @Feature({"Omaha"}) | |
| 157 public void testValidXMLWithInstallEvent() throws RequestFailureException { | |
| 158 ResponseParser parser = new ResponseParser(VALID_XML_WITH_INSTALL, "{APP _ID}", true); | |
| 159 assertEquals("Version number doesn't match.", null, parser.getMarketVers ion()); | |
| 160 assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystart Seconds()); | |
| 161 assertEquals("Market URL doesn't match.", null, parser.getURL()); | |
| 162 assertEquals("Update status doesn't match.", null, parser.getUpdateStatu s()); | |
| 163 } | |
| 164 | |
| 165 @SmallTest | |
| 166 @Feature({"Omaha"}) | |
| 167 public void testValidXMLNoUpdateNecessary() throws RequestFailureException { | |
| 168 ResponseParser parser = new ResponseParser(VALID_XML_UP_TO_DATE, "{APP_I D}", false); | |
| 169 assertEquals("Version number doesn't match.", null, parser.getMarketVers ion()); | |
| 170 assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystart Seconds()); | |
| 171 assertEquals("Market URL doesn't match.", null, parser.getURL()); | |
| 172 assertEquals("Update status doesn't match.", UPDATE_STATUS_NOUPDATE, | |
| 173 parser.getUpdateStatus()); | |
| 174 } | |
| 175 | |
| 176 @SmallTest | |
| 177 @Feature({"Omaha"}) | |
| 178 public void testBogusResponse() throws RequestFailureException { | |
| 179 failureTest(BOGUS_RESPONSE, | |
| 180 "Failed to throw exception when bogus XML was given.", 0); | |
| 181 } | |
| 182 | |
| 183 @SmallTest | |
| 184 @Feature({"Omaha"}) | |
| 185 public void testInvalidTags() throws RequestFailureException { | |
| 186 failureTest(INVALID_TAGS, | |
| 187 "Failed to throw exception when invalid tag was given.", 0); | |
| 188 } | |
| 189 | |
| 190 @SmallTest | |
| 191 @Feature({"Omaha"}) | |
| 192 public void testDaystart() throws RequestFailureException { | |
| 193 failureTest(NO_DAYSTART, | |
| 194 "Failed to throw exception when <daystart> was missing.", 0); | |
| 195 } | |
| 196 | |
| 197 @SmallTest | |
| 198 @Feature({"Omaha"}) | |
| 199 public void testUpdateNoPing() { | |
| 200 failureTest(UPDATE_NO_PING, | |
| 201 "Failed to throw exception when <ping> was missing.", 12345); | |
| 202 } | |
| 203 | |
| 204 @SmallTest | |
| 205 @Feature({"Omaha"}) | |
| 206 public void testPingNoUpdate() { | |
| 207 failureTest(PING_NO_UPDATE, | |
| 208 "Failed to throw exception when <updatecheck> was missing.", 123 45); | |
| 209 } | |
| 210 | |
| 211 @SmallTest | |
| 212 @Feature({"Omaha"}) | |
| 213 public void testBadUpdateStatusIgnored() throws RequestFailureException { | |
| 214 ResponseParser parser = new ResponseParser(BAD_UPDATE_STATUS, "{APP_ID}" , false); | |
| 215 assertEquals("Version number doesn't match.", null, parser.getMarketVers ion()); | |
| 216 assertEquals("elapsed_seconds doesn't match.", 12345, parser.getDaystart Seconds()); | |
| 217 assertEquals("Market URL doesn't match.", null, parser.getURL()); | |
| 218 assertEquals("Update status doesn't match.", UPDATE_STATUS_WTF, parser.g etUpdateStatus()); | |
| 219 } | |
| 220 | |
| 221 /** | |
| 222 * Checks that the parser fails to handle certain inputs. | |
| 223 * @param xml XML that will be parsed. | |
| 224 * @param failureMessage Message that will be printed if the test fails to p ass. | |
| 225 * @param expectedDaystart Expected value of the server's elapsed_seconds va lue. | |
| 226 */ | |
| 227 private void failureTest(String xml, String failureMessage, int expectedDays tart) { | |
| 228 boolean failed = false; | |
| 229 ResponseParser parser = null; | |
| 230 try { | |
| 231 parser = new ResponseParser(xml, "{APP_ID}", false); | |
| 232 } catch (RequestFailureException e) { | |
| 233 failed = true; | |
| 234 } | |
| 235 assertTrue(failureMessage, failed); | |
| 236 if (parser != null) { | |
| 237 assertEquals("elapsed_seconds doesn't match.", expectedDaystart, | |
| 238 parser.getDaystartSeconds()); | |
| 239 } | |
| 240 } | |
| 241 } | |
| OLD | NEW |