1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/cros/p2p_manager.h"
18
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <sys/xattr.h>
24 #include <unistd.h>
25
26 #include <memory>
27 #include <string>
28 #include <vector>
29
30 #include <base/bind.h>
31 #include <base/callback.h>
32 #include <base/files/file_util.h>
33 #if BASE_VER < 780000 // Android
34 #include <base/message_loop/message_loop.h>
35 #endif // BASE_VER < 780000
36 #include <base/strings/stringprintf.h>
37 #if BASE_VER >= 780000 // CrOS
38 #include <base/task/single_thread_task_executor.h>
39 #endif // BASE_VER >= 780000
40 #include <brillo/asynchronous_signal_handler.h>
41 #include <brillo/message_loops/base_message_loop.h>
42 #include <brillo/message_loops/message_loop.h>
43 #include <brillo/message_loops/message_loop_utils.h>
44 #include <gmock/gmock.h>
45 #include <gtest/gtest.h>
46 #include <policy/libpolicy.h>
47 #include <policy/mock_device_policy.h>
48
49 #include "update_engine/common/prefs.h"
50 #include "update_engine/common/subprocess.h"
51 #include "update_engine/common/test_utils.h"
52 #include "update_engine/common/utils.h"
53 #include "update_engine/cros/fake_p2p_manager_configuration.h"
54 #include "update_engine/cros/fake_system_state.h"
55 #include "update_engine/update_manager/fake_update_manager.h"
56 #include "update_engine/update_manager/mock_policy.h"
57
58 using base::TimeDelta;
59 using brillo::MessageLoop;
60 using std::string;
61 using std::unique_ptr;
62 using std::vector;
63 using testing::_;
64 using testing::DoAll;
65 using testing::Return;
66 using testing::SetArgPointee;
67
68 namespace chromeos_update_engine {
69
70 // Test fixture that sets up a testing configuration (with e.g. a
71 // temporary p2p dir) for P2PManager and cleans up when the test is
72 // done.
73 class P2PManagerTest : public testing::Test {
74 protected:
75 P2PManagerTest() = default;
76 ~P2PManagerTest() override = default;
77
78 // Derived from testing::Test.
SetUp()79 void SetUp() override {
80 loop_.SetAsCurrent();
81 FakeSystemState::CreateInstance();
82 async_signal_handler_.Init();
83 subprocess_.Init(&async_signal_handler_);
84 test_conf_ = new FakeP2PManagerConfiguration();
85
86 // Allocate and install a mock policy implementation in the fake Update
87 // Manager. Note that the FakeUpdateManager takes ownership of the policy
88 // object.
89 mock_policy_ = new chromeos_update_manager::MockPolicy();
90 fake_um_.set_policy(mock_policy_);
91
92 // Construct the P2P manager under test.
93 manager_.reset(P2PManager::Construct(test_conf_,
94 &fake_um_,
95 "cros_au",
96 3,
97 TimeDelta::FromDays(5)));
98 }
99
100 #if BASE_VER < 780000 // Android
101 base::MessageLoopForIO base_loop_;
102 brillo::BaseMessageLoop loop_{&base_loop_};
103 #else // CrOS
104 base::SingleThreadTaskExecutor base_loop_{base::MessagePumpType::IO};
105 brillo::BaseMessageLoop loop_{base_loop_.task_runner()};
106 #endif // BASE_VER < 780000
107 brillo::AsynchronousSignalHandler async_signal_handler_;
108 Subprocess subprocess_;
109
110 // The P2PManager::Configuration instance used for testing.
111 FakeP2PManagerConfiguration* test_conf_;
112
113 chromeos_update_manager::MockPolicy* mock_policy_ = nullptr;
114 chromeos_update_manager::FakeUpdateManager fake_um_;
115
116 unique_ptr<P2PManager> manager_;
117 };
118
119 // Check that IsP2PEnabled() polls the policy correctly, with the value not
120 // changing between calls.
TEST_F(P2PManagerTest,P2PEnabledInitAndNotChanged)121 TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
122 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _));
123 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
124
125 EXPECT_FALSE(manager_->IsP2PEnabled());
126 brillo::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
127 EXPECT_FALSE(manager_->IsP2PEnabled());
128 }
129
130 // Check that IsP2PEnabled() polls the policy correctly, with the value changing
131 // between calls.
TEST_F(P2PManagerTest,P2PEnabledInitAndChanged)132 TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) {
133 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _))
134 .WillOnce(DoAll(SetArgPointee<3>(true),
135 Return(chromeos_update_manager::EvalStatus::kSucceeded)));
136 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true));
137 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
138
139 EXPECT_TRUE(manager_->IsP2PEnabled());
140 brillo::MessageLoopRunMaxIterations(MessageLoop::current(), 100);
141 EXPECT_FALSE(manager_->IsP2PEnabled());
142 }
143
144 // Check that we keep the $N newest files with the .$EXT.p2p extension.
TEST_F(P2PManagerTest,HousekeepingCountLimit)145 TEST_F(P2PManagerTest, HousekeepingCountLimit) {
146 // Specifically pass 0 for |max_file_age| to allow files of any age. Note that
147 // we need to reallocate the test_conf_ member, whose currently aliased object
148 // will be freed.
149 test_conf_ = new FakeP2PManagerConfiguration();
150 manager_.reset(P2PManager::Construct(test_conf_,
151 &fake_um_,
152 "cros_au",
153 3,
154 TimeDelta() /* max_file_age */));
155 EXPECT_EQ(manager_->CountSharedFiles(), 0);
156
157 base::Time start_time = base::Time::FromDoubleT(1246996800.);
158 // Generate files with different timestamps matching our pattern and generate
159 // other files not matching the pattern.
160 for (int n = 0; n < 5; n++) {
161 base::FilePath path = test_conf_->GetP2PDir().Append(
162 base::StringPrintf("file_%d.cros_au.p2p", n));
163 base::Time file_time = start_time + TimeDelta::FromMinutes(n);
164 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
165 EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
166
167 path = test_conf_->GetP2PDir().Append(
168 base::StringPrintf("file_%d.OTHER.p2p", n));
169 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
170 EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
171 }
172 // CountSharedFiles() only counts 'cros_au' files.
173 EXPECT_EQ(manager_->CountSharedFiles(), 5);
174
175 EXPECT_TRUE(manager_->PerformHousekeeping());
176
177 // At this point - after HouseKeeping - we should only have
178 // eight files left.
179 for (int n = 0; n < 5; n++) {
180 string file_name;
181 bool expect;
182
183 expect = (n >= 2);
184 file_name = base::StringPrintf(
185 "%s/file_%d.cros_au.p2p", test_conf_->GetP2PDir().value().c_str(), n);
186 EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
187
188 file_name = base::StringPrintf(
189 "%s/file_%d.OTHER.p2p", test_conf_->GetP2PDir().value().c_str(), n);
190 EXPECT_TRUE(utils::FileExists(file_name.c_str()));
191 }
192 // CountSharedFiles() only counts 'cros_au' files.
193 EXPECT_EQ(manager_->CountSharedFiles(), 3);
194 }
195
196 // Check that we keep files with the .$EXT.p2p extension not older
197 // than some specific age (5 days, in this test).
TEST_F(P2PManagerTest,HousekeepingAgeLimit)198 TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
199 // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9
200 // September 2001 - arbitrary number, but constant to avoid test
201 // flakiness) since the epoch and then we put two files before that
202 // date and three files after.
203 base::Time cutoff_time = base::Time::FromTimeT(1000000000);
204 TimeDelta age_limit = TimeDelta::FromDays(5);
205
206 // Set the clock just so files with a timestamp before |cutoff_time|
207 // will be deleted at housekeeping.
208 FakeSystemState::Get()->fake_clock()->SetWallclockTime(cutoff_time +
209 age_limit);
210
211 // Specifically pass 0 for |num_files_to_keep| to allow any number of files.
212 // Note that we need to reallocate the test_conf_ member, whose currently
213 // aliased object will be freed.
214 test_conf_ = new FakeP2PManagerConfiguration();
215 manager_.reset(P2PManager::Construct(test_conf_,
216 &fake_um_,
217 "cros_au",
218 0 /* num_files_to_keep */,
219 age_limit));
220 EXPECT_EQ(manager_->CountSharedFiles(), 0);
221
222 // Generate files with different timestamps matching our pattern and generate
223 // other files not matching the pattern.
224 for (int n = 0; n < 5; n++) {
225 base::FilePath path = test_conf_->GetP2PDir().Append(
226 base::StringPrintf("file_%d.cros_au.p2p", n));
227
228 // With five files and aiming for two of them to be before
229 // |cutoff_time|, we distribute it like this:
230 //
231 // -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 --------
232 // |
233 // cutoff_time
234 //
235 base::Time file_date = cutoff_time + (n - 2) * TimeDelta::FromDays(1) +
236 TimeDelta::FromHours(12);
237
238 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
239 EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
240
241 path = test_conf_->GetP2PDir().Append(
242 base::StringPrintf("file_%d.OTHER.p2p", n));
243 EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
244 EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
245 }
246 // CountSharedFiles() only counts 'cros_au' files.
247 EXPECT_EQ(manager_->CountSharedFiles(), 5);
248
249 EXPECT_TRUE(manager_->PerformHousekeeping());
250
251 // At this point - after HouseKeeping - we should only have
252 // eight files left.
253 for (int n = 0; n < 5; n++) {
254 string file_name;
255 bool expect;
256
257 expect = (n >= 2);
258 file_name = base::StringPrintf(
259 "%s/file_%d.cros_au.p2p", test_conf_->GetP2PDir().value().c_str(), n);
260 EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
261
262 file_name = base::StringPrintf(
263 "%s/file_%d.OTHER.p2p", test_conf_->GetP2PDir().value().c_str(), n);
264 EXPECT_TRUE(utils::FileExists(file_name.c_str()));
265 }
266 // CountSharedFiles() only counts 'cros_au' files.
267 EXPECT_EQ(manager_->CountSharedFiles(), 3);
268 }
269
CheckP2PFile(const string & p2p_dir,const string & file_name,ssize_t expected_size,ssize_t expected_size_xattr)270 static bool CheckP2PFile(const string& p2p_dir,
271 const string& file_name,
272 ssize_t expected_size,
273 ssize_t expected_size_xattr) {
274 string path = p2p_dir + "/" + file_name;
275 char ea_value[64] = {0};
276 ssize_t ea_size;
277
278 off_t p2p_size = utils::FileSize(path);
279 if (p2p_size < 0) {
280 LOG(ERROR) << "File " << path << " does not exist";
281 return false;
282 }
283
284 if (expected_size != 0) {
285 if (p2p_size != expected_size) {
286 LOG(ERROR) << "Expected size " << expected_size << " but size was "
287 << p2p_size;
288 return false;
289 }
290 }
291
292 if (expected_size_xattr == 0) {
293 ea_size = getxattr(
294 path.c_str(), "user.cros-p2p-filesize", &ea_value, sizeof ea_value - 1);
295 if (ea_size == -1 && errno == ENODATA) {
296 // This is valid behavior as we support files without the xattr set.
297 } else {
298 PLOG(ERROR) << "getxattr() didn't fail with ENODATA as expected, "
299 << "ea_size=" << ea_size << ", errno=" << errno;
300 return false;
301 }
302 } else {
303 ea_size = getxattr(
304 path.c_str(), "user.cros-p2p-filesize", &ea_value, sizeof ea_value - 1);
305 if (ea_size < 0) {
306 LOG(ERROR) << "Error getting xattr attribute";
307 return false;
308 }
309 char* endp = nullptr;
310 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
311 if (endp == nullptr || *endp != '\0') {
312 LOG(ERROR) << "Error parsing xattr '" << ea_value << "' as an integer";
313 return false;
314 }
315 if (val != expected_size_xattr) {
316 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
317 << " but size was " << val;
318 return false;
319 }
320 }
321
322 return true;
323 }
324
CreateP2PFile(string p2p_dir,string file_name,size_t size,size_t size_xattr)325 static bool CreateP2PFile(string p2p_dir,
326 string file_name,
327 size_t size,
328 size_t size_xattr) {
329 string path = p2p_dir + "/" + file_name;
330
331 int fd = open(path.c_str(), O_CREAT | O_RDWR, 0644);
332 if (fd == -1) {
333 PLOG(ERROR) << "Error creating file with path " << path;
334 return false;
335 }
336 if (ftruncate(fd, size) != 0) {
337 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
338 close(fd);
339 return false;
340 }
341
342 if (size_xattr != 0) {
343 string decimal_size = std::to_string(size_xattr);
344 if (fsetxattr(fd,
345 "user.cros-p2p-filesize",
346 decimal_size.c_str(),
347 decimal_size.size(),
348 0) != 0) {
349 PLOG(ERROR) << "Error setting xattr on " << path;
350 close(fd);
351 return false;
352 }
353 }
354
355 close(fd);
356 return true;
357 }
358
359 // Check that sharing a *new* file works.
TEST_F(P2PManagerTest,ShareFile)360 TEST_F(P2PManagerTest, ShareFile) {
361 const int kP2PTestFileSize = 1000 * 8; // 8 KB
362
363 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
364 EXPECT_EQ(manager_->FileGetPath("foo"),
365 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
366 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
367 "foo.cros_au.p2p.tmp",
368 0,
369 kP2PTestFileSize));
370
371 // Sharing it again - with the same expected size - should return true
372 EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
373
374 // ... but if we use the wrong size, it should fail
375 EXPECT_FALSE(manager_->FileShare("foo", kP2PTestFileSize + 1));
376 }
377
378 // Check that making a shared file visible, does what is expected.
TEST_F(P2PManagerTest,MakeFileVisible)379 TEST_F(P2PManagerTest, MakeFileVisible) {
380 const int kP2PTestFileSize = 1000 * 8; // 8 KB
381
382 // First, check that it's not visible.
383 manager_->FileShare("foo", kP2PTestFileSize);
384 EXPECT_EQ(manager_->FileGetPath("foo"),
385 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
386 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
387 "foo.cros_au.p2p.tmp",
388 0,
389 kP2PTestFileSize));
390 // Make the file visible and check that it changed its name. Do it
391 // twice to check that FileMakeVisible() is idempotent.
392 for (int n = 0; n < 2; n++) {
393 manager_->FileMakeVisible("foo");
394 EXPECT_EQ(manager_->FileGetPath("foo"),
395 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
396 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
397 "foo.cros_au.p2p",
398 0,
399 kP2PTestFileSize));
400 }
401 }
402
403 // Check that we return the right values for existing files in P2P_DIR.
TEST_F(P2PManagerTest,ExistingFiles)404 TEST_F(P2PManagerTest, ExistingFiles) {
405 bool visible;
406
407 // Check that errors are returned if the file does not exist
408 EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath());
409 EXPECT_EQ(manager_->FileGetSize("foo"), -1);
410 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
411 EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
412 // ... then create the file ...
413 EXPECT_TRUE(CreateP2PFile(
414 test_conf_->GetP2PDir().value(), "foo.cros_au.p2p", 42, 43));
415 // ... and then check that the expected values are returned
416 EXPECT_EQ(manager_->FileGetPath("foo"),
417 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
418 EXPECT_EQ(manager_->FileGetSize("foo"), 42);
419 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43);
420 EXPECT_TRUE(manager_->FileGetVisible("foo", &visible));
421 EXPECT_TRUE(visible);
422
423 // One more time, this time with a .tmp variant. First ensure it errors out..
424 EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath());
425 EXPECT_EQ(manager_->FileGetSize("bar"), -1);
426 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
427 EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
428 // ... then create the file ...
429 EXPECT_TRUE(CreateP2PFile(
430 test_conf_->GetP2PDir().value(), "bar.cros_au.p2p.tmp", 44, 45));
431 // ... and then check that the expected values are returned
432 EXPECT_EQ(manager_->FileGetPath("bar"),
433 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
434 EXPECT_EQ(manager_->FileGetSize("bar"), 44);
435 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45);
436 EXPECT_TRUE(manager_->FileGetVisible("bar", &visible));
437 EXPECT_FALSE(visible);
438 }
439
440 // This is a little bit ugly but short of mocking a 'p2p' service this
441 // will have to do. E.g. we essentially simulate the various
442 // behaviours of initctl(8) that we rely on.
TEST_F(P2PManagerTest,StartP2P)443 TEST_F(P2PManagerTest, StartP2P) {
444 // Check that we can start the service
445 test_conf_->SetInitctlStartCommand({"true"});
446 EXPECT_TRUE(manager_->EnsureP2PRunning());
447 test_conf_->SetInitctlStartCommand({"false"});
448 EXPECT_FALSE(manager_->EnsureP2PRunning());
449 test_conf_->SetInitctlStartCommand(
450 {"sh", "-c", "echo \"initctl: Job is already running: p2p\" >&2; false"});
451 EXPECT_TRUE(manager_->EnsureP2PRunning());
452 test_conf_->SetInitctlStartCommand(
453 {"sh", "-c", "echo something else >&2; false"});
454 EXPECT_FALSE(manager_->EnsureP2PRunning());
455 }
456
457 // Same comment as for StartP2P
TEST_F(P2PManagerTest,StopP2P)458 TEST_F(P2PManagerTest, StopP2P) {
459 // Check that we can start the service
460 test_conf_->SetInitctlStopCommand({"true"});
461 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
462 test_conf_->SetInitctlStopCommand({"false"});
463 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
464 test_conf_->SetInitctlStopCommand(
465 {"sh", "-c", "echo \"initctl: Unknown instance \" >&2; false"});
466 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
467 test_conf_->SetInitctlStopCommand(
468 {"sh", "-c", "echo something else >&2; false"});
469 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
470 }
471
ExpectUrl(const string & expected_url,const string & url)472 static void ExpectUrl(const string& expected_url, const string& url) {
473 EXPECT_EQ(url, expected_url);
474 MessageLoop::current()->BreakLoop();
475 }
476
477 // Like StartP2P, we're mocking the different results that p2p-client
478 // can return. It's not pretty but it works.
TEST_F(P2PManagerTest,LookupURL)479 TEST_F(P2PManagerTest, LookupURL) {
480 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
481 // being propagated in the right places.
482 test_conf_->SetP2PClientCommand(
483 {"echo", "http://1.2.3.4/{file_id}_{minsize}"});
484 manager_->LookupUrlForFile(
485 "fooX",
486 42,
487 TimeDelta(),
488 base::Bind(ExpectUrl, "http://1.2.3.4/fooX.cros_au_42"));
489 loop_.Run();
490
491 // Emulate p2p-client returning invalid URL.
492 test_conf_->SetP2PClientCommand({"echo", "not_a_valid_url"});
493 manager_->LookupUrlForFile(
494 "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, ""));
495 loop_.Run();
496
497 // Emulate p2p-client conveying failure.
498 test_conf_->SetP2PClientCommand({"false"});
499 manager_->LookupUrlForFile(
500 "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, ""));
501 loop_.Run();
502
503 // Emulate p2p-client not existing.
504 test_conf_->SetP2PClientCommand({"/path/to/non/existent/helper/program"});
505 manager_->LookupUrlForFile(
506 "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, ""));
507 loop_.Run();
508
509 // Emulate p2p-client crashing.
510 test_conf_->SetP2PClientCommand({"sh", "-c", "kill -SEGV $$"});
511 manager_->LookupUrlForFile(
512 "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, ""));
513 loop_.Run();
514
515 // Emulate p2p-client exceeding its timeout.
516 test_conf_->SetP2PClientCommand(
517 {"sh",
518 "-c",
519 // The 'sleep' launched below could be left behind as an orphaned
520 // process when the 'sh' process is terminated by SIGTERM. As a
521 // remedy, trap SIGTERM and kill the 'sleep' process, which requires
522 // launching 'sleep' in background and then waiting for it.
523 "cleanup() { kill \"${sleep_pid}\"; exit 0; }; "
524 "trap cleanup TERM; "
525 "sleep 5 & "
526 "sleep_pid=$!; "
527 "echo http://1.2.3.4/; "
528 "wait"});
529 manager_->LookupUrlForFile("foobar",
530 42,
531 TimeDelta::FromMilliseconds(500),
532 base::Bind(ExpectUrl, ""));
533 loop_.Run();
534 }
535
536 } // namespace chromeos_update_engine
537