blob: acceb2b9b3e9101d6eaa12c7c14f287b20133de8 [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
adlr@google.com3defe6a2009-12-04 20:57:17 +000011#include <string>
12#include <vector>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080013
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070014#include <base/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070015#include <base/strings/string_util.h>
16#include <base/strings/stringprintf.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000017#include <gtest/gtest.h>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080018
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070019#include "update_engine/constants.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include "update_engine/test_utils.h"
21#include "update_engine/utils.h"
22
23using std::string;
24using std::vector;
25
26namespace chromeos_update_engine {
27
Darin Petkov6f03a3b2010-11-10 14:27:14 -080028namespace {
29gboolean StartProcessorInRunLoop(gpointer data) {
30 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
31 processor->StartProcessing();
32 return FALSE;
33}
34} // namespace
35
adlr@google.com3defe6a2009-12-04 20:57:17 +000036class PostinstallRunnerActionTest : public ::testing::Test {
37 public:
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070038 // DoTest with various combinations of do_losetup, err_code and
39 // powerwash_required.
40 void DoTest(bool do_losetup, int err_code, bool powerwash_required);
Gilad Arnold30dedd82013-07-03 06:19:09 -070041
42 private:
43 static const char* kImageMountPointTemplate;
adlr@google.com3defe6a2009-12-04 20:57:17 +000044};
45
46class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
47 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070048 PostinstActionProcessorDelegate()
Darin Petkov6f03a3b2010-11-10 14:27:14 -080049 : loop_(NULL),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070050 code_(ErrorCode::kError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070051 code_set_(false) {}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080052 void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070053 ErrorCode code) {
Darin Petkov6f03a3b2010-11-10 14:27:14 -080054 ASSERT_TRUE(loop_);
55 g_main_loop_quit(loop_);
56 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000057 void ActionCompleted(ActionProcessor* processor,
58 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070059 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000060 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070061 code_ = code;
62 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000063 }
64 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -080065 GMainLoop* loop_;
David Zeuthena99981f2013-04-29 13:42:47 -070066 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070067 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000068};
69
70TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
71 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070072 DoTest(true, 0, false);
73}
74
75TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) {
76 ASSERT_EQ(0, getuid());
77 DoTest(true, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000078}
79
80TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
81 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070082 DoTest(false, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000083}
84
85TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
86 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070087 DoTest(true, 1, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +000088}
89
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070090TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) {
91 ASSERT_EQ(0, getuid());
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) {
96 ASSERT_EQ(0, getuid());
97 DoTest(true, 4, false);
98}
99
Gilad Arnold30dedd82013-07-03 06:19:09 -0700100const char* PostinstallRunnerActionTest::kImageMountPointTemplate =
101 "au_destination-XXXXXX";
102
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700103void PostinstallRunnerActionTest::DoTest(
104 bool do_losetup,
105 int err_code,
106 bool powerwash_required) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000107 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
108 << "as root, tho.";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700109 // True if the post-install action is expected to succeed.
110 bool should_succeed = do_losetup && !err_code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000111
Gilad Arnold30dedd82013-07-03 06:19:09 -0700112 string orig_cwd;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000113 {
114 vector<char> buf(1000);
115 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size()));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700116 orig_cwd = string(&buf[0], strlen(&buf[0]));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000117 }
118
Gilad Arnold30dedd82013-07-03 06:19:09 -0700119 // Create a unique named working directory and chdir into it.
120 string cwd;
121 ASSERT_TRUE(utils::MakeTempDirectory(
Gilad Arnolda6742b32014-01-11 00:18:34 -0800122 "postinstall_runner_action_unittest-XXXXXX",
Gilad Arnold30dedd82013-07-03 06:19:09 -0700123 &cwd));
124 ASSERT_EQ(0, Chdir(cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000125
Gilad Arnold30dedd82013-07-03 06:19:09 -0700126 // Create a 10MiB sparse file to be used as image; format it as ext2.
127 ASSERT_EQ(0, system(
128 "dd if=/dev/zero of=image.dat seek=10485759 bs=1 count=1"));
Andrew de los Reyesbfabc302011-01-31 17:23:50 -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
Don Garrett58e8b1f2012-01-31 16:38:16 -0800160 scoped_ptr<ScopedLoopbackDeviceBinder> loop_releaser;
Darin Petkov56dad722011-03-03 16:03:56 -0800161 if (do_losetup) {
Don Garrett58e8b1f2012-01-31 16:38:16 -0800162 loop_releaser.reset(new ScopedLoopbackDeviceBinder(cwd + "/image.dat",
163 &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
187 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
188 delegate.loop_ = loop;
189 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
190 g_main_loop_run(loop);
191 g_main_loop_unref(loop);
192 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000193
Darin Petkovc1a8b422010-07-19 11:34:49 -0700194 EXPECT_TRUE(delegate.code_set_);
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700195 EXPECT_EQ(should_succeed, delegate.code_ == ErrorCode::kSuccess);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700196 EXPECT_EQ(should_succeed, !collector_action.object().install_path.empty());
197 if (should_succeed)
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700198 EXPECT_TRUE(install_plan == collector_action.object());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700199
Alex Vakulenko75039d72014-03-25 12:36:28 -0700200 const base::FilePath kPowerwashMarkerPath(powerwash_marker_file);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700201 string actual_cmd;
202 if (should_succeed && powerwash_required) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700203 EXPECT_TRUE(base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700204 EXPECT_EQ(kPowerwashCommand, actual_cmd);
205 } else {
206 EXPECT_FALSE(
Alex Vakulenko75039d72014-03-25 12:36:28 -0700207 base::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000208 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700209
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700210 if (err_code == 2)
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700211 EXPECT_EQ(ErrorCode::kPostinstallBootedFromFirmwareB, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000212
213 struct stat stbuf;
214 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700215 if (should_succeed)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000216 ASSERT_EQ(0, rc);
217 else
218 ASSERT_LT(rc, 0);
219
Darin Petkov56dad722011-03-03 16:03:56 -0800220 if (do_losetup) {
221 loop_releaser.reset(NULL);
222 }
Gilad Arnold30dedd82013-07-03 06:19:09 -0700223
224 // Remove unique stateful directory.
225 ASSERT_EQ(0, System(string("rm -fr ") + mountpoint));
226
227 // Remove the temporary work directory.
228 ASSERT_EQ(0, Chdir(orig_cwd));
229 ASSERT_EQ(0, System(string("rm -fr ") + cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000230}
231
232// Death tests don't seem to be working on Hardy
233TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
234 ASSERT_EQ(0, getuid());
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800235 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000236 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
237 "postinstall_runner_action.h:.*] Check failed");
238}
239
240} // namespace chromeos_update_engine