blob: baf03b656f0fcb727f6302e308570c21cf3fa349 [file] [log] [blame]
Darin Petkov6d5dbf62010-11-08 16:09:55 -08001// Copyright (c) 2010 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
5#include <sys/stat.h>
6#include <sys/types.h>
7#include <unistd.h>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -08008
adlr@google.com3defe6a2009-12-04 20:57:17 +00009#include <string>
10#include <vector>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080011
12#include <base/string_util.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000013#include <gtest/gtest.h>
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080014
adlr@google.com3defe6a2009-12-04 20:57:17 +000015#include "update_engine/postinstall_runner_action.h"
16#include "update_engine/test_utils.h"
17#include "update_engine/utils.h"
18
19using std::string;
20using std::vector;
21
22namespace chromeos_update_engine {
23
Darin Petkov6f03a3b2010-11-10 14:27:14 -080024namespace {
25gboolean StartProcessorInRunLoop(gpointer data) {
26 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
27 processor->StartProcessing();
28 return FALSE;
29}
30} // namespace
31
adlr@google.com3defe6a2009-12-04 20:57:17 +000032class PostinstallRunnerActionTest : public ::testing::Test {
33 public:
34 void DoTest(bool do_losetup, bool do_err_script);
35};
36
37class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
38 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070039 PostinstActionProcessorDelegate()
Darin Petkov6f03a3b2010-11-10 14:27:14 -080040 : loop_(NULL),
41 code_(kActionCodeError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070042 code_set_(false) {}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080043 void ProcessingDone(const ActionProcessor* processor,
44 ActionExitCode code) {
45 ASSERT_TRUE(loop_);
46 g_main_loop_quit(loop_);
47 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000048 void ActionCompleted(ActionProcessor* processor,
49 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070050 ActionExitCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000051 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070052 code_ = code;
53 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000054 }
55 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -080056 GMainLoop* loop_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070057 ActionExitCode code_;
58 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000059};
60
61TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
62 ASSERT_EQ(0, getuid());
63 DoTest(true, false);
64}
65
66TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
67 ASSERT_EQ(0, getuid());
68 DoTest(false, false);
69}
70
71TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
72 ASSERT_EQ(0, getuid());
73 DoTest(true, true);
74}
75
76void PostinstallRunnerActionTest::DoTest(bool do_losetup, bool do_err_script) {
77 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
78 << "as root, tho.";
79
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080080 const string mountpoint(string(utils::kStatefulPartition) +
81 "/au_destination");
adlr@google.com3defe6a2009-12-04 20:57:17 +000082
83 string cwd;
84 {
85 vector<char> buf(1000);
86 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size()));
87 cwd = string(&buf[0], strlen(&buf[0]));
88 }
89
90 // create the au destination, if it doesn't exist
91 ASSERT_EQ(0, System(string("mkdir -p ") + mountpoint));
92
93 // create 10MiB sparse file
94 ASSERT_EQ(0, system("dd if=/dev/zero of=image.dat seek=10485759 bs=1 "
95 "count=1"));
96
Andrew de los Reyesbfabc302011-01-31 17:23:50 -080097 // format it as ext2
98 ASSERT_EQ(0, system("mkfs.ext2 -F image.dat"));
adlr@google.com3defe6a2009-12-04 20:57:17 +000099
100 // mount it
101 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
102
103 // put a postinst script in
Andrew de los Reyesbfabc302011-01-31 17:23:50 -0800104 string script = StringPrintf("#!/bin/bash\n"
105 "mount | grep au_postint_mount | grep ext2\n"
106 "if [ $? -eq 0 ]; then\n"
107 " touch %s/postinst_called\n"
108 "fi\n",
109 cwd.c_str());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000110 if (do_err_script) {
111 script = "#!/bin/bash\nexit 1";
112 }
113 ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script));
114 ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst"));
115
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700116 ASSERT_EQ(0, System(string("umount -d ") + mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000117
118 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called"));
119
120 // get a loop device we can use for the install device
121 FILE* find_dev_cmd = popen("losetup -f", "r");
122 ASSERT_TRUE(find_dev_cmd);
123
124 char dev[100] = {0};
125 size_t r = fread(dev, 1, sizeof(dev), find_dev_cmd);
126 ASSERT_GT(r, 0);
127 ASSERT_LT(r, sizeof(dev));
128 ASSERT_TRUE(feof(find_dev_cmd));
129 fclose(find_dev_cmd);
130
131 // strip trailing newline on dev
132 if (dev[strlen(dev) - 1] == '\n')
133 dev[strlen(dev) - 1] = '\0';
134
135 if (do_losetup)
136 ASSERT_EQ(0, System(string("losetup ") + dev + " " + cwd + "/image.dat"));
137
138 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700139 ObjectFeederAction<InstallPlan> feeder_action;
140 InstallPlan install_plan;
141 install_plan.install_path = dev;
142 feeder_action.set_obj(install_plan);
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800143 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000144 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700145 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000146 BondActions(&runner_action, &collector_action);
147 PostinstActionProcessorDelegate delegate;
148 processor.EnqueueAction(&feeder_action);
149 processor.EnqueueAction(&runner_action);
150 processor.EnqueueAction(&collector_action);
151 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800152
153 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
154 delegate.loop_ = loop;
155 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
156 g_main_loop_run(loop);
157 g_main_loop_unref(loop);
158 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000159
Darin Petkovc1a8b422010-07-19 11:34:49 -0700160 EXPECT_TRUE(delegate.code_set_);
161 EXPECT_EQ(do_losetup && !do_err_script, delegate.code_ == kActionCodeSuccess);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700162 EXPECT_EQ(do_losetup && !do_err_script,
163 !collector_action.object().install_path.empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000164 if (do_losetup && !do_err_script) {
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700165 EXPECT_TRUE(install_plan == collector_action.object());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000166 }
167
168 struct stat stbuf;
169 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
170 if (do_losetup && !do_err_script)
171 ASSERT_EQ(0, rc);
172 else
173 ASSERT_LT(rc, 0);
174
175 if (do_losetup)
176 ASSERT_EQ(0, System(string("losetup -d ") + dev));
177 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called"));
178 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat"));
179}
180
181// Death tests don't seem to be working on Hardy
182TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
183 ASSERT_EQ(0, getuid());
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800184 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000185 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
186 "postinstall_runner_action.h:.*] Check failed");
187}
188
189} // namespace chromeos_update_engine