blob: 59ab1523c391eeea1a1bb20d45a85fecdbd109dc [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
13#include "gmock/gmock.h"
14#include "gtest/gtest.h"
15
16#include "base/bind.h"
17#include "base/callback.h"
Alex Vakulenko75039d72014-03-25 12:36:28 -070018#include <base/strings/stringprintf.h>
David Zeuthen92d9c8b2013-09-11 10:58:11 -070019#include <policy/libpolicy.h>
20#include <policy/mock_device_policy.h>
David Zeuthen27a48bc2013-08-06 12:06:29 -070021
David Zeuthen27a48bc2013-08-06 12:06:29 -070022#include "update_engine/fake_p2p_manager_configuration.h"
Alex Vakulenko44cab302014-07-23 13:12:15 -070023#include "update_engine/p2p_manager.h"
David Zeuthen27a48bc2013-08-06 12:06:29 -070024#include "update_engine/prefs.h"
25#include "update_engine/test_utils.h"
26#include "update_engine/utils.h"
27
28using std::string;
29using std::vector;
30using base::TimeDelta;
31
32namespace chromeos_update_engine {
33
34// Test fixture that sets up a testing configuration (with e.g. a
35// temporary p2p dir) for P2PManager and cleans up when the test is
36// done.
37class P2PManagerTest : public testing::Test {
Alex Vakulenkod2779df2014-06-16 13:19:00 -070038 protected:
David Zeuthen27a48bc2013-08-06 12:06:29 -070039 P2PManagerTest() {}
40 virtual ~P2PManagerTest() {}
41
42 // Derived from testing::Test.
43 virtual void SetUp() {
44 test_conf_ = new FakeP2PManagerConfiguration();
45 }
46 virtual void TearDown() {}
47
48 // The P2PManager::Configuration instance used for testing.
49 FakeP2PManagerConfiguration *test_conf_;
50};
51
52
David Zeuthen92d9c8b2013-09-11 10:58:11 -070053// Check that IsP2PEnabled() returns false if neither the crosh flag
54// nor the Enterprise Policy indicates p2p is to be used.
55TEST_F(P2PManagerTest, P2PEnabledNeitherCroshFlagNotEnterpriseSetting) {
David Zeuthen27a48bc2013-08-06 12:06:29 -070056 string temp_dir;
57 Prefs prefs;
Gilad Arnolda6742b32014-01-11 00:18:34 -080058 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthen27a48bc2013-08-06 12:06:29 -070059 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -070060 prefs.Init(base::FilePath(temp_dir));
David Zeuthen27a48bc2013-08-06 12:06:29 -070061
62 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
63 &prefs, "cros_au", 3));
64 EXPECT_FALSE(manager->IsP2PEnabled());
David Zeuthen92d9c8b2013-09-11 10:58:11 -070065
66 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
67}
68
69// Check that IsP2PEnabled() corresponds to value of the crosh flag
70// when Enterprise Policy is not set.
71TEST_F(P2PManagerTest, P2PEnabledCroshFlag) {
72 string temp_dir;
73 Prefs prefs;
Gilad Arnolda6742b32014-01-11 00:18:34 -080074 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthen92d9c8b2013-09-11 10:58:11 -070075 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -070076 prefs.Init(base::FilePath(temp_dir));
David Zeuthen92d9c8b2013-09-11 10:58:11 -070077
78 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
79 &prefs, "cros_au", 3));
80 EXPECT_FALSE(manager->IsP2PEnabled());
81 prefs.SetBoolean(kPrefsP2PEnabled, true);
82 EXPECT_TRUE(manager->IsP2PEnabled());
83 prefs.SetBoolean(kPrefsP2PEnabled, false);
84 EXPECT_FALSE(manager->IsP2PEnabled());
85
86 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
87}
88
89// Check that IsP2PEnabled() always returns true if Enterprise Policy
90// indicates that p2p is to be used.
91TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingTrue) {
92 string temp_dir;
93 Prefs prefs;
Gilad Arnolda6742b32014-01-11 00:18:34 -080094 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthen92d9c8b2013-09-11 10:58:11 -070095 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -070096 prefs.Init(base::FilePath(temp_dir));
David Zeuthen92d9c8b2013-09-11 10:58:11 -070097
98 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
99 &prefs, "cros_au", 3));
100 scoped_ptr<policy::MockDevicePolicy> device_policy(
101 new policy::MockDevicePolicy());
102 EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700103 DoAll(testing::SetArgumentPointee<0>(true),
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700104 testing::Return(true)));
105 manager->SetDevicePolicy(device_policy.get());
106 EXPECT_TRUE(manager->IsP2PEnabled());
107
108 // Should still return true even if crosh flag says otherwise.
109 prefs.SetBoolean(kPrefsP2PEnabled, false);
110 EXPECT_TRUE(manager->IsP2PEnabled());
111
112 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
113}
114
Alex Vakulenko072359c2014-07-18 11:41:07 -0700115// Check that IsP2PEnabled() corresponds to the value of the crosh
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700116// flag if Enterprise Policy indicates that p2p is not to be used.
117TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingFalse) {
118 string temp_dir;
119 Prefs prefs;
Gilad Arnolda6742b32014-01-11 00:18:34 -0800120 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700121 &temp_dir));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700122 prefs.Init(base::FilePath(temp_dir));
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700123
124 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
125 &prefs, "cros_au", 3));
126 scoped_ptr<policy::MockDevicePolicy> device_policy(
127 new policy::MockDevicePolicy());
128 EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly(
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700129 DoAll(testing::SetArgumentPointee<0>(false),
David Zeuthen92d9c8b2013-09-11 10:58:11 -0700130 testing::Return(true)));
131 manager->SetDevicePolicy(device_policy.get());
132 EXPECT_FALSE(manager->IsP2PEnabled());
133
David Zeuthen27a48bc2013-08-06 12:06:29 -0700134 prefs.SetBoolean(kPrefsP2PEnabled, true);
135 EXPECT_TRUE(manager->IsP2PEnabled());
136 prefs.SetBoolean(kPrefsP2PEnabled, false);
137 EXPECT_FALSE(manager->IsP2PEnabled());
138
139 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
140}
141
David Zeuthen9a58e6a2014-09-22 17:38:44 -0400142// Check that IsP2PEnabled() returns TRUE if
143// - The crosh flag is not set.
144// - Enterprise Policy is not set.
145// - Device is Enterprise Enrolled.
146TEST_F(P2PManagerTest, P2PEnabledEnterpriseEnrolledDevicesDefaultToEnabled) {
147 string temp_dir;
148 Prefs prefs;
149 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
150 &temp_dir));
151 prefs.Init(base::FilePath(temp_dir));
152
153 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
154 &prefs, "cros_au", 3));
155 scoped_ptr<policy::MockDevicePolicy> device_policy(
156 new policy::MockDevicePolicy());
157 // We return an empty owner as this is an enterprise.
158 EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly(
159 DoAll(testing::SetArgumentPointee<0>(std::string("")),
160 testing::Return(true)));
161 manager->SetDevicePolicy(device_policy.get());
162 EXPECT_TRUE(manager->IsP2PEnabled());
163
164 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
165}
166
167// Check that IsP2PEnabled() returns FALSE if
168// - The crosh flag is not set.
169// - Enterprise Policy is set to FALSE.
170// - Device is Enterprise Enrolled.
171TEST_F(P2PManagerTest, P2PEnabledEnterpriseEnrolledDevicesOverrideDefault) {
172 string temp_dir;
173 Prefs prefs;
174 EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX",
175 &temp_dir));
176 prefs.Init(base::FilePath(temp_dir));
177
178 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
179 &prefs, "cros_au", 3));
180 scoped_ptr<policy::MockDevicePolicy> device_policy(
181 new policy::MockDevicePolicy());
182 // We return an empty owner as this is an enterprise.
183 EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly(
184 DoAll(testing::SetArgumentPointee<0>(std::string("")),
185 testing::Return(true)));
186 // Make Enterprise Policy indicate p2p is not enabled.
187 EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly(
188 DoAll(testing::SetArgumentPointee<0>(false),
189 testing::Return(true)));
190 manager->SetDevicePolicy(device_policy.get());
191 EXPECT_FALSE(manager->IsP2PEnabled());
192
193 EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir));
194}
195
David Zeuthen27a48bc2013-08-06 12:06:29 -0700196// Check that we keep the $N newest files with the .$EXT.p2p extension.
David Zeuthen45e2ae22013-09-03 11:46:11 -0700197TEST_F(P2PManagerTest, Housekeeping) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700198 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700199 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700200 EXPECT_EQ(manager->CountSharedFiles(), 0);
201
Alex Deymo0f513512013-09-13 14:11:26 -0700202 // Generate files with different timestamps matching our pattern and generate
203 // other files not matching the pattern.
204 double last_timestamp = -1;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700205 for (int n = 0; n < 5; n++) {
Alex Deymo0f513512013-09-13 14:11:26 -0700206 double current_timestamp;
207 do {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700208 base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf(
Alex Deymo0f513512013-09-13 14:11:26 -0700209 "file_%d.cros_au.p2p", n));
Alex Vakulenko75039d72014-03-25 12:36:28 -0700210 EXPECT_EQ(0, System(base::StringPrintf("touch %s",
211 file.value().c_str())));
David Zeuthen45e2ae22013-09-03 11:46:11 -0700212
Alex Deymo0f513512013-09-13 14:11:26 -0700213 // Check that the current timestamp on the file is different from the
214 // previous generated file. This timestamp depends on the file system
215 // time resolution, for example, ext2/ext3 have a time resolution of one
216 // second while ext4 has a resolution of one nanosecond. If the assigned
217 // timestamp is the same, we introduce a bigger sleep and call touch
218 // again.
219 struct stat statbuf;
220 EXPECT_EQ(stat(file.value().c_str(), &statbuf), 0);
221 current_timestamp = utils::TimeFromStructTimespec(&statbuf.st_ctim)
222 .ToDoubleT();
223 if (current_timestamp == last_timestamp)
224 sleep(1);
225 } while (current_timestamp == last_timestamp);
226 last_timestamp = current_timestamp;
227
Alex Vakulenko75039d72014-03-25 12:36:28 -0700228 EXPECT_EQ(0, System(base::StringPrintf(
229 "touch %s/file_%d.OTHER.p2p",
230 test_conf_->GetP2PDir().value().c_str(), n)));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700231
Alex Deymo0f513512013-09-13 14:11:26 -0700232 // A sleep of one micro-second is enough to have a different "Change" time
233 // on the file on newer file systems.
234 g_usleep(1);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700235 }
236 // CountSharedFiles() only counts 'cros_au' files.
237 EXPECT_EQ(manager->CountSharedFiles(), 5);
238
239 EXPECT_TRUE(manager->PerformHousekeeping());
240
241 // At this point - after HouseKeeping - we should only have
242 // eight files left.
243 for (int n = 0; n < 5; n++) {
244 string file_name;
245 bool expect;
246
247 expect = (n >= 2);
Alex Vakulenko75039d72014-03-25 12:36:28 -0700248 file_name = base::StringPrintf(
249 "%s/file_%d.cros_au.p2p",
250 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700251 EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect);
252
Alex Vakulenko75039d72014-03-25 12:36:28 -0700253 file_name = base::StringPrintf(
254 "%s/file_%d.OTHER.p2p",
255 test_conf_->GetP2PDir().value().c_str(), n);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700256 EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS));
257 }
258 // CountSharedFiles() only counts 'cros_au' files.
259 EXPECT_EQ(manager->CountSharedFiles(), 3);
260}
261
262static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
263 ssize_t expected_size, ssize_t expected_size_xattr) {
264 string path = p2p_dir + "/" + file_name;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700265 char ea_value[64] = { 0 };
266 ssize_t ea_size;
267
Gabe Blacka77939e2014-09-09 23:35:08 -0700268 off_t p2p_size = utils::FileSize(path);
269 if (p2p_size < 0) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700270 LOG(ERROR) << "File " << path << " does not exist";
271 return false;
272 }
273
274 if (expected_size != 0) {
Gabe Blacka77939e2014-09-09 23:35:08 -0700275 if (p2p_size != expected_size) {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700276 LOG(ERROR) << "Expected size " << expected_size
Gabe Blacka77939e2014-09-09 23:35:08 -0700277 << " but size was " << p2p_size;
David Zeuthen27a48bc2013-08-06 12:06:29 -0700278 return false;
279 }
280 }
281
282 if (expected_size_xattr == 0) {
283 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
284 &ea_value, sizeof ea_value - 1);
285 if (ea_size == -1 && errno == ENOATTR) {
286 // This is valid behavior as we support files without the xattr set.
287 } else {
288 PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, "
289 << "ea_size=" << ea_size << ", errno=" << errno;
290 return false;
291 }
292 } else {
293 ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
294 &ea_value, sizeof ea_value - 1);
295 if (ea_size < 0) {
296 LOG(ERROR) << "Error getting xattr attribute";
297 return false;
298 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700299 char* endp = nullptr;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700300 long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700301 if (endp == nullptr || *endp != '\0') {
David Zeuthen27a48bc2013-08-06 12:06:29 -0700302 LOG(ERROR) << "Error parsing xattr '" << ea_value
303 << "' as an integer";
304 return false;
305 }
306 if (val != expected_size_xattr) {
307 LOG(ERROR) << "Expected xattr size " << expected_size_xattr
308 << " but size was " << val;
309 return false;
310 }
311 }
312
313 return true;
314}
315
316static bool CreateP2PFile(string p2p_dir, string file_name,
317 size_t size, size_t size_xattr) {
318 string path = p2p_dir + "/" + file_name;
319
320 int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
321 if (fd == -1) {
322 PLOG(ERROR) << "Error creating file with path " << path;
323 return false;
324 }
325 if (ftruncate(fd, size) != 0) {
326 PLOG(ERROR) << "Error truncating " << path << " to size " << size;
327 close(fd);
328 return false;
329 }
330
331 if (size_xattr != 0) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700332 string decimal_size = base::StringPrintf("%zu", size_xattr);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700333 if (fsetxattr(fd, "user.cros-p2p-filesize",
334 decimal_size.c_str(), decimal_size.size(), 0) != 0) {
335 PLOG(ERROR) << "Error setting xattr on " << path;
336 close(fd);
337 return false;
338 }
339 }
340
341 close(fd);
342 return true;
343}
344
345// Check that sharing a *new* file works.
346TEST_F(P2PManagerTest, ShareFile) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700347 if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700348 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
349 << "Please update your system to support this feature.";
350 return;
351 }
352
David Zeuthen27a48bc2013-08-06 12:06:29 -0700353 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700354 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700355 EXPECT_TRUE(manager->FileShare("foo", 10 * 1000 * 1000));
356 EXPECT_EQ(manager->FileGetPath("foo"),
357 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
362 EXPECT_TRUE(manager->FileShare("foo", 10 * 1000 * 1000));
363
364 // ... but if we use the wrong size, it should fail
365 EXPECT_FALSE(manager->FileShare("foo", 10 * 1000 * 1000 + 1));
366}
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 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700377 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700378 // First, check that it's not visible.
379 manager->FileShare("foo", 10*1000*1000);
380 EXPECT_EQ(manager->FileGetPath("foo"),
381 test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
382 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
383 "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000));
384 // Make the file visible and check that it changed its name. Do it
385 // twice to check that FileMakeVisible() is idempotent.
386 for (int n = 0; n < 2; n++) {
387 manager->FileMakeVisible("foo");
388 EXPECT_EQ(manager->FileGetPath("foo"),
389 test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
390 EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
391 "foo.cros_au.p2p", 0, 10 * 1000 * 1000));
392 }
393}
394
395// Check that we return the right values for existing files in P2P_DIR.
396TEST_F(P2PManagerTest, ExistingFiles) {
Alex Vakulenko75039d72014-03-25 12:36:28 -0700397 if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) {
David Zeuthen910ec5b2013-09-26 12:10:58 -0700398 LOG(WARNING) << "Skipping test because /tmp does not support xattr. "
399 << "Please update your system to support this feature.";
400 return;
401 }
402
David Zeuthen27a48bc2013-08-06 12:06:29 -0700403 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700404 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700405 bool visible;
406
407 // Check that errors are returned if the file does not exist
Alex Vakulenko75039d72014-03-25 12:36:28 -0700408 EXPECT_EQ(manager->FileGetPath("foo"), base::FilePath());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700409 EXPECT_EQ(manager->FileGetSize("foo"), -1);
410 EXPECT_EQ(manager->FileGetExpectedSize("foo"), -1);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700411 EXPECT_FALSE(manager->FileGetVisible("foo", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700412 // ... then create the file ...
413 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
414 "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..
Alex Vakulenko75039d72014-03-25 12:36:28 -0700424 EXPECT_EQ(manager->FileGetPath("bar"), base::FilePath());
David Zeuthen27a48bc2013-08-06 12:06:29 -0700425 EXPECT_EQ(manager->FileGetSize("bar"), -1);
426 EXPECT_EQ(manager->FileGetExpectedSize("bar"), -1);
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700427 EXPECT_FALSE(manager->FileGetVisible("bar", nullptr));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700428 // ... then create the file ...
429 EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
430 "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
David Zeuthen27a48bc2013-08-06 12:06:29 -0700440// 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.
443TEST_F(P2PManagerTest, StartP2P) {
444 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700445 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700446
447 // Check that we can start the service
448 test_conf_->SetInitctlStartCommandLine("true");
449 EXPECT_TRUE(manager->EnsureP2PRunning());
450 test_conf_->SetInitctlStartCommandLine("false");
451 EXPECT_FALSE(manager->EnsureP2PRunning());
452 test_conf_->SetInitctlStartCommandLine(
453 "sh -c 'echo \"initctl: Job is already running: p2p\" >&2; false'");
454 EXPECT_TRUE(manager->EnsureP2PRunning());
455 test_conf_->SetInitctlStartCommandLine(
456 "sh -c 'echo something else >&2; false'");
457 EXPECT_FALSE(manager->EnsureP2PRunning());
458}
459
460// Same comment as for StartP2P
461TEST_F(P2PManagerTest, StopP2P) {
462 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700463 nullptr, "cros_au", 3));
David Zeuthen27a48bc2013-08-06 12:06:29 -0700464
465 // Check that we can start the service
466 test_conf_->SetInitctlStopCommandLine("true");
467 EXPECT_TRUE(manager->EnsureP2PNotRunning());
468 test_conf_->SetInitctlStopCommandLine("false");
469 EXPECT_FALSE(manager->EnsureP2PNotRunning());
470 test_conf_->SetInitctlStopCommandLine(
471 "sh -c 'echo \"initctl: Unknown instance \" >&2; false'");
472 EXPECT_TRUE(manager->EnsureP2PNotRunning());
473 test_conf_->SetInitctlStopCommandLine(
474 "sh -c 'echo something else >&2; false'");
475 EXPECT_FALSE(manager->EnsureP2PNotRunning());
476}
477
478static void ExpectUrl(const string& expected_url,
479 GMainLoop* loop,
480 const string& url) {
481 EXPECT_EQ(url, expected_url);
482 g_main_loop_quit(loop);
483}
484
485// Like StartP2P, we're mocking the different results that p2p-client
486// can return. It's not pretty but it works.
487TEST_F(P2PManagerTest, LookupURL) {
488 scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_,
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700489 nullptr, "cros_au", 3));
490 GMainLoop *loop = g_main_loop_new(nullptr, FALSE);
David Zeuthen27a48bc2013-08-06 12:06:29 -0700491
492 // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
493 // being propagated in the right places.
Alex Deymo8ad6da92014-07-15 17:17:45 -0700494 test_conf_->SetP2PClientCommandLine(
495 "echo 'http://1.2.3.4/{file_id}_{minsize}'");
David Zeuthen27a48bc2013-08-06 12:06:29 -0700496 manager->LookupUrlForFile("fooX", 42, TimeDelta(),
497 base::Bind(ExpectUrl,
498 "http://1.2.3.4/fooX.cros_au_42", loop));
499 g_main_loop_run(loop);
500
501 // Emulate p2p-client returning invalid URL.
502 test_conf_->SetP2PClientCommandLine("echo 'not_a_valid_url'");
503 manager->LookupUrlForFile("foobar", 42, TimeDelta(),
504 base::Bind(ExpectUrl, "", loop));
505 g_main_loop_run(loop);
506
507 // Emulate p2p-client conveying failure.
508 test_conf_->SetP2PClientCommandLine("false");
509 manager->LookupUrlForFile("foobar", 42, TimeDelta(),
510 base::Bind(ExpectUrl, "", loop));
511 g_main_loop_run(loop);
512
513 // Emulate p2p-client not existing.
514 test_conf_->SetP2PClientCommandLine("/path/to/non/existent/helper/program");
515 manager->LookupUrlForFile("foobar", 42,
516 TimeDelta(),
517 base::Bind(ExpectUrl, "", loop));
518 g_main_loop_run(loop);
519
520 // Emulate p2p-client crashing.
521 test_conf_->SetP2PClientCommandLine("sh -c 'kill -SEGV $$'");
522 manager->LookupUrlForFile("foobar", 42, TimeDelta(),
523 base::Bind(ExpectUrl, "", loop));
524 g_main_loop_run(loop);
525
526 // Emulate p2p-client exceeding its timeout.
527 test_conf_->SetP2PClientCommandLine("sh -c 'echo http://1.2.3.4/; sleep 2'");
528 manager->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
529 base::Bind(ExpectUrl, "", loop));
530 g_main_loop_run(loop);
531
532 g_main_loop_unref(loop);
533}
534
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700535} // namespace chromeos_update_engine