blob: ca6627ef384433c894b9943f96b3b52fba6a87af [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070017#include "update_engine/postinstall_runner_action.h"
18
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080022
Ben Chan02f7c1d2014-10-18 15:18:02 -070023#include <memory>
adlr@google.com3defe6a2009-12-04 20:57:17 +000024#include <string>
25#include <vector>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080026
Ben Chan06c76a42014-09-05 08:21:06 -070027#include <base/files/file_util.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070028#include <base/message_loop/message_loop.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070029#include <base/strings/string_util.h>
30#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070031#include <brillo/bind_lambda.h>
32#include <brillo/message_loops/base_message_loop.h>
33#include <brillo/message_loops/message_loop_utils.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000034#include <gtest/gtest.h>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080035
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070036#include "update_engine/constants.h"
Alex Deymo31d95ac2015-09-17 11:56:18 -070037#include "update_engine/fake_system_state.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000038#include "update_engine/test_utils.h"
39#include "update_engine/utils.h"
40
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070041using brillo::MessageLoop;
Alex Deymo10875d92014-11-10 21:52:57 -080042using chromeos_update_engine::test_utils::System;
43using chromeos_update_engine::test_utils::WriteFileString;
adlr@google.com3defe6a2009-12-04 20:57:17 +000044using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070045using std::unique_ptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000046using std::vector;
47
48namespace chromeos_update_engine {
49
50class PostinstallRunnerActionTest : public ::testing::Test {
Alex Deymo29b81532015-07-09 11:51:49 -070051 protected:
52 void SetUp() override {
53 loop_.SetAsCurrent();
Alex Deymob7ca0962014-10-01 17:58:07 -070054 async_signal_handler_.Init();
55 subprocess_.Init(&async_signal_handler_);
Alex Deymo29b81532015-07-09 11:51:49 -070056 }
57
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070058 // DoTest with various combinations of do_losetup, err_code and
59 // powerwash_required.
60 void DoTest(bool do_losetup, int err_code, bool powerwash_required);
Gilad Arnold30dedd82013-07-03 06:19:09 -070061
Alex Deymo31d95ac2015-09-17 11:56:18 -070062 protected:
Gilad Arnold30dedd82013-07-03 06:19:09 -070063 static const char* kImageMountPointTemplate;
Alex Deymo29b81532015-07-09 11:51:49 -070064
Alex Deymo0b3db6b2015-08-10 15:19:37 -070065 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070066 brillo::BaseMessageLoop loop_{&base_loop_};
67 brillo::AsynchronousSignalHandler async_signal_handler_;
Alex Deymo461b2592015-07-24 20:10:52 -070068 Subprocess subprocess_;
Alex Deymo31d95ac2015-09-17 11:56:18 -070069 FakeSystemState fake_system_state_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000070};
71
72class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
73 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070074 PostinstActionProcessorDelegate()
Alex Deymo29b81532015-07-09 11:51:49 -070075 : code_(ErrorCode::kError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070076 code_set_(false) {}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080077 void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070078 ErrorCode code) {
Alex Deymo29b81532015-07-09 11:51:49 -070079 MessageLoop::current()->BreakLoop();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080080 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000081 void ActionCompleted(ActionProcessor* processor,
82 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070083 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000084 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070085 code_ = code;
86 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000087 }
88 }
David Zeuthena99981f2013-04-29 13:42:47 -070089 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070090 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000091};
92
93TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070094 DoTest(true, 0, false);
95}
96
97TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070098 DoTest(true, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000099}
100
101TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700102 DoTest(false, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000103}
104
105TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700106 DoTest(true, 1, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000107}
108
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700109TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700110 DoTest(true, 3, false);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700111}
112
Don Garrett81018e02013-07-30 18:46:31 -0700113TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareROErrScriptTest) {
Don Garrett81018e02013-07-30 18:46:31 -0700114 DoTest(true, 4, false);
115}
116
Gilad Arnold30dedd82013-07-03 06:19:09 -0700117const char* PostinstallRunnerActionTest::kImageMountPointTemplate =
118 "au_destination-XXXXXX";
119
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700120void PostinstallRunnerActionTest::DoTest(
121 bool do_losetup,
122 int err_code,
123 bool powerwash_required) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000124 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
125 << "as root, tho.";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700126 // True if the post-install action is expected to succeed.
127 bool should_succeed = do_losetup && !err_code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000128
Gilad Arnold30dedd82013-07-03 06:19:09 -0700129 string orig_cwd;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000130 {
131 vector<char> buf(1000);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800132 ASSERT_EQ(buf.data(), getcwd(buf.data(), buf.size()));
133 orig_cwd = string(buf.data(), strlen(buf.data()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000134 }
135
Gilad Arnold30dedd82013-07-03 06:19:09 -0700136 // Create a unique named working directory and chdir into it.
137 string cwd;
138 ASSERT_TRUE(utils::MakeTempDirectory(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800139 "postinstall_runner_action_unittest-XXXXXX",
Gilad Arnold30dedd82013-07-03 06:19:09 -0700140 &cwd));
Alex Deymo10875d92014-11-10 21:52:57 -0800141 ASSERT_EQ(0, test_utils::Chdir(cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000142
Gilad Arnold30dedd82013-07-03 06:19:09 -0700143 // Create a 10MiB sparse file to be used as image; format it as ext2.
Alex Deymo10875d92014-11-10 21:52:57 -0800144 ASSERT_EQ(0, System(
Alex Deymo1f93d032015-03-10 18:58:32 -0700145 "dd if=/dev/zero of=image.dat seek=10485759 bs=1 count=1 "
146 "status=none"));
Alex Deymo10875d92014-11-10 21:52:57 -0800147 ASSERT_EQ(0, System("mkfs.ext2 -F image.dat"));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000148
Gilad Arnold30dedd82013-07-03 06:19:09 -0700149 // Create a uniquely named image mount point, mount the image.
150 ASSERT_EQ(0, System(string("mkdir -p ") + kStatefulPartition));
151 string mountpoint;
152 ASSERT_TRUE(utils::MakeTempDirectory(
153 string(kStatefulPartition) + "/" + kImageMountPointTemplate,
154 &mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000155 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
156
Gilad Arnold30dedd82013-07-03 06:19:09 -0700157 // Generate a fake postinst script inside the image.
158 string script = (err_code ?
Alex Vakulenko75039d72014-03-25 12:36:28 -0700159 base::StringPrintf("#!/bin/bash\nexit %d", err_code) :
160 base::StringPrintf(
161 "#!/bin/bash\n"
162 "mount | grep au_postint_mount | grep ext2\n"
163 "if [ $? -eq 0 ]; then\n"
164 " touch %s/postinst_called\n"
165 "fi\n",
166 cwd.c_str()));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700167 const string script_file_name = mountpoint + "/postinst";
168 ASSERT_TRUE(WriteFileString(script_file_name, script));
169 ASSERT_EQ(0, System(string("chmod a+x ") + script_file_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000170
Gilad Arnold30dedd82013-07-03 06:19:09 -0700171 // Unmount image; do not remove the uniquely named directory as it will be
172 // reused during the test.
Ben Chan77a1eba2012-10-07 22:54:55 -0700173 ASSERT_TRUE(utils::UnmountFilesystem(mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000174
adlr@google.com3defe6a2009-12-04 20:57:17 +0000175 // get a loop device we can use for the install device
Don Garrett58e8b1f2012-01-31 16:38:16 -0800176 string dev = "/dev/null";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000177
Alex Deymo10875d92014-11-10 21:52:57 -0800178 unique_ptr<test_utils::ScopedLoopbackDeviceBinder> loop_releaser;
Darin Petkov56dad722011-03-03 16:03:56 -0800179 if (do_losetup) {
Alex Deymo10875d92014-11-10 21:52:57 -0800180 loop_releaser.reset(new test_utils::ScopedLoopbackDeviceBinder(
181 cwd + "/image.dat", &dev));
Darin Petkov56dad722011-03-03 16:03:56 -0800182 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000183
Gilad Arnold30dedd82013-07-03 06:19:09 -0700184 // We use a test-specific powerwash marker file, to avoid race conditions.
185 string powerwash_marker_file = mountpoint + "/factory_install_reset";
186 LOG(INFO) << ">>> powerwash_marker_file=" << powerwash_marker_file;
187
adlr@google.com3defe6a2009-12-04 20:57:17 +0000188 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700189 ObjectFeederAction<InstallPlan> feeder_action;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700190 InstallPlan::Partition part;
191 part.name = "part";
192 part.target_path = dev;
193 part.run_postinstall = true;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700194 InstallPlan install_plan;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700195 install_plan.partitions = {part};
Chris Sosa000ecb32014-04-23 12:21:18 -0700196 install_plan.download_url = "http://devserver:8080/update";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700197 install_plan.powerwash_required = powerwash_required;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700198 feeder_action.set_obj(install_plan);
Alex Deymo31d95ac2015-09-17 11:56:18 -0700199 PostinstallRunnerAction runner_action(&fake_system_state_,
200 powerwash_marker_file.c_str());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000201 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700202 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000203 BondActions(&runner_action, &collector_action);
204 PostinstActionProcessorDelegate delegate;
205 processor.EnqueueAction(&feeder_action);
206 processor.EnqueueAction(&runner_action);
207 processor.EnqueueAction(&collector_action);
208 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800209
Alex Deymo29b81532015-07-09 11:51:49 -0700210 loop_.PostTask(FROM_HERE,
211 base::Bind([&processor] { processor.StartProcessing(); }));
212 loop_.Run();
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800213 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000214
Darin Petkovc1a8b422010-07-19 11:34:49 -0700215 EXPECT_TRUE(delegate.code_set_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700216 EXPECT_EQ(should_succeed, delegate.code_ == ErrorCode::kSuccess);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700217 if (should_succeed)
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700218 EXPECT_TRUE(install_plan == collector_action.object());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700219
Alex Vakulenko75039d72014-03-25 12:36:28 -0700220 const base::FilePath kPowerwashMarkerPath(powerwash_marker_file);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700221 string actual_cmd;
222 if (should_succeed && powerwash_required) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700223 EXPECT_TRUE(base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700224 EXPECT_EQ(kPowerwashCommand, actual_cmd);
225 } else {
226 EXPECT_FALSE(
Alex Vakulenko75039d72014-03-25 12:36:28 -0700227 base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000228 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700229
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700230 if (err_code == 2)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700231 EXPECT_EQ(ErrorCode::kPostinstallBootedFromFirmwareB, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000232
233 struct stat stbuf;
234 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700235 if (should_succeed)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000236 ASSERT_EQ(0, rc);
237 else
238 ASSERT_LT(rc, 0);
239
Darin Petkov56dad722011-03-03 16:03:56 -0800240 if (do_losetup) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700241 loop_releaser.reset(nullptr);
Darin Petkov56dad722011-03-03 16:03:56 -0800242 }
Gilad Arnold30dedd82013-07-03 06:19:09 -0700243
244 // Remove unique stateful directory.
245 ASSERT_EQ(0, System(string("rm -fr ") + mountpoint));
246
247 // Remove the temporary work directory.
Alex Deymo10875d92014-11-10 21:52:57 -0800248 ASSERT_EQ(0, test_utils::Chdir(orig_cwd));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700249 ASSERT_EQ(0, System(string("rm -fr ") + cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000250}
251
252// Death tests don't seem to be working on Hardy
253TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
254 ASSERT_EQ(0, getuid());
Alex Deymo31d95ac2015-09-17 11:56:18 -0700255 PostinstallRunnerAction runner_action(&fake_system_state_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000256 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
257 "postinstall_runner_action.h:.*] Check failed");
258}
259
260} // namespace chromeos_update_engine