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 "libxml/globals.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace update_client
{
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'>"
18 " <url codebase='http://example.com/'/>"
19 " <url codebasediff='http://diff.example.com/'/>"
21 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
23 " <package name='extension_1_2_3_4.crx'/>"
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'>"
36 " <url codebase='http://example.com/'/>"
38 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
40 " <package name='extension_1_2_3_4.crx' hash_sha256='1234'/>"
47 const char* valid_xml_with_invalid_sizes
=
48 "<?xml version='1.0' encoding='UTF-8'?>"
49 "<response protocol='3.0'>"
50 " <app appid='12345'>"
51 " <updatecheck status='ok'>"
53 " <url codebase='http://example.com/'/>"
55 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
57 " <package name='1' size='1234'/>"
58 " <package name='2' size='-1234'/>"
59 " <package name='3' />"
60 " <package name='4' size='-a'/>"
61 " <package name='5' size='-123467890123456789'/>"
62 " <package name='6' size='123467890123456789'/>"
69 const char* kInvalidValidXmlMissingCodebase
=
70 "<?xml version='1.0' encoding='UTF-8'?>"
71 "<response protocol='3.0'>"
72 " <app appid='12345'>"
73 " <updatecheck status='ok'>"
75 " <url codebasediff='http://diff.example.com/'/>"
77 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
79 " <package namediff='extension_1_2_3_4.crx'/>"
86 const char* kMissingAppId
=
87 "<?xml version='1.0'?>"
88 "<response protocol='3.0'>"
90 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
91 " version='1.2.3.4' />"
95 const char* kInvalidCodebase
=
96 "<?xml version='1.0'?>"
97 "<response protocol='3.0'>"
98 " <app appid='12345' status='ok'>"
99 " <updatecheck codebase='example.com/extension_1.2.3.4.crx'"
100 " version='1.2.3.4' />"
104 const char* kMissingVersion
=
105 "<?xml version='1.0'?>"
106 "<response protocol='3.0'>"
107 " <app appid='12345' status='ok'>"
108 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' />"
112 const char* kInvalidVersion
=
113 "<?xml version='1.0'?>"
114 "<response protocol='3.0'>"
115 " <app appid='12345' status='ok'>"
116 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' "
117 " version='1.2.3.a'/>"
121 // The v3 version of the protocol is not using namespaces. However, the parser
122 // must be able to parse responses that include namespaces.
123 const char* kUsesNamespacePrefix
=
124 "<?xml version='1.0' encoding='UTF-8'?>"
125 "<g:response xmlns:g='http://www.google.com/update2/response' "
127 " <g:app appid='12345'>"
128 " <g:updatecheck status='ok'>"
130 " <g:url codebase='http://example.com/'/>"
132 " <g:manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
134 " <g:package name='extension_1_2_3_4.crx'/>"
141 // Includes unrelated <app> tags from other xml namespaces - this should
142 // not cause problems.
143 const char* kSimilarTagnames
=
144 "<?xml version='1.0' encoding='UTF-8'?>"
145 "<response xmlns:a='http://a' protocol='3.0'>"
146 " <a:app appid='12345'>"
147 " <updatecheck status='ok'>"
149 " <url codebase='http://example.com/'/>"
151 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
153 " <package name='extension_1_2_3_4.crx'/>"
158 " <b:app appid='xyz' xmlns:b='http://b'>"
159 " <updatecheck status='noupdate'/>"
163 // Includes a <daystart> tag.
164 const char* kWithDaystart
=
165 "<?xml version='1.0' encoding='UTF-8'?>"
166 "<response protocol='3.0'>"
167 " <daystart elapsed_seconds='456' />"
168 " <app appid='12345'>"
169 " <updatecheck status='ok'>"
171 " <url codebase='http://example.com/'/>"
173 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
175 " <package name='extension_1_2_3_4.crx'/>"
182 // Indicates no updates available - this should not be a parse error.
183 const char* kNoUpdate
=
184 "<?xml version='1.0' encoding='UTF-8'?>"
185 "<response protocol='3.0'>"
186 " <app appid='12345'>"
187 " <updatecheck status='noupdate' />"
191 // Includes two <app> tags, one with an error.
192 const char* kTwoAppsOneError
=
193 "<?xml version='1.0' encoding='UTF-8'?>"
194 "<response protocol='3.0'>"
195 " <app appid='aaaaaaaa' status='error-unknownApplication'>"
196 " <updatecheck status='error-unknownapplication'/>"
198 " <app appid='bbbbbbbb'>"
199 " <updatecheck status='ok'>"
201 " <url codebase='http://example.com/'/>"
203 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
205 " <package name='extension_1_2_3_4.crx'/>"
212 TEST(ComponentUpdaterUpdateResponseTest
, TestParser
) {
213 UpdateResponse parser
;
215 // Test parsing of a number of invalid xml cases
216 EXPECT_FALSE(parser
.Parse(std::string()));
217 EXPECT_FALSE(parser
.errors().empty());
219 EXPECT_TRUE(parser
.Parse(kMissingAppId
));
220 EXPECT_TRUE(parser
.results().list
.empty());
221 EXPECT_FALSE(parser
.errors().empty());
223 EXPECT_TRUE(parser
.Parse(kInvalidCodebase
));
224 EXPECT_TRUE(parser
.results().list
.empty());
225 EXPECT_FALSE(parser
.errors().empty());
227 EXPECT_TRUE(parser
.Parse(kMissingVersion
));
228 EXPECT_TRUE(parser
.results().list
.empty());
229 EXPECT_FALSE(parser
.errors().empty());
231 EXPECT_TRUE(parser
.Parse(kInvalidVersion
));
232 EXPECT_TRUE(parser
.results().list
.empty());
233 EXPECT_FALSE(parser
.errors().empty());
235 EXPECT_TRUE(parser
.Parse(kInvalidValidXmlMissingCodebase
));
236 EXPECT_TRUE(parser
.results().list
.empty());
237 EXPECT_FALSE(parser
.errors().empty());
239 // Parse some valid XML, and check that all params came out as expected
240 EXPECT_TRUE(parser
.Parse(kValidXml
));
241 EXPECT_TRUE(parser
.errors().empty());
242 EXPECT_EQ(1u, parser
.results().list
.size());
243 const UpdateResponse::Result
* firstResult
= &parser
.results().list
[0];
244 EXPECT_EQ(1u, firstResult
->crx_urls
.size());
245 EXPECT_EQ(GURL("http://example.com/"), firstResult
->crx_urls
[0]);
246 EXPECT_EQ(GURL("http://diff.example.com/"), firstResult
->crx_diffurls
[0]);
247 EXPECT_EQ("1.2.3.4", firstResult
->manifest
.version
);
248 EXPECT_EQ("2.0.143.0", firstResult
->manifest
.browser_min_version
);
249 EXPECT_EQ(1u, firstResult
->manifest
.packages
.size());
250 EXPECT_EQ("extension_1_2_3_4.crx", firstResult
->manifest
.packages
[0].name
);
252 // Parse some xml that uses namespace prefixes.
253 EXPECT_TRUE(parser
.Parse(kUsesNamespacePrefix
));
254 EXPECT_TRUE(parser
.errors().empty());
255 EXPECT_TRUE(parser
.Parse(kSimilarTagnames
));
256 EXPECT_TRUE(parser
.errors().empty());
259 // Parse xml with hash value
260 EXPECT_TRUE(parser
.Parse(valid_xml_with_hash
));
261 EXPECT_TRUE(parser
.errors().empty());
262 EXPECT_FALSE(parser
.results().list
.empty());
263 firstResult
= &parser
.results().list
[0];
264 EXPECT_FALSE(firstResult
->manifest
.packages
.empty());
265 EXPECT_EQ("1234", firstResult
->manifest
.packages
[0].hash_sha256
);
267 // Parse xml with package size value
268 EXPECT_TRUE(parser
.Parse(valid_xml_with_invalid_sizes
));
269 EXPECT_TRUE(parser
.errors().empty());
270 EXPECT_FALSE(parser
.results().list
.empty());
271 firstResult
= &parser
.results().list
[0];
272 EXPECT_FALSE(firstResult
->manifest
.packages
.empty());
273 EXPECT_EQ(1234, firstResult
->manifest
.packages
[0].size
);
274 EXPECT_EQ(-1234, firstResult
->manifest
.packages
[1].size
);
275 EXPECT_EQ(0, firstResult
->manifest
.packages
[2].size
);
276 EXPECT_EQ(0, firstResult
->manifest
.packages
[3].size
);
277 EXPECT_EQ(0, firstResult
->manifest
.packages
[4].size
);
278 EXPECT_EQ(0, firstResult
->manifest
.packages
[5].size
);
280 // Parse xml with a <daystart> element.
281 EXPECT_TRUE(parser
.Parse(kWithDaystart
));
282 EXPECT_TRUE(parser
.errors().empty());
283 EXPECT_FALSE(parser
.results().list
.empty());
284 EXPECT_EQ(parser
.results().daystart_elapsed_seconds
, 456);
286 // Parse a no-update response.
287 EXPECT_TRUE(parser
.Parse(kNoUpdate
));
288 EXPECT_TRUE(parser
.errors().empty());
289 EXPECT_FALSE(parser
.results().list
.empty());
290 firstResult
= &parser
.results().list
[0];
291 EXPECT_EQ(firstResult
->extension_id
, "12345");
292 EXPECT_EQ(firstResult
->manifest
.version
, "");
294 // Parse xml with one error and one success <app> tag.
295 EXPECT_TRUE(parser
.Parse(kTwoAppsOneError
));
296 EXPECT_FALSE(parser
.errors().empty());
297 EXPECT_EQ(1u, parser
.results().list
.size());
298 firstResult
= &parser
.results().list
[0];
299 EXPECT_EQ(firstResult
->extension_id
, "bbbbbbbb");
302 } // namespace update_client