Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2010 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 <glib.h> |
| 6 | #include <set> |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 7 | #include <string> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 8 | #include <vector> |
| 9 | #include <gtest/gtest.h> |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 10 | #include "base/string_util.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 11 | #include "update_engine/filesystem_copier_action.h" |
| 12 | #include "update_engine/filesystem_iterator.h" |
| 13 | #include "update_engine/omaha_hash_calculator.h" |
| 14 | #include "update_engine/test_utils.h" |
| 15 | #include "update_engine/utils.h" |
| 16 | |
| 17 | using std::set; |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 18 | using std::string; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 | using std::vector; |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | class FilesystemCopierActionTest : public ::testing::Test { |
| 24 | protected: |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 25 | void DoTest(bool run_out_of_space, |
| 26 | bool terminate_early, |
| 27 | bool use_kernel_partition); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 28 | void SetUp() { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 29 | } |
| 30 | void TearDown() { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 31 | } |
| 32 | }; |
| 33 | |
| 34 | class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate { |
| 35 | public: |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 36 | FilesystemCopierActionTestDelegate() : ran_(false), code_(kActionCodeError) {} |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 37 | void ExitMainLoop() { |
| 38 | while (g_main_context_pending(NULL)) { |
| 39 | g_main_context_iteration(NULL, false); |
| 40 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | g_main_loop_quit(loop_); |
| 42 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 43 | void ProcessingDone(const ActionProcessor* processor, ActionExitCode code) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 44 | ExitMainLoop(); |
| 45 | } |
| 46 | void ProcessingStopped(const ActionProcessor* processor) { |
| 47 | ExitMainLoop(); |
| 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() == FilesystemCopierAction::StaticType()) { |
| 53 | ran_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 54 | code_ = code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | void set_loop(GMainLoop* loop) { |
| 58 | loop_ = loop; |
| 59 | } |
| 60 | bool ran() { return ran_; } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 61 | ActionExitCode code() { return code_; } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 62 | private: |
| 63 | GMainLoop* loop_; |
| 64 | bool ran_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 65 | ActionExitCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 68 | struct StartProcessorCallbackArgs { |
| 69 | ActionProcessor* processor; |
| 70 | FilesystemCopierAction* filesystem_copier_action; |
| 71 | bool terminate_early; |
| 72 | }; |
| 73 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | gboolean StartProcessorInRunLoop(gpointer data) { |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 75 | StartProcessorCallbackArgs* args = |
| 76 | reinterpret_cast<StartProcessorCallbackArgs*>(data); |
| 77 | ActionProcessor* processor = args->processor; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 78 | processor->StartProcessing(); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 79 | if (args->terminate_early) { |
| 80 | EXPECT_TRUE(args->filesystem_copier_action); |
| 81 | args->processor->StopProcessing(); |
| 82 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 83 | return FALSE; |
| 84 | } |
| 85 | |
| 86 | TEST_F(FilesystemCopierActionTest, RunAsRootSimpleTest) { |
| 87 | ASSERT_EQ(0, getuid()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 88 | DoTest(false, false, false); |
| 89 | |
| 90 | DoTest(false, false, true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 91 | } |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 92 | void FilesystemCopierActionTest::DoTest(bool run_out_of_space, |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 93 | bool terminate_early, |
| 94 | bool use_kernel_partition) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 95 | GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 96 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 97 | string a_loop_file; |
| 98 | string b_loop_file; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 99 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 100 | EXPECT_TRUE(utils::MakeTempFile("/tmp/a_loop_file.XXXXXX", |
| 101 | &a_loop_file, |
| 102 | NULL)); |
| 103 | ScopedPathUnlinker a_loop_file_unlinker(a_loop_file); |
| 104 | EXPECT_TRUE(utils::MakeTempFile("/tmp/b_loop_file.XXXXXX", |
| 105 | &b_loop_file, |
| 106 | NULL)); |
| 107 | ScopedPathUnlinker b_loop_file_unlinker(b_loop_file); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 108 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 109 | // Make random data for a, zero filled data for b. |
| 110 | const size_t kLoopFileSize = 10 * 1024 * 1024 + 512; |
| 111 | vector<char> a_loop_data(kLoopFileSize); |
| 112 | FillWithData(&a_loop_data); |
| 113 | vector<char> b_loop_data(run_out_of_space ? |
| 114 | (kLoopFileSize - 1) : |
| 115 | kLoopFileSize, |
| 116 | '\0'); // Fill with 0s |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 117 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 118 | // Write data to disk |
| 119 | EXPECT_TRUE(WriteFileVector(a_loop_file, a_loop_data)); |
| 120 | EXPECT_TRUE(WriteFileVector(b_loop_file, b_loop_data)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 122 | // Make loop devices for the files |
| 123 | string a_dev = GetUnusedLoopDevice(); |
| 124 | EXPECT_FALSE(a_dev.empty()); |
| 125 | EXPECT_EQ(0, System(StringPrintf("losetup %s %s", |
| 126 | a_dev.c_str(), |
| 127 | a_loop_file.c_str()))); |
| 128 | ScopedLoopbackDeviceReleaser a_dev_releaser(a_dev); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 129 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 130 | string b_dev = GetUnusedLoopDevice(); |
| 131 | EXPECT_FALSE(b_dev.empty()); |
| 132 | EXPECT_EQ(0, System(StringPrintf("losetup %s %s", |
| 133 | b_dev.c_str(), |
| 134 | b_loop_file.c_str()))); |
| 135 | ScopedLoopbackDeviceReleaser b_dev_releaser(b_dev); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 136 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 137 | // Set up the action objects |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 138 | InstallPlan install_plan; |
| 139 | install_plan.is_full_update = false; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 140 | if (use_kernel_partition) |
| 141 | install_plan.kernel_install_path = b_dev; |
| 142 | else |
| 143 | install_plan.install_path = b_dev; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 144 | |
| 145 | ActionProcessor processor; |
| 146 | FilesystemCopierActionTestDelegate delegate; |
| 147 | delegate.set_loop(loop); |
| 148 | processor.set_delegate(&delegate); |
| 149 | |
| 150 | ObjectFeederAction<InstallPlan> feeder_action; |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 151 | FilesystemCopierAction copier_action(use_kernel_partition); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 152 | ObjectCollectorAction<InstallPlan> collector_action; |
| 153 | |
| 154 | BondActions(&feeder_action, &copier_action); |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 155 | BondActions(&copier_action, &collector_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 156 | |
| 157 | processor.EnqueueAction(&feeder_action); |
| 158 | processor.EnqueueAction(&copier_action); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 159 | processor.EnqueueAction(&collector_action); |
| 160 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 161 | copier_action.set_copy_source(a_dev); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 162 | feeder_action.set_obj(install_plan); |
| 163 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 164 | StartProcessorCallbackArgs start_callback_args; |
| 165 | start_callback_args.processor = &processor; |
| 166 | start_callback_args.filesystem_copier_action = &copier_action; |
| 167 | start_callback_args.terminate_early = terminate_early; |
| 168 | |
| 169 | g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 170 | g_main_loop_run(loop); |
| 171 | g_main_loop_unref(loop); |
| 172 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 173 | if (!terminate_early) |
| 174 | EXPECT_TRUE(delegate.ran()); |
| 175 | if (run_out_of_space || terminate_early) { |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 176 | EXPECT_EQ(kActionCodeError, delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 177 | return; |
| 178 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 179 | EXPECT_EQ(kActionCodeSuccess, delegate.code()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 180 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 181 | // Make sure everything in the out_image is there |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 182 | vector<char> a_out; |
| 183 | vector<char> b_out; |
| 184 | EXPECT_TRUE(utils::ReadFile(a_dev, &a_out)); |
| 185 | EXPECT_TRUE(utils::ReadFile(b_dev, &b_out)); |
| 186 | EXPECT_TRUE(ExpectVectorsEq(a_out, b_out)); |
| 187 | EXPECT_TRUE(ExpectVectorsEq(a_loop_data, a_out)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 188 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 189 | EXPECT_TRUE(collector_action.object() == install_plan); |
| 190 | } |
| 191 | |
| 192 | class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate { |
| 193 | public: |
| 194 | void ActionCompleted(ActionProcessor* processor, |
| 195 | AbstractAction* action, |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 196 | ActionExitCode code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 197 | if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 198 | ran_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 199 | code_ = code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | GMainLoop *loop_; |
| 203 | bool ran_; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 204 | ActionExitCode code_; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) { |
| 208 | ActionProcessor processor; |
| 209 | FilesystemCopierActionTest2Delegate delegate; |
| 210 | |
| 211 | processor.set_delegate(&delegate); |
| 212 | |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 213 | FilesystemCopierAction copier_action(false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 214 | ObjectCollectorAction<InstallPlan> collector_action; |
| 215 | |
| 216 | BondActions(&copier_action, &collector_action); |
| 217 | |
| 218 | processor.EnqueueAction(&copier_action); |
| 219 | processor.EnqueueAction(&collector_action); |
| 220 | processor.StartProcessing(); |
| 221 | EXPECT_FALSE(processor.IsRunning()); |
| 222 | EXPECT_TRUE(delegate.ran_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 223 | EXPECT_EQ(kActionCodeError, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | TEST_F(FilesystemCopierActionTest, FullUpdateTest) { |
| 227 | ActionProcessor processor; |
| 228 | FilesystemCopierActionTest2Delegate delegate; |
| 229 | |
| 230 | processor.set_delegate(&delegate); |
| 231 | |
| 232 | ObjectFeederAction<InstallPlan> feeder_action; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 233 | const char* kUrl = "http://some/url"; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame^] | 234 | InstallPlan install_plan(true, false, kUrl, 0, "", "", ""); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 235 | feeder_action.set_obj(install_plan); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 236 | FilesystemCopierAction copier_action(false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 237 | ObjectCollectorAction<InstallPlan> collector_action; |
| 238 | |
| 239 | BondActions(&feeder_action, &copier_action); |
| 240 | BondActions(&copier_action, &collector_action); |
| 241 | |
| 242 | processor.EnqueueAction(&feeder_action); |
| 243 | processor.EnqueueAction(&copier_action); |
| 244 | processor.EnqueueAction(&collector_action); |
| 245 | processor.StartProcessing(); |
| 246 | EXPECT_FALSE(processor.IsRunning()); |
| 247 | EXPECT_TRUE(delegate.ran_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 248 | EXPECT_EQ(kActionCodeSuccess, delegate.code_); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 249 | EXPECT_EQ(kUrl, collector_action.object().download_url); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) { |
| 253 | ActionProcessor processor; |
| 254 | FilesystemCopierActionTest2Delegate delegate; |
| 255 | |
| 256 | processor.set_delegate(&delegate); |
| 257 | |
| 258 | ObjectFeederAction<InstallPlan> feeder_action; |
Darin Petkov | 0406e40 | 2010-10-06 21:33:11 -0700 | [diff] [blame^] | 259 | InstallPlan install_plan(false, |
| 260 | false, |
| 261 | "", |
| 262 | 0, |
| 263 | "", |
| 264 | "/no/such/file", |
| 265 | "/no/such/file"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 266 | feeder_action.set_obj(install_plan); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 267 | FilesystemCopierAction copier_action(false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 268 | ObjectCollectorAction<InstallPlan> collector_action; |
| 269 | |
| 270 | BondActions(&copier_action, &collector_action); |
| 271 | |
| 272 | processor.EnqueueAction(&feeder_action); |
| 273 | processor.EnqueueAction(&copier_action); |
| 274 | processor.EnqueueAction(&collector_action); |
| 275 | processor.StartProcessing(); |
| 276 | EXPECT_FALSE(processor.IsRunning()); |
| 277 | EXPECT_TRUE(delegate.ran_); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 278 | EXPECT_EQ(kActionCodeError, delegate.code_); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 281 | TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 282 | ASSERT_EQ(0, getuid()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 283 | DoTest(true, false, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Andrew de los Reyes | c702078 | 2010-04-28 10:46:04 -0700 | [diff] [blame] | 286 | TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 287 | ASSERT_EQ(0, getuid()); |
Andrew de los Reyes | f918517 | 2010-05-03 11:07:05 -0700 | [diff] [blame] | 288 | DoTest(false, true, false); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | } // namespace chromeos_update_engine |