OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #include "base/memory/scoped_vector.h" |
| 6 #include "chrome/browser/component_updater/update_manifest.h" |
| 7 #include "libxml/globals.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace component_updater { |
| 11 |
| 12 const char* kValidXml = |
| 13 "<?xml version='1.0' encoding='UTF-8'?>" |
| 14 "<response protocol='3.0'>" |
| 15 " <app appid='12345'>" |
| 16 " <updatecheck status='ok'>" |
| 17 " <urls>" |
| 18 " <url codebase='http://example.com/'/>" |
| 19 " <url codebasediff='http://diff.example.com/'/>" |
| 20 " </urls>" |
| 21 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>" |
| 22 " <packages>" |
| 23 " <package name='extension_1_2_3_4.crx'/>" |
| 24 " </packages>" |
| 25 " </manifest>" |
| 26 " </updatecheck>" |
| 27 " </app>" |
| 28 "</response>"; |
| 29 |
| 30 const char *valid_xml_with_hash = |
| 31 "<?xml version='1.0' encoding='UTF-8'?>" |
| 32 "<response protocol='3.0'>" |
| 33 " <app appid='12345'>" |
| 34 " <updatecheck status='ok'>" |
| 35 " <urls>" |
| 36 " <url codebase='http://example.com/'/>" |
| 37 " </urls>" |
| 38 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>" |
| 39 " <packages>" |
| 40 " <package name='extension_1_2_3_4.crx' hash_sha256='1234'/>" |
| 41 " </packages>" |
| 42 " </manifest>" |
| 43 " </updatecheck>" |
| 44 " </app>" |
| 45 "</response>"; |
| 46 |
| 47 const char* kMissingAppId = |
| 48 "<?xml version='1.0'?>" |
| 49 "<response protocol='3.0'>" |
| 50 " <app>" |
| 51 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'" |
| 52 " version='1.2.3.4' />" |
| 53 " </app>" |
| 54 "</response>"; |
| 55 |
| 56 const char* kInvalidCodebase = |
| 57 "<?xml version='1.0'?>" |
| 58 "<response protocol='3.0'>" |
| 59 " <app appid='12345' status='ok'>" |
| 60 " <updatecheck codebase='example.com/extension_1.2.3.4.crx'" |
| 61 " version='1.2.3.4' />" |
| 62 " </app>" |
| 63 "</response>"; |
| 64 |
| 65 const char* kMissingVersion = |
| 66 "<?xml version='1.0'?>" |
| 67 "<response protocol='3.0'>" |
| 68 " <app appid='12345' status='ok'>" |
| 69 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' />" |
| 70 " </app>" |
| 71 "</response>"; |
| 72 |
| 73 const char* kInvalidVersion = |
| 74 "<?xml version='1.0'?>" |
| 75 "<response protocol='3.0'>" |
| 76 " <app appid='12345' status='ok'>" |
| 77 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' " |
| 78 " version='1.2.3.a'/>" |
| 79 " </app>" |
| 80 "</response>"; |
| 81 |
| 82 // The v3 version of the protocol is not using namespaces. However, the parser |
| 83 // must be able to parse responses that include namespaces. |
| 84 const char* kUsesNamespacePrefix = |
| 85 "<?xml version='1.0' encoding='UTF-8'?>" |
| 86 "<g:response xmlns='http://www.google.com/update2/response' protocol='3.0'>" |
| 87 " <g:app appid='12345'>" |
| 88 " <g:updatecheck status='ok'>" |
| 89 " <g:urls>" |
| 90 " <g:url codebase='http://example.com/'/>" |
| 91 " </g:urls>" |
| 92 " <g:manifest version='1.2.3.4' prodversionmin='2.0.143.0'>" |
| 93 " <g:packages>" |
| 94 " <g:package name='extension_1_2_3_4.crx'/>" |
| 95 " </g:packages>" |
| 96 " </g:manifest>" |
| 97 " </g:updatecheck>" |
| 98 " </g:app>" |
| 99 "</g:response>"; |
| 100 |
| 101 // Includes unrelated <app> tags from other xml namespaces - this should |
| 102 // not cause problems. |
| 103 const char* kSimilarTagnames = |
| 104 "<?xml version='1.0' encoding='UTF-8'?>" |
| 105 "<response xmlns:a='http://a' protocol='3.0'>" |
| 106 " <a:app appid='12345'>" |
| 107 " <updatecheck status='ok'>" |
| 108 " <urls>" |
| 109 " <url codebase='http://example.com/'/>" |
| 110 " </urls>" |
| 111 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>" |
| 112 " <packages>" |
| 113 " <package name='extension_1_2_3_4.crx'/>" |
| 114 " </packages>" |
| 115 " </manifest>" |
| 116 " </updatecheck>" |
| 117 " </a:app>" |
| 118 " <b:app appid='xyz' xmlns:b='http://b'>" |
| 119 " <updatecheck status='noupdate'/>" |
| 120 " </b:app>" |
| 121 "</response>"; |
| 122 |
| 123 // Includes a <daystart> tag. |
| 124 const char* kWithDaystart = |
| 125 "<?xml version='1.0' encoding='UTF-8'?>" |
| 126 "<response protocol='3.0'>" |
| 127 " <daystart elapsed_seconds='456' />" |
| 128 " <app appid='12345'>" |
| 129 " <updatecheck status='ok'>" |
| 130 " <urls>" |
| 131 " <url codebase='http://example.com/'/>" |
| 132 " </urls>" |
| 133 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>" |
| 134 " <packages>" |
| 135 " <package name='extension_1_2_3_4.crx'/>" |
| 136 " </packages>" |
| 137 " </manifest>" |
| 138 " </updatecheck>" |
| 139 " </app>" |
| 140 "</response>"; |
| 141 |
| 142 // Indicates no updates available - this should not be a parse error. |
| 143 const char* kNoUpdate = |
| 144 "<?xml version='1.0' encoding='UTF-8'?>" |
| 145 "<response protocol='3.0'>" |
| 146 " <app appid='12345'>" |
| 147 " <updatecheck status='noupdate' />" |
| 148 " </app>" |
| 149 "</response>"; |
| 150 |
| 151 // Includes two <app> tags, one with an error. |
| 152 const char* kTwoAppsOneError = |
| 153 "<?xml version='1.0' encoding='UTF-8'?>" |
| 154 "<response protocol='3.0'>" |
| 155 " <app appid='aaaaaaaa' status='error-unknownApplication'>" |
| 156 " <updatecheck status='error-unknownapplication'/>" |
| 157 " </app>" |
| 158 " <app appid='bbbbbbbb'>" |
| 159 " <updatecheck status='ok'>" |
| 160 " <urls>" |
| 161 " <url codebase='http://example.com/'/>" |
| 162 " </urls>" |
| 163 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>" |
| 164 " <packages>" |
| 165 " <package name='extension_1_2_3_4.crx'/>" |
| 166 " </packages>" |
| 167 " </manifest>" |
| 168 " </updatecheck>" |
| 169 " </app>" |
| 170 "</response>"; |
| 171 |
| 172 TEST(ComponentUpdaterManifestTest, TestUpdateManifest) { |
| 173 UpdateManifest parser; |
| 174 |
| 175 // Test parsing of a number of invalid xml cases |
| 176 EXPECT_FALSE(parser.Parse(std::string())); |
| 177 EXPECT_FALSE(parser.errors().empty()); |
| 178 |
| 179 EXPECT_TRUE(parser.Parse(kMissingAppId)); |
| 180 EXPECT_TRUE(parser.results().list.empty()); |
| 181 EXPECT_FALSE(parser.errors().empty()); |
| 182 |
| 183 EXPECT_TRUE(parser.Parse(kInvalidCodebase)); |
| 184 EXPECT_TRUE(parser.results().list.empty()); |
| 185 EXPECT_FALSE(parser.errors().empty()); |
| 186 |
| 187 EXPECT_TRUE(parser.Parse(kMissingVersion)); |
| 188 EXPECT_TRUE(parser.results().list.empty()); |
| 189 EXPECT_FALSE(parser.errors().empty()); |
| 190 |
| 191 EXPECT_TRUE(parser.Parse(kInvalidVersion)); |
| 192 EXPECT_TRUE(parser.results().list.empty()); |
| 193 EXPECT_FALSE(parser.errors().empty()); |
| 194 |
| 195 // Parse some valid XML, and check that all params came out as expected |
| 196 EXPECT_TRUE(parser.Parse(kValidXml)); |
| 197 EXPECT_TRUE(parser.errors().empty()); |
| 198 EXPECT_EQ(1u, parser.results().list.size()); |
| 199 const UpdateManifest::Result* firstResult = &parser.results().list[0]; |
| 200 EXPECT_EQ(1u, firstResult->crx_urls.size()); |
| 201 EXPECT_EQ(GURL("http://example.com/"), firstResult->crx_urls[0]); |
| 202 EXPECT_EQ(GURL("http://diff.example.com/"), firstResult->crx_diffurls[0]); |
| 203 EXPECT_EQ("1.2.3.4", firstResult->manifest.version); |
| 204 EXPECT_EQ("2.0.143.0", firstResult->manifest.browser_min_version); |
| 205 EXPECT_EQ(1u, firstResult->manifest.packages.size()); |
| 206 EXPECT_EQ("extension_1_2_3_4.crx", firstResult->manifest.packages[0].name); |
| 207 |
| 208 // Parse some xml that uses namespace prefixes. |
| 209 EXPECT_TRUE(parser.Parse(kUsesNamespacePrefix)); |
| 210 EXPECT_TRUE(parser.errors().empty()); |
| 211 EXPECT_TRUE(parser.Parse(kSimilarTagnames)); |
| 212 EXPECT_TRUE(parser.errors().empty()); |
| 213 xmlCleanupGlobals(); |
| 214 |
| 215 // Parse xml with hash value |
| 216 EXPECT_TRUE(parser.Parse(valid_xml_with_hash)); |
| 217 EXPECT_TRUE(parser.errors().empty()); |
| 218 EXPECT_FALSE(parser.results().list.empty()); |
| 219 firstResult = &parser.results().list[0]; |
| 220 EXPECT_FALSE(firstResult->manifest.packages.empty()); |
| 221 EXPECT_EQ("1234", firstResult->manifest.packages[0].hash_sha256); |
| 222 |
| 223 // Parse xml with a <daystart> element. |
| 224 EXPECT_TRUE(parser.Parse(kWithDaystart)); |
| 225 EXPECT_TRUE(parser.errors().empty()); |
| 226 EXPECT_FALSE(parser.results().list.empty()); |
| 227 EXPECT_EQ(parser.results().daystart_elapsed_seconds, 456); |
| 228 |
| 229 // Parse a no-update response. |
| 230 EXPECT_TRUE(parser.Parse(kNoUpdate)); |
| 231 EXPECT_TRUE(parser.errors().empty()); |
| 232 EXPECT_FALSE(parser.results().list.empty()); |
| 233 firstResult = &parser.results().list[0]; |
| 234 EXPECT_EQ(firstResult->extension_id, "12345"); |
| 235 EXPECT_EQ(firstResult->manifest.version, ""); |
| 236 |
| 237 // Parse xml with one error and one success <app> tag. |
| 238 EXPECT_TRUE(parser.Parse(kTwoAppsOneError)); |
| 239 EXPECT_FALSE(parser.errors().empty()); |
| 240 EXPECT_EQ(1u, parser.results().list.size()); |
| 241 firstResult = &parser.results().list[0]; |
| 242 EXPECT_EQ(firstResult->extension_id, "bbbbbbbb"); |
| 243 } |
| 244 |
| 245 } // namespace component_updater |
| 246 |
OLD | NEW |