blob: 12b67095cf7805afc0e3186ad26b143d5f6a9799 [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 Deymo8427b4a2014-11-05 14:00:32 -080019#include <gmock/gmock.h>
20#include <gtest/gtest.h>
David Zeuthen92d9c8b2013-09-11 10:58:11 -070021#include <policy/libpolicy.h>
22#include <policy/mock_device_policy.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070023
David Zeuthen41f2cf52014-11-05 12:29:45 -050024#include "update_engine/fake_clock.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070025#include "update_engine/fake_p2p_manager_configuration.h"
26#include "update_engine/prefs.h"
27#include "update_engine/test_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070028#include "update_engine/update_manager/fake_update_manager.h"
29#include "update_engine/update_manager/mock_policy.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070030#include "update_engine/utils.h"
31
Alex Deymof329b932014-10-30 01:37:48 -070032using base::TimeDelta;
Alex Deymo10875d92014-11-10 21:52:57 -080033using chromeos_update_engine::test_utils::System;
David Zeuthen27a48bc2013-08-06 12:06:29 -070034using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070035using std::unique_ptr;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070036using testing::DoAll;
37using testing::Return;
38using testing::SetArgPointee;
39using testing::_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070040
41namespace chromeos_update_engine {
42
43// Test fixture that sets up a testing configuration (with e.g. a
44// temporary p2p dir) for P2PManager and cleans up when the test is
45// done.
46class P2PManagerTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070047 protected:
Gilad Arnold4a0321b2014-10-28 15:57:30 -070048 P2PManagerTest() : fake_um_(&fake_clock_) {}
David Zeuthen27a48bc2013-08-06 12:06:29 -070049 virtual ~P2PManagerTest() {}
50
51 // Derived from testing::Test.
52 virtual void SetUp() {
53 test_conf_ = new FakeP2PManagerConfiguration();
Gilad Arnold4a0321b2014-10-28 15:57:30 -070054
55 // Allocate and install a mock policy implementation in the fake Update
56 // Manager. Note that the FakeUpdateManager takes ownership of the policy
57 // object.
58 mock_policy_ = new chromeos_update_manager::MockPolicy(&fake_clock_);
59 fake_um_.set_policy(mock_policy_);
60
61 // Construct the P2P manager under test.
62 manager_.reset(P2PManager::Construct(test_conf_, &fake_clock_, &fake_um_,
63 "cros_au", 3,
64 base::TimeDelta::FromDays(5)));
David Zeuthen27a48bc2013-08-06 12:06:29 -070065 }
66 virtual void TearDown() {}
67
68 // The P2PManager::Configuration instance used for testing.
69 FakeP2PManagerConfiguration *test_conf_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070070
71 FakeClock fake_clock_;
72 chromeos_update_manager::MockPolicy *mock_policy_ = nullptr;
73 chromeos_update_manager::FakeUpdateManager fake_um_;
74
75 unique_ptr<P2PManager> manager_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070076};
77
78
Gilad Arnold4a0321b2014-10-28 15:57:30 -070079// Check that IsP2PEnabled() polls the policy correctly, with the value not
80// changing between calls.
81TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
82 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _));
83 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen27a48bc2013-08-06 12:06:29 -070084
Gilad Arnold4a0321b2014-10-28 15:57:30 -070085 EXPECT_FALSE(manager_->IsP2PEnabled());
Alex Deymo10875d92014-11-10 21:52:57 -080086 test_utils::RunGMainLoopMaxIterations(100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -070087 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen92d9c8b2013-09-11 10:58:11 -070088}
89
Gilad Arnold4a0321b2014-10-28 15:57:30 -070090// Check that IsP2PEnabled() polls the policy correctly, with the value changing
91// between calls.
92TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) {
93 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _))
94 .WillOnce(DoAll(
95 SetArgPointee<3>(true),
96 Return(chromeos_update_manager::EvalStatus::kSucceeded)));
97 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true));
98 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen92d9c8b2013-09-11 10:58:11 -070099
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700100 EXPECT_TRUE(manager_->IsP2PEnabled());
Alex Deymo10875d92014-11-10 21:52:57 -0800101 test_utils::RunGMainLoopMaxIterations(100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700102 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400103}
104
David Zeuthen27a48bc2013-08-06 12:06:29 -0700105// Check that we keep the $N newest files with the .$EXT.p2p extension.
David Zeuthen41f2cf52014-11-05 12:29:45 -0500106TEST_F(P2PManagerTest, HousekeepingCountLimit) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700107 // Specifically pass 0 for |max_file_age| to allow files of any age. Note that
108 // we need to reallocate the test_conf_ member, whose currently aliased object
109 // will be freed.
110 test_conf_ = new FakeP2PManagerConfiguration();
111 manager_.reset(P2PManager::Construct(
112 test_conf_, &fake_clock_, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500113 base::TimeDelta() /* max_file_age */));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700114 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700115
Alex Deymo0f513512013-09-13 14:11:26 -0700116 // Generate files with different timestamps matching our pattern and generate
117 // other files not matching the pattern.
118 double last_timestamp = -1;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700119 for (int n = 0; n < 5; n++) {
Alex Deymo0f513512013-09-13 14:11:26 -0700120 double current_timestamp;
121 do {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700122 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
Alex Deymo0f513512013-09-13 14:11:26 -0700123 "file_%d.cros_au.p2p", n));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700124 EXPECT_EQ(0, System(base::StringPrintf("touch %s",
125 file.value().c_str())));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700126
Alex Deymo0f513512013-09-13 14:11:26 -0700127 // Check that the current timestamp on the file is different from the
128 // previous generated file. This timestamp depends on the file system
129 // time resolution, for example, ext2/ext3 have a time resolution of one
130 // second while ext4 has a resolution of one nanosecond. If the assigned
131 // timestamp is the same, we introduce a bigger sleep and call touch
132 // again.
133 struct stat statbuf;
134 EXPECT_EQ(stat(file.value().c_str(), &statbuf), 0);
135 current_timestamp = utils::TimeFromStructTimespec(&statbuf.st_ctim)
136 .ToDoubleT();
137 if (current_timestamp == last_timestamp)
138 sleep(1);
139 } while (current_timestamp == last_timestamp);
140 last_timestamp = current_timestamp;
141
Alex Vakulenko75039d72014-03-25 12:36:28 -0700142 EXPECT_EQ(0, System(base::StringPrintf(
143 "touch %s/file_%d.OTHER.p2p",
144 test_conf_->GetP2PDir().value().c_str(), n)));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700145
Alex Deymo0f513512013-09-13 14:11:26 -0700146 // A sleep of one micro-second is enough to have a different "Change" time
147 // on the file on newer file systems.
148 g_usleep(1);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700149 }
150 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700151 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700152
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700153 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700154
155 // At this point - after HouseKeeping - we should only have
156 // eight files left.
157 for (int n = 0; n < 5; n++) {
158 string file_name;
159 bool expect;
160
161 expect = (n >= 2);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700162 file_name = base::StringPrintf(
163 "%s/file_%d.cros_au.p2p",
164 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700165 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
166
Alex Vakulenko75039d72014-03-25 12:36:28 -0700167 file_name = base::StringPrintf(
168 "%s/file_%d.OTHER.p2p",
169 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700170 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
171 }
172 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700173 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700174}
175
David Zeuthen41f2cf52014-11-05 12:29:45 -0500176// Check that we keep files with the .$EXT.p2p extension not older
177// than some specificed age (5 days, in this test).
178TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
179 // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9
180 // September 2001 - arbitrary number, but constant to avoid test
181 // flakiness) since the epoch and then we put two files before that
182 // date and three files after.
183 time_t cutoff_time = 1000000000;
Alex Deymo10875d92014-11-10 21:52:57 -0800184 TimeDelta age_limit = TimeDelta::FromDays(5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500185
186 // Set the clock just so files with a timestamp before |cutoff_time|
187 // will be deleted at housekeeping.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700188 fake_clock_.SetWallclockTime(base::Time::FromTimeT(cutoff_time) + age_limit);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500189
190 // Specifically pass 0 for |num_files_to_keep| to allow files of any age.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700191 // Note that we need to reallocate the test_conf_ member, whose currently
192 // aliased object will be freed.
193 test_conf_ = new FakeP2PManagerConfiguration();
194 manager_.reset(P2PManager::Construct(
195 test_conf_, &fake_clock_, &fake_um_, "cros_au",
David Zeuthen41f2cf52014-11-05 12:29:45 -0500196 0 /* num_files_to_keep */, age_limit));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700197 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500198
199 // Generate files with different timestamps matching our pattern and generate
200 // other files not matching the pattern.
201 for (int n = 0; n < 5; n++) {
202 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
203 "file_%d.cros_au.p2p", n));
204
205 // With five files and aiming for two of them to be before
206 // |cutoff_time|, we distribute it like this:
207 //
208 // -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 --------
209 // |
210 // cutoff_time
211 //
212 base::Time file_date = base::Time::FromTimeT(cutoff_time) +
Alex Deymo10875d92014-11-10 21:52:57 -0800213 (n - 2)*TimeDelta::FromDays(1) + TimeDelta::FromHours(12);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500214
215 // The touch(1) command expects input like this
216 // --date="2004-02-27 14:19:13.489392193 +0530"
217 base::Time::Exploded exploded;
218 file_date.UTCExplode(&exploded);
219 string file_date_string = base::StringPrintf(
220 "%d-%02d-%02d %02d:%02d:%02d +0000",
221 exploded.year, exploded.month, exploded.day_of_month,
222 exploded.hour, exploded.minute, exploded.second);
223
224 // Sanity check that we generated the correct string.
225 base::Time parsed_time;
226 EXPECT_TRUE(base::Time::FromUTCString(file_date_string.c_str(),
227 &parsed_time));
228 EXPECT_EQ(parsed_time, file_date);
229
230 EXPECT_EQ(0, System(base::StringPrintf("touch --date=\"%s\" %s",
231 file_date_string.c_str(),
232 file.value().c_str())));
233
234 EXPECT_EQ(0, System(base::StringPrintf(
235 "touch --date=\"%s\" %s/file_%d.OTHER.p2p",
236 file_date_string.c_str(),
237 test_conf_->GetP2PDir().value().c_str(), n)));
238 }
239 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700240 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500241
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700242 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen41f2cf52014-11-05 12:29:45 -0500243
244 // At this point - after HouseKeeping - we should only have
245 // eight files left.
246 for (int n = 0; n < 5; n++) {
247 string file_name;
248 bool expect;
249
250 expect = (n >= 2);
251 file_name = base::StringPrintf(
252 "%s/file_%d.cros_au.p2p",
253 test_conf_->GetP2PDir().value().c_str(), n);
254 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
255
256 file_name = base::StringPrintf(
257 "%s/file_%d.OTHER.p2p",
258 test_conf_->GetP2PDir().value().c_str(), n);
259 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
260 }
261 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700262 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500263}
264
David Zeuthen27a48bc2013-08-06 12:06:29 -0700265static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
266 ssize_t expected_size, ssize_t expected_size_xattr) {
267 string path = p2p_dir + "/" + file_name;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700268 char ea_value[64] = { 0 };
269 ssize_t ea_size;
270
Gabe Blacka77939e2014-09-09 23:35:08 -0700271 off_t p2p_size = utils::FileSize(path);
272 if (p2p_size < 0) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700273 LOG(ERROR) << "File " << path << " does not exist";
274 return false;
275 }
276
277 if (expected_size != 0) {
Gabe Blacka77939e2014-09-09 23:35:08 -0700278 if (p2p_size != expected_size) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700279 LOG(ERROR) << "Expected size " << expected_size
Gabe Blacka77939e2014-09-09 23:35:08 -0700280 << " but size was " << p2p_size;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700281 return false;
282 }
283 }
284
285 if (expected_size_xattr == 0) {
286 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
287 &ea_value, sizeof ea_value - 1);
288 if (ea_size == -1 && errno == ENOATTR) {
289 // This is valid behavior as we support files without the xattr set.
290 } else {
291 PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, "
292 << "ea_size=" << ea_size << ", errno=" << errno;
293 return false;
294 }
295 } else {
296 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
297 &ea_value, sizeof ea_value - 1);
298 if (ea_size < 0) {
299 LOG(ERROR) << "Error getting xattr attribute";
300 return false;
301 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700302 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700303 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700304 if (endp == nullptr || *endp != '\0') {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700305 LOG(ERROR) << "Error parsing xattr '" << ea_value
306 << "' as an integer";
307 return false;
308 }
309 if (val != expected_size_xattr) {
310 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
311 << " but size was " << val;
312 return false;
313 }
314 }
315
316 return true;
317}
318
319static bool CreateP2PFile(string p2p_dir, string file_name,
320 size_t size, size_t size_xattr) {
321 string path = p2p_dir + "/" + file_name;
322
323 int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
324 if (fd == -1) {
325 PLOG(ERROR) << "Error creating file with path " << path;
326 return false;
327 }
328 if (ftruncate(fd, size) != 0) {
329 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
330 close(fd);
331 return false;
332 }
333
334 if (size_xattr != 0) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700335 string decimal_size = base::StringPrintf("%zu", size_xattr);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700336 if (fsetxattr(fd, "user.cros-p2p-filesize",
337 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
338 PLOG(ERROR) << "Error setting xattr on " << path;
339 close(fd);
340 return false;
341 }
342 }
343
344 close(fd);
345 return true;
346}
347
348// Check that sharing a *new* file works.
349TEST_F(P2PManagerTest, ShareFile) {
Alex Deymo10875d92014-11-10 21:52:57 -0800350 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700351 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
352 << "Please update your system to support this feature.";
353 return;
354 }
355
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700356 EXPECT_TRUE(manager_->FileShare("foo", 10 * 1000 * 1000));
357 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700358 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
359 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
360 "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000));
361
362 // Sharing it again - with the same expected size - should return true
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700363 EXPECT_TRUE(manager_->FileShare("foo", 10 * 1000 * 1000));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700364
365 // ... but if we use the wrong size, it should fail
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700366 EXPECT_FALSE(manager_->FileShare("foo", 10 * 1000 * 1000 + 1));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700367}
368
369// Check that making a shared file visible, does what is expected.
370TEST_F(P2PManagerTest, MakeFileVisible) {
Alex Deymo10875d92014-11-10 21:52:57 -0800371 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700372 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
373 << "Please update your system to support this feature.";
374 return;
375 }
376
David Zeuthen27a48bc2013-08-06 12:06:29 -0700377 // First, check that it's not visible.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700378 manager_->FileShare("foo", 10*1000*1000);
379 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700380 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
381 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
382 "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000));
383 // Make the file visible and check that it changed its name. Do it
384 // twice to check that FileMakeVisible() is idempotent.
385 for (int n = 0; n < 2; n++) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700386 manager_->FileMakeVisible("foo");
387 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700388 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
389 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
390 "foo.cros_au.p2p", 0, 10 * 1000 * 1000));
391 }
392}
393
394// Check that we return the right values for existing files in P2P_DIR.
395TEST_F(P2PManagerTest, ExistingFiles) {
Alex Deymo10875d92014-11-10 21:52:57 -0800396 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700397 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
398 << "Please update your system to support this feature.";
399 return;
400 }
401
David Zeuthen27a48bc2013-08-06 12:06:29 -0700402 bool visible;
403
404 // Check that errors are returned if the file does not exist
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700405 EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath());
406 EXPECT_EQ(manager_->FileGetSize("foo"), -1);
407 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
408 EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700409 // ... then create the file ...
410 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
411 "foo.cros_au.p2p", 42, 43));
412 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700413 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700414 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700415 EXPECT_EQ(manager_->FileGetSize("foo"), 42);
416 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43);
417 EXPECT_TRUE(manager_->FileGetVisible("foo", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700418 EXPECT_TRUE(visible);
419
420 // One more time, this time with a .tmp variant. First ensure it errors out..
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700421 EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath());
422 EXPECT_EQ(manager_->FileGetSize("bar"), -1);
423 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
424 EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700425 // ... then create the file ...
426 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
427 "bar.cros_au.p2p.tmp", 44, 45));
428 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700429 EXPECT_EQ(manager_->FileGetPath("bar"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700430 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700431 EXPECT_EQ(manager_->FileGetSize("bar"), 44);
432 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45);
433 EXPECT_TRUE(manager_->FileGetVisible("bar", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700434 EXPECT_FALSE(visible);
435}
436
David Zeuthen27a48bc2013-08-06 12:06:29 -0700437// This is a little bit ugly but short of mocking a 'p2p' service this
438// will have to do. E.g. we essentially simulate the various
439// behaviours of initctl(8) that we rely on.
440TEST_F(P2PManagerTest, StartP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700441 // Check that we can start the service
442 test_conf_->SetInitctlStartCommandLine("true");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700443 EXPECT_TRUE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700444 test_conf_->SetInitctlStartCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700445 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700446 test_conf_->SetInitctlStartCommandLine(
447 "sh -c 'echo \"initctl: Job is already running: p2p\" >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700448 EXPECT_TRUE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700449 test_conf_->SetInitctlStartCommandLine(
450 "sh -c 'echo something else >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700451 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700452}
453
454// Same comment as for StartP2P
455TEST_F(P2PManagerTest, StopP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700456 // Check that we can start the service
457 test_conf_->SetInitctlStopCommandLine("true");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700458 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700459 test_conf_->SetInitctlStopCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700460 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700461 test_conf_->SetInitctlStopCommandLine(
462 "sh -c 'echo \"initctl: Unknown instance \" >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700463 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700464 test_conf_->SetInitctlStopCommandLine(
465 "sh -c 'echo something else >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700466 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700467}
468
469static void ExpectUrl(const string& expected_url,
470 GMainLoop* loop,
471 const string& url) {
472 EXPECT_EQ(url, expected_url);
473 g_main_loop_quit(loop);
474}
475
476// Like StartP2P, we're mocking the different results that p2p-client
477// can return. It's not pretty but it works.
478TEST_F(P2PManagerTest, LookupURL) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700479 GMainLoop *loop = g_main_loop_new(nullptr, FALSE);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700480
481 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
482 // being propagated in the right places.
Alex Deymo8ad6da92014-07-15 17:17:45 -0700483 test_conf_->SetP2PClientCommandLine(
484 "echo 'http://1.2.3.4/{file_id}_{minsize}'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700485 manager_->LookupUrlForFile("fooX", 42, TimeDelta(),
486 base::Bind(ExpectUrl,
487 "http://1.2.3.4/fooX.cros_au_42",
488 loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700489 g_main_loop_run(loop);
490
491 // Emulate p2p-client returning invalid URL.
492 test_conf_->SetP2PClientCommandLine("echo 'not_a_valid_url'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700493 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
494 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700495 g_main_loop_run(loop);
496
497 // Emulate p2p-client conveying failure.
498 test_conf_->SetP2PClientCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700499 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
500 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700501 g_main_loop_run(loop);
502
503 // Emulate p2p-client not existing.
504 test_conf_->SetP2PClientCommandLine("/path/to/non/existent/helper/program");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700505 manager_->LookupUrlForFile("foobar", 42,
506 TimeDelta(),
507 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700508 g_main_loop_run(loop);
509
510 // Emulate p2p-client crashing.
511 test_conf_->SetP2PClientCommandLine("sh -c 'kill -SEGV $$'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700512 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
513 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700514 g_main_loop_run(loop);
515
516 // Emulate p2p-client exceeding its timeout.
517 test_conf_->SetP2PClientCommandLine("sh -c 'echo http://1.2.3.4/; sleep 2'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700518 manager_->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
519 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700520 g_main_loop_run(loop);
521
522 g_main_loop_unref(loop);
523}
524
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700525} // namespace chromeos_update_engine