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> |
| 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 | |
| 15 | using std::string; |
| 16 | using std::vector; |
| 17 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 20 | namespace { |
| 21 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 22 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 23 | processor->StartProcessing(); |
| 24 | return FALSE; |
| 25 | } |
| 26 | } // namespace |
| 27 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 28 | class PostinstallRunnerActionTest : public ::testing::Test { |
| 29 | public: |
| 30 | void DoTest(bool do_losetup, bool do_err_script); |
| 31 | }; |
| 32 | |
| 33 | class PostinstActionProcessorDelegate : public ActionProcessorDelegate { |
| 34 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 35 | PostinstActionProcessorDelegate() |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 36 | : loop_(NULL), |
| 37 | code_(kActionCodeError), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 38 | code_set_(false) {} |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 39 | void ProcessingDone(const ActionProcessor* processor, |
| 40 | ActionExitCode code) { |
| 41 | ASSERT_TRUE(loop_); |
| 42 | g_main_loop_quit(loop_); |
| 43 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 44 | void ActionCompleted(ActionProcessor* processor, |
| 45 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 46 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 47 | if (action->Type() == PostinstallRunnerAction::StaticType()) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 48 | code_ = code; |
| 49 | code_set_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 52 | GMainLoop* loop_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 53 | ActionExitCode code_; |
| 54 | bool code_set_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) { |
| 58 | ASSERT_EQ(0, getuid()); |
| 59 | DoTest(true, false); |
| 60 | } |
| 61 | |
| 62 | TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) { |
| 63 | ASSERT_EQ(0, getuid()); |
| 64 | DoTest(false, false); |
| 65 | } |
| 66 | |
| 67 | TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) { |
| 68 | ASSERT_EQ(0, getuid()); |
| 69 | DoTest(true, true); |
| 70 | } |
| 71 | |
| 72 | void 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 Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 76 | const string mountpoint(string(utils::kStatefulPartition) + |
| 77 | "/au_destination"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 78 | |
| 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 Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 107 | ASSERT_EQ(0, System(string("umount -d ") + mountpoint)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 108 | |
| 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 Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 130 | ObjectFeederAction<InstallPlan> feeder_action; |
| 131 | InstallPlan install_plan; |
| 132 | install_plan.install_path = dev; |
| 133 | feeder_action.set_obj(install_plan); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 134 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 135 | BondActions(&feeder_action, &runner_action); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 136 | ObjectCollectorAction<InstallPlan> collector_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 137 | 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 Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 143 | |
| 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.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 150 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 151 | EXPECT_TRUE(delegate.code_set_); |
| 152 | EXPECT_EQ(do_losetup && !do_err_script, delegate.code_ == kActionCodeSuccess); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 153 | EXPECT_EQ(do_losetup && !do_err_script, |
| 154 | !collector_action.object().install_path.empty()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 155 | if (do_losetup && !do_err_script) { |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 156 | EXPECT_TRUE(install_plan == collector_action.object()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 157 | } |
| 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 |
| 173 | TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { |
| 174 | ASSERT_EQ(0, getuid()); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 175 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 176 | ASSERT_DEATH({ runner_action.TerminateProcessing(); }, |
| 177 | "postinstall_runner_action.h:.*] Check failed"); |
| 178 | } |
| 179 | |
| 180 | } // namespace chromeos_update_engine |