blob: 3926f93ffcc22f56e209565cba82a69593f49d3a [file] [log] [blame]
David Zeuthen27a48bc2013-08-06 12:06:29 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Alex Deymo8427b4a2014-11-05 14:00:32 -08005#include "update_engine/p2p_manager.h"
6
David Zeuthen27a48bc2013-08-06 12:06:29 -07007#include <dirent.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -07008#include <fcntl.h>
Gilad Arnold4a0321b2014-10-28 15:57:30 -07009#include <glib.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070010#include <sys/stat.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070011#include <unistd.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070012#include <attr/xattr.h> // NOLINT - requires typed defined in unistd.h
David Zeuthen27a48bc2013-08-06 12:06:29 -070013
Ben Chan02f7c1d2014-10-18 15:18:02 -070014#include <memory>
Alex Deymob3391552015-07-10 10:48:06 -070015#include <string>
16#include <vector>
Ben Chan02f7c1d2014-10-18 15:18:02 -070017
Alex Deymo8427b4a2014-11-05 14:00:32 -080018#include <base/bind.h>
19#include <base/callback.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070020#include <base/strings/stringprintf.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070021#include <chromeos/message_loops/glib_message_loop.h>
Alex Deymo509dd532015-06-10 14:11:05 -070022#include <chromeos/message_loops/message_loop.h>
23#include <chromeos/message_loops/message_loop_utils.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080024#include <gmock/gmock.h>
25#include <gtest/gtest.h>
David Zeuthen92d9c8b2013-09-11 10:58:11 -070026#include <policy/libpolicy.h>
27#include <policy/mock_device_policy.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070028
David Zeuthen41f2cf52014-11-05 12:29:45 -050029#include "update_engine/fake_clock.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070030#include "update_engine/fake_p2p_manager_configuration.h"
31#include "update_engine/prefs.h"
32#include "update_engine/test_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070033#include "update_engine/update_manager/fake_update_manager.h"
34#include "update_engine/update_manager/mock_policy.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070035#include "update_engine/utils.h"
36
Alex Deymof329b932014-10-30 01:37:48 -070037using base::TimeDelta;
Alex Deymo509dd532015-06-10 14:11:05 -070038using chromeos::MessageLoop;
Alex Deymo10875d92014-11-10 21:52:57 -080039using chromeos_update_engine::test_utils::System;
David Zeuthen27a48bc2013-08-06 12:06:29 -070040using std::string;
Alex Deymob3391552015-07-10 10:48:06 -070041using std::vector;
Ben Chan02f7c1d2014-10-18 15:18:02 -070042using std::unique_ptr;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070043using testing::DoAll;
44using testing::Return;
45using testing::SetArgPointee;
46using testing::_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070047
48namespace chromeos_update_engine {
49
50// Test fixture that sets up a testing configuration (with e.g. a
51// temporary p2p dir) for P2PManager and cleans up when the test is
52// done.
53class P2PManagerTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070054 protected:
Gilad Arnold4a0321b2014-10-28 15:57:30 -070055 P2PManagerTest() : fake_um_(&fake_clock_) {}
Alex Deymo610277e2014-11-11 21:18:11 -080056 ~P2PManagerTest() override {}
David Zeuthen27a48bc2013-08-06 12:06:29 -070057
58 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -080059 void SetUp() override {
Alex Deymo509dd532015-06-10 14:11:05 -070060 loop_.SetAsCurrent();
David Zeuthen27a48bc2013-08-06 12:06:29 -070061 test_conf_ = new FakeP2PManagerConfiguration();
Gilad Arnold4a0321b2014-10-28 15:57:30 -070062
63 // Allocate and install a mock policy implementation in the fake Update
64 // Manager. Note that the FakeUpdateManager takes ownership of the policy
65 // object.
66 mock_policy_ = new chromeos_update_manager::MockPolicy(&fake_clock_);
67 fake_um_.set_policy(mock_policy_);
68
69 // Construct the P2P manager under test.
70 manager_.reset(P2PManager::Construct(test_conf_, &fake_clock_, &fake_um_,
71 "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -070072 TimeDelta::FromDays(5)));
David Zeuthen27a48bc2013-08-06 12:06:29 -070073 }
Alex Deymo509dd532015-06-10 14:11:05 -070074
75 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -070076 EXPECT_EQ(0, chromeos::MessageLoopRunMaxIterations(&loop_, 1));
Alex Deymo509dd532015-06-10 14:11:05 -070077 }
David Zeuthen27a48bc2013-08-06 12:06:29 -070078
Alex Deymo60ca1a72015-06-18 18:19:15 -070079 // TODO(deymo): Replace this with a FakeMessageLoop. P2PManager uses glib to
80 // interact with the p2p-client tool, so we need to run a GlibMessageLoop
81 // here.
82 chromeos::GlibMessageLoop loop_;
83
David Zeuthen27a48bc2013-08-06 12:06:29 -070084 // The P2PManager::Configuration instance used for testing.
85 FakeP2PManagerConfiguration *test_conf_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070086
87 FakeClock fake_clock_;
88 chromeos_update_manager::MockPolicy *mock_policy_ = nullptr;
89 chromeos_update_manager::FakeUpdateManager fake_um_;
90
91 unique_ptr<P2PManager> manager_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070092};
93
94
Gilad Arnold4a0321b2014-10-28 15:57:30 -070095// Check that IsP2PEnabled() polls the policy correctly, with the value not
96// changing between calls.
97TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
98 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _));
99 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700100
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700101 EXPECT_FALSE(manager_->IsP2PEnabled());
Alex Deymo509dd532015-06-10 14:11:05 -0700102 chromeos::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700103 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700104}
105
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700106// Check that IsP2PEnabled() polls the policy correctly, with the value changing
107// between calls.
108TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) {
109 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _))
110 .WillOnce(DoAll(
111 SetArgPointee<3>(true),
112 Return(chromeos_update_manager::EvalStatus::kSucceeded)));
113 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true));
114 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700115
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700116 EXPECT_TRUE(manager_->IsP2PEnabled());
Alex Deymo509dd532015-06-10 14:11:05 -0700117 chromeos::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700118 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400119}
120
David Zeuthen27a48bc2013-08-06 12:06:29 -0700121// Check that we keep the $N newest files with the .$EXT.p2p extension.
David Zeuthen41f2cf52014-11-05 12:29:45 -0500122TEST_F(P2PManagerTest, HousekeepingCountLimit) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700123 // Specifically pass 0 for |max_file_age| to allow files of any age. Note that
124 // we need to reallocate the test_conf_ member, whose currently aliased object
125 // will be freed.
126 test_conf_ = new FakeP2PManagerConfiguration();
127 manager_.reset(P2PManager::Construct(
128 test_conf_, &fake_clock_, &fake_um_, "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -0700129 TimeDelta() /* max_file_age */));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700130 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700131
Alex Deymo0f513512013-09-13 14:11:26 -0700132 // Generate files with different timestamps matching our pattern and generate
133 // other files not matching the pattern.
134 double last_timestamp = -1;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700135 for (int n = 0; n < 5; n++) {
Alex Deymo0f513512013-09-13 14:11:26 -0700136 double current_timestamp;
137 do {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700138 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
Alex Deymo0f513512013-09-13 14:11:26 -0700139 "file_%d.cros_au.p2p", n));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700140 EXPECT_EQ(0, System(base::StringPrintf("touch %s",
141 file.value().c_str())));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700142
Alex Deymo0f513512013-09-13 14:11:26 -0700143 // Check that the current timestamp on the file is different from the
144 // previous generated file. This timestamp depends on the file system
145 // time resolution, for example, ext2/ext3 have a time resolution of one
146 // second while ext4 has a resolution of one nanosecond. If the assigned
147 // timestamp is the same, we introduce a bigger sleep and call touch
148 // again.
149 struct stat statbuf;
150 EXPECT_EQ(stat(file.value().c_str(), &statbuf), 0);
151 current_timestamp = utils::TimeFromStructTimespec(&statbuf.st_ctim)
152 .ToDoubleT();
153 if (current_timestamp == last_timestamp)
154 sleep(1);
155 } while (current_timestamp == last_timestamp);
156 last_timestamp = current_timestamp;
157
Alex Vakulenko75039d72014-03-25 12:36:28 -0700158 EXPECT_EQ(0, System(base::StringPrintf(
159 "touch %s/file_%d.OTHER.p2p",
160 test_conf_->GetP2PDir().value().c_str(), n)));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700161
Alex Deymo0f513512013-09-13 14:11:26 -0700162 // A sleep of one micro-second is enough to have a different "Change" time
163 // on the file on newer file systems.
164 g_usleep(1);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700165 }
166 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700167 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700168
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700169 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700170
171 // At this point - after HouseKeeping - we should only have
172 // eight files left.
173 for (int n = 0; n < 5; n++) {
174 string file_name;
175 bool expect;
176
177 expect = (n >= 2);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700178 file_name = base::StringPrintf(
179 "%s/file_%d.cros_au.p2p",
180 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700181 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
182
Alex Vakulenko75039d72014-03-25 12:36:28 -0700183 file_name = base::StringPrintf(
184 "%s/file_%d.OTHER.p2p",
185 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700186 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
187 }
188 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700189 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700190}
191
David Zeuthen41f2cf52014-11-05 12:29:45 -0500192// Check that we keep files with the .$EXT.p2p extension not older
193// than some specificed age (5 days, in this test).
194TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
195 // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9
196 // September 2001 - arbitrary number, but constant to avoid test
197 // flakiness) since the epoch and then we put two files before that
198 // date and three files after.
199 time_t cutoff_time = 1000000000;
Alex Deymo10875d92014-11-10 21:52:57 -0800200 TimeDelta age_limit = TimeDelta::FromDays(5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500201
202 // Set the clock just so files with a timestamp before |cutoff_time|
203 // will be deleted at housekeeping.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700204 fake_clock_.SetWallclockTime(base::Time::FromTimeT(cutoff_time) + age_limit);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500205
206 // Specifically pass 0 for |num_files_to_keep| to allow files of any age.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700207 // Note that we need to reallocate the test_conf_ member, whose currently
208 // aliased object will be freed.
209 test_conf_ = new FakeP2PManagerConfiguration();
210 manager_.reset(P2PManager::Construct(
211 test_conf_, &fake_clock_, &fake_um_, "cros_au",
David Zeuthen41f2cf52014-11-05 12:29:45 -0500212 0 /* num_files_to_keep */, age_limit));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700213 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500214
215 // Generate files with different timestamps matching our pattern and generate
216 // other files not matching the pattern.
217 for (int n = 0; n < 5; n++) {
218 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
219 "file_%d.cros_au.p2p", n));
220
221 // With five files and aiming for two of them to be before
222 // |cutoff_time|, we distribute it like this:
223 //
224 // -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 --------
225 // |
226 // cutoff_time
227 //
228 base::Time file_date = base::Time::FromTimeT(cutoff_time) +
Alex Deymo10875d92014-11-10 21:52:57 -0800229 (n - 2)*TimeDelta::FromDays(1) + TimeDelta::FromHours(12);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500230
231 // The touch(1) command expects input like this
232 // --date="2004-02-27 14:19:13.489392193 +0530"
233 base::Time::Exploded exploded;
234 file_date.UTCExplode(&exploded);
235 string file_date_string = base::StringPrintf(
236 "%d-%02d-%02d %02d:%02d:%02d +0000",
237 exploded.year, exploded.month, exploded.day_of_month,
238 exploded.hour, exploded.minute, exploded.second);
239
240 // Sanity check that we generated the correct string.
241 base::Time parsed_time;
242 EXPECT_TRUE(base::Time::FromUTCString(file_date_string.c_str(),
243 &parsed_time));
244 EXPECT_EQ(parsed_time, file_date);
245
246 EXPECT_EQ(0, System(base::StringPrintf("touch --date=\"%s\" %s",
247 file_date_string.c_str(),
248 file.value().c_str())));
249
250 EXPECT_EQ(0, System(base::StringPrintf(
251 "touch --date=\"%s\" %s/file_%d.OTHER.p2p",
252 file_date_string.c_str(),
253 test_conf_->GetP2PDir().value().c_str(), n)));
254 }
255 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700256 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500257
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700258 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen41f2cf52014-11-05 12:29:45 -0500259
260 // At this point - after HouseKeeping - we should only have
261 // eight files left.
262 for (int n = 0; n < 5; n++) {
263 string file_name;
264 bool expect;
265
266 expect = (n >= 2);
267 file_name = base::StringPrintf(
268 "%s/file_%d.cros_au.p2p",
269 test_conf_->GetP2PDir().value().c_str(), n);
270 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
271
272 file_name = base::StringPrintf(
273 "%s/file_%d.OTHER.p2p",
274 test_conf_->GetP2PDir().value().c_str(), n);
275 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
276 }
277 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700278 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500279}
280
David Zeuthen27a48bc2013-08-06 12:06:29 -0700281static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
282 ssize_t expected_size, ssize_t expected_size_xattr) {
283 string path = p2p_dir + "/" + file_name;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700284 char ea_value[64] = { 0 };
285 ssize_t ea_size;
286
Gabe Blacka77939e2014-09-09 23:35:08 -0700287 off_t p2p_size = utils::FileSize(path);
288 if (p2p_size < 0) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700289 LOG(ERROR) << "File " << path << " does not exist";
290 return false;
291 }
292
293 if (expected_size != 0) {
Gabe Blacka77939e2014-09-09 23:35:08 -0700294 if (p2p_size != expected_size) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700295 LOG(ERROR) << "Expected size " << expected_size
Gabe Blacka77939e2014-09-09 23:35:08 -0700296 << " but size was " << p2p_size;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700297 return false;
298 }
299 }
300
301 if (expected_size_xattr == 0) {
302 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
303 &ea_value, sizeof ea_value - 1);
304 if (ea_size == -1 && errno == ENOATTR) {
305 // This is valid behavior as we support files without the xattr set.
306 } else {
307 PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, "
308 << "ea_size=" << ea_size << ", errno=" << errno;
309 return false;
310 }
311 } else {
312 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
313 &ea_value, sizeof ea_value - 1);
314 if (ea_size < 0) {
315 LOG(ERROR) << "Error getting xattr attribute";
316 return false;
317 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700318 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700319 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700320 if (endp == nullptr || *endp != '\0') {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700321 LOG(ERROR) << "Error parsing xattr '" << ea_value
322 << "' as an integer";
323 return false;
324 }
325 if (val != expected_size_xattr) {
326 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
327 << " but size was " << val;
328 return false;
329 }
330 }
331
332 return true;
333}
334
335static bool CreateP2PFile(string p2p_dir, string file_name,
336 size_t size, size_t size_xattr) {
337 string path = p2p_dir + "/" + file_name;
338
339 int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
340 if (fd == -1) {
341 PLOG(ERROR) << "Error creating file with path " << path;
342 return false;
343 }
344 if (ftruncate(fd, size) != 0) {
345 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
346 close(fd);
347 return false;
348 }
349
350 if (size_xattr != 0) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700351 string decimal_size = base::StringPrintf("%zu", size_xattr);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700352 if (fsetxattr(fd, "user.cros-p2p-filesize",
353 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
354 PLOG(ERROR) << "Error setting xattr on " << path;
355 close(fd);
356 return false;
357 }
358 }
359
360 close(fd);
361 return true;
362}
363
364// Check that sharing a *new* file works.
365TEST_F(P2PManagerTest, ShareFile) {
Alex Deymo10875d92014-11-10 21:52:57 -0800366 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700367 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
368 << "Please update your system to support this feature.";
369 return;
370 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800371 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700372
Alex Deymo3c3807c2015-01-30 09:32:41 -0800373 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700374 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700375 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
376 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800377 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700378
379 // Sharing it again - with the same expected size - should return true
Alex Deymo3c3807c2015-01-30 09:32:41 -0800380 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700381
382 // ... but if we use the wrong size, it should fail
Alex Deymo3c3807c2015-01-30 09:32:41 -0800383 EXPECT_FALSE(manager_->FileShare("foo", kP2PTestFileSize + 1));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700384}
385
386// Check that making a shared file visible, does what is expected.
387TEST_F(P2PManagerTest, MakeFileVisible) {
Alex Deymo10875d92014-11-10 21:52:57 -0800388 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700389 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
390 << "Please update your system to support this feature.";
391 return;
392 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800393 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700394
David Zeuthen27a48bc2013-08-06 12:06:29 -0700395 // First, check that it's not visible.
Alex Deymo3c3807c2015-01-30 09:32:41 -0800396 manager_->FileShare("foo", kP2PTestFileSize);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700397 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700398 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
399 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800400 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700401 // Make the file visible and check that it changed its name. Do it
402 // twice to check that FileMakeVisible() is idempotent.
403 for (int n = 0; n < 2; n++) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700404 manager_->FileMakeVisible("foo");
405 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700406 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
407 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800408 "foo.cros_au.p2p", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700409 }
410}
411
412// Check that we return the right values for existing files in P2P_DIR.
413TEST_F(P2PManagerTest, ExistingFiles) {
Alex Deymo10875d92014-11-10 21:52:57 -0800414 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700415 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
416 << "Please update your system to support this feature.";
417 return;
418 }
419
David Zeuthen27a48bc2013-08-06 12:06:29 -0700420 bool visible;
421
422 // Check that errors are returned if the file does not exist
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700423 EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath());
424 EXPECT_EQ(manager_->FileGetSize("foo"), -1);
425 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
426 EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700427 // ... then create the file ...
428 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
429 "foo.cros_au.p2p", 42, 43));
430 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700431 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700432 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700433 EXPECT_EQ(manager_->FileGetSize("foo"), 42);
434 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43);
435 EXPECT_TRUE(manager_->FileGetVisible("foo", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700436 EXPECT_TRUE(visible);
437
438 // One more time, this time with a .tmp variant. First ensure it errors out..
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700439 EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath());
440 EXPECT_EQ(manager_->FileGetSize("bar"), -1);
441 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
442 EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700443 // ... then create the file ...
444 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
445 "bar.cros_au.p2p.tmp", 44, 45));
446 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700447 EXPECT_EQ(manager_->FileGetPath("bar"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700448 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700449 EXPECT_EQ(manager_->FileGetSize("bar"), 44);
450 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45);
451 EXPECT_TRUE(manager_->FileGetVisible("bar", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700452 EXPECT_FALSE(visible);
453}
454
David Zeuthen27a48bc2013-08-06 12:06:29 -0700455// This is a little bit ugly but short of mocking a 'p2p' service this
456// will have to do. E.g. we essentially simulate the various
457// behaviours of initctl(8) that we rely on.
458TEST_F(P2PManagerTest, StartP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700459 // Check that we can start the service
Alex Deymob3391552015-07-10 10:48:06 -0700460 test_conf_->SetInitctlStartCommand({"true"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700461 EXPECT_TRUE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700462 test_conf_->SetInitctlStartCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700463 EXPECT_FALSE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700464 test_conf_->SetInitctlStartCommand({
465 "sh", "-c", "echo \"initctl: Job is already running: p2p\" >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700466 EXPECT_TRUE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700467 test_conf_->SetInitctlStartCommand({
468 "sh", "-c", "echo something else >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700469 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700470}
471
472// Same comment as for StartP2P
473TEST_F(P2PManagerTest, StopP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700474 // Check that we can start the service
Alex Deymob3391552015-07-10 10:48:06 -0700475 test_conf_->SetInitctlStopCommand({"true"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700476 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700477 test_conf_->SetInitctlStopCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700478 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700479 test_conf_->SetInitctlStopCommand({
480 "sh", "-c", "echo \"initctl: Unknown instance \" >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700481 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700482 test_conf_->SetInitctlStopCommand({
483 "sh", "-c", "echo something else >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700484 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700485}
486
487static void ExpectUrl(const string& expected_url,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700488 const string& url) {
489 EXPECT_EQ(url, expected_url);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700490 MessageLoop::current()->BreakLoop();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700491}
492
493// Like StartP2P, we're mocking the different results that p2p-client
494// can return. It's not pretty but it works.
495TEST_F(P2PManagerTest, LookupURL) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700496 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
497 // being propagated in the right places.
Alex Deymob3391552015-07-10 10:48:06 -0700498 test_conf_->SetP2PClientCommand({
499 "echo", "http://1.2.3.4/{file_id}_{minsize}"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700500 manager_->LookupUrlForFile("fooX", 42, TimeDelta(),
501 base::Bind(ExpectUrl,
Alex Deymo60ca1a72015-06-18 18:19:15 -0700502 "http://1.2.3.4/fooX.cros_au_42"));
503 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700504
505 // Emulate p2p-client returning invalid URL.
Alex Deymob3391552015-07-10 10:48:06 -0700506 test_conf_->SetP2PClientCommand({"echo", "not_a_valid_url"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700507 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700508 base::Bind(ExpectUrl, ""));
509 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700510
511 // Emulate p2p-client conveying failure.
Alex Deymob3391552015-07-10 10:48:06 -0700512 test_conf_->SetP2PClientCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700513 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700514 base::Bind(ExpectUrl, ""));
515 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700516
517 // Emulate p2p-client not existing.
Alex Deymob3391552015-07-10 10:48:06 -0700518 test_conf_->SetP2PClientCommand({"/path/to/non/existent/helper/program"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700519 manager_->LookupUrlForFile("foobar", 42,
520 TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700521 base::Bind(ExpectUrl, ""));
522 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700523
524 // Emulate p2p-client crashing.
Alex Deymob3391552015-07-10 10:48:06 -0700525 test_conf_->SetP2PClientCommand({"sh", "-c", "kill -SEGV $$"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700526 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700527 base::Bind(ExpectUrl, ""));
528 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700529
530 // Emulate p2p-client exceeding its timeout.
Alex Deymob3391552015-07-10 10:48:06 -0700531 test_conf_->SetP2PClientCommand({
532 "sh", "-c", "echo http://1.2.3.4/; sleep 2"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700533 manager_->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700534 base::Bind(ExpectUrl, ""));
535 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700536}
537
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700538} // namespace chromeos_update_engine