| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <glib.h> | 5 #include <glib.h> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 #include <gtest/gtest.h> | 9 #include <gtest/gtest.h> |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 bool terminate_early, | 26 bool terminate_early, |
| 27 bool use_kernel_partition); | 27 bool use_kernel_partition); |
| 28 void SetUp() { | 28 void SetUp() { |
| 29 } | 29 } |
| 30 void TearDown() { | 30 void TearDown() { |
| 31 } | 31 } |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate { | 34 class FilesystemCopierActionTestDelegate : public ActionProcessorDelegate { |
| 35 public: | 35 public: |
| 36 FilesystemCopierActionTestDelegate() : ran_(false), success_(false) {} | 36 FilesystemCopierActionTestDelegate() : ran_(false), code_(kActionCodeError) {} |
| 37 void ExitMainLoop() { | 37 void ExitMainLoop() { |
| 38 while (g_main_context_pending(NULL)) { | 38 while (g_main_context_pending(NULL)) { |
| 39 g_main_context_iteration(NULL, false); | 39 g_main_context_iteration(NULL, false); |
| 40 } | 40 } |
| 41 g_main_loop_quit(loop_); | 41 g_main_loop_quit(loop_); |
| 42 } | 42 } |
| 43 void ProcessingDone(const ActionProcessor* processor, bool success) { | 43 void ProcessingDone(const ActionProcessor* processor, ActionExitCode code) { |
| 44 ExitMainLoop(); | 44 ExitMainLoop(); |
| 45 } | 45 } |
| 46 void ProcessingStopped(const ActionProcessor* processor) { | 46 void ProcessingStopped(const ActionProcessor* processor) { |
| 47 ExitMainLoop(); | 47 ExitMainLoop(); |
| 48 } | 48 } |
| 49 void ActionCompleted(ActionProcessor* processor, | 49 void ActionCompleted(ActionProcessor* processor, |
| 50 AbstractAction* action, | 50 AbstractAction* action, |
| 51 bool success) { | 51 ActionExitCode code) { |
| 52 if (action->Type() == FilesystemCopierAction::StaticType()) { | 52 if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 53 ran_ = true; | 53 ran_ = true; |
| 54 success_ = success; | 54 code_ = code; |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 void set_loop(GMainLoop* loop) { | 57 void set_loop(GMainLoop* loop) { |
| 58 loop_ = loop; | 58 loop_ = loop; |
| 59 } | 59 } |
| 60 bool ran() { return ran_; } | 60 bool ran() { return ran_; } |
| 61 bool success() { return success_; } | 61 ActionExitCode code() { return code_; } |
| 62 private: | 62 private: |
| 63 GMainLoop* loop_; | 63 GMainLoop* loop_; |
| 64 bool ran_; | 64 bool ran_; |
| 65 bool success_; | 65 ActionExitCode code_; |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 struct StartProcessorCallbackArgs { | 68 struct StartProcessorCallbackArgs { |
| 69 ActionProcessor* processor; | 69 ActionProcessor* processor; |
| 70 FilesystemCopierAction* filesystem_copier_action; | 70 FilesystemCopierAction* filesystem_copier_action; |
| 71 bool terminate_early; | 71 bool terminate_early; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 gboolean StartProcessorInRunLoop(gpointer data) { | 74 gboolean StartProcessorInRunLoop(gpointer data) { |
| 75 StartProcessorCallbackArgs* args = | 75 StartProcessorCallbackArgs* args = |
| (...skipping 13 matching lines...) Expand all Loading... |
| 89 | 89 |
| 90 DoTest(false, false, true); | 90 DoTest(false, false, true); |
| 91 } | 91 } |
| 92 void FilesystemCopierActionTest::DoTest(bool run_out_of_space, | 92 void FilesystemCopierActionTest::DoTest(bool run_out_of_space, |
| 93 bool terminate_early, | 93 bool terminate_early, |
| 94 bool use_kernel_partition) { | 94 bool use_kernel_partition) { |
| 95 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 95 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 96 | 96 |
| 97 string a_loop_file; | 97 string a_loop_file; |
| 98 string b_loop_file; | 98 string b_loop_file; |
| 99 | 99 |
| 100 EXPECT_TRUE(utils::MakeTempFile("/tmp/a_loop_file.XXXXXX", | 100 EXPECT_TRUE(utils::MakeTempFile("/tmp/a_loop_file.XXXXXX", |
| 101 &a_loop_file, | 101 &a_loop_file, |
| 102 NULL)); | 102 NULL)); |
| 103 ScopedPathUnlinker a_loop_file_unlinker(a_loop_file); | 103 ScopedPathUnlinker a_loop_file_unlinker(a_loop_file); |
| 104 EXPECT_TRUE(utils::MakeTempFile("/tmp/b_loop_file.XXXXXX", | 104 EXPECT_TRUE(utils::MakeTempFile("/tmp/b_loop_file.XXXXXX", |
| 105 &b_loop_file, | 105 &b_loop_file, |
| 106 NULL)); | 106 NULL)); |
| 107 ScopedPathUnlinker b_loop_file_unlinker(b_loop_file); | 107 ScopedPathUnlinker b_loop_file_unlinker(b_loop_file); |
| 108 | 108 |
| 109 // Make random data for a, zero filled data for b. | 109 // Make random data for a, zero filled data for b. |
| 110 const size_t kLoopFileSize = 10 * 1024 * 1024 + 512; | 110 const size_t kLoopFileSize = 10 * 1024 * 1024 + 512; |
| 111 vector<char> a_loop_data(kLoopFileSize); | 111 vector<char> a_loop_data(kLoopFileSize); |
| 112 FillWithData(&a_loop_data); | 112 FillWithData(&a_loop_data); |
| 113 vector<char> b_loop_data(run_out_of_space ? | 113 vector<char> b_loop_data(run_out_of_space ? |
| 114 (kLoopFileSize - 1) : | 114 (kLoopFileSize - 1) : |
| 115 kLoopFileSize, | 115 kLoopFileSize, |
| 116 '\0'); // Fill with 0s | 116 '\0'); // Fill with 0s |
| 117 | 117 |
| 118 // Write data to disk | 118 // Write data to disk |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 start_callback_args.filesystem_copier_action = &copier_action; | 166 start_callback_args.filesystem_copier_action = &copier_action; |
| 167 start_callback_args.terminate_early = terminate_early; | 167 start_callback_args.terminate_early = terminate_early; |
| 168 | 168 |
| 169 g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args); | 169 g_timeout_add(0, &StartProcessorInRunLoop, &start_callback_args); |
| 170 g_main_loop_run(loop); | 170 g_main_loop_run(loop); |
| 171 g_main_loop_unref(loop); | 171 g_main_loop_unref(loop); |
| 172 | 172 |
| 173 if (!terminate_early) | 173 if (!terminate_early) |
| 174 EXPECT_TRUE(delegate.ran()); | 174 EXPECT_TRUE(delegate.ran()); |
| 175 if (run_out_of_space || terminate_early) { | 175 if (run_out_of_space || terminate_early) { |
| 176 EXPECT_FALSE(delegate.success()); | 176 EXPECT_EQ(kActionCodeError, delegate.code()); |
| 177 return; | 177 return; |
| 178 } | 178 } |
| 179 EXPECT_TRUE(delegate.success()); | 179 EXPECT_EQ(kActionCodeSuccess, delegate.code()); |
| 180 | 180 |
| 181 // Make sure everything in the out_image is there | 181 // Make sure everything in the out_image is there |
| 182 vector<char> a_out; | 182 vector<char> a_out; |
| 183 vector<char> b_out; | 183 vector<char> b_out; |
| 184 EXPECT_TRUE(utils::ReadFile(a_dev, &a_out)); | 184 EXPECT_TRUE(utils::ReadFile(a_dev, &a_out)); |
| 185 EXPECT_TRUE(utils::ReadFile(b_dev, &b_out)); | 185 EXPECT_TRUE(utils::ReadFile(b_dev, &b_out)); |
| 186 EXPECT_TRUE(ExpectVectorsEq(a_out, b_out)); | 186 EXPECT_TRUE(ExpectVectorsEq(a_out, b_out)); |
| 187 EXPECT_TRUE(ExpectVectorsEq(a_loop_data, a_out)); | 187 EXPECT_TRUE(ExpectVectorsEq(a_loop_data, a_out)); |
| 188 | 188 |
| 189 EXPECT_TRUE(collector_action.object() == install_plan); | 189 EXPECT_TRUE(collector_action.object() == install_plan); |
| 190 } | 190 } |
| 191 | 191 |
| 192 class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate { | 192 class FilesystemCopierActionTest2Delegate : public ActionProcessorDelegate { |
| 193 public: | 193 public: |
| 194 void ActionCompleted(ActionProcessor* processor, | 194 void ActionCompleted(ActionProcessor* processor, |
| 195 AbstractAction* action, | 195 AbstractAction* action, |
| 196 bool success) { | 196 ActionExitCode code) { |
| 197 if (action->Type() == FilesystemCopierAction::StaticType()) { | 197 if (action->Type() == FilesystemCopierAction::StaticType()) { |
| 198 ran_ = true; | 198 ran_ = true; |
| 199 success_ = success; | 199 code_ = code; |
| 200 } | 200 } |
| 201 } | 201 } |
| 202 GMainLoop *loop_; | 202 GMainLoop *loop_; |
| 203 bool ran_; | 203 bool ran_; |
| 204 bool success_; | 204 ActionExitCode code_; |
| 205 }; | 205 }; |
| 206 | 206 |
| 207 TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) { | 207 TEST_F(FilesystemCopierActionTest, MissingInputObjectTest) { |
| 208 ActionProcessor processor; | 208 ActionProcessor processor; |
| 209 FilesystemCopierActionTest2Delegate delegate; | 209 FilesystemCopierActionTest2Delegate delegate; |
| 210 | 210 |
| 211 processor.set_delegate(&delegate); | 211 processor.set_delegate(&delegate); |
| 212 | 212 |
| 213 FilesystemCopierAction copier_action(false); | 213 FilesystemCopierAction copier_action(false); |
| 214 ObjectCollectorAction<InstallPlan> collector_action; | 214 ObjectCollectorAction<InstallPlan> collector_action; |
| 215 | 215 |
| 216 BondActions(&copier_action, &collector_action); | 216 BondActions(&copier_action, &collector_action); |
| 217 | 217 |
| 218 processor.EnqueueAction(&copier_action); | 218 processor.EnqueueAction(&copier_action); |
| 219 processor.EnqueueAction(&collector_action); | 219 processor.EnqueueAction(&collector_action); |
| 220 processor.StartProcessing(); | 220 processor.StartProcessing(); |
| 221 EXPECT_FALSE(processor.IsRunning()); | 221 EXPECT_FALSE(processor.IsRunning()); |
| 222 EXPECT_TRUE(delegate.ran_); | 222 EXPECT_TRUE(delegate.ran_); |
| 223 EXPECT_FALSE(delegate.success_); | 223 EXPECT_EQ(kActionCodeError, delegate.code_); |
| 224 } | 224 } |
| 225 | 225 |
| 226 TEST_F(FilesystemCopierActionTest, FullUpdateTest) { | 226 TEST_F(FilesystemCopierActionTest, FullUpdateTest) { |
| 227 ActionProcessor processor; | 227 ActionProcessor processor; |
| 228 FilesystemCopierActionTest2Delegate delegate; | 228 FilesystemCopierActionTest2Delegate delegate; |
| 229 | 229 |
| 230 processor.set_delegate(&delegate); | 230 processor.set_delegate(&delegate); |
| 231 | 231 |
| 232 ObjectFeederAction<InstallPlan> feeder_action; | 232 ObjectFeederAction<InstallPlan> feeder_action; |
| 233 const char* kUrl = "http://some/url"; | 233 const char* kUrl = "http://some/url"; |
| 234 InstallPlan install_plan(true, kUrl, 0, "", "", ""); | 234 InstallPlan install_plan(true, kUrl, 0, "", "", ""); |
| 235 feeder_action.set_obj(install_plan); | 235 feeder_action.set_obj(install_plan); |
| 236 FilesystemCopierAction copier_action(false); | 236 FilesystemCopierAction copier_action(false); |
| 237 ObjectCollectorAction<InstallPlan> collector_action; | 237 ObjectCollectorAction<InstallPlan> collector_action; |
| 238 | 238 |
| 239 BondActions(&feeder_action, &copier_action); | 239 BondActions(&feeder_action, &copier_action); |
| 240 BondActions(&copier_action, &collector_action); | 240 BondActions(&copier_action, &collector_action); |
| 241 | 241 |
| 242 processor.EnqueueAction(&feeder_action); | 242 processor.EnqueueAction(&feeder_action); |
| 243 processor.EnqueueAction(&copier_action); | 243 processor.EnqueueAction(&copier_action); |
| 244 processor.EnqueueAction(&collector_action); | 244 processor.EnqueueAction(&collector_action); |
| 245 processor.StartProcessing(); | 245 processor.StartProcessing(); |
| 246 EXPECT_FALSE(processor.IsRunning()); | 246 EXPECT_FALSE(processor.IsRunning()); |
| 247 EXPECT_TRUE(delegate.ran_); | 247 EXPECT_TRUE(delegate.ran_); |
| 248 EXPECT_TRUE(delegate.success_); | 248 EXPECT_EQ(kActionCodeSuccess, delegate.code_); |
| 249 EXPECT_EQ(kUrl, collector_action.object().download_url); | 249 EXPECT_EQ(kUrl, collector_action.object().download_url); |
| 250 } | 250 } |
| 251 | 251 |
| 252 TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) { | 252 TEST_F(FilesystemCopierActionTest, NonExistentDriveTest) { |
| 253 ActionProcessor processor; | 253 ActionProcessor processor; |
| 254 FilesystemCopierActionTest2Delegate delegate; | 254 FilesystemCopierActionTest2Delegate delegate; |
| 255 | 255 |
| 256 processor.set_delegate(&delegate); | 256 processor.set_delegate(&delegate); |
| 257 | 257 |
| 258 ObjectFeederAction<InstallPlan> feeder_action; | 258 ObjectFeederAction<InstallPlan> feeder_action; |
| 259 InstallPlan install_plan(false, "", 0, "", "/no/such/file", "/no/such/file"); | 259 InstallPlan install_plan(false, "", 0, "", "/no/such/file", "/no/such/file"); |
| 260 feeder_action.set_obj(install_plan); | 260 feeder_action.set_obj(install_plan); |
| 261 FilesystemCopierAction copier_action(false); | 261 FilesystemCopierAction copier_action(false); |
| 262 ObjectCollectorAction<InstallPlan> collector_action; | 262 ObjectCollectorAction<InstallPlan> collector_action; |
| 263 | 263 |
| 264 BondActions(&copier_action, &collector_action); | 264 BondActions(&copier_action, &collector_action); |
| 265 | 265 |
| 266 processor.EnqueueAction(&feeder_action); | 266 processor.EnqueueAction(&feeder_action); |
| 267 processor.EnqueueAction(&copier_action); | 267 processor.EnqueueAction(&copier_action); |
| 268 processor.EnqueueAction(&collector_action); | 268 processor.EnqueueAction(&collector_action); |
| 269 processor.StartProcessing(); | 269 processor.StartProcessing(); |
| 270 EXPECT_FALSE(processor.IsRunning()); | 270 EXPECT_FALSE(processor.IsRunning()); |
| 271 EXPECT_TRUE(delegate.ran_); | 271 EXPECT_TRUE(delegate.ran_); |
| 272 EXPECT_FALSE(delegate.success_); | 272 EXPECT_EQ(kActionCodeError, delegate.code_); |
| 273 } | 273 } |
| 274 | 274 |
| 275 TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) { | 275 TEST_F(FilesystemCopierActionTest, RunAsRootNoSpaceTest) { |
| 276 ASSERT_EQ(0, getuid()); | 276 ASSERT_EQ(0, getuid()); |
| 277 DoTest(true, false, false); | 277 DoTest(true, false, false); |
| 278 } | 278 } |
| 279 | 279 |
| 280 TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) { | 280 TEST_F(FilesystemCopierActionTest, RunAsRootTerminateEarlyTest) { |
| 281 ASSERT_EQ(0, getuid()); | 281 ASSERT_EQ(0, getuid()); |
| 282 DoTest(false, true, false); | 282 DoTest(false, true, false); |
| 283 } | 283 } |
| 284 | 284 |
| 285 } // namespace chromeos_update_engine | 285 } // namespace chromeos_update_engine |
| OLD | NEW |