summaryrefslogtreecommitdiffstats
path: root/chrome/browser/component_updater/test/update_response_unittest.cc
blob: 248c028b23196be1bea78589bc3b70f31630e196 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/scoped_vector.h"
#include "chrome/browser/component_updater/update_response.h"
#include "libxml/globals.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace component_updater {

const char* kValidXml =
"<?xml version='1.0' encoding='UTF-8'?>"
"<response protocol='3.0'>"
" <app appid='12345'>"
"   <updatecheck status='ok'>"
"     <urls>"
"       <url codebase='http://example.com/'/>"
"       <url codebasediff='http://diff.example.com/'/>"
"     </urls>"
"     <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
"       <packages>"
"         <package name='extension_1_2_3_4.crx'/>"
"       </packages>"
"     </manifest>"
"   </updatecheck>"
" </app>"
"</response>";

const char* valid_xml_with_hash =
"<?xml version='1.0' encoding='UTF-8'?>"
"<response protocol='3.0'>"
" <app appid='12345'>"
"   <updatecheck status='ok'>"
"     <urls>"
"       <url codebase='http://example.com/'/>"
"     </urls>"
"     <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
"       <packages>"
"         <package name='extension_1_2_3_4.crx' hash_sha256='1234'/>"
"       </packages>"
"     </manifest>"
"   </updatecheck>"
" </app>"
"</response>";

const char* valid_xml_with_invalid_sizes =
"<?xml version='1.0' encoding='UTF-8'?>"
"<response protocol='3.0'>"
" <app appid='12345'>"
"   <updatecheck status='ok'>"
"     <urls>"
"       <url codebase='http://example.com/'/>"
"     </urls>"
"     <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
"       <packages>"
"         <package name='1' size='1234'/>"
"         <package name='2' size='-1234'/>"
"         <package name='3' />"
"         <package name='4' size='-a'/>"
"         <package name='5' size='-123467890123456789'/>"
"         <package name='6' size='123467890123456789'/>"
"       </packages>"
"     </manifest>"
"   </updatecheck>"
" </app>"
"</response>";

const char* kMissingAppId =
"<?xml version='1.0'?>"
"<response protocol='3.0'>"
" <app>"
"  <updatecheck codebase='http://example.com/extension_1.2.3.4.crx'"
"               version='1.2.3.4' />"
" </app>"
"</response>";

const char* kInvalidCodebase =
"<?xml version='1.0'?>"
"<response protocol='3.0'>"
" <app appid='12345' status='ok'>"
"  <updatecheck codebase='example.com/extension_1.2.3.4.crx'"
"               version='1.2.3.4' />"
" </app>"
"</response>";

const char* kMissingVersion =
"<?xml version='1.0'?>"
"<response protocol='3.0'>"
" <app appid='12345' status='ok'>"
"  <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' />"
" </app>"
"</response>";

const char* kInvalidVersion =
"<?xml version='1.0'?>"
"<response protocol='3.0'>"
" <app appid='12345' status='ok'>"
"  <updatecheck codebase='http://example.com/extension_1.2.3.4.crx' "
"               version='1.2.3.a'/>"
" </app>"
"</response>";

// The v3 version of the protocol is not using namespaces. However, the parser
// must be able to parse responses that include namespaces.
const char* kUsesNamespacePrefix =
"<?xml version='1.0' encoding='UTF-8'?>"
"<g:response xmlns='http://www.google.com/update2/response' protocol='3.0'>"
" <g:app appid='12345'>"
"   <g:updatecheck status='ok'>"
"     <g:urls>"
"       <g:url codebase='http://example.com/'/>"
"     </g:urls>"
"     <g:manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
"       <g:packages>"
"         <g:package name='extension_1_2_3_4.crx'/>"
"       </g:packages>"
"     </g:manifest>"
"   </g:updatecheck>"
" </g:app>"
"</g:response>";

// Includes unrelated <app> tags from other xml namespaces - this should
// not cause problems.
const char* kSimilarTagnames =
"<?xml version='1.0' encoding='UTF-8'?>"
"<response xmlns:a='http://a' protocol='3.0'>"
" <a:app appid='12345'>"
"   <updatecheck status='ok'>"
"     <urls>"
"       <url codebase='http://example.com/'/>"
"     </urls>"
"     <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
"       <packages>"
"         <package name='extension_1_2_3_4.crx'/>"
"       </packages>"
"     </manifest>"
"   </updatecheck>"
" </a:app>"
" <b:app appid='xyz' xmlns:b='http://b'>"
"   <updatecheck status='noupdate'/>"
" </b:app>"
"</response>";

// Includes a <daystart> tag.
const char* kWithDaystart =
"<?xml version='1.0' encoding='UTF-8'?>"
"<response protocol='3.0'>"
" <daystart elapsed_seconds='456' />"
" <app appid='12345'>"
"   <updatecheck status='ok'>"
"     <urls>"
"       <url codebase='http://example.com/'/>"
"     </urls>"
"     <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
"       <packages>"
"         <package name='extension_1_2_3_4.crx'/>"
"       </packages>"
"     </manifest>"
"   </updatecheck>"
" </app>"
"</response>";

// Indicates no updates available - this should not be a parse error.
const char* kNoUpdate =
"<?xml version='1.0' encoding='UTF-8'?>"
"<response protocol='3.0'>"
" <app appid='12345'>"
"  <updatecheck status='noupdate' />"
" </app>"
"</response>";

