OLD | NEW |
(Empty) | |
| 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 |
| 59 const string mountpoint(utils::kStatefulPartition + "/au_destination"); |
| 60 |
| 61 string cwd; |
| 62 { |
| 63 vector<char> buf(1000); |
| 64 ASSERT_EQ(&buf[0], getcwd(&buf[0], buf.size())); |
| 65 cwd = string(&buf[0], strlen(&buf[0])); |
| 66 } |
| 67 |
| 68 // create the au destination, if it doesn't exist |
| 69 ASSERT_EQ(0, System(string("mkdir -p ") + mountpoint)); |
| 70 |
| 71 // create 10MiB sparse file |
| 72 ASSERT_EQ(0, system("dd if=/dev/zero of=image.dat seek=10485759 bs=1 " |
| 73 "count=1")); |
| 74 |
| 75 // format it as ext3 |
| 76 ASSERT_EQ(0, system("mkfs.ext3 -F image.dat")); |
| 77 |
| 78 // mount it |
| 79 ASSERT_EQ(0, System(string("mount -o loop image.dat ") + mountpoint)); |
| 80 |
| 81 // put a postinst script in |
| 82 string script = string("#!/bin/bash\ntouch ") + cwd + "/postinst_called\n"; |
| 83 if (do_err_script) { |
| 84 script = "#!/bin/bash\nexit 1"; |
| 85 } |
| 86 ASSERT_TRUE(WriteFileString(mountpoint + "/postinst", script)); |
| 87 ASSERT_EQ(0, System(string("chmod a+x ") + mountpoint + "/postinst")); |
| 88 |
| 89 ASSERT_EQ(0, System(string("umount ") + mountpoint)); |
| 90 |
| 91 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
| 92 |
| 93 // get a loop device we can use for the install device |
| 94 FILE* find_dev_cmd = popen("losetup -f", "r"); |
| 95 ASSERT_TRUE(find_dev_cmd); |
| 96 |
| 97 char dev[100] = {0}; |
| 98 size_t r = fread(dev, 1, sizeof(dev), find_dev_cmd); |
| 99 ASSERT_GT(r, 0); |
| 100 ASSERT_LT(r, sizeof(dev)); |
| 101 ASSERT_TRUE(feof(find_dev_cmd)); |
| 102 fclose(find_dev_cmd); |
| 103 |
| 104 // strip trailing newline on dev |
| 105 if (dev[strlen(dev) - 1] == '\n') |
| 106 dev[strlen(dev) - 1] = '\0'; |
| 107 |
| 108 if (do_losetup) |
| 109 ASSERT_EQ(0, System(string("losetup ") + dev + " " + cwd + "/image.dat")); |
| 110 |
| 111 ActionProcessor processor; |
| 112 ObjectFeederAction<string> feeder_action; |
| 113 feeder_action.set_obj(dev); |
| 114 PostinstallRunnerAction runner_action; |
| 115 BondActions(&feeder_action, &runner_action); |
| 116 ObjectCollectorAction<string> collector_action; |
| 117 BondActions(&runner_action, &collector_action); |
| 118 PostinstActionProcessorDelegate delegate; |
| 119 processor.EnqueueAction(&feeder_action); |
| 120 processor.EnqueueAction(&runner_action); |
| 121 processor.EnqueueAction(&collector_action); |
| 122 processor.set_delegate(&delegate); |
| 123 processor.StartProcessing(); |
| 124 ASSERT_FALSE(processor.IsRunning()) |
| 125 << "Update test to handle non-asynch actions"; |
| 126 |
| 127 EXPECT_TRUE(delegate.success_set_); |
| 128 EXPECT_EQ(do_losetup && !do_err_script, delegate.success_); |
| 129 EXPECT_EQ(do_losetup && !do_err_script, !collector_action.object().empty()); |
| 130 if (do_losetup && !do_err_script) { |
| 131 EXPECT_EQ(dev, collector_action.object()); |
| 132 } |
| 133 |
| 134 struct stat stbuf; |
| 135 int rc = lstat((string(cwd) + "/postinst_called").c_str(), &stbuf); |
| 136 if (do_losetup && !do_err_script) |
| 137 ASSERT_EQ(0, rc); |
| 138 else |
| 139 ASSERT_LT(rc, 0); |
| 140 |
| 141 if (do_losetup) |
| 142 ASSERT_EQ(0, System(string("losetup -d ") + dev)); |
| 143 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/postinst_called")); |
| 144 ASSERT_EQ(0, System(string("rm -f ") + cwd + "/image.dat")); |
| 145 } |
| 146 |
| 147 // Death tests don't seem to be working on Hardy |
| 148 TEST_F(PostinstallRunnerActionTest, DISABLED_RunAsRootDeathTest) { |
| 149 ASSERT_EQ(0, getuid()); |
| 150 PostinstallRunnerAction runner_action; |
| 151 ASSERT_DEATH({ runner_action.TerminateProcessing(); }, |
| 152 "postinstall_runner_action.h:.*] Check failed"); |
| 153 } |
| 154 |
| 155 } // namespace chromeos_update_engine |
OLD | NEW |