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