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'>"
16 " <url codebase='http://example.com/'/>"
17 " <url codebasediff='http://diff.example.com/'/>"
19 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
21 " <package name='extension_1_2_3_4.crx'/>"
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'>"
34 " <url codebase='http://example.com/'/>"
36 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
38 " <package name='extension_1_2_3_4.crx' hash_sha256='1234'/>"
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'>"
51 " <url codebase='http://example.com/'/>"
53 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
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'/>"
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'>"
73 " <url codebasediff='http://diff.example.com/'/>"
75 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
77 " <package namediff='extension_1_2_3_4.crx'/>"
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'>"
90 " <url codebase='http://example.com/'/>"
96 const char* kMissingAppId
=
97 "<?xml version='1.0'?>"
98 "<response protocol='3.0'>"
100 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
101 " version='1.2.3.4' />"
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' />"
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' />"
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'/>"
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' "
137 " <g:app appid='12345'>"
138 " <g:updatecheck status='ok'>"
140 " <g:url codebase='http://example.com/'/>"
142 " <g:manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
144 " <g:package name='extension_1_2_3_4.crx'/>"
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'>"
159 " <url codebase='http://example.com/'/>"
161 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
163 " <package name='extension_1_2_3_4.crx'/>"
168 " <b:app appid='xyz' xmlns:b='http://b'>"
169 " <updatecheck status='noupdate'/>"
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'>"
181 " <url codebase='http://example.com/'/>"
183 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
185 " <package name='extension_1_2_3_4.crx'/>"
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' />"
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'/>"
208 " <app appid='bbbbbbbb'>"
209 " <updatecheck status='ok'>"
211 " <url codebase='http://example.com/'/>"
213 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
215 " <package name='extension_1_2_3_4.crx'/>"
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