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