blob: 7c4cfd9f1a6613fdff0b456e9c3a125c146adfd6 [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>
15
Alex Deymo8427b4a2014-11-05 14:00:32 -080016#include <base/bind.h>
17#include <base/callback.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070018#include <base/strings/stringprintf.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070019#include <chromeos/message_loops/glib_message_loop.h>
Alex Deymo509dd532015-06-10 14:11:05 -070020#include <chromeos/message_loops/message_loop.h>
21#include <chromeos/message_loops/message_loop_utils.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080022#include <gmock/gmock.h>
23#include <gtest/gtest.h>
David Zeuthen92d9c8b2013-09-11 10:58:11 -070024#include <policy/libpolicy.h>
25#include <policy/mock_device_policy.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070026
David Zeuthen41f2cf52014-11-05 12:29:45 -050027#include "update_engine/fake_clock.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070028#include "update_engine/fake_p2p_manager_configuration.h"
29#include "update_engine/prefs.h"
30#include "update_engine/test_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070031#include "update_engine/update_manager/fake_update_manager.h"
32#include "update_engine/update_manager/mock_policy.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070033#include "update_engine/utils.h"
34
Alex Deymof329b932014-10-30 01:37:48 -070035using base::TimeDelta;
Alex Deymo509dd532015-06-10 14:11:05 -070036using chromeos::MessageLoop;
Alex Deymo10875d92014-11-10 21:52:57 -080037using chromeos_update_engine::test_utils::System;
David Zeuthen27a48bc2013-08-06 12:06:29 -070038using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070039using std::unique_ptr;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070040using testing::DoAll;
41using testing::Return;
42using testing::SetArgPointee;
43using testing::_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070044
45namespace chromeos_update_engine {
46
47// Test fixture that sets up a testing configuration (with e.g. a
48// temporary p2p dir) for P2PManager and cleans up when the test is
49// done.
50class P2PManagerTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070051 protected:
Gilad Arnold4a0321b2014-10-28 15:57:30 -070052 P2PManagerTest() : fake_um_(&fake_clock_) {}
Alex Deymo610277e2014-11-11 21:18:11 -080053 ~P2PManagerTest() override {}
David Zeuthen27a48bc2013-08-06 12:06:29 -070054
55 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -080056 void SetUp() override {
Alex Deymo509dd532015-06-10 14:11:05 -070057 loop_.SetAsCurrent();
David Zeuthen27a48bc2013-08-06 12:06:29 -070058 test_conf_ = new FakeP2PManagerConfiguration();
Gilad Arnold4a0321b2014-10-28 15:57:30 -070059
60 // Allocate and install a mock policy implementation in the fake Update
61 // Manager. Note that the FakeUpdateManager takes ownership of the policy
62 // object.
63 mock_policy_ = new chromeos_update_manager::MockPolicy(&fake_clock_);
64 fake_um_.set_policy(mock_policy_);
65
66 // Construct the P2P manager under test.
67 manager_.reset(P2PManager::Construct(test_conf_, &fake_clock_, &fake_um_,
68 "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -070069 TimeDelta::FromDays(5)));
David Zeuthen27a48bc2013-08-06 12:06:29 -070070 }
Alex Deymo509dd532015-06-10 14:11:05 -070071
72 void TearDown() override {
Alex Deymo60ca1a72015-06-18 18:19:15 -070073 EXPECT_EQ(0, chromeos::MessageLoopRunMaxIterations(&loop_, 1));
Alex Deymo509dd532015-06-10 14:11:05 -070074 }
David Zeuthen27a48bc2013-08-06 12:06:29 -070075
Alex Deymo60ca1a72015-06-18 18:19:15 -070076 // TODO(deymo): Replace this with a FakeMessageLoop. P2PManager uses glib to
77 // interact with the p2p-client tool, so we need to run a GlibMessageLoop
78 // here.
79 chromeos::GlibMessageLoop loop_;
80
David Zeuthen27a48bc2013-08-06 12:06:29 -070081 // The P2PManager::Configuration instance used for testing.
82 FakeP2PManagerConfiguration *test_conf_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070083
84 FakeClock fake_clock_;
85 chromeos_update_manager::MockPolicy *mock_policy_ = nullptr;
86 chromeos_update_manager::FakeUpdateManager fake_um_;
87
88 unique_ptr<P2PManager> manager_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070089};
90
91
Gilad Arnold4a0321b2014-10-28 15:57:30 -070092// Check that IsP2PEnabled() polls the policy correctly, with the value not
93// changing between calls.
94TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
95 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _));
96 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen27a48bc2013-08-06 12:06:29 -070097
Gilad Arnold4a0321b2014-10-28 15:57:30 -070098 EXPECT_FALSE(manager_->IsP2PEnabled());
Alex Deymo509dd532015-06-10 14:11:05 -070099 chromeos::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700100 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700101}
102
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700103// Check that IsP2PEnabled() polls the policy correctly, with the value changing
104// between calls.
105TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) {
106 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _))
107 .WillOnce(DoAll(
108 SetArgPointee<3>(true),
109 Return(chromeos_update_manager::EvalStatus::kSucceeded)));
110 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true));
111 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700112
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700113 EXPECT_TRUE(manager_->IsP2PEnabled());
Alex Deymo509dd532015-06-10 14:11:05 -0700114 chromeos::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700115 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400116}
117
David Zeuthen27a48bc2013-08-06 12:06:29 -0700118// Check that we keep the $N newest files with the .$EXT.p2p extension.
David Zeuthen41f2cf52014-11-05 12:29:45 -0500119TEST_F(P2PManagerTest, HousekeepingCountLimit) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700120 // Specifically pass 0 for |max_file_age| to allow files of any age. Note that
121 // we need to reallocate the test_conf_ member, whose currently aliased object
122 // will be freed.
123 test_conf_ = new FakeP2PManagerConfiguration();
124 manager_.reset(P2PManager::Construct(
125 test_conf_, &fake_clock_, &fake_um_, "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -0700126 TimeDelta() /* max_file_age */));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700127 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700128
Alex Deymo0f513512013-09-13 14:11:26 -0700129 // Generate files with different timestamps matching our pattern and generate
130 // other files not matching the pattern.
131 double last_timestamp = -1;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700132 for (int n = 0; n < 5; n++) {
Alex Deymo0f513512013-09-13 14:11:26 -0700133 double current_timestamp;
134 do {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700135 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
Alex Deymo0f513512013-09-13 14:11:26 -0700136 "file_%d.cros_au.p2p", n));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700137 EXPECT_EQ(0, System(base::StringPrintf("touch %s",
138 file.value().c_str())));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700139
Alex Deymo0f513512013-09-13 14:11:26 -0700140 // Check that the current timestamp on the file is different from the
141 // previous generated file. This timestamp depends on the file system
142 // time resolution, for example, ext2/ext3 have a time resolution of one
143 // second while ext4 has a resolution of one nanosecond. If the assigned
144 // timestamp is the same, we introduce a bigger sleep and call touch
145 // again.
146 struct stat statbuf;
147 EXPECT_EQ(stat(file.value().c_str(), &statbuf), 0);
148 current_timestamp = utils::TimeFromStructTimespec(&statbuf.st_ctim)
149 .ToDoubleT();
150 if (current_timestamp == last_timestamp)
151 sleep(1);
152 } while (current_timestamp == last_timestamp);
153 last_timestamp = current_timestamp;
154
Alex Vakulenko75039d72014-03-25 12:36:28 -0700155 EXPECT_EQ(0, System(base::StringPrintf(
156 "touch %s/file_%d.OTHER.p2p",
157 test_conf_->GetP2PDir().value().c_str(), n)));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700158
Alex Deymo0f513512013-09-13 14:11:26 -0700159 // A sleep of one micro-second is enough to have a different "Change" time
160 // on the file on newer file systems.
161 g_usleep(1);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700162 }
163 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700164 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700165
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700166 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700167
168 // At this point - after HouseKeeping - we should only have
169 // eight files left.
170 for (int n = 0; n < 5; n++) {
171 string file_name;
172 bool expect;
173
174 expect = (n >= 2);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700175 file_name = base::StringPrintf(
176 "%s/file_%d.cros_au.p2p",
177 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700178 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
179
Alex Vakulenko75039d72014-03-25 12:36:28 -0700180 file_name = base::StringPrintf(
181 "%s/file_%d.OTHER.p2p",
182 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700183 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
184 }
185 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700186 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700187}
188
David Zeuthen41f2cf52014-11-05 12:29:45 -0500189// Check that we keep files with the .$EXT.p2p extension not older
190// than some specificed age (5 days, in this test).
191TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
192 // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9
193 // September 2001 - arbitrary number, but constant to avoid test
194 // flakiness) since the epoch and then we put two files before that
195 // date and three files after.
196 time_t cutoff_time = 1000000000;
Alex Deymo10875d92014-11-10 21:52:57 -0800197 TimeDelta age_limit = TimeDelta::FromDays(5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500198
199 // Set the clock just so files with a timestamp before |cutoff_time|
200 // will be deleted at housekeeping.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700201 fake_clock_.SetWallclockTime(base::Time::FromTimeT(cutoff_time) + age_limit);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500202
203 // Specifically pass 0 for |num_files_to_keep| to allow files of any age.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700204 // Note that we need to reallocate the test_conf_ member, whose currently
205 // aliased object will be freed.
206 test_conf_ = new FakeP2PManagerConfiguration();
207 manager_.reset(P2PManager::Construct(
208 test_conf_, &fake_clock_, &fake_um_, "cros_au",
David Zeuthen41f2cf52014-11-05 12:29:45 -0500209 0 /* num_files_to_keep */, age_limit));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700210 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500211
212 // Generate files with different timestamps matching our pattern and generate
213 // other files not matching the pattern.
214 for (int n = 0; n < 5; n++) {
215 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
216 "file_%d.cros_au.p2p", n));
217
218 // With five files and aiming for two of them to be before
219 // |cutoff_time|, we distribute it like this:
220 //
221 // -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 --------
222 // |
223 // cutoff_time
224 //
225 base::Time file_date = base::Time::FromTimeT(cutoff_time) +
Alex Deymo10875d92014-11-10 21:52:57 -0800226 (n - 2)*TimeDelta::FromDays(1) + TimeDelta::FromHours(12);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500227
228 // The touch(1) command expects input like this
229 // --date="2004-02-27 14:19:13.489392193 +0530"
230 base::Time::Exploded exploded;
231 file_date.UTCExplode(&exploded);
232 string file_date_string = base::StringPrintf(
233 "%d-%02d-%02d %02d:%02d:%02d +0000",
234 exploded.year, exploded.month, exploded.day_of_month,
235 exploded.hour, exploded.minute, exploded.second);
236
237 // Sanity check that we generated the correct string.
238 base::Time parsed_time;
239 EXPECT_TRUE(base::Time::FromUTCString(file_date_string.c_str(),
240 &parsed_time));
241 EXPECT_EQ(parsed_time, file_date);
242
243 EXPECT_EQ(0, System(base::StringPrintf("touch --date=\"%s\" %s",
244 file_date_string.c_str(),
245 file.value().c_str())));
246
247 EXPECT_EQ(0, System(base::StringPrintf(
248 "touch --date=\"%s\" %s/file_%d.OTHER.p2p",
249 file_date_string.c_str(),
250 test_conf_->GetP2PDir().value().c_str(), n)));
251 }
252 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700253 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500254
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700255 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen41f2cf52014-11-05 12:29:45 -0500256
257 // At this point - after HouseKeeping - we should only have
258 // eight files left.
259 for (int n = 0; n < 5; n++) {
260 string file_name;
261 bool expect;
262
263 expect = (n >= 2);
264 file_name = base::StringPrintf(
265 "%s/file_%d.cros_au.p2p",
266 test_conf_->GetP2PDir().value().c_str(), n);
267 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
268
269 file_name = base::StringPrintf(
270 "%s/file_%d.OTHER.p2p",
271 test_conf_->GetP2PDir().value().c_str(), n);
272 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
273 }
274 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700275 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500276}
277
David Zeuthen27a48bc2013-08-06 12:06:29 -0700278static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
279 ssize_t expected_size, ssize_t expected_size_xattr) {
280 string path = p2p_dir + "/" + file_name;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700281 char ea_value[64] = { 0 };
282 ssize_t ea_size;
283
Gabe Blacka77939e2014-09-09 23:35:08 -0700284 off_t p2p_size = utils::FileSize(path);
285 if (p2p_size < 0) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700286 LOG(ERROR) << "File " << path << " does not exist";
287 return false;
288 }
289
290 if (expected_size != 0) {
Gabe Blacka77939e2014-09-09 23:35:08 -0700291 if (p2p_size != expected_size) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700292 LOG(ERROR) << "Expected size " << expected_size
Gabe Blacka77939e2014-09-09 23:35:08 -0700293 << " but size was " << p2p_size;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700294 return false;
295 }
296 }
297
298 if (expected_size_xattr == 0) {
299 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
300 &ea_value, sizeof ea_value - 1);
301 if (ea_size == -1 && errno == ENOATTR) {
302 // This is valid behavior as we support files without the xattr set.
303 } else {
304 PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, "
305 << "ea_size=" << ea_size << ", errno=" << errno;
306 return false;
307 }
308 } else {
309 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
310 &ea_value, sizeof ea_value - 1);
311 if (ea_size < 0) {
312 LOG(ERROR) << "Error getting xattr attribute";
313 return false;
314 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700315 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700316 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700317 if (endp == nullptr || *endp != '\0') {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700318 LOG(ERROR) << "Error parsing xattr '" << ea_value
319 << "' as an integer";
320 return false;
321 }
322 if (val != expected_size_xattr) {
323 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
324 << " but size was " << val;
325 return false;
326 }
327 }
328
329 return true;
330}
331
332static bool CreateP2PFile(string p2p_dir, string file_name,
333 size_t size, size_t size_xattr) {
334 string path = p2p_dir + "/" + file_name;
335
336 int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
337 if (fd == -1) {
338 PLOG(ERROR) << "Error creating file with path " << path;
339 return false;
340 }
341 if (ftruncate(fd, size) != 0) {
342 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
343 close(fd);
344 return false;
345 }
346
347 if (size_xattr != 0) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700348 string decimal_size = base::StringPrintf("%zu", size_xattr);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700349 if (fsetxattr(fd, "user.cros-p2p-filesize",
350 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
351 PLOG(ERROR) << "Error setting xattr on " << path;
352 close(fd);
353 return false;
354 }
355 }
356
357 close(fd);
358 return true;
359}
360
361// Check that sharing a *new* file works.
362TEST_F(P2PManagerTest, ShareFile) {
Alex Deymo10875d92014-11-10 21:52:57 -0800363 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700364 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
365 << "Please update your system to support this feature.";
366 return;
367 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800368 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700369
Alex Deymo3c3807c2015-01-30 09:32:41 -0800370 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700371 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700372 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
373 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800374 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700375
376 // Sharing it again - with the same expected size - should return true
Alex Deymo3c3807c2015-01-30 09:32:41 -0800377 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700378
379 // ... but if we use the wrong size, it should fail
Alex Deymo3c3807c2015-01-30 09:32:41 -0800380 EXPECT_FALSE(manager_->FileShare("foo", kP2PTestFileSize + 1));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700381}
382
383// Check that making a shared file visible, does what is expected.
384TEST_F(P2PManagerTest, MakeFileVisible) {
Alex Deymo10875d92014-11-10 21:52:57 -0800385 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700386 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
387 << "Please update your system to support this feature.";
388 return;
389 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800390 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700391
David Zeuthen27a48bc2013-08-06 12:06:29 -0700392 // First, check that it's not visible.
Alex Deymo3c3807c2015-01-30 09:32:41 -0800393 manager_->FileShare("foo", kP2PTestFileSize);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700394 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700395 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
396 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800397 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700398 // Make the file visible and check that it changed its name. Do it
399 // twice to check that FileMakeVisible() is idempotent.
400 for (int n = 0; n < 2; n++) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700401 manager_->FileMakeVisible("foo");
402 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700403 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
404 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800405 "foo.cros_au.p2p", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700406 }
407}
408
409// Check that we return the right values for existing files in P2P_DIR.
410TEST_F(P2PManagerTest, ExistingFiles) {
Alex Deymo10875d92014-11-10 21:52:57 -0800411 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700412 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
413 << "Please update your system to support this feature.";
414 return;
415 }
416
David Zeuthen27a48bc2013-08-06 12:06:29 -0700417 bool visible;
418
419 // Check that errors are returned if the file does not exist
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700420 EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath());
421 EXPECT_EQ(manager_->FileGetSize("foo"), -1);
422 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
423 EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700424 // ... then create the file ...
425 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
426 "foo.cros_au.p2p", 42, 43));
427 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700428 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700429 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700430 EXPECT_EQ(manager_->FileGetSize("foo"), 42);
431 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43);
432 EXPECT_TRUE(manager_->FileGetVisible("foo", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700433 EXPECT_TRUE(visible);
434
435 // One more time, this time with a .tmp variant. First ensure it errors out..
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700436 EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath());
437 EXPECT_EQ(manager_->FileGetSize("bar"), -1);
438 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
439 EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700440 // ... then create the file ...
441 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
442 "bar.cros_au.p2p.tmp", 44, 45));
443 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700444 EXPECT_EQ(manager_->FileGetPath("bar"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700445 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700446 EXPECT_EQ(manager_->FileGetSize("bar"), 44);
447 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45);
448 EXPECT_TRUE(manager_->FileGetVisible("bar", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700449 EXPECT_FALSE(visible);
450}
451
David Zeuthen27a48bc2013-08-06 12:06:29 -0700452// This is a little bit ugly but short of mocking a 'p2p' service this
453// will have to do. E.g. we essentially simulate the various
454// behaviours of initctl(8) that we rely on.
455TEST_F(P2PManagerTest, StartP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700456 // Check that we can start the service
457 test_conf_->SetInitctlStartCommandLine("true");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700458 EXPECT_TRUE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700459 test_conf_->SetInitctlStartCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700460 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700461 test_conf_->SetInitctlStartCommandLine(
462 "sh -c 'echo \"initctl: Job is already running: p2p\" >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700463 EXPECT_TRUE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700464 test_conf_->SetInitctlStartCommandLine(
465 "sh -c 'echo something else >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700466 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700467}
468
469// Same comment as for StartP2P
470TEST_F(P2PManagerTest, StopP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700471 // Check that we can start the service
472 test_conf_->SetInitctlStopCommandLine("true");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700473 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700474 test_conf_->SetInitctlStopCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700475 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700476 test_conf_->SetInitctlStopCommandLine(
477 "sh -c 'echo \"initctl: Unknown instance \" >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700478 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700479 test_conf_->SetInitctlStopCommandLine(
480 "sh -c 'echo something else >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700481 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700482}
483
484static void ExpectUrl(const string& expected_url,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700485 const string& url) {
486 EXPECT_EQ(url, expected_url);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700487 MessageLoop::current()->BreakLoop();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700488}
489
490// Like StartP2P, we're mocking the different results that p2p-client
491// can return. It's not pretty but it works.
492TEST_F(P2PManagerTest, LookupURL) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700493 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
494 // being propagated in the right places.
Alex Deymo8ad6da92014-07-15 17:17:45 -0700495 test_conf_->SetP2PClientCommandLine(
496 "echo 'http://1.2.3.4/{file_id}_{minsize}'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700497 manager_->LookupUrlForFile("fooX", 42, TimeDelta(),
498 base::Bind(ExpectUrl,
Alex Deymo60ca1a72015-06-18 18:19:15 -0700499 "http://1.2.3.4/fooX.cros_au_42"));
500 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700501
502 // Emulate p2p-client returning invalid URL.
503 test_conf_->SetP2PClientCommandLine("echo 'not_a_valid_url'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700504 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700505 base::Bind(ExpectUrl, ""));
506 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700507
508 // Emulate p2p-client conveying failure.
509 test_conf_->SetP2PClientCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700510 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700511 base::Bind(ExpectUrl, ""));
512 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700513
514 // Emulate p2p-client not existing.
515 test_conf_->SetP2PClientCommandLine("/path/to/non/existent/helper/program");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700516 manager_->LookupUrlForFile("foobar", 42,
517 TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700518 base::Bind(ExpectUrl, ""));
519 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700520
521 // Emulate p2p-client crashing.
522 test_conf_->SetP2PClientCommandLine("sh -c 'kill -SEGV $$'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700523 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700524 base::Bind(ExpectUrl, ""));
525 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700526
527 // Emulate p2p-client exceeding its timeout.
528 test_conf_->SetP2PClientCommandLine("sh -c 'echo http://1.2.3.4/; sleep 2'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700529 manager_->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700530 base::Bind(ExpectUrl, ""));
531 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700532}
533
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700534} // namespace chromeos_update_engine