Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 5 | #include "update_engine/postinstall_runner_action.h" |
| 6 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 7 | #include <sys/stat.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <unistd.h> |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 10 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 13 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 14 | #include <base/file_util.h> |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 15 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 16 | #include <base/stringprintf.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 18 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 19 | #include "update_engine/constants.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 20 | #include "update_engine/test_utils.h" |
| 21 | #include "update_engine/utils.h" |
| 22 | |
| 23 | using std::string; |
| 24 | using std::vector; |
| 25 | |
| 26 | namespace chromeos_update_engine { |
| 27 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 28 | namespace { |
| 29 | gboolean StartProcessorInRunLoop(gpointer data) { |
| 30 | ActionProcessor *processor = reinterpret_cast<ActionProcessor*>(data); |
| 31 | processor->StartProcessing(); |
| 32 | return FALSE; |
| 33 | } |
| 34 | } // namespace |
| 35 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 36 | class PostinstallRunnerActionTest : public ::testing::Test { |
| 37 | public: |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 38 | // DoTest with various combinations of do_losetup, err_code and |
| 39 | // powerwash_required. |
| 40 | void DoTest(bool do_losetup, int err_code, bool powerwash_required); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class PostinstActionProcessorDelegate : public ActionProcessorDelegate { |
| 44 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 45 | PostinstActionProcessorDelegate() |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 46 | : loop_(NULL), |
| 47 | code_(kActionCodeError), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 48 | code_set_(false) {} |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 49 | void ProcessingDone(const ActionProcessor* processor, |
| 50 | ActionExitCode code) { |
| 51 | ASSERT_TRUE(loop_); |
| 52 | g_main_loop_quit(loop_); |
| 53 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 54 | void ActionCompleted(ActionProcessor* processor, |
| 55 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 56 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 57 | if (action->Type() == PostinstallRunnerAction::StaticType()) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 58 | code_ = code; |
| 59 | code_set_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 62 | GMainLoop* loop_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 63 | ActionExitCode code_; |
| 64 | bool code_set_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) { |
| 68 | ASSERT_EQ(0, getuid()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 69 | DoTest(true, 0, false); |
| 70 | } |
| 71 | |
| 72 | TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) { |
| 73 | ASSERT_EQ(0, getuid()); |
| 74 | DoTest(true, 0, true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) { |
| 78 | ASSERT_EQ(0, getuid()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 79 | DoTest(false, 0, true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) { |
| 83 | ASSERT_EQ(0, getuid()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 84 | DoTest(true, 1, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 87 | TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) { |
| 88 | ASSERT_EQ(0, getuid()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 89 | DoTest(true, 3, false); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 92 | void PostinstallRunnerActionTest::DoTest( |
| 93 | bool do_losetup, |
| 94 | int err_code, |
| 95 | bool powerwash_required) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 96 | ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests " |
| 97 | << "as root, tho."; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 98 | // True if the post-install action is expected to succeed. |
| 99 | bool should_succeed = do_losetup && !err_code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 100 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 101 | const string mountpoint(string(utils::kStatefulPartition) + |
| 102 | "/au_destination"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 103 | |
| 104 | string cwd; |
| 105 | { |
| 106 | vector<char> buf(1000); |
| 107 | ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size())); |
| 108 | cwd = string(&buf[0], strlen(&buf[0])); |
| 109 | } |
| 110 | |
| 111 | // create the au destination, if it doesn't exist |
| 112 | ASSERT_EQ(0, System(string("mkdir -p ") + mountpoint)); |
| 113 | |
| 114 | // create 10MiB sparse file |
| 115 | ASSERT_EQ(0, system("dd if=/dev/zero of=image.dat seek=10485759 bs=1 " |
| 116 | "count=1")); |
| 117 | |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 118 | // format it as ext2 |
| 119 | ASSERT_EQ(0, system("mkfs.ext2 -F image.dat")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 120 | |
| 121 | // mount it |
| 122 | ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint)); |
| 123 | |
| 124 | // put a postinst script in |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 125 | string script = StringPrintf("#!/bin/bash\n" |
| 126 | "mount | grep au_postint_mount | grep ext2\n" |
| 127 | "if [ $? -eq 0 ]; then\n" |
| 128 | " touch %s/postinst_called\n" |
| 129 | "fi\n", |
| 130 | cwd.c_str()); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 131 | if (err_code) { |
| 132 | script = StringPrintf("#!/bin/bash\nexit %d", err_code); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 133 | } |
| 134 | ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script)); |
| 135 | ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst")); |
| 136 | |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 137 | ASSERT_TRUE(utils::UnmountFilesystem(mountpoint)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 138 | |
| 139 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
| 140 | |
| 141 | // get a loop device we can use for the install device |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 142 | string dev = "/dev/null"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 143 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 144 | scoped_ptr<ScopedLoopbackDeviceBinder> loop_releaser; |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 145 | if (do_losetup) { |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 146 | loop_releaser.reset(new ScopedLoopbackDeviceBinder(cwd + "/image.dat", |
| 147 | &dev)); |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 148 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 149 | |
| 150 | ActionProcessor processor; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 151 | ObjectFeederAction<InstallPlan> feeder_action; |
| 152 | InstallPlan install_plan; |
| 153 | install_plan.install_path = dev; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 154 | install_plan.powerwash_required = powerwash_required; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 155 | feeder_action.set_obj(install_plan); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 156 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 157 | BondActions(&feeder_action, &runner_action); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 158 | ObjectCollectorAction<InstallPlan> collector_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 159 | BondActions(&runner_action, &collector_action); |
| 160 | PostinstActionProcessorDelegate delegate; |
| 161 | processor.EnqueueAction(&feeder_action); |
| 162 | processor.EnqueueAction(&runner_action); |
| 163 | processor.EnqueueAction(&collector_action); |
| 164 | processor.set_delegate(&delegate); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 165 | |
| 166 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 167 | delegate.loop_ = loop; |
| 168 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 169 | g_main_loop_run(loop); |
| 170 | g_main_loop_unref(loop); |
| 171 | ASSERT_FALSE(processor.IsRunning()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 172 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 173 | EXPECT_TRUE(delegate.code_set_); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 174 | EXPECT_EQ(should_succeed, delegate.code_ == kActionCodeSuccess); |
| 175 | EXPECT_EQ(should_succeed, !collector_action.object().install_path.empty()); |
| 176 | if (should_succeed) |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 177 | EXPECT_TRUE(install_plan == collector_action.object()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 178 | |
| 179 | const FilePath kPowerwashMarkerPath(kPowerwashMarkerFile); |
| 180 | string actual_cmd; |
| 181 | if (should_succeed && powerwash_required) { |
| 182 | EXPECT_TRUE(file_util::ReadFileToString(kPowerwashMarkerPath, &actual_cmd)); |
| 183 | EXPECT_EQ(kPowerwashCommand, actual_cmd); |
| 184 | } else { |
| 185 | EXPECT_FALSE( |
| 186 | file_util::ReadFileToString(kPowerwashMarkerPath, &actual_cmd)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 187 | } |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 188 | |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 189 | if (err_code == 2) |
| 190 | EXPECT_EQ(kActionCodePostinstallBootedFromFirmwareB, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 191 | |
| 192 | struct stat stbuf; |
| 193 | int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 194 | if (should_succeed) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 195 | ASSERT_EQ(0, rc); |
| 196 | else |
| 197 | ASSERT_LT(rc, 0); |
| 198 | |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 199 | if (do_losetup) { |
| 200 | loop_releaser.reset(NULL); |
| 201 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 202 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
| 203 | ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat")); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 204 | utils::DeletePowerwashMarkerFile(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Death tests don't seem to be working on Hardy |
| 208 | TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { |
| 209 | ASSERT_EQ(0, getuid()); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 210 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 211 | ASSERT_DEATH({ runner_action.TerminateProcessing(); }, |
| 212 | "postinstall_runner_action.h:.*] Check failed"); |
| 213 | } |
| 214 | |
| 215 | } // namespace chromeos_update_engine |