Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS 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. |
| 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 7 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 8 | #include <glib.h> |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 9 | |
| 10 | #include "base/string_util.h" |
| 11 | #include "gtest/gtest.h" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 12 | #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" |
| 16 | #include "update_engine/test_utils.h" |
| 17 | |
| 18 | using std::string; |
| 19 | using std::vector; |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | class OmahaRequestActionTest : public ::testing::Test { }; |
| 24 | |
| 25 | namespace { |
| 26 | string GetNoUpdateResponse(const string& app_id) { |
| 27 | return string( |
| 28 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 29 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 30 | "appid=\"") + app_id + "\" status=\"ok\"><ping " |
| 31 | "status=\"ok\"/><updatecheck status=\"noupdate\"/></app></gupdate>"; |
| 32 | } |
| 33 | |
| 34 | string GetUpdateResponse(const string& app_id, |
| 35 | const string& display_version, |
| 36 | const string& more_info_url, |
| 37 | const string& prompt, |
| 38 | const string& codebase, |
| 39 | const string& hash, |
| 40 | const string& needsadmin, |
| 41 | const string& size) { |
| 42 | return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 43 | "xmlns=\"http://www.google.com/update2/response\" " |
| 44 | "protocol=\"2.0\"><app " |
| 45 | "appid=\"") + app_id + "\" status=\"ok\"><ping " |
| 46 | "status=\"ok\"/><updatecheck DisplayVersion=\"" + display_version + "\" " |
| 47 | "MoreInfo=\"" + more_info_url + "\" Prompt=\"" + prompt + "\" " |
| 48 | "codebase=\"" + codebase + "\" " |
| 49 | "hash=\"" + hash + "\" needsadmin=\"" + needsadmin + "\" " |
| 50 | "size=\"" + size + "\" status=\"ok\"/></app></gupdate>"; |
| 51 | } |
| 52 | |
| 53 | class OmahaRequestActionTestProcessorDelegate : public ActionProcessorDelegate { |
| 54 | public: |
| 55 | OmahaRequestActionTestProcessorDelegate() |
| 56 | : loop_(NULL), |
| 57 | expected_success_(true) {} |
| 58 | virtual ~OmahaRequestActionTestProcessorDelegate() { |
| 59 | } |
| 60 | virtual void ProcessingDone(const ActionProcessor* processor, bool success) { |
| 61 | ASSERT_TRUE(loop_); |
| 62 | g_main_loop_quit(loop_); |
| 63 | } |
| 64 | |
| 65 | virtual void ActionCompleted(ActionProcessor* processor, |
| 66 | AbstractAction* action, |
| 67 | bool success) { |
| 68 | // make sure actions always succeed |
| 69 | if (action->Type() == OmahaRequestAction::StaticType()) |
| 70 | EXPECT_EQ(expected_success_, success); |
| 71 | else |
| 72 | EXPECT_TRUE(success); |
| 73 | } |
| 74 | GMainLoop *loop_; |
| 75 | bool expected_success_; |
| 76 | }; |
| 77 | |
| 78 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 79 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 80 | processor->StartProcessing(); |
| 81 | return FALSE; |
| 82 | } |
| 83 | |
| 84 | } // namespace {} |
| 85 | |
| 86 | class OutputObjectCollectorAction; |
| 87 | |
| 88 | template<> |
| 89 | class ActionTraits<OutputObjectCollectorAction> { |
| 90 | public: |
| 91 | // Does not take an object for input |
| 92 | typedef OmahaResponse InputObjectType; |
| 93 | // On success, puts the output path on output |
| 94 | typedef NoneType OutputObjectType; |
| 95 | }; |
| 96 | |
| 97 | class OutputObjectCollectorAction : public Action<OutputObjectCollectorAction> { |
| 98 | public: |
| 99 | OutputObjectCollectorAction() : has_input_object_(false) {} |
| 100 | void PerformAction() { |
| 101 | // copy input object |
| 102 | has_input_object_ = HasInputObject(); |
| 103 | if (has_input_object_) |
| 104 | omaha_response_ = GetInputObject(); |
| 105 | processor_->ActionComplete(this, true); |
| 106 | } |
| 107 | // Should never be called |
| 108 | void TerminateProcessing() { |
| 109 | CHECK(false); |
| 110 | } |
| 111 | // Debugging/logging |
| 112 | static std::string StaticType() { |
| 113 | return "OutputObjectCollectorAction"; |
| 114 | } |
| 115 | std::string Type() const { return StaticType(); } |
| 116 | bool has_input_object_; |
| 117 | OmahaResponse omaha_response_; |
| 118 | }; |
| 119 | |
| 120 | // returns true iff an output response was obtained from the |
| 121 | // OmahaRequestAction. out_response may be NULL. |
| 122 | // out_post_data may be null; if non-null, the post-data received by the |
| 123 | // mock HttpFetcher is returned. |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 124 | bool TestUpdateCheck(const OmahaRequestParams& params, |
| 125 | const string& http_response, |
| 126 | bool expected_success, |
| 127 | OmahaResponse* out_response, |
| 128 | vector<char>* out_post_data) { |
| 129 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 130 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 131 | http_response.size()); |
| 132 | ObjectFeederAction<OmahaRequestParams> feeder_action; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 133 | OmahaRequestAction action(NULL, fetcher); // takes ownership of fetcher |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 134 | OmahaRequestActionTestProcessorDelegate delegate; |
| 135 | delegate.loop_ = loop; |
| 136 | delegate.expected_success_ = expected_success; |
| 137 | ActionProcessor processor; |
| 138 | feeder_action.set_obj(params); |
| 139 | processor.set_delegate(&delegate); |
| 140 | processor.EnqueueAction(&feeder_action); |
| 141 | processor.EnqueueAction(&action); |
| 142 | |
| 143 | OutputObjectCollectorAction collector_action; |
| 144 | |
| 145 | BondActions(&feeder_action, &action); |
| 146 | BondActions(&action, &collector_action); |
| 147 | processor.EnqueueAction(&collector_action); |
| 148 | |
| 149 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 150 | g_main_loop_run(loop); |
| 151 | g_main_loop_unref(loop); |
| 152 | if (collector_action.has_input_object_ && out_response) |
| 153 | *out_response = collector_action.omaha_response_; |
| 154 | if (out_post_data) |
| 155 | *out_post_data = fetcher->post_data(); |
| 156 | return collector_action.has_input_object_; |
| 157 | } |
| 158 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 159 | // Tests Event requests -- they should always succeed. |out_post_data| |
| 160 | // may be null; if non-null, the post-data received by the mock |
| 161 | // HttpFetcher is returned. |
| 162 | void TestEvent(const OmahaRequestParams& params, |
| 163 | OmahaEvent* event, |
| 164 | const string& http_response, |
| 165 | vector<char>* out_post_data) { |
| 166 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 167 | MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(), |
| 168 | http_response.size()); |
| 169 | ObjectFeederAction<OmahaRequestParams> feeder_action; |
| 170 | OmahaRequestAction action(event, fetcher); // takes ownership of fetcher |
| 171 | OmahaRequestActionTestProcessorDelegate delegate; |
| 172 | delegate.loop_ = loop; |
| 173 | ActionProcessor processor; |
| 174 | feeder_action.set_obj(params); |
| 175 | processor.set_delegate(&delegate); |
| 176 | processor.EnqueueAction(&feeder_action); |
| 177 | processor.EnqueueAction(&action); |
| 178 | |
| 179 | BondActions(&feeder_action, &action); |
| 180 | |
| 181 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 182 | g_main_loop_run(loop); |
| 183 | g_main_loop_unref(loop); |
| 184 | if (out_post_data) |
| 185 | *out_post_data = fetcher->post_data(); |
| 186 | } |
| 187 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 188 | TEST(OmahaRequestActionTest, NoUpdateTest) { |
| 189 | OmahaRequestParams params("", // machine_id |
| 190 | "", // user_id |
| 191 | OmahaRequestParams::kOsPlatform, |
| 192 | OmahaRequestParams::kOsVersion, |
| 193 | "", // os_sp |
| 194 | "x86-generic", |
| 195 | OmahaRequestParams::kAppId, |
| 196 | "0.1.0.0", |
| 197 | "en-US", |
| 198 | "unittest", |
| 199 | ""); // url |
| 200 | OmahaResponse response; |
| 201 | ASSERT_TRUE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 202 | TestUpdateCheck(params, |
| 203 | GetNoUpdateResponse(OmahaRequestParams::kAppId), |
| 204 | true, |
| 205 | &response, |
| 206 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 207 | EXPECT_FALSE(response.update_exists); |
| 208 | } |
| 209 | |
| 210 | TEST(OmahaRequestActionTest, ValidUpdateTest) { |
| 211 | OmahaRequestParams params("machine_id", |
| 212 | "user_id", |
| 213 | OmahaRequestParams::kOsPlatform, |
| 214 | OmahaRequestParams::kOsVersion, |
| 215 | "service_pack", |
| 216 | "arm-generic", |
| 217 | OmahaRequestParams::kAppId, |
| 218 | "0.1.0.0", |
| 219 | "en-US", |
| 220 | "unittest_track", |
| 221 | ""); // url |
| 222 | OmahaResponse response; |
| 223 | ASSERT_TRUE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 224 | TestUpdateCheck(params, |
| 225 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 226 | "1.2.3.4", // version |
| 227 | "http://more/info", |
| 228 | "true", // prompt |
| 229 | "http://code/base", // dl url |
| 230 | "HASH1234=", // checksum |
| 231 | "false", // needs admin |
| 232 | "123"), // size |
| 233 | true, |
| 234 | &response, |
| 235 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 236 | EXPECT_TRUE(response.update_exists); |
| 237 | EXPECT_EQ("1.2.3.4", response.display_version); |
| 238 | EXPECT_EQ("http://code/base", response.codebase); |
| 239 | EXPECT_EQ("http://more/info", response.more_info_url); |
| 240 | EXPECT_EQ("HASH1234=", response.hash); |
| 241 | EXPECT_EQ(123, response.size); |
| 242 | EXPECT_FALSE(response.needs_admin); |
| 243 | EXPECT_TRUE(response.prompt); |
| 244 | } |
| 245 | |
| 246 | TEST(OmahaRequestActionTest, NoOutputPipeTest) { |
| 247 | OmahaRequestParams params("", // machine_id |
| 248 | "", // usr_id |
| 249 | OmahaRequestParams::kOsPlatform, |
| 250 | OmahaRequestParams::kOsVersion, |
| 251 | "", // os_sp |
| 252 | "", // os_board |
| 253 | OmahaRequestParams::kAppId, |
| 254 | "0.1.0.0", |
| 255 | "en-US", |
| 256 | "unittest", |
| 257 | ""); // url |
| 258 | const string http_response(GetNoUpdateResponse(OmahaRequestParams::kAppId)); |
| 259 | |
| 260 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 261 | |
| 262 | ObjectFeederAction<OmahaRequestParams> feeder_action; |
| 263 | feeder_action.set_obj(params); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 264 | OmahaRequestAction action(NULL, |
| 265 | new MockHttpFetcher(http_response.data(), |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 266 | http_response.size())); |
| 267 | OmahaRequestActionTestProcessorDelegate delegate; |
| 268 | delegate.loop_ = loop; |
| 269 | ActionProcessor processor; |
| 270 | processor.set_delegate(&delegate); |
| 271 | processor.EnqueueAction(&feeder_action); |
| 272 | processor.EnqueueAction(&action); |
| 273 | BondActions(&feeder_action, &action); |
| 274 | |
| 275 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 276 | g_main_loop_run(loop); |
| 277 | g_main_loop_unref(loop); |
| 278 | EXPECT_FALSE(processor.IsRunning()); |
| 279 | } |
| 280 | |
| 281 | TEST(OmahaRequestActionTest, InvalidXmlTest) { |
| 282 | OmahaRequestParams params("machine_id", |
| 283 | "user_id", |
| 284 | OmahaRequestParams::kOsPlatform, |
| 285 | OmahaRequestParams::kOsVersion, |
| 286 | "service_pack", |
| 287 | "x86-generic", |
| 288 | OmahaRequestParams::kAppId, |
| 289 | "0.1.0.0", |
| 290 | "en-US", |
| 291 | "unittest_track", |
| 292 | "http://url"); |
| 293 | OmahaResponse response; |
| 294 | ASSERT_FALSE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 295 | TestUpdateCheck(params, |
| 296 | "invalid xml>", |
| 297 | false, |
| 298 | &response, |
| 299 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 300 | EXPECT_FALSE(response.update_exists); |
| 301 | } |
| 302 | |
| 303 | TEST(OmahaRequestActionTest, MissingStatusTest) { |
| 304 | OmahaRequestParams params("machine_id", |
| 305 | "user_id", |
| 306 | OmahaRequestParams::kOsPlatform, |
| 307 | OmahaRequestParams::kOsVersion, |
| 308 | "service_pack", |
| 309 | "x86-generic", |
| 310 | OmahaRequestParams::kAppId, |
| 311 | "0.1.0.0", |
| 312 | "en-US", |
| 313 | "unittest_track", |
| 314 | "http://url"); |
| 315 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 316 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 317 | params, |
| 318 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 319 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 320 | "appid=\"foo\" status=\"ok\"><ping " |
| 321 | "status=\"ok\"/><updatecheck/></app></gupdate>", |
| 322 | false, |
| 323 | &response, |
| 324 | NULL)); |
| 325 | EXPECT_FALSE(response.update_exists); |
| 326 | } |
| 327 | |
| 328 | TEST(OmahaRequestActionTest, InvalidStatusTest) { |
| 329 | OmahaRequestParams params("machine_id", |
| 330 | "user_id", |
| 331 | OmahaRequestParams::kOsPlatform, |
| 332 | OmahaRequestParams::kOsVersion, |
| 333 | "service_pack", |
| 334 | "x86-generic", |
| 335 | OmahaRequestParams::kAppId, |
| 336 | "0.1.0.0", |
| 337 | "en-US", |
| 338 | "unittest_track", |
| 339 | "http://url"); |
| 340 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 341 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 342 | params, |
| 343 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 344 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 345 | "appid=\"foo\" status=\"ok\"><ping " |
| 346 | "status=\"ok\"/><updatecheck status=\"foo\"/></app></gupdate>", |
| 347 | false, |
| 348 | &response, |
| 349 | NULL)); |
| 350 | EXPECT_FALSE(response.update_exists); |
| 351 | } |
| 352 | |
| 353 | TEST(OmahaRequestActionTest, MissingNodesetTest) { |
| 354 | OmahaRequestParams params("machine_id", |
| 355 | "user_id", |
| 356 | OmahaRequestParams::kOsPlatform, |
| 357 | OmahaRequestParams::kOsVersion, |
| 358 | "service_pack", |
| 359 | "x86-generic", |
| 360 | OmahaRequestParams::kAppId, |
| 361 | "0.1.0.0", |
| 362 | "en-US", |
| 363 | "unittest_track", |
| 364 | "http://url"); |
| 365 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 366 | ASSERT_FALSE(TestUpdateCheck( |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 367 | params, |
| 368 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gupdate " |
| 369 | "xmlns=\"http://www.google.com/update2/response\" protocol=\"2.0\"><app " |
| 370 | "appid=\"foo\" status=\"ok\"><ping " |
| 371 | "status=\"ok\"/></app></gupdate>", |
| 372 | false, |
| 373 | &response, |
| 374 | NULL)); |
| 375 | EXPECT_FALSE(response.update_exists); |
| 376 | } |
| 377 | |
| 378 | TEST(OmahaRequestActionTest, MissingFieldTest) { |
| 379 | OmahaRequestParams params("machine_id", |
| 380 | "user_id", |
| 381 | OmahaRequestParams::kOsPlatform, |
| 382 | OmahaRequestParams::kOsVersion, |
| 383 | "service_pack", |
| 384 | "x86-generic", |
| 385 | OmahaRequestParams::kAppId, |
| 386 | "0.1.0.0", |
| 387 | "en-US", |
| 388 | "unittest_track", |
| 389 | "http://url"); |
| 390 | OmahaResponse response; |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 391 | ASSERT_TRUE(TestUpdateCheck(params, |
| 392 | string("<?xml version=\"1.0\" " |
| 393 | "encoding=\"UTF-8\"?><gupdate " |
| 394 | "xmlns=\"http://www.google.com/" |
| 395 | "update2/response\" " |
| 396 | "protocol=\"2.0\"><app appid=\"") + |
| 397 | OmahaRequestParams::kAppId |
| 398 | + "\" status=\"ok\"><ping " |
| 399 | "status=\"ok\"/><updatecheck " |
| 400 | "DisplayVersion=\"1.2.3.4\" " |
| 401 | "Prompt=\"false\" " |
| 402 | "codebase=\"http://code/base\" " |
| 403 | "hash=\"HASH1234=\" needsadmin=\"true\" " |
| 404 | "size=\"123\" " |
| 405 | "status=\"ok\"/></app></gupdate>", |
| 406 | true, |
| 407 | &response, |
| 408 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 409 | EXPECT_TRUE(response.update_exists); |
| 410 | EXPECT_EQ("1.2.3.4", response.display_version); |
| 411 | EXPECT_EQ("http://code/base", response.codebase); |
| 412 | EXPECT_EQ("", response.more_info_url); |
| 413 | EXPECT_EQ("HASH1234=", response.hash); |
| 414 | EXPECT_EQ(123, response.size); |
| 415 | EXPECT_TRUE(response.needs_admin); |
| 416 | EXPECT_FALSE(response.prompt); |
| 417 | } |
| 418 | |
| 419 | namespace { |
| 420 | class 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 | |
| 429 | gboolean 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 | |
| 438 | TEST(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", |
| 449 | "http://url"); |
| 450 | string http_response("doesn't matter"); |
| 451 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 452 | |
| 453 | ObjectFeederAction<OmahaRequestParams> feeder_action; |
| 454 | feeder_action.set_obj(params); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 455 | OmahaRequestAction action(NULL, |
| 456 | new MockHttpFetcher(http_response.data(), |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 457 | http_response.size())); |
| 458 | TerminateEarlyTestProcessorDelegate delegate; |
| 459 | delegate.loop_ = loop; |
| 460 | ActionProcessor processor; |
| 461 | processor.set_delegate(&delegate); |
| 462 | processor.EnqueueAction(&feeder_action); |
| 463 | processor.EnqueueAction(&action); |
| 464 | BondActions(&feeder_action, &action); |
| 465 | |
| 466 | g_timeout_add(0, &TerminateTransferTestStarter, &processor); |
| 467 | g_main_loop_run(loop); |
| 468 | g_main_loop_unref(loop); |
| 469 | } |
| 470 | |
| 471 | TEST(OmahaRequestActionTest, XmlEncodeTest) { |
| 472 | EXPECT_EQ("ab", XmlEncode("ab")); |
| 473 | EXPECT_EQ("a<b", XmlEncode("a<b")); |
| 474 | EXPECT_EQ("foo-Ω", XmlEncode("foo-\xce\xa9")); |
| 475 | EXPECT_EQ("<&>", XmlEncode("<&>")); |
| 476 | EXPECT_EQ("&lt;&amp;&gt;", XmlEncode("<&>")); |
| 477 | |
| 478 | vector<char> post_data; |
| 479 | |
| 480 | // Make sure XML Encode is being called on the params |
| 481 | OmahaRequestParams params("testthemachine<id", |
| 482 | "testtheuser_id<", |
| 483 | OmahaRequestParams::kOsPlatform, |
| 484 | OmahaRequestParams::kOsVersion, |
| 485 | "testtheservice_pack>", |
| 486 | "x86 generic", |
| 487 | OmahaRequestParams::kAppId, |
| 488 | "0.1.0.0", |
| 489 | "en-US", |
| 490 | "unittest_track", |
| 491 | "http://url"); |
| 492 | OmahaResponse response; |
| 493 | ASSERT_FALSE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 494 | TestUpdateCheck(params, |
| 495 | "invalid xml>", |
| 496 | false, |
| 497 | &response, |
| 498 | &post_data)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 499 | // convert post_data to string |
| 500 | string post_str(&post_data[0], post_data.size()); |
| 501 | EXPECT_NE(post_str.find("testthemachine<id"), string::npos); |
| 502 | EXPECT_EQ(post_str.find("testthemachine<id"), string::npos); |
| 503 | EXPECT_NE(post_str.find("testtheuser_id&lt;"), string::npos); |
| 504 | EXPECT_EQ(post_str.find("testtheuser_id<"), string::npos); |
| 505 | EXPECT_NE(post_str.find("testtheservice_pack>"), string::npos); |
| 506 | EXPECT_EQ(post_str.find("testtheservice_pack>"), string::npos); |
| 507 | EXPECT_NE(post_str.find("x86 generic"), string::npos); |
| 508 | } |
| 509 | |
| 510 | TEST(OmahaRequestActionTest, XmlDecodeTest) { |
| 511 | OmahaRequestParams params("machine_id", |
| 512 | "user_id", |
| 513 | OmahaRequestParams::kOsPlatform, |
| 514 | OmahaRequestParams::kOsVersion, |
| 515 | "service_pack", |
| 516 | "x86-generic", |
| 517 | OmahaRequestParams::kAppId, |
| 518 | "0.1.0.0", |
| 519 | "en-US", |
| 520 | "unittest_track", |
| 521 | "http://url"); |
| 522 | OmahaResponse response; |
| 523 | ASSERT_TRUE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 524 | TestUpdateCheck(params, |
| 525 | GetUpdateResponse(OmahaRequestParams::kAppId, |
| 526 | "1.2.3.4", // version |
| 527 | "testthe<url", // more info |
| 528 | "true", // prompt |
| 529 | "testthe&codebase", // dl url |
| 530 | "HASH1234=", // checksum |
| 531 | "false", // needs admin |
| 532 | "123"), // size |
| 533 | true, |
| 534 | &response, |
| 535 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 536 | |
| 537 | EXPECT_EQ(response.more_info_url, "testthe<url"); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 538 | EXPECT_EQ(response.codebase, "testthe&codebase"); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | TEST(OmahaRequestActionTest, ParseIntTest) { |
| 542 | OmahaRequestParams params("machine_id", |
| 543 | "user_id", |
| 544 | OmahaRequestParams::kOsPlatform, |
| 545 | OmahaRequestParams::kOsVersion, |
| 546 | "service_pack", |
| 547 | "the_board", |
| 548 | OmahaRequestParams::kAppId, |
| 549 | "0.1.0.0", |
| 550 | "en-US", |
| 551 | "unittest_track", |
| 552 | "http://url"); |
| 553 | OmahaResponse response; |
| 554 | ASSERT_TRUE( |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 555 | 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 |
| 565 | true, |
| 566 | &response, |
| 567 | NULL)); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 568 | |
| 569 | EXPECT_EQ(response.size, 123123123123123ll); |
| 570 | } |
| 571 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame^] | 572 | TEST(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", |
| 584 | "http://url"); |
| 585 | OmahaResponse response; |
| 586 | ASSERT_FALSE(TestUpdateCheck(params, |
| 587 | "invalid xml>", |
| 588 | false, |
| 589 | &response, |
| 590 | &post_data)); |
| 591 | // convert post_data to string |
| 592 | string post_str(&post_data[0], post_data.size()); |
| 593 | EXPECT_NE(post_str.find(" <o:ping active=\"0\"></o:ping>\n" |
| 594 | " <o:updatecheck></o:updatecheck>\n"), |
| 595 | string::npos); |
| 596 | EXPECT_EQ(post_str.find("o:event"), string::npos); |
| 597 | } |
| 598 | |
| 599 | TEST(OmahaRequestActionTest, FormatEventOutputTest) { |
| 600 | vector<char> post_data; |
| 601 | OmahaRequestParams params("machine_id", |
| 602 | "user_id", |
| 603 | OmahaRequestParams::kOsPlatform, |
| 604 | OmahaRequestParams::kOsVersion, |
| 605 | "service_pack", |
| 606 | "x86-generic", |
| 607 | OmahaRequestParams::kAppId, |
| 608 | "0.1.0.0", |
| 609 | "en-US", |
| 610 | "unittest_track", |
| 611 | "http://url"); |
| 612 | TestEvent(params, |
| 613 | new OmahaEvent(OmahaEvent::kTypeDownloadComplete, |
| 614 | OmahaEvent::kResultError, |
| 615 | 5), |
| 616 | "invalid xml>", |
| 617 | &post_data); |
| 618 | // convert post_data to string |
| 619 | string post_str(&post_data[0], post_data.size()); |
| 620 | string expected_event = StringPrintf( |
| 621 | " <o:event eventtype=\"%d\" eventresult=\"%d\" " |
| 622 | "errorcode=\"%d\"></o:event>\n", |
| 623 | OmahaEvent::kTypeDownloadComplete, |
| 624 | OmahaEvent::kResultError, |
| 625 | 5); |
| 626 | EXPECT_NE(post_str.find(expected_event), string::npos); |
| 627 | EXPECT_EQ(post_str.find("o:updatecheck"), string::npos); |
| 628 | } |
| 629 | |
| 630 | TEST(OmahaRequestActionTest, IsEventTest) { |
| 631 | string http_response("doesn't matter"); |
| 632 | |
| 633 | OmahaRequestAction update_check_action( |
| 634 | NULL, |
| 635 | new MockHttpFetcher(http_response.data(), |
| 636 | http_response.size())); |
| 637 | EXPECT_FALSE(update_check_action.IsEvent()); |
| 638 | |
| 639 | OmahaRequestAction event_action( |
| 640 | new OmahaEvent(OmahaEvent::kTypeInstallComplete, |
| 641 | OmahaEvent::kResultError, |
| 642 | 0), |
| 643 | new MockHttpFetcher(http_response.data(), |
| 644 | http_response.size())); |
| 645 | EXPECT_TRUE(event_action.IsEvent()); |
| 646 | } |
| 647 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 648 | } // namespace chromeos_update_engine |