// Includes two <app> tags, one with an error.
const char* kTwoAppsOneError =
"<?xml version='1.0' encoding='UTF-8'?>"
"<response protocol='3.0'>"
" <app appid='aaaaaaaa' status='error-unknownApplication'>"
"  <updatecheck status='error-unknownapplication'/>"
" </app>"
" <app appid='bbbbbbbb'>"
"   <updatecheck status='ok'>"
"     <urls>"
"       <url codebase='http://example.com/'/>"
"     </urls>"
"     <manifest version='1.2.3.4' prodversionmin='2.0.143.0'>"
"       <packages>"
"         <package name='extension_1_2_3_4.crx'/>"
"       </packages>"
"     </manifest>"
"   </updatecheck>"
" </app>"
"</response>";

TEST(ComponentUpdaterUpdateResponseTest, TestParser) {
  UpdateResponse parser;

  // Test parsing of a number of invalid xml cases
  EXPECT_FALSE(parser.Parse(std::string()));
  EXPECT_FALSE(parser.errors().empty());

  EXPECT_TRUE(parser.Parse(kMissingAppId));
  EXPECT_TRUE(parser.results().list.empty());
  EXPECT_FALSE(parser.errors().empty());

  EXPECT_TRUE(parser.Parse(kInvalidCodebase));
  EXPECT_TRUE(parser.results().list.empty());
  EXPECT_FALSE(parser.errors().empty());

  EXPECT_TRUE(parser.Parse(kMissingVersion));
  EXPECT_TRUE(parser.results().list.empty());
  EXPECT_FALSE(parser.errors().empty());

  EXPECT_TRUE(parser.Parse(kInvalidVersion));
  EXPECT_TRUE(parser.results().list.empty());
  EXPECT_FALSE(parser.errors().empty());

  // Parse some valid XML, and check that all params came out as expected
  EXPECT_TRUE(parser.Parse(kValidXml));
  EXPECT_TRUE(parser.errors().empty());
  EXPECT_EQ(1u, parser.results().list.size());
  const UpdateResponse::Result* firstResult = &parser.results().list[0];
  EXPECT_EQ(1u, firstResult->crx_urls.size());
  EXPECT_EQ(GURL("http://example.com/"), firstResult->crx_urls[0]);
  EXPECT_EQ(GURL("http://diff.example.com/"), firstResult->crx_diffurls[0]);
  EXPECT_EQ("1.2.3.4", firstResult->manifest.version);
  EXPECT_EQ("2.0.143.0", firstResult->manifest.browser_min_version);
  EXPECT_EQ(1u, firstResult->manifest.packages.size());
  EXPECT_EQ("extension_1_2_3_4.crx", firstResult->manifest.packages[0].name);

  // Parse some xml that uses namespace prefixes.
  EXPECT_TRUE(parser.Parse(kUsesNamespacePrefix));
  EXPECT_TRUE(parser.errors().empty());
  EXPECT_TRUE(parser.Parse(kSimilarTagnames));
  EXPECT_TRUE(parser.errors().empty());
  xmlCleanupGlobals();

  // Parse xml with hash value
  EXPECT_TRUE(parser.Parse(valid_xml_with_hash));
  EXPECT_TRUE(parser.errors().empty());
  EXPECT_FALSE(parser.results().list.empty());
  firstResult = &parser.results().list[0];
  EXPECT_FALSE(firstResult->manifest.packages.empty());
  EXPECT_EQ("1234", firstResult->manifest.packages[0].hash_sha256);

  // Parse xml with package size value
  EXPECT_TRUE(parser.Parse(valid_xml_with_invalid_sizes));
  EXPECT_TRUE(parser.errors().empty());
  EXPECT_FALSE(parser.results().list.empty());
  firstResult = &parser.results().list[0];
  EXPECT_FALSE(firstResult->manifest.packages.empty());
  EXPECT_EQ(1234, firstResult->manifest.packages[0].size);
  EXPECT_EQ(-1234, firstResult->manifest.packages[1].size);
  EXPECT_EQ(0, firstResult->manifest.packages[2].size);
  EXPECT_EQ(0, firstResult->manifest.packages[3].size);
  EXPECT_EQ(0, firstResult->manifest.packages[4].size);
  EXPECT_EQ(0, firstResult->manifest.packages[5].size);

  // Parse xml with a <daystart> element.
  EXPECT_TRUE(parser.Parse(kWithDaystart));
  EXPECT_TRUE(parser.errors().empty());
  EXPECT_FALSE(parser.results().list.empty());
  EXPECT_EQ(parser.results().daystart_elapsed_seconds, 456);

  // Parse a no-update response.
  EXPECT_TRUE(parser.Parse(kNoUpdate));
  EXPECT_TRUE(parser.errors().empty());
  EXPECT_FALSE(parser.results().list.empty());
  firstResult = &parser.results().list[0];
  EXPECT_EQ(firstResult->extension_id, "12345");
  EXPECT_EQ(firstResult->manifest.version, "");

  // Parse xml with one error and one success <app> tag.
  EXPECT_TRUE(parser.Parse(kTwoAppsOneError));
  EXPECT_FALSE(parser.errors().empty());
  EXPECT_EQ(1u, parser.results().list.size());
  firstResult = &parser.results().list[0];
  EXPECT_EQ(firstResult->extension_id, "bbbbbbbb");
}

}  // namespace component_updater