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 | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame^] | 15 | using testing::NiceMock; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 16 | using testing::Return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
| 20 | class OmahaResponseHandlerActionTest : public ::testing::Test { |
| 21 | public: |
| 22 | // Return true iff the OmahaResponseHandlerAction succeeded. |
| 23 | // 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] | 24 | bool DoTest(const OmahaResponse& in, |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 25 | const string& boot_dev, |
| 26 | InstallPlan* out); |
| 27 | }; |
| 28 | |
| 29 | class OmahaResponseHandlerActionProcessorDelegate |
| 30 | : public ActionProcessorDelegate { |
| 31 | public: |
| 32 | OmahaResponseHandlerActionProcessorDelegate() |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 33 | : code_(kActionCodeError), |
| 34 | code_set_(false) {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | void ActionCompleted(ActionProcessor* processor, |
| 36 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 37 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 38 | if (action->Type() == OmahaResponseHandlerAction::StaticType()) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 39 | code_ = code; |
| 40 | code_set_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 43 | ActionExitCode code_; |
| 44 | bool code_set_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | namespace { |
| 48 | const string kLongName = |
| 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 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 56 | "-the_update_a.b.c.d_DELTA_.tgz"; |
| 57 | } // namespace {} |
| 58 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 59 | bool OmahaResponseHandlerActionTest::DoTest(const OmahaResponse& in, |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 60 | const string& boot_dev, |
| 61 | InstallPlan* out) { |
| 62 | ActionProcessor processor; |
| 63 | OmahaResponseHandlerActionProcessorDelegate delegate; |
| 64 | processor.set_delegate(&delegate); |
| 65 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 66 | ObjectFeederAction<OmahaResponse> feeder_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 67 | feeder_action.set_obj(in); |
Darin Petkov | 9c096d6 | 2010-11-17 14:49:04 -0800 | [diff] [blame^] | 68 | NiceMock<PrefsMock> prefs; |
Darin Petkov | 73058b4 | 2010-10-06 16:32:19 -0700 | [diff] [blame] | 69 | if (in.update_exists) { |
| 70 | EXPECT_CALL(prefs, SetString(kPrefsUpdateCheckResponseHash, in.hash)) |
| 71 | .WillOnce(Return(true)); |
| 72 | } |
| 73 | OmahaResponseHandlerAction response_handler_action(&prefs); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | response_handler_action.set_boot_device(boot_dev); |
| 75 | BondActions(&feeder_action, &response_handler_action); |
| 76 | ObjectCollectorAction<InstallPlan> collector_action; |
| 77 | BondActions(&response_handler_action, &collector_action); |
| 78 | processor.EnqueueAction(&feeder_action); |
| 79 | processor.EnqueueAction(&response_handler_action); |
| 80 | processor.EnqueueAction(&collector_action); |
| 81 | processor.StartProcessing(); |
| 82 | EXPECT_TRUE(!processor.IsRunning()) |
| 83 | << "Update test to handle non-asynch actions"; |
| 84 | if (out) |
| 85 | *out = collector_action.object(); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 86 | EXPECT_TRUE(delegate.code_set_); |
| 87 | return delegate.code_ == kActionCodeSuccess; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST_F(OmahaResponseHandlerActionTest, SimpleTest) { |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 91 | ScopedPathUnlinker deadline_unlinker( |
| 92 | OmahaResponseHandlerAction::kDeadlineFile); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 93 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 94 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 95 | in.update_exists = true; |
| 96 | in.display_version = "a.b.c.d"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 97 | in.codebase = "http://foo/the_update_a.b.c.d.tgz"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 98 | in.more_info_url = "http://more/info"; |
| 99 | in.hash = "HASH+"; |
| 100 | in.size = 12; |
| 101 | in.needs_admin = true; |
| 102 | in.prompt = false; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 103 | in.is_delta = false; |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 104 | in.deadline = "20101020"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 105 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 106 | EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 107 | EXPECT_TRUE(install_plan.is_full_update); |
| 108 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 109 | EXPECT_EQ(in.hash, install_plan.download_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 110 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 111 | string deadline; |
| 112 | EXPECT_TRUE(utils::ReadFileToString( |
| 113 | OmahaResponseHandlerAction::kDeadlineFile, |
| 114 | &deadline)); |
| 115 | EXPECT_EQ("20101020", deadline); |
| 116 | struct stat deadline_stat; |
| 117 | EXPECT_EQ(0, stat(OmahaResponseHandlerAction::kDeadlineFile, |
| 118 | &deadline_stat)); |
| 119 | EXPECT_EQ(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, |
| 120 | deadline_stat.st_mode); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | } |
| 122 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 123 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 124 | in.update_exists = true; |
| 125 | in.display_version = "a.b.c.d"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 126 | in.codebase = "http://foo/the_update_a.b.c.d.tgz"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 127 | in.more_info_url = "http://more/info"; |
| 128 | in.hash = "HASHj+"; |
| 129 | in.size = 12; |
| 130 | in.needs_admin = true; |
| 131 | in.prompt = true; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 132 | in.is_delta = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 133 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 134 | EXPECT_TRUE(DoTest(in, "/dev/sda5", &install_plan)); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 135 | EXPECT_FALSE(install_plan.is_full_update); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 136 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 137 | EXPECT_EQ(in.hash, install_plan.download_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 138 | EXPECT_EQ("/dev/sda3", install_plan.install_path); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 139 | string deadline; |
| 140 | EXPECT_TRUE(utils::ReadFileToString( |
| 141 | OmahaResponseHandlerAction::kDeadlineFile, |
| 142 | &deadline) && deadline.empty()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 143 | } |
| 144 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 145 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 146 | in.update_exists = true; |
| 147 | in.display_version = "a.b.c.d"; |
| 148 | in.codebase = kLongName; |
| 149 | in.more_info_url = "http://more/info"; |
| 150 | in.hash = "HASHj+"; |
| 151 | in.size = 12; |
| 152 | in.needs_admin = true; |
| 153 | in.prompt = true; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 154 | in.is_delta = false; |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 155 | in.deadline = "some-deadline"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 156 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 157 | EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan)); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 158 | EXPECT_TRUE(install_plan.is_full_update); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 159 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 160 | EXPECT_EQ(in.hash, install_plan.download_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 161 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 162 | string deadline; |
| 163 | EXPECT_TRUE(utils::ReadFileToString( |
| 164 | OmahaResponseHandlerAction::kDeadlineFile, |
| 165 | &deadline)); |
| 166 | EXPECT_EQ("some-deadline", deadline); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 171 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 172 | in.update_exists = false; |
| 173 | InstallPlan install_plan; |
| 174 | EXPECT_FALSE(DoTest(in, "/dev/sda1", &install_plan)); |
| 175 | EXPECT_FALSE(install_plan.is_full_update); |
| 176 | EXPECT_EQ("", install_plan.download_url); |
| 177 | EXPECT_EQ("", install_plan.download_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 178 | EXPECT_EQ("", install_plan.install_path); |
| 179 | } |
| 180 | |
| 181 | } // namespace chromeos_update_engine |