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) { |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame^] | 90 | ScopedPathUnlinker deadline_unlinker( |
| 91 | OmahaResponseHandlerAction::kDeadlineFile); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 92 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 93 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 94 | in.update_exists = true; |
| 95 | in.display_version = "a.b.c.d"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 96 | in.codebase = "http://foo/the_update_a.b.c.d.tgz"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 97 | in.more_info_url = "http://more/info"; |
| 98 | in.hash = "HASH+"; |
| 99 | in.size = 12; |
| 100 | in.needs_admin = true; |
| 101 | in.prompt = false; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 102 | in.is_delta = 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; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -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_TRUE(install_plan.is_full_update); |
| 107 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 108 | EXPECT_EQ(in.hash, install_plan.download_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 109 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame^] | 110 | string deadline; |
| 111 | EXPECT_TRUE(utils::ReadFileToString( |
| 112 | OmahaResponseHandlerAction::kDeadlineFile, |
| 113 | &deadline)); |
| 114 | EXPECT_EQ("20101020", deadline); |
| 115 | struct stat deadline_stat; |
| 116 | EXPECT_EQ(0, stat(OmahaResponseHandlerAction::kDeadlineFile, |
| 117 | &deadline_stat)); |
| 118 | EXPECT_EQ(S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, |
| 119 | deadline_stat.st_mode); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 120 | } |
| 121 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 122 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 123 | in.update_exists = true; |
| 124 | in.display_version = "a.b.c.d"; |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 125 | in.codebase = "http://foo/the_update_a.b.c.d.tgz"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 126 | in.more_info_url = "http://more/info"; |
| 127 | in.hash = "HASHj+"; |
| 128 | in.size = 12; |
| 129 | in.needs_admin = true; |
| 130 | in.prompt = true; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 131 | in.is_delta = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 132 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 133 | EXPECT_TRUE(DoTest(in, "/dev/sda5", &install_plan)); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 134 | EXPECT_FALSE(install_plan.is_full_update); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 135 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 136 | EXPECT_EQ(in.hash, install_plan.download_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 137 | EXPECT_EQ("/dev/sda3", install_plan.install_path); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame^] | 138 | string deadline; |
| 139 | EXPECT_TRUE(utils::ReadFileToString( |
| 140 | OmahaResponseHandlerAction::kDeadlineFile, |
| 141 | &deadline) && deadline.empty()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 142 | } |
| 143 | { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 144 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 145 | in.update_exists = true; |
| 146 | in.display_version = "a.b.c.d"; |
| 147 | in.codebase = kLongName; |
| 148 | in.more_info_url = "http://more/info"; |
| 149 | in.hash = "HASHj+"; |
| 150 | in.size = 12; |
| 151 | in.needs_admin = true; |
| 152 | in.prompt = true; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 153 | in.is_delta = false; |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame^] | 154 | in.deadline = "some-deadline"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 155 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 156 | EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan)); |
Andrew de los Reyes | 63b96d7 | 2010-05-10 13:08:54 -0700 | [diff] [blame] | 157 | EXPECT_TRUE(install_plan.is_full_update); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 158 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 159 | EXPECT_EQ(in.hash, install_plan.download_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 160 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame^] | 161 | string deadline; |
| 162 | EXPECT_TRUE(utils::ReadFileToString( |
| 163 | OmahaResponseHandlerAction::kDeadlineFile, |
| 164 | &deadline)); |
| 165 | EXPECT_EQ("some-deadline", deadline); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 170 | OmahaResponse in; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 171 | in.update_exists = false; |
| 172 | InstallPlan install_plan; |
| 173 | EXPECT_FALSE(DoTest(in, "/dev/sda1", &install_plan)); |
| 174 | EXPECT_FALSE(install_plan.is_full_update); |
| 175 | EXPECT_EQ("", install_plan.download_url); |
| 176 | EXPECT_EQ("", install_plan.download_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 177 | EXPECT_EQ("", install_plan.install_path); |
| 178 | } |
| 179 | |
| 180 | } // namespace chromeos_update_engine |