summaryrefslogtreecommitdiff
path: root/p2p_manager_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'p2p_manager_unittest.cc')
-rw-r--r--p2p_manager_unittest.cc205
1 files changed, 111 insertions, 94 deletions
diff --git a/p2p_manager_unittest.cc b/p2p_manager_unittest.cc
index 736a87a7..5771ec1d 100644
--- a/p2p_manager_unittest.cc
+++ b/p2p_manager_unittest.cc
@@ -55,10 +55,10 @@ using brillo::MessageLoop;
using std::string;
using std::unique_ptr;
using std::vector;
+using testing::_;
using testing::DoAll;
using testing::Return;
using testing::SetArgPointee;
-using testing::_;
namespace chromeos_update_engine {
@@ -84,8 +84,11 @@ class P2PManagerTest : public testing::Test {
fake_um_.set_policy(mock_policy_);
// Construct the P2P manager under test.
- manager_.reset(P2PManager::Construct(test_conf_, &fake_clock_, &fake_um_,
- "cros_au", 3,
+ manager_.reset(P2PManager::Construct(test_conf_,
+ &fake_clock_,
+ &fake_um_,
+ "cros_au",
+ 3,
TimeDelta::FromDays(5)));
}
@@ -95,16 +98,15 @@ class P2PManagerTest : public testing::Test {
Subprocess subprocess_;
// The P2PManager::Configuration instance used for testing.
- FakeP2PManagerConfiguration *test_conf_;
+ FakeP2PManagerConfiguration* test_conf_;
FakeClock fake_clock_;
- chromeos_update_manager::MockPolicy *mock_policy_ = nullptr;
+ chromeos_update_manager::MockPolicy* mock_policy_ = nullptr;
chromeos_update_manager::FakeUpdateManager fake_um_;
unique_ptr<P2PManager> manager_;
};
-
// Check that IsP2PEnabled() polls the policy correctly, with the value not
// changing between calls.
TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
@@ -120,9 +122,8 @@ TEST_F(P2PManagerTest, P2PEnabledInitAndNotChanged) {
// between calls.
TEST_F(P2PManagerTest, P2PEnabledInitAndChanged) {
EXPECT_CALL(*mock_policy_, P2PEnabled(_, _, _, _))
- .WillOnce(DoAll(
- SetArgPointee<3>(true),
- Return(chromeos_update_manager::EvalStatus::kSucceeded)));
+ .WillOnce(DoAll(SetArgPointee<3>(true),
+ Return(chromeos_update_manager::EvalStatus::kSucceeded)));
EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, true));
EXPECT_CALL(*mock_policy_, P2PEnabledChanged(_, _, _, _, false));
@@ -137,23 +138,26 @@ TEST_F(P2PManagerTest, HousekeepingCountLimit) {
// we need to reallocate the test_conf_ member, whose currently aliased object
// will be freed.
test_conf_ = new FakeP2PManagerConfiguration();
- manager_.reset(P2PManager::Construct(
- test_conf_, &fake_clock_, &fake_um_, "cros_au", 3,
- TimeDelta() /* max_file_age */));
+ manager_.reset(P2PManager::Construct(test_conf_,
+ &fake_clock_,
+ &fake_um_,
+ "cros_au",
+ 3,
+ TimeDelta() /* max_file_age */));
EXPECT_EQ(manager_->CountSharedFiles(), 0);
base::Time start_time = base::Time::FromDoubleT(1246996800.);
// Generate files with different timestamps matching our pattern and generate
// other files not matching the pattern.
for (int n = 0; n < 5; n++) {
- base::FilePath path = test_conf_->GetP2PDir().Append(base::StringPrintf(
- "file_%d.cros_au.p2p", n));
+ base::FilePath path = test_conf_->GetP2PDir().Append(
+ base::StringPrintf("file_%d.cros_au.p2p", n));
base::Time file_time = start_time + TimeDelta::FromMinutes(n);
EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
- path = test_conf_->GetP2PDir().Append(base::StringPrintf(
- "file_%d.OTHER.p2p", n));
+ path = test_conf_->GetP2PDir().Append(
+ base::StringPrintf("file_%d.OTHER.p2p", n));
EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
EXPECT_TRUE(base::TouchFile(path, file_time, file_time));
}
@@ -170,13 +174,11 @@ TEST_F(P2PManagerTest, HousekeepingCountLimit) {
expect = (n >= 2);
file_name = base::StringPrintf(
- "%s/file_%d.cros_au.p2p",
- test_conf_->GetP2PDir().value().c_str(), n);
+ "%s/file_%d.cros_au.p2p", test_conf_->GetP2PDir().value().c_str(), n);
EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
file_name = base::StringPrintf(
- "%s/file_%d.OTHER.p2p",
- test_conf_->GetP2PDir().value().c_str(), n);
+ "%s/file_%d.OTHER.p2p", test_conf_->GetP2PDir().value().c_str(), n);
EXPECT_TRUE(utils::FileExists(file_name.c_str()));
}
// CountSharedFiles() only counts 'cros_au' files.
@@ -201,16 +203,19 @@ TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
// Note that we need to reallocate the test_conf_ member, whose currently
// aliased object will be freed.
test_conf_ = new FakeP2PManagerConfiguration();
- manager_.reset(P2PManager::Construct(
- test_conf_, &fake_clock_, &fake_um_, "cros_au",
- 0 /* num_files_to_keep */, age_limit));
+ manager_.reset(P2PManager::Construct(test_conf_,
+ &fake_clock_,
+ &fake_um_,
+ "cros_au",
+ 0 /* num_files_to_keep */,
+ age_limit));
EXPECT_EQ(manager_->CountSharedFiles(), 0);
// Generate files with different timestamps matching our pattern and generate
// other files not matching the pattern.
for (int n = 0; n < 5; n++) {
- base::FilePath path = test_conf_->GetP2PDir().Append(base::StringPrintf(
- "file_%d.cros_au.p2p", n));
+ base::FilePath path = test_conf_->GetP2PDir().Append(
+ base::StringPrintf("file_%d.cros_au.p2p", n));
// With five files and aiming for two of them to be before
// |cutoff_time|, we distribute it like this:
@@ -219,14 +224,14 @@ TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
// |
// cutoff_time
//
- base::Time file_date = cutoff_time + (n - 2) * TimeDelta::FromDays(1)
- + TimeDelta::FromHours(12);
+ base::Time file_date = cutoff_time + (n - 2) * TimeDelta::FromDays(1) +
+ TimeDelta::FromHours(12);
EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
- path = test_conf_->GetP2PDir().Append(base::StringPrintf(
- "file_%d.OTHER.p2p", n));
+ path = test_conf_->GetP2PDir().Append(
+ base::StringPrintf("file_%d.OTHER.p2p", n));
EXPECT_EQ(0, base::WriteFile(path, nullptr, 0));
EXPECT_TRUE(base::TouchFile(path, file_date, file_date));
}
@@ -243,23 +248,23 @@ TEST_F(P2PManagerTest, HousekeepingAgeLimit) {
expect = (n >= 2);
file_name = base::StringPrintf(
- "%s/file_%d.cros_au.p2p",
- test_conf_->GetP2PDir().value().c_str(), n);
+ "%s/file_%d.cros_au.p2p", test_conf_->GetP2PDir().value().c_str(), n);
EXPECT_EQ(expect, utils::FileExists(file_name.c_str()));
file_name = base::StringPrintf(
- "%s/file_%d.OTHER.p2p",
- test_conf_->GetP2PDir().value().c_str(), n);
+ "%s/file_%d.OTHER.p2p", test_conf_->GetP2PDir().value().c_str(), n);
EXPECT_TRUE(utils::FileExists(file_name.c_str()));
}
// CountSharedFiles() only counts 'cros_au' files.
EXPECT_EQ(manager_->CountSharedFiles(), 3);
}
-static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
- ssize_t expected_size, ssize_t expected_size_xattr) {
+static bool CheckP2PFile(const string& p2p_dir,
+ const string& file_name,
+ ssize_t expected_size,
+ ssize_t expected_size_xattr) {
string path = p2p_dir + "/" + file_name;
- char ea_value[64] = { 0 };
+ char ea_value[64] = {0};
ssize_t ea_size;
off_t p2p_size = utils::FileSize(path);
@@ -270,15 +275,15 @@ static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
if (expected_size != 0) {
if (p2p_size != expected_size) {
- LOG(ERROR) << "Expected size " << expected_size
- << " but size was " << p2p_size;
+ LOG(ERROR) << "Expected size " << expected_size << " but size was "
+ << p2p_size;
return false;
}
}
if (expected_size_xattr == 0) {
- ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
- &ea_value, sizeof ea_value - 1);
+ ea_size = getxattr(
+ path.c_str(), "user.cros-p2p-filesize", &ea_value, sizeof ea_value - 1);
if (ea_size == -1 && errno == ENODATA) {
// This is valid behavior as we support files without the xattr set.
} else {
@@ -287,8 +292,8 @@ static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
return false;
}
} else {
- ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize",
- &ea_value, sizeof ea_value - 1);
+ ea_size = getxattr(
+ path.c_str(), "user.cros-p2p-filesize", &ea_value, sizeof ea_value - 1);
if (ea_size < 0) {
LOG(ERROR) << "Error getting xattr attribute";
return false;
@@ -296,8 +301,7 @@ static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
char* endp = nullptr;
long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int)
if (endp == nullptr || *endp != '\0') {
- LOG(ERROR) << "Error parsing xattr '" << ea_value
- << "' as an integer";
+ LOG(ERROR) << "Error parsing xattr '" << ea_value << "' as an integer";
return false;
}
if (val != expected_size_xattr) {
@@ -310,11 +314,13 @@ static bool CheckP2PFile(const string& p2p_dir, const string& file_name,
return true;
}
-static bool CreateP2PFile(string p2p_dir, string file_name,
- size_t size, size_t size_xattr) {
+static bool CreateP2PFile(string p2p_dir,
+ string file_name,
+ size_t size,
+ size_t size_xattr) {
string path = p2p_dir + "/" + file_name;
- int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644);
+ int fd = open(path.c_str(), O_CREAT | O_RDWR, 0644);
if (fd == -1) {
PLOG(ERROR) << "Error creating file with path " << path;
return false;
@@ -327,8 +333,11 @@ static bool CreateP2PFile(string p2p_dir, string file_name,
if (size_xattr != 0) {
string decimal_size = std::to_string(size_xattr);
- if (fsetxattr(fd, "user.cros-p2p-filesize",
- decimal_size.c_str(), decimal_size.size(), 0) != 0) {
+ if (fsetxattr(fd,
+ "user.cros-p2p-filesize",
+ decimal_size.c_str(),
+ decimal_size.size(),
+ 0) != 0) {
PLOG(ERROR) << "Error setting xattr on " << path;
close(fd);
return false;
@@ -347,7 +356,9 @@ TEST_F(P2PManagerTest, ShareFile) {
EXPECT_EQ(manager_->FileGetPath("foo"),
test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
- "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
+ "foo.cros_au.p2p.tmp",
+ 0,
+ kP2PTestFileSize));
// Sharing it again - with the same expected size - should return true
EXPECT_TRUE(manager_->FileShare("foo", kP2PTestFileSize));
@@ -365,7 +376,9 @@ TEST_F(P2PManagerTest, MakeFileVisible) {
EXPECT_EQ(manager_->FileGetPath("foo"),
test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp"));
EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
- "foo.cros_au.p2p.tmp", 0, kP2PTestFileSize));
+ "foo.cros_au.p2p.tmp",
+ 0,
+ kP2PTestFileSize));
// Make the file visible and check that it changed its name. Do it
// twice to check that FileMakeVisible() is idempotent.
for (int n = 0; n < 2; n++) {
@@ -373,7 +386,9 @@ TEST_F(P2PManagerTest, MakeFileVisible) {
EXPECT_EQ(manager_->FileGetPath("foo"),
test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(),
- "foo.cros_au.p2p", 0, kP2PTestFileSize));
+ "foo.cros_au.p2p",
+ 0,
+ kP2PTestFileSize));
}
}
@@ -387,8 +402,8 @@ TEST_F(P2PManagerTest, ExistingFiles) {
EXPECT_EQ(manager_->FileGetExpectedSize("foo"), -1);
EXPECT_FALSE(manager_->FileGetVisible("foo", nullptr));
// ... then create the file ...
- EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
- "foo.cros_au.p2p", 42, 43));
+ EXPECT_TRUE(CreateP2PFile(
+ test_conf_->GetP2PDir().value(), "foo.cros_au.p2p", 42, 43));
// ... and then check that the expected values are returned
EXPECT_EQ(manager_->FileGetPath("foo"),
test_conf_->GetP2PDir().Append("foo.cros_au.p2p"));
@@ -403,8 +418,8 @@ TEST_F(P2PManagerTest, ExistingFiles) {
EXPECT_EQ(manager_->FileGetExpectedSize("bar"), -1);
EXPECT_FALSE(manager_->FileGetVisible("bar", nullptr));
// ... then create the file ...
- EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(),
- "bar.cros_au.p2p.tmp", 44, 45));
+ EXPECT_TRUE(CreateP2PFile(
+ test_conf_->GetP2PDir().value(), "bar.cros_au.p2p.tmp", 44, 45));
// ... and then check that the expected values are returned
EXPECT_EQ(manager_->FileGetPath("bar"),
test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp"));
@@ -423,11 +438,11 @@ TEST_F(P2PManagerTest, StartP2P) {
EXPECT_TRUE(manager_->EnsureP2PRunning());
test_conf_->SetInitctlStartCommand({"false"});
EXPECT_FALSE(manager_->EnsureP2PRunning());
- test_conf_->SetInitctlStartCommand({
- "sh", "-c", "echo \"initctl: Job is already running: p2p\" >&2; false"});
+ test_conf_->SetInitctlStartCommand(
+ {"sh", "-c", "echo \"initctl: Job is already running: p2p\" >&2; false"});
EXPECT_TRUE(manager_->EnsureP2PRunning());
- test_conf_->SetInitctlStartCommand({
- "sh", "-c", "echo something else >&2; false"});
+ test_conf_->SetInitctlStartCommand(
+ {"sh", "-c", "echo something else >&2; false"});
EXPECT_FALSE(manager_->EnsureP2PRunning());
}
@@ -438,16 +453,15 @@ TEST_F(P2PManagerTest, StopP2P) {
EXPECT_TRUE(manager_->EnsureP2PNotRunning());
test_conf_->SetInitctlStopCommand({"false"});
EXPECT_FALSE(manager_->EnsureP2PNotRunning());
- test_conf_->SetInitctlStopCommand({
- "sh", "-c", "echo \"initctl: Unknown instance \" >&2; false"});
+ test_conf_->SetInitctlStopCommand(
+ {"sh", "-c", "echo \"initctl: Unknown instance \" >&2; false"});
EXPECT_TRUE(manager_->EnsureP2PNotRunning());
- test_conf_->SetInitctlStopCommand({
- "sh", "-c", "echo something else >&2; false"});
+ test_conf_->SetInitctlStopCommand(
+ {"sh", "-c", "echo something else >&2; false"});
EXPECT_FALSE(manager_->EnsureP2PNotRunning());
}
-static void ExpectUrl(const string& expected_url,
- const string& url) {
+static void ExpectUrl(const string& expected_url, const string& url) {
EXPECT_EQ(url, expected_url);
MessageLoop::current()->BreakLoop();
}
@@ -457,53 +471,56 @@ static void ExpectUrl(const string& expected_url,
TEST_F(P2PManagerTest, LookupURL) {
// Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
// being propagated in the right places.
- test_conf_->SetP2PClientCommand({
- "echo", "http://1.2.3.4/{file_id}_{minsize}"});
- manager_->LookupUrlForFile("fooX", 42, TimeDelta(),
- base::Bind(ExpectUrl,
- "http://1.2.3.4/fooX.cros_au_42"));
+ test_conf_->SetP2PClientCommand(
+ {"echo", "http://1.2.3.4/{file_id}_{minsize}"});
+ manager_->LookupUrlForFile(
+ "fooX",
+ 42,
+ TimeDelta(),
+ base::Bind(ExpectUrl, "http://1.2.3.4/fooX.cros_au_42"));
loop_.Run();
// Emulate p2p-client returning invalid URL.
test_conf_->SetP2PClientCommand({"echo", "not_a_valid_url"});
- manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
- base::Bind(ExpectUrl, ""));
+ manager_->LookupUrlForFile(
+ "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, ""));
loop_.Run();
// Emulate p2p-client conveying failure.
test_conf_->SetP2PClientCommand({"false"});
- manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
- base::Bind(ExpectUrl, ""));
+ manager_->LookupUrlForFile(
+ "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, ""));
loop_.Run();
// Emulate p2p-client not existing.
test_conf_->SetP2PClientCommand({"/path/to/non/existent/helper/program"});
- manager_->LookupUrlForFile("foobar", 42,
- TimeDelta(),
- base::Bind(ExpectUrl, ""));
+ manager_->LookupUrlForFile(
+ "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, ""));
loop_.Run();
// Emulate p2p-client crashing.
test_conf_->SetP2PClientCommand({"sh", "-c", "kill -SEGV $$"});
- manager_->LookupUrlForFile("foobar", 42, TimeDelta(),
- base::Bind(ExpectUrl, ""));
+ manager_->LookupUrlForFile(
+ "foobar", 42, TimeDelta(), base::Bind(ExpectUrl, ""));
loop_.Run();
// Emulate p2p-client exceeding its timeout.
- test_conf_->SetP2PClientCommand({
- "sh", "-c",
- // The 'sleep' launched below could be left behind as an orphaned
- // process when the 'sh' process is terminated by SIGTERM. As a
- // remedy, trap SIGTERM and kill the 'sleep' process, which requires
- // launching 'sleep' in background and then waiting for it.
- "cleanup() { kill \"${sleep_pid}\"; exit 0; }; "
- "trap cleanup TERM; "
- "sleep 5 & "
- "sleep_pid=$!; "
- "echo http://1.2.3.4/; "
- "wait"
- });
- manager_->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500),
+ test_conf_->SetP2PClientCommand(
+ {"sh",
+ "-c",
+ // The 'sleep' launched below could be left behind as an orphaned
+ // process when the 'sh' process is terminated by SIGTERM. As a
+ // remedy, trap SIGTERM and kill the 'sleep' process, which requires
+ // launching 'sleep' in background and then waiting for it.
+ "cleanup() { kill \"${sleep_pid}\"; exit 0; }; "
+ "trap cleanup TERM; "
+ "sleep 5 & "
+ "sleep_pid=$!; "
+ "echo http://1.2.3.4/; "
+ "wait"});
+ manager_->LookupUrlForFile("foobar",
+ 42,
+ TimeDelta::FromMilliseconds(500),
base::Bind(ExpectUrl, ""));
loop_.Run();
}