blob: 736a87a78836ce517500ca8d64a9356e7d085daa [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Zeuthen27a48bc2013-08-06 12:06:29 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/p2p_manager.h"
18
David Zeuthen27a48bc2013-08-06 12:06:29 -070019#include <dirent.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070020#include <fcntl.h>
21#include <sys/stat.h>
Alex Deymo6f20dd42015-08-18 16:42:46 -070022#include <sys/types.h>
23#include <sys/xattr.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070024#include <unistd.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070025
Ben Chan02f7c1d2014-10-18 15:18:02 -070026#include <memory>
Alex Deymob3391552015-07-10 10:48:06 -070027#include <string>
28#include <vector>
Ben Chan02f7c1d2014-10-18 15:18:02 -070029
Alex Deymo8427b4a2014-11-05 14:00:32 -080030#include <base/bind.h>
31#include <base/callback.h>
Alex Deymo454b7982015-07-10 10:49:29 -070032#include <base/files/file_util.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070033#include <base/message_loop/message_loop.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070034#include <base/strings/stringprintf.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070035#include <brillo/asynchronous_signal_handler.h>
36#include <brillo/message_loops/base_message_loop.h>
37#include <brillo/message_loops/message_loop.h>
38#include <brillo/message_loops/message_loop_utils.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080039#include <gmock/gmock.h>
40#include <gtest/gtest.h>
David Zeuthen92d9c8b2013-09-11 10:58:11 -070041#include <policy/libpolicy.h>
42#include <policy/mock_device_policy.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070043
Alex Deymo39910dc2015-11-09 17:04:30 -080044#include "update_engine/common/fake_clock.h"
45#include "update_engine/common/prefs.h"
Sen Jiangab6bda92019-01-09 16:33:51 -080046#include "update_engine/common/subprocess.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080047#include "update_engine/common/test_utils.h"
48#include "update_engine/common/utils.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070049#include "update_engine/fake_p2p_manager_configuration.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070050#include "update_engine/update_manager/fake_update_manager.h"
51#include "update_engine/update_manager/mock_policy.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070052
Alex Deymof329b932014-10-30 01:37:48 -070053using base::TimeDelta;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070054using brillo::MessageLoop;
David Zeuthen27a48bc2013-08-06 12:06:29 -070055using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070056using std::unique_ptr;
Alex Deymo454b7982015-07-10 10:49:29 -070057using std::vector;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070058using testing::DoAll;
59using testing::Return;
60using testing::SetArgPointee;
61using testing::_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070062
63namespace chromeos_update_engine {
64
65// Test fixture that sets up a testing configuration (with e.g. a
66// temporary p2p dir) for P2PManager and cleans up when the test is
67// done.
68class P2PManagerTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070069 protected:
Gilad Arnold4a0321b2014-10-28 15:57:30 -070070 P2PManagerTest() : fake_um_(&fake_clock_) {}
Alex Deymo610277e2014-11-11 21:18:11 -080071 ~P2PManagerTest() override {}
David Zeuthen27a48bc2013-08-06 12:06:29 -070072
73 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -080074 void SetUp() override {
Alex Deymo509dd532015-06-10 14:11:05 -070075 loop_.SetAsCurrent();
Alex Deymob7ca0962014-10-01 17:58:07 -070076 async_signal_handler_.Init();
77 subprocess_.Init(&async_signal_handler_);
David Zeuthen27a48bc2013-08-06 12:06:29 -070078 test_conf_ = new FakeP2PManagerConfiguration();
Gilad Arnold4a0321b2014-10-28 15:57:30 -070079
80 // Allocate and install a mock policy implementation in the fake Update
81 // Manager. Note that the FakeUpdateManager takes ownership of the policy
82 // object.
83 mock_policy_ = new chromeos_update_manager::MockPolicy(&fake_clock_);
84 fake_um_.set_policy(mock_policy_);
85
86 // Construct the P2P manager under test.
87 manager_.reset(P2PManager::Construct(test_conf_, &fake_clock_, &fake_um_,
88 "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -070089 TimeDelta::FromDays(5)));
David Zeuthen27a48bc2013-08-06 12:06:29 -070090 }
Alex Deymo509dd532015-06-10 14:11:05 -070091
Alex Deymo0b3db6b2015-08-10 15:19:37 -070092 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070093 brillo::BaseMessageLoop loop_{&base_loop_};
94 brillo::AsynchronousSignalHandler async_signal_handler_;
Alex Deymo461b2592015-07-24 20:10:52 -070095 Subprocess subprocess_;
Alex Deymo60ca1a72015-06-18 18:19:15 -070096
David Zeuthen27a48bc2013-08-06 12:06:29 -070097 // The P2PManager::Configuration instance used for testing.
98 FakeP2PManagerConfiguration *test_conf_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070099
100 FakeClock fake_clock_;
101 chromeos_update_manager::MockPolicy *mock_policy_ = nullptr;
102 chromeos_update_manager::FakeUpdateManager fake_um_;
103
104 unique_ptr<P2PManager> manager_;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700105};
106
107
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700108// Check that IsP2PEnabled() polls the policy correctly, with the value not
109// changing between calls.
110TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
111 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _));
112 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700113
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700114 EXPECT_FALSE(manager_->IsP2PEnabled());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700115 brillo::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700116 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700117}
118
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700119// Check that IsP2PEnabled() polls the policy correctly, with the value changing
120// between calls.
121TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) {
122 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _))
123 .WillOnce(DoAll(
124 SetArgPointee<3>(true),
125 Return(chromeos_update_manager::EvalStatus::kSucceeded)));
126 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true));
127 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700128
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700129 EXPECT_TRUE(manager_->IsP2PEnabled());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700130 brillo::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700131 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400132}
133
David Zeuthen27a48bc2013-08-06 12:06:29 -0700134// Check that we keep the $N newest files with the .$EXT.p2p extension.
David Zeuthen41f2cf52014-11-05 12:29:45 -0500135TEST_F(P2PManagerTest, HousekeepingCountLimit) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700136 // Specifically pass 0 for |max_file_age| to allow files of any age. Note that
137 // we need to reallocate the test_conf_ member, whose currently aliased object
138 // will be freed.
139 test_conf_ = new FakeP2PManagerConfiguration();
140 manager_.reset(P2PManager::Construct(
141 test_conf_, &fake_clock_, &fake_um_, "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -0700142 TimeDelta() /* max_file_age */));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700143 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700144
Alex Deymo454b7982015-07-10 10:49:29 -0700145 base::Time start_time = base::Time::FromDoubleT(1246996800.);
Alex Deymo0f513512013-09-13 14:11:26 -0700146 // Generate files with different timestamps matching our pattern and generate
147 // other files not matching the pattern.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700148 for (int n = 0; n < 5; n++) {
Alex Deymo454b7982015-07-10 10:49:29 -0700149 base::FilePath path = test_conf_->GetP2PDir().Append(base::StringPrintf(
150 "file_%d.cros_au.p2p", n));
151 base::Time file_time = start_time + TimeDelta::FromMinutes(n);
152 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
153 EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700154
Alex Deymo454b7982015-07-10 10:49:29 -0700155 path = test_conf_->GetP2PDir().Append(base::StringPrintf(
156 "file_%d.OTHER.p2p", n));
157 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
158 EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700159 }
160 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700161 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700162
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700163 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700164
165 // At this point - after HouseKeeping - we should only have
166 // eight files left.
167 for (int n = 0; n < 5; n++) {
168 string file_name;
169 bool expect;
170
171 expect = (n >= 2);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700172 file_name = base::StringPrintf(
173 "%s/file_%d.cros_au.p2p",
174 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700175 EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700176
Alex Vakulenko75039d72014-03-25 12:36:28 -0700177 file_name = base::StringPrintf(
178 "%s/file_%d.OTHER.p2p",
179 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700180 EXPECT_TRUE(utils::FileExists(file_name.c_str()));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700181 }
182 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700183 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700184}
185
David Zeuthen41f2cf52014-11-05 12:29:45 -0500186// Check that we keep files with the .$EXT.p2p extension not older
Sen Jiang771f6482018-04-04 17:59:10 -0700187// than some specific age (5 days, in this test).
David Zeuthen41f2cf52014-11-05 12:29:45 -0500188TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
189 // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9
190 // September 2001 - arbitrary number, but constant to avoid test
191 // flakiness) since the epoch and then we put two files before that
192 // date and three files after.
Alex Deymo454b7982015-07-10 10:49:29 -0700193 base::Time cutoff_time = base::Time::FromTimeT(1000000000);
Alex Deymo10875d92014-11-10 21:52:57 -0800194 TimeDelta age_limit = TimeDelta::FromDays(5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500195
196 // Set the clock just so files with a timestamp before |cutoff_time|
197 // will be deleted at housekeeping.
Alex Deymo454b7982015-07-10 10:49:29 -0700198 fake_clock_.SetWallclockTime(cutoff_time + age_limit);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500199
Alex Deymo454b7982015-07-10 10:49:29 -0700200 // Specifically pass 0 for |num_files_to_keep| to allow any number of files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700201 // Note that we need to reallocate the test_conf_ member, whose currently
202 // aliased object will be freed.
203 test_conf_ = new FakeP2PManagerConfiguration();
204 manager_.reset(P2PManager::Construct(
205 test_conf_, &fake_clock_, &fake_um_, "cros_au",
David Zeuthen41f2cf52014-11-05 12:29:45 -0500206 0 /* num_files_to_keep */, age_limit));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700207 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500208
209 // Generate files with different timestamps matching our pattern and generate
210 // other files not matching the pattern.
211 for (int n = 0; n < 5; n++) {
Alex Deymo454b7982015-07-10 10:49:29 -0700212 base::FilePath path = test_conf_->GetP2PDir().Append(base::StringPrintf(
David Zeuthen41f2cf52014-11-05 12:29:45 -0500213 "file_%d.cros_au.p2p", n));
214
215 // With five files and aiming for two of them to be before
216 // |cutoff_time|, we distribute it like this:
217 //
218 // -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 --------
219 // |
220 // cutoff_time
221 //
Alex Deymo454b7982015-07-10 10:49:29 -0700222 base::Time file_date = cutoff_time + (n - 2) * TimeDelta::FromDays(1)
223 + TimeDelta::FromHours(12);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500224
Alex Deymo454b7982015-07-10 10:49:29 -0700225 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
226 EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500227
Alex Deymo454b7982015-07-10 10:49:29 -0700228 path = test_conf_->GetP2PDir().Append(base::StringPrintf(
229 "file_%d.OTHER.p2p", n));
230 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
231 EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500232 }
233 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700234 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500235
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700236 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen41f2cf52014-11-05 12:29:45 -0500237
238 // At this point - after HouseKeeping - we should only have
239 // eight files left.
240 for (int n = 0; n < 5; n++) {
241 string file_name;
242 bool expect;
243
244 expect = (n >= 2);
245 file_name = base::StringPrintf(
246 "%s/file_%d.cros_au.p2p",
247 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700248 EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500249
250 file_name = base::StringPrintf(
251 "%s/file_%d.OTHER.p2p",
252 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700253 EXPECT_TRUE(utils::FileExists(file_name.c_str()));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500254 }
255 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700256 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500257}
258
David Zeuthen27a48bc2013-08-06 12:06:29 -0700259static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
260 ssize_t expected_size, ssize_t expected_size_xattr) {
261 string path = p2p_dir + "/" + file_name;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700262 char ea_value[64] = { 0 };
263 ssize_t ea_size;
264
Gabe Blacka77939e2014-09-09 23:35:08 -0700265 off_t p2p_size = utils::FileSize(path);
266 if (p2p_size < 0) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700267 LOG(ERROR) << "File " << path << " does not exist";
268 return false;
269 }
270
271 if (expected_size != 0) {
Gabe Blacka77939e2014-09-09 23:35:08 -0700272 if (p2p_size != expected_size) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700273 LOG(ERROR) << "Expected size " << expected_size
Gabe Blacka77939e2014-09-09 23:35:08 -0700274 << " but size was " << p2p_size;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700275 return false;
276 }
277 }
278
279 if (expected_size_xattr == 0) {
280 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
281 &ea_value, sizeof ea_value - 1);
Alex Deymo6f20dd42015-08-18 16:42:46 -0700282 if (ea_size == -1 && errno == ENODATA) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700283 // This is valid behavior as we support files without the xattr set.
284 } else {
Alex Deymo6f20dd42015-08-18 16:42:46 -0700285 PLOG(ERROR) << "getxattr() didn't fail with ENODATA as expected, "
David Zeuthen27a48bc2013-08-06 12:06:29 -0700286 << "ea_size=" << ea_size << ", errno=" << errno;
287 return false;
288 }
289 } else {
290 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
291 &ea_value, sizeof ea_value - 1);
292 if (ea_size < 0) {
293 LOG(ERROR) << "Error getting xattr attribute";
294 return false;
295 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700296 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700297 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700298 if (endp == nullptr || *endp != '\0') {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700299 LOG(ERROR) << "Error parsing xattr '" << ea_value
300 << "' as an integer";
301 return false;
302 }
303 if (val != expected_size_xattr) {
304 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
305 << " but size was " << val;
306 return false;
307 }
308 }
309
310 return true;
311}
312
313static bool CreateP2PFile(string p2p_dir, string file_name,
314 size_t size, size_t size_xattr) {
315 string path = p2p_dir + "/" + file_name;
316
317 int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
318 if (fd == -1) {
319 PLOG(ERROR) << "Error creating file with path " << path;
320 return false;
321 }
322 if (ftruncate(fd, size) != 0) {
323 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
324 close(fd);
325 return false;
326 }
327
328 if (size_xattr != 0) {
Alex Deymoc00c98a2015-03-17 17:38:00 -0700329 string decimal_size = std::to_string(size_xattr);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700330 if (fsetxattr(fd, "user.cros-p2p-filesize",
331 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
332 PLOG(ERROR) << "Error setting xattr on " << path;
333 close(fd);
334 return false;
335 }
336 }
337
338 close(fd);
339 return true;
340}
341
342// Check that sharing a *new* file works.
343TEST_F(P2PManagerTest, ShareFile) {
Alex Deymo3c3807c2015-01-30 09:32:41 -0800344 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700345
Alex Deymo3c3807c2015-01-30 09:32:41 -0800346 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700347 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700348 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
349 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800350 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700351
352 // Sharing it again - with the same expected size - should return true
Alex Deymo3c3807c2015-01-30 09:32:41 -0800353 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700354
355 // ... but if we use the wrong size, it should fail
Alex Deymo3c3807c2015-01-30 09:32:41 -0800356 EXPECT_FALSE(manager_->FileShare("foo", kP2PTestFileSize + 1));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700357}
358
359// Check that making a shared file visible, does what is expected.
360TEST_F(P2PManagerTest, MakeFileVisible) {
Alex Deymo3c3807c2015-01-30 09:32:41 -0800361 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700362
David Zeuthen27a48bc2013-08-06 12:06:29 -0700363 // First, check that it's not visible.
Alex Deymo3c3807c2015-01-30 09:32:41 -0800364 manager_->FileShare("foo", kP2PTestFileSize);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700365 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700366 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
367 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800368 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700369 // Make the file visible and check that it changed its name. Do it
370 // twice to check that FileMakeVisible() is idempotent.
371 for (int n = 0; n < 2; n++) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700372 manager_->FileMakeVisible("foo");
373 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700374 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
375 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800376 "foo.cros_au.p2p", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700377 }
378}
379
380// Check that we return the right values for existing files in P2P_DIR.
381TEST_F(P2PManagerTest, ExistingFiles) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700382 bool visible;
383
384 // Check that errors are returned if the file does not exist
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700385 EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath());
386 EXPECT_EQ(manager_->FileGetSize("foo"), -1);
387 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
388 EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700389 // ... then create the file ...
390 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
391 "foo.cros_au.p2p", 42, 43));
392 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700393 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700394 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700395 EXPECT_EQ(manager_->FileGetSize("foo"), 42);
396 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43);
397 EXPECT_TRUE(manager_->FileGetVisible("foo", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700398 EXPECT_TRUE(visible);
399
400 // One more time, this time with a .tmp variant. First ensure it errors out..
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700401 EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath());
402 EXPECT_EQ(manager_->FileGetSize("bar"), -1);
403 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
404 EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700405 // ... then create the file ...
406 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
407 "bar.cros_au.p2p.tmp", 44, 45));
408 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700409 EXPECT_EQ(manager_->FileGetPath("bar"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700410 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700411 EXPECT_EQ(manager_->FileGetSize("bar"), 44);
412 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45);
413 EXPECT_TRUE(manager_->FileGetVisible("bar", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700414 EXPECT_FALSE(visible);
415}
416
David Zeuthen27a48bc2013-08-06 12:06:29 -0700417// This is a little bit ugly but short of mocking a 'p2p' service this
418// will have to do. E.g. we essentially simulate the various
419// behaviours of initctl(8) that we rely on.
420TEST_F(P2PManagerTest, StartP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700421 // Check that we can start the service
Alex Deymob3391552015-07-10 10:48:06 -0700422 test_conf_->SetInitctlStartCommand({"true"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700423 EXPECT_TRUE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700424 test_conf_->SetInitctlStartCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700425 EXPECT_FALSE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700426 test_conf_->SetInitctlStartCommand({
427 "sh", "-c", "echo \"initctl: Job is already running: p2p\" >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700428 EXPECT_TRUE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700429 test_conf_->SetInitctlStartCommand({
430 "sh", "-c", "echo something else >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700431 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700432}
433
434// Same comment as for StartP2P
435TEST_F(P2PManagerTest, StopP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700436 // Check that we can start the service
Alex Deymob3391552015-07-10 10:48:06 -0700437 test_conf_->SetInitctlStopCommand({"true"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700438 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700439 test_conf_->SetInitctlStopCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700440 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700441 test_conf_->SetInitctlStopCommand({
442 "sh", "-c", "echo \"initctl: Unknown instance \" >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700443 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700444 test_conf_->SetInitctlStopCommand({
445 "sh", "-c", "echo something else >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700446 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700447}
448
449static void ExpectUrl(const string& expected_url,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700450 const string& url) {
451 EXPECT_EQ(url, expected_url);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700452 MessageLoop::current()->BreakLoop();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700453}
454
455// Like StartP2P, we're mocking the different results that p2p-client
456// can return. It's not pretty but it works.
457TEST_F(P2PManagerTest, LookupURL) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700458 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
459 // being propagated in the right places.
Alex Deymob3391552015-07-10 10:48:06 -0700460 test_conf_->SetP2PClientCommand({
461 "echo", "http://1.2.3.4/{file_id}_{minsize}"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700462 manager_->LookupUrlForFile("fooX", 42, TimeDelta(),
463 base::Bind(ExpectUrl,
Alex Deymo60ca1a72015-06-18 18:19:15 -0700464 "http://1.2.3.4/fooX.cros_au_42"));
465 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700466
467 // Emulate p2p-client returning invalid URL.
Alex Deymob3391552015-07-10 10:48:06 -0700468 test_conf_->SetP2PClientCommand({"echo", "not_a_valid_url"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700469 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700470 base::Bind(ExpectUrl, ""));
471 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700472
473 // Emulate p2p-client conveying failure.
Alex Deymob3391552015-07-10 10:48:06 -0700474 test_conf_->SetP2PClientCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700475 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700476 base::Bind(ExpectUrl, ""));
477 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700478
479 // Emulate p2p-client not existing.
Alex Deymob3391552015-07-10 10:48:06 -0700480 test_conf_->SetP2PClientCommand({"/path/to/non/existent/helper/program"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700481 manager_->LookupUrlForFile("foobar", 42,
482 TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700483 base::Bind(ExpectUrl, ""));
484 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700485
486 // Emulate p2p-client crashing.
Alex Deymob3391552015-07-10 10:48:06 -0700487 test_conf_->SetP2PClientCommand({"sh", "-c", "kill -SEGV $$"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700488 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700489 base::Bind(ExpectUrl, ""));
490 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700491
492 // Emulate p2p-client exceeding its timeout.
Alex Deymob3391552015-07-10 10:48:06 -0700493 test_conf_->SetP2PClientCommand({
Ben Chan80098612017-01-10 22:28:22 -0800494 "sh", "-c",
495 // The 'sleep' launched below could be left behind as an orphaned
496 // process when the 'sh' process is terminated by SIGTERM. As a
497 // remedy, trap SIGTERM and kill the 'sleep' process, which requires
498 // launching 'sleep' in background and then waiting for it.
499 "cleanup() { kill \"${sleep_pid}\"; exit 0; }; "
500 "trap cleanup TERM; "
501 "sleep 5 & "
502 "sleep_pid=$!; "
503 "echo http://1.2.3.4/; "
504 "wait"
505 });
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700506 manager_->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700507 base::Bind(ExpectUrl, ""));
508 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700509}
510
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700511} // namespace chromeos_update_engine