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