Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 6 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 8 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 9 | #include "update_engine/omaha_response_handler_action.h" |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 10 | #include "update_engine/prefs_mock.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include "update_engine/test_utils.h" |
| 12 | #include "update_engine/utils.h" |
| 13 | |
| 14 | using std::string; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 15 | using testing::Return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | class OmahaResponseHandlerActionTest : public ::testing::Test { |
| 20 | public: |
| 21 | // Return true iff the OmahaResponseHandlerAction succeeded. |
| 22 | // If out is non-NULL, it's set w/ the response from the action. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 23 | bool DoTest(const OmahaResponse& in, |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | const string& boot_dev, |
| 25 | InstallPlan* out); |
| 26 | }; |
| 27 | |
| 28 | class OmahaResponseHandlerActionProcessorDelegate |
| 29 | : public ActionProcessorDelegate { |
| 30 | public: |
| 31 | OmahaResponseHandlerActionProcessorDelegate() |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 32 | : code_(kActionCodeError), |
| 33 | code_set_(false) {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 34 | void ActionCompleted(ActionProcessor* processor, |
| 35 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 36 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 37 | if (action->Type() == OmahaResponseHandlerAction::StaticType()) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 38 | code_ = code; |
| 39 | code_set_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 42 | ActionExitCode code_; |
| 43 | bool code_set_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | namespace { |
| 47 | const string kLongName = |
| 48 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 49 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 50 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 51 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 52 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 53 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 54 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 55 | "-the_update_a.b.c.d_DELTA_.tgz"; |
| 56 | } // namespace {} |
| 57 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 58 | bool OmahaResponseHandlerActionTest::DoTest(const OmahaResponse& in, |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 59 | const string& boot_dev, |
| 60 | InstallPlan* out) { |
| 61 | ActionProcessor processor; |
| 62 | OmahaResponseHandlerActionProcessorDelegate delegate; |
| 63 | processor.set_delegate(&delegate); |
| 64 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 65 | ObjectFeederAction<OmahaResponse> feeder_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 66 | feeder_action.set_obj(in); |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 67 | PrefsMock prefs; |
| 68 | if (in.update_exists) { |
| 69 | EXPECT_CALL(prefs, SetString(kPrefsUpdateCheckResponseHash, in.hash)) |
| 70 | .WillOnce(Return(true)); |
| 71 | } |
| 72 | OmahaResponseHandlerAction response_handler_action(&prefs); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 73 | response_handler_action.set_boot_device(boot_dev); |
| 74 | BondActions(&feeder_action, &response_handler_action); |
| 75 | ObjectCollectorAction<InstallPlan> collector_action; |
| 76 | BondActions(&response_handler_action, &collector_action); |
| 77 | processor.EnqueueAction(&feeder_action); |
| 78 | processor.EnqueueAction(&response_handler_action); |
| 79 | processor.EnqueueAction(&collector_action); |
| 80 | processor.StartProcessing(); |
| 81 | EXPECT_TRUE(!processor.IsRunning()) |
| 82 | << "Update test to handle non-asynch actions"; |
| 83 | if (out) |
| 84 | *out = collector_action.object(); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 85 | EXPECT_TRUE(delegate.code_set_); |
| 86 | return delegate.code_ == kActionCodeSuccess; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST_F(OmahaResponseHandlerActionTest, SimpleTest) { |
| 90 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 91 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 92 | in.update_exists = true; |
| 93 | in.display_version = "a.b.c.d"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 94 | in.codebase = "http://foo/the_update_a.b.c.d.tgz"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 95 | in.more_info_url = "http://more/info"; |
| 96 | in.hash = "HASH+"; |
| 97 | in.size = 12; |
| 98 | in.needs_admin = true; |
| 99 | in.prompt = false; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 100 | in.is_delta = false; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 101 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 102 | EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 103 | EXPECT_TRUE(install_plan.is_full_update); |
| 104 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 105 | EXPECT_EQ(in.hash, install_plan.download_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 106 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 107 | } |
| 108 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 109 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 110 | in.update_exists = true; |
| 111 | in.display_version = "a.b.c.d"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 112 | in.codebase = "http://foo/the_update_a.b.c.d.tgz"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 113 | in.more_info_url = "http://more/info"; |
| 114 | in.hash = "HASHj+"; |
| 115 | in.size = 12; |
| 116 | in.needs_admin = true; |
| 117 | in.prompt = true; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 118 | in.is_delta = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 119 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 120 | EXPECT_TRUE(DoTest(in, "/dev/sda5", &install_plan)); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 121 | EXPECT_FALSE(install_plan.is_full_update); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 122 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 123 | EXPECT_EQ(in.hash, install_plan.download_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 124 | EXPECT_EQ("/dev/sda3", install_plan.install_path); |
| 125 | } |
| 126 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 127 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 128 | in.update_exists = true; |
| 129 | in.display_version = "a.b.c.d"; |
| 130 | in.codebase = kLongName; |
| 131 | in.more_info_url = "http://more/info"; |
| 132 | in.hash = "HASHj+"; |
| 133 | in.size = 12; |
| 134 | in.needs_admin = true; |
| 135 | in.prompt = true; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 136 | in.is_delta = false; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 137 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 138 | EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan)); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 139 | EXPECT_TRUE(install_plan.is_full_update); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 140 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 141 | EXPECT_EQ(in.hash, install_plan.download_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 142 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 147 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 148 | in.update_exists = false; |
| 149 | InstallPlan install_plan; |
| 150 | EXPECT_FALSE(DoTest(in, "/dev/sda1", &install_plan)); |
| 151 | EXPECT_FALSE(install_plan.is_full_update); |
| 152 | EXPECT_EQ("", install_plan.download_url); |
| 153 | EXPECT_EQ("", install_plan.download_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 154 | EXPECT_EQ("", install_plan.install_path); |
| 155 | } |
| 156 | |
| 157 | } // namespace chromeos_update_engine |