Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // 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 Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 8 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 11 | |
| 12 | #include <base/string_util.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 14 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 15 | #include "update_engine/postinstall_runner_action.h" |
| 16 | #include "update_engine/test_utils.h" |
| 17 | #include "update_engine/utils.h" |
| 18 | |
| 19 | using std::string; |
| 20 | using std::vector; |
| 21 | |
| 22 | namespace chromeos_update_engine { |
| 23 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 24 | namespace { |
| 25 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 26 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 27 | processor->StartProcessing(); |
| 28 | return FALSE; |
| 29 | } |
| 30 | } // namespace |
| 31 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 32 | class PostinstallRunnerActionTest : public ::testing::Test { |
| 33 | public: |
| 34 | void DoTest(bool do_losetup, bool do_err_script); |
| 35 | }; |
| 36 | |
| 37 | class PostinstActionProcessorDelegate : public ActionProcessorDelegate { |
| 38 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 39 | PostinstActionProcessorDelegate() |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 40 | : loop_(NULL), |
| 41 | code_(kActionCodeError), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 42 | code_set_(false) {} |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 43 | void ProcessingDone(const ActionProcessor* processor, |
| 44 | ActionExitCode code) { |
| 45 | ASSERT_TRUE(loop_); |
| 46 | g_main_loop_quit(loop_); |
| 47 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 48 | void ActionCompleted(ActionProcessor* processor, |
| 49 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 50 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 51 | if (action->Type() == PostinstallRunnerAction::StaticType()) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 52 | code_ = code; |
| 53 | code_set_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 56 | GMainLoop* loop_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 57 | ActionExitCode code_; |
| 58 | bool code_set_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) { |
| 62 | ASSERT_EQ(0, getuid()); |
| 63 | DoTest(true, false); |
| 64 | } |
| 65 | |
| 66 | TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) { |
| 67 | ASSERT_EQ(0, getuid()); |
| 68 | DoTest(false, false); |
| 69 | } |
| 70 | |
| 71 | TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) { |
| 72 | ASSERT_EQ(0, getuid()); |
| 73 | DoTest(true, true); |
| 74 | } |
| 75 | |
| 76 | void 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 Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 80 | const string mountpoint(string(utils::kStatefulPartition) + |
| 81 | "/au_destination"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 82 | |
| 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 Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 97 | // format it as ext2 |
| 98 | ASSERT_EQ(0, system("mkfs.ext2 -F image.dat")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 99 | |
| 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 Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 104 | 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 110 | 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 Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 116 | ASSERT_EQ(0, System(string("umount -d ") + mountpoint)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 117 | |
| 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 Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 139 | ObjectFeederAction<InstallPlan> feeder_action; |
| 140 | InstallPlan install_plan; |
| 141 | install_plan.install_path = dev; |
| 142 | feeder_action.set_obj(install_plan); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 143 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 144 | BondActions(&feeder_action, &runner_action); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 145 | ObjectCollectorAction<InstallPlan> collector_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 146 | 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 Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 152 | |
| 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 159 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 160 | EXPECT_TRUE(delegate.code_set_); |
| 161 | EXPECT_EQ(do_losetup && !do_err_script, delegate.code_ == kActionCodeSuccess); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 162 | EXPECT_EQ(do_losetup && !do_err_script, |
| 163 | !collector_action.object().install_path.empty()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 164 | if (do_losetup && !do_err_script) { |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 165 | EXPECT_TRUE(install_plan == collector_action.object()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 166 | } |
| 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 |
| 182 | TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { |
| 183 | ASSERT_EQ(0, getuid()); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 184 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 185 | ASSERT_DEATH({ runner_action.TerminateProcessing(); }, |
| 186 | "postinstall_runner_action.h:.*] Check failed"); |
| 187 | } |
| 188 | |
| 189 | } // namespace chromeos_update_engine |