blob: 9af54587a1d083d4fd35db560a98aeebf7c9915f [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
5#include <glib.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>
14
David Zeuthen27a48bc2013-08-06 12:06:29 -070015#include "gmock/gmock.h"
16#include "gtest/gtest.h"
17
18#include "base/bind.h"
19#include "base/callback.h"
Alex Vakulenko75039d72014-03-25 12:36:28 -070020#include <base/strings/stringprintf.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 Zeuthen27a48bc2013-08-06 12:06:29 -070024#include "update_engine/fake_p2p_manager_configuration.h"
Alex Vakulenko44cab302014-07-23 13:12:15 -070025#include "update_engine/p2p_manager.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070026#include "update_engine/prefs.h"
27#include "update_engine/test_utils.h"
28#include "update_engine/utils.h"
29
Alex Deymof329b932014-10-30 01:37:48 -070030using base::TimeDelta;
David Zeuthen27a48bc2013-08-06 12:06:29 -070031using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070032using std::unique_ptr;
David Zeuthen27a48bc2013-08-06 12:06:29 -070033
34namespace chromeos_update_engine {
35
36// Test fixture that sets up a testing configuration (with e.g. a
37// temporary p2p dir) for P2PManager and cleans up when the test is
38// done.
39class P2PManagerTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070040 protected:
David Zeuthen27a48bc2013-08-06 12:06:29 -070041 P2PManagerTest() {}
42 virtual ~P2PManagerTest() {}
43
44 // Derived from testing::Test.
45 virtual void SetUp() {
46 test_conf_ = new FakeP2PManagerConfiguration();
47 }
48 virtual void TearDown() {}
49
50 // The P2PManager::Configuration instance used for testing.
51 FakeP2PManagerConfiguration *test_conf_;
52};
53
54
David Zeuthen92d9c8b2013-09-11 10:58:11 -070055// Check that IsP2PEnabled() returns false if neither the crosh flag
56// nor the Enterprise Policy indicates p2p is to be used.
57TEST_F(P2PManagerTest, P2PEnabledNeitherCroshFlagNotEnterpriseSetting) {
David Zeuthen27a48bc2013-08-06 12:06:29 -070058 string temp_dir;
59 Prefs prefs;
Gilad Arnolda6742b32014-01-11 00:18:34 -080060 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthen27a48bc2013-08-06 12:06:29 -070061 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -070062 prefs.Init(base::FilePath(temp_dir));
David Zeuthen27a48bc2013-08-06 12:06:29 -070063
Ben Chan02f7c1d2014-10-18 15:18:02 -070064 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
David Zeuthen27a48bc2013-08-06 12:06:29 -070065 &prefs, "cros_au", 3));
66 EXPECT_FALSE(manager->IsP2PEnabled());
David Zeuthen92d9c8b2013-09-11 10:58:11 -070067
68 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
69}
70
71// Check that IsP2PEnabled() corresponds to value of the crosh flag
72// when Enterprise Policy is not set.
73TEST_F(P2PManagerTest, P2PEnabledCroshFlag) {
74 string temp_dir;
75 Prefs prefs;
Gilad Arnolda6742b32014-01-11 00:18:34 -080076 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthen92d9c8b2013-09-11 10:58:11 -070077 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -070078 prefs.Init(base::FilePath(temp_dir));
David Zeuthen92d9c8b2013-09-11 10:58:11 -070079
Ben Chan02f7c1d2014-10-18 15:18:02 -070080 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
David Zeuthen92d9c8b2013-09-11 10:58:11 -070081 &prefs, "cros_au", 3));
82 EXPECT_FALSE(manager->IsP2PEnabled());
83 prefs.SetBoolean(kPrefsP2PEnabled, true);
84 EXPECT_TRUE(manager->IsP2PEnabled());
85 prefs.SetBoolean(kPrefsP2PEnabled, false);
86 EXPECT_FALSE(manager->IsP2PEnabled());
87
88 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
89}
90
91// Check that IsP2PEnabled() always returns true if Enterprise Policy
92// indicates that p2p is to be used.
93TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingTrue) {
94 string temp_dir;
95 Prefs prefs;
Gilad Arnolda6742b32014-01-11 00:18:34 -080096 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthen92d9c8b2013-09-11 10:58:11 -070097 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -070098 prefs.Init(base::FilePath(temp_dir));
David Zeuthen92d9c8b2013-09-11 10:58:11 -070099
Ben Chan02f7c1d2014-10-18 15:18:02 -0700100 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700101 &prefs, "cros_au", 3));
Ben Chan02f7c1d2014-10-18 15:18:02 -0700102 unique_ptr<policy::MockDevicePolicy> device_policy(
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700103 new policy::MockDevicePolicy());
104 EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700105 DoAll(testing::SetArgumentPointee<0>(true),
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700106 testing::Return(true)));
107 manager->SetDevicePolicy(device_policy.get());
108 EXPECT_TRUE(manager->IsP2PEnabled());
109
110 // Should still return true even if crosh flag says otherwise.
111 prefs.SetBoolean(kPrefsP2PEnabled, false);
112 EXPECT_TRUE(manager->IsP2PEnabled());
113
114 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
115}
116
Alex Vakulenko072359c2014-07-18 11:41:07 -0700117// Check that IsP2PEnabled() corresponds to the value of the crosh
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700118// flag if Enterprise Policy indicates that p2p is not to be used.
119TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingFalse) {
120 string temp_dir;
121 Prefs prefs;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800122 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700123 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700124 prefs.Init(base::FilePath(temp_dir));
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700125
Ben Chan02f7c1d2014-10-18 15:18:02 -0700126 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700127 &prefs, "cros_au", 3));
Ben Chan02f7c1d2014-10-18 15:18:02 -0700128 unique_ptr<policy::MockDevicePolicy> device_policy(
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700129 new policy::MockDevicePolicy());
130 EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700131 DoAll(testing::SetArgumentPointee<0>(false),
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700132 testing::Return(true)));
133 manager->SetDevicePolicy(device_policy.get());
134 EXPECT_FALSE(manager->IsP2PEnabled());
135
David Zeuthen27a48bc2013-08-06 12:06:29 -0700136 prefs.SetBoolean(kPrefsP2PEnabled, true);
137 EXPECT_TRUE(manager->IsP2PEnabled());
138 prefs.SetBoolean(kPrefsP2PEnabled, false);
139 EXPECT_FALSE(manager->IsP2PEnabled());
140
141 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
142}
143
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400144// Check that IsP2PEnabled() returns TRUE if
145// - The crosh flag is not set.
146// - Enterprise Policy is not set.
147// - Device is Enterprise Enrolled.
148TEST_F(P2PManagerTest, P2PEnabledEnterpriseEnrolledDevicesDefaultToEnabled) {
149 string temp_dir;
150 Prefs prefs;
151 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
152 &temp_dir));
153 prefs.Init(base::FilePath(temp_dir));
154
Ben Chan02f7c1d2014-10-18 15:18:02 -0700155 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400156 &prefs, "cros_au", 3));
Ben Chan02f7c1d2014-10-18 15:18:02 -0700157 unique_ptr<policy::MockDevicePolicy> device_policy(
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400158 new policy::MockDevicePolicy());
159 // We return an empty owner as this is an enterprise.
160 EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700161 DoAll(testing::SetArgumentPointee<0>(string("")),
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400162 testing::Return(true)));
163 manager->SetDevicePolicy(device_policy.get());
164 EXPECT_TRUE(manager->IsP2PEnabled());
165
166 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
167}
168
169// Check that IsP2PEnabled() returns FALSE if
170// - The crosh flag is not set.
171// - Enterprise Policy is set to FALSE.
172// - Device is Enterprise Enrolled.
173TEST_F(P2PManagerTest, P2PEnabledEnterpriseEnrolledDevicesOverrideDefault) {
174 string temp_dir;
175 Prefs prefs;
176 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
177 &temp_dir));
178 prefs.Init(base::FilePath(temp_dir));
179
Ben Chan02f7c1d2014-10-18 15:18:02 -0700180 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400181 &prefs, "cros_au", 3));
Ben Chan02f7c1d2014-10-18 15:18:02 -0700182 unique_ptr<policy::MockDevicePolicy> device_policy(
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400183 new policy::MockDevicePolicy());
184 // We return an empty owner as this is an enterprise.
185 EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly(
Alex Deymof329b932014-10-30 01:37:48 -0700186 DoAll(testing::SetArgumentPointee<0>(string("")),
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400187 testing::Return(true)));
188 // Make Enterprise Policy indicate p2p is not enabled.
189 EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly(
190 DoAll(testing::SetArgumentPointee<0>(false),
191 testing::Return(true)));
192 manager->SetDevicePolicy(device_policy.get());
193 EXPECT_FALSE(manager->IsP2PEnabled());
194
195 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
196}
197
David Zeuthen27a48bc2013-08-06 12:06:29 -0700198// Check that we keep the $N newest files with the .$EXT.p2p extension.
David Zeuthen45e2ae22013-09-03 11:46:11 -0700199TEST_F(P2PManagerTest, Housekeeping) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700200 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700201 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700202 EXPECT_EQ(manager->CountSharedFiles(), 0);
203
Alex Deymo0f513512013-09-13 14:11:26 -0700204 // Generate files with different timestamps matching our pattern and generate
205 // other files not matching the pattern.
206 double last_timestamp = -1;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700207 for (int n = 0; n < 5; n++) {
Alex Deymo0f513512013-09-13 14:11:26 -0700208 double current_timestamp;
209 do {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700210 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
Alex Deymo0f513512013-09-13 14:11:26 -0700211 "file_%d.cros_au.p2p", n));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700212 EXPECT_EQ(0, System(base::StringPrintf("touch %s",
213 file.value().c_str())));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700214
Alex Deymo0f513512013-09-13 14:11:26 -0700215 // Check that the current timestamp on the file is different from the
216 // previous generated file. This timestamp depends on the file system
217 // time resolution, for example, ext2/ext3 have a time resolution of one
218 // second while ext4 has a resolution of one nanosecond. If the assigned
219 // timestamp is the same, we introduce a bigger sleep and call touch
220 // again.
221 struct stat statbuf;
222 EXPECT_EQ(stat(file.value().c_str(), &statbuf), 0);
223 current_timestamp = utils::TimeFromStructTimespec(&statbuf.st_ctim)
224 .ToDoubleT();
225 if (current_timestamp == last_timestamp)
226 sleep(1);
227 } while (current_timestamp == last_timestamp);
228 last_timestamp = current_timestamp;
229
Alex Vakulenko75039d72014-03-25 12:36:28 -0700230 EXPECT_EQ(0, System(base::StringPrintf(
231 "touch %s/file_%d.OTHER.p2p",
232 test_conf_->GetP2PDir().value().c_str(), n)));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700233
Alex Deymo0f513512013-09-13 14:11:26 -0700234 // A sleep of one micro-second is enough to have a different "Change" time
235 // on the file on newer file systems.
236 g_usleep(1);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700237 }
238 // CountSharedFiles() only counts 'cros_au' files.
239 EXPECT_EQ(manager->CountSharedFiles(), 5);
240
241 EXPECT_TRUE(manager->PerformHousekeeping());
242
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);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700250 file_name = base::StringPrintf(
251 "%s/file_%d.cros_au.p2p",
252 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700253 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
254
Alex Vakulenko75039d72014-03-25 12:36:28 -0700255 file_name = base::StringPrintf(
256 "%s/file_%d.OTHER.p2p",
257 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700258 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
259 }
260 // CountSharedFiles() only counts 'cros_au' files.
261 EXPECT_EQ(manager->CountSharedFiles(), 3);
262}
263
264static 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
Ben Chan02f7c1d2014-10-18 15:18:02 -0700355 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700356 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700357 EXPECT_TRUE(manager->FileShare("foo", 10 * 1000 * 1000));
358 EXPECT_EQ(manager->FileGetPath("foo"),
359 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
360 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
361 "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000));
362
363 // Sharing it again - with the same expected size - should return true
364 EXPECT_TRUE(manager->FileShare("foo", 10 * 1000 * 1000));
365
366 // ... but if we use the wrong size, it should fail
367 EXPECT_FALSE(manager->FileShare("foo", 10 * 1000 * 1000 + 1));
368}
369
370// Check that making a shared file visible, does what is expected.
371TEST_F(P2PManagerTest, MakeFileVisible) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700372 if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700373 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
374 << "Please update your system to support this feature.";
375 return;
376 }
377
Ben Chan02f7c1d2014-10-18 15:18:02 -0700378 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700379 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700380 // First, check that it's not visible.
381 manager->FileShare("foo", 10*1000*1000);
382 EXPECT_EQ(manager->FileGetPath("foo"),
383 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
384 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
385 "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000));
386 // Make the file visible and check that it changed its name. Do it
387 // twice to check that FileMakeVisible() is idempotent.
388 for (int n = 0; n < 2; n++) {
389 manager->FileMakeVisible("foo");
390 EXPECT_EQ(manager->FileGetPath("foo"),
391 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
392 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
393 "foo.cros_au.p2p", 0, 10 * 1000 * 1000));
394 }
395}
396
397// Check that we return the right values for existing files in P2P_DIR.
398TEST_F(P2PManagerTest, ExistingFiles) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700399 if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700400 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
401 << "Please update your system to support this feature.";
402 return;
403 }
404
Ben Chan02f7c1d2014-10-18 15:18:02 -0700405 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700406 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700407 bool visible;
408
409 // Check that errors are returned if the file does not exist
Alex Vakulenko75039d72014-03-25 12:36:28 -0700410 EXPECT_EQ(manager->FileGetPath("foo"), base::FilePath());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700411 EXPECT_EQ(manager->FileGetSize("foo"), -1);
412 EXPECT_EQ(manager->FileGetExpectedSize("foo"), -1);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700413 EXPECT_FALSE(manager->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700414 // ... then create the file ...
415 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
416 "foo.cros_au.p2p", 42, 43));
417 // ... and then check that the expected values are returned
418 EXPECT_EQ(manager->FileGetPath("foo"),
419 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
420 EXPECT_EQ(manager->FileGetSize("foo"), 42);
421 EXPECT_EQ(manager->FileGetExpectedSize("foo"), 43);
422 EXPECT_TRUE(manager->FileGetVisible("foo", &visible));
423 EXPECT_TRUE(visible);
424
425 // One more time, this time with a .tmp variant. First ensure it errors out..
Alex Vakulenko75039d72014-03-25 12:36:28 -0700426 EXPECT_EQ(manager->FileGetPath("bar"), base::FilePath());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700427 EXPECT_EQ(manager->FileGetSize("bar"), -1);
428 EXPECT_EQ(manager->FileGetExpectedSize("bar"), -1);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700429 EXPECT_FALSE(manager->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700430 // ... then create the file ...
431 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
432 "bar.cros_au.p2p.tmp", 44, 45));
433 // ... and then check that the expected values are returned
434 EXPECT_EQ(manager->FileGetPath("bar"),
435 test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
436 EXPECT_EQ(manager->FileGetSize("bar"), 44);
437 EXPECT_EQ(manager->FileGetExpectedSize("bar"), 45);
438 EXPECT_TRUE(manager->FileGetVisible("bar", &visible));
439 EXPECT_FALSE(visible);
440}
441
David Zeuthen27a48bc2013-08-06 12:06:29 -0700442// This is a little bit ugly but short of mocking a 'p2p' service this
443// will have to do. E.g. we essentially simulate the various
444// behaviours of initctl(8) that we rely on.
445TEST_F(P2PManagerTest, StartP2P) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700446 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700447 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700448
449 // Check that we can start the service
450 test_conf_->SetInitctlStartCommandLine("true");
451 EXPECT_TRUE(manager->EnsureP2PRunning());
452 test_conf_->SetInitctlStartCommandLine("false");
453 EXPECT_FALSE(manager->EnsureP2PRunning());
454 test_conf_->SetInitctlStartCommandLine(
455 "sh -c 'echo \"initctl: Job is already running: p2p\" >&2; false'");
456 EXPECT_TRUE(manager->EnsureP2PRunning());
457 test_conf_->SetInitctlStartCommandLine(
458 "sh -c 'echo something else >&2; false'");
459 EXPECT_FALSE(manager->EnsureP2PRunning());
460}
461
462// Same comment as for StartP2P
463TEST_F(P2PManagerTest, StopP2P) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700464 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700465 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700466
467 // Check that we can start the service
468 test_conf_->SetInitctlStopCommandLine("true");
469 EXPECT_TRUE(manager->EnsureP2PNotRunning());
470 test_conf_->SetInitctlStopCommandLine("false");
471 EXPECT_FALSE(manager->EnsureP2PNotRunning());
472 test_conf_->SetInitctlStopCommandLine(
473 "sh -c 'echo \"initctl: Unknown instance \" >&2; false'");
474 EXPECT_TRUE(manager->EnsureP2PNotRunning());
475 test_conf_->SetInitctlStopCommandLine(
476 "sh -c 'echo something else >&2; false'");
477 EXPECT_FALSE(manager->EnsureP2PNotRunning());
478}
479
480static void ExpectUrl(const string& expected_url,
481 GMainLoop* loop,
482 const string& url) {
483 EXPECT_EQ(url, expected_url);
484 g_main_loop_quit(loop);
485}
486
487// Like StartP2P, we're mocking the different results that p2p-client
488// can return. It's not pretty but it works.
489TEST_F(P2PManagerTest, LookupURL) {
Ben Chan02f7c1d2014-10-18 15:18:02 -0700490 unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700491 nullptr, "cros_au", 3));
492 GMainLoop *loop = g_main_loop_new(nullptr, FALSE);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700493
494 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
495 // being propagated in the right places.
Alex Deymo8ad6da92014-07-15 17:17:45 -0700496 test_conf_->SetP2PClientCommandLine(
497 "echo 'http://1.2.3.4/{file_id}_{minsize}'");
David Zeuthen27a48bc2013-08-06 12:06:29 -0700498 manager->LookupUrlForFile("fooX", 42, TimeDelta(),
499 base::Bind(ExpectUrl,
500 "http://1.2.3.4/fooX.cros_au_42", loop));
501 g_main_loop_run(loop);
502
503 // Emulate p2p-client returning invalid URL.
504 test_conf_->SetP2PClientCommandLine("echo 'not_a_valid_url'");
505 manager->LookupUrlForFile("foobar", 42, TimeDelta(),
506 base::Bind(ExpectUrl, "", loop));
507 g_main_loop_run(loop);
508
509 // Emulate p2p-client conveying failure.
510 test_conf_->SetP2PClientCommandLine("false");
511 manager->LookupUrlForFile("foobar", 42, TimeDelta(),
512 base::Bind(ExpectUrl, "", loop));
513 g_main_loop_run(loop);
514
515 // Emulate p2p-client not existing.
516 test_conf_->SetP2PClientCommandLine("/path/to/non/existent/helper/program");
517 manager->LookupUrlForFile("foobar", 42,
518 TimeDelta(),
519 base::Bind(ExpectUrl, "", loop));
520 g_main_loop_run(loop);
521
522 // Emulate p2p-client crashing.
523 test_conf_->SetP2PClientCommandLine("sh -c 'kill -SEGV $$'");
524 manager->LookupUrlForFile("foobar", 42, TimeDelta(),
525 base::Bind(ExpectUrl, "", loop));
526 g_main_loop_run(loop);
527
528 // Emulate p2p-client exceeding its timeout.
529 test_conf_->SetP2PClientCommandLine("sh -c 'echo http://1.2.3.4/; sleep 2'");
530 manager->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
531 base::Bind(ExpectUrl, "", loop));
532 g_main_loop_run(loop);
533
534 g_main_loop_unref(loop);
535}
536
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700537} // namespace chromeos_update_engine