blob: 18b4184754f4e42ccd1147b10795819864f1bf41 [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 Deymo0b3db6b2015-08-10 15:19:37 -070020#include <base/message_loop/message_loop.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/strings/stringprintf.h>
Alex Deymob7ca0962014-10-01 17:58:07 -070022#include <chromeos/asynchronous_signal_handler.h>
Alex Deymo0b3db6b2015-08-10 15:19:37 -070023#include <chromeos/message_loops/base_message_loop.h>
Alex Deymo509dd532015-06-10 14:11:05 -070024#include <chromeos/message_loops/message_loop.h>
25#include <chromeos/message_loops/message_loop_utils.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080026#include <gmock/gmock.h>
27#include <gtest/gtest.h>
David Zeuthen92d9c8b2013-09-11 10:58:11 -070028#include <policy/libpolicy.h>
29#include <policy/mock_device_policy.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070030
David Zeuthen41f2cf52014-11-05 12:29:45 -050031#include "update_engine/fake_clock.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070032#include "update_engine/fake_p2p_manager_configuration.h"
33#include "update_engine/prefs.h"
34#include "update_engine/test_utils.h"
Gilad Arnold4a0321b2014-10-28 15:57:30 -070035#include "update_engine/update_manager/fake_update_manager.h"
36#include "update_engine/update_manager/mock_policy.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070037#include "update_engine/utils.h"
38
Alex Deymof329b932014-10-30 01:37:48 -070039using base::TimeDelta;
Alex Deymo509dd532015-06-10 14:11:05 -070040using chromeos::MessageLoop;
David Zeuthen27a48bc2013-08-06 12:06:29 -070041using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070042using std::unique_ptr;
Alex Deymo454b7982015-07-10 10:49:29 -070043using std::vector;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070044using testing::DoAll;
45using testing::Return;
46using testing::SetArgPointee;
47using testing::_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070048
49namespace chromeos_update_engine {
50
51// Test fixture that sets up a testing configuration (with e.g. a
52// temporary p2p dir) for P2PManager and cleans up when the test is
53// done.
54class P2PManagerTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070055 protected:
Gilad Arnold4a0321b2014-10-28 15:57:30 -070056 P2PManagerTest() : fake_um_(&fake_clock_) {}
Alex Deymo610277e2014-11-11 21:18:11 -080057 ~P2PManagerTest() override {}
David Zeuthen27a48bc2013-08-06 12:06:29 -070058
59 // Derived from testing::Test.
Alex Deymo610277e2014-11-11 21:18:11 -080060 void SetUp() override {
Alex Deymo509dd532015-06-10 14:11:05 -070061 loop_.SetAsCurrent();
Alex Deymob7ca0962014-10-01 17:58:07 -070062 async_signal_handler_.Init();
63 subprocess_.Init(&async_signal_handler_);
David Zeuthen27a48bc2013-08-06 12:06:29 -070064 test_conf_ = new FakeP2PManagerConfiguration();
Gilad Arnold4a0321b2014-10-28 15:57:30 -070065
66 // Allocate and install a mock policy implementation in the fake Update
67 // Manager. Note that the FakeUpdateManager takes ownership of the policy
68 // object.
69 mock_policy_ = new chromeos_update_manager::MockPolicy(&fake_clock_);
70 fake_um_.set_policy(mock_policy_);
71
72 // Construct the P2P manager under test.
73 manager_.reset(P2PManager::Construct(test_conf_, &fake_clock_, &fake_um_,
74 "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -070075 TimeDelta::FromDays(5)));
David Zeuthen27a48bc2013-08-06 12:06:29 -070076 }
Alex Deymo509dd532015-06-10 14:11:05 -070077
Alex Deymo0b3db6b2015-08-10 15:19:37 -070078 base::MessageLoopForIO base_loop_;
79 chromeos::BaseMessageLoop loop_{&base_loop_};
Alex Deymob7ca0962014-10-01 17:58:07 -070080 chromeos::AsynchronousSignalHandler async_signal_handler_;
Alex Deymo461b2592015-07-24 20:10:52 -070081 Subprocess subprocess_;
Alex Deymo60ca1a72015-06-18 18:19:15 -070082
David Zeuthen27a48bc2013-08-06 12:06:29 -070083 // The P2PManager::Configuration instance used for testing.
84 FakeP2PManagerConfiguration *test_conf_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070085
86 FakeClock fake_clock_;
87 chromeos_update_manager::MockPolicy *mock_policy_ = nullptr;
88 chromeos_update_manager::FakeUpdateManager fake_um_;
89
90 unique_ptr<P2PManager> manager_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070091};
92
93
Gilad Arnold4a0321b2014-10-28 15:57:30 -070094// Check that IsP2PEnabled() polls the policy correctly, with the value not
95// changing between calls.
96TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
97 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _));
98 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen27a48bc2013-08-06 12:06:29 -070099
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700100 EXPECT_FALSE(manager_->IsP2PEnabled());
Alex Deymo509dd532015-06-10 14:11:05 -0700101 chromeos::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700102 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700103}
104
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700105// Check that IsP2PEnabled() polls the policy correctly, with the value changing
106// between calls.
107TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) {
108 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _))
109 .WillOnce(DoAll(
110 SetArgPointee<3>(true),
111 Return(chromeos_update_manager::EvalStatus::kSucceeded)));
112 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true));
113 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700114
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700115 EXPECT_TRUE(manager_->IsP2PEnabled());
Alex Deymo509dd532015-06-10 14:11:05 -0700116 chromeos::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700117 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400118}
119
David Zeuthen27a48bc2013-08-06 12:06:29 -0700120// Check that we keep the $N newest files with the .$EXT.p2p extension.
David Zeuthen41f2cf52014-11-05 12:29:45 -0500121TEST_F(P2PManagerTest, HousekeepingCountLimit) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700122 // Specifically pass 0 for |max_file_age| to allow files of any age. Note that
123 // we need to reallocate the test_conf_ member, whose currently aliased object
124 // will be freed.
125 test_conf_ = new FakeP2PManagerConfiguration();
126 manager_.reset(P2PManager::Construct(
127 test_conf_, &fake_clock_, &fake_um_, "cros_au", 3,
Alex Deymo509dd532015-06-10 14:11:05 -0700128 TimeDelta() /* max_file_age */));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700129 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700130
Alex Deymo454b7982015-07-10 10:49:29 -0700131 base::Time start_time = base::Time::FromDoubleT(1246996800.);
Alex Deymo0f513512013-09-13 14:11:26 -0700132 // Generate files with different timestamps matching our pattern and generate
133 // other files not matching the pattern.
David Zeuthen27a48bc2013-08-06 12:06:29 -0700134 for (int n = 0; n < 5; n++) {
Alex Deymo454b7982015-07-10 10:49:29 -0700135 base::FilePath path = test_conf_->GetP2PDir().Append(base::StringPrintf(
136 "file_%d.cros_au.p2p", n));
137 base::Time file_time = start_time + TimeDelta::FromMinutes(n);
138 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
139 EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700140
Alex Deymo454b7982015-07-10 10:49:29 -0700141 path = test_conf_->GetP2PDir().Append(base::StringPrintf(
142 "file_%d.OTHER.p2p", n));
143 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
144 EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700145 }
146 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700147 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700148
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700149 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700150
151 // At this point - after HouseKeeping - we should only have
152 // eight files left.
153 for (int n = 0; n < 5; n++) {
154 string file_name;
155 bool expect;
156
157 expect = (n >= 2);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700158 file_name = base::StringPrintf(
159 "%s/file_%d.cros_au.p2p",
160 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700161 EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700162
Alex Vakulenko75039d72014-03-25 12:36:28 -0700163 file_name = base::StringPrintf(
164 "%s/file_%d.OTHER.p2p",
165 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700166 EXPECT_TRUE(utils::FileExists(file_name.c_str()));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700167 }
168 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700169 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700170}
171
David Zeuthen41f2cf52014-11-05 12:29:45 -0500172// Check that we keep files with the .$EXT.p2p extension not older
173// than some specificed age (5 days, in this test).
174TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
175 // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9
176 // September 2001 - arbitrary number, but constant to avoid test
177 // flakiness) since the epoch and then we put two files before that
178 // date and three files after.
Alex Deymo454b7982015-07-10 10:49:29 -0700179 base::Time cutoff_time = base::Time::FromTimeT(1000000000);
Alex Deymo10875d92014-11-10 21:52:57 -0800180 TimeDelta age_limit = TimeDelta::FromDays(5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500181
182 // Set the clock just so files with a timestamp before |cutoff_time|
183 // will be deleted at housekeeping.
Alex Deymo454b7982015-07-10 10:49:29 -0700184 fake_clock_.SetWallclockTime(cutoff_time + age_limit);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500185
Alex Deymo454b7982015-07-10 10:49:29 -0700186 // Specifically pass 0 for |num_files_to_keep| to allow any number of files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700187 // Note that we need to reallocate the test_conf_ member, whose currently
188 // aliased object will be freed.
189 test_conf_ = new FakeP2PManagerConfiguration();
190 manager_.reset(P2PManager::Construct(
191 test_conf_, &fake_clock_, &fake_um_, "cros_au",
David Zeuthen41f2cf52014-11-05 12:29:45 -0500192 0 /* num_files_to_keep */, age_limit));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700193 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500194
195 // Generate files with different timestamps matching our pattern and generate
196 // other files not matching the pattern.
197 for (int n = 0; n < 5; n++) {
Alex Deymo454b7982015-07-10 10:49:29 -0700198 base::FilePath path = test_conf_->GetP2PDir().Append(base::StringPrintf(
David Zeuthen41f2cf52014-11-05 12:29:45 -0500199 "file_%d.cros_au.p2p", n));
200
201 // With five files and aiming for two of them to be before
202 // |cutoff_time|, we distribute it like this:
203 //
204 // -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 --------
205 // |
206 // cutoff_time
207 //
Alex Deymo454b7982015-07-10 10:49:29 -0700208 base::Time file_date = cutoff_time + (n - 2) * TimeDelta::FromDays(1)
209 + TimeDelta::FromHours(12);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500210
Alex Deymo454b7982015-07-10 10:49:29 -0700211 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
212 EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500213
Alex Deymo454b7982015-07-10 10:49:29 -0700214 path = test_conf_->GetP2PDir().Append(base::StringPrintf(
215 "file_%d.OTHER.p2p", n));
216 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
217 EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500218 }
219 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700220 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500221
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700222 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen41f2cf52014-11-05 12:29:45 -0500223
224 // At this point - after HouseKeeping - we should only have
225 // eight files left.
226 for (int n = 0; n < 5; n++) {
227 string file_name;
228 bool expect;
229
230 expect = (n >= 2);
231 file_name = base::StringPrintf(
232 "%s/file_%d.cros_au.p2p",
233 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700234 EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500235
236 file_name = base::StringPrintf(
237 "%s/file_%d.OTHER.p2p",
238 test_conf_->GetP2PDir().value().c_str(), n);
Alex Deymo454b7982015-07-10 10:49:29 -0700239 EXPECT_TRUE(utils::FileExists(file_name.c_str()));
David Zeuthen41f2cf52014-11-05 12:29:45 -0500240 }
241 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700242 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500243}
244
David Zeuthen27a48bc2013-08-06 12:06:29 -0700245static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
246 ssize_t expected_size, ssize_t expected_size_xattr) {
247 string path = p2p_dir + "/" + file_name;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700248 char ea_value[64] = { 0 };
249 ssize_t ea_size;
250
Gabe Blacka77939e2014-09-09 23:35:08 -0700251 off_t p2p_size = utils::FileSize(path);
252 if (p2p_size < 0) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700253 LOG(ERROR) << "File " << path << " does not exist";
254 return false;
255 }
256
257 if (expected_size != 0) {
Gabe Blacka77939e2014-09-09 23:35:08 -0700258 if (p2p_size != expected_size) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700259 LOG(ERROR) << "Expected size " << expected_size
Gabe Blacka77939e2014-09-09 23:35:08 -0700260 << " but size was " << p2p_size;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700261 return false;
262 }
263 }
264
265 if (expected_size_xattr == 0) {
266 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
267 &ea_value, sizeof ea_value - 1);
268 if (ea_size == -1 && errno == ENOATTR) {
269 // This is valid behavior as we support files without the xattr set.
270 } else {
271 PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, "
272 << "ea_size=" << ea_size << ", errno=" << errno;
273 return false;
274 }
275 } else {
276 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
277 &ea_value, sizeof ea_value - 1);
278 if (ea_size < 0) {
279 LOG(ERROR) << "Error getting xattr attribute";
280 return false;
281 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700282 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700283 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700284 if (endp == nullptr || *endp != '\0') {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700285 LOG(ERROR) << "Error parsing xattr '" << ea_value
286 << "' as an integer";
287 return false;
288 }
289 if (val != expected_size_xattr) {
290 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
291 << " but size was " << val;
292 return false;
293 }
294 }
295
296 return true;
297}
298
299static bool CreateP2PFile(string p2p_dir, string file_name,
300 size_t size, size_t size_xattr) {
301 string path = p2p_dir + "/" + file_name;
302
303 int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
304 if (fd == -1) {
305 PLOG(ERROR) << "Error creating file with path " << path;
306 return false;
307 }
308 if (ftruncate(fd, size) != 0) {
309 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
310 close(fd);
311 return false;
312 }
313
314 if (size_xattr != 0) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700315 string decimal_size = base::StringPrintf("%zu", size_xattr);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700316 if (fsetxattr(fd, "user.cros-p2p-filesize",
317 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
318 PLOG(ERROR) << "Error setting xattr on " << path;
319 close(fd);
320 return false;
321 }
322 }
323
324 close(fd);
325 return true;
326}
327
328// Check that sharing a *new* file works.
329TEST_F(P2PManagerTest, ShareFile) {
Alex Deymo10875d92014-11-10 21:52:57 -0800330 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700331 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
332 << "Please update your system to support this feature.";
333 return;
334 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800335 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700336
Alex Deymo3c3807c2015-01-30 09:32:41 -0800337 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700338 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700339 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
340 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800341 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700342
343 // Sharing it again - with the same expected size - should return true
Alex Deymo3c3807c2015-01-30 09:32:41 -0800344 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700345
346 // ... but if we use the wrong size, it should fail
Alex Deymo3c3807c2015-01-30 09:32:41 -0800347 EXPECT_FALSE(manager_->FileShare("foo", kP2PTestFileSize + 1));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700348}
349
350// Check that making a shared file visible, does what is expected.
351TEST_F(P2PManagerTest, MakeFileVisible) {
Alex Deymo10875d92014-11-10 21:52:57 -0800352 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700353 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
354 << "Please update your system to support this feature.";
355 return;
356 }
Alex Deymo3c3807c2015-01-30 09:32:41 -0800357 const int kP2PTestFileSize = 1000 * 1000; // 1 MB
David Zeuthen910ec5b2013-09-26 12:10:58 -0700358
David Zeuthen27a48bc2013-08-06 12:06:29 -0700359 // First, check that it's not visible.
Alex Deymo3c3807c2015-01-30 09:32:41 -0800360 manager_->FileShare("foo", kP2PTestFileSize);
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700361 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700362 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
363 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800364 "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700365 // Make the file visible and check that it changed its name. Do it
366 // twice to check that FileMakeVisible() is idempotent.
367 for (int n = 0; n < 2; n++) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700368 manager_->FileMakeVisible("foo");
369 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700370 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
371 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
Alex Deymo3c3807c2015-01-30 09:32:41 -0800372 "foo.cros_au.p2p", 0, kP2PTestFileSize));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700373 }
374}
375
376// Check that we return the right values for existing files in P2P_DIR.
377TEST_F(P2PManagerTest, ExistingFiles) {
Alex Deymo10875d92014-11-10 21:52:57 -0800378 if (!test_utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700379 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
380 << "Please update your system to support this feature.";
381 return;
382 }
383
David Zeuthen27a48bc2013-08-06 12:06:29 -0700384 bool visible;
385
386 // Check that errors are returned if the file does not exist
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700387 EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath());
388 EXPECT_EQ(manager_->FileGetSize("foo"), -1);
389 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
390 EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700391 // ... then create the file ...
392 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
393 "foo.cros_au.p2p", 42, 43));
394 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700395 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700396 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700397 EXPECT_EQ(manager_->FileGetSize("foo"), 42);
398 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43);
399 EXPECT_TRUE(manager_->FileGetVisible("foo", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700400 EXPECT_TRUE(visible);
401
402 // One more time, this time with a .tmp variant. First ensure it errors out..
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700403 EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath());
404 EXPECT_EQ(manager_->FileGetSize("bar"), -1);
405 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
406 EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700407 // ... then create the file ...
408 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
409 "bar.cros_au.p2p.tmp", 44, 45));
410 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700411 EXPECT_EQ(manager_->FileGetPath("bar"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700412 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700413 EXPECT_EQ(manager_->FileGetSize("bar"), 44);
414 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45);
415 EXPECT_TRUE(manager_->FileGetVisible("bar", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700416 EXPECT_FALSE(visible);
417}
418
David Zeuthen27a48bc2013-08-06 12:06:29 -0700419// This is a little bit ugly but short of mocking a 'p2p' service this
420// will have to do. E.g. we essentially simulate the various
421// behaviours of initctl(8) that we rely on.
422TEST_F(P2PManagerTest, StartP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700423 // Check that we can start the service
Alex Deymob3391552015-07-10 10:48:06 -0700424 test_conf_->SetInitctlStartCommand({"true"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700425 EXPECT_TRUE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700426 test_conf_->SetInitctlStartCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700427 EXPECT_FALSE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700428 test_conf_->SetInitctlStartCommand({
429 "sh", "-c", "echo \"initctl: Job is already running: p2p\" >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700430 EXPECT_TRUE(manager_->EnsureP2PRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700431 test_conf_->SetInitctlStartCommand({
432 "sh", "-c", "echo something else >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700433 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700434}
435
436// Same comment as for StartP2P
437TEST_F(P2PManagerTest, StopP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700438 // Check that we can start the service
Alex Deymob3391552015-07-10 10:48:06 -0700439 test_conf_->SetInitctlStopCommand({"true"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700440 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700441 test_conf_->SetInitctlStopCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700442 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700443 test_conf_->SetInitctlStopCommand({
444 "sh", "-c", "echo \"initctl: Unknown instance \" >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700445 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
Alex Deymob3391552015-07-10 10:48:06 -0700446 test_conf_->SetInitctlStopCommand({
447 "sh", "-c", "echo something else >&2; false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700448 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700449}
450
451static void ExpectUrl(const string& expected_url,
David Zeuthen27a48bc2013-08-06 12:06:29 -0700452 const string& url) {
453 EXPECT_EQ(url, expected_url);
Alex Deymo60ca1a72015-06-18 18:19:15 -0700454 MessageLoop::current()->BreakLoop();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700455}
456
457// Like StartP2P, we're mocking the different results that p2p-client
458// can return. It's not pretty but it works.
459TEST_F(P2PManagerTest, LookupURL) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700460 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
461 // being propagated in the right places.
Alex Deymob3391552015-07-10 10:48:06 -0700462 test_conf_->SetP2PClientCommand({
463 "echo", "http://1.2.3.4/{file_id}_{minsize}"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700464 manager_->LookupUrlForFile("fooX", 42, TimeDelta(),
465 base::Bind(ExpectUrl,
Alex Deymo60ca1a72015-06-18 18:19:15 -0700466 "http://1.2.3.4/fooX.cros_au_42"));
467 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700468
469 // Emulate p2p-client returning invalid URL.
Alex Deymob3391552015-07-10 10:48:06 -0700470 test_conf_->SetP2PClientCommand({"echo", "not_a_valid_url"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700471 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700472 base::Bind(ExpectUrl, ""));
473 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700474
475 // Emulate p2p-client conveying failure.
Alex Deymob3391552015-07-10 10:48:06 -0700476 test_conf_->SetP2PClientCommand({"false"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700477 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700478 base::Bind(ExpectUrl, ""));
479 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700480
481 // Emulate p2p-client not existing.
Alex Deymob3391552015-07-10 10:48:06 -0700482 test_conf_->SetP2PClientCommand({"/path/to/non/existent/helper/program"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700483 manager_->LookupUrlForFile("foobar", 42,
484 TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700485 base::Bind(ExpectUrl, ""));
486 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700487
488 // Emulate p2p-client crashing.
Alex Deymob3391552015-07-10 10:48:06 -0700489 test_conf_->SetP2PClientCommand({"sh", "-c", "kill -SEGV $$"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700490 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700491 base::Bind(ExpectUrl, ""));
492 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700493
494 // Emulate p2p-client exceeding its timeout.
Alex Deymob3391552015-07-10 10:48:06 -0700495 test_conf_->SetP2PClientCommand({
496 "sh", "-c", "echo http://1.2.3.4/; sleep 2"});
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700497 manager_->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700498 base::Bind(ExpectUrl, ""));
499 loop_.Run();
David Zeuthen27a48bc2013-08-06 12:06:29 -0700500}
501
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700502} // namespace chromeos_update_engine