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 | |
| 30 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 31 | using std::unique_ptr; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 32 | using std::vector; |
| 33 | using base::TimeDelta; |
| 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
| 37 | // Test fixture that sets up a testing configuration (with e.g. a |
| 38 | // temporary p2p dir) for P2PManager and cleans up when the test is |
| 39 | // done. |
| 40 | class P2PManagerTest : public testing::Test { |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 41 | protected: |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 42 | P2PManagerTest() {} |
| 43 | virtual ~P2PManagerTest() {} |
| 44 | |
| 45 | // Derived from testing::Test. |
| 46 | virtual void SetUp() { |
| 47 | test_conf_ = new FakeP2PManagerConfiguration(); |
| 48 | } |
| 49 | virtual void TearDown() {} |
| 50 | |
| 51 | // The P2PManager::Configuration instance used for testing. |
| 52 | FakeP2PManagerConfiguration *test_conf_; |
| 53 | }; |
| 54 | |
| 55 | |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 56 | // Check that IsP2PEnabled() returns false if neither the crosh flag |
| 57 | // nor the Enterprise Policy indicates p2p is to be used. |
| 58 | TEST_F(P2PManagerTest, P2PEnabledNeitherCroshFlagNotEnterpriseSetting) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 59 | string temp_dir; |
| 60 | Prefs prefs; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 61 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 62 | &temp_dir)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 63 | prefs.Init(base::FilePath(temp_dir)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 64 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 65 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 66 | &prefs, "cros_au", 3)); |
| 67 | EXPECT_FALSE(manager->IsP2PEnabled()); |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 68 | |
| 69 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 70 | } |
| 71 | |
| 72 | // Check that IsP2PEnabled() corresponds to value of the crosh flag |
| 73 | // when Enterprise Policy is not set. |
| 74 | TEST_F(P2PManagerTest, P2PEnabledCroshFlag) { |
| 75 | string temp_dir; |
| 76 | Prefs prefs; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 77 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 78 | &temp_dir)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 79 | prefs.Init(base::FilePath(temp_dir)); |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 80 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 81 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 82 | &prefs, "cros_au", 3)); |
| 83 | EXPECT_FALSE(manager->IsP2PEnabled()); |
| 84 | prefs.SetBoolean(kPrefsP2PEnabled, true); |
| 85 | EXPECT_TRUE(manager->IsP2PEnabled()); |
| 86 | prefs.SetBoolean(kPrefsP2PEnabled, false); |
| 87 | EXPECT_FALSE(manager->IsP2PEnabled()); |
| 88 | |
| 89 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 90 | } |
| 91 | |
| 92 | // Check that IsP2PEnabled() always returns true if Enterprise Policy |
| 93 | // indicates that p2p is to be used. |
| 94 | TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingTrue) { |
| 95 | string temp_dir; |
| 96 | Prefs prefs; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 97 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 98 | &temp_dir)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 99 | prefs.Init(base::FilePath(temp_dir)); |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 100 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 101 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 102 | &prefs, "cros_au", 3)); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 103 | unique_ptr<policy::MockDevicePolicy> device_policy( |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 104 | new policy::MockDevicePolicy()); |
| 105 | EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly( |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 106 | DoAll(testing::SetArgumentPointee<0>(true), |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 107 | testing::Return(true))); |
| 108 | manager->SetDevicePolicy(device_policy.get()); |
| 109 | EXPECT_TRUE(manager->IsP2PEnabled()); |
| 110 | |
| 111 | // Should still return true even if crosh flag says otherwise. |
| 112 | prefs.SetBoolean(kPrefsP2PEnabled, false); |
| 113 | EXPECT_TRUE(manager->IsP2PEnabled()); |
| 114 | |
| 115 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 116 | } |
| 117 | |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 118 | // Check that IsP2PEnabled() corresponds to the value of the crosh |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 119 | // flag if Enterprise Policy indicates that p2p is not to be used. |
| 120 | TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingFalse) { |
| 121 | string temp_dir; |
| 122 | Prefs prefs; |
Gilad Arnold | a6742b3 | 2014-01-11 00:18:34 -0800 | [diff] [blame] | 123 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 124 | &temp_dir)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 125 | prefs.Init(base::FilePath(temp_dir)); |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 126 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 127 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 128 | &prefs, "cros_au", 3)); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 129 | unique_ptr<policy::MockDevicePolicy> device_policy( |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 130 | new policy::MockDevicePolicy()); |
| 131 | EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly( |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 132 | DoAll(testing::SetArgumentPointee<0>(false), |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame] | 133 | testing::Return(true))); |
| 134 | manager->SetDevicePolicy(device_policy.get()); |
| 135 | EXPECT_FALSE(manager->IsP2PEnabled()); |
| 136 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 137 | prefs.SetBoolean(kPrefsP2PEnabled, true); |
| 138 | EXPECT_TRUE(manager->IsP2PEnabled()); |
| 139 | prefs.SetBoolean(kPrefsP2PEnabled, false); |
| 140 | EXPECT_FALSE(manager->IsP2PEnabled()); |
| 141 | |
| 142 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 143 | } |
| 144 | |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 145 | // Check that IsP2PEnabled() returns TRUE if |
| 146 | // - The crosh flag is not set. |
| 147 | // - Enterprise Policy is not set. |
| 148 | // - Device is Enterprise Enrolled. |
| 149 | TEST_F(P2PManagerTest, P2PEnabledEnterpriseEnrolledDevicesDefaultToEnabled) { |
| 150 | string temp_dir; |
| 151 | Prefs prefs; |
| 152 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
| 153 | &temp_dir)); |
| 154 | prefs.Init(base::FilePath(temp_dir)); |
| 155 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 156 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 157 | &prefs, "cros_au", 3)); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 158 | unique_ptr<policy::MockDevicePolicy> device_policy( |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 159 | new policy::MockDevicePolicy()); |
| 160 | // We return an empty owner as this is an enterprise. |
| 161 | EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly( |
| 162 | DoAll(testing::SetArgumentPointee<0>(std::string("")), |
| 163 | testing::Return(true))); |
| 164 | manager->SetDevicePolicy(device_policy.get()); |
| 165 | EXPECT_TRUE(manager->IsP2PEnabled()); |
| 166 | |
| 167 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 168 | } |
| 169 | |
| 170 | // Check that IsP2PEnabled() returns FALSE if |
| 171 | // - The crosh flag is not set. |
| 172 | // - Enterprise Policy is set to FALSE. |
| 173 | // - Device is Enterprise Enrolled. |
| 174 | TEST_F(P2PManagerTest, P2PEnabledEnterpriseEnrolledDevicesOverrideDefault) { |
| 175 | string temp_dir; |
| 176 | Prefs prefs; |
| 177 | EXPECT_TRUE(utils::MakeTempDirectory("PayloadStateP2PTests.XXXXXX", |
| 178 | &temp_dir)); |
| 179 | prefs.Init(base::FilePath(temp_dir)); |
| 180 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 181 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 182 | &prefs, "cros_au", 3)); |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 183 | unique_ptr<policy::MockDevicePolicy> device_policy( |
David Zeuthen | 9a58e6a | 2014-09-22 17:38:44 -0400 | [diff] [blame] | 184 | new policy::MockDevicePolicy()); |
| 185 | // We return an empty owner as this is an enterprise. |
| 186 | EXPECT_CALL(*device_policy, GetOwner(testing::_)).WillRepeatedly( |
| 187 | DoAll(testing::SetArgumentPointee<0>(std::string("")), |
| 188 | testing::Return(true))); |
| 189 | // Make Enterprise Policy indicate p2p is not enabled. |
| 190 | EXPECT_CALL(*device_policy, GetAuP2PEnabled(testing::_)).WillRepeatedly( |
| 191 | DoAll(testing::SetArgumentPointee<0>(false), |
| 192 | testing::Return(true))); |
| 193 | manager->SetDevicePolicy(device_policy.get()); |
| 194 | EXPECT_FALSE(manager->IsP2PEnabled()); |
| 195 | |
| 196 | EXPECT_TRUE(utils::RecursiveUnlinkDir(temp_dir)); |
| 197 | } |
| 198 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 199 | // 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] | 200 | TEST_F(P2PManagerTest, Housekeeping) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 201 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 202 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 203 | EXPECT_EQ(manager->CountSharedFiles(), 0); |
| 204 | |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 205 | // Generate files with different timestamps matching our pattern and generate |
| 206 | // other files not matching the pattern. |
| 207 | double last_timestamp = -1; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 208 | for (int n = 0; n < 5; n++) { |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 209 | double current_timestamp; |
| 210 | do { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 211 | base::FilePath file = test_conf_->GetP2PDir().Append(base::StringPrintf( |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 212 | "file_%d.cros_au.p2p", n)); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 213 | EXPECT_EQ(0, System(base::StringPrintf("touch %s", |
| 214 | file.value().c_str()))); |
David Zeuthen | 45e2ae2 | 2013-09-03 11:46:11 -0700 | [diff] [blame] | 215 | |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 216 | // Check that the current timestamp on the file is different from the |
| 217 | // previous generated file. This timestamp depends on the file system |
| 218 | // time resolution, for example, ext2/ext3 have a time resolution of one |
| 219 | // second while ext4 has a resolution of one nanosecond. If the assigned |
| 220 | // timestamp is the same, we introduce a bigger sleep and call touch |
| 221 | // again. |
| 222 | struct stat statbuf; |
| 223 | EXPECT_EQ(stat(file.value().c_str(), &statbuf), 0); |
| 224 | current_timestamp = utils::TimeFromStructTimespec(&statbuf.st_ctim) |
| 225 | .ToDoubleT(); |
| 226 | if (current_timestamp == last_timestamp) |
| 227 | sleep(1); |
| 228 | } while (current_timestamp == last_timestamp); |
| 229 | last_timestamp = current_timestamp; |
| 230 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 231 | EXPECT_EQ(0, System(base::StringPrintf( |
| 232 | "touch %s/file_%d.OTHER.p2p", |
| 233 | test_conf_->GetP2PDir().value().c_str(), n))); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 234 | |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 235 | // A sleep of one micro-second is enough to have a different "Change" time |
| 236 | // on the file on newer file systems. |
| 237 | g_usleep(1); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 238 | } |
| 239 | // CountSharedFiles() only counts 'cros_au' files. |
| 240 | EXPECT_EQ(manager->CountSharedFiles(), 5); |
| 241 | |
| 242 | EXPECT_TRUE(manager->PerformHousekeeping()); |
| 243 | |
| 244 | // At this point - after HouseKeeping - we should only have |
| 245 | // eight files left. |
| 246 | for (int n = 0; n < 5; n++) { |
| 247 | string file_name; |
| 248 | bool expect; |
| 249 | |
| 250 | expect = (n >= 2); |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 251 | file_name = base::StringPrintf( |
| 252 | "%s/file_%d.cros_au.p2p", |
| 253 | test_conf_->GetP2PDir().value().c_str(), n); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 254 | EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect); |
| 255 | |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 256 | file_name = base::StringPrintf( |
| 257 | "%s/file_%d.OTHER.p2p", |
| 258 | test_conf_->GetP2PDir().value().c_str(), n); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 259 | EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS)); |
| 260 | } |
| 261 | // CountSharedFiles() only counts 'cros_au' files. |
| 262 | EXPECT_EQ(manager->CountSharedFiles(), 3); |
| 263 | } |
| 264 | |
| 265 | static bool CheckP2PFile(const string& p2p_dir, const string& file_name, |
| 266 | ssize_t expected_size, ssize_t expected_size_xattr) { |
| 267 | string path = p2p_dir + "/" + file_name; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 268 | char ea_value[64] = { 0 }; |
| 269 | ssize_t ea_size; |
| 270 | |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 271 | off_t p2p_size = utils::FileSize(path); |
| 272 | if (p2p_size < 0) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 273 | LOG(ERROR) << "File " << path << " does not exist"; |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | if (expected_size != 0) { |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 278 | if (p2p_size != expected_size) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 279 | LOG(ERROR) << "Expected size " << expected_size |
Gabe Black | a77939e | 2014-09-09 23:35:08 -0700 | [diff] [blame] | 280 | << " but size was " << p2p_size; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if (expected_size_xattr == 0) { |
| 286 | ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize", |
| 287 | &ea_value, sizeof ea_value - 1); |
| 288 | if (ea_size == -1 && errno == ENOATTR) { |
| 289 | // This is valid behavior as we support files without the xattr set. |
| 290 | } else { |
| 291 | PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, " |
| 292 | << "ea_size=" << ea_size << ", errno=" << errno; |
| 293 | return false; |
| 294 | } |
| 295 | } else { |
| 296 | ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize", |
| 297 | &ea_value, sizeof ea_value - 1); |
| 298 | if (ea_size < 0) { |
| 299 | LOG(ERROR) << "Error getting xattr attribute"; |
| 300 | return false; |
| 301 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 302 | char* endp = nullptr; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 303 | long long int val = strtoll(ea_value, &endp, 0); // NOLINT(runtime/int) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 304 | if (endp == nullptr || *endp != '\0') { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 305 | LOG(ERROR) << "Error parsing xattr '" << ea_value |
| 306 | << "' as an integer"; |
| 307 | return false; |
| 308 | } |
| 309 | if (val != expected_size_xattr) { |
| 310 | LOG(ERROR) << "Expected xattr size " << expected_size_xattr |
| 311 | << " but size was " << val; |
| 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | static bool CreateP2PFile(string p2p_dir, string file_name, |
| 320 | size_t size, size_t size_xattr) { |
| 321 | string path = p2p_dir + "/" + file_name; |
| 322 | |
| 323 | int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644); |
| 324 | if (fd == -1) { |
| 325 | PLOG(ERROR) << "Error creating file with path " << path; |
| 326 | return false; |
| 327 | } |
| 328 | if (ftruncate(fd, size) != 0) { |
| 329 | PLOG(ERROR) << "Error truncating " << path << " to size " << size; |
| 330 | close(fd); |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | if (size_xattr != 0) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 335 | string decimal_size = base::StringPrintf("%zu", size_xattr); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 336 | if (fsetxattr(fd, "user.cros-p2p-filesize", |
| 337 | decimal_size.c_str(), decimal_size.size(), 0) != 0) { |
| 338 | PLOG(ERROR) << "Error setting xattr on " << path; |
| 339 | close(fd); |
| 340 | return false; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | close(fd); |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | // Check that sharing a *new* file works. |
| 349 | TEST_F(P2PManagerTest, ShareFile) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 350 | if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 351 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 352 | << "Please update your system to support this feature."; |
| 353 | return; |
| 354 | } |
| 355 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 356 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 357 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 358 | EXPECT_TRUE(manager->FileShare("foo", 10 * 1000 * 1000)); |
| 359 | EXPECT_EQ(manager->FileGetPath("foo"), |
| 360 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp")); |
| 361 | EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(), |
| 362 | "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000)); |
| 363 | |
| 364 | // Sharing it again - with the same expected size - should return true |
| 365 | EXPECT_TRUE(manager->FileShare("foo", 10 * 1000 * 1000)); |
| 366 | |
| 367 | // ... but if we use the wrong size, it should fail |
| 368 | EXPECT_FALSE(manager->FileShare("foo", 10 * 1000 * 1000 + 1)); |
| 369 | } |
| 370 | |
| 371 | // Check that making a shared file visible, does what is expected. |
| 372 | TEST_F(P2PManagerTest, MakeFileVisible) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 373 | if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 374 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 375 | << "Please update your system to support this feature."; |
| 376 | return; |
| 377 | } |
| 378 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 379 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 380 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 381 | // First, check that it's not visible. |
| 382 | manager->FileShare("foo", 10*1000*1000); |
| 383 | EXPECT_EQ(manager->FileGetPath("foo"), |
| 384 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp")); |
| 385 | EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(), |
| 386 | "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000)); |
| 387 | // Make the file visible and check that it changed its name. Do it |
| 388 | // twice to check that FileMakeVisible() is idempotent. |
| 389 | for (int n = 0; n < 2; n++) { |
| 390 | manager->FileMakeVisible("foo"); |
| 391 | EXPECT_EQ(manager->FileGetPath("foo"), |
| 392 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p")); |
| 393 | EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(), |
| 394 | "foo.cros_au.p2p", 0, 10 * 1000 * 1000)); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // Check that we return the right values for existing files in P2P_DIR. |
| 399 | TEST_F(P2PManagerTest, ExistingFiles) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 400 | if (!utils::IsXAttrSupported(base::FilePath("/tmp"))) { |
David Zeuthen | 910ec5b | 2013-09-26 12:10:58 -0700 | [diff] [blame] | 401 | LOG(WARNING) << "Skipping test because /tmp does not support xattr. " |
| 402 | << "Please update your system to support this feature."; |
| 403 | return; |
| 404 | } |
| 405 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 406 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 407 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 408 | bool visible; |
| 409 | |
| 410 | // Check that errors are returned if the file does not exist |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 411 | EXPECT_EQ(manager->FileGetPath("foo"), base::FilePath()); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 412 | EXPECT_EQ(manager->FileGetSize("foo"), -1); |
| 413 | EXPECT_EQ(manager->FileGetExpectedSize("foo"), -1); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 414 | EXPECT_FALSE(manager->FileGetVisible("foo", nullptr)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 415 | // ... then create the file ... |
| 416 | EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(), |
| 417 | "foo.cros_au.p2p", 42, 43)); |
| 418 | // ... and then check that the expected values are returned |
| 419 | EXPECT_EQ(manager->FileGetPath("foo"), |
| 420 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p")); |
| 421 | EXPECT_EQ(manager->FileGetSize("foo"), 42); |
| 422 | EXPECT_EQ(manager->FileGetExpectedSize("foo"), 43); |
| 423 | EXPECT_TRUE(manager->FileGetVisible("foo", &visible)); |
| 424 | EXPECT_TRUE(visible); |
| 425 | |
| 426 | // 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] | 427 | EXPECT_EQ(manager->FileGetPath("bar"), base::FilePath()); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 428 | EXPECT_EQ(manager->FileGetSize("bar"), -1); |
| 429 | EXPECT_EQ(manager->FileGetExpectedSize("bar"), -1); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 430 | EXPECT_FALSE(manager->FileGetVisible("bar", nullptr)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 431 | // ... then create the file ... |
| 432 | EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(), |
| 433 | "bar.cros_au.p2p.tmp", 44, 45)); |
| 434 | // ... and then check that the expected values are returned |
| 435 | EXPECT_EQ(manager->FileGetPath("bar"), |
| 436 | test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp")); |
| 437 | EXPECT_EQ(manager->FileGetSize("bar"), 44); |
| 438 | EXPECT_EQ(manager->FileGetExpectedSize("bar"), 45); |
| 439 | EXPECT_TRUE(manager->FileGetVisible("bar", &visible)); |
| 440 | EXPECT_FALSE(visible); |
| 441 | } |
| 442 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 443 | // This is a little bit ugly but short of mocking a 'p2p' service this |
| 444 | // will have to do. E.g. we essentially simulate the various |
| 445 | // behaviours of initctl(8) that we rely on. |
| 446 | TEST_F(P2PManagerTest, StartP2P) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 447 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 448 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 449 | |
| 450 | // Check that we can start the service |
| 451 | test_conf_->SetInitctlStartCommandLine("true"); |
| 452 | EXPECT_TRUE(manager->EnsureP2PRunning()); |
| 453 | test_conf_->SetInitctlStartCommandLine("false"); |
| 454 | EXPECT_FALSE(manager->EnsureP2PRunning()); |
| 455 | test_conf_->SetInitctlStartCommandLine( |
| 456 | "sh -c 'echo \"initctl: Job is already running: p2p\" >&2; false'"); |
| 457 | EXPECT_TRUE(manager->EnsureP2PRunning()); |
| 458 | test_conf_->SetInitctlStartCommandLine( |
| 459 | "sh -c 'echo something else >&2; false'"); |
| 460 | EXPECT_FALSE(manager->EnsureP2PRunning()); |
| 461 | } |
| 462 | |
| 463 | // Same comment as for StartP2P |
| 464 | TEST_F(P2PManagerTest, StopP2P) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 465 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 466 | nullptr, "cros_au", 3)); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 467 | |
| 468 | // Check that we can start the service |
| 469 | test_conf_->SetInitctlStopCommandLine("true"); |
| 470 | EXPECT_TRUE(manager->EnsureP2PNotRunning()); |
| 471 | test_conf_->SetInitctlStopCommandLine("false"); |
| 472 | EXPECT_FALSE(manager->EnsureP2PNotRunning()); |
| 473 | test_conf_->SetInitctlStopCommandLine( |
| 474 | "sh -c 'echo \"initctl: Unknown instance \" >&2; false'"); |
| 475 | EXPECT_TRUE(manager->EnsureP2PNotRunning()); |
| 476 | test_conf_->SetInitctlStopCommandLine( |
| 477 | "sh -c 'echo something else >&2; false'"); |
| 478 | EXPECT_FALSE(manager->EnsureP2PNotRunning()); |
| 479 | } |
| 480 | |
| 481 | static void ExpectUrl(const string& expected_url, |
| 482 | GMainLoop* loop, |
| 483 | const string& url) { |
| 484 | EXPECT_EQ(url, expected_url); |
| 485 | g_main_loop_quit(loop); |
| 486 | } |
| 487 | |
| 488 | // Like StartP2P, we're mocking the different results that p2p-client |
| 489 | // can return. It's not pretty but it works. |
| 490 | TEST_F(P2PManagerTest, LookupURL) { |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame^] | 491 | unique_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 492 | nullptr, "cros_au", 3)); |
| 493 | GMainLoop *loop = g_main_loop_new(nullptr, FALSE); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 494 | |
| 495 | // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au" |
| 496 | // being propagated in the right places. |
Alex Deymo | 8ad6da9 | 2014-07-15 17:17:45 -0700 | [diff] [blame] | 497 | test_conf_->SetP2PClientCommandLine( |
| 498 | "echo 'http://1.2.3.4/{file_id}_{minsize}'"); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 499 | manager->LookupUrlForFile("fooX", 42, TimeDelta(), |
| 500 | base::Bind(ExpectUrl, |
| 501 | "http://1.2.3.4/fooX.cros_au_42", loop)); |
| 502 | g_main_loop_run(loop); |
| 503 | |
| 504 | // Emulate p2p-client returning invalid URL. |
| 505 | test_conf_->SetP2PClientCommandLine("echo 'not_a_valid_url'"); |
| 506 | manager->LookupUrlForFile("foobar", 42, TimeDelta(), |
| 507 | base::Bind(ExpectUrl, "", loop)); |
| 508 | g_main_loop_run(loop); |
| 509 | |
| 510 | // Emulate p2p-client conveying failure. |
| 511 | test_conf_->SetP2PClientCommandLine("false"); |
| 512 | manager->LookupUrlForFile("foobar", 42, TimeDelta(), |
| 513 | base::Bind(ExpectUrl, "", loop)); |
| 514 | g_main_loop_run(loop); |
| 515 | |
| 516 | // Emulate p2p-client not existing. |
| 517 | test_conf_->SetP2PClientCommandLine("/path/to/non/existent/helper/program"); |
| 518 | manager->LookupUrlForFile("foobar", 42, |
| 519 | TimeDelta(), |
| 520 | base::Bind(ExpectUrl, "", loop)); |
| 521 | g_main_loop_run(loop); |
| 522 | |
| 523 | // Emulate p2p-client crashing. |
| 524 | test_conf_->SetP2PClientCommandLine("sh -c 'kill -SEGV $$'"); |
| 525 | manager->LookupUrlForFile("foobar", 42, TimeDelta(), |
| 526 | base::Bind(ExpectUrl, "", loop)); |
| 527 | g_main_loop_run(loop); |
| 528 | |
| 529 | // Emulate p2p-client exceeding its timeout. |
| 530 | test_conf_->SetP2PClientCommandLine("sh -c 'echo http://1.2.3.4/; sleep 2'"); |
| 531 | manager->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500), |
| 532 | base::Bind(ExpectUrl, "", loop)); |
| 533 | g_main_loop_run(loop); |
| 534 | |
| 535 | g_main_loop_unref(loop); |
| 536 | } |
| 537 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 538 | } // namespace chromeos_update_engine |