blob: 7f4e65c16e33e60e7d310548d29afa61a137830a [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>
8#include <string>
9#include <vector>
10#include <gtest/gtest.h>
11#include "update_engine/postinstall_runner_action.h"
12#include "update_engine/test_utils.h"
13#include "update_engine/utils.h"
14
15using std::string;
16using std::vector;
17
18namespace chromeos_update_engine {
19
Darin Petkov6f03a3b2010-11-10 14:27:14 -080020namespace {
21gboolean StartProcessorInRunLoop(gpointer data) {
22 ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data);
23 processor->StartProcessing();
24 return FALSE;
25}
26} // namespace
27
adlr@google.com3defe6a2009-12-04 20:57:17 +000028class PostinstallRunnerActionTest : public ::testing::Test {
29 public:
30 void DoTest(bool do_losetup, bool do_err_script);
31};
32
33class PostinstActionProcessorDelegate : public ActionProcessorDelegate {
34 public:
Darin Petkovc1a8b422010-07-19 11:34:49 -070035 PostinstActionProcessorDelegate()
Darin Petkov6f03a3b2010-11-10 14:27:14 -080036 : loop_(NULL),
37 code_(kActionCodeError),
Darin Petkovc1a8b422010-07-19 11:34:49 -070038 code_set_(false) {}
Darin Petkov6f03a3b2010-11-10 14:27:14 -080039 void ProcessingDone(const ActionProcessor* processor,
40 ActionExitCode code) {
41 ASSERT_TRUE(loop_);
42 g_main_loop_quit(loop_);
43 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000044 void ActionCompleted(ActionProcessor* processor,
45 AbstractAction* action,
Darin Petkovc1a8b422010-07-19 11:34:49 -070046 ActionExitCode code) {
adlr@google.com3defe6a2009-12-04 20:57:17 +000047 if (action->Type() == PostinstallRunnerAction::StaticType()) {
Darin Petkovc1a8b422010-07-19 11:34:49 -070048 code_ = code;
49 code_set_ = true;
adlr@google.com3defe6a2009-12-04 20:57:17 +000050 }
51 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -080052 GMainLoop* loop_;
Darin Petkovc1a8b422010-07-19 11:34:49 -070053 ActionExitCode code_;
54 bool code_set_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000055};
56
57TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) {
58 ASSERT_EQ(0, getuid());
59 DoTest(true, false);
60}
61
62TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) {
63 ASSERT_EQ(0, getuid());
64 DoTest(false, false);
65}
66
67TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) {
68 ASSERT_EQ(0, getuid());
69 DoTest(true, true);
70}
71
72void PostinstallRunnerActionTest::DoTest(bool do_losetup, bool do_err_script) {
73 ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests "
74 << "as root, tho.";
75
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080076 const string mountpoint(string(utils::kStatefulPartition) +
77 "/au_destination");
adlr@google.com3defe6a2009-12-04 20:57:17 +000078
79 string cwd;
80 {
81 vector<char> buf(1000);
82 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size()));
83 cwd = string(&buf[0], strlen(&buf[0]));
84 }
85
86 // create the au destination, if it doesn't exist
87 ASSERT_EQ(0, System(string("mkdir -p ") + mountpoint));
88
89 // create 10MiB sparse file
90 ASSERT_EQ(0, system("dd if=/dev/zero of=image.dat seek=10485759 bs=1 "
91 "count=1"));
92
93 // format it as ext3
94 ASSERT_EQ(0, system("mkfs.ext3 -F image.dat"));
95
96 // mount it
97 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint));
98
99 // put a postinst script in
100 string script = string("#!/bin/bash\ntouch ") + cwd + "/postinst_called\n";
101 if (do_err_script) {
102 script = "#!/bin/bash\nexit 1";
103 }
104 ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script));
105 ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst"));
106
Andrew de los Reyesb10320d2010-03-31 16:44:44 -0700107 ASSERT_EQ(0, System(string("umount -d ") + mountpoint));
adlr@google.com3defe6a2009-12-04 20:57:17 +0000108
109 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called"));
110
111 // get a loop device we can use for the install device
112 FILE* find_dev_cmd = popen("losetup -f", "r");
113 ASSERT_TRUE(find_dev_cmd);
114
115 char dev[100] = {0};
116 size_t r = fread(dev, 1, sizeof(dev), find_dev_cmd);
117 ASSERT_GT(r, 0);
118 ASSERT_LT(r, sizeof(dev));
119 ASSERT_TRUE(feof(find_dev_cmd));
120 fclose(find_dev_cmd);
121
122 // strip trailing newline on dev
123 if (dev[strlen(dev) - 1] == '\n')
124 dev[strlen(dev) - 1] = '\0';
125
126 if (do_losetup)
127 ASSERT_EQ(0, System(string("losetup ") + dev + " " + cwd + "/image.dat"));
128
129 ActionProcessor processor;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700130 ObjectFeederAction<InstallPlan> feeder_action;
131 InstallPlan install_plan;
132 install_plan.install_path = dev;
133 feeder_action.set_obj(install_plan);
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800134 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000135 BondActions(&feeder_action, &runner_action);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700136 ObjectCollectorAction<InstallPlan> collector_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000137 BondActions(&runner_action, &collector_action);
138 PostinstActionProcessorDelegate delegate;
139 processor.EnqueueAction(&feeder_action);
140 processor.EnqueueAction(&runner_action);
141 processor.EnqueueAction(&collector_action);
142 processor.set_delegate(&delegate);
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800143
144 GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
145 delegate.loop_ = loop;
146 g_timeout_add(0, &StartProcessorInRunLoop, &processor);
147 g_main_loop_run(loop);
148 g_main_loop_unref(loop);
149 ASSERT_FALSE(processor.IsRunning());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000150
Darin Petkovc1a8b422010-07-19 11:34:49 -0700151 EXPECT_TRUE(delegate.code_set_);
152 EXPECT_EQ(do_losetup && !do_err_script, delegate.code_ == kActionCodeSuccess);
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700153 EXPECT_EQ(do_losetup && !do_err_script,
154 !collector_action.object().install_path.empty());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000155 if (do_losetup && !do_err_script) {
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700156 EXPECT_TRUE(install_plan == collector_action.object());
adlr@google.com3defe6a2009-12-04 20:57:17 +0000157 }
158
159 struct stat stbuf;
160 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf);
161 if (do_losetup && !do_err_script)
162 ASSERT_EQ(0, rc);
163 else
164 ASSERT_LT(rc, 0);
165
166 if (do_losetup)
167 ASSERT_EQ(0, System(string("losetup -d ") + dev));
168 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called"));
169 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat"));
170}
171
172// Death tests don't seem to be working on Hardy
173TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) {
174 ASSERT_EQ(0, getuid());
Darin Petkov6d5dbf62010-11-08 16:09:55 -0800175 PostinstallRunnerAction runner_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000176 ASSERT_DEATH({ runner_action.TerminateProcessing(); },
177 "postinstall_runner_action.h:.*] Check failed");
178}
179
180} // namespace chromeos_update_engine