blob: 128610f7b11294eaf12149f3ebea21287dbe9cf5 [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>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080015#include <base/string_util.h>
Mike Frysinger8155d082012-04-06 15:23:18 -040016#include <base/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),
David Zeuthena99981f2013-04-29 13:42:47 -070050 code_(kErrorCodeError),
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(
122 orig_cwd + "/postinstall_runner_action_unittest-XXXXXX",
123 &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 ?
141 StringPrintf("#!/bin/bash\nexit %d", err_code) :
142 StringPrintf("#!/bin/bash\n"
143 "mount | grep au_postint_mount | grep ext2\n"
144 "if [ $? -eq 0 ]; then\n"
145 " touch %s/postinst_called\n"
146 "fi\n",
147 cwd.c_str()));
148 const string script_file_name = mountpoint + "/postinst";
149 ASSERT_TRUE(WriteFileString(script_file_name, script));
150 ASSERT_EQ(0, System(string("chmod a+x ") + script_file_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000151
Gilad Arnold30dedd82013-07-03 06:19:09 -0700152 // Unmount image; do not remove the uniquely named directory as it will be
153 // reused during the test.
Ben Chan77a1eba2012-10-07 22:54:55 -0700154 ASSERT_TRUE(utils::UnmountFilesystem(mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000155
adlr@google.com3defe6a2009-12-04 20:57:17 +0000156 // get a loop device we can use for the install device
Don Garrett58e8b1f2012-01-31 16:38:16 -0800157 string dev = "/dev/null";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000158
Don Garrett58e8b1f2012-01-31 16:38:16 -0800159 scoped_ptr<ScopedLoopbackDeviceBinder> loop_releaser;
Darin Petkov56dad722011-03-03 16:03:56 -0800160 if (do_losetup) {
Don Garrett58e8b1f2012-01-31 16:38:16 -0800161 loop_releaser.reset(new ScopedLoopbackDeviceBinder(cwd + "/image.dat",
162 &dev));
Darin Petkov56dad722011-03-03 16:03:56 -0800163 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000164
Gilad Arnold30dedd82013-07-03 06:19:09 -0700165 // We use a test-specific powerwash marker file, to avoid race conditions.
166 string powerwash_marker_file = mountpoint + "/factory_install_reset";
167 LOG(INFO) << ">>> powerwash_marker_file=" << powerwash_marker_file;
168
adlr@google.com3defe6a2009-12-04 20:57:17 +0000169 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700170 ObjectFeederAction<InstallPlan> feeder_action;
171 InstallPlan install_plan;
172 install_plan.install_path = dev;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700173 install_plan.powerwash_required = powerwash_required;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700174 feeder_action.set_obj(install_plan);
Gilad Arnold30dedd82013-07-03 06:19:09 -0700175 PostinstallRunnerAction runner_action(powerwash_marker_file.c_str());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000176 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700177 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000178 BondActions(&runner_action, &collector_action);
179 PostinstActionProcessorDelegate delegate;
180 processor.EnqueueAction(&feeder_action);
181 processor.EnqueueAction(&runner_action);
182 processor.EnqueueAction(&collector_action);
183 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800184
185 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
186 delegate.loop_ = loop;
187 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
188 g_main_loop_run(loop);
189 g_main_loop_unref(loop);
190 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_);
David Zeuthena99981f2013-04-29 13:42:47 -0700193 EXPECT_EQ(should_succeed, delegate.code_ == kErrorCodeSuccess);
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
Gilad Arnold30dedd82013-07-03 06:19:09 -0700198 const FilePath kPowerwashMarkerPath(powerwash_marker_file);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700199 string actual_cmd;
200 if (should_succeed && powerwash_required) {
201 EXPECT_TRUE(file_util::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
202 EXPECT_EQ(kPowerwashCommand, actual_cmd);
203 } else {
204 EXPECT_FALSE(
205 file_util::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)
David Zeuthena99981f2013-04-29 13:42:47 -0700209 EXPECT_EQ(kErrorCodePostinstallBootedFromFirmwareB, 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) {
219 loop_releaser.reset(NULL);
220 }
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.
226 ASSERT_EQ(0, Chdir(orig_cwd));
227 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