Reland "Non-SFI mode: Switch to newlib. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / components / update_client / update_response_unittest.cc
blob2e12ce3ef5af6ed7521407e71320dede46715103
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* kInvalidValidXmlMissingManifest =
87 "<?xml version='1.0' encoding='UTF-8'?>"
88 "<response protocol='3.0'>"
89 " <app appid='12345'>"
90 " <updatecheck status='ok'>"
91 " <urls>"
92 " <url codebase='http://example.com/'/>"
93 " </urls>"
94 " </updatecheck>"
95 " </app>"
96 "</response>";
98 const char* kMissingAppId =
99 "<?xml version='1.0'?>"
100 "<response protocol='3.0'>"
101 " <app>"
102 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
103 " version='1.2.3.4' />"
104 " </app>"
105 "</response>";
107 const char* kInvalidCodebase =
108 "<?xml version='1.0'?>"
109 "<response protocol='3.0'>"
110 " <app appid='12345' status='ok'>"
111 " <updatecheck codebase='example.com/extension_1.2.3.4.crx'"
112 " version='1.2.3.4' />"
113 " </app>"
114 "</response>";
116 const char* kMissingVersion =
117 "<?xml version='1.0'?>"
118 "<response protocol='3.0'>"
119 " <app appid='12345' status='ok'>"
120 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' />"
121 " </app>"
122 "</response>";
124 const char* kInvalidVersion =
125 "<?xml version='1.0'?>"
126 "<response protocol='3.0'>"
127 " <app appid='12345' status='ok'>"
128 " <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' "
129 " version='1.2.3.a'/>"
130 " </app>"
131 "</response>";
133 // The v3 version of the protocol is not using namespaces. However, the parser
134 // must be able to parse responses that include namespaces.
135 const char* kUsesNamespacePrefix =
136 "<?xml version='1.0' encoding='UTF-8'?>"
137 "<g:response xmlns:g='http://www.google.com/update2/response' "
138 "protocol='3.0'>"
139 " <g:app appid='12345'>"
140 " <g:updatecheck status='ok'>"
141 " <g:urls>"
142 " <g:url codebase='http://example.com/'/>"
143 " </g:urls>"
144 " <g:manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
145 " <g:packages>"
146 " <g:package name='extension_1_2_3_4.crx'/>"
147 " </g:packages>"
148 " </g:manifest>"
149 " </g:updatecheck>"
150 " </g:app>"
151 "</g:response>";
153 // Includes unrelated <app> tags from other xml namespaces - this should
154 // not cause problems.
155 const char* kSimilarTagnames =
156 "<?xml version='1.0' encoding='UTF-8'?>"
157 "<response xmlns:a='http://a' protocol='3.0'>"
158 " <a:app appid='12345'>"
159 " <updatecheck status='ok'>"
160 " <urls>"
161 " <url codebase='http://example.com/'/>"
162 " </urls>"
163 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
164 " <packages>"
165 " <package name='extension_1_2_3_4.crx'/>"
166 " </packages>"
167 " </manifest>"
168 " </updatecheck>"
169 " </a:app>"
170 " <b:app appid='xyz' xmlns:b='http://b'>"
171 " <updatecheck status='noupdate'/>"
172 " </b:app>"
173 "</response>";
175 // Includes a <daystart> tag.
176 const char* kWithDaystart =
177 "<?xml version='1.0' encoding='UTF-8'?>"
178 "<response protocol='3.0'>"
179 " <daystart elapsed_seconds='456' />"
180 " <app appid='12345'>"
181 " <updatecheck status='ok'>"
182 " <urls>"
183 " <url codebase='http://example.com/'/>"
184 " </urls>"
185 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
186 " <packages>"
187 " <package name='extension_1_2_3_4.crx'/>"
188 " </packages>"
189 " </manifest>"
190 " </updatecheck>"
191 " </app>"
192 "</response>";
194 // Indicates no updates available - this should not be a parse error.
195 const char* kNoUpdate =
196 "<?xml version='1.0' encoding='UTF-8'?>"
197 "<response protocol='3.0'>"
198 " <app appid='12345'>"
199 " <updatecheck status='noupdate' />"
200 " </app>"
201 "</response>";
203 // Includes two <app> tags, one with an error.
204 const char* kTwoAppsOneError =
205 "<?xml version='1.0' encoding='UTF-8'?>"
206 "<response protocol='3.0'>"
207 " <app appid='aaaaaaaa' status='error-unknownApplication'>"
208 " <updatecheck status='error-unknownapplication'/>"
209 " </app>"
210 " <app appid='bbbbbbbb'>"
211 " <updatecheck status='ok'>"
212 " <urls>"
213 " <url codebase='http://example.com/'/>"
214 " </urls>"
215 " <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
216 " <packages>"
217 " <package name='extension_1_2_3_4.crx'/>"
218 " </packages>"
219 " </manifest>"
220 " </updatecheck>"
221 " </app>"
222 "</response>";
224 TEST(ComponentUpdaterUpdateResponseTest, TestParser) {
225 UpdateResponse parser;
227 // Test parsing of a number of invalid xml cases
228 EXPECT_FALSE(parser.Parse(std::string()));
229 EXPECT_FALSE(parser.errors().empty());
231 EXPECT_TRUE(parser.Parse(kMissingAppId));
232 EXPECT_TRUE(parser.results().list.empty());
233 EXPECT_FALSE(parser.errors().empty());
235 EXPECT_TRUE(parser.Parse(kInvalidCodebase));
236 EXPECT_TRUE(parser.results().list.empty());
237 EXPECT_FALSE(parser.errors().empty());
239 EXPECT_TRUE(parser.Parse(kMissingVersion));
240 EXPECT_TRUE(parser.results().list.empty());
241 EXPECT_FALSE(parser.errors().empty());
243 EXPECT_TRUE(parser.Parse(kInvalidVersion));
244 EXPECT_TRUE(parser.results().list.empty());
245 EXPECT_FALSE(parser.errors().empty());
247 EXPECT_TRUE(parser.Parse(kInvalidValidXmlMissingCodebase));
248 EXPECT_TRUE(parser.results().list.empty());
249 EXPECT_FALSE(parser.errors().empty());
251 EXPECT_TRUE(parser.Parse(kInvalidValidXmlMissingManifest));
252 EXPECT_TRUE(parser.results().list.empty());
253 EXPECT_FALSE(parser.errors().empty());
255 // Parse some valid XML, and check that all params came out as expected
256 EXPECT_TRUE(parser.Parse(kValidXml));
257 EXPECT_TRUE(parser.errors().empty());
258 EXPECT_EQ(1u, parser.results().list.size());
259 const UpdateResponse::Result* firstResult = &parser.results().list[0];
260 EXPECT_EQ(1u, firstResult->crx_urls.size());
261 EXPECT_EQ(GURL("http://example.com/"), firstResult->crx_urls[0]);
262 EXPECT_EQ(GURL("http://diff.example.com/"), firstResult->crx_diffurls[0]);
263 EXPECT_EQ("1.2.3.4", firstResult->manifest.version);
264 EXPECT_EQ("2.0.143.0", firstResult->manifest.browser_min_version);
265 EXPECT_EQ(1u, firstResult->manifest.packages.size());
266 EXPECT_EQ("extension_1_2_3_4.crx", firstResult->manifest.packages[0].name);
268 // Parse some xml that uses namespace prefixes.
269 EXPECT_TRUE(parser.Parse(kUsesNamespacePrefix));
270 EXPECT_TRUE(parser.errors().empty());
271 EXPECT_TRUE(parser.Parse(kSimilarTagnames));
272 EXPECT_TRUE(parser.errors().empty());
273 xmlCleanupGlobals();
275 // Parse xml with hash value
276 EXPECT_TRUE(parser.Parse(valid_xml_with_hash));
277 EXPECT_TRUE(parser.errors().empty());
278 EXPECT_FALSE(parser.results().list.empty());
279 firstResult = &parser.results().list[0];
280 EXPECT_FALSE(firstResult->manifest.packages.empty());
281 EXPECT_EQ("1234", firstResult->manifest.packages[0].hash_sha256);
283 // Parse xml with package size value
284 EXPECT_TRUE(parser.Parse(valid_xml_with_invalid_sizes));
285 EXPECT_TRUE(parser.errors().empty());
286 EXPECT_FALSE(parser.results().list.empty());
287 firstResult = &parser.results().list[0];
288 EXPECT_FALSE(firstResult->manifest.packages.empty());
289 EXPECT_EQ(1234, firstResult->manifest.packages[0].size);
290 EXPECT_EQ(-1234, firstResult->manifest.packages[1].size);
291 EXPECT_EQ(0, firstResult->manifest.packages[2].size);
292 EXPECT_EQ(0, firstResult->manifest.packages[3].size);
293 EXPECT_EQ(0, firstResult->manifest.packages[4].size);
294 EXPECT_EQ(0, firstResult->manifest.packages[5].size);
296 // Parse xml with a <daystart> element.
297 EXPECT_TRUE(parser.Parse(kWithDaystart));
298 EXPECT_TRUE(parser.errors().empty());
299 EXPECT_FALSE(parser.results().list.empty());
300 EXPECT_EQ(parser.results().daystart_elapsed_seconds, 456);
302 // Parse a no-update response.
303 EXPECT_TRUE(parser.Parse(kNoUpdate));
304 EXPECT_TRUE(parser.errors().empty());
305 EXPECT_FALSE(parser.results().list.empty());
306 firstResult = &parser.results().list[0];
307 EXPECT_EQ(firstResult->extension_id, "12345");
308 EXPECT_EQ(firstResult->manifest.version, "");
310 // Parse xml with one error and one success <app> tag.
311 EXPECT_TRUE(parser.Parse(kTwoAppsOneError));
312 EXPECT_FALSE(parser.errors().empty());
313 EXPECT_EQ(1u, parser.results().list.size());
314 firstResult = &parser.results().list[0];
315 EXPECT_EQ(firstResult->extension_id, "bbbbbbbb");
318 } // namespace update_client