adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 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 | |
| 20 | class PostinstallRunnerActionTest : public ::testing::Test { |
| 21 | public: |
| 22 | void DoTest(bool do_losetup, bool do_err_script); |
| 23 | }; |
| 24 | |
| 25 | class PostinstActionProcessorDelegate : public ActionProcessorDelegate { |
| 26 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame^] | 27 | PostinstActionProcessorDelegate() |
| 28 | : code_(kActionCodeError), |
| 29 | code_set_(false) {} |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 30 | void ActionCompleted(ActionProcessor* processor, |
| 31 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame^] | 32 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 33 | if (action->Type() == PostinstallRunnerAction::StaticType()) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame^] | 34 | code_ = code; |
| 35 | code_set_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 36 | } |
| 37 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame^] | 38 | ActionExitCode code_; |
| 39 | bool code_set_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) { |
| 43 | ASSERT_EQ(0, getuid()); |
| 44 | DoTest(true, false); |
| 45 | } |
| 46 | |
| 47 | TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) { |
| 48 | ASSERT_EQ(0, getuid()); |
| 49 | DoTest(false, false); |
| 50 | } |
| 51 | |
| 52 | TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) { |
| 53 | ASSERT_EQ(0, getuid()); |
| 54 | DoTest(true, true); |
| 55 | } |
| 56 | |
| 57 | void PostinstallRunnerActionTest::DoTest(bool do_losetup, bool do_err_script) { |
| 58 | ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests " |
| 59 | << "as root, tho."; |
| 60 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 61 | const string mountpoint(string(utils::kStatefulPartition) + |
| 62 | "/au_destination"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 63 | |
| 64 | string cwd; |
| 65 | { |
| 66 | vector<char> buf(1000); |
| 67 | ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size())); |
| 68 | cwd = string(&buf[0], strlen(&buf[0])); |
| 69 | } |
| 70 | |
| 71 | // create the au destination, if it doesn't exist |
| 72 | ASSERT_EQ(0, System(string("mkdir -p ") + mountpoint)); |
| 73 | |
| 74 | // create 10MiB sparse file |
| 75 | ASSERT_EQ(0, system("dd if=/dev/zero of=image.dat seek=10485759 bs=1 " |
| 76 | "count=1")); |
| 77 | |
| 78 | // format it as ext3 |
| 79 | ASSERT_EQ(0, system("mkfs.ext3 -F image.dat")); |
| 80 | |
| 81 | // mount it |
| 82 | ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint)); |
| 83 | |
| 84 | // put a postinst script in |
| 85 | string script = string("#!/bin/bash\ntouch ") + cwd + "/postinst_called\n"; |
| 86 | if (do_err_script) { |
| 87 | script = "#!/bin/bash\nexit 1"; |
| 88 | } |
| 89 | ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script)); |
| 90 | ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst")); |
| 91 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 92 | ASSERT_EQ(0, System(string("umount -d ") + mountpoint)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 93 | |
| 94 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
| 95 | |
| 96 | // get a loop device we can use for the install device |
| 97 | FILE* find_dev_cmd = popen("losetup -f", "r"); |
| 98 | ASSERT_TRUE(find_dev_cmd); |
| 99 | |
| 100 | char dev[100] = {0}; |
| 101 | size_t r = fread(dev, 1, sizeof(dev), find_dev_cmd); |
| 102 | ASSERT_GT(r, 0); |
| 103 | ASSERT_LT(r, sizeof(dev)); |
| 104 | ASSERT_TRUE(feof(find_dev_cmd)); |
| 105 | fclose(find_dev_cmd); |
| 106 | |
| 107 | // strip trailing newline on dev |
| 108 | if (dev[strlen(dev) - 1] == '\n') |
| 109 | dev[strlen(dev) - 1] = '\0'; |
| 110 | |
| 111 | if (do_losetup) |
| 112 | ASSERT_EQ(0, System(string("losetup ") + dev + " " + cwd + "/image.dat")); |
| 113 | |
| 114 | ActionProcessor processor; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 115 | ObjectFeederAction<InstallPlan> feeder_action; |
| 116 | InstallPlan install_plan; |
| 117 | install_plan.install_path = dev; |
| 118 | feeder_action.set_obj(install_plan); |
| 119 | PostinstallRunnerAction runner_action(true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 120 | BondActions(&feeder_action, &runner_action); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 121 | ObjectCollectorAction<InstallPlan> collector_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 122 | BondActions(&runner_action, &collector_action); |
| 123 | PostinstActionProcessorDelegate delegate; |
| 124 | processor.EnqueueAction(&feeder_action); |
| 125 | processor.EnqueueAction(&runner_action); |
| 126 | processor.EnqueueAction(&collector_action); |
| 127 | processor.set_delegate(&delegate); |
| 128 | processor.StartProcessing(); |
| 129 | ASSERT_FALSE(processor.IsRunning()) |
| 130 | << "Update test to handle non-asynch actions"; |
| 131 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame^] | 132 | EXPECT_TRUE(delegate.code_set_); |
| 133 | EXPECT_EQ(do_losetup && !do_err_script, delegate.code_ == kActionCodeSuccess); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 134 | EXPECT_EQ(do_losetup && !do_err_script, |
| 135 | !collector_action.object().install_path.empty()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 136 | if (do_losetup && !do_err_script) { |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 137 | EXPECT_TRUE(install_plan == collector_action.object()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | struct stat stbuf; |
| 141 | int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf); |
| 142 | if (do_losetup && !do_err_script) |
| 143 | ASSERT_EQ(0, rc); |
| 144 | else |
| 145 | ASSERT_LT(rc, 0); |
| 146 | |
| 147 | if (do_losetup) |
| 148 | ASSERT_EQ(0, System(string("losetup -d ") + dev)); |
| 149 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
| 150 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat")); |
| 151 | } |
| 152 | |
| 153 | // Death tests don't seem to be working on Hardy |
| 154 | TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { |
| 155 | ASSERT_EQ(0, getuid()); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 156 | PostinstallRunnerAction runner_action(true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 157 | ASSERT_DEATH({ runner_action.TerminateProcessing(); }, |
| 158 | "postinstall_runner_action.h:.*] Check failed"); |
| 159 | } |
| 160 | |
| 161 | } // namespace chromeos_update_engine |