Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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_); |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 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; |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 103 | in.deadline = "20101020"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 104 | InstallPlan install_plan; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 105 | EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 106 | EXPECT_EQ(in.codebase, install_plan.download_url); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 107 | EXPECT_EQ(in.hash, install_plan.payload_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 108 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 109 | string deadline; |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 110 | EXPECT_TRUE(utils::ReadFile( |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 111 | OmahaResponseHandlerAction::kDeadlineFile, |
| 112 | &deadline)); |
| 113 | EXPECT_EQ("20101020", deadline); |
| 114 | struct stat deadline_stat; |
| 115 | EXPECT_EQ(0, stat(OmahaResponseHandlerAction::kDeadlineFile, |
| 116 | &deadline_stat)); |
| 117 | EXPECT_EQ(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, |
| 118 | deadline_stat.st_mode); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 119 | } |
| 120 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 121 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 122 | in.update_exists = true; |
| 123 | in.display_version = "a.b.c.d"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 124 | in.codebase = "http://foo/the_update_a.b.c.d.tgz"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 125 | in.more_info_url = "http://more/info"; |
| 126 | in.hash = "HASHj+"; |
| 127 | in.size = 12; |
| 128 | in.needs_admin = true; |
| 129 | in.prompt = true; |
| 130 | InstallPlan install_plan; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 131 | EXPECT_TRUE(DoTest(in, "/dev/sda5", &install_plan)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 132 | EXPECT_EQ(in.codebase, install_plan.download_url); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 133 | EXPECT_EQ(in.hash, install_plan.payload_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 134 | EXPECT_EQ("/dev/sda3", install_plan.install_path); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 135 | string deadline; |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 136 | EXPECT_TRUE(utils::ReadFile( |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 137 | OmahaResponseHandlerAction::kDeadlineFile, |
| 138 | &deadline) && deadline.empty()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 139 | } |
| 140 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 141 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 142 | in.update_exists = true; |
| 143 | in.display_version = "a.b.c.d"; |
| 144 | in.codebase = kLongName; |
| 145 | in.more_info_url = "http://more/info"; |
| 146 | in.hash = "HASHj+"; |
| 147 | in.size = 12; |
| 148 | in.needs_admin = true; |
| 149 | in.prompt = true; |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 150 | in.deadline = "some-deadline"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 151 | InstallPlan install_plan; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 152 | EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 153 | EXPECT_EQ(in.codebase, install_plan.download_url); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 154 | EXPECT_EQ(in.hash, install_plan.payload_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 155 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 156 | string deadline; |
Gilad Arnold | 19a45f0 | 2012-07-19 12:36:10 -0700 | [diff] [blame] | 157 | EXPECT_TRUE(utils::ReadFile( |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 158 | OmahaResponseHandlerAction::kDeadlineFile, |
| 159 | &deadline)); |
| 160 | EXPECT_EQ("some-deadline", deadline); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 165 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 166 | in.update_exists = false; |
| 167 | InstallPlan install_plan; |
Darin Petkov | 7ed561b | 2011-10-04 02:59:03 -0700 | [diff] [blame] | 168 | EXPECT_FALSE(DoTest(in, "/dev/sda1", &install_plan)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 169 | EXPECT_EQ("", install_plan.download_url); |
Jay Srinivasan | 51dcf26 | 2012-09-13 17:24:32 -0700 | [diff] [blame] | 170 | EXPECT_EQ("", install_plan.payload_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 171 | EXPECT_EQ("", install_plan.install_path); |
| 172 | } |
| 173 | |
Jay Srinivasan | 738fdf3 | 2012-12-07 17:40:54 -0800 | [diff] [blame^] | 174 | TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpTest) { |
| 175 | OmahaResponse in; |
| 176 | in.update_exists = true; |
| 177 | in.display_version = "a.b.c.d"; |
| 178 | in.codebase = "http://test.should/need/hash.checks.signed"; |
| 179 | in.more_info_url = "http://more/info"; |
| 180 | in.hash = "HASHj+"; |
| 181 | in.size = 12; |
| 182 | InstallPlan install_plan; |
| 183 | EXPECT_TRUE(DoTest(in, "/dev/sda5", &install_plan)); |
| 184 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 185 | EXPECT_EQ(in.hash, install_plan.payload_hash); |
| 186 | EXPECT_TRUE(install_plan.hash_checks_mandatory); |
| 187 | } |
| 188 | |
| 189 | TEST_F(OmahaResponseHandlerActionTest, HashChecksForHttpsTest) { |
| 190 | OmahaResponse in; |
| 191 | in.update_exists = true; |
| 192 | in.display_version = "a.b.c.d"; |
| 193 | in.codebase = "https://test.should.not/need/hash.checks.signed"; |
| 194 | in.more_info_url = "http://more/info"; |
| 195 | in.hash = "HASHj+"; |
| 196 | in.size = 12; |
| 197 | InstallPlan install_plan; |
| 198 | EXPECT_TRUE(DoTest(in, "/dev/sda5", &install_plan)); |
| 199 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 200 | EXPECT_EQ(in.hash, install_plan.payload_hash); |
| 201 | EXPECT_FALSE(install_plan.hash_checks_mandatory); |
| 202 | } |
| 203 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 204 | } // namespace chromeos_update_engine |