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: |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 34 | void DoTest(bool do_losetup, int err_code); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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()); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 63 | DoTest(true, 0); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) { |
| 67 | ASSERT_EQ(0, getuid()); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 68 | DoTest(false, 0); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) { |
| 72 | ASSERT_EQ(0, getuid()); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 73 | DoTest(true, 1); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 76 | TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) { |
| 77 | ASSERT_EQ(0, getuid()); |
Andrew de los Reyes | fe57d54 | 2011-06-07 09:00:36 -0700 | [diff] [blame^] | 78 | DoTest(true, 3); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void PostinstallRunnerActionTest::DoTest(bool do_losetup, int err_code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 82 | ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests " |
| 83 | << "as root, tho."; |
| 84 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 85 | const string mountpoint(string(utils::kStatefulPartition) + |
| 86 | "/au_destination"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 87 | |
| 88 | string cwd; |
| 89 | { |
| 90 | vector<char> buf(1000); |
| 91 | ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size())); |
| 92 | cwd = string(&buf[0], strlen(&buf[0])); |
| 93 | } |
| 94 | |
| 95 | // create the au destination, if it doesn't exist |
| 96 | ASSERT_EQ(0, System(string("mkdir -p ") + mountpoint)); |
| 97 | |
| 98 | // create 10MiB sparse file |
| 99 | ASSERT_EQ(0, system("dd if=/dev/zero of=image.dat seek=10485759 bs=1 " |
| 100 | "count=1")); |
| 101 | |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 102 | // format it as ext2 |
| 103 | ASSERT_EQ(0, system("mkfs.ext2 -F image.dat")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 104 | |
| 105 | // mount it |
| 106 | ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint)); |
| 107 | |
| 108 | // put a postinst script in |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 109 | string script = StringPrintf("#!/bin/bash\n" |
| 110 | "mount | grep au_postint_mount | grep ext2\n" |
| 111 | "if [ $? -eq 0 ]; then\n" |
| 112 | " touch %s/postinst_called\n" |
| 113 | "fi\n", |
| 114 | cwd.c_str()); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 115 | if (err_code) { |
| 116 | script = StringPrintf("#!/bin/bash\nexit %d", err_code); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 117 | } |
| 118 | ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script)); |
| 119 | ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst")); |
| 120 | |
Andrew de los Reyes | b10320d | 2010-03-31 16:44:44 -0700 | [diff] [blame] | 121 | ASSERT_EQ(0, System(string("umount -d ") + mountpoint)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 122 | |
| 123 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
| 124 | |
| 125 | // get a loop device we can use for the install device |
| 126 | FILE* find_dev_cmd = popen("losetup -f", "r"); |
| 127 | ASSERT_TRUE(find_dev_cmd); |
| 128 | |
| 129 | char dev[100] = {0}; |
| 130 | size_t r = fread(dev, 1, sizeof(dev), find_dev_cmd); |
| 131 | ASSERT_GT(r, 0); |
| 132 | ASSERT_LT(r, sizeof(dev)); |
| 133 | ASSERT_TRUE(feof(find_dev_cmd)); |
| 134 | fclose(find_dev_cmd); |
| 135 | |
| 136 | // strip trailing newline on dev |
| 137 | if (dev[strlen(dev) - 1] == '\n') |
| 138 | dev[strlen(dev) - 1] = '\0'; |
| 139 | |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 140 | scoped_ptr<ScopedLoopbackDeviceReleaser> loop_releaser; |
| 141 | if (do_losetup) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 142 | ASSERT_EQ(0, System(string("losetup ") + dev + " " + cwd + "/image.dat")); |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 143 | loop_releaser.reset(new ScopedLoopbackDeviceReleaser(dev)); |
| 144 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 145 | |
| 146 | ActionProcessor processor; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 147 | ObjectFeederAction<InstallPlan> feeder_action; |
| 148 | InstallPlan install_plan; |
| 149 | install_plan.install_path = dev; |
| 150 | feeder_action.set_obj(install_plan); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 151 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 152 | BondActions(&feeder_action, &runner_action); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 153 | ObjectCollectorAction<InstallPlan> collector_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 154 | BondActions(&runner_action, &collector_action); |
| 155 | PostinstActionProcessorDelegate delegate; |
| 156 | processor.EnqueueAction(&feeder_action); |
| 157 | processor.EnqueueAction(&runner_action); |
| 158 | processor.EnqueueAction(&collector_action); |
| 159 | processor.set_delegate(&delegate); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 160 | |
| 161 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 162 | delegate.loop_ = loop; |
| 163 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 164 | g_main_loop_run(loop); |
| 165 | g_main_loop_unref(loop); |
| 166 | ASSERT_FALSE(processor.IsRunning()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 167 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 168 | EXPECT_TRUE(delegate.code_set_); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 169 | EXPECT_EQ(do_losetup && !err_code, delegate.code_ == kActionCodeSuccess); |
| 170 | EXPECT_EQ(do_losetup && !err_code, |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 171 | !collector_action.object().install_path.empty()); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 172 | if (do_losetup && !err_code) { |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 173 | EXPECT_TRUE(install_plan == collector_action.object()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 174 | } |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 175 | if (err_code == 2) |
| 176 | EXPECT_EQ(kActionCodePostinstallBootedFromFirmwareB, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 177 | |
| 178 | struct stat stbuf; |
| 179 | int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 180 | if (do_losetup && !err_code) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 181 | ASSERT_EQ(0, rc); |
| 182 | else |
| 183 | ASSERT_LT(rc, 0); |
| 184 | |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 185 | if (do_losetup) { |
| 186 | loop_releaser.reset(NULL); |
| 187 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 188 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
| 189 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat")); |
| 190 | } |
| 191 | |
| 192 | // Death tests don't seem to be working on Hardy |
| 193 | TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { |
| 194 | ASSERT_EQ(0, getuid()); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 195 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 196 | ASSERT_DEATH({ runner_action.TerminateProcessing(); }, |
| 197 | "postinstall_runner_action.h:.*] Check failed"); |
| 198 | } |
| 199 | |
| 200 | } // namespace chromeos_update_engine |