blob: ff6412858398d834596f34db1ed3ac1952d6f233 [file] [log] [blame]
Mike Frysinger8155d082012-04-06 15:23:18 -04001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Jay Srinivasan1c0fe792013-03-28 16:45:25 -07005#include "update_engine/postinstall_runner_action.h"
6
adlr@google.com3defe6a2009-12-04 20:57:17 +00007#include <sys/stat.h>
8#include <sys/types.h>
9#include <unistd.h>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080010
Ben Chan02f7c1d2014-10-18 15:18:02 -070011#include <memory>
adlr@google.com3defe6a2009-12-04 20:57:17 +000012#include <string>
13#include <vector>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080014
Ben Chan06c76a42014-09-05 08:21:06 -070015#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070016#include <base/strings/string_util.h>
17#include <base/strings/stringprintf.h>
Alex Deymo29b81532015-07-09 11:51:49 -070018#include <chromeos/bind_lambda.h>
19#include <chromeos/message_loops/glib_message_loop.h>
20#include <chromeos/message_loops/message_loop_utils.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <gtest/gtest.h>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080022
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070023#include "update_engine/constants.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000024#include "update_engine/test_utils.h"
25#include "update_engine/utils.h"
26
Alex Deymo29b81532015-07-09 11:51:49 -070027using chromeos::MessageLoop;
Alex Deymo10875d92014-11-10 21:52:57 -080028using chromeos_update_engine::test_utils::System;
29using chromeos_update_engine::test_utils::WriteFileString;
adlr@google.com3defe6a2009-12-04 20:57:17 +000030using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070031using std::unique_ptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000032using std::vector;
33
34namespace chromeos_update_engine {
35
36class PostinstallRunnerActionTest : public ::testing::Test {
Alex Deymo29b81532015-07-09 11:51:49 -070037 protected:
38 void SetUp() override {
39 loop_.SetAsCurrent();
Alex Deymo461b2592015-07-24 20:10:52 -070040 subprocess_.Init();
Alex Deymo29b81532015-07-09 11:51:49 -070041 }
42
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070043 // DoTest with various combinations of do_losetup, err_code and
44 // powerwash_required.
45 void DoTest(bool do_losetup, int err_code, bool powerwash_required);
Gilad Arnold30dedd82013-07-03 06:19:09 -070046
47 private:
48 static const char* kImageMountPointTemplate;
Alex Deymo29b81532015-07-09 11:51:49 -070049
50 chromeos::GlibMessageLoop loop_;
Alex Deymo461b2592015-07-24 20:10:52 -070051 Subprocess subprocess_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000052};
53
54class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
55 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070056 PostinstActionProcessorDelegate()
Alex Deymo29b81532015-07-09 11:51:49 -070057 : code_(ErrorCode::kError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070058 code_set_(false) {}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080059 void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070060 ErrorCode code) {
Alex Deymo29b81532015-07-09 11:51:49 -070061 MessageLoop::current()->BreakLoop();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080062 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000063 void ActionCompleted(ActionProcessor* processor,
64 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070065 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000066 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070067 code_ = code;
68 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000069 }
70 }
David Zeuthena99981f2013-04-29 13:42:47 -070071 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070072 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000073};
74
75TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070076 DoTest(true, 0, false);
77}
78
79TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070080 DoTest(true, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000081}
82
83TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070084 DoTest(false, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000085}
86
87TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070088 DoTest(true, 1, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +000089}
90
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070091TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070092 DoTest(true, 3, false);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070093}
94
Don Garrett81018e02013-07-30 18:46:31 -070095TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareROErrScriptTest) {
Don Garrett81018e02013-07-30 18:46:31 -070096 DoTest(true, 4, false);
97}
98
Gilad Arnold30dedd82013-07-03 06:19:09 -070099const char* PostinstallRunnerActionTest::kImageMountPointTemplate =
100 "au_destination-XXXXXX";
101
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700102void PostinstallRunnerActionTest::DoTest(
103 bool do_losetup,
104 int err_code,
105 bool powerwash_required) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000106 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
107 << "as root, tho.";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700108 // True if the post-install action is expected to succeed.
109 bool should_succeed = do_losetup && !err_code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000110
Gilad Arnold30dedd82013-07-03 06:19:09 -0700111 string orig_cwd;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000112 {
113 vector<char> buf(1000);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800114 ASSERT_EQ(buf.data(), getcwd(buf.data(), buf.size()));
115 orig_cwd = string(buf.data(), strlen(buf.data()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000116 }
117
Gilad Arnold30dedd82013-07-03 06:19:09 -0700118 // Create a unique named working directory and chdir into it.
119 string cwd;
120 ASSERT_TRUE(utils::MakeTempDirectory(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800121 "postinstall_runner_action_unittest-XXXXXX",
Gilad Arnold30dedd82013-07-03 06:19:09 -0700122 &cwd));
Alex Deymo10875d92014-11-10 21:52:57 -0800123 ASSERT_EQ(0, test_utils::Chdir(cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000124
Gilad Arnold30dedd82013-07-03 06:19:09 -0700125 // Create a 10MiB sparse file to be used as image; format it as ext2.
Alex Deymo10875d92014-11-10 21:52:57 -0800126 ASSERT_EQ(0, System(
Alex Deymo1f93d032015-03-10 18:58:32 -0700127 "dd if=/dev/zero of=image.dat seek=10485759 bs=1 count=1 "
128 "status=none"));
Alex Deymo10875d92014-11-10 21:52:57 -0800129 ASSERT_EQ(0, System("mkfs.ext2 -F image.dat"));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000130
Gilad Arnold30dedd82013-07-03 06:19:09 -0700131 // Create a uniquely named image mount point, mount the image.
132 ASSERT_EQ(0, System(string("mkdir -p ") + kStatefulPartition));
133 string mountpoint;
134 ASSERT_TRUE(utils::MakeTempDirectory(
135 string(kStatefulPartition) + "/" + kImageMountPointTemplate,
136 &mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000137 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
138
Gilad Arnold30dedd82013-07-03 06:19:09 -0700139 // Generate a fake postinst script inside the image.
140 string script = (err_code ?
Alex Vakulenko75039d72014-03-25 12:36:28 -0700141 base::StringPrintf("#!/bin/bash\nexit %d", err_code) :
142 base::StringPrintf(
143 "#!/bin/bash\n"
144 "mount | grep au_postint_mount | grep ext2\n"
145 "if [ $? -eq 0 ]; then\n"
146 " touch %s/postinst_called\n"
147 "fi\n",
148 cwd.c_str()));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700149 const string script_file_name = mountpoint + "/postinst";
150 ASSERT_TRUE(WriteFileString(script_file_name, script));
151 ASSERT_EQ(0, System(string("chmod a+x ") + script_file_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000152
Gilad Arnold30dedd82013-07-03 06:19:09 -0700153 // Unmount image; do not remove the uniquely named directory as it will be
154 // reused during the test.
Ben Chan77a1eba2012-10-07 22:54:55 -0700155 ASSERT_TRUE(utils::UnmountFilesystem(mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000156
adlr@google.com3defe6a2009-12-04 20:57:17 +0000157 // get a loop device we can use for the install device
Don Garrett58e8b1f2012-01-31 16:38:16 -0800158 string dev = "/dev/null";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000159
Alex Deymo10875d92014-11-10 21:52:57 -0800160 unique_ptr<test_utils::ScopedLoopbackDeviceBinder> loop_releaser;
Darin Petkov56dad722011-03-03 16:03:56 -0800161 if (do_losetup) {
Alex Deymo10875d92014-11-10 21:52:57 -0800162 loop_releaser.reset(new test_utils::ScopedLoopbackDeviceBinder(
163 cwd + "/image.dat", &dev));
Darin Petkov56dad722011-03-03 16:03:56 -0800164 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000165
Gilad Arnold30dedd82013-07-03 06:19:09 -0700166 // We use a test-specific powerwash marker file, to avoid race conditions.
167 string powerwash_marker_file = mountpoint + "/factory_install_reset";
168 LOG(INFO) << ">>> powerwash_marker_file=" << powerwash_marker_file;
169
adlr@google.com3defe6a2009-12-04 20:57:17 +0000170 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700171 ObjectFeederAction<InstallPlan> feeder_action;
172 InstallPlan install_plan;
173 install_plan.install_path = dev;
Chris Sosa000ecb32014-04-23 12:21:18 -0700174 install_plan.download_url = "http://devserver:8080/update";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700175 install_plan.powerwash_required = powerwash_required;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700176 feeder_action.set_obj(install_plan);
Gilad Arnold30dedd82013-07-03 06:19:09 -0700177 PostinstallRunnerAction runner_action(powerwash_marker_file.c_str());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000178 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700179 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000180 BondActions(&runner_action, &collector_action);
181 PostinstActionProcessorDelegate delegate;
182 processor.EnqueueAction(&feeder_action);
183 processor.EnqueueAction(&runner_action);
184 processor.EnqueueAction(&collector_action);
185 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800186
Alex Deymo29b81532015-07-09 11:51:49 -0700187 loop_.PostTask(FROM_HERE,
188 base::Bind([&processor] { processor.StartProcessing(); }));
189 loop_.Run();
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800190 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000191
Darin Petkovc1a8b422010-07-19 11:34:49 -0700192 EXPECT_TRUE(delegate.code_set_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700193 EXPECT_EQ(should_succeed, delegate.code_ == ErrorCode::kSuccess);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700194 EXPECT_EQ(should_succeed, !collector_action.object().install_path.empty());
195 if (should_succeed)
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700196 EXPECT_TRUE(install_plan == collector_action.object());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700197
Alex Vakulenko75039d72014-03-25 12:36:28 -0700198 const base::FilePath kPowerwashMarkerPath(powerwash_marker_file);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700199 string actual_cmd;
200 if (should_succeed && powerwash_required) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700201 EXPECT_TRUE(base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700202 EXPECT_EQ(kPowerwashCommand, actual_cmd);
203 } else {
204 EXPECT_FALSE(
Alex Vakulenko75039d72014-03-25 12:36:28 -0700205 base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000206 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700207
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700208 if (err_code == 2)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700209 EXPECT_EQ(ErrorCode::kPostinstallBootedFromFirmwareB, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000210
211 struct stat stbuf;
212 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700213 if (should_succeed)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000214 ASSERT_EQ(0, rc);
215 else
216 ASSERT_LT(rc, 0);
217
Darin Petkov56dad722011-03-03 16:03:56 -0800218 if (do_losetup) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700219 loop_releaser.reset(nullptr);
Darin Petkov56dad722011-03-03 16:03:56 -0800220 }
Gilad Arnold30dedd82013-07-03 06:19:09 -0700221
222 // Remove unique stateful directory.
223 ASSERT_EQ(0, System(string("rm -fr ") + mountpoint));
224
225 // Remove the temporary work directory.
Alex Deymo10875d92014-11-10 21:52:57 -0800226 ASSERT_EQ(0, test_utils::Chdir(orig_cwd));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700227 ASSERT_EQ(0, System(string("rm -fr ") + cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000228}
229
230// Death tests don't seem to be working on Hardy
231TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
232 ASSERT_EQ(0, getuid());
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800233 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000234 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
235 "postinstall_runner_action.h:.*] Check failed");
236}
237
238} // namespace chromeos_update_engine