blob: 0deb8594a5f5f37b603a432d314e7fb83bd014d6 [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
Gilad Arnold30dedd82013-07-03 06:19:09 -070095const char* PostinstallRunnerActionTest::kImageMountPointTemplate =
96 "au_destination-XXXXXX";
97
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070098void PostinstallRunnerActionTest::DoTest(
99 bool do_losetup,
100 int err_code,
101 bool powerwash_required) {
adlr@google.com3defe6a2009-12-04 20:57:17 +0000102 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
103 << "as root, tho.";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700104 // True if the post-install action is expected to succeed.
105 bool should_succeed = do_losetup && !err_code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000106
Gilad Arnold30dedd82013-07-03 06:19:09 -0700107 string orig_cwd;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000108 {
109 vector<char> buf(1000);
110 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size()));
Gilad Arnold30dedd82013-07-03 06:19:09 -0700111 orig_cwd = string(&buf[0], strlen(&buf[0]));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000112 }
113
Gilad Arnold30dedd82013-07-03 06:19:09 -0700114 // Create a unique named working directory and chdir into it.
115 string cwd;
116 ASSERT_TRUE(utils::MakeTempDirectory(
117 orig_cwd + "/postinstall_runner_action_unittest-XXXXXX",
118 &cwd));
119 ASSERT_EQ(0, Chdir(cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000120
Gilad Arnold30dedd82013-07-03 06:19:09 -0700121 // Create a 10MiB sparse file to be used as image; format it as ext2.
122 ASSERT_EQ(0, system(
123 "dd if=/dev/zero of=image.dat seek=10485759 bs=1 count=1"));
Andrew de los Reyesbfabc302011-01-31 17:23:50 -0800124 ASSERT_EQ(0, system("mkfs.ext2 -F image.dat"));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000125
Gilad Arnold30dedd82013-07-03 06:19:09 -0700126 // Create a uniquely named image mount point, mount the image.
127 ASSERT_EQ(0, System(string("mkdir -p ") + kStatefulPartition));
128 string mountpoint;
129 ASSERT_TRUE(utils::MakeTempDirectory(
130 string(kStatefulPartition) + "/" + kImageMountPointTemplate,
131 &mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000132 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
133
Gilad Arnold30dedd82013-07-03 06:19:09 -0700134 // Generate a fake postinst script inside the image.
135 string script = (err_code ?
136 StringPrintf("#!/bin/bash\nexit %d", err_code) :
137 StringPrintf("#!/bin/bash\n"
138 "mount | grep au_postint_mount | grep ext2\n"
139 "if [ $? -eq 0 ]; then\n"
140 " touch %s/postinst_called\n"
141 "fi\n",
142 cwd.c_str()));
143 const string script_file_name = mountpoint + "/postinst";
144 ASSERT_TRUE(WriteFileString(script_file_name, script));
145 ASSERT_EQ(0, System(string("chmod a+x ") + script_file_name));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000146
Gilad Arnold30dedd82013-07-03 06:19:09 -0700147 // Unmount image; do not remove the uniquely named directory as it will be
148 // reused during the test.
Ben Chan77a1eba2012-10-07 22:54:55 -0700149 ASSERT_TRUE(utils::UnmountFilesystem(mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000150
adlr@google.com3defe6a2009-12-04 20:57:17 +0000151 // get a loop device we can use for the install device
Don Garrett58e8b1f2012-01-31 16:38:16 -0800152 string dev = "/dev/null";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000153
Don Garrett58e8b1f2012-01-31 16:38:16 -0800154 scoped_ptr<ScopedLoopbackDeviceBinder> loop_releaser;
Darin Petkov56dad722011-03-03 16:03:56 -0800155 if (do_losetup) {
Don Garrett58e8b1f2012-01-31 16:38:16 -0800156 loop_releaser.reset(new ScopedLoopbackDeviceBinder(cwd + "/image.dat",
157 &dev));
Darin Petkov56dad722011-03-03 16:03:56 -0800158 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000159
Gilad Arnold30dedd82013-07-03 06:19:09 -0700160 // We use a test-specific powerwash marker file, to avoid race conditions.
161 string powerwash_marker_file = mountpoint + "/factory_install_reset";
162 LOG(INFO) << ">>> powerwash_marker_file=" << powerwash_marker_file;
163
adlr@google.com3defe6a2009-12-04 20:57:17 +0000164 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700165 ObjectFeederAction<InstallPlan> feeder_action;
166 InstallPlan install_plan;
167 install_plan.install_path = dev;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700168 install_plan.powerwash_required = powerwash_required;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700169 feeder_action.set_obj(install_plan);
Gilad Arnold30dedd82013-07-03 06:19:09 -0700170 PostinstallRunnerAction runner_action(powerwash_marker_file.c_str());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000171 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700172 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000173 BondActions(&runner_action, &collector_action);
174 PostinstActionProcessorDelegate delegate;
175 processor.EnqueueAction(&feeder_action);
176 processor.EnqueueAction(&runner_action);
177 processor.EnqueueAction(&collector_action);
178 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800179
180 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
181 delegate.loop_ = loop;
182 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
183 g_main_loop_run(loop);
184 g_main_loop_unref(loop);
185 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000186
Darin Petkovc1a8b422010-07-19 11:34:49 -0700187 EXPECT_TRUE(delegate.code_set_);
David Zeuthena99981f2013-04-29 13:42:47 -0700188 EXPECT_EQ(should_succeed, delegate.code_ == kErrorCodeSuccess);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700189 EXPECT_EQ(should_succeed, !collector_action.object().install_path.empty());
190 if (should_succeed)
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700191 EXPECT_TRUE(install_plan == collector_action.object());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700192
Gilad Arnold30dedd82013-07-03 06:19:09 -0700193 const FilePath kPowerwashMarkerPath(powerwash_marker_file);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700194 string actual_cmd;
195 if (should_succeed && powerwash_required) {
196 EXPECT_TRUE(file_util::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
197 EXPECT_EQ(kPowerwashCommand, actual_cmd);
198 } else {
199 EXPECT_FALSE(
200 file_util::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000201 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700202
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700203 if (err_code == 2)
David Zeuthena99981f2013-04-29 13:42:47 -0700204 EXPECT_EQ(kErrorCodePostinstallBootedFromFirmwareB, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000205
206 struct stat stbuf;
207 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700208 if (should_succeed)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000209 ASSERT_EQ(0, rc);
210 else
211 ASSERT_LT(rc, 0);
212
Darin Petkov56dad722011-03-03 16:03:56 -0800213 if (do_losetup) {
214 loop_releaser.reset(NULL);
215 }
Gilad Arnold30dedd82013-07-03 06:19:09 -0700216
217 // Remove unique stateful directory.
218 ASSERT_EQ(0, System(string("rm -fr ") + mountpoint));
219
220 // Remove the temporary work directory.
221 ASSERT_EQ(0, Chdir(orig_cwd));
222 ASSERT_EQ(0, System(string("rm -fr ") + cwd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000223}
224
225// Death tests don't seem to be working on Hardy
226TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
227 ASSERT_EQ(0, getuid());
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800228 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000229 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
230 "postinstall_runner_action.h:.*] Check failed");
231}
232
233} // namespace chromeos_update_engine