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 <glib.h> |
| 6 #include <set> |
| 7 #include <vector> |
| 8 #include <gtest/gtest.h> |
| 9 #include "update_engine/filesystem_copier_action.h" |
| 10 #include "update_engine/filesystem_iterator.h" |
| 11 #include "update_engine/omaha_hash_calculator.h" |
| 12 #include "update_engine/test_utils.h" |
| 13 #include "update_engine/utils.h" |
| 14 |
| 15 using std::set; |
| 16 using std::vector; |
| 17 |
| 18 namespace chromeos_update_engine { |
| 19 |
| 20 class FilesystemCopierActionTest : public ::testing::Test { |
| 21 protected: |
| 22 void DoTest(bool double_copy, bool run_out_of_space); |
| 23 string TestDir() { return "./FilesystemCopierActionTestDir"; } |
| 24 void SetUp() { |
| 25 System(string("mkdir -p ") + TestDir()); |
| 26 } |
| 27 void TearDown() { |
| 28 System(string("rm -rf ") + TestDir()); |
| 29 } |
| 30 }; |
| 31 |
| 32 class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate { |
| 33 public: |
| 34 FilesystemCopierActionTestDelegate() : ran_(false), success_(false) {} |
| 35 void ProcessingDone(const ActionProcessor* processor) { |
| 36 g_main_loop_quit(loop_); |
| 37 } |
| 38 void ActionCompleted(ActionProcessor* processor, |
| 39 AbstractAction* action, |
| 40 bool success) { |
| 41 if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 42 ran_ = true; |
| 43 success_ = success; |
| 44 } |
| 45 } |
| 46 void set_loop(GMainLoop* loop) { |
| 47 loop_ = loop; |
| 48 } |
| 49 bool ran() { return ran_; } |
| 50 bool success() { return success_; } |
| 51 private: |
| 52 GMainLoop* loop_; |
| 53 bool ran_; |
| 54 bool success_; |
| 55 }; |
| 56 |
| 57 gboolean StartProcessorInRunLoop(gpointer data) { |
| 58 ActionProcessor* processor = reinterpret_cast<ActionProcessor*>(data); |
| 59 processor->StartProcessing(); |
| 60 return FALSE; |
| 61 } |
| 62 |
| 63 TEST_F(FilesystemCopierActionTest, RunAsRootSimpleTest) { |
| 64 ASSERT_EQ(0, getuid()); |
| 65 DoTest(false, false); |
| 66 } |
| 67 void FilesystemCopierActionTest::DoTest(bool double_copy, |
| 68 bool run_out_of_space) { |
| 69 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 70 |
| 71 // make two populated ext images, mount them both (one in the other), |
| 72 // and copy to a loop device setup to correspond to another file. |
| 73 |
| 74 const string a_image(TestDir() + "/a_image"); |
| 75 const string b_image(TestDir() + "/b_image"); |
| 76 const string out_image(TestDir() + "/out_image"); |
| 77 |
| 78 vector<string> expected_paths_vector; |
| 79 CreateExtImageAtPath(a_image, &expected_paths_vector); |
| 80 CreateExtImageAtPath(b_image, NULL); |
| 81 |
| 82 // create 5 MiB file |
| 83 ASSERT_EQ(0, System(string("dd if=/dev/zero of=") + out_image |
| 84 + " seek=5242879 bs=1 count=1")); |
| 85 |
| 86 // mount them both |
| 87 System(("mkdir -p " + TestDir() + "/mnt").c_str()); |
| 88 ASSERT_EQ(0, System(string("mount -o loop ") + a_image + " " + |
| 89 TestDir() + "/mnt")); |
| 90 ASSERT_EQ(0, |
| 91 System(string("mount -o loop ") + b_image + " " + |
| 92 TestDir() + "/mnt/some_dir/mnt")); |
| 93 |
| 94 if (run_out_of_space) |
| 95 ASSERT_EQ(0, System(string("dd if=/dev/zero of=") + |
| 96 TestDir() + "/mnt/big_zero bs=5M count=1")); |
| 97 |
| 98 string dev = GetUnusedLoopDevice(); |
| 99 |
| 100 EXPECT_EQ(0, System(string("losetup ") + dev + " " + out_image)); |
| 101 |
| 102 InstallPlan install_plan; |
| 103 install_plan.is_full_update = false; |
| 104 install_plan.install_path = dev; |
| 105 |
| 106 ActionProcessor processor; |
| 107 FilesystemCopierActionTestDelegate delegate; |
| 108 delegate.set_loop(loop); |
| 109 processor.set_delegate(&delegate); |
| 110 |
| 111 ObjectFeederAction<InstallPlan> feeder_action; |
| 112 FilesystemCopierAction copier_action; |
| 113 FilesystemCopierAction copier_action2; |
| 114 ObjectCollectorAction<InstallPlan> collector_action; |
| 115 |
| 116 BondActions(&feeder_action, &copier_action); |
| 117 if (double_copy) { |
| 118 BondActions(&copier_action, &copier_action2); |
| 119 BondActions(&copier_action2, &collector_action); |
| 120 } else { |
| 121 BondActions(&copier_action, &collector_action); |
| 122 } |
| 123 |
| 124 processor.EnqueueAction(&feeder_action); |
| 125 processor.EnqueueAction(&copier_action); |
| 126 if (double_copy) |
| 127 processor.EnqueueAction(&copier_action2); |
| 128 processor.EnqueueAction(&collector_action); |
| 129 |
| 130 copier_action.set_copy_source(TestDir() + "/mnt"); |
| 131 feeder_action.set_obj(install_plan); |
| 132 |
| 133 g_timeout_add(0, &StartProcessorInRunLoop, &processor); |
| 134 g_main_loop_run(loop); |
| 135 g_main_loop_unref(loop); |
| 136 |
| 137 EXPECT_EQ(0, System(string("losetup -d ") + dev)); |
| 138 EXPECT_EQ(0, System(string("umount ") + TestDir() + "/mnt/some_dir/mnt")); |
| 139 EXPECT_EQ(0, System(string("umount ") + TestDir() + "/mnt")); |
| 140 EXPECT_EQ(0, unlink(a_image.c_str())); |
| 141 EXPECT_EQ(0, unlink(b_image.c_str())); |
| 142 |
| 143 EXPECT_TRUE(delegate.ran()); |
| 144 if (run_out_of_space) { |
| 145 EXPECT_FALSE(delegate.success()); |
| 146 EXPECT_EQ(0, unlink(out_image.c_str())); |
| 147 EXPECT_EQ(0, rmdir((TestDir() + "/mnt").c_str())); |
| 148 return; |
| 149 } |
| 150 EXPECT_TRUE(delegate.success()); |
| 151 |
| 152 EXPECT_EQ(0, System(string("mount -o loop ") + out_image + " " + |
| 153 TestDir() + "/mnt")); |
| 154 // Make sure everything in the out_image is there |
| 155 expected_paths_vector.push_back("/update_engine_copy_success"); |
| 156 for (vector<string>::iterator it = expected_paths_vector.begin(); |
| 157 it != expected_paths_vector.end(); ++it) { |
| 158 *it = TestDir() + "/mnt" + *it; |
| 159 } |
| 160 set<string> expected_paths(expected_paths_vector.begin(), |
| 161 expected_paths_vector.end()); |
| 162 VerifyAllPaths(TestDir() + "/mnt", expected_paths); |
| 163 string file_data; |
| 164 EXPECT_TRUE(utils::ReadFileToString(TestDir() + "/mnt/hi", &file_data)); |
| 165 EXPECT_EQ("hi\n", file_data); |
| 166 EXPECT_TRUE(utils::ReadFileToString(TestDir() + "/mnt/hello", &file_data)); |
| 167 EXPECT_EQ("hello\n", file_data); |
| 168 EXPECT_EQ("/some/target", Readlink(TestDir() + "/mnt/sym")); |
| 169 EXPECT_EQ(0, System(string("umount ") + TestDir() + "/mnt")); |
| 170 |
| 171 EXPECT_EQ(0, unlink(out_image.c_str())); |
| 172 EXPECT_EQ(0, rmdir((TestDir() + "/mnt").c_str())); |
| 173 |
| 174 EXPECT_FALSE(copier_action.skipped_copy()); |
| 175 LOG(INFO) << "collected plan:"; |
| 176 collector_action.object().Dump(); |
| 177 LOG(INFO) << "expected plan:"; |
| 178 install_plan.Dump(); |
| 179 EXPECT_TRUE(collector_action.object() == install_plan); |
| 180 } |
| 181 |
| 182 class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate { |
| 183 public: |
| 184 void ActionCompleted(ActionProcessor* processor, |
| 185 AbstractAction* action, |
| 186 bool success) { |
| 187 if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 188 ran_ = true; |
| 189 success_ = success; |
| 190 } |
| 191 } |
| 192 GMainLoop *loop_; |
| 193 bool ran_; |
| 194 bool success_; |
| 195 }; |
| 196 |
| 197 TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) { |
| 198 ActionProcessor processor; |
| 199 FilesystemCopierActionTest2Delegate delegate; |
| 200 |
| 201 processor.set_delegate(&delegate); |
| 202 |
| 203 FilesystemCopierAction copier_action; |
| 204 ObjectCollectorAction<InstallPlan> collector_action; |
| 205 |
| 206 BondActions(&copier_action, &collector_action); |
| 207 |
| 208 processor.EnqueueAction(&copier_action); |
| 209 processor.EnqueueAction(&collector_action); |
| 210 processor.StartProcessing(); |
| 211 EXPECT_FALSE(processor.IsRunning()); |
| 212 EXPECT_TRUE(delegate.ran_); |
| 213 EXPECT_FALSE(delegate.success_); |
| 214 } |
| 215 |
| 216 TEST_F(FilesystemCopierActionTest, FullUpdateTest) { |
| 217 ActionProcessor processor; |
| 218 FilesystemCopierActionTest2Delegate delegate; |
| 219 |
| 220 processor.set_delegate(&delegate); |
| 221 |
| 222 ObjectFeederAction<InstallPlan> feeder_action; |
| 223 InstallPlan install_plan(true, "", "", "", ""); |
| 224 feeder_action.set_obj(install_plan); |
| 225 FilesystemCopierAction copier_action; |
| 226 ObjectCollectorAction<InstallPlan> collector_action; |
| 227 |
| 228 BondActions(&feeder_action, &copier_action); |
| 229 BondActions(&copier_action, &collector_action); |
| 230 |
| 231 processor.EnqueueAction(&feeder_action); |
| 232 processor.EnqueueAction(&copier_action); |
| 233 processor.EnqueueAction(&collector_action); |
| 234 processor.StartProcessing(); |
| 235 EXPECT_FALSE(processor.IsRunning()); |
| 236 EXPECT_TRUE(delegate.ran_); |
| 237 EXPECT_TRUE(delegate.success_); |
| 238 } |
| 239 |
| 240 TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) { |
| 241 ActionProcessor processor; |
| 242 FilesystemCopierActionTest2Delegate delegate; |
| 243 |
| 244 processor.set_delegate(&delegate); |
| 245 |
| 246 ObjectFeederAction<InstallPlan> feeder_action; |
| 247 InstallPlan install_plan(false, "", "", "", "/some/missing/file/path"); |
| 248 feeder_action.set_obj(install_plan); |
| 249 FilesystemCopierAction copier_action; |
| 250 ObjectCollectorAction<InstallPlan> collector_action; |
| 251 |
| 252 BondActions(&copier_action, &collector_action); |
| 253 |
| 254 processor.EnqueueAction(&feeder_action); |
| 255 processor.EnqueueAction(&copier_action); |
| 256 processor.EnqueueAction(&collector_action); |
| 257 processor.StartProcessing(); |
| 258 EXPECT_FALSE(processor.IsRunning()); |
| 259 EXPECT_TRUE(delegate.ran_); |
| 260 EXPECT_FALSE(delegate.success_); |
| 261 } |
| 262 |
| 263 TEST_F(FilesystemCopierActionTest, RunAsRootSkipUpdateTest) { |
| 264 ASSERT_EQ(0, getuid()); |
| 265 DoTest(true, false); |
| 266 } |
| 267 |
| 268 TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) { |
| 269 ASSERT_EQ(0, getuid()); |
| 270 DoTest(false, true); |
| 271 } |
| 272 |
| 273 } // namespace chromeos_update_engine |
OLD | NEW |