| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2012 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 16 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/cros/p2p_manager.h" | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 18 |  | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 19 | #include <dirent.h> | 
| Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 20 | #include <fcntl.h> | 
|  | 21 | #include <sys/stat.h> | 
| Alex Deymo | 6f20dd4 | 2015-08-18 16:42:46 -0700 | [diff] [blame] | 22 | #include <sys/types.h> | 
|  | 23 | #include <sys/xattr.h> | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 24 | #include <unistd.h> | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 25 |  | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 26 | #include <memory> | 
| Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 27 | #include <string> | 
|  | 28 | #include <vector> | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 29 |  | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 30 | #include <base/bind.h> | 
|  | 31 | #include <base/callback.h> | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 32 | #include <base/files/file_util.h> | 
| Qijiang Fan | b0b6cc2 | 2020-10-15 21:54:11 +0900 | [diff] [blame] | 33 | #if BASE_VER < 780000  // Android | 
| Alex Deymo | 0b3db6b | 2015-08-10 15:19:37 -0700 | [diff] [blame] | 34 | #include <base/message_loop/message_loop.h> | 
| Qijiang Fan | b0b6cc2 | 2020-10-15 21:54:11 +0900 | [diff] [blame] | 35 | #endif  // BASE_VER < 780000 | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 36 | #include <base/strings/stringprintf.h> | 
| Qijiang Fan | b0b6cc2 | 2020-10-15 21:54:11 +0900 | [diff] [blame] | 37 | #if BASE_VER >= 780000  // CrOS | 
|  | 38 | #include <base/task/single_thread_task_executor.h> | 
|  | 39 | #endif  // BASE_VER >= 780000 | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 40 | #include <brillo/asynchronous_signal_handler.h> | 
|  | 41 | #include <brillo/message_loops/base_message_loop.h> | 
|  | 42 | #include <brillo/message_loops/message_loop.h> | 
|  | 43 | #include <brillo/message_loops/message_loop_utils.h> | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 44 | #include <gmock/gmock.h> | 
|  | 45 | #include <gtest/gtest.h> | 
| David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 46 | #include <policy/libpolicy.h> | 
|  | 47 | #include <policy/mock_device_policy.h> | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 48 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 49 | #include "update_engine/common/prefs.h" | 
| Sen Jiang | ab6bda9 | 2019-01-09 16:33:51 -0800 | [diff] [blame] | 50 | #include "update_engine/common/subprocess.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 51 | #include "update_engine/common/test_utils.h" | 
|  | 52 | #include "update_engine/common/utils.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 53 | #include "update_engine/cros/fake_p2p_manager_configuration.h" | 
| Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 54 | #include "update_engine/cros/fake_system_state.h" | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 55 | #include "update_engine/update_manager/fake_update_manager.h" | 
|  | 56 | #include "update_engine/update_manager/mock_policy.h" | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 57 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 58 | using base::TimeDelta; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 59 | using brillo::MessageLoop; | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 60 | using std::string; | 
| Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 61 | using std::unique_ptr; | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 62 | using std::vector; | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 63 | using testing::_; | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 64 | using testing::DoAll; | 
|  | 65 | using testing::Return; | 
|  | 66 | using testing::SetArgPointee; | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 67 |  | 
|  | 68 | namespace chromeos_update_engine { | 
|  | 69 |  | 
|  | 70 | // Test fixture that sets up a testing configuration (with e.g. a | 
|  | 71 | // temporary p2p dir) for P2PManager and cleans up when the test is | 
|  | 72 | // done. | 
|  | 73 | class P2PManagerTest : public testing::Test { | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 74 | protected: | 
| Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 75 | P2PManagerTest() = default; | 
|  | 76 | ~P2PManagerTest() override = default; | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | // Derived from testing::Test. | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 79 | void SetUp() override { | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 80 | loop_.SetAsCurrent(); | 
| Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 81 | FakeSystemState::CreateInstance(); | 
| Alex Deymo | b7ca096 | 2014-10-01 17:58:07 -0700 | [diff] [blame] | 82 | async_signal_handler_.Init(); | 
|  | 83 | subprocess_.Init(&async_signal_handler_); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 84 | test_conf_ = new FakeP2PManagerConfiguration(); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | // Allocate and install a mock policy implementation in the fake Update | 
|  | 87 | // Manager.  Note that the FakeUpdateManager takes ownership of the policy | 
|  | 88 | // object. | 
| Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 89 | mock_policy_ = new chromeos_update_manager::MockPolicy(); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 90 | fake_um_.set_policy(mock_policy_); | 
|  | 91 |  | 
|  | 92 | // Construct the P2P manager under test. | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 93 | manager_.reset(P2PManager::Construct(test_conf_, | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 94 | &fake_um_, | 
|  | 95 | "cros_au", | 
|  | 96 | 3, | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 97 | TimeDelta::FromDays(5))); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 98 | } | 
| Alex Deymo | 509dd53 | 2015-06-10 14:11:05 -0700 | [diff] [blame] | 99 |  | 
| Qijiang Fan | b0b6cc2 | 2020-10-15 21:54:11 +0900 | [diff] [blame] | 100 | #if BASE_VER < 780000  // Android | 
| Alex Deymo | 0b3db6b | 2015-08-10 15:19:37 -0700 | [diff] [blame] | 101 | base::MessageLoopForIO base_loop_; | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 102 | brillo::BaseMessageLoop loop_{&base_loop_}; | 
| Qijiang Fan | b0b6cc2 | 2020-10-15 21:54:11 +0900 | [diff] [blame] | 103 | #else   // CrOS | 
|  | 104 | base::SingleThreadTaskExecutor base_loop_{base::MessagePumpType::IO}; | 
|  | 105 | brillo::BaseMessageLoop loop_{base_loop_.task_runner()}; | 
|  | 106 | #endif  // BASE_VER < 780000 | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 107 | brillo::AsynchronousSignalHandler async_signal_handler_; | 
| Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 108 | Subprocess subprocess_; | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 109 |  | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 110 | // The P2PManager::Configuration instance used for testing. | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 111 | FakeP2PManagerConfiguration* test_conf_; | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 112 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 113 | chromeos_update_manager::MockPolicy* mock_policy_ = nullptr; | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 114 | chromeos_update_manager::FakeUpdateManager fake_um_; | 
|  | 115 |  | 
|  | 116 | unique_ptr<P2PManager> manager_; | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 117 | }; | 
|  | 118 |  | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 119 | // Check that IsP2PEnabled() polls the policy correctly, with the value not | 
|  | 120 | // changing between calls. | 
|  | 121 | TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) { | 
|  | 122 | EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _)); | 
|  | 123 | EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 124 |  | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 125 | EXPECT_FALSE(manager_->IsP2PEnabled()); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 126 | brillo::MessageLoopRunMaxIterations(MessageLoop::current(), 100); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 127 | EXPECT_FALSE(manager_->IsP2PEnabled()); | 
| David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 130 | // Check that IsP2PEnabled() polls the policy correctly, with the value changing | 
|  | 131 | // between calls. | 
|  | 132 | TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) { | 
|  | 133 | EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _)) | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 134 | .WillOnce(DoAll(SetArgPointee<3>(true), | 
|  | 135 | Return(chromeos_update_manager::EvalStatus::kSucceeded))); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 136 | EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true)); | 
|  | 137 | EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false)); | 
| David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 138 |  | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 139 | EXPECT_TRUE(manager_->IsP2PEnabled()); | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 140 | brillo::MessageLoopRunMaxIterations(MessageLoop::current(), 100); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 141 | EXPECT_FALSE(manager_->IsP2PEnabled()); | 
| David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 144 | // Check that we keep the $N newest files with the .$EXT.p2p extension. | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 145 | TEST_F(P2PManagerTest, HousekeepingCountLimit) { | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 146 | // Specifically pass 0 for |max_file_age| to allow files of any age. Note that | 
|  | 147 | // we need to reallocate the test_conf_ member, whose currently aliased object | 
|  | 148 | // will be freed. | 
|  | 149 | test_conf_ = new FakeP2PManagerConfiguration(); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 150 | manager_.reset(P2PManager::Construct(test_conf_, | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 151 | &fake_um_, | 
|  | 152 | "cros_au", | 
|  | 153 | 3, | 
|  | 154 | TimeDelta() /* max_file_age */)); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 155 | EXPECT_EQ(manager_->CountSharedFiles(), 0); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 156 |  | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 157 | base::Time start_time = base::Time::FromDoubleT(1246996800.); | 
| Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 158 | // Generate files with different timestamps matching our pattern and generate | 
|  | 159 | // other files not matching the pattern. | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 160 | for (int n = 0; n < 5; n++) { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 161 | base::FilePath path = test_conf_->GetP2PDir().Append( | 
|  | 162 | base::StringPrintf("file_%d.cros_au.p2p", n)); | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 163 | base::Time file_time = start_time + TimeDelta::FromMinutes(n); | 
|  | 164 | EXPECT_EQ(0, base::WriteFile(path, nullptr, 0)); | 
|  | 165 | EXPECT_TRUE(base::TouchFile(path, file_time, file_time)); | 
| David Zeuthen | 45e2ae2 | 2013-09-03 11:46:11 -0700 | [diff] [blame] | 166 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 167 | path = test_conf_->GetP2PDir().Append( | 
|  | 168 | base::StringPrintf("file_%d.OTHER.p2p", n)); | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 169 | EXPECT_EQ(0, base::WriteFile(path, nullptr, 0)); | 
|  | 170 | EXPECT_TRUE(base::TouchFile(path, file_time, file_time)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 171 | } | 
|  | 172 | // CountSharedFiles() only counts 'cros_au' files. | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 173 | EXPECT_EQ(manager_->CountSharedFiles(), 5); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 174 |  | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 175 | EXPECT_TRUE(manager_->PerformHousekeeping()); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 176 |  | 
|  | 177 | // At this point - after HouseKeeping - we should only have | 
|  | 178 | // eight files left. | 
|  | 179 | for (int n = 0; n < 5; n++) { | 
|  | 180 | string file_name; | 
|  | 181 | bool expect; | 
|  | 182 |  | 
|  | 183 | expect = (n >= 2); | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 184 | file_name = base::StringPrintf( | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 185 | "%s/file_%d.cros_au.p2p", test_conf_->GetP2PDir().value().c_str(), n); | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 186 | EXPECT_EQ(expect, utils::FileExists(file_name.c_str())); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 187 |  | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 188 | file_name = base::StringPrintf( | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 189 | "%s/file_%d.OTHER.p2p", test_conf_->GetP2PDir().value().c_str(), n); | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 190 | EXPECT_TRUE(utils::FileExists(file_name.c_str())); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 191 | } | 
|  | 192 | // CountSharedFiles() only counts 'cros_au' files. | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 193 | EXPECT_EQ(manager_->CountSharedFiles(), 3); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 196 | // Check that we keep files with the .$EXT.p2p extension not older | 
| Sen Jiang | 771f648 | 2018-04-04 17:59:10 -0700 | [diff] [blame] | 197 | // than some specific age (5 days, in this test). | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 198 | TEST_F(P2PManagerTest, HousekeepingAgeLimit) { | 
|  | 199 | // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9 | 
|  | 200 | // September 2001 - arbitrary number, but constant to avoid test | 
|  | 201 | // flakiness) since the epoch and then we put two files before that | 
|  | 202 | // date and three files after. | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 203 | base::Time cutoff_time = base::Time::FromTimeT(1000000000); | 
| Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 204 | TimeDelta age_limit = TimeDelta::FromDays(5); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 205 |  | 
|  | 206 | // Set the clock just so files with a timestamp before |cutoff_time| | 
|  | 207 | // will be deleted at housekeeping. | 
| Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 208 | FakeSystemState::Get()->fake_clock()->SetWallclockTime(cutoff_time + | 
|  | 209 | age_limit); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 210 |  | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 211 | // Specifically pass 0 for |num_files_to_keep| to allow any number of files. | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 212 | // Note that we need to reallocate the test_conf_ member, whose currently | 
|  | 213 | // aliased object will be freed. | 
|  | 214 | test_conf_ = new FakeP2PManagerConfiguration(); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 215 | manager_.reset(P2PManager::Construct(test_conf_, | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 216 | &fake_um_, | 
|  | 217 | "cros_au", | 
|  | 218 | 0 /* num_files_to_keep */, | 
|  | 219 | age_limit)); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 220 | EXPECT_EQ(manager_->CountSharedFiles(), 0); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 221 |  | 
|  | 222 | // Generate files with different timestamps matching our pattern and generate | 
|  | 223 | // other files not matching the pattern. | 
|  | 224 | for (int n = 0; n < 5; n++) { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 225 | base::FilePath path = test_conf_->GetP2PDir().Append( | 
|  | 226 | base::StringPrintf("file_%d.cros_au.p2p", n)); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 227 |  | 
|  | 228 | // With five files and aiming for two of them to be before | 
|  | 229 | // |cutoff_time|, we distribute it like this: | 
|  | 230 | // | 
|  | 231 | //  -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 -------- | 
|  | 232 | //                            | | 
|  | 233 | //                       cutoff_time | 
|  | 234 | // | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 235 | base::Time file_date = cutoff_time + (n - 2) * TimeDelta::FromDays(1) + | 
|  | 236 | TimeDelta::FromHours(12); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 237 |  | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 238 | EXPECT_EQ(0, base::WriteFile(path, nullptr, 0)); | 
|  | 239 | EXPECT_TRUE(base::TouchFile(path, file_date, file_date)); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 240 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 241 | path = test_conf_->GetP2PDir().Append( | 
|  | 242 | base::StringPrintf("file_%d.OTHER.p2p", n)); | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 243 | EXPECT_EQ(0, base::WriteFile(path, nullptr, 0)); | 
|  | 244 | EXPECT_TRUE(base::TouchFile(path, file_date, file_date)); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 245 | } | 
|  | 246 | // CountSharedFiles() only counts 'cros_au' files. | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 247 | EXPECT_EQ(manager_->CountSharedFiles(), 5); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 248 |  | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 249 | EXPECT_TRUE(manager_->PerformHousekeeping()); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 250 |  | 
|  | 251 | // At this point - after HouseKeeping - we should only have | 
|  | 252 | // eight files left. | 
|  | 253 | for (int n = 0; n < 5; n++) { | 
|  | 254 | string file_name; | 
|  | 255 | bool expect; | 
|  | 256 |  | 
|  | 257 | expect = (n >= 2); | 
|  | 258 | file_name = base::StringPrintf( | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 259 | "%s/file_%d.cros_au.p2p", test_conf_->GetP2PDir().value().c_str(), n); | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 260 | EXPECT_EQ(expect, utils::FileExists(file_name.c_str())); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 261 |  | 
|  | 262 | file_name = base::StringPrintf( | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 263 | "%s/file_%d.OTHER.p2p", test_conf_->GetP2PDir().value().c_str(), n); | 
| Alex Deymo | 454b798 | 2015-07-10 10:49:29 -0700 | [diff] [blame] | 264 | EXPECT_TRUE(utils::FileExists(file_name.c_str())); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 265 | } | 
|  | 266 | // CountSharedFiles() only counts 'cros_au' files. | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 267 | EXPECT_EQ(manager_->CountSharedFiles(), 3); | 
| David Zeuthen | 41f2cf5 | 2014-11-05 12:29:45 -0500 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 270 | static bool CheckP2PFile(const string& p2p_dir, | 
|  | 271 | const string& file_name, | 
|  | 272 | ssize_t expected_size, | 
|  | 273 | ssize_t expected_size_xattr) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 274 | string path = p2p_dir + "/" + file_name; | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 275 | char ea_value[64] = {0}; | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 276 | ssize_t ea_size; | 
|  | 277 |  | 
| Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 278 | off_t p2p_size = utils::FileSize(path); | 
|  | 279 | if (p2p_size < 0) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 280 | LOG(ERROR) << "File " << path << " does not exist"; | 
|  | 281 | return false; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | if (expected_size != 0) { | 
| Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 285 | if (p2p_size != expected_size) { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 286 | LOG(ERROR) << "Expected size " << expected_size << " but size was " | 
|  | 287 | << p2p_size; | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 288 | return false; | 
|  | 289 | } | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | if (expected_size_xattr == 0) { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 293 | ea_size = getxattr( | 
|  | 294 | path.c_str(), "user.cros-p2p-filesize", &ea_value, sizeof ea_value - 1); | 
| Alex Deymo | 6f20dd4 | 2015-08-18 16:42:46 -0700 | [diff] [blame] | 295 | if (ea_size == -1 && errno == ENODATA) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 296 | // This is valid behavior as we support files without the xattr set. | 
|  | 297 | } else { | 
| Alex Deymo | 6f20dd4 | 2015-08-18 16:42:46 -0700 | [diff] [blame] | 298 | PLOG(ERROR) << "getxattr() didn't fail with ENODATA as expected, " | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 299 | << "ea_size=" << ea_size << ", errno=" << errno; | 
|  | 300 | return false; | 
|  | 301 | } | 
|  | 302 | } else { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 303 | ea_size = getxattr( | 
|  | 304 | path.c_str(), "user.cros-p2p-filesize", &ea_value, sizeof ea_value - 1); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 305 | if (ea_size < 0) { | 
|  | 306 | LOG(ERROR) << "Error getting xattr attribute"; | 
|  | 307 | return false; | 
|  | 308 | } | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 309 | char* endp = nullptr; | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 310 | long long int val = strtoll(ea_value, &endp, 0);  // NOLINT(runtime/int) | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 311 | if (endp == nullptr || *endp != '\0') { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 312 | LOG(ERROR) << "Error parsing xattr '" << ea_value << "' as an integer"; | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 313 | return false; | 
|  | 314 | } | 
|  | 315 | if (val != expected_size_xattr) { | 
|  | 316 | LOG(ERROR) << "Expected xattr size " << expected_size_xattr | 
|  | 317 | << " but size was " << val; | 
|  | 318 | return false; | 
|  | 319 | } | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | return true; | 
|  | 323 | } | 
|  | 324 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 325 | static bool CreateP2PFile(string p2p_dir, | 
|  | 326 | string file_name, | 
|  | 327 | size_t size, | 
|  | 328 | size_t size_xattr) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 329 | string path = p2p_dir + "/" + file_name; | 
|  | 330 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 331 | int fd = open(path.c_str(), O_CREAT | O_RDWR, 0644); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 332 | if (fd == -1) { | 
|  | 333 | PLOG(ERROR) << "Error creating file with path " << path; | 
|  | 334 | return false; | 
|  | 335 | } | 
|  | 336 | if (ftruncate(fd, size) != 0) { | 
|  | 337 | PLOG(ERROR) << "Error truncating " << path << " to size " << size; | 
|  | 338 | close(fd); | 
|  | 339 | return false; | 
|  | 340 | } | 
|  | 341 |  | 
|  | 342 | if (size_xattr != 0) { | 
| Alex Deymo | c00c98a | 2015-03-17 17:38:00 -0700 | [diff] [blame] | 343 | string decimal_size = std::to_string(size_xattr); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 344 | if (fsetxattr(fd, | 
|  | 345 | "user.cros-p2p-filesize", | 
|  | 346 | decimal_size.c_str(), | 
|  | 347 | decimal_size.size(), | 
|  | 348 | 0) != 0) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 349 | PLOG(ERROR) << "Error setting xattr on " << path; | 
|  | 350 | close(fd); | 
|  | 351 | return false; | 
|  | 352 | } | 
|  | 353 | } | 
|  | 354 |  | 
|  | 355 | close(fd); | 
|  | 356 | return true; | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | // Check that sharing a *new* file works. | 
|  | 360 | TEST_F(P2PManagerTest, ShareFile) { | 
| Amin Hassani | 4f5657e | 2020-12-02 13:16:05 -0800 | [diff] [blame] | 361 | const int kP2PTestFileSize = 1000 * 8;  // 8 KB | 
| David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 362 |  | 
| Alex Deymo | 3c3807c | 2015-01-30 09:32:41 -0800 | [diff] [blame] | 363 | EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize)); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 364 | EXPECT_EQ(manager_->FileGetPath("foo"), | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 365 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp")); | 
|  | 366 | EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(), | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 367 | "foo.cros_au.p2p.tmp", | 
|  | 368 | 0, | 
|  | 369 | kP2PTestFileSize)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 370 |  | 
|  | 371 | // Sharing it again - with the same expected size - should return true | 
| Alex Deymo | 3c3807c | 2015-01-30 09:32:41 -0800 | [diff] [blame] | 372 | EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 373 |  | 
|  | 374 | // ... but if we use the wrong size, it should fail | 
| Alex Deymo | 3c3807c | 2015-01-30 09:32:41 -0800 | [diff] [blame] | 375 | EXPECT_FALSE(manager_->FileShare("foo", kP2PTestFileSize + 1)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 376 | } | 
|  | 377 |  | 
|  | 378 | // Check that making a shared file visible, does what is expected. | 
|  | 379 | TEST_F(P2PManagerTest, MakeFileVisible) { | 
| Amin Hassani | 4f5657e | 2020-12-02 13:16:05 -0800 | [diff] [blame] | 380 | const int kP2PTestFileSize = 1000 * 8;  // 8 KB | 
| David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 381 |  | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 382 | // First, check that it's not visible. | 
| Alex Deymo | 3c3807c | 2015-01-30 09:32:41 -0800 | [diff] [blame] | 383 | manager_->FileShare("foo", kP2PTestFileSize); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 384 | EXPECT_EQ(manager_->FileGetPath("foo"), | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 385 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp")); | 
|  | 386 | EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(), | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 387 | "foo.cros_au.p2p.tmp", | 
|  | 388 | 0, | 
|  | 389 | kP2PTestFileSize)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 390 | // Make the file visible and check that it changed its name. Do it | 
|  | 391 | // twice to check that FileMakeVisible() is idempotent. | 
|  | 392 | for (int n = 0; n < 2; n++) { | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 393 | manager_->FileMakeVisible("foo"); | 
|  | 394 | EXPECT_EQ(manager_->FileGetPath("foo"), | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 395 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p")); | 
|  | 396 | EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(), | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 397 | "foo.cros_au.p2p", | 
|  | 398 | 0, | 
|  | 399 | kP2PTestFileSize)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 400 | } | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | // Check that we return the right values for existing files in P2P_DIR. | 
|  | 404 | TEST_F(P2PManagerTest, ExistingFiles) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 405 | bool visible; | 
|  | 406 |  | 
|  | 407 | // Check that errors are returned if the file does not exist | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 408 | EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath()); | 
|  | 409 | EXPECT_EQ(manager_->FileGetSize("foo"), -1); | 
|  | 410 | EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1); | 
|  | 411 | EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 412 | // ... then create the file ... | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 413 | EXPECT_TRUE(CreateP2PFile( | 
|  | 414 | test_conf_->GetP2PDir().value(), "foo.cros_au.p2p", 42, 43)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 415 | // ... and then check that the expected values are returned | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 416 | EXPECT_EQ(manager_->FileGetPath("foo"), | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 417 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p")); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 418 | EXPECT_EQ(manager_->FileGetSize("foo"), 42); | 
|  | 419 | EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43); | 
|  | 420 | EXPECT_TRUE(manager_->FileGetVisible("foo", &visible)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 421 | EXPECT_TRUE(visible); | 
|  | 422 |  | 
|  | 423 | // One more time, this time with a .tmp variant. First ensure it errors out.. | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 424 | EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath()); | 
|  | 425 | EXPECT_EQ(manager_->FileGetSize("bar"), -1); | 
|  | 426 | EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1); | 
|  | 427 | EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 428 | // ... then create the file ... | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 429 | EXPECT_TRUE(CreateP2PFile( | 
|  | 430 | test_conf_->GetP2PDir().value(), "bar.cros_au.p2p.tmp", 44, 45)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 431 | // ... and then check that the expected values are returned | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 432 | EXPECT_EQ(manager_->FileGetPath("bar"), | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 433 | test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp")); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 434 | EXPECT_EQ(manager_->FileGetSize("bar"), 44); | 
|  | 435 | EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45); | 
|  | 436 | EXPECT_TRUE(manager_->FileGetVisible("bar", &visible)); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 437 | EXPECT_FALSE(visible); | 
|  | 438 | } | 
|  | 439 |  | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 440 | // This is a little bit ugly but short of mocking a 'p2p' service this | 
|  | 441 | // will have to do. E.g. we essentially simulate the various | 
|  | 442 | // behaviours of initctl(8) that we rely on. | 
|  | 443 | TEST_F(P2PManagerTest, StartP2P) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 444 | // Check that we can start the service | 
| Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 445 | test_conf_->SetInitctlStartCommand({"true"}); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 446 | EXPECT_TRUE(manager_->EnsureP2PRunning()); | 
| Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 447 | test_conf_->SetInitctlStartCommand({"false"}); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 448 | EXPECT_FALSE(manager_->EnsureP2PRunning()); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 449 | test_conf_->SetInitctlStartCommand( | 
|  | 450 | {"sh", "-c", "echo \"initctl: Job is already running: p2p\" >&2; false"}); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 451 | EXPECT_TRUE(manager_->EnsureP2PRunning()); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 452 | test_conf_->SetInitctlStartCommand( | 
|  | 453 | {"sh", "-c", "echo something else >&2; false"}); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 454 | EXPECT_FALSE(manager_->EnsureP2PRunning()); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 455 | } | 
|  | 456 |  | 
|  | 457 | // Same comment as for StartP2P | 
|  | 458 | TEST_F(P2PManagerTest, StopP2P) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 459 | // Check that we can start the service | 
| Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 460 | test_conf_->SetInitctlStopCommand({"true"}); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 461 | EXPECT_TRUE(manager_->EnsureP2PNotRunning()); | 
| Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 462 | test_conf_->SetInitctlStopCommand({"false"}); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 463 | EXPECT_FALSE(manager_->EnsureP2PNotRunning()); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 464 | test_conf_->SetInitctlStopCommand( | 
|  | 465 | {"sh", "-c", "echo \"initctl: Unknown instance \" >&2; false"}); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 466 | EXPECT_TRUE(manager_->EnsureP2PNotRunning()); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 467 | test_conf_->SetInitctlStopCommand( | 
|  | 468 | {"sh", "-c", "echo something else >&2; false"}); | 
| Gilad Arnold | 4a0321b | 2014-10-28 15:57:30 -0700 | [diff] [blame] | 469 | EXPECT_FALSE(manager_->EnsureP2PNotRunning()); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 470 | } | 
|  | 471 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 472 | static void ExpectUrl(const string& expected_url, const string& url) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 473 | EXPECT_EQ(url, expected_url); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 474 | MessageLoop::current()->BreakLoop(); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 475 | } | 
|  | 476 |  | 
|  | 477 | // Like StartP2P, we're mocking the different results that p2p-client | 
|  | 478 | // can return. It's not pretty but it works. | 
|  | 479 | TEST_F(P2PManagerTest, LookupURL) { | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 480 | // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au" | 
|  | 481 | // being propagated in the right places. | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 482 | test_conf_->SetP2PClientCommand( | 
|  | 483 | {"echo", "http://1.2.3.4/{file_id}_{minsize}"}); | 
|  | 484 | manager_->LookupUrlForFile( | 
|  | 485 | "fooX", | 
|  | 486 | 42, | 
|  | 487 | TimeDelta(), | 
|  | 488 | base::Bind(ExpectUrl, "http://1.2.3.4/fooX.cros_au_42")); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 489 | loop_.Run(); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 490 |  | 
|  | 491 | // Emulate p2p-client returning invalid URL. | 
| Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 492 | test_conf_->SetP2PClientCommand({"echo", "not_a_valid_url"}); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 493 | manager_->LookupUrlForFile( | 
|  | 494 | "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, "")); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 495 | loop_.Run(); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 496 |  | 
|  | 497 | // Emulate p2p-client conveying failure. | 
| Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 498 | test_conf_->SetP2PClientCommand({"false"}); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 499 | manager_->LookupUrlForFile( | 
|  | 500 | "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, "")); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 501 | loop_.Run(); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 502 |  | 
|  | 503 | // Emulate p2p-client not existing. | 
| Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 504 | test_conf_->SetP2PClientCommand({"/path/to/non/existent/helper/program"}); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 505 | manager_->LookupUrlForFile( | 
|  | 506 | "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, "")); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 507 | loop_.Run(); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 508 |  | 
|  | 509 | // Emulate p2p-client crashing. | 
| Alex Deymo | b339155 | 2015-07-10 10:48:06 -0700 | [diff] [blame] | 510 | test_conf_->SetP2PClientCommand({"sh", "-c", "kill -SEGV $$"}); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 511 | manager_->LookupUrlForFile( | 
|  | 512 | "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, "")); | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 513 | loop_.Run(); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 514 |  | 
|  | 515 | // Emulate p2p-client exceeding its timeout. | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 516 | test_conf_->SetP2PClientCommand( | 
|  | 517 | {"sh", | 
|  | 518 | "-c", | 
|  | 519 | // The 'sleep' launched below could be left behind as an orphaned | 
|  | 520 | // process when the 'sh' process is terminated by SIGTERM. As a | 
|  | 521 | // remedy, trap SIGTERM and kill the 'sleep' process, which requires | 
|  | 522 | // launching 'sleep' in background and then waiting for it. | 
|  | 523 | "cleanup() { kill \"${sleep_pid}\"; exit 0; }; " | 
|  | 524 | "trap cleanup TERM; " | 
|  | 525 | "sleep 5 & " | 
|  | 526 | "sleep_pid=$!; " | 
|  | 527 | "echo http://1.2.3.4/; " | 
|  | 528 | "wait"}); | 
|  | 529 | manager_->LookupUrlForFile("foobar", | 
|  | 530 | 42, | 
|  | 531 | TimeDelta::FromMilliseconds(500), | 
| Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 532 | base::Bind(ExpectUrl, "")); | 
|  | 533 | loop_.Run(); | 
| David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 534 | } | 
|  | 535 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 536 | }  // namespace chromeos_update_engine |