blob: a790289aaab52518f20dd2bc5f6c5876efc46e80 [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;
David Zeuthen27a48bc2013-08-06 12:06:29 -070033using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070034using std::unique_ptr;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070035using testing::DoAll;
36using testing::Return;
37using testing::SetArgPointee;
38using testing::_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070039
40namespace chromeos_update_engine {
41
42// Test fixture that sets up a testing configuration (with e.g. a
43// temporary p2p dir) for P2PManager and cleans up when the test is
44// done.
45class P2PManagerTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070046 protected:
Gilad Arnold4a0321b2014-10-28 15:57:30 -070047 P2PManagerTest() : fake_um_(&fake_clock_) {}
David Zeuthen27a48bc2013-08-06 12:06:29 -070048 virtual ~P2PManagerTest() {}
49
50 // Derived from testing::Test.
51 virtual void SetUp() {
52 test_conf_ = new FakeP2PManagerConfiguration();
Gilad Arnold4a0321b2014-10-28 15:57:30 -070053
54 // Allocate and install a mock policy implementation in the fake Update
55 // Manager. Note that the FakeUpdateManager takes ownership of the policy
56 // object.
57 mock_policy_ = new chromeos_update_manager::MockPolicy(&fake_clock_);
58 fake_um_.set_policy(mock_policy_);
59
60 // Construct the P2P manager under test.
61 manager_.reset(P2PManager::Construct(test_conf_, &fake_clock_, &fake_um_,
62 "cros_au", 3,
63 base::TimeDelta::FromDays(5)));
David Zeuthen27a48bc2013-08-06 12:06:29 -070064 }
65 virtual void TearDown() {}
66
67 // The P2PManager::Configuration instance used for testing.
68 FakeP2PManagerConfiguration *test_conf_;
Gilad Arnold4a0321b2014-10-28 15:57:30 -070069
70 FakeClock fake_clock_;
71 chromeos_update_manager::MockPolicy *mock_policy_ = nullptr;
72 chromeos_update_manager::FakeUpdateManager fake_um_;
73
74 unique_ptr<P2PManager> manager_;
David Zeuthen27a48bc2013-08-06 12:06:29 -070075};
76
77
Gilad Arnold4a0321b2014-10-28 15:57:30 -070078// Check that IsP2PEnabled() polls the policy correctly, with the value not
79// changing between calls.
80TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
81 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _));
82 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen27a48bc2013-08-06 12:06:29 -070083
Gilad Arnold4a0321b2014-10-28 15:57:30 -070084 EXPECT_FALSE(manager_->IsP2PEnabled());
85 RunGMainLoopMaxIterations(100);
86 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen92d9c8b2013-09-11 10:58:11 -070087}
88
Gilad Arnold4a0321b2014-10-28 15:57:30 -070089// Check that IsP2PEnabled() polls the policy correctly, with the value changing
90// between calls.
91TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) {
92 EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _))
93 .WillOnce(DoAll(
94 SetArgPointee<3>(true),
95 Return(chromeos_update_manager::EvalStatus::kSucceeded)));
96 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true));
97 EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
David Zeuthen92d9c8b2013-09-11 10:58:11 -070098
Gilad Arnold4a0321b2014-10-28 15:57:30 -070099 EXPECT_TRUE(manager_->IsP2PEnabled());
100 RunGMainLoopMaxIterations(100);
101 EXPECT_FALSE(manager_->IsP2PEnabled());
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400102}
103
David Zeuthen27a48bc2013-08-06 12:06:29 -0700104// Check that we keep the $N newest files with the .$EXT.p2p extension.
David Zeuthen41f2cf52014-11-05 12:29:45 -0500105TEST_F(P2PManagerTest, HousekeepingCountLimit) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700106 // Specifically pass 0 for |max_file_age| to allow files of any age. Note that
107 // we need to reallocate the test_conf_ member, whose currently aliased object
108 // will be freed.
109 test_conf_ = new FakeP2PManagerConfiguration();
110 manager_.reset(P2PManager::Construct(
111 test_conf_, &fake_clock_, &fake_um_, "cros_au", 3,
David Zeuthen41f2cf52014-11-05 12:29:45 -0500112 base::TimeDelta() /* max_file_age */));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700113 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700114
Alex Deymo0f513512013-09-13 14:11:26 -0700115 // Generate files with different timestamps matching our pattern and generate
116 // other files not matching the pattern.
117 double last_timestamp = -1;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700118 for (int n = 0; n < 5; n++) {
Alex Deymo0f513512013-09-13 14:11:26 -0700119 double current_timestamp;
120 do {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700121 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
Alex Deymo0f513512013-09-13 14:11:26 -0700122 "file_%d.cros_au.p2p", n));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700123 EXPECT_EQ(0, System(base::StringPrintf("touch %s",
124 file.value().c_str())));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700125
Alex Deymo0f513512013-09-13 14:11:26 -0700126 // Check that the current timestamp on the file is different from the
127 // previous generated file. This timestamp depends on the file system
128 // time resolution, for example, ext2/ext3 have a time resolution of one
129 // second while ext4 has a resolution of one nanosecond. If the assigned
130 // timestamp is the same, we introduce a bigger sleep and call touch
131 // again.
132 struct stat statbuf;
133 EXPECT_EQ(stat(file.value().c_str(), &statbuf), 0);
134 current_timestamp = utils::TimeFromStructTimespec(&statbuf.st_ctim)
135 .ToDoubleT();
136 if (current_timestamp == last_timestamp)
137 sleep(1);
138 } while (current_timestamp == last_timestamp);
139 last_timestamp = current_timestamp;
140
Alex Vakulenko75039d72014-03-25 12:36:28 -0700141 EXPECT_EQ(0, System(base::StringPrintf(
142 "touch %s/file_%d.OTHER.p2p",
143 test_conf_->GetP2PDir().value().c_str(), n)));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700144
Alex Deymo0f513512013-09-13 14:11:26 -0700145 // A sleep of one micro-second is enough to have a different "Change" time
146 // on the file on newer file systems.
147 g_usleep(1);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700148 }
149 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700150 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700151
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700152 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700153
154 // At this point - after HouseKeeping - we should only have
155 // eight files left.
156 for (int n = 0; n < 5; n++) {
157 string file_name;
158 bool expect;
159
160 expect = (n >= 2);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700161 file_name = base::StringPrintf(
162 "%s/file_%d.cros_au.p2p",
163 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700164 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
165
Alex Vakulenko75039d72014-03-25 12:36:28 -0700166 file_name = base::StringPrintf(
167 "%s/file_%d.OTHER.p2p",
168 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700169 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
170 }
171 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700172 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700173}
174
David Zeuthen41f2cf52014-11-05 12:29:45 -0500175// Check that we keep files with the .$EXT.p2p extension not older
176// than some specificed age (5 days, in this test).
177TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
178 // We set the cutoff time to be 1 billion seconds (01:46:40 UTC on 9
179 // September 2001 - arbitrary number, but constant to avoid test
180 // flakiness) since the epoch and then we put two files before that
181 // date and three files after.
182 time_t cutoff_time = 1000000000;
183 base::TimeDelta age_limit = base::TimeDelta::FromDays(5);
184
185 // Set the clock just so files with a timestamp before |cutoff_time|
186 // will be deleted at housekeeping.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700187 fake_clock_.SetWallclockTime(base::Time::FromTimeT(cutoff_time) + age_limit);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500188
189 // Specifically pass 0 for |num_files_to_keep| to allow files of any age.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700190 // Note that we need to reallocate the test_conf_ member, whose currently
191 // aliased object will be freed.
192 test_conf_ = new FakeP2PManagerConfiguration();
193 manager_.reset(P2PManager::Construct(
194 test_conf_, &fake_clock_, &fake_um_, "cros_au",
David Zeuthen41f2cf52014-11-05 12:29:45 -0500195 0 /* num_files_to_keep */, age_limit));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700196 EXPECT_EQ(manager_->CountSharedFiles(), 0);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500197
198 // Generate files with different timestamps matching our pattern and generate
199 // other files not matching the pattern.
200 for (int n = 0; n < 5; n++) {
201 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
202 "file_%d.cros_au.p2p", n));
203
204 // With five files and aiming for two of them to be before
205 // |cutoff_time|, we distribute it like this:
206 //
207 // -------- 0 -------- 1 -------- 2 -------- 3 -------- 4 --------
208 // |
209 // cutoff_time
210 //
211 base::Time file_date = base::Time::FromTimeT(cutoff_time) +
212 (n - 2)*base::TimeDelta::FromDays(1) + base::TimeDelta::FromHours(12);
213
214 // The touch(1) command expects input like this
215 // --date="2004-02-27 14:19:13.489392193 +0530"
216 base::Time::Exploded exploded;
217 file_date.UTCExplode(&exploded);
218 string file_date_string = base::StringPrintf(
219 "%d-%02d-%02d %02d:%02d:%02d +0000",
220 exploded.year, exploded.month, exploded.day_of_month,
221 exploded.hour, exploded.minute, exploded.second);
222
223 // Sanity check that we generated the correct string.
224 base::Time parsed_time;
225 EXPECT_TRUE(base::Time::FromUTCString(file_date_string.c_str(),
226 &parsed_time));
227 EXPECT_EQ(parsed_time, file_date);
228
229 EXPECT_EQ(0, System(base::StringPrintf("touch --date=\"%s\" %s",
230 file_date_string.c_str(),
231 file.value().c_str())));
232
233 EXPECT_EQ(0, System(base::StringPrintf(
234 "touch --date=\"%s\" %s/file_%d.OTHER.p2p",
235 file_date_string.c_str(),
236 test_conf_->GetP2PDir().value().c_str(), n)));
237 }
238 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700239 EXPECT_EQ(manager_->CountSharedFiles(), 5);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500240
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700241 EXPECT_TRUE(manager_->PerformHousekeeping());
David Zeuthen41f2cf52014-11-05 12:29:45 -0500242
243 // At this point - after HouseKeeping - we should only have
244 // eight files left.
245 for (int n = 0; n < 5; n++) {
246 string file_name;
247 bool expect;
248
249 expect = (n >= 2);
250 file_name = base::StringPrintf(
251 "%s/file_%d.cros_au.p2p",
252 test_conf_->GetP2PDir().value().c_str(), n);
253 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
254
255 file_name = base::StringPrintf(
256 "%s/file_%d.OTHER.p2p",
257 test_conf_->GetP2PDir().value().c_str(), n);
258 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
259 }
260 // CountSharedFiles() only counts 'cros_au' files.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700261 EXPECT_EQ(manager_->CountSharedFiles(), 3);
David Zeuthen41f2cf52014-11-05 12:29:45 -0500262}
263
David Zeuthen27a48bc2013-08-06 12:06:29 -0700264static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
265 ssize_t expected_size, ssize_t expected_size_xattr) {
266 string path = p2p_dir + "/" + file_name;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700267 char ea_value[64] = { 0 };
268 ssize_t ea_size;
269
Gabe Blacka77939e2014-09-09 23:35:08 -0700270 off_t p2p_size = utils::FileSize(path);
271 if (p2p_size < 0) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700272 LOG(ERROR) << "File " << path << " does not exist";
273 return false;
274 }
275
276 if (expected_size != 0) {
Gabe Blacka77939e2014-09-09 23:35:08 -0700277 if (p2p_size != expected_size) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700278 LOG(ERROR) << "Expected size " << expected_size
Gabe Blacka77939e2014-09-09 23:35:08 -0700279 << " but size was " << p2p_size;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700280 return false;
281 }
282 }
283
284 if (expected_size_xattr == 0) {
285 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
286 &ea_value, sizeof ea_value - 1);
287 if (ea_size == -1 && errno == ENOATTR) {
288 // This is valid behavior as we support files without the xattr set.
289 } else {
290 PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, "
291 << "ea_size=" << ea_size << ", errno=" << errno;
292 return false;
293 }
294 } else {
295 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
296 &ea_value, sizeof ea_value - 1);
297 if (ea_size < 0) {
298 LOG(ERROR) << "Error getting xattr attribute";
299 return false;
300 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700301 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700302 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700303 if (endp == nullptr || *endp != '\0') {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700304 LOG(ERROR) << "Error parsing xattr '" << ea_value
305 << "' as an integer";
306 return false;
307 }
308 if (val != expected_size_xattr) {
309 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
310 << " but size was " << val;
311 return false;
312 }
313 }
314
315 return true;
316}
317
318static bool CreateP2PFile(string p2p_dir, string file_name,
319 size_t size, size_t size_xattr) {
320 string path = p2p_dir + "/" + file_name;
321
322 int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
323 if (fd == -1) {
324 PLOG(ERROR) << "Error creating file with path " << path;
325 return false;
326 }
327 if (ftruncate(fd, size) != 0) {
328 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
329 close(fd);
330 return false;
331 }
332
333 if (size_xattr != 0) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700334 string decimal_size = base::StringPrintf("%zu", size_xattr);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700335 if (fsetxattr(fd, "user.cros-p2p-filesize",
336 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
337 PLOG(ERROR) << "Error setting xattr on " << path;
338 close(fd);
339 return false;
340 }
341 }
342
343 close(fd);
344 return true;
345}
346
347// Check that sharing a *new* file works.
348TEST_F(P2PManagerTest, ShareFile) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700349 if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700350 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
351 << "Please update your system to support this feature.";
352 return;
353 }
354
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700355 EXPECT_TRUE(manager_->FileShare("foo", 10 * 1000 * 1000));
356 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700357 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
358 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
359 "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000));
360
361 // Sharing it again - with the same expected size - should return true
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700362 EXPECT_TRUE(manager_->FileShare("foo", 10 * 1000 * 1000));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700363
364 // ... but if we use the wrong size, it should fail
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700365 EXPECT_FALSE(manager_->FileShare("foo", 10 * 1000 * 1000 + 1));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700366}
367
368// Check that making a shared file visible, does what is expected.
369TEST_F(P2PManagerTest, MakeFileVisible) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700370 if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700371 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
372 << "Please update your system to support this feature.";
373 return;
374 }
375
David Zeuthen27a48bc2013-08-06 12:06:29 -0700376 // First, check that it's not visible.
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700377 manager_->FileShare("foo", 10*1000*1000);
378 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700379 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
380 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
381 "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000));
382 // Make the file visible and check that it changed its name. Do it
383 // twice to check that FileMakeVisible() is idempotent.
384 for (int n = 0; n < 2; n++) {
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700385 manager_->FileMakeVisible("foo");
386 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700387 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
388 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
389 "foo.cros_au.p2p", 0, 10 * 1000 * 1000));
390 }
391}
392
393// Check that we return the right values for existing files in P2P_DIR.
394TEST_F(P2PManagerTest, ExistingFiles) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700395 if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700396 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
397 << "Please update your system to support this feature.";
398 return;
399 }
400
David Zeuthen27a48bc2013-08-06 12:06:29 -0700401 bool visible;
402
403 // Check that errors are returned if the file does not exist
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700404 EXPECT_EQ(manager_->FileGetPath("foo"), base::FilePath());
405 EXPECT_EQ(manager_->FileGetSize("foo"), -1);
406 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
407 EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700408 // ... then create the file ...
409 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
410 "foo.cros_au.p2p", 42, 43));
411 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700412 EXPECT_EQ(manager_->FileGetPath("foo"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700413 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700414 EXPECT_EQ(manager_->FileGetSize("foo"), 42);
415 EXPECT_EQ(manager_->FileGetExpectedSize("foo"), 43);
416 EXPECT_TRUE(manager_->FileGetVisible("foo", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700417 EXPECT_TRUE(visible);
418
419 // One more time, this time with a .tmp variant. First ensure it errors out..
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700420 EXPECT_EQ(manager_->FileGetPath("bar"), base::FilePath());
421 EXPECT_EQ(manager_->FileGetSize("bar"), -1);
422 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
423 EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700424 // ... then create the file ...
425 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
426 "bar.cros_au.p2p.tmp", 44, 45));
427 // ... and then check that the expected values are returned
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700428 EXPECT_EQ(manager_->FileGetPath("bar"),
David Zeuthen27a48bc2013-08-06 12:06:29 -0700429 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700430 EXPECT_EQ(manager_->FileGetSize("bar"), 44);
431 EXPECT_EQ(manager_->FileGetExpectedSize("bar"), 45);
432 EXPECT_TRUE(manager_->FileGetVisible("bar", &visible));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700433 EXPECT_FALSE(visible);
434}
435
David Zeuthen27a48bc2013-08-06 12:06:29 -0700436// This is a little bit ugly but short of mocking a 'p2p' service this
437// will have to do. E.g. we essentially simulate the various
438// behaviours of initctl(8) that we rely on.
439TEST_F(P2PManagerTest, StartP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700440 // Check that we can start the service
441 test_conf_->SetInitctlStartCommandLine("true");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700442 EXPECT_TRUE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700443 test_conf_->SetInitctlStartCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700444 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700445 test_conf_->SetInitctlStartCommandLine(
446 "sh -c 'echo \"initctl: Job is already running: p2p\" >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700447 EXPECT_TRUE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700448 test_conf_->SetInitctlStartCommandLine(
449 "sh -c 'echo something else >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700450 EXPECT_FALSE(manager_->EnsureP2PRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700451}
452
453// Same comment as for StartP2P
454TEST_F(P2PManagerTest, StopP2P) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700455 // Check that we can start the service
456 test_conf_->SetInitctlStopCommandLine("true");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700457 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700458 test_conf_->SetInitctlStopCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700459 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700460 test_conf_->SetInitctlStopCommandLine(
461 "sh -c 'echo \"initctl: Unknown instance \" >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700462 EXPECT_TRUE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700463 test_conf_->SetInitctlStopCommandLine(
464 "sh -c 'echo something else >&2; false'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700465 EXPECT_FALSE(manager_->EnsureP2PNotRunning());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700466}
467
468static void ExpectUrl(const string& expected_url,
469 GMainLoop* loop,
470 const string& url) {
471 EXPECT_EQ(url, expected_url);
472 g_main_loop_quit(loop);
473}
474
475// Like StartP2P, we're mocking the different results that p2p-client
476// can return. It's not pretty but it works.
477TEST_F(P2PManagerTest, LookupURL) {
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700478 GMainLoop *loop = g_main_loop_new(nullptr, FALSE);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700479
480 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
481 // being propagated in the right places.
Alex Deymo8ad6da92014-07-15 17:17:45 -0700482 test_conf_->SetP2PClientCommandLine(
483 "echo 'http://1.2.3.4/{file_id}_{minsize}'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700484 manager_->LookupUrlForFile("fooX", 42, TimeDelta(),
485 base::Bind(ExpectUrl,
486 "http://1.2.3.4/fooX.cros_au_42",
487 loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700488 g_main_loop_run(loop);
489
490 // Emulate p2p-client returning invalid URL.
491 test_conf_->SetP2PClientCommandLine("echo 'not_a_valid_url'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700492 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
493 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700494 g_main_loop_run(loop);
495
496 // Emulate p2p-client conveying failure.
497 test_conf_->SetP2PClientCommandLine("false");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700498 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
499 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700500 g_main_loop_run(loop);
501
502 // Emulate p2p-client not existing.
503 test_conf_->SetP2PClientCommandLine("/path/to/non/existent/helper/program");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700504 manager_->LookupUrlForFile("foobar", 42,
505 TimeDelta(),
506 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700507 g_main_loop_run(loop);
508
509 // Emulate p2p-client crashing.
510 test_conf_->SetP2PClientCommandLine("sh -c 'kill -SEGV $$'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700511 manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
512 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700513 g_main_loop_run(loop);
514
515 // Emulate p2p-client exceeding its timeout.
516 test_conf_->SetP2PClientCommandLine("sh -c 'echo http://1.2.3.4/; sleep 2'");
Gilad Arnold4a0321b2014-10-28 15:57:30 -0700517 manager_->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
518 base::Bind(ExpectUrl, "", loop));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700519 g_main_loop_run(loop);
520
521 g_main_loop_unref(loop);
522}
523
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700524} // namespace chromeos_update_engine