blob: fdde1800dd922a547c98871a4ebb3fb6b72ce354 [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);
adlr@google.com3defe6a2009-12-04 20:57:17 +000041};
42
43class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
44 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070045 PostinstActionProcessorDelegate()
Darin Petkov6f03a3b2010-11-10 14:27:14 -080046 : loop_(NULL),
David Zeuthena99981f2013-04-29 13:42:47 -070047 code_(kErrorCodeError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070048 code_set_(false) {}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080049 void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070050 ErrorCode code) {
Darin Petkov6f03a3b2010-11-10 14:27:14 -080051 ASSERT_TRUE(loop_);
52 g_main_loop_quit(loop_);
53 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000054 void ActionCompleted(ActionProcessor* processor,
55 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070056 ErrorCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000057 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070058 code_ = code;
59 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000060 }
61 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -080062 GMainLoop* loop_;
David Zeuthena99981f2013-04-29 13:42:47 -070063 ErrorCode code_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070064 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000065};
66
67TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
68 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070069 DoTest(true, 0, false);
70}
71
72TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) {
73 ASSERT_EQ(0, getuid());
74 DoTest(true, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000075}
76
77TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
78 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070079 DoTest(false, 0, true);
adlr@google.com3defe6a2009-12-04 20:57:17 +000080}
81
82TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
83 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070084 DoTest(true, 1, false);
adlr@google.com3defe6a2009-12-04 20:57:17 +000085}
86
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070087TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) {
88 ASSERT_EQ(0, getuid());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070089 DoTest(true, 3, false);
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -070090}
91
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070092void PostinstallRunnerActionTest::DoTest(
93 bool do_losetup,
94 int err_code,
95 bool powerwash_required) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000096 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
97 << "as root, tho.";
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070098 // True if the post-install action is expected to succeed.
99 bool should_succeed = do_losetup && !err_code;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000100
Chris Sosabe45bef2013-04-09 18:25:12 -0700101 const string mountpoint(string(kStatefulPartition) +
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800102 "/au_destination");
adlr@google.com3defe6a2009-12-04 20:57:17 +0000103
104 string cwd;
105 {
106 vector<char> buf(1000);
107 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size()));
108 cwd = string(&buf[0], strlen(&buf[0]));
109 }
110
111 // create the au destination, if it doesn't exist
112 ASSERT_EQ(0, System(string("mkdir -p ") + mountpoint));
113
114 // create 10MiB sparse file
115 ASSERT_EQ(0, system("dd if=/dev/zero of=image.dat seek=10485759 bs=1 "
116 "count=1"));
117
Andrew de los Reyesbfabc302011-01-31 17:23:50 -0800118 // format it as ext2
119 ASSERT_EQ(0, system("mkfs.ext2 -F image.dat"));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000120
121 // mount it
122 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
123
124 // put a postinst script in
Andrew de los Reyesbfabc302011-01-31 17:23:50 -0800125 string script = StringPrintf("#!/bin/bash\n"
126 "mount | grep au_postint_mount | grep ext2\n"
127 "if [ $? -eq 0 ]; then\n"
128 " touch %s/postinst_called\n"
129 "fi\n",
130 cwd.c_str());
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700131 if (err_code) {
132 script = StringPrintf("#!/bin/bash\nexit %d", err_code);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000133 }
134 ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script));
135 ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst"));
136
Ben Chan77a1eba2012-10-07 22:54:55 -0700137 ASSERT_TRUE(utils::UnmountFilesystem(mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000138
139 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called"));
140
141 // get a loop device we can use for the install device
Don Garrett58e8b1f2012-01-31 16:38:16 -0800142 string dev = "/dev/null";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000143
Don Garrett58e8b1f2012-01-31 16:38:16 -0800144 scoped_ptr<ScopedLoopbackDeviceBinder> loop_releaser;
Darin Petkov56dad722011-03-03 16:03:56 -0800145 if (do_losetup) {
Don Garrett58e8b1f2012-01-31 16:38:16 -0800146 loop_releaser.reset(new ScopedLoopbackDeviceBinder(cwd + "/image.dat",
147 &dev));
Darin Petkov56dad722011-03-03 16:03:56 -0800148 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000149
150 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700151 ObjectFeederAction<InstallPlan> feeder_action;
152 InstallPlan install_plan;
153 install_plan.install_path = dev;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700154 install_plan.powerwash_required = powerwash_required;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700155 feeder_action.set_obj(install_plan);
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800156 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000157 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700158 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000159 BondActions(&runner_action, &collector_action);
160 PostinstActionProcessorDelegate delegate;
161 processor.EnqueueAction(&feeder_action);
162 processor.EnqueueAction(&runner_action);
163 processor.EnqueueAction(&collector_action);
164 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800165
166 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
167 delegate.loop_ = loop;
168 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
169 g_main_loop_run(loop);
170 g_main_loop_unref(loop);
171 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000172
Darin Petkovc1a8b422010-07-19 11:34:49 -0700173 EXPECT_TRUE(delegate.code_set_);
David Zeuthena99981f2013-04-29 13:42:47 -0700174 EXPECT_EQ(should_succeed, delegate.code_ == kErrorCodeSuccess);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700175 EXPECT_EQ(should_succeed, !collector_action.object().install_path.empty());
176 if (should_succeed)
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700177 EXPECT_TRUE(install_plan == collector_action.object());
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700178
179 const FilePath kPowerwashMarkerPath(kPowerwashMarkerFile);
180 string actual_cmd;
181 if (should_succeed && powerwash_required) {
182 EXPECT_TRUE(file_util::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
183 EXPECT_EQ(kPowerwashCommand, actual_cmd);
184 } else {
185 EXPECT_FALSE(
186 file_util::ReadFileToString(kPowerwashMarkerPath, &actual_cmd));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000187 }
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700188
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700189 if (err_code == 2)
David Zeuthena99981f2013-04-29 13:42:47 -0700190 EXPECT_EQ(kErrorCodePostinstallBootedFromFirmwareB, delegate.code_);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000191
192 struct stat stbuf;
193 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700194 if (should_succeed)
adlr@google.com3defe6a2009-12-04 20:57:17 +0000195 ASSERT_EQ(0, rc);
196 else
197 ASSERT_LT(rc, 0);
198
Darin Petkov56dad722011-03-03 16:03:56 -0800199 if (do_losetup) {
200 loop_releaser.reset(NULL);
201 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000202 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called"));
203 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat"));
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700204 utils::DeletePowerwashMarkerFile();
adlr@google.com3defe6a2009-12-04 20:57:17 +0000205}
206
207// Death tests don't seem to be working on Hardy
208TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
209 ASSERT_EQ(0, getuid());
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800210 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000211 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
212 "postinstall_runner_action.h:.*] Check failed");
213}
214
215} // namespace chromeos_update_engine