blob: 05ec1b83d9f55cf758d7fbeed386b5c2e98eb29c [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
Darin Petkov6a5b3222010-07-13 14:55:28 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <vector>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07007
Darin Petkov6a5b3222010-07-13 14:55:28 -07008#include <glib.h>
Darin Petkov0dc8e9a2010-07-14 14:51:57 -07009
10#include "base/string_util.h"
11#include "gtest/gtest.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070012#include "update_engine/action_pipe.h"
13#include "update_engine/mock_http_fetcher.h"
14#include "update_engine/omaha_hash_calculator.h"
15#include "update_engine/omaha_request_action.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070016#include "update_engine/omaha_request_params.h"
Darin Petkov6a5b3222010-07-13 14:55:28 -070017#include "update_engine/test_utils.h"
18
19using std::string;
20using std::vector;
21
22namespace chromeos_update_engine {
23
24class OmahaRequestActionTest : public ::testing::Test { };
25
26namespace {
27string GetNoUpdateResponse(const string& app_id) {
28 return string(
29 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
30 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
31 "appid=\"") + app_id + "\" status=\"ok\"><ping "
32 "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>";
33}
34
35string GetUpdateResponse(const string& app_id,
36 const string& display_version,
37 const string& more_info_url,
38 const string& prompt,
39 const string& codebase,
40 const string& hash,
41 const string& needsadmin,
42 const string& size) {
43 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
44 "xmlns=\"http://www.google.com/update2/response\" "
45 "protocol=\"2.0\"><app "
46 "appid=\"") + app_id + "\" status=\"ok\"><ping "
47 "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" "
48 "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" "
Andrew de los Reyes3270f742010-07-15 22:28:14 -070049 "IsDelta=\"true\" "
Darin Petkov6a5b3222010-07-13 14:55:28 -070050 "codebase=\"" + codebase + "\" "
51 "hash=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" "
52 "size=\"" + size + "\" status=\"ok\"/></app></gupdate>";
53}
54
55class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate {
56 public:
57 OmahaRequestActionTestProcessorDelegate()
58 : loop_(NULL),
Darin Petkovc1a8b422010-07-19 11:34:49 -070059 expected_code_(kActionCodeSuccess) {}
Darin Petkov6a5b3222010-07-13 14:55:28 -070060 virtual ~OmahaRequestActionTestProcessorDelegate() {
61 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070062 virtual void ProcessingDone(const ActionProcessor* processor,
63 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070064 ASSERT_TRUE(loop_);
65 g_main_loop_quit(loop_);
66 }
67
68 virtual void ActionCompleted(ActionProcessor* processor,
69 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070070 ActionExitCode code) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070071 // make sure actions always succeed
72 if (action->Type() == OmahaRequestAction::StaticType())
Darin Petkovc1a8b422010-07-19 11:34:49 -070073 EXPECT_EQ(expected_code_, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -070074 else
Darin Petkovc1a8b422010-07-19 11:34:49 -070075 EXPECT_EQ(kActionCodeSuccess, code);
Darin Petkov6a5b3222010-07-13 14:55:28 -070076 }
77 GMainLoop *loop_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070078 ActionExitCode expected_code_;
Darin Petkov6a5b3222010-07-13 14:55:28 -070079};
80
81gboolean StartProcessorInRunLoop(gpointer data) {
82 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
83 processor->StartProcessing();
84 return FALSE;
85}
86
87} // namespace {}
88
89class OutputObjectCollectorAction;
90
91template<>
92class ActionTraits<OutputObjectCollectorAction> {
93 public:
94 // Does not take an object for input
95 typedef OmahaResponse InputObjectType;
96 // On success, puts the output path on output
97 typedef NoneType OutputObjectType;
98};
99
100class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> {
101 public:
102 OutputObjectCollectorAction() : has_input_object_(false) {}
103 void PerformAction() {
104 // copy input object
105 has_input_object_ = HasInputObject();
106 if (has_input_object_)
107 omaha_response_ = GetInputObject();
Darin Petkovc1a8b422010-07-19 11:34:49 -0700108 processor_->ActionComplete(this, kActionCodeSuccess);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700109 }
110 // Should never be called
111 void TerminateProcessing() {
112 CHECK(false);
113 }
114 // Debugging/logging
115 static std::string StaticType() {
116 return "OutputObjectCollectorAction";
117 }
118 std::string Type() const { return StaticType(); }
119 bool has_input_object_;
120 OmahaResponse omaha_response_;
121};
122
123// returns true iff an output response was obtained from the
124// OmahaRequestAction. out_response may be NULL.
125// out_post_data may be null; if non-null, the post-data received by the
126// mock HttpFetcher is returned.
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700127bool TestUpdateCheck(const OmahaRequestParams& params,
128 const string& http_response,
Darin Petkovc1a8b422010-07-19 11:34:49 -0700129 ActionExitCode expected_code,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700130 OmahaResponse* out_response,
131 vector<char>* out_post_data) {
132 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
133 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
Darin Petkov6a5b3222010-07-13 14:55:28 -0700134 http_response.size());
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700135 OmahaRequestAction action(params, NULL, fetcher);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700136 OmahaRequestActionTestProcessorDelegate delegate;
137 delegate.loop_ = loop;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700138 delegate.expected_code_ = expected_code;
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700139
Darin Petkov6a5b3222010-07-13 14:55:28 -0700140 ActionProcessor processor;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700141 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700142 processor.EnqueueAction(&action);
143
144 OutputObjectCollectorAction collector_action;
Darin Petkov6a5b3222010-07-13 14:55:28 -0700145 BondActions(&action, &collector_action);
146 processor.EnqueueAction(&collector_action);
147
148 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
149 g_main_loop_run(loop);
150 g_main_loop_unref(loop);
151 if (collector_action.has_input_object_ && out_response)
152 *out_response = collector_action.omaha_response_;
153 if (out_post_data)
154 *out_post_data = fetcher->post_data();
155 return collector_action.has_input_object_;
156}
157
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700158// Tests Event requests -- they should always succeed. |out_post_data|
159// may be null; if non-null, the post-data received by the mock
160// HttpFetcher is returned.
161void TestEvent(const OmahaRequestParams& params,
162 OmahaEvent* event,
163 const string& http_response,
164 vector<char>* out_post_data) {
165 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
166 MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
167 http_response.size());
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700168 OmahaRequestAction action(params, event, fetcher);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700169 OmahaRequestActionTestProcessorDelegate delegate;
170 delegate.loop_ = loop;
171 ActionProcessor processor;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700172 processor.set_delegate(&delegate);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700173 processor.EnqueueAction(&action);
174
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700175 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
176 g_main_loop_run(loop);
177 g_main_loop_unref(loop);
178 if (out_post_data)
179 *out_post_data = fetcher->post_data();
180}
181
Darin Petkov6a5b3222010-07-13 14:55:28 -0700182TEST(OmahaRequestActionTest, NoUpdateTest) {
183 OmahaRequestParams params("", // machine_id
184 "", // user_id
185 OmahaRequestParams::kOsPlatform,
186 OmahaRequestParams::kOsVersion,
187 "", // os_sp
188 "x86-generic",
189 OmahaRequestParams::kAppId,
190 "0.1.0.0",
191 "en-US",
192 "unittest",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700193 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700194 ""); // url
195 OmahaResponse response;
196 ASSERT_TRUE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700197 TestUpdateCheck(params,
198 GetNoUpdateResponse(OmahaRequestParams::kAppId),
Darin Petkovc1a8b422010-07-19 11:34:49 -0700199 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700200 &response,
201 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700202 EXPECT_FALSE(response.update_exists);
203}
204
205TEST(OmahaRequestActionTest, ValidUpdateTest) {
206 OmahaRequestParams params("machine_id",
207 "user_id",
208 OmahaRequestParams::kOsPlatform,
209 OmahaRequestParams::kOsVersion,
210 "service_pack",
211 "arm-generic",
212 OmahaRequestParams::kAppId,
213 "0.1.0.0",
214 "en-US",
215 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700216 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700217 ""); // url
218 OmahaResponse response;
219 ASSERT_TRUE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700220 TestUpdateCheck(params,
221 GetUpdateResponse(OmahaRequestParams::kAppId,
222 "1.2.3.4", // version
223 "http://more/info",
224 "true", // prompt
225 "http://code/base", // dl url
226 "HASH1234=", // checksum
227 "false", // needs admin
228 "123"), // size
Darin Petkovc1a8b422010-07-19 11:34:49 -0700229 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700230 &response,
231 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700232 EXPECT_TRUE(response.update_exists);
233 EXPECT_EQ("1.2.3.4", response.display_version);
234 EXPECT_EQ("http://code/base", response.codebase);
235 EXPECT_EQ("http://more/info", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700236 EXPECT_TRUE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700237 EXPECT_EQ("HASH1234=", response.hash);
238 EXPECT_EQ(123, response.size);
239 EXPECT_FALSE(response.needs_admin);
240 EXPECT_TRUE(response.prompt);
241}
242
243TEST(OmahaRequestActionTest, NoOutputPipeTest) {
244 OmahaRequestParams params("", // machine_id
245 "", // usr_id
246 OmahaRequestParams::kOsPlatform,
247 OmahaRequestParams::kOsVersion,
248 "", // os_sp
249 "", // os_board
250 OmahaRequestParams::kAppId,
251 "0.1.0.0",
252 "en-US",
253 "unittest",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700254 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700255 ""); // url
256 const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId));
257
258 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
259
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700260 OmahaRequestAction action(params, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700261 new MockHttpFetcher(http_response.data(),
Darin Petkov6a5b3222010-07-13 14:55:28 -0700262 http_response.size()));
263 OmahaRequestActionTestProcessorDelegate delegate;
264 delegate.loop_ = loop;
265 ActionProcessor processor;
266 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700267 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700268
269 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
270 g_main_loop_run(loop);
271 g_main_loop_unref(loop);
272 EXPECT_FALSE(processor.IsRunning());
273}
274
275TEST(OmahaRequestActionTest, InvalidXmlTest) {
276 OmahaRequestParams params("machine_id",
277 "user_id",
278 OmahaRequestParams::kOsPlatform,
279 OmahaRequestParams::kOsVersion,
280 "service_pack",
281 "x86-generic",
282 OmahaRequestParams::kAppId,
283 "0.1.0.0",
284 "en-US",
285 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700286 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700287 "http://url");
288 OmahaResponse response;
289 ASSERT_FALSE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700290 TestUpdateCheck(params,
291 "invalid xml>",
Darin Petkovc1a8b422010-07-19 11:34:49 -0700292 kActionCodeError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700293 &response,
294 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700295 EXPECT_FALSE(response.update_exists);
296}
297
298TEST(OmahaRequestActionTest, MissingStatusTest) {
299 OmahaRequestParams params("machine_id",
300 "user_id",
301 OmahaRequestParams::kOsPlatform,
302 OmahaRequestParams::kOsVersion,
303 "service_pack",
304 "x86-generic",
305 OmahaRequestParams::kAppId,
306 "0.1.0.0",
307 "en-US",
308 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700309 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700310 "http://url");
311 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700312 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov6a5b3222010-07-13 14:55:28 -0700313 params,
314 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
315 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
316 "appid=\"foo\" status=\"ok\"><ping "
317 "status=\"ok\"/><updatecheck/></app></gupdate>",
Darin Petkovc1a8b422010-07-19 11:34:49 -0700318 kActionCodeError,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700319 &response,
320 NULL));
321 EXPECT_FALSE(response.update_exists);
322}
323
324TEST(OmahaRequestActionTest, InvalidStatusTest) {
325 OmahaRequestParams params("machine_id",
326 "user_id",
327 OmahaRequestParams::kOsPlatform,
328 OmahaRequestParams::kOsVersion,
329 "service_pack",
330 "x86-generic",
331 OmahaRequestParams::kAppId,
332 "0.1.0.0",
333 "en-US",
334 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700335 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700336 "http://url");
337 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700338 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov6a5b3222010-07-13 14:55:28 -0700339 params,
340 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
341 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
342 "appid=\"foo\" status=\"ok\"><ping "
343 "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>",
Darin Petkovc1a8b422010-07-19 11:34:49 -0700344 kActionCodeError,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700345 &response,
346 NULL));
347 EXPECT_FALSE(response.update_exists);
348}
349
350TEST(OmahaRequestActionTest, MissingNodesetTest) {
351 OmahaRequestParams params("machine_id",
352 "user_id",
353 OmahaRequestParams::kOsPlatform,
354 OmahaRequestParams::kOsVersion,
355 "service_pack",
356 "x86-generic",
357 OmahaRequestParams::kAppId,
358 "0.1.0.0",
359 "en-US",
360 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700361 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700362 "http://url");
363 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700364 ASSERT_FALSE(TestUpdateCheck(
Darin Petkov6a5b3222010-07-13 14:55:28 -0700365 params,
366 "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate "
367 "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app "
368 "appid=\"foo\" status=\"ok\"><ping "
369 "status=\"ok\"/></app></gupdate>",
Darin Petkovc1a8b422010-07-19 11:34:49 -0700370 kActionCodeError,
Darin Petkov6a5b3222010-07-13 14:55:28 -0700371 &response,
372 NULL));
373 EXPECT_FALSE(response.update_exists);
374}
375
376TEST(OmahaRequestActionTest, MissingFieldTest) {
377 OmahaRequestParams params("machine_id",
378 "user_id",
379 OmahaRequestParams::kOsPlatform,
380 OmahaRequestParams::kOsVersion,
381 "service_pack",
382 "x86-generic",
383 OmahaRequestParams::kAppId,
384 "0.1.0.0",
385 "en-US",
386 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700387 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700388 "http://url");
389 OmahaResponse response;
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700390 ASSERT_TRUE(TestUpdateCheck(params,
391 string("<?xml version=\"1.0\" "
392 "encoding=\"UTF-8\"?><gupdate "
393 "xmlns=\"http://www.google.com/"
394 "update2/response\" "
395 "protocol=\"2.0\"><app appid=\"") +
396 OmahaRequestParams::kAppId
397 + "\" status=\"ok\"><ping "
398 "status=\"ok\"/><updatecheck "
399 "DisplayVersion=\"1.2.3.4\" "
400 "Prompt=\"false\" "
401 "codebase=\"http://code/base\" "
402 "hash=\"HASH1234=\" needsadmin=\"true\" "
403 "size=\"123\" "
404 "status=\"ok\"/></app></gupdate>",
Darin Petkovc1a8b422010-07-19 11:34:49 -0700405 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700406 &response,
407 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700408 EXPECT_TRUE(response.update_exists);
409 EXPECT_EQ("1.2.3.4", response.display_version);
410 EXPECT_EQ("http://code/base", response.codebase);
411 EXPECT_EQ("", response.more_info_url);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700412 EXPECT_FALSE(response.is_delta);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700413 EXPECT_EQ("HASH1234=", response.hash);
414 EXPECT_EQ(123, response.size);
415 EXPECT_TRUE(response.needs_admin);
416 EXPECT_FALSE(response.prompt);
417}
418
419namespace {
420class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
421 public:
422 void ProcessingStopped(const ActionProcessor* processor) {
423 ASSERT_TRUE(loop_);
424 g_main_loop_quit(loop_);
425 }
426 GMainLoop *loop_;
427};
428
429gboolean TerminateTransferTestStarter(gpointer data) {
430 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
431 processor->StartProcessing();
432 CHECK(processor->IsRunning());
433 processor->StopProcessing();
434 return FALSE;
435}
436} // namespace {}
437
438TEST(OmahaRequestActionTest, TerminateTransferTest) {
439 OmahaRequestParams params("", // machine_id
440 "", // usr_id
441 OmahaRequestParams::kOsPlatform,
442 OmahaRequestParams::kOsVersion,
443 "", // os_sp
444 "", // os_board
445 OmahaRequestParams::kAppId,
446 "0.1.0.0",
447 "en-US",
448 "unittest",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700449 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700450 "http://url");
451 string http_response("doesn't matter");
452 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
453
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700454 OmahaRequestAction action(params, NULL,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700455 new MockHttpFetcher(http_response.data(),
Darin Petkov6a5b3222010-07-13 14:55:28 -0700456 http_response.size()));
457 TerminateEarlyTestProcessorDelegate delegate;
458 delegate.loop_ = loop;
459 ActionProcessor processor;
460 processor.set_delegate(&delegate);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700461 processor.EnqueueAction(&action);
Darin Petkov6a5b3222010-07-13 14:55:28 -0700462
463 g_timeout_add(0, &TerminateTransferTestStarter, &processor);
464 g_main_loop_run(loop);
465 g_main_loop_unref(loop);
466}
467
468TEST(OmahaRequestActionTest, XmlEncodeTest) {
469 EXPECT_EQ("ab", XmlEncode("ab"));
470 EXPECT_EQ("a&lt;b", XmlEncode("a<b"));
471 EXPECT_EQ("foo-&#x3A9;", XmlEncode("foo-\xce\xa9"));
472 EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>"));
473 EXPECT_EQ("&amp;lt;&amp;amp;&amp;gt;", XmlEncode("&lt;&amp;&gt;"));
474
475 vector<char> post_data;
476
477 // Make sure XML Encode is being called on the params
478 OmahaRequestParams params("testthemachine<id",
479 "testtheuser_id&lt;",
480 OmahaRequestParams::kOsPlatform,
481 OmahaRequestParams::kOsVersion,
482 "testtheservice_pack>",
483 "x86 generic",
484 OmahaRequestParams::kAppId,
485 "0.1.0.0",
486 "en-US",
487 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700488 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700489 "http://url");
490 OmahaResponse response;
491 ASSERT_FALSE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700492 TestUpdateCheck(params,
493 "invalid xml>",
Darin Petkovc1a8b422010-07-19 11:34:49 -0700494 kActionCodeError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700495 &response,
496 &post_data));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700497 // convert post_data to string
498 string post_str(&post_data[0], post_data.size());
499 EXPECT_NE(post_str.find("testthemachine&lt;id"), string::npos);
500 EXPECT_EQ(post_str.find("testthemachine<id"), string::npos);
501 EXPECT_NE(post_str.find("testtheuser_id&amp;lt;"), string::npos);
502 EXPECT_EQ(post_str.find("testtheuser_id&lt;"), string::npos);
503 EXPECT_NE(post_str.find("testtheservice_pack&gt;"), string::npos);
504 EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos);
505 EXPECT_NE(post_str.find("x86 generic"), string::npos);
506}
507
508TEST(OmahaRequestActionTest, XmlDecodeTest) {
509 OmahaRequestParams params("machine_id",
510 "user_id",
511 OmahaRequestParams::kOsPlatform,
512 OmahaRequestParams::kOsVersion,
513 "service_pack",
514 "x86-generic",
515 OmahaRequestParams::kAppId,
516 "0.1.0.0",
517 "en-US",
518 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700519 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700520 "http://url");
521 OmahaResponse response;
522 ASSERT_TRUE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700523 TestUpdateCheck(params,
524 GetUpdateResponse(OmahaRequestParams::kAppId,
525 "1.2.3.4", // version
526 "testthe&lt;url", // more info
527 "true", // prompt
528 "testthe&amp;codebase", // dl url
529 "HASH1234=", // checksum
530 "false", // needs admin
531 "123"), // size
Darin Petkovc1a8b422010-07-19 11:34:49 -0700532 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700533 &response,
534 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700535
536 EXPECT_EQ(response.more_info_url, "testthe<url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700537 EXPECT_EQ(response.codebase, "testthe&codebase");
Darin Petkov6a5b3222010-07-13 14:55:28 -0700538}
539
540TEST(OmahaRequestActionTest, ParseIntTest) {
541 OmahaRequestParams params("machine_id",
542 "user_id",
543 OmahaRequestParams::kOsPlatform,
544 OmahaRequestParams::kOsVersion,
545 "service_pack",
546 "the_board",
547 OmahaRequestParams::kAppId,
548 "0.1.0.0",
549 "en-US",
550 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700551 false, // delta okay
Darin Petkov6a5b3222010-07-13 14:55:28 -0700552 "http://url");
553 OmahaResponse response;
554 ASSERT_TRUE(
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700555 TestUpdateCheck(params,
556 GetUpdateResponse(OmahaRequestParams::kAppId,
557 "1.2.3.4", // version
558 "theurl", // more info
559 "true", // prompt
560 "thecodebase", // dl url
561 "HASH1234=", // checksum
562 "false", // needs admin
563 // overflows int32:
564 "123123123123123"), // size
Darin Petkovc1a8b422010-07-19 11:34:49 -0700565 kActionCodeSuccess,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700566 &response,
567 NULL));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700568
569 EXPECT_EQ(response.size, 123123123123123ll);
570}
571
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700572TEST(OmahaRequestActionTest, FormatUpdateCheckOutputTest) {
573 vector<char> post_data;
574 OmahaRequestParams params("machine_id",
575 "user_id",
576 OmahaRequestParams::kOsPlatform,
577 OmahaRequestParams::kOsVersion,
578 "service_pack",
579 "x86-generic",
580 OmahaRequestParams::kAppId,
581 "0.1.0.0",
582 "en-US",
583 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700584 false, // delta okay
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700585 "http://url");
586 OmahaResponse response;
587 ASSERT_FALSE(TestUpdateCheck(params,
588 "invalid xml>",
Darin Petkovc1a8b422010-07-19 11:34:49 -0700589 kActionCodeError,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700590 &response,
591 &post_data));
592 // convert post_data to string
593 string post_str(&post_data[0], post_data.size());
594 EXPECT_NE(post_str.find(" <o:ping active=\"0\"></o:ping>\n"
595 " <o:updatecheck></o:updatecheck>\n"),
596 string::npos);
597 EXPECT_EQ(post_str.find("o:event"), string::npos);
598}
599
600TEST(OmahaRequestActionTest, FormatEventOutputTest) {
601 vector<char> post_data;
602 OmahaRequestParams params("machine_id",
603 "user_id",
604 OmahaRequestParams::kOsPlatform,
605 OmahaRequestParams::kOsVersion,
606 "service_pack",
607 "x86-generic",
608 OmahaRequestParams::kAppId,
609 "0.1.0.0",
610 "en-US",
611 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700612 false, // delta okay
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700613 "http://url");
614 TestEvent(params,
615 new OmahaEvent(OmahaEvent::kTypeDownloadComplete,
616 OmahaEvent::kResultError,
617 5),
618 "invalid xml>",
619 &post_data);
620 // convert post_data to string
621 string post_str(&post_data[0], post_data.size());
622 string expected_event = StringPrintf(
623 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
624 "errorcode=\"%d\"></o:event>\n",
625 OmahaEvent::kTypeDownloadComplete,
626 OmahaEvent::kResultError,
627 5);
628 EXPECT_NE(post_str.find(expected_event), string::npos);
629 EXPECT_EQ(post_str.find("o:updatecheck"), string::npos);
630}
631
632TEST(OmahaRequestActionTest, IsEventTest) {
633 string http_response("doesn't matter");
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700634 OmahaRequestParams params("machine_id",
635 "user_id",
636 OmahaRequestParams::kOsPlatform,
637 OmahaRequestParams::kOsVersion,
638 "service_pack",
639 "x86-generic",
640 OmahaRequestParams::kAppId,
641 "0.1.0.0",
642 "en-US",
643 "unittest_track",
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700644 false, // delta okay
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700645 "http://url");
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700646
647 OmahaRequestAction update_check_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700648 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700649 NULL,
650 new MockHttpFetcher(http_response.data(),
651 http_response.size()));
652 EXPECT_FALSE(update_check_action.IsEvent());
653
654 OmahaRequestAction event_action(
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700655 params,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700656 new OmahaEvent(OmahaEvent::kTypeInstallComplete,
657 OmahaEvent::kResultError,
658 0),
659 new MockHttpFetcher(http_response.data(),
660 http_response.size()));
661 EXPECT_TRUE(event_action.IsEvent());
662}
663
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700664TEST(OmahaRequestActionTest, FormatDeltaOkayOutputTest) {
665 for (int i = 0; i < 2; i++) {
666 bool delta_okay = i == 1;
667 const char* delta_okay_str = delta_okay ? "true" : "false";
668 vector<char> post_data;
669 OmahaRequestParams params("machine_id",
670 "user_id",
671 OmahaRequestParams::kOsPlatform,
672 OmahaRequestParams::kOsVersion,
673 "service_pack",
674 "x86-generic",
675 OmahaRequestParams::kAppId,
676 "0.1.0.0",
677 "en-US",
678 "unittest_track",
679 delta_okay,
680 "http://url");
681 ASSERT_FALSE(TestUpdateCheck(params,
682 "invalid xml>",
Darin Petkovc1a8b422010-07-19 11:34:49 -0700683 kActionCodeError,
Andrew de los Reyes3f0303a2010-07-15 22:35:35 -0700684 NULL,
685 &post_data));
686 // convert post_data to string
687 string post_str(&post_data[0], post_data.size());
688 EXPECT_NE(post_str.find(StringPrintf(" delta_okay=\"%s\"", delta_okay_str)),
689 string::npos)
690 << "i = " << i;
691 }
692}
693
Darin Petkov6a5b3222010-07-13 14:55:28 -0700694} // namespace chromeos_update_engine