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); |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 41 | |
| 42 | private: |
| 43 | static const char* kImageMountPointTemplate; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | class PostinstActionProcessorDelegate : public ActionProcessorDelegate { |
| 47 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 48 | PostinstActionProcessorDelegate() |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 49 | : loop_(NULL), |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 50 | code_(kErrorCodeError), |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 51 | code_set_(false) {} |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 52 | void ProcessingDone(const ActionProcessor* processor, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 53 | ErrorCode code) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 54 | ASSERT_TRUE(loop_); |
| 55 | g_main_loop_quit(loop_); |
| 56 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 57 | void ActionCompleted(ActionProcessor* processor, |
| 58 | AbstractAction* action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 59 | ErrorCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 60 | if (action->Type() == PostinstallRunnerAction::StaticType()) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 61 | code_ = code; |
| 62 | code_set_ = true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 65 | GMainLoop* loop_; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 66 | ErrorCode code_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 67 | bool code_set_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | TEST_F(PostinstallRunnerActionTest, RunAsRootSimpleTest) { |
| 71 | ASSERT_EQ(0, getuid()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 72 | DoTest(true, 0, false); |
| 73 | } |
| 74 | |
| 75 | TEST_F(PostinstallRunnerActionTest, RunAsRootPowerwashRequiredTest) { |
| 76 | ASSERT_EQ(0, getuid()); |
| 77 | DoTest(true, 0, true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST_F(PostinstallRunnerActionTest, RunAsRootCantMountTest) { |
| 81 | ASSERT_EQ(0, getuid()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 82 | DoTest(false, 0, true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TEST_F(PostinstallRunnerActionTest, RunAsRootErrScriptTest) { |
| 86 | ASSERT_EQ(0, getuid()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 87 | DoTest(true, 1, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 90 | TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareBErrScriptTest) { |
| 91 | ASSERT_EQ(0, getuid()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 92 | DoTest(true, 3, false); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 95 | TEST_F(PostinstallRunnerActionTest, RunAsRootFirmwareROErrScriptTest) { |
| 96 | ASSERT_EQ(0, getuid()); |
| 97 | DoTest(true, 4, false); |
| 98 | } |
| 99 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 100 | const char* PostinstallRunnerActionTest::kImageMountPointTemplate = |
| 101 | "au_destination-XXXXXX"; |
| 102 | |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 103 | void PostinstallRunnerActionTest::DoTest( |
| 104 | bool do_losetup, |
| 105 | int err_code, |
| 106 | bool powerwash_required) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 107 | ASSERT_EQ(0, getuid()) << "Run me as root. Ideally don't run other tests " |
| 108 | << "as root, tho."; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 109 | // True if the post-install action is expected to succeed. |
| 110 | bool should_succeed = do_losetup && !err_code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 111 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 112 | string orig_cwd; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 113 | { |
| 114 | vector<char> buf(1000); |
| 115 | ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size())); |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 116 | orig_cwd = string(&buf[0], strlen(&buf[0])); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 119 | // Create a unique named working directory and chdir into it. |
| 120 | string cwd; |
| 121 | ASSERT_TRUE(utils::MakeTempDirectory( |
| 122 | orig_cwd + "/postinstall_runner_action_unittest-XXXXXX", |
| 123 | &cwd)); |
| 124 | ASSERT_EQ(0, Chdir(cwd)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 125 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 126 | // Create a 10MiB sparse file to be used as image; format it as ext2. |
| 127 | ASSERT_EQ(0, system( |
| 128 | "dd if=/dev/zero of=image.dat seek=10485759 bs=1 count=1")); |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 129 | ASSERT_EQ(0, system("mkfs.ext2 -F image.dat")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 130 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 131 | // Create a uniquely named image mount point, mount the image. |
| 132 | ASSERT_EQ(0, System(string("mkdir -p ") + kStatefulPartition)); |
| 133 | string mountpoint; |
| 134 | ASSERT_TRUE(utils::MakeTempDirectory( |
| 135 | string(kStatefulPartition) + "/" + kImageMountPointTemplate, |
| 136 | &mountpoint)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 137 | ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint)); |
| 138 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 139 | // Generate a fake postinst script inside the image. |
| 140 | string script = (err_code ? |
| 141 | StringPrintf("#!/bin/bash\nexit %d", err_code) : |
| 142 | StringPrintf("#!/bin/bash\n" |
| 143 | "mount | grep au_postint_mount | grep ext2\n" |
| 144 | "if [ $? -eq 0 ]; then\n" |
| 145 | " touch %s/postinst_called\n" |
| 146 | "fi\n", |
| 147 | cwd.c_str())); |
| 148 | const string script_file_name = mountpoint + "/postinst"; |
| 149 | ASSERT_TRUE(WriteFileString(script_file_name, script)); |
| 150 | ASSERT_EQ(0, System(string("chmod a+x ") + script_file_name)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 151 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 152 | // Unmount image; do not remove the uniquely named directory as it will be |
| 153 | // reused during the test. |
Ben Chan | 77a1eba | 2012-10-07 22:54:55 -0700 | [diff] [blame] | 154 | ASSERT_TRUE(utils::UnmountFilesystem(mountpoint)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 155 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 156 | // get a loop device we can use for the install device |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 157 | string dev = "/dev/null"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 158 | |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 159 | scoped_ptr<ScopedLoopbackDeviceBinder> loop_releaser; |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 160 | if (do_losetup) { |
Don Garrett | 58e8b1f | 2012-01-31 16:38:16 -0800 | [diff] [blame] | 161 | loop_releaser.reset(new ScopedLoopbackDeviceBinder(cwd + "/image.dat", |
| 162 | &dev)); |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 163 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 164 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 165 | // We use a test-specific powerwash marker file, to avoid race conditions. |
| 166 | string powerwash_marker_file = mountpoint + "/factory_install_reset"; |
| 167 | LOG(INFO) << ">>> powerwash_marker_file=" << powerwash_marker_file; |
| 168 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 169 | ActionProcessor processor; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 170 | ObjectFeederAction<InstallPlan> feeder_action; |
| 171 | InstallPlan install_plan; |
| 172 | install_plan.install_path = dev; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 173 | install_plan.powerwash_required = powerwash_required; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 174 | feeder_action.set_obj(install_plan); |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 175 | PostinstallRunnerAction runner_action(powerwash_marker_file.c_str()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 176 | BondActions(&feeder_action, &runner_action); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 177 | ObjectCollectorAction<InstallPlan> collector_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 178 | BondActions(&runner_action, &collector_action); |
| 179 | PostinstActionProcessorDelegate delegate; |
| 180 | processor.EnqueueAction(&feeder_action); |
| 181 | processor.EnqueueAction(&runner_action); |
| 182 | processor.EnqueueAction(&collector_action); |
| 183 | processor.set_delegate(&delegate); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 184 | |
| 185 | GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 186 | delegate.loop_ = loop; |
| 187 | g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 188 | g_main_loop_run(loop); |
| 189 | g_main_loop_unref(loop); |
| 190 | ASSERT_FALSE(processor.IsRunning()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 191 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 192 | EXPECT_TRUE(delegate.code_set_); |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 193 | EXPECT_EQ(should_succeed, delegate.code_ == kErrorCodeSuccess); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 194 | EXPECT_EQ(should_succeed, !collector_action.object().install_path.empty()); |
| 195 | if (should_succeed) |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 196 | EXPECT_TRUE(install_plan == collector_action.object()); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 197 | |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 198 | const FilePath kPowerwashMarkerPath(powerwash_marker_file); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 199 | string actual_cmd; |
| 200 | if (should_succeed && powerwash_required) { |
| 201 | EXPECT_TRUE(file_util::ReadFileToString(kPowerwashMarkerPath, &actual_cmd)); |
| 202 | EXPECT_EQ(kPowerwashCommand, actual_cmd); |
| 203 | } else { |
| 204 | EXPECT_FALSE( |
| 205 | file_util::ReadFileToString(kPowerwashMarkerPath, &actual_cmd)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 206 | } |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 207 | |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 208 | if (err_code == 2) |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 209 | EXPECT_EQ(kErrorCodePostinstallBootedFromFirmwareB, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 210 | |
| 211 | struct stat stbuf; |
| 212 | int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 213 | if (should_succeed) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 214 | ASSERT_EQ(0, rc); |
| 215 | else |
| 216 | ASSERT_LT(rc, 0); |
| 217 | |
Darin Petkov | 56dad72 | 2011-03-03 16:03:56 -0800 | [diff] [blame] | 218 | if (do_losetup) { |
| 219 | loop_releaser.reset(NULL); |
| 220 | } |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 221 | |
| 222 | // Remove unique stateful directory. |
| 223 | ASSERT_EQ(0, System(string("rm -fr ") + mountpoint)); |
| 224 | |
| 225 | // Remove the temporary work directory. |
| 226 | ASSERT_EQ(0, Chdir(orig_cwd)); |
| 227 | ASSERT_EQ(0, System(string("rm -fr ") + cwd)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Death tests don't seem to be working on Hardy |
| 231 | TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { |
| 232 | ASSERT_EQ(0, getuid()); |
Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 233 | PostinstallRunnerAction runner_action; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 234 | ASSERT_DEATH({ runner_action.TerminateProcessing(); }, |
| 235 | "postinstall_runner_action.h:.*] Check failed"); |
| 236 | } |
| 237 | |
| 238 | } // namespace chromeos_update_engine |