blob: 3b1f1c0ce86271acc9099340c78b584a242964fa [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>
9#include <sys/stat.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070010#include <unistd.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070011#include <attr/xattr.h> // NOLINT - requires typed defined in unistd.h
David Zeuthen27a48bc2013-08-06 12:06:29 -070012
Ben Chan02f7c1d2014-10-18 15:18:02 -070013#include <memory>
Alex Deymob3391552015-07-10 10:48:06 -070014#include <string>
15#include <vector>
Ben Chan02f7c1d2014-10-18 15:18:02 -070016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include <base/bind.h>
18#include <base/callback.h>
Alex Deymo454b7982015-07-10 10:49:29 -070019#include <base/files/file_util.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070020#include <base/strings/stringprintf.h>
Alex Deymob7ca0962014-10-01 17:58:07 -070021#include <chromeos/asynchronous_signal_handler.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070022#include <chromeos/message_loops/glib_message_loop.h>
Alex Deymo509dd532015-06-10 14:11:05 -070023#include <chromeos/message_loops/message_loop.h>
24#include <chromeos/message_loops/message_loop_utils.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080025#include <gmock/gmock.h>
26#include <gtest/gtest.h>
David Zeuthen92d9c8b2013-09-11 10:58:11 -070027#include <policy/libpolicy.h>
28#include <policy/mock_device_policy.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070029
David Zeuthen41f2cf52014-11-05 12:29:45 -050030#include "update_engine/fake_clock.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070031#include "update_engine/fake_p2p_manager_configuration.h"
32#include "update_engine/prefs.h"
33#include "update_engine/test_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070034#include "update_engine/update_manager/fake_update_manager.h"
35#include "update_engine/update_manager/mock_policy.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070036#include "update_engine/utils.h"
37
Alex Deymof329b932014-10-30 01:37:48 -070038using base::TimeDelta;
Alex Deymo509dd532015-06-10 14:11:05 -070039using chromeos::MessageLoop;
David Zeuthen27a48bc2013-08-06 12:06:29 -070040using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070041using std::unique_ptr;
Alex Deymo454b7982015-07-10 10:49:29 -070042using std::vector;
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();
Alex Deymob7ca0962014-10-01 17:58:07 -070061 async_signal_handler_.Init();
62 subprocess_.Init(&async_signal_handler_);
David Zeuthen27a48bc2013-08-06 12:06:29 -070063 test_conf_ = new FakeP2PManagerConfiguration();
Gilad Arnold4a0321b2014-10-28 15:57:30 -070064
65 // Allocate and install a mock policy implementation in the fake Update
66 // Manager. Note that the FakeUpdateManager takes ownership of the policy
67 // object.
68 mock_policy_ = new chromeos_update_manager::MockPolicy(&fake_clock_);
69 fake_um_.set_policy(mock_policy_);
70
71 // Construct the P2P manager under test.
72 manager_.reset(P2PManager::Construct(test_conf_, &fake_clock_, &fake_um_,
73 "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -070074 TimeDelta::FromDays(5)));
David Zeuthen27a48bc2013-08-06 12:06:29 -070075 }
Alex Deymo509dd532015-06-10 14:11:05 -070076
Alex Deymo60ca1a72015-06-18 18:19:15 -070077 // TODO(deymo): Replace this with a FakeMessageLoop. P2PManager uses glib to
78 // interact with the p2p-client tool, so we need to run a GlibMessageLoop
79 // here.
80 chromeos::GlibMessageLoop loop_;
Alex Deymob7ca0962014-10-01 17:58:07 -070081 chromeos::AsynchronousSignalHandler async_signal_handler_;
Alex Deymo461b2592015-07-24 20:10:52 -070082 Subprocess subprocess_;
Alex Deymo60ca1a72015-06-18 18:19:15 -070083
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 Deymo454b7982015-07-10 10:49:29 -0700132 base::Time start_time = base::Time::FromDoubleT(1246996800.);
Alex Deymo0f513512013-09-13 14:11:26 -0700133 // Generate files with different timestamps matching our pattern and generate
134 // other files not matching the pattern.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700135 for (int n = 0; n < 5; n++) {
Alex Deymo454b7982015-07-10 10:49:29 -0700136 base::FilePath path = test_conf_->GetP2PDir().Append(base::StringPrintf(
137 "file_%d.cros_au.p2p", n));
138 base::Time file_time = start_time + TimeDelta::FromMinutes(n);
139 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
140 EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700141
Alex Deymo454b7982015-07-10 10:49:29 -0700142 path = test_conf_->GetP2PDir().Append(base::StringPrintf(
143 "file_%d.OTHER.p2p", n));
144 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
145 EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700146 }
147 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700148 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700149
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700150 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700151
152 // At this point - after HouseKeeping - we should only have
153 // eight files left.
154 for (int n = 0; n < 5; n++) {
155 string file_name;
156 bool expect;
157
158 expect = (n >= 2);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700159 file_name = base::StringPrintf(
160 "%s/file_%d.cros_au.p2p",
161 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700162 EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700163
Alex Vakulenko75039d72014-03-25 12:36:28 -0700164 file_name = base::StringPrintf(
165 "%s/file_%d.OTHER.p2p",
166 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700167 EXPECT_TRUE(utils::FileExists(file_name.c_str()));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700168 }
169 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700170 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700171}
172
David Zeuthen41f2cf52014-11-05 12:29:45 -0500173// Check that we keep files with the .$EXT.p2p extension not older
174// than some specificed age (5 days, in this test).
175TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
176 // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9
177 // September 2001 - arbitrary number, but constant to avoid test
178 // flakiness) since the epoch and then we put two files before that
179 // date and three files after.
Alex Deymo454b7982015-07-10 10:49:29 -0700180 base::Time cutoff_time = base::Time::FromTimeT(1000000000);
Alex Deymo10875d92014-11-10 21:52:57 -0800181 TimeDelta age_limit = TimeDelta::FromDays(5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500182
183 // Set the clock just so files with a timestamp before |cutoff_time|
184 // will be deleted at housekeeping.
Alex Deymo454b7982015-07-10 10:49:29 -0700185 fake_clock_.SetWallclockTime(cutoff_time + age_limit);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500186
Alex Deymo454b7982015-07-10 10:49:29 -0700187 // Specifically pass 0 for |num_files_to_keep| to allow any number of files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700188 // Note that we need to reallocate the test_conf_ member, whose currently
189 // aliased object will be freed.
190 test_conf_ = new FakeP2PManagerConfiguration();
191 manager_.reset(P2PManager::Construct(
192 test_conf_, &fake_clock_, &fake_um_, "cros_au",
David Zeuthen41f2cf52014-11-05 12:29:45 -0500193 0 /* num_files_to_keep */, age_limit));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700194 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500195
196 // Generate files with different timestamps matching our pattern and generate
197 // other files not matching the pattern.
198 for (int n = 0; n < 5; n++) {
Alex Deymo454b7982015-07-10 10:49:29 -0700199 base::FilePath path = test_conf_->GetP2PDir().Append(base::StringPrintf(
David Zeuthen41f2cf52014-11-05 12:29:45 -0500200 "file_%d.cros_au.p2p", n));
201
202 // With five files and aiming for two of them to be before
203 // |cutoff_time|, we distribute it like this:
204 //
205 // -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 --------
206 // |
207 // cutoff_time
208 //
Alex Deymo454b7982015-07-10 10:49:29 -0700209 base::Time file_date = cutoff_time + (n - 2) * TimeDelta::FromDays(1)
210 + TimeDelta::FromHours(12);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500211
Alex Deymo454b7982015-07-10 10:49:29 -0700212 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
213 EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500214
Alex Deymo454b7982015-07-10 10:49:29 -0700215 path = test_conf_->GetP2PDir().Append(base::StringPrintf(
216 "file_%d.OTHER.p2p", n));
217 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
218 EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500219 }
220 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700221 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500222
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700223 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen41f2cf52014-11-05 12:29:45 -0500224
225 // At this point - after HouseKeeping - we should only have
226 // eight files left.
227 for (int n = 0; n < 5; n++) {
228 string file_name;
229 bool expect;
230
231 expect = (n >= 2);
232 file_name = base::StringPrintf(
233 "%s/file_%d.cros_au.p2p",
234 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700235 EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500236
237 file_name = base::StringPrintf(
238 "%s/file_%d.OTHER.p2p",
239 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700240 EXPECT_TRUE(utils::FileExists(file_name.c_str()));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500241 }
242 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700243 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500244}
245
David Zeuthen27a48bc2013-08-06 12:06:29 -0700246static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
247 ssize_t expected_size, ssize_t expected_size_xattr) {
248 string path = p2p_dir + "/" + file_name;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700249 char ea_value[64] = { 0 };
250 ssize_t ea_size;
251
Gabe Blacka77939e2014-09-09 23:35:08 -0700252 off_t p2p_size = utils::FileSize(path);
253 if (p2p_size < 0) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700254 LOG(ERROR) << "File " << path << " does not exist";
255 return false;
256 }
257
258 if (expected_size != 0) {
Gabe Blacka77939e2014-09-09 23:35:08 -0700259 if (p2p_size != expected_size) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700260 LOG(ERROR) << "Expected size " << expected_size
Gabe Blacka77939e2014-09-09 23:35:08 -0700261 << " but size was " << p2p_size;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700262 return false;
263 }
264 }
265
266 if (expected_size_xattr == 0) {
267 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
268 &ea_value, sizeof ea_value - 1);
269 if (ea_size == -1 && errno == ENOATTR) {
270 // This is valid behavior as we support files without the xattr set.
271 } else {
272 PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, "
273 << "ea_size=" << ea_size << ", errno=" << errno;
274 return false;
275 }
276 } else {
277 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
278 &ea_value, sizeof ea_value - 1);
279 if (ea_size < 0) {
280 LOG(ERROR) << "Error getting xattr attribute";
281 return false;
282 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700283 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700284 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700285 if (endp == nullptr || *endp != '\0') {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700286 LOG(ERROR) << "Error parsing xattr '" << ea_value
287 << "' as an integer";
288 return false;
289 }
290 if (val != expected_size_xattr) {
291 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
292 << " but size was " << val;
293 return false;
294 }
295 }
296
297 return true;
298}
299
300static bool CreateP2PFile(string p2p_dir, string file_name,
301 size_t size, size_t size_xattr) {
302 string path = p2p_dir + "/" + file_name;
303
304 int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
305 if (fd == -1) {
306 PLOG(ERROR) << "Error creating file with path " << path;
307 return false;
308 }
309 if (ftruncate(fd, size) != 0) {
310 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
311 close(fd);
312 return false;
313 }
314
315 if (size_xattr != 0) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700316 string decimal_size = base::StringPrintf("%zu", size_xattr);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700317 if (fsetxattr(fd, "user.cros-p2p-filesize",
318 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
319 PLOG(ERROR) << "Error setting xattr on " << path;
320 close(fd);
321 return false;
322 }
323 }
324
325 close(fd);
326 return true;
327}
328
329// Check that sharing a *new* file works.
330TEST_F(P2PManagerTest, ShareFile) {
Alex Deymo10875d92014-11-10 21:52:57 -0800331 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700332 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
333 << "Please update your system to support this feature.";
334 return;
335 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800336 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700337
Alex Deymo3c3807c2015-01-30 09:32:41 -0800338 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700339 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700340 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
341 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800342 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700343
344 // Sharing it again - with the same expected size - should return true
Alex Deymo3c3807c2015-01-30 09:32:41 -0800345 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700346
347 // ... but if we use the wrong size, it should fail
Alex Deymo3c3807c2015-01-30 09:32:41 -0800348 EXPECT_FALSE(manager_->FileShare("foo", kP2PTestFileSize + 1));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700349}
350
351// Check that making a shared file visible, does what is expected.
352TEST_F(P2PManagerTest, MakeFileVisible) {
Alex Deymo10875d92014-11-10 21:52:57 -0800353 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700354 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
355 << "Please update your system to support this feature.";
356 return;
357 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800358 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700359
David Zeuthen27a48bc2013-08-06 12:06:29 -0700360 // First, check that it's not visible.
Alex Deymo3c3807c2015-01-30 09:32:41 -0800361 manager_->FileShare("foo", kP2PTestFileSize);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700362 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700363 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
364 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800365 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700366 // Make the file visible and check that it changed its name. Do it
367 // twice to check that FileMakeVisible() is idempotent.
368 for (int n = 0; n < 2; n++) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700369 manager_->FileMakeVisible("foo");
370 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700371 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
372 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800373 "foo.cros_au.p2p", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700374 }
375}
376
377// Check that we return the right values for existing files in P2P_DIR.
378TEST_F(P2PManagerTest, ExistingFiles) {
Alex Deymo10875d92014-11-10 21:52:57 -0800379 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700380 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
381 << "Please update your system to support this feature.";
382 return;
383 }
384
David Zeuthen27a48bc2013-08-06 12:06:29 -0700385 bool visible;
386
387 // Check that errors are returned if the file does not exist
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700388 EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath());
389 EXPECT_EQ(manager_->FileGetSize("foo"), -1);
390 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
391 EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700392 // ... then create the file ...
393 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
394 "foo.cros_au.p2p", 42, 43));
395 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700396 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700397 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700398 EXPECT_EQ(manager_->FileGetSize("foo"), 42);
399 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43);
400 EXPECT_TRUE(manager_->FileGetVisible("foo", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700401 EXPECT_TRUE(visible);
402
403 // One more time, this time with a .tmp variant. First ensure it errors out..
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700404 EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath());
405 EXPECT_EQ(manager_->FileGetSize("bar"), -1);
406 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
407 EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700408 // ... then create the file ...
409 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
410 "bar.cros_au.p2p.tmp", 44, 45));
411 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700412 EXPECT_EQ(manager_->FileGetPath("bar"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700413 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700414 EXPECT_EQ(manager_->FileGetSize("bar"), 44);
415 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45);
416 EXPECT_TRUE(manager_->FileGetVisible("bar", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700417 EXPECT_FALSE(visible);
418}
419
David Zeuthen27a48bc2013-08-06 12:06:29 -0700420// This is a little bit ugly but short of mocking a 'p2p' service this
421// will have to do. E.g. we essentially simulate the various
422// behaviours of initctl(8) that we rely on.
423TEST_F(P2PManagerTest, StartP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700424 // Check that we can start the service
Alex Deymob3391552015-07-10 10:48:06 -0700425 test_conf_->SetInitctlStartCommand({"true"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700426 EXPECT_TRUE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700427 test_conf_->SetInitctlStartCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700428 EXPECT_FALSE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700429 test_conf_->SetInitctlStartCommand({
430 "sh", "-c", "echo \"initctl: Job is already running: p2p\" >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700431 EXPECT_TRUE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700432 test_conf_->SetInitctlStartCommand({
433 "sh", "-c", "echo something else >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700434 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700435}
436
437// Same comment as for StartP2P
438TEST_F(P2PManagerTest, StopP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700439 // Check that we can start the service
Alex Deymob3391552015-07-10 10:48:06 -0700440 test_conf_->SetInitctlStopCommand({"true"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700441 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700442 test_conf_->SetInitctlStopCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700443 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700444 test_conf_->SetInitctlStopCommand({
445 "sh", "-c", "echo \"initctl: Unknown instance \" >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700446 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700447 test_conf_->SetInitctlStopCommand({
448 "sh", "-c", "echo something else >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700449 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700450}
451
452static void ExpectUrl(const string& expected_url,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700453 const string& url) {
454 EXPECT_EQ(url, expected_url);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700455 MessageLoop::current()->BreakLoop();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700456}
457
458// Like StartP2P, we're mocking the different results that p2p-client
459// can return. It's not pretty but it works.
460TEST_F(P2PManagerTest, LookupURL) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700461 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
462 // being propagated in the right places.
Alex Deymob3391552015-07-10 10:48:06 -0700463 test_conf_->SetP2PClientCommand({
464 "echo", "http://1.2.3.4/{file_id}_{minsize}"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700465 manager_->LookupUrlForFile("fooX", 42, TimeDelta(),
466 base::Bind(ExpectUrl,
Alex Deymo60ca1a72015-06-18 18:19:15 -0700467 "http://1.2.3.4/fooX.cros_au_42"));
468 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700469
470 // Emulate p2p-client returning invalid URL.
Alex Deymob3391552015-07-10 10:48:06 -0700471 test_conf_->SetP2PClientCommand({"echo", "not_a_valid_url"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700472 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700473 base::Bind(ExpectUrl, ""));
474 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700475
476 // Emulate p2p-client conveying failure.
Alex Deymob3391552015-07-10 10:48:06 -0700477 test_conf_->SetP2PClientCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700478 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700479 base::Bind(ExpectUrl, ""));
480 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700481
482 // Emulate p2p-client not existing.
Alex Deymob3391552015-07-10 10:48:06 -0700483 test_conf_->SetP2PClientCommand({"/path/to/non/existent/helper/program"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700484 manager_->LookupUrlForFile("foobar", 42,
485 TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700486 base::Bind(ExpectUrl, ""));
487 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700488
489 // Emulate p2p-client crashing.
Alex Deymob3391552015-07-10 10:48:06 -0700490 test_conf_->SetP2PClientCommand({"sh", "-c", "kill -SEGV $$"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700491 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700492 base::Bind(ExpectUrl, ""));
493 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700494
495 // Emulate p2p-client exceeding its timeout.
Alex Deymob3391552015-07-10 10:48:06 -0700496 test_conf_->SetP2PClientCommand({
497 "sh", "-c", "echo http://1.2.3.4/; sleep 2"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700498 manager_->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700499 base::Bind(ExpectUrl, ""));
500 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700501}
502
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700503} // namespace chromeos_update_engine