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 | |
| 7 | #include <sys/stat.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <dirent.h> |
| 10 | #include <unistd.h> |
| 11 | #include <attr/xattr.h> |
| 12 | |
| 13 | #include "gmock/gmock.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | #include "base/bind.h" |
| 17 | #include "base/callback.h" |
| 18 | #include "base/stringprintf.h" |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame^] | 19 | #include <policy/libpolicy.h> |
| 20 | #include <policy/mock_device_policy.h> |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 21 | |
| 22 | #include "update_engine/p2p_manager.h" |
| 23 | #include "update_engine/fake_p2p_manager_configuration.h" |
| 24 | #include "update_engine/prefs.h" |
| 25 | #include "update_engine/test_utils.h" |
| 26 | #include "update_engine/utils.h" |
| 27 | |
| 28 | using std::string; |
| 29 | using std::vector; |
| 30 | using base::TimeDelta; |
| 31 | |
| 32 | namespace 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. |
| 37 | class P2PManagerTest : public testing::Test { |
| 38 | protected: |
| 39 | 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 Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame^] | 53 | // Check that IsP2PEnabled() returns false if neither the crosh flag |
| 54 | // nor the Enterprise Policy indicates p2p is to be used. |
| 55 | TEST_F(P2PManagerTest, P2PEnabledNeitherCroshFlagNotEnterpriseSetting) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 56 | string temp_dir; |
| 57 | Prefs prefs; |
| 58 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/PayloadStateP2PTests.XXXXXX", |
| 59 | &temp_dir)); |
| 60 | prefs.Init(FilePath(temp_dir)); |
| 61 | |
| 62 | scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
| 63 | &prefs, "cros_au", 3)); |
| 64 | EXPECT_FALSE(manager->IsP2PEnabled()); |
David Zeuthen | 92d9c8b | 2013-09-11 10:58:11 -0700 | [diff] [blame^] | 65 | |
| 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. |
| 71 | TEST_F(P2PManagerTest, P2PEnabledCroshFlag) { |
| 72 | string temp_dir; |
| 73 | Prefs prefs; |
| 74 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/PayloadStateP2PTests.XXXXXX", |
| 75 | &temp_dir)); |
| 76 | prefs.Init(FilePath(temp_dir)); |
| 77 | |
| 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. |
| 91 | TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingTrue) { |
| 92 | string temp_dir; |
| 93 | Prefs prefs; |
| 94 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/PayloadStateP2PTests.XXXXXX", |
| 95 | &temp_dir)); |
| 96 | prefs.Init(FilePath(temp_dir)); |
| 97 | |
| 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( |
| 103 | DoAll(testing::SetArgumentPointee<0>(bool(true)), |
| 104 | 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 | |
| 115 | // Check that IsP2PEnabled() corrresponds to the value of the crosh |
| 116 | // flag if Enterprise Policy indicates that p2p is not to be used. |
| 117 | TEST_F(P2PManagerTest, P2PEnabledEnterpriseSettingFalse) { |
| 118 | string temp_dir; |
| 119 | Prefs prefs; |
| 120 | EXPECT_TRUE(utils::MakeTempDirectory("/tmp/PayloadStateP2PTests.XXXXXX", |
| 121 | &temp_dir)); |
| 122 | prefs.Init(FilePath(temp_dir)); |
| 123 | |
| 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( |
| 129 | DoAll(testing::SetArgumentPointee<0>(bool(false)), |
| 130 | testing::Return(true))); |
| 131 | manager->SetDevicePolicy(device_policy.get()); |
| 132 | EXPECT_FALSE(manager->IsP2PEnabled()); |
| 133 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 134 | 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 | |
| 142 | // 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] | 143 | TEST_F(P2PManagerTest, Housekeeping) { |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 144 | scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
| 145 | NULL, "cros_au", 3)); |
| 146 | EXPECT_EQ(manager->CountSharedFiles(), 0); |
| 147 | |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 148 | // Generate files with different timestamps matching our pattern and generate |
| 149 | // other files not matching the pattern. |
| 150 | double last_timestamp = -1; |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 151 | for (int n = 0; n < 5; n++) { |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 152 | double current_timestamp; |
| 153 | do { |
| 154 | FilePath file = test_conf_->GetP2PDir().Append(StringPrintf( |
| 155 | "file_%d.cros_au.p2p", n)); |
| 156 | EXPECT_EQ(0, System(StringPrintf("touch %s", file.value().c_str()))); |
David Zeuthen | 45e2ae2 | 2013-09-03 11:46:11 -0700 | [diff] [blame] | 157 | |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 158 | // Check that the current timestamp on the file is different from the |
| 159 | // previous generated file. This timestamp depends on the file system |
| 160 | // time resolution, for example, ext2/ext3 have a time resolution of one |
| 161 | // second while ext4 has a resolution of one nanosecond. If the assigned |
| 162 | // timestamp is the same, we introduce a bigger sleep and call touch |
| 163 | // again. |
| 164 | struct stat statbuf; |
| 165 | EXPECT_EQ(stat(file.value().c_str(), &statbuf), 0); |
| 166 | current_timestamp = utils::TimeFromStructTimespec(&statbuf.st_ctim) |
| 167 | .ToDoubleT(); |
| 168 | if (current_timestamp == last_timestamp) |
| 169 | sleep(1); |
| 170 | } while (current_timestamp == last_timestamp); |
| 171 | last_timestamp = current_timestamp; |
| 172 | |
| 173 | EXPECT_EQ(0, System(StringPrintf("touch %s/file_%d.OTHER.p2p", |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 174 | test_conf_->GetP2PDir().value().c_str(), |
| 175 | n))); |
| 176 | |
Alex Deymo | 0f51351 | 2013-09-13 14:11:26 -0700 | [diff] [blame] | 177 | // A sleep of one micro-second is enough to have a different "Change" time |
| 178 | // on the file on newer file systems. |
| 179 | g_usleep(1); |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 180 | } |
| 181 | // CountSharedFiles() only counts 'cros_au' files. |
| 182 | EXPECT_EQ(manager->CountSharedFiles(), 5); |
| 183 | |
| 184 | EXPECT_TRUE(manager->PerformHousekeeping()); |
| 185 | |
| 186 | // At this point - after HouseKeeping - we should only have |
| 187 | // eight files left. |
| 188 | for (int n = 0; n < 5; n++) { |
| 189 | string file_name; |
| 190 | bool expect; |
| 191 | |
| 192 | expect = (n >= 2); |
| 193 | file_name = StringPrintf("%s/file_%d.cros_au.p2p", |
| 194 | test_conf_->GetP2PDir().value().c_str(), n); |
| 195 | EXPECT_EQ(!!g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS), expect); |
| 196 | |
| 197 | file_name = StringPrintf("%s/file_%d.OTHER.p2p", |
| 198 | test_conf_->GetP2PDir().value().c_str(), n); |
| 199 | EXPECT_TRUE(g_file_test(file_name.c_str(), G_FILE_TEST_EXISTS)); |
| 200 | } |
| 201 | // CountSharedFiles() only counts 'cros_au' files. |
| 202 | EXPECT_EQ(manager->CountSharedFiles(), 3); |
| 203 | } |
| 204 | |
David Zeuthen | ac9c186 | 2013-08-29 10:26:13 -0700 | [diff] [blame] | 205 | // TODO(zeuthen): Some builders do not support fallocate(2) or xattrs |
| 206 | // in the tmp directories where the code runs so comment out these |
| 207 | // tests for now. See http://crbug.com/281496 |
| 208 | #if 0 |
| 209 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 210 | static bool CheckP2PFile(const string& p2p_dir, const string& file_name, |
| 211 | ssize_t expected_size, ssize_t expected_size_xattr) { |
| 212 | string path = p2p_dir + "/" + file_name; |
| 213 | struct stat statbuf; |
| 214 | char ea_value[64] = { 0 }; |
| 215 | ssize_t ea_size; |
| 216 | |
| 217 | if (stat(path.c_str(), &statbuf) != 0) { |
| 218 | LOG(ERROR) << "File " << path << " does not exist"; |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | if (expected_size != 0) { |
| 223 | if (statbuf.st_size != expected_size) { |
| 224 | LOG(ERROR) << "Expected size " << expected_size |
| 225 | << " but size was " << statbuf.st_size; |
| 226 | return false; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (expected_size_xattr == 0) { |
| 231 | ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize", |
| 232 | &ea_value, sizeof ea_value - 1); |
| 233 | if (ea_size == -1 && errno == ENOATTR) { |
| 234 | // This is valid behavior as we support files without the xattr set. |
| 235 | } else { |
| 236 | PLOG(ERROR) << "getxattr() didn't fail with ENOATTR as expected, " |
| 237 | << "ea_size=" << ea_size << ", errno=" << errno; |
| 238 | return false; |
| 239 | } |
| 240 | } else { |
| 241 | ea_size = getxattr(path.c_str(), "user.cros-p2p-filesize", |
| 242 | &ea_value, sizeof ea_value - 1); |
| 243 | if (ea_size < 0) { |
| 244 | LOG(ERROR) << "Error getting xattr attribute"; |
| 245 | return false; |
| 246 | } |
| 247 | char* endp = NULL; |
| 248 | long long int val = strtoll(ea_value, &endp, 0); |
| 249 | if (endp == NULL || *endp != '\0') { |
| 250 | LOG(ERROR) << "Error parsing xattr '" << ea_value |
| 251 | << "' as an integer"; |
| 252 | return false; |
| 253 | } |
| 254 | if (val != expected_size_xattr) { |
| 255 | LOG(ERROR) << "Expected xattr size " << expected_size_xattr |
| 256 | << " but size was " << val; |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | static bool CreateP2PFile(string p2p_dir, string file_name, |
| 265 | size_t size, size_t size_xattr) { |
| 266 | string path = p2p_dir + "/" + file_name; |
| 267 | |
| 268 | int fd = open(path.c_str(), O_CREAT|O_RDWR, 0644); |
| 269 | if (fd == -1) { |
| 270 | PLOG(ERROR) << "Error creating file with path " << path; |
| 271 | return false; |
| 272 | } |
| 273 | if (ftruncate(fd, size) != 0) { |
| 274 | PLOG(ERROR) << "Error truncating " << path << " to size " << size; |
| 275 | close(fd); |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | if (size_xattr != 0) { |
| 280 | string decimal_size = StringPrintf("%zu", size_xattr); |
| 281 | if (fsetxattr(fd, "user.cros-p2p-filesize", |
| 282 | decimal_size.c_str(), decimal_size.size(), 0) != 0) { |
| 283 | PLOG(ERROR) << "Error setting xattr on " << path; |
| 284 | close(fd); |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | close(fd); |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | // Check that sharing a *new* file works. |
| 294 | TEST_F(P2PManagerTest, ShareFile) { |
| 295 | scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
| 296 | NULL, "cros_au", 3)); |
| 297 | EXPECT_TRUE(manager->FileShare("foo", 10 * 1000 * 1000)); |
| 298 | EXPECT_EQ(manager->FileGetPath("foo"), |
| 299 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp")); |
| 300 | EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(), |
| 301 | "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000)); |
| 302 | |
| 303 | // Sharing it again - with the same expected size - should return true |
| 304 | EXPECT_TRUE(manager->FileShare("foo", 10 * 1000 * 1000)); |
| 305 | |
| 306 | // ... but if we use the wrong size, it should fail |
| 307 | EXPECT_FALSE(manager->FileShare("foo", 10 * 1000 * 1000 + 1)); |
| 308 | } |
| 309 | |
| 310 | // Check that making a shared file visible, does what is expected. |
| 311 | TEST_F(P2PManagerTest, MakeFileVisible) { |
| 312 | scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
| 313 | NULL, "cros_au", 3)); |
| 314 | // First, check that it's not visible. |
| 315 | manager->FileShare("foo", 10*1000*1000); |
| 316 | EXPECT_EQ(manager->FileGetPath("foo"), |
| 317 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p.tmp")); |
| 318 | EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(), |
| 319 | "foo.cros_au.p2p.tmp", 0, 10 * 1000 * 1000)); |
| 320 | // Make the file visible and check that it changed its name. Do it |
| 321 | // twice to check that FileMakeVisible() is idempotent. |
| 322 | for (int n = 0; n < 2; n++) { |
| 323 | manager->FileMakeVisible("foo"); |
| 324 | EXPECT_EQ(manager->FileGetPath("foo"), |
| 325 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p")); |
| 326 | EXPECT_TRUE(CheckP2PFile(test_conf_->GetP2PDir().value(), |
| 327 | "foo.cros_au.p2p", 0, 10 * 1000 * 1000)); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // Check that we return the right values for existing files in P2P_DIR. |
| 332 | TEST_F(P2PManagerTest, ExistingFiles) { |
| 333 | scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
| 334 | NULL, "cros_au", 3)); |
| 335 | bool visible; |
| 336 | |
| 337 | // Check that errors are returned if the file does not exist |
| 338 | EXPECT_EQ(manager->FileGetPath("foo"), FilePath()); |
| 339 | EXPECT_EQ(manager->FileGetSize("foo"), -1); |
| 340 | EXPECT_EQ(manager->FileGetExpectedSize("foo"), -1); |
| 341 | EXPECT_FALSE(manager->FileGetVisible("foo", NULL)); |
| 342 | // ... then create the file ... |
| 343 | EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(), |
| 344 | "foo.cros_au.p2p", 42, 43)); |
| 345 | // ... and then check that the expected values are returned |
| 346 | EXPECT_EQ(manager->FileGetPath("foo"), |
| 347 | test_conf_->GetP2PDir().Append("foo.cros_au.p2p")); |
| 348 | EXPECT_EQ(manager->FileGetSize("foo"), 42); |
| 349 | EXPECT_EQ(manager->FileGetExpectedSize("foo"), 43); |
| 350 | EXPECT_TRUE(manager->FileGetVisible("foo", &visible)); |
| 351 | EXPECT_TRUE(visible); |
| 352 | |
| 353 | // One more time, this time with a .tmp variant. First ensure it errors out.. |
| 354 | EXPECT_EQ(manager->FileGetPath("bar"), FilePath()); |
| 355 | EXPECT_EQ(manager->FileGetSize("bar"), -1); |
| 356 | EXPECT_EQ(manager->FileGetExpectedSize("bar"), -1); |
| 357 | EXPECT_FALSE(manager->FileGetVisible("bar", NULL)); |
| 358 | // ... then create the file ... |
| 359 | EXPECT_TRUE(CreateP2PFile(test_conf_->GetP2PDir().value(), |
| 360 | "bar.cros_au.p2p.tmp", 44, 45)); |
| 361 | // ... and then check that the expected values are returned |
| 362 | EXPECT_EQ(manager->FileGetPath("bar"), |
| 363 | test_conf_->GetP2PDir().Append("bar.cros_au.p2p.tmp")); |
| 364 | EXPECT_EQ(manager->FileGetSize("bar"), 44); |
| 365 | EXPECT_EQ(manager->FileGetExpectedSize("bar"), 45); |
| 366 | EXPECT_TRUE(manager->FileGetVisible("bar", &visible)); |
| 367 | EXPECT_FALSE(visible); |
| 368 | } |
| 369 | |
David Zeuthen | ac9c186 | 2013-08-29 10:26:13 -0700 | [diff] [blame] | 370 | #endif // http://crbug.com/281496 |
| 371 | |
David Zeuthen | 27a48bc | 2013-08-06 12:06:29 -0700 | [diff] [blame] | 372 | // This is a little bit ugly but short of mocking a 'p2p' service this |
| 373 | // will have to do. E.g. we essentially simulate the various |
| 374 | // behaviours of initctl(8) that we rely on. |
| 375 | TEST_F(P2PManagerTest, StartP2P) { |
| 376 | scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
| 377 | NULL, "cros_au", 3)); |
| 378 | |
| 379 | // Check that we can start the service |
| 380 | test_conf_->SetInitctlStartCommandLine("true"); |
| 381 | EXPECT_TRUE(manager->EnsureP2PRunning()); |
| 382 | test_conf_->SetInitctlStartCommandLine("false"); |
| 383 | EXPECT_FALSE(manager->EnsureP2PRunning()); |
| 384 | test_conf_->SetInitctlStartCommandLine( |
| 385 | "sh -c 'echo \"initctl: Job is already running: p2p\" >&2; false'"); |
| 386 | EXPECT_TRUE(manager->EnsureP2PRunning()); |
| 387 | test_conf_->SetInitctlStartCommandLine( |
| 388 | "sh -c 'echo something else >&2; false'"); |
| 389 | EXPECT_FALSE(manager->EnsureP2PRunning()); |
| 390 | } |
| 391 | |
| 392 | // Same comment as for StartP2P |
| 393 | TEST_F(P2PManagerTest, StopP2P) { |
| 394 | scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
| 395 | NULL, "cros_au", 3)); |
| 396 | |
| 397 | // Check that we can start the service |
| 398 | test_conf_->SetInitctlStopCommandLine("true"); |
| 399 | EXPECT_TRUE(manager->EnsureP2PNotRunning()); |
| 400 | test_conf_->SetInitctlStopCommandLine("false"); |
| 401 | EXPECT_FALSE(manager->EnsureP2PNotRunning()); |
| 402 | test_conf_->SetInitctlStopCommandLine( |
| 403 | "sh -c 'echo \"initctl: Unknown instance \" >&2; false'"); |
| 404 | EXPECT_TRUE(manager->EnsureP2PNotRunning()); |
| 405 | test_conf_->SetInitctlStopCommandLine( |
| 406 | "sh -c 'echo something else >&2; false'"); |
| 407 | EXPECT_FALSE(manager->EnsureP2PNotRunning()); |
| 408 | } |
| 409 | |
| 410 | static void ExpectUrl(const string& expected_url, |
| 411 | GMainLoop* loop, |
| 412 | const string& url) { |
| 413 | EXPECT_EQ(url, expected_url); |
| 414 | g_main_loop_quit(loop); |
| 415 | } |
| 416 | |
| 417 | // Like StartP2P, we're mocking the different results that p2p-client |
| 418 | // can return. It's not pretty but it works. |
| 419 | TEST_F(P2PManagerTest, LookupURL) { |
| 420 | scoped_ptr<P2PManager> manager(P2PManager::Construct(test_conf_, |
| 421 | NULL, "cros_au", 3)); |
| 422 | GMainLoop *loop = g_main_loop_new(NULL, FALSE); |
| 423 | |
| 424 | // Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au" |
| 425 | // being propagated in the right places. |
| 426 | test_conf_->SetP2PClientCommandLine("echo 'http://1.2.3.4/%s_%zu'"); |
| 427 | manager->LookupUrlForFile("fooX", 42, TimeDelta(), |
| 428 | base::Bind(ExpectUrl, |
| 429 | "http://1.2.3.4/fooX.cros_au_42", loop)); |
| 430 | g_main_loop_run(loop); |
| 431 | |
| 432 | // Emulate p2p-client returning invalid URL. |
| 433 | test_conf_->SetP2PClientCommandLine("echo 'not_a_valid_url'"); |
| 434 | manager->LookupUrlForFile("foobar", 42, TimeDelta(), |
| 435 | base::Bind(ExpectUrl, "", loop)); |
| 436 | g_main_loop_run(loop); |
| 437 | |
| 438 | // Emulate p2p-client conveying failure. |
| 439 | test_conf_->SetP2PClientCommandLine("false"); |
| 440 | manager->LookupUrlForFile("foobar", 42, TimeDelta(), |
| 441 | base::Bind(ExpectUrl, "", loop)); |
| 442 | g_main_loop_run(loop); |
| 443 | |
| 444 | // Emulate p2p-client not existing. |
| 445 | test_conf_->SetP2PClientCommandLine("/path/to/non/existent/helper/program"); |
| 446 | manager->LookupUrlForFile("foobar", 42, |
| 447 | TimeDelta(), |
| 448 | base::Bind(ExpectUrl, "", loop)); |
| 449 | g_main_loop_run(loop); |
| 450 | |
| 451 | // Emulate p2p-client crashing. |
| 452 | test_conf_->SetP2PClientCommandLine("sh -c 'kill -SEGV $$'"); |
| 453 | manager->LookupUrlForFile("foobar", 42, TimeDelta(), |
| 454 | base::Bind(ExpectUrl, "", loop)); |
| 455 | g_main_loop_run(loop); |
| 456 | |
| 457 | // Emulate p2p-client exceeding its timeout. |
| 458 | test_conf_->SetP2PClientCommandLine("sh -c 'echo http://1.2.3.4/; sleep 2'"); |
| 459 | manager->LookupUrlForFile("foobar", 42, TimeDelta::FromMilliseconds(500), |
| 460 | base::Bind(ExpectUrl, "", loop)); |
| 461 | g_main_loop_run(loop); |
| 462 | |
| 463 | g_main_loop_unref(loop); |
| 464 | } |
| 465 | |
| 466 | } // namespace chromeos_update_engine |