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'>"
17 " <url codebase='http://example.com/'/>"
18 " <url codebasediff='http://diff.example.com/'/>"
20 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
22 " <package name='extension_1_2_3_4.crx'/>"
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'>"
35 " <url codebase='http://example.com/'/>"
37 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
39 " <package name='extension_1_2_3_4.crx' hash_sha256='1234'/>"
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'>"
52 " <url codebase='http://example.com/'/>"
54 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
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'/>"
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'>"
74 " <url codebasediff='http://diff.example.com/'/>"
76 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
78 " <package namediff='extension_1_2_3_4.crx'/>"
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'>"
91 " <url codebase='http://example.com/'/>"
97 const char* kMissingAppId
=
98 "<?xml version='1.0'?>"
99 "<response protocol='3.0'>"
101 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
102 " version='1.2.3.4' />"
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' />"
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' />"
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'/>"
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' "
138 " <g:app appid='12345'>"
139 " <g:updatecheck status='ok'>"
141 " <g:url codebase='http://example.com/'/>"
143 " <g:manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
145 " <g:package name='extension_1_2_3_4.crx'/>"
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'>"
160 " <url codebase='http://example.com/'/>"
162 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
164 " <package name='extension_1_2_3_4.crx'/>"
169 " <b:app appid='xyz' xmlns:b='http://b'>"
170 " <updatecheck status='noupdate'/>"
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'>"
182 " <url codebase='http://example.com/'/>"
184 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
186 " <package name='extension_1_2_3_4.crx'/>"
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' />"
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'/>"
209 " <app appid='bbbbbbbb'>"
210 " <updatecheck status='ok'>"
212 " <url codebase='http://example.com/'/>"
214 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
216 " <package name='extension_1_2_3_4.crx'/>"
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