blob: fd03a8fca0305b923d9aa079883c2b42c485fca7 [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 Deymo29b81532015-07-09 11:51:49 -070031#include <chromeos/bind_lambda.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070032#include <chromeos/message_loops/base_message_loop.h>
Alex Deymo29b81532015-07-09 11:51:49 -070033#include <chromeos/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"
adlr@google.com3defe6a2009-12-04 20:57:17 +000037#include "update_engine/test_utils.h"
38#include "update_engine/utils.h"
39
Alex Deymo29b81532015-07-09 11:51:49 -070040using chromeos::MessageLoop;
Alex Deymo10875d92014-11-10 21:52:57 -080041using chromeos_update_engine::test_utils::System;
42using chromeos_update_engine::test_utils::WriteFileString;
adlr@google.com3defe6a2009-12-04 20:57:17 +000043using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070044using std::unique_ptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000045using std::vector;
46
47namespace chromeos_update_engine {
48
49class PostinstallRunnerActionTest : public ::testing::Test {
Alex Deymo29b81532015-07-09 11:51:49 -070050 protected:
51 void SetUp() override {
52 loop_.SetAsCurrent();
Alex Deymob7ca0962014-10-01 17:58:07 -070053 async_signal_handler_.Init();
54 subprocess_.Init(&async_signal_handler_);
Alex Deymo29b81532015-07-09 11:51:49 -070055 }
56
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070057 // DoTest with various combinations of do_losetup, err_code and
58 // powerwash_required.
59 void DoTest(bool do_losetup, int err_code, bool powerwash_required);
Gilad Arnold30dedd82013-07-03 06:19:09 -070060
61 private:
62 static const char* kImageMountPointTemplate;
Alex Deymo29b81532015-07-09 11:51:49 -070063
Alex Deymo0b3db6b2015-08-10 15:19:37 -070064 base::MessageLoopForIO base_loop_;
65 chromeos::BaseMessageLoop loop_{&base_loop_};
Alex Deymob7ca0962014-10-01 17:58:07 -070066 chromeos::AsynchronousSignalHandler async_signal_handler_;
Alex Deymo461b2592015-07-24 20:10:52 -070067 Subprocess subprocess_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000068};
69
70class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
71 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070072 PostinstActionProcessorDelegate()
Alex Deymo29b81532015-07-09 11:51:49 -070073 : code_(ErrorCode::kError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070074 code_set_(false) {}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080075 void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070076 ErrorCode code) {
Alex Deymo29b81532015-07-09 11:51:49 -070077 MessageLoop::current()->BreakLoop();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080078 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000079 void ActionCompleted(ActionProcessor* processor,
80 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070081 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000082 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070083 code_ = code;
84 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000085 }
86 }
David Zeuthena99981f2013-04-29 13:42:47 -070087 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070088 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000089};
90
91TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070092 DoTest(true, 0, false);
93}
94
95TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070096 DoTest(true, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000097}
98
99TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700100 DoTest(false, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000101}
102
103TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700104 DoTest(true, 1, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000105}
106
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700107TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700108 DoTest(true, 3, false);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700109}
110
Don Garrett81018e02013-07-30 18:46:31 -0700111TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareROErrScriptTest) {
Don Garrett81018e02013-07-30 18:46:31 -0700112 DoTest(true, 4, false);
113}
114
Gilad Arnold30dedd82013-07-03 06:19:09 -0700115const char* PostinstallRunnerActionTest::kImageMountPointTemplate =
116 "au_destination-XXXXXX";
117
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700118void PostinstallRunnerActionTest::DoTest(
119 bool do_losetup,
120 int err_code,
121 bool powerwash_required) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000122 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
123 << "as root, tho.";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700124 // True if the post-install action is expected to succeed.
125 bool should_succeed = do_losetup && !err_code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000126
Gilad Arnold30dedd82013-07-03 06:19:09 -0700127 string orig_cwd;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000128 {
129 vector<char> buf(1000);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800130 ASSERT_EQ(buf.data(), getcwd(buf.data(), buf.size()));
131 orig_cwd = string(buf.data(), strlen(buf.data()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000132 }
133
Gilad Arnold30dedd82013-07-03 06:19:09 -0700134 // Create a unique named working directory and chdir into it.
135 string cwd;
136 ASSERT_TRUE(utils::MakeTempDirectory(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800137 "postinstall_runner_action_unittest-XXXXXX",
Gilad Arnold30dedd82013-07-03 06:19:09 -0700138 &cwd));
Alex Deymo10875d92014-11-10 21:52:57 -0800139 ASSERT_EQ(0, test_utils::Chdir(cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000140
Gilad Arnold30dedd82013-07-03 06:19:09 -0700141 // Create a 10MiB sparse file to be used as image; format it as ext2.
Alex Deymo10875d92014-11-10 21:52:57 -0800142 ASSERT_EQ(0, System(
Alex Deymo1f93d032015-03-10 18:58:32 -0700143 "dd if=/dev/zero of=image.dat seek=10485759 bs=1 count=1 "
144 "status=none"));
Alex Deymo10875d92014-11-10 21:52:57 -0800145 ASSERT_EQ(0, System("mkfs.ext2 -F image.dat"));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000146
Gilad Arnold30dedd82013-07-03 06:19:09 -0700147 // Create a uniquely named image mount point, mount the image.
148 ASSERT_EQ(0, System(string("mkdir -p ") + kStatefulPartition));
149 string mountpoint;
150 ASSERT_TRUE(utils::MakeTempDirectory(
151 string(kStatefulPartition) + "/" + kImageMountPointTemplate,
152 &mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000153 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
154
Gilad Arnold30dedd82013-07-03 06:19:09 -0700155 // Generate a fake postinst script inside the image.
156 string script = (err_code ?
Alex Vakulenko75039d72014-03-25 12:36:28 -0700157 base::StringPrintf("#!/bin/bash\nexit %d", err_code) :
158 base::StringPrintf(
159 "#!/bin/bash\n"
160 "mount | grep au_postint_mount | grep ext2\n"
161 "if [ $? -eq 0 ]; then\n"
162 " touch %s/postinst_called\n"
163 "fi\n",
164 cwd.c_str()));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700165 const string script_file_name = mountpoint + "/postinst";
166 ASSERT_TRUE(WriteFileString(script_file_name, script));
167 ASSERT_EQ(0, System(string("chmod a+x ") + script_file_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000168
Gilad Arnold30dedd82013-07-03 06:19:09 -0700169 // Unmount image; do not remove the uniquely named directory as it will be
170 // reused during the test.
Ben Chan77a1eba2012-10-07 22:54:55 -0700171 ASSERT_TRUE(utils::UnmountFilesystem(mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000172
adlr@google.com3defe6a2009-12-04 20:57:17 +0000173 // get a loop device we can use for the install device
Don Garrett58e8b1f2012-01-31 16:38:16 -0800174 string dev = "/dev/null";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000175
Alex Deymo10875d92014-11-10 21:52:57 -0800176 unique_ptr<test_utils::ScopedLoopbackDeviceBinder> loop_releaser;
Darin Petkov56dad722011-03-03 16:03:56 -0800177 if (do_losetup) {
Alex Deymo10875d92014-11-10 21:52:57 -0800178 loop_releaser.reset(new test_utils::ScopedLoopbackDeviceBinder(
179 cwd + "/image.dat", &dev));
Darin Petkov56dad722011-03-03 16:03:56 -0800180 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000181
Gilad Arnold30dedd82013-07-03 06:19:09 -0700182 // We use a test-specific powerwash marker file, to avoid race conditions.
183 string powerwash_marker_file = mountpoint + "/factory_install_reset";
184 LOG(INFO) << ">>> powerwash_marker_file=" << powerwash_marker_file;
185
adlr@google.com3defe6a2009-12-04 20:57:17 +0000186 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700187 ObjectFeederAction<InstallPlan> feeder_action;
188 InstallPlan install_plan;
189 install_plan.install_path = dev;
Chris Sosa000ecb32014-04-23 12:21:18 -0700190 install_plan.download_url = "http://devserver:8080/update";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700191 install_plan.powerwash_required = powerwash_required;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700192 feeder_action.set_obj(install_plan);
Gilad Arnold30dedd82013-07-03 06:19:09 -0700193 PostinstallRunnerAction runner_action(powerwash_marker_file.c_str());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000194 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700195 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000196 BondActions(&runner_action, &collector_action);
197 PostinstActionProcessorDelegate delegate;
198 processor.EnqueueAction(&feeder_action);
199 processor.EnqueueAction(&runner_action);
200 processor.EnqueueAction(&collector_action);
201 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800202
Alex Deymo29b81532015-07-09 11:51:49 -0700203 loop_.PostTask(FROM_HERE,
204 base::Bind([&processor] { processor.StartProcessing(); }));
205 loop_.Run();
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800206 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000207
Darin Petkovc1a8b422010-07-19 11:34:49 -0700208 EXPECT_TRUE(delegate.code_set_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700209 EXPECT_EQ(should_succeed, delegate.code_ == ErrorCode::kSuccess);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700210 EXPECT_EQ(should_succeed, !collector_action.object().install_path.empty());
211 if (should_succeed)
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700212 EXPECT_TRUE(install_plan == collector_action.object());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700213
Alex Vakulenko75039d72014-03-25 12:36:28 -0700214 const base::FilePath kPowerwashMarkerPath(powerwash_marker_file);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700215 string actual_cmd;
216 if (should_succeed && powerwash_required) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700217 EXPECT_TRUE(base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700218 EXPECT_EQ(kPowerwashCommand, actual_cmd);
219 } else {
220 EXPECT_FALSE(
Alex Vakulenko75039d72014-03-25 12:36:28 -0700221 base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000222 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700223
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700224 if (err_code == 2)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700225 EXPECT_EQ(ErrorCode::kPostinstallBootedFromFirmwareB, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000226
227 struct stat stbuf;
228 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700229 if (should_succeed)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000230 ASSERT_EQ(0, rc);
231 else
232 ASSERT_LT(rc, 0);
233
Darin Petkov56dad722011-03-03 16:03:56 -0800234 if (do_losetup) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700235 loop_releaser.reset(nullptr);
Darin Petkov56dad722011-03-03 16:03:56 -0800236 }
Gilad Arnold30dedd82013-07-03 06:19:09 -0700237
238 // Remove unique stateful directory.
239 ASSERT_EQ(0, System(string("rm -fr ") + mountpoint));
240
241 // Remove the temporary work directory.
Alex Deymo10875d92014-11-10 21:52:57 -0800242 ASSERT_EQ(0, test_utils::Chdir(orig_cwd));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700243 ASSERT_EQ(0, System(string("rm -fr ") + cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000244}
245
246// Death tests don't seem to be working on Hardy
247TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
248 ASSERT_EQ(0, getuid());
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800249 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000250 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
251 "postinstall_runner_action.h:.*] Check failed");
252}
253
254} // namespace chromeos_update_engine