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