David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 1 | // 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 Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 7 | #include <dirent.h> |
Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 8 | #include <fcntl.h> |
| 9 | #include <sys/stat.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 10 | #include <unistd.h> |
Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 11 | #include <attr/xattr.h> // NOLINT - requires typed defined in unistd.h |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 12 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 15 | #include "gmock/gmock.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | #include "base/bind.h" |
| 19 | #include "base/callback.h" |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 20 | #include <base/strings/stringprintf.h> |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 21 | #include <policy/libpolicy.h> |
| 22 | #include <policy/mock_device_policy.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 23 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 24 | #include "update_engine/fake_p2p_manager_configuration.h" |
Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 25 | #include "update_engine/p2p_manager.h" |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 26 | #include "update_engine/prefs.h" |
| 27 | #include "update_engine/test_utils.h" |
| 28 | #include "update_engine/utils.h" |
| 29 | |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame^] | 30 | using base::TimeDelta; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 31 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 32 | using std::unique_ptr; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 33 | |
| 34 | namespace 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. |
| 39 | class P2PManagerTest : public testing::Test { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 40 | protected: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 41 | 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 Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 55 | // Check that IsP2PEnabled() returns false if neither the crosh flag |
| 56 | // nor the Enterprise Policy indicates p2p is to be used. |
| 57 | TEST_F(P2PManagerTest, P2PEnabledNeitherCroshFlagNotEnterpriseSetting) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 58 | string temp_dir; |
| 59 | Prefs prefs; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 60 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 61 | &temp_dir)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 62 | prefs.Init(base::FilePath(temp_dir)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 63 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 64 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 65 | &prefs, "cros_au", 3)); |
| 66 | EXPECT_FALSE(manager->IsP2PEnabled()); |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 67 | |
| 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. |
| 73 | TEST_F(P2PManagerTest, P2PEnabledCroshFlag) { |
| 74 | string temp_dir; |
| 75 | Prefs prefs; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 76 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 77 | &temp_dir)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 78 | prefs.Init(base::FilePath(temp_dir)); |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 79 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 80 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 81 | &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. |
| 93 | TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingTrue) { |
| 94 | string temp_dir; |
| 95 | Prefs prefs; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 96 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 97 | &temp_dir)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 98 | prefs.Init(base::FilePath(temp_dir)); |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 99 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 100 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 101 | &prefs, "cros_au", 3)); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 102 | unique_ptr<policy::MockDevicePolicy> device_policy( |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 103 | new policy::MockDevicePolicy()); |
| 104 | EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly( |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 105 | DoAll(testing::SetArgumentPointee<0>(true), |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 106 | 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 Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 117 | // Check that IsP2PEnabled() corresponds to the value of the crosh |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 118 | // flag if Enterprise Policy indicates that p2p is not to be used. |
| 119 | TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingFalse) { |
| 120 | string temp_dir; |
| 121 | Prefs prefs; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 122 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 123 | &temp_dir)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 124 | prefs.Init(base::FilePath(temp_dir)); |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 125 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 126 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 127 | &prefs, "cros_au", 3)); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 128 | unique_ptr<policy::MockDevicePolicy> device_policy( |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 129 | new policy::MockDevicePolicy()); |
| 130 | EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly( |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 131 | DoAll(testing::SetArgumentPointee<0>(false), |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 132 | testing::Return(true))); |
| 133 | manager->SetDevicePolicy(device_policy.get()); |
| 134 | EXPECT_FALSE(manager->IsP2PEnabled()); |
| 135 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 136 | 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 Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 144 | // Check that IsP2PEnabled() returns TRUE if |
| 145 | // - The crosh flag is not set. |
| 146 | // - Enterprise Policy is not set. |
| 147 | // - Device is Enterprise Enrolled. |
| 148 | TEST_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 Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 155 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 156 | &prefs, "cros_au", 3)); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 157 | unique_ptr<policy::MockDevicePolicy> device_policy( |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 158 | new policy::MockDevicePolicy()); |
| 159 | // We return an empty owner as this is an enterprise. |
| 160 | EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly( |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame^] | 161 | DoAll(testing::SetArgumentPointee<0>(string("")), |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 162 | 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. |
| 173 | TEST_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 Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 180 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 181 | &prefs, "cros_au", 3)); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 182 | unique_ptr<policy::MockDevicePolicy> device_policy( |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 183 | new policy::MockDevicePolicy()); |
| 184 | // We return an empty owner as this is an enterprise. |
| 185 | EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly( |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame^] | 186 | DoAll(testing::SetArgumentPointee<0>(string("")), |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 187 | 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 Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 198 | // Check that we keep the $N newest files with the .$EXT.p2p extension. |
David Zeuthen | 45e2ae2 | 2013-09-03 11:46:11 -0700 | [diff] [blame] | 199 | TEST_F(P2PManagerTest, Housekeeping) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 200 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 201 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 202 | EXPECT_EQ(manager->CountSharedFiles(), 0); |
| 203 | |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 204 | // Generate files with different timestamps matching our pattern and generate |
| 205 | // other files not matching the pattern. |
| 206 | double last_timestamp = -1; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 207 | for (int n = 0; n < 5; n++) { |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 208 | double current_timestamp; |
| 209 | do { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 210 | base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf( |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 211 | "file_%d.cros_au.p2p", n)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 212 | EXPECT_EQ(0, System(base::StringPrintf("touch %s", |
| 213 | file.value().c_str()))); |
David Zeuthen | 45e2ae2 | 2013-09-03 11:46:11 -0700 | [diff] [blame] | 214 | |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 215 | // 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 Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 230 | EXPECT_EQ(0, System(base::StringPrintf( |
| 231 | "touch %s/file_%d.OTHER.p2p", |
| 232 | test_conf_->GetP2PDir().value().c_str(), n))); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 233 | |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 234 | // 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 Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 237 | } |
| 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 Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 250 | file_name = base::StringPrintf( |
| 251 | "%s/file_%d.cros_au.p2p", |
| 252 | test_conf_->GetP2PDir().value().c_str(), n); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 253 | EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect); |
| 254 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 255 | file_name = base::StringPrintf( |
| 256 | "%s/file_%d.OTHER.p2p", |
| 257 | test_conf_->GetP2PDir().value().c_str(), n); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 258 | 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 | |
| 264 | static 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 Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 267 | char ea_value[64] = { 0 }; |
| 268 | ssize_t ea_size; |
| 269 | |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 270 | off_t p2p_size = utils::FileSize(path); |
| 271 | if (p2p_size < 0) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 272 | LOG(ERROR) << "File " << path << " does not exist"; |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | if (expected_size != 0) { |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 277 | if (p2p_size != expected_size) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 278 | LOG(ERROR) << "Expected size " << expected_size |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 279 | << " but size was " << p2p_size; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 280 | 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 Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 301 | char* endp = nullptr; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 302 | long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 303 | if (endp == nullptr || *endp != '\0') { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 304 | 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 | |
| 318 | static 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 Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 334 | string decimal_size = base::StringPrintf("%zu", size_xattr); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 335 | 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. |
| 348 | TEST_F(P2PManagerTest, ShareFile) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 349 | if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 350 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 351 | << "Please update your system to support this feature."; |
| 352 | return; |
| 353 | } |
| 354 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 355 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 356 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 357 | 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. |
| 371 | TEST_F(P2PManagerTest, MakeFileVisible) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 372 | if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 373 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 374 | << "Please update your system to support this feature."; |
| 375 | return; |
| 376 | } |
| 377 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 378 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 379 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 380 | // 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. |
| 398 | TEST_F(P2PManagerTest, ExistingFiles) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 399 | if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 400 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 401 | << "Please update your system to support this feature."; |
| 402 | return; |
| 403 | } |
| 404 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 405 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 406 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 407 | bool visible; |
| 408 | |
| 409 | // Check that errors are returned if the file does not exist |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 410 | EXPECT_EQ(manager->FileGetPath("foo"), base::FilePath()); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 411 | EXPECT_EQ(manager->FileGetSize("foo"), -1); |
| 412 | EXPECT_EQ(manager->FileGetExpectedSize("foo"), -1); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 413 | EXPECT_FALSE(manager->FileGetVisible("foo", nullptr)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 414 | // ... 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 Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 426 | EXPECT_EQ(manager->FileGetPath("bar"), base::FilePath()); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 427 | EXPECT_EQ(manager->FileGetSize("bar"), -1); |
| 428 | EXPECT_EQ(manager->FileGetExpectedSize("bar"), -1); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 429 | EXPECT_FALSE(manager->FileGetVisible("bar", nullptr)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 430 | // ... 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 Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 442 | // 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. |
| 445 | TEST_F(P2PManagerTest, StartP2P) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 446 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 447 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 448 | |
| 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 |
| 463 | TEST_F(P2PManagerTest, StopP2P) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 464 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 465 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 466 | |
| 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 | |
| 480 | static 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. |
| 489 | TEST_F(P2PManagerTest, LookupURL) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 490 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 491 | nullptr, "cros_au", 3)); |
| 492 | GMainLoop *loop = g_main_loop_new(nullptr, FALSE); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 493 | |
| 494 | // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au" |
| 495 | // being propagated in the right places. |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 496 | test_conf_->SetP2PClientCommandLine( |
| 497 | "echo 'http://1.2.3.4/{file_id}_{minsize}'"); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 498 | 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 Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 537 | } // namespace chromeos_update_engine |