blob: 990fb3e647d336ea84a1fbc211003d6b737d61d5 [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 Deymo509dd532015-06-10 14:11:05 -070019#include <chromeos/message_loops/fake_message_loop.h>
20#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 {
73 EXPECT_FALSE(loop_.PendingTasks());
74 }
David Zeuthen27a48bc2013-08-06 12:06:29 -070075
76 // The P2PManager::Configuration instance used for testing.
77 FakeP2PManagerConfiguration *test_conf_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070078
Alex Deymo509dd532015-06-10 14:11:05 -070079 chromeos::FakeMessageLoop loop_{nullptr};
Gilad Arnold4a0321b2014-10-28 15:57:30 -070080 FakeClock fake_clock_;
81 chromeos_update_manager::MockPolicy *mock_policy_ = nullptr;
82 chromeos_update_manager::FakeUpdateManager fake_um_;
83
84 unique_ptr<P2PManager> manager_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070085};
86
87
Gilad Arnold4a0321b2014-10-28 15:57:30 -070088// Check that IsP2PEnabled() polls the policy correctly, with the value not
89// changing between calls.
90TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
91 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _));
92 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen27a48bc2013-08-06 12:06:29 -070093
Gilad Arnold4a0321b2014-10-28 15:57:30 -070094 EXPECT_FALSE(manager_->IsP2PEnabled());
Alex Deymo509dd532015-06-10 14:11:05 -070095 chromeos::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -070096 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen92d9c8b2013-09-11 10:58:11 -070097}
98
Gilad Arnold4a0321b2014-10-28 15:57:30 -070099// Check that IsP2PEnabled() polls the policy correctly, with the value changing
100// between calls.
101TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) {
102 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _))
103 .WillOnce(DoAll(
104 SetArgPointee<3>(true),
105 Return(chromeos_update_manager::EvalStatus::kSucceeded)));
106 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true));
107 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700108
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700109 EXPECT_TRUE(manager_->IsP2PEnabled());
Alex Deymo509dd532015-06-10 14:11:05 -0700110 chromeos::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700111 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400112}
113
David Zeuthen27a48bc2013-08-06 12:06:29 -0700114// Check that we keep the $N newest files with the .$EXT.p2p extension.
David Zeuthen41f2cf52014-11-05 12:29:45 -0500115TEST_F(P2PManagerTest, HousekeepingCountLimit) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700116 // Specifically pass 0 for |max_file_age| to allow files of any age. Note that
117 // we need to reallocate the test_conf_ member, whose currently aliased object
118 // will be freed.
119 test_conf_ = new FakeP2PManagerConfiguration();
120 manager_.reset(P2PManager::Construct(
121 test_conf_, &fake_clock_, &fake_um_, "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -0700122 TimeDelta() /* max_file_age */));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700123 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700124
Alex Deymo0f513512013-09-13 14:11:26 -0700125 // Generate files with different timestamps matching our pattern and generate
126 // other files not matching the pattern.
127 double last_timestamp = -1;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700128 for (int n = 0; n < 5; n++) {
Alex Deymo0f513512013-09-13 14:11:26 -0700129 double current_timestamp;
130 do {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700131 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
Alex Deymo0f513512013-09-13 14:11:26 -0700132 "file_%d.cros_au.p2p", n));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700133 EXPECT_EQ(0, System(base::StringPrintf("touch %s",
134 file.value().c_str())));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700135
Alex Deymo0f513512013-09-13 14:11:26 -0700136 // Check that the current timestamp on the file is different from the
137 // previous generated file. This timestamp depends on the file system
138 // time resolution, for example, ext2/ext3 have a time resolution of one
139 // second while ext4 has a resolution of one nanosecond. If the assigned
140 // timestamp is the same, we introduce a bigger sleep and call touch
141 // again.
142 struct stat statbuf;
143 EXPECT_EQ(stat(file.value().c_str(), &statbuf), 0);
144 current_timestamp = utils::TimeFromStructTimespec(&statbuf.st_ctim)
145 .ToDoubleT();
146 if (current_timestamp == last_timestamp)
147 sleep(1);
148 } while (current_timestamp == last_timestamp);
149 last_timestamp = current_timestamp;
150
Alex Vakulenko75039d72014-03-25 12:36:28 -0700151 EXPECT_EQ(0, System(base::StringPrintf(
152 "touch %s/file_%d.OTHER.p2p",
153 test_conf_->GetP2PDir().value().c_str(), n)));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700154
Alex Deymo0f513512013-09-13 14:11:26 -0700155 // A sleep of one micro-second is enough to have a different "Change" time
156 // on the file on newer file systems.
157 g_usleep(1);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700158 }
159 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700160 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700161
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700162 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700163
164 // At this point - after HouseKeeping - we should only have
165 // eight files left.
166 for (int n = 0; n < 5; n++) {
167 string file_name;
168 bool expect;
169
170 expect = (n >= 2);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700171 file_name = base::StringPrintf(
172 "%s/file_%d.cros_au.p2p",
173 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700174 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
175
Alex Vakulenko75039d72014-03-25 12:36:28 -0700176 file_name = base::StringPrintf(
177 "%s/file_%d.OTHER.p2p",
178 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700179 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
180 }
181 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700182 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700183}
184
David Zeuthen41f2cf52014-11-05 12:29:45 -0500185// Check that we keep files with the .$EXT.p2p extension not older
186// than some specificed age (5 days, in this test).
187TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
188 // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9
189 // September 2001 - arbitrary number, but constant to avoid test
190 // flakiness) since the epoch and then we put two files before that
191 // date and three files after.
192 time_t cutoff_time = 1000000000;
Alex Deymo10875d92014-11-10 21:52:57 -0800193 TimeDelta age_limit = TimeDelta::FromDays(5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500194
195 // Set the clock just so files with a timestamp before |cutoff_time|
196 // will be deleted at housekeeping.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700197 fake_clock_.SetWallclockTime(base::Time::FromTimeT(cutoff_time) + age_limit);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500198
199 // Specifically pass 0 for |num_files_to_keep| to allow files of any age.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700200 // Note that we need to reallocate the test_conf_ member, whose currently
201 // aliased object will be freed.
202 test_conf_ = new FakeP2PManagerConfiguration();
203 manager_.reset(P2PManager::Construct(
204 test_conf_, &fake_clock_, &fake_um_, "cros_au",
David Zeuthen41f2cf52014-11-05 12:29:45 -0500205 0 /* num_files_to_keep */, age_limit));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700206 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500207
208 // Generate files with different timestamps matching our pattern and generate
209 // other files not matching the pattern.
210 for (int n = 0; n < 5; n++) {
211 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
212 "file_%d.cros_au.p2p", n));
213
214 // With five files and aiming for two of them to be before
215 // |cutoff_time|, we distribute it like this:
216 //
217 // -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 --------
218 // |
219 // cutoff_time
220 //
221 base::Time file_date = base::Time::FromTimeT(cutoff_time) +
Alex Deymo10875d92014-11-10 21:52:57 -0800222 (n - 2)*TimeDelta::FromDays(1) + TimeDelta::FromHours(12);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500223
224 // The touch(1) command expects input like this
225 // --date="2004-02-27 14:19:13.489392193 +0530"
226 base::Time::Exploded exploded;
227 file_date.UTCExplode(&exploded);
228 string file_date_string = base::StringPrintf(
229 "%d-%02d-%02d %02d:%02d:%02d +0000",
230 exploded.year, exploded.month, exploded.day_of_month,
231 exploded.hour, exploded.minute, exploded.second);
232
233 // Sanity check that we generated the correct string.
234 base::Time parsed_time;
235 EXPECT_TRUE(base::Time::FromUTCString(file_date_string.c_str(),
236 &parsed_time));
237 EXPECT_EQ(parsed_time, file_date);
238
239 EXPECT_EQ(0, System(base::StringPrintf("touch --date=\"%s\" %s",
240 file_date_string.c_str(),
241 file.value().c_str())));
242
243 EXPECT_EQ(0, System(base::StringPrintf(
244 "touch --date=\"%s\" %s/file_%d.OTHER.p2p",
245 file_date_string.c_str(),
246 test_conf_->GetP2PDir().value().c_str(), n)));
247 }
248 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700249 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500250
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700251 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen41f2cf52014-11-05 12:29:45 -0500252
253 // At this point - after HouseKeeping - we should only have
254 // eight files left.
255 for (int n = 0; n < 5; n++) {
256 string file_name;
257 bool expect;
258
259 expect = (n >= 2);
260 file_name = base::StringPrintf(
261 "%s/file_%d.cros_au.p2p",
262 test_conf_->GetP2PDir().value().c_str(), n);
263 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
264
265 file_name = base::StringPrintf(
266 "%s/file_%d.OTHER.p2p",
267 test_conf_->GetP2PDir().value().c_str(), n);
268 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
269 }
270 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700271 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500272}
273
David Zeuthen27a48bc2013-08-06 12:06:29 -0700274static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
275 ssize_t expected_size, ssize_t expected_size_xattr) {
276 string path = p2p_dir + "/" + file_name;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700277 char ea_value[64] = { 0 };
278 ssize_t ea_size;
279
Gabe Blacka77939e2014-09-09 23:35:08 -0700280 off_t p2p_size = utils::FileSize(path);
281 if (p2p_size < 0) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700282 LOG(ERROR) << "File " << path << " does not exist";
283 return false;
284 }
285
286 if (expected_size != 0) {
Gabe Blacka77939e2014-09-09 23:35:08 -0700287 if (p2p_size != expected_size) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700288 LOG(ERROR) << "Expected size " << expected_size
Gabe Blacka77939e2014-09-09 23:35:08 -0700289 << " but size was " << p2p_size;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700290 return false;
291 }
292 }
293
294 if (expected_size_xattr == 0) {
295 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
296 &ea_value, sizeof ea_value - 1);
297 if (ea_size == -1 && errno == ENOATTR) {
298 // This is valid behavior as we support files without the xattr set.
299 } else {
300 PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, "
301 << "ea_size=" << ea_size << ", errno=" << errno;
302 return false;
303 }
304 } else {
305 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
306 &ea_value, sizeof ea_value - 1);
307 if (ea_size < 0) {
308 LOG(ERROR) << "Error getting xattr attribute";
309 return false;
310 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700311 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700312 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700313 if (endp == nullptr || *endp != '\0') {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700314 LOG(ERROR) << "Error parsing xattr '" << ea_value
315 << "' as an integer";
316 return false;
317 }
318 if (val != expected_size_xattr) {
319 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
320 << " but size was " << val;
321 return false;
322 }
323 }
324
325 return true;
326}
327
328static bool CreateP2PFile(string p2p_dir, string file_name,
329 size_t size, size_t size_xattr) {
330 string path = p2p_dir + "/" + file_name;
331
332 int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
333 if (fd == -1) {
334 PLOG(ERROR) << "Error creating file with path " << path;
335 return false;
336 }
337 if (ftruncate(fd, size) != 0) {
338 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
339 close(fd);
340 return false;
341 }
342
343 if (size_xattr != 0) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700344 string decimal_size = base::StringPrintf("%zu", size_xattr);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700345 if (fsetxattr(fd, "user.cros-p2p-filesize",
346 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
347 PLOG(ERROR) << "Error setting xattr on " << path;
348 close(fd);
349 return false;
350 }
351 }
352
353 close(fd);
354 return true;
355}
356
357// Check that sharing a *new* file works.
358TEST_F(P2PManagerTest, ShareFile) {
Alex Deymo10875d92014-11-10 21:52:57 -0800359 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700360 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
361 << "Please update your system to support this feature.";
362 return;
363 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800364 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700365
Alex Deymo3c3807c2015-01-30 09:32:41 -0800366 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700367 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700368 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
369 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800370 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700371
372 // Sharing it again - with the same expected size - should return true
Alex Deymo3c3807c2015-01-30 09:32:41 -0800373 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700374
375 // ... but if we use the wrong size, it should fail
Alex Deymo3c3807c2015-01-30 09:32:41 -0800376 EXPECT_FALSE(manager_->FileShare("foo", kP2PTestFileSize + 1));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700377}
378
379// Check that making a shared file visible, does what is expected.
380TEST_F(P2PManagerTest, MakeFileVisible) {
Alex Deymo10875d92014-11-10 21:52:57 -0800381 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700382 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
383 << "Please update your system to support this feature.";
384 return;
385 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800386 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700387
David Zeuthen27a48bc2013-08-06 12:06:29 -0700388 // First, check that it's not visible.
Alex Deymo3c3807c2015-01-30 09:32:41 -0800389 manager_->FileShare("foo", kP2PTestFileSize);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700390 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700391 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
392 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800393 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700394 // Make the file visible and check that it changed its name. Do it
395 // twice to check that FileMakeVisible() is idempotent.
396 for (int n = 0; n < 2; n++) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700397 manager_->FileMakeVisible("foo");
398 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700399 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
400 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800401 "foo.cros_au.p2p", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700402 }
403}
404
405// Check that we return the right values for existing files in P2P_DIR.
406TEST_F(P2PManagerTest, ExistingFiles) {
Alex Deymo10875d92014-11-10 21:52:57 -0800407 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700408 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
409 << "Please update your system to support this feature.";
410 return;
411 }
412
David Zeuthen27a48bc2013-08-06 12:06:29 -0700413 bool visible;
414
415 // Check that errors are returned if the file does not exist
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700416 EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath());
417 EXPECT_EQ(manager_->FileGetSize("foo"), -1);
418 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
419 EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700420 // ... then create the file ...
421 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
422 "foo.cros_au.p2p", 42, 43));
423 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700424 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700425 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700426 EXPECT_EQ(manager_->FileGetSize("foo"), 42);
427 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43);
428 EXPECT_TRUE(manager_->FileGetVisible("foo", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700429 EXPECT_TRUE(visible);
430
431 // One more time, this time with a .tmp variant. First ensure it errors out..
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700432 EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath());
433 EXPECT_EQ(manager_->FileGetSize("bar"), -1);
434 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
435 EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700436 // ... then create the file ...
437 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
438 "bar.cros_au.p2p.tmp", 44, 45));
439 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700440 EXPECT_EQ(manager_->FileGetPath("bar"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700441 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700442 EXPECT_EQ(manager_->FileGetSize("bar"), 44);
443 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45);
444 EXPECT_TRUE(manager_->FileGetVisible("bar", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700445 EXPECT_FALSE(visible);
446}
447
David Zeuthen27a48bc2013-08-06 12:06:29 -0700448// This is a little bit ugly but short of mocking a 'p2p' service this
449// will have to do. E.g. we essentially simulate the various
450// behaviours of initctl(8) that we rely on.
451TEST_F(P2PManagerTest, StartP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700452 // Check that we can start the service
453 test_conf_->SetInitctlStartCommandLine("true");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700454 EXPECT_TRUE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700455 test_conf_->SetInitctlStartCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700456 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700457 test_conf_->SetInitctlStartCommandLine(
458 "sh -c 'echo \"initctl: Job is already running: p2p\" >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700459 EXPECT_TRUE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700460 test_conf_->SetInitctlStartCommandLine(
461 "sh -c 'echo something else >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700462 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700463}
464
465// Same comment as for StartP2P
466TEST_F(P2PManagerTest, StopP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700467 // Check that we can start the service
468 test_conf_->SetInitctlStopCommandLine("true");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700469 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700470 test_conf_->SetInitctlStopCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700471 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700472 test_conf_->SetInitctlStopCommandLine(
473 "sh -c 'echo \"initctl: Unknown instance \" >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700474 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700475 test_conf_->SetInitctlStopCommandLine(
476 "sh -c 'echo something else >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700477 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700478}
479
480static void ExpectUrl(const string& expected_url,
481 GMainLoop* loop,
482 const string& url) {
483 EXPECT_EQ(url, expected_url);
484 g_main_loop_quit(loop);
485}
486
487// Like StartP2P, we're mocking the different results that p2p-client
488// can return. It's not pretty but it works.
489TEST_F(P2PManagerTest, LookupURL) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700490 GMainLoop *loop = g_main_loop_new(nullptr, FALSE);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700491
492 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
493 // being propagated in the right places.
Alex Deymo8ad6da92014-07-15 17:17:45 -0700494 test_conf_->SetP2PClientCommandLine(
495 "echo 'http://1.2.3.4/{file_id}_{minsize}'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700496 manager_->LookupUrlForFile("fooX", 42, TimeDelta(),
497 base::Bind(ExpectUrl,
498 "http://1.2.3.4/fooX.cros_au_42",
499 loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700500 g_main_loop_run(loop);
501
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(),
505 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700506 g_main_loop_run(loop);
507
508 // Emulate p2p-client conveying failure.
509 test_conf_->SetP2PClientCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700510 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
511 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700512 g_main_loop_run(loop);
513
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(),
518 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700519 g_main_loop_run(loop);
520
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(),
524 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700525 g_main_loop_run(loop);
526
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),
530 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700531 g_main_loop_run(loop);
532
533 g_main_loop_unref(loop);
534}
535
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700536} // namespace chromeos_update_engine