Roll src/third_party/WebKit 2146135:dc7c4b0 (svn 202183:202185)
[chromium-blink-merge.git] / components / update_client / update_response_unittest.cc
bloba53d2e90e90b9ffa7cdfc11304f643babef3c7f5
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.
5 #include "components/update_client/update_response.h"
6 #include "testing/gtest/include/gtest/gtest.h"
8 namespace update_client {
10 const char* kValidXml =
11 "<?xml version='1.0' encoding='UTF-8'?>"
12 "<response protocol='3.0'>"
13 " <app appid='12345'>"
14 " <updatecheck status='ok'>"
15 " <urls>"
16 " <url codebase='http://example.com/'/>"
17 " <url codebasediff='http://diff.example.com/'/>"
18 " </urls>"
19 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
20 " <packages>"
21 " <package name='extension_1_2_3_4.crx'/>"
22 " </packages>"
23 " </manifest>"
24 " </updatecheck>"
25 " </app>"
26 "</response>";
28 const char* valid_xml_with_hash =
29 "<?xml version='1.0' encoding='UTF-8'?>"
30 "<response protocol='3.0'>"
31 " <app appid='12345'>"
32 " <updatecheck status='ok'>"
33 " <urls>"
34 " <url codebase='http://example.com/'/>"
35 " </urls>"
36 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
37 " <packages>"
38 " <package name='extension_1_2_3_4.crx' hash_sha256='1234'/>"
39 " </packages>"
40 " </manifest>"
41 " </updatecheck>"
42 " </app>"
43 "</response>";
45 const char* valid_xml_with_invalid_sizes =
46 "<?xml version='1.0' encoding='UTF-8'?>"
47 "<response protocol='3.0'>"
48 " <app appid='12345'>"
49 " <updatecheck status='ok'>"
50 " <urls>"
51 " <url codebase='http://example.com/'/>"
52 " </urls>"
53 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
54 " <packages>"
55 " <package name='1' size='1234'/>"
56 " <package name='2' size='-1234'/>"
57 " <package name='3' />"
58 " <package name='4' size='-a'/>"
59 " <package name='5' size='-123467890123456789'/>"
60 " <package name='6' size='123467890123456789'/>"
61 " </packages>"
62 " </manifest>"
63 " </updatecheck>"
64 " </app>"
65 "</response>";
67 const char* kInvalidValidXmlMissingCodebase =
68 "<?xml version='1.0' encoding='UTF-8'?>"
69 "<response protocol='3.0'>"
70 " <app appid='12345'>"
71 " <updatecheck status='ok'>"
72 " <urls>"
73 " <url codebasediff='http://diff.example.com/'/>"
74 " </urls>"
75 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
76 " <packages>"
77 " <package namediff='extension_1_2_3_4.crx'/>"
78 " </packages>"
79 " </manifest>"
80 " </updatecheck>"
81 " </app>"
82 "</response>";
84 const char* kInvalidValidXmlMissingManifest =
85 "<?xml version='1.0' encoding='UTF-8'?>"
86 "<response protocol='3.0'>"
87 " <app appid='12345'>"
88 " <updatecheck status='ok'>"
89 " <urls>"
90 " <url codebase='http://example.com/'/>"
91 " </urls>"
92 " </updatecheck>"
93 " </app>"
94 "</response>";
96 const char* kMissingAppId =
97 "<?xml version='1.0'?>"
98 "<response protocol='3.0'>"
99 " <app>"
100 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
101 " version='1.2.3.4' />"
102 " </app>"
103 "</response>";
105 const char* kInvalidCodebase =
106 "<?xml version='1.0'?>"
107 "<response protocol='3.0'>"
108 " <app appid='12345' status='ok'>"
109 " <updatecheck codebase='example.com/extension_1.2.3.4.crx'"
110 " version='1.2.3.4' />"
111 " </app>"
112 "</response>";
114 const char* kMissingVersion =
115 "<?xml version='1.0'?>"
116 "<response protocol='3.0'>"
117 " <app appid='12345' status='ok'>"
118 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' />"
119 " </app>"
120 "</response>";
122 const char* kInvalidVersion =
123 "<?xml version='1.0'?>"
124 "<response protocol='3.0'>"
125 " <app appid='12345' status='ok'>"
126 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' "
127 " version='1.2.3.a'/>"
128 " </app>"
129 "</response>";
131 // The v3 version of the protocol is not using namespaces. However, the parser
132 // must be able to parse responses that include namespaces.
133 const char* kUsesNamespacePrefix =
134 "<?xml version='1.0' encoding='UTF-8'?>"
135 "<g:response xmlns:g='http://www.google.com/update2/response' "
136 "protocol='3.0'>"
137 " <g:app appid='12345'>"
138 " <g:updatecheck status='ok'>"
139 " <g:urls>"
140 " <g:url codebase='http://example.com/'/>"
141 " </g:urls>"
142 " <g:manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
143 " <g:packages>"
144 " <g:package name='extension_1_2_3_4.crx'/>"
145 " </g:packages>"
146 " </g:manifest>"
147 " </g:updatecheck>"
148 " </g:app>"
149 "</g:response>";
151 // Includes unrelated <app> tags from other xml namespaces - this should
152 // not cause problems.
153 const char* kSimilarTagnames =
154 "<?xml version='1.0' encoding='UTF-8'?>"
155 "<response xmlns:a='http://a' protocol='3.0'>"
156 " <a:app appid='12345'>"
157 " <updatecheck status='ok'>"
158 " <urls>"
159 " <url codebase='http://example.com/'/>"
160 " </urls>"
161 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
162 " <packages>"
163 " <package name='extension_1_2_3_4.crx'/>"
164 " </packages>"
165 " </manifest>"
166 " </updatecheck>"
167 " </a:app>"
168 " <b:app appid='xyz' xmlns:b='http://b'>"
169 " <updatecheck status='noupdate'/>"
170 " </b:app>"
171 "</response>";
173 // Includes a <daystart> tag.
174 const char* kWithDaystart =
175 "<?xml version='1.0' encoding='UTF-8'?>"
176 "<response protocol='3.0'>"
177 " <daystart elapsed_seconds='456' />"
178 " <app appid='12345'>"
179 " <updatecheck status='ok'>"
180 " <urls>"
181 " <url codebase='http://example.com/'/>"
182 " </urls>"
183 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
184 " <packages>"
185 " <package name='extension_1_2_3_4.crx'/>"
186 " </packages>"
187 " </manifest>"
188 " </updatecheck>"
189 " </app>"
190 "</response>";
192 // Indicates no updates available - this should not be a parse error.
193 const char* kNoUpdate =
194 "<?xml version='1.0' encoding='UTF-8'?>"
195 "<response protocol='3.0'>"
196 " <app appid='12345'>"
197 " <updatecheck status='noupdate' />"
198 " </app>"
199 "</response>";
201 // Includes two <app> tags, one with an error.
202 const char* kTwoAppsOneError =
203 "<?xml version='1.0' encoding='UTF-8'?>"
204 "<response protocol='3.0'>"
205 " <app appid='aaaaaaaa' status='error-unknownApplication'>"
206 " <updatecheck status='error-unknownapplication'/>"
207 " </app>"
208 " <app appid='bbbbbbbb'>"
209 " <updatecheck status='ok'>"
210 " <urls>"
211 " <url codebase='http://example.com/'/>"
212 " </urls>"
213 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
214 " <packages>"
215 " <package name='extension_1_2_3_4.crx'/>"
216 " </packages>"
217 " </manifest>"
218 " </updatecheck>"
219 " </app>"
220 "</response>";
222 TEST(ComponentUpdaterUpdateResponseTest, TestParser) {
223 UpdateResponse parser;
225 // Test parsing of a number of invalid xml cases
226 EXPECT_FALSE(parser.Parse(std::string()));
227 EXPECT_FALSE(parser.errors().empty());
229 EXPECT_TRUE(parser.Parse(kMissingAppId));
230 EXPECT_TRUE(parser.results().list.empty());
231 EXPECT_FALSE(parser.errors().empty());
233 EXPECT_TRUE(parser.Parse(kInvalidCodebase));
234 EXPECT_TRUE(parser.results().list.empty());
235 EXPECT_FALSE(parser.errors().empty());
237 EXPECT_TRUE(parser.Parse(kMissingVersion));
238 EXPECT_TRUE(parser.results().list.empty());
239 EXPECT_FALSE(parser.errors().empty());
241 EXPECT_TRUE(parser.Parse(kInvalidVersion));
242 EXPECT_TRUE(parser.results().list.empty());
243 EXPECT_FALSE(parser.errors().empty());
245 EXPECT_TRUE(parser.Parse(kInvalidValidXmlMissingCodebase));
246 EXPECT_TRUE(parser.results().list.empty());
247 EXPECT_FALSE(parser.errors().empty());
249 EXPECT_TRUE(parser.Parse(kInvalidValidXmlMissingManifest));
250 EXPECT_TRUE(parser.results().list.empty());
251 EXPECT_FALSE(parser.errors().empty());
253 // Parse some valid XML, and check that all params came out as expected
254 EXPECT_TRUE(parser.Parse(kValidXml));
255 EXPECT_TRUE(parser.errors().empty());
256 EXPECT_EQ(1u, parser.results().list.size());
257 const UpdateResponse::Result* firstResult = &parser.results().list[0];
258 EXPECT_EQ(1u, firstResult->crx_urls.size());
259 EXPECT_EQ(GURL("http://example.com/"), firstResult->crx_urls[0]);
260 EXPECT_EQ(GURL("http://diff.example.com/"), firstResult->crx_diffurls[0]);
261 EXPECT_EQ("1.2.3.4", firstResult->manifest.version);
262 EXPECT_EQ("2.0.143.0", firstResult->manifest.browser_min_version);
263 EXPECT_EQ(1u, firstResult->manifest.packages.size());
264 EXPECT_EQ("extension_1_2_3_4.crx", firstResult->manifest.packages[0].name);
266 // Parse some xml that uses namespace prefixes.
267 EXPECT_TRUE(parser.Parse(kUsesNamespacePrefix));
268 EXPECT_TRUE(parser.errors().empty());
269 EXPECT_TRUE(parser.Parse(kSimilarTagnames));
270 EXPECT_TRUE(parser.errors().empty());
272 // Parse xml with hash value
273 EXPECT_TRUE(parser.Parse(valid_xml_with_hash));
274 EXPECT_TRUE(parser.errors().empty());
275 EXPECT_FALSE(parser.results().list.empty());
276 firstResult = &parser.results().list[0];
277 EXPECT_FALSE(firstResult->manifest.packages.empty());
278 EXPECT_EQ("1234", firstResult->manifest.packages[0].hash_sha256);
280 // Parse xml with package size value
281 EXPECT_TRUE(parser.Parse(valid_xml_with_invalid_sizes));
282 EXPECT_TRUE(parser.errors().empty());
283 EXPECT_FALSE(parser.results().list.empty());
284 firstResult = &parser.results().list[0];
285 EXPECT_FALSE(firstResult->manifest.packages.empty());
286 EXPECT_EQ(1234, firstResult->manifest.packages[0].size);
287 EXPECT_EQ(-1234, firstResult->manifest.packages[1].size);
288 EXPECT_EQ(0, firstResult->manifest.packages[2].size);
289 EXPECT_EQ(0, firstResult->manifest.packages[3].size);
290 EXPECT_EQ(0, firstResult->manifest.packages[4].size);
291 EXPECT_EQ(0, firstResult->manifest.packages[5].size);
293 // Parse xml with a <daystart> element.
294 EXPECT_TRUE(parser.Parse(kWithDaystart));
295 EXPECT_TRUE(parser.errors().empty());
296 EXPECT_FALSE(parser.results().list.empty());
297 EXPECT_EQ(parser.results().daystart_elapsed_seconds, 456);
299 // Parse a no-update response.
300 EXPECT_TRUE(parser.Parse(kNoUpdate));
301 EXPECT_TRUE(parser.errors().empty());
302 EXPECT_FALSE(parser.results().list.empty());
303 firstResult = &parser.results().list[0];
304 EXPECT_EQ(firstResult->extension_id, "12345");
305 EXPECT_EQ(firstResult->manifest.version, "");
307 // Parse xml with one error and one success <app> tag.
308 EXPECT_TRUE(parser.Parse(kTwoAppsOneError));
309 EXPECT_FALSE(parser.errors().empty());
310 EXPECT_EQ(1u, parser.results().list.size());
311 firstResult = &parser.results().list[0];
312 EXPECT_EQ(firstResult->extension_id, "bbbbbbbb");
315 } // namespace update_client