blob: 0460c6cd551bd5bb1d8a7073cf4d7f066be3a344 [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>
adlr@google.com3defe6a2009-12-04 20:57:17 +000018#include <gtest/gtest.h>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080019
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070020#include "update_engine/constants.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include "update_engine/test_utils.h"
22#include "update_engine/utils.h"
23
Alex Deymo10875d92014-11-10 21:52:57 -080024using chromeos_update_engine::test_utils::System;
25using chromeos_update_engine::test_utils::WriteFileString;
adlr@google.com3defe6a2009-12-04 20:57:17 +000026using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070027using std::unique_ptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000028using std::vector;
29
30namespace chromeos_update_engine {
31
Darin Petkov6f03a3b2010-11-10 14:27:14 -080032namespace {
33gboolean StartProcessorInRunLoop(gpointer data) {
34 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
35 processor->StartProcessing();
36 return FALSE;
37}
38} // namespace
39
adlr@google.com3defe6a2009-12-04 20:57:17 +000040class PostinstallRunnerActionTest : public ::testing::Test {
41 public:
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070042 // DoTest with various combinations of do_losetup, err_code and
43 // powerwash_required.
44 void DoTest(bool do_losetup, int err_code, bool powerwash_required);
Gilad Arnold30dedd82013-07-03 06:19:09 -070045
46 private:
47 static const char* kImageMountPointTemplate;
adlr@google.com3defe6a2009-12-04 20:57:17 +000048};
49
50class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
51 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070052 PostinstActionProcessorDelegate()
Alex Vakulenko88b591f2014-08-28 16:48:57 -070053 : loop_(nullptr),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070054 code_(ErrorCode::kError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070055 code_set_(false) {}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080056 void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070057 ErrorCode code) {
Darin Petkov6f03a3b2010-11-10 14:27:14 -080058 ASSERT_TRUE(loop_);
59 g_main_loop_quit(loop_);
60 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000061 void ActionCompleted(ActionProcessor* processor,
62 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070063 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000064 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070065 code_ = code;
66 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000067 }
68 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -080069 GMainLoop* loop_;
David Zeuthena99981f2013-04-29 13:42:47 -070070 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070071 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000072};
73
74TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
75 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070076 DoTest(true, 0, false);
77}
78
79TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) {
80 ASSERT_EQ(0, getuid());
81 DoTest(true, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000082}
83
84TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
85 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070086 DoTest(false, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000087}
88
89TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
90 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070091 DoTest(true, 1, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +000092}
93
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070094TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) {
95 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070096 DoTest(true, 3, false);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070097}
98
Don Garrett81018e02013-07-30 18:46:31 -070099TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareROErrScriptTest) {
100 ASSERT_EQ(0, getuid());
101 DoTest(true, 4, false);
102}
103
Gilad Arnold30dedd82013-07-03 06:19:09 -0700104const char* PostinstallRunnerActionTest::kImageMountPointTemplate =
105 "au_destination-XXXXXX";
106
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700107void PostinstallRunnerActionTest::DoTest(
108 bool do_losetup,
109 int err_code,
110 bool powerwash_required) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000111 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
112 << "as root, tho.";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700113 // True if the post-install action is expected to succeed.
114 bool should_succeed = do_losetup && !err_code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000115
Gilad Arnold30dedd82013-07-03 06:19:09 -0700116 string orig_cwd;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000117 {
118 vector<char> buf(1000);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800119 ASSERT_EQ(buf.data(), getcwd(buf.data(), buf.size()));
120 orig_cwd = string(buf.data(), strlen(buf.data()));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000121 }
122
Gilad Arnold30dedd82013-07-03 06:19:09 -0700123 // Create a unique named working directory and chdir into it.
124 string cwd;
125 ASSERT_TRUE(utils::MakeTempDirectory(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800126 "postinstall_runner_action_unittest-XXXXXX",
Gilad Arnold30dedd82013-07-03 06:19:09 -0700127 &cwd));
Alex Deymo10875d92014-11-10 21:52:57 -0800128 ASSERT_EQ(0, test_utils::Chdir(cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000129
Gilad Arnold30dedd82013-07-03 06:19:09 -0700130 // Create a 10MiB sparse file to be used as image; format it as ext2.
Alex Deymo10875d92014-11-10 21:52:57 -0800131 ASSERT_EQ(0, System(
Alex Deymo1f93d032015-03-10 18:58:32 -0700132 "dd if=/dev/zero of=image.dat seek=10485759 bs=1 count=1 "
133 "status=none"));
Alex Deymo10875d92014-11-10 21:52:57 -0800134 ASSERT_EQ(0, System("mkfs.ext2 -F image.dat"));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000135
Gilad Arnold30dedd82013-07-03 06:19:09 -0700136 // Create a uniquely named image mount point, mount the image.
137 ASSERT_EQ(0, System(string("mkdir -p ") + kStatefulPartition));
138 string mountpoint;
139 ASSERT_TRUE(utils::MakeTempDirectory(
140 string(kStatefulPartition) + "/" + kImageMountPointTemplate,
141 &mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000142 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
143
Gilad Arnold30dedd82013-07-03 06:19:09 -0700144 // Generate a fake postinst script inside the image.
145 string script = (err_code ?
Alex Vakulenko75039d72014-03-25 12:36:28 -0700146 base::StringPrintf("#!/bin/bash\nexit %d", err_code) :
147 base::StringPrintf(
148 "#!/bin/bash\n"
149 "mount | grep au_postint_mount | grep ext2\n"
150 "if [ $? -eq 0 ]; then\n"
151 " touch %s/postinst_called\n"
152 "fi\n",
153 cwd.c_str()));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700154 const string script_file_name = mountpoint + "/postinst";
155 ASSERT_TRUE(WriteFileString(script_file_name, script));
156 ASSERT_EQ(0, System(string("chmod a+x ") + script_file_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000157
Gilad Arnold30dedd82013-07-03 06:19:09 -0700158 // Unmount image; do not remove the uniquely named directory as it will be
159 // reused during the test.
Ben Chan77a1eba2012-10-07 22:54:55 -0700160 ASSERT_TRUE(utils::UnmountFilesystem(mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000161
adlr@google.com3defe6a2009-12-04 20:57:17 +0000162 // get a loop device we can use for the install device
Don Garrett58e8b1f2012-01-31 16:38:16 -0800163 string dev = "/dev/null";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000164
Alex Deymo10875d92014-11-10 21:52:57 -0800165 unique_ptr<test_utils::ScopedLoopbackDeviceBinder> loop_releaser;
Darin Petkov56dad722011-03-03 16:03:56 -0800166 if (do_losetup) {
Alex Deymo10875d92014-11-10 21:52:57 -0800167 loop_releaser.reset(new test_utils::ScopedLoopbackDeviceBinder(
168 cwd + "/image.dat", &dev));
Darin Petkov56dad722011-03-03 16:03:56 -0800169 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000170
Gilad Arnold30dedd82013-07-03 06:19:09 -0700171 // We use a test-specific powerwash marker file, to avoid race conditions.
172 string powerwash_marker_file = mountpoint + "/factory_install_reset";
173 LOG(INFO) << ">>> powerwash_marker_file=" << powerwash_marker_file;
174
adlr@google.com3defe6a2009-12-04 20:57:17 +0000175 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700176 ObjectFeederAction<InstallPlan> feeder_action;
177 InstallPlan install_plan;
178 install_plan.install_path = dev;
Chris Sosa000ecb32014-04-23 12:21:18 -0700179 install_plan.download_url = "http://devserver:8080/update";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700180 install_plan.powerwash_required = powerwash_required;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700181 feeder_action.set_obj(install_plan);
Gilad Arnold30dedd82013-07-03 06:19:09 -0700182 PostinstallRunnerAction runner_action(powerwash_marker_file.c_str());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000183 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700184 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000185 BondActions(&runner_action, &collector_action);
186 PostinstActionProcessorDelegate delegate;
187 processor.EnqueueAction(&feeder_action);
188 processor.EnqueueAction(&runner_action);
189 processor.EnqueueAction(&collector_action);
190 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800191
192 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
193 delegate.loop_ = loop;
194 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
195 g_main_loop_run(loop);
196 g_main_loop_unref(loop);
197 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000198
Darin Petkovc1a8b422010-07-19 11:34:49 -0700199 EXPECT_TRUE(delegate.code_set_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700200 EXPECT_EQ(should_succeed, delegate.code_ == ErrorCode::kSuccess);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700201 EXPECT_EQ(should_succeed, !collector_action.object().install_path.empty());
202 if (should_succeed)
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700203 EXPECT_TRUE(install_plan == collector_action.object());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700204
Alex Vakulenko75039d72014-03-25 12:36:28 -0700205 const base::FilePath kPowerwashMarkerPath(powerwash_marker_file);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700206 string actual_cmd;
207 if (should_succeed && powerwash_required) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700208 EXPECT_TRUE(base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700209 EXPECT_EQ(kPowerwashCommand, actual_cmd);
210 } else {
211 EXPECT_FALSE(
Alex Vakulenko75039d72014-03-25 12:36:28 -0700212 base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000213 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700214
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700215 if (err_code == 2)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700216 EXPECT_EQ(ErrorCode::kPostinstallBootedFromFirmwareB, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000217
218 struct stat stbuf;
219 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700220 if (should_succeed)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000221 ASSERT_EQ(0, rc);
222 else
223 ASSERT_LT(rc, 0);
224
Darin Petkov56dad722011-03-03 16:03:56 -0800225 if (do_losetup) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700226 loop_releaser.reset(nullptr);
Darin Petkov56dad722011-03-03 16:03:56 -0800227 }
Gilad Arnold30dedd82013-07-03 06:19:09 -0700228
229 // Remove unique stateful directory.
230 ASSERT_EQ(0, System(string("rm -fr ") + mountpoint));
231
232 // Remove the temporary work directory.
Alex Deymo10875d92014-11-10 21:52:57 -0800233 ASSERT_EQ(0, test_utils::Chdir(orig_cwd));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700234 ASSERT_EQ(0, System(string("rm -fr ") + cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000235}
236
237// Death tests don't seem to be working on Hardy
238TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
239 ASSERT_EQ(0, getuid());
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800240 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000241 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
242 "postinstall_runner_action.h:.*] Check failed");
243}
244
245} // namespace chromeos_update_engine