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 | |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 135 | scoped_ptr<ScopedLoopbackDeviceReleaser> loop_releaser; |
| 136 | if (do_losetup) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 137 | ASSERT_EQ(0, System(string("losetup ") + dev + " " + cwd + "/image.dat")); |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 138 | loop_releaser.reset(new ScopedLoopbackDeviceReleaser(dev)); |
| 139 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 140 | |
| 141 | ActionProcessor processor; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 142 | ObjectFeederAction<InstallPlan> feeder_action; |
| 143 | InstallPlan install_plan; |
| 144 | install_plan.install_path = dev; |
| 145 | feeder_action.set_obj(install_plan); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 146 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 147 | BondActions(&feeder_action, &runner_action); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 148 | ObjectCollectorAction<InstallPlan> collector_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 149 | BondActions(&runner_action, &collector_action); |
| 150 | PostinstActionProcessorDelegate delegate; |
| 151 | processor.EnqueueAction(&feeder_action); |
| 152 | processor.EnqueueAction(&runner_action); |
| 153 | processor.EnqueueAction(&collector_action); |
| 154 | processor.set_delegate(&delegate); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 155 | |
| 156 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 157 | delegate.loop_ = loop; |
| 158 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 159 | g_main_loop_run(loop); |
| 160 | g_main_loop_unref(loop); |
| 161 | ASSERT_FALSE(processor.IsRunning()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 162 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 163 | EXPECT_TRUE(delegate.code_set_); |
| 164 | EXPECT_EQ(do_losetup && !do_err_script, delegate.code_ == kActionCodeSuccess); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 165 | EXPECT_EQ(do_losetup && !do_err_script, |
| 166 | !collector_action.object().install_path.empty()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 167 | if (do_losetup && !do_err_script) { |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 168 | EXPECT_TRUE(install_plan == collector_action.object()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | struct stat stbuf; |
| 172 | int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf); |
| 173 | if (do_losetup && !do_err_script) |
| 174 | ASSERT_EQ(0, rc); |
| 175 | else |
| 176 | ASSERT_LT(rc, 0); |
| 177 | |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 178 | if (do_losetup) { |
| 179 | loop_releaser.reset(NULL); |
| 180 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 181 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
| 182 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat")); |
| 183 | } |
| 184 | |
| 185 | // Death tests don't seem to be working on Hardy |
| 186 | TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { |
| 187 | ASSERT_EQ(0, getuid()); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 188 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 189 | ASSERT_DEATH({ runner_action.TerminateProcessing(); }, |
| 190 | "postinstall_runner_action.h:.*] Check failed"); |
| 191 | } |
| 192 | |
| 193 | } // namespace chromeos_update_engine |