Roll src/third_party/skia de7665a:76033be
[chromium-blink-merge.git] / components / update_client / test / update_response_unittest.cc
blobe1069168d9165552a2bbd3e5af7ea4a7a265ebbb
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'>"
17 " <urls>"
18 " <url codebase='http://example.com/'/>"
19 " <url codebasediff='http://diff.example.com/'/>"
20 " </urls>"
21 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
22 " <packages>"
23 " <package name='extension_1_2_3_4.crx'/>"
24 " </packages>"
25 " </manifest>"
26 " </updatecheck>"
27 " </app>"
28 "</response>";
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'>"
35 " <urls>"
36 " <url codebase='http://example.com/'/>"
37 " </urls>"
38 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
39 " <packages>"
40 " <package name='extension_1_2_3_4.crx' hash_sha256='1234'/>"
41 " </packages>"
42 " </manifest>"
43 " </updatecheck>"
44 " </app>"
45 "</response>";
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'>"
52 " <urls>"
53 " <url codebase='http://example.com/'/>"
54 " </urls>"
55 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
56 " <packages>"
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'/>"
63 " </packages>"
64 " </manifest>"
65 " </updatecheck>"
66 " </app>"
67 "</response>";
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'>"
74 " <urls>"
75 " <url codebasediff='http://diff.example.com/'/>"
76 " </urls>"
77 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
78 " <packages>"
79 " <package namediff='extension_1_2_3_4.crx'/>"
80 " </packages>"
81 " </manifest>"
82 " </updatecheck>"
83 " </app>"
84 "</response>";
86 const char* kMissingAppId =
87 "<?xml version='1.0'?>"
88 "<response protocol='3.0'>"
89 " <app>"
90 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
91 " version='1.2.3.4' />"
92 " </app>"
93 "</response>";
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' />"
101 " </app>"
102 "</response>";
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' />"
109 " </app>"
110 "</response>";
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'/>"
118 " </app>"
119 "</response>";
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' "
126 "protocol='3.0'>"
127 " <g:app appid='12345'>"
128 " <g:updatecheck status='ok'>"
129 " <g:urls>"
130 " <g:url codebase='http://example.com/'/>"
131 " </g:urls>"
132 " <g:manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
133 " <g:packages>"
134 " <g:package name='extension_1_2_3_4.crx'/>"
135 " </g:packages>"
136 " </g:manifest>"
137 " </g:updatecheck>"
138 " </g:app>"
139 "</g:response>";
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'>"
148 " <urls>"
149 " <url codebase='http://example.com/'/>"
150 " </urls>"
151 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
152 " <packages>"
153 " <package name='extension_1_2_3_4.crx'/>"
154 " </packages>"
155 " </manifest>"
156 " </updatecheck>"
157 " </a:app>"
158 " <b:app appid='xyz' xmlns:b='http://b'>"
159 " <updatecheck status='noupdate'/>"
160 " </b:app>"
161 "</response>";
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'>"
170 " <urls>"
171 " <url codebase='http://example.com/'/>"
172 " </urls>"
173 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
174 " <packages>"
175 " <package name='extension_1_2_3_4.crx'/>"
176 " </packages>"
177 " </manifest>"
178 " </updatecheck>"
179 " </app>"
180 "</response>";
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' />"
188 " </app>"
189 "</response>";
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'/>"
197 " </app>"
198 " <app appid='bbbbbbbb'>"
199 " <updatecheck status='ok'>"
200 " <urls>"
201 " <url codebase='http://example.com/'/>"
202 " </urls>"
203 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
204 " <packages>"
205 " <package name='extension_1_2_3_4.crx'/>"
206 " </packages>"
207 " </manifest>"
208 " </updatecheck>"
209 " </app>"
210 "</response>";
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());
257 xmlCleanupGlobals();
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