blob: 9dd493d0358e9bc2a68031d220ad910f18a9b386 [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
24using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070025using std::unique_ptr;
adlr@google.com3defe6a2009-12-04 20:57:17 +000026using std::vector;
27
28namespace chromeos_update_engine {
29
Darin Petkov6f03a3b2010-11-10 14:27:14 -080030namespace {
31gboolean StartProcessorInRunLoop(gpointer data) {
32 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
33 processor->StartProcessing();
34 return FALSE;
35}
36} // namespace
37
adlr@google.com3defe6a2009-12-04 20:57:17 +000038class PostinstallRunnerActionTest : public ::testing::Test {
39 public:
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070040 // DoTest with various combinations of do_losetup, err_code and
41 // powerwash_required.
42 void DoTest(bool do_losetup, int err_code, bool powerwash_required);
Gilad Arnold30dedd82013-07-03 06:19:09 -070043
44 private:
45 static const char* kImageMountPointTemplate;
adlr@google.com3defe6a2009-12-04 20:57:17 +000046};
47
48class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
49 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070050 PostinstActionProcessorDelegate()
Alex Vakulenko88b591f2014-08-28 16:48:57 -070051 : loop_(nullptr),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070052 code_(ErrorCode::kError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070053 code_set_(false) {}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080054 void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070055 ErrorCode code) {
Darin Petkov6f03a3b2010-11-10 14:27:14 -080056 ASSERT_TRUE(loop_);
57 g_main_loop_quit(loop_);
58 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000059 void ActionCompleted(ActionProcessor* processor,
60 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070061 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000062 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070063 code_ = code;
64 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000065 }
66 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -080067 GMainLoop* loop_;
David Zeuthena99981f2013-04-29 13:42:47 -070068 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070069 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000070};
71
72TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
73 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070074 DoTest(true, 0, false);
75}
76
77TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) {
78 ASSERT_EQ(0, getuid());
79 DoTest(true, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000080}
81
82TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
83 ASSERT_EQ(0, getuid());
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) {
88 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070089 DoTest(true, 1, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +000090}
91
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070092TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) {
93 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070094 DoTest(true, 3, false);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070095}
96
Don Garrett81018e02013-07-30 18:46:31 -070097TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareROErrScriptTest) {
98 ASSERT_EQ(0, getuid());
99 DoTest(true, 4, false);
100}
101
Gilad Arnold30dedd82013-07-03 06:19:09 -0700102const char* PostinstallRunnerActionTest::kImageMountPointTemplate =
103 "au_destination-XXXXXX";
104
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700105void PostinstallRunnerActionTest::DoTest(
106 bool do_losetup,
107 int err_code,
108 bool powerwash_required) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000109 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
110 << "as root, tho.";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700111 // True if the post-install action is expected to succeed.
112 bool should_succeed = do_losetup && !err_code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000113
Gilad Arnold30dedd82013-07-03 06:19:09 -0700114 string orig_cwd;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000115 {
116 vector<char> buf(1000);
117 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size()));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700118 orig_cwd = string(&buf[0], strlen(&buf[0]));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000119 }
120
Gilad Arnold30dedd82013-07-03 06:19:09 -0700121 // Create a unique named working directory and chdir into it.
122 string cwd;
123 ASSERT_TRUE(utils::MakeTempDirectory(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800124 "postinstall_runner_action_unittest-XXXXXX",
Gilad Arnold30dedd82013-07-03 06:19:09 -0700125 &cwd));
126 ASSERT_EQ(0, Chdir(cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000127
Gilad Arnold30dedd82013-07-03 06:19:09 -0700128 // Create a 10MiB sparse file to be used as image; format it as ext2.
129 ASSERT_EQ(0, system(
130 "dd if=/dev/zero of=image.dat seek=10485759 bs=1 count=1"));
Andrew de los Reyesbfabc302011-01-31 17:23:50 -0800131 ASSERT_EQ(0, system("mkfs.ext2 -F image.dat"));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000132
Gilad Arnold30dedd82013-07-03 06:19:09 -0700133 // Create a uniquely named image mount point, mount the image.
134 ASSERT_EQ(0, System(string("mkdir -p ") + kStatefulPartition));
135 string mountpoint;
136 ASSERT_TRUE(utils::MakeTempDirectory(
137 string(kStatefulPartition) + "/" + kImageMountPointTemplate,
138 &mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000139 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
140
Gilad Arnold30dedd82013-07-03 06:19:09 -0700141 // Generate a fake postinst script inside the image.
142 string script = (err_code ?
Alex Vakulenko75039d72014-03-25 12:36:28 -0700143 base::StringPrintf("#!/bin/bash\nexit %d", err_code) :
144 base::StringPrintf(
145 "#!/bin/bash\n"
146 "mount | grep au_postint_mount | grep ext2\n"
147 "if [ $? -eq 0 ]; then\n"
148 " touch %s/postinst_called\n"
149 "fi\n",
150 cwd.c_str()));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700151 const string script_file_name = mountpoint + "/postinst";
152 ASSERT_TRUE(WriteFileString(script_file_name, script));
153 ASSERT_EQ(0, System(string("chmod a+x ") + script_file_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000154
Gilad Arnold30dedd82013-07-03 06:19:09 -0700155 // Unmount image; do not remove the uniquely named directory as it will be
156 // reused during the test.
Ben Chan77a1eba2012-10-07 22:54:55 -0700157 ASSERT_TRUE(utils::UnmountFilesystem(mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000158
adlr@google.com3defe6a2009-12-04 20:57:17 +0000159 // get a loop device we can use for the install device
Don Garrett58e8b1f2012-01-31 16:38:16 -0800160 string dev = "/dev/null";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000161
Ben Chan02f7c1d2014-10-18 15:18:02 -0700162 unique_ptr<ScopedLoopbackDeviceBinder> loop_releaser;
Darin Petkov56dad722011-03-03 16:03:56 -0800163 if (do_losetup) {
Don Garrett58e8b1f2012-01-31 16:38:16 -0800164 loop_releaser.reset(new ScopedLoopbackDeviceBinder(cwd + "/image.dat",
165 &dev));
Darin Petkov56dad722011-03-03 16:03:56 -0800166 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000167
Gilad Arnold30dedd82013-07-03 06:19:09 -0700168 // We use a test-specific powerwash marker file, to avoid race conditions.
169 string powerwash_marker_file = mountpoint + "/factory_install_reset";
170 LOG(INFO) << ">>> powerwash_marker_file=" << powerwash_marker_file;
171
adlr@google.com3defe6a2009-12-04 20:57:17 +0000172 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700173 ObjectFeederAction<InstallPlan> feeder_action;
174 InstallPlan install_plan;
175 install_plan.install_path = dev;
Chris Sosa000ecb32014-04-23 12:21:18 -0700176 install_plan.download_url = "http://devserver:8080/update";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700177 install_plan.powerwash_required = powerwash_required;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700178 feeder_action.set_obj(install_plan);
Gilad Arnold30dedd82013-07-03 06:19:09 -0700179 PostinstallRunnerAction runner_action(powerwash_marker_file.c_str());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000180 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700181 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000182 BondActions(&runner_action, &collector_action);
183 PostinstActionProcessorDelegate delegate;
184 processor.EnqueueAction(&feeder_action);
185 processor.EnqueueAction(&runner_action);
186 processor.EnqueueAction(&collector_action);
187 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800188
189 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
190 delegate.loop_ = loop;
191 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
192 g_main_loop_run(loop);
193 g_main_loop_unref(loop);
194 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000195
Darin Petkovc1a8b422010-07-19 11:34:49 -0700196 EXPECT_TRUE(delegate.code_set_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700197 EXPECT_EQ(should_succeed, delegate.code_ == ErrorCode::kSuccess);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700198 EXPECT_EQ(should_succeed, !collector_action.object().install_path.empty());
199 if (should_succeed)
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700200 EXPECT_TRUE(install_plan == collector_action.object());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700201
Alex Vakulenko75039d72014-03-25 12:36:28 -0700202 const base::FilePath kPowerwashMarkerPath(powerwash_marker_file);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700203 string actual_cmd;
204 if (should_succeed && powerwash_required) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700205 EXPECT_TRUE(base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700206 EXPECT_EQ(kPowerwashCommand, actual_cmd);
207 } else {
208 EXPECT_FALSE(
Alex Vakulenko75039d72014-03-25 12:36:28 -0700209 base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000210 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700211
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700212 if (err_code == 2)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700213 EXPECT_EQ(ErrorCode::kPostinstallBootedFromFirmwareB, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000214
215 struct stat stbuf;
216 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700217 if (should_succeed)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000218 ASSERT_EQ(0, rc);
219 else
220 ASSERT_LT(rc, 0);
221
Darin Petkov56dad722011-03-03 16:03:56 -0800222 if (do_losetup) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700223 loop_releaser.reset(nullptr);
Darin Petkov56dad722011-03-03 16:03:56 -0800224 }
Gilad Arnold30dedd82013-07-03 06:19:09 -0700225
226 // Remove unique stateful directory.
227 ASSERT_EQ(0, System(string("rm -fr ") + mountpoint));
228
229 // Remove the temporary work directory.
230 ASSERT_EQ(0, Chdir(orig_cwd));
231 ASSERT_EQ(0, System(string("rm -fr ") + cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000232}
233
234// Death tests don't seem to be working on Hardy
235TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
236 ASSERT_EQ(0, getuid());
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800237 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000238 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
239 "postinstall_runner_action.h:.*] Check failed");
240}
241
242} // namespace chromeos_update_engine