| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "build/build_config.h" |
| 11 #include "chrome/browser/importer/firefox_profile_lock.h" | 12 #include "chrome/browser/importer/firefox_profile_lock.h" |
| 12 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/test/file_test_utils.h" |
| 13 | 15 |
| 14 class FirefoxProfileLockTest : public testing::Test { | 16 class FirefoxProfileLockTest : public testing::Test { |
| 15 public: | 17 public: |
| 16 protected: | 18 protected: |
| 17 virtual void SetUp() { | 19 virtual void SetUp() { |
| 18 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_path_)); | 20 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_path_)); |
| 19 std::wstring dir_name(L"FirefoxProfileLockTest"); | 21 std::wstring dir_name(L"FirefoxProfileLockTest"); |
| 20 dir_name.append(StringPrintf(L"-%d", base::GetCurrentProcId())); | 22 dir_name.append(StringPrintf(L"-%d", base::GetCurrentProcId())); |
| 21 test_path_ = test_path_.Append(FilePath::FromWStringHack(dir_name)); | 23 test_path_ = test_path_.Append(FilePath::FromWStringHack(dir_name)); |
| 22 file_util::Delete(test_path_, true); | 24 file_util::Delete(test_path_, true); |
| 23 file_util::CreateDirectory(test_path_); | 25 file_util::CreateDirectory(test_path_); |
| 24 } | 26 } |
| 25 | 27 |
| 26 virtual void TearDown() { | 28 virtual void TearDown() { |
| 27 ASSERT_TRUE(file_util::Delete(test_path_, true)); | 29 ASSERT_TRUE(file_util::Delete(test_path_, true)); |
| 28 ASSERT_FALSE(file_util::PathExists(test_path_)); | 30 ASSERT_FALSE(file_util::PathExists(test_path_)); |
| 29 } | 31 } |
| 30 | 32 |
| 31 FilePath test_path_; | 33 FilePath test_path_; |
| 32 }; | 34 }; |
| 33 | 35 |
| 34 TEST_F(FirefoxProfileLockTest, LockTest) { | 36 TEST_F(FirefoxProfileLockTest, LockTest) { |
| 35 FirefoxProfileLock lock1(test_path_.ToWStringHack()); | 37 FirefoxProfileLock lock1(test_path_.ToWStringHack()); |
| 36 ASSERT_TRUE(lock1.HasAcquired()); | 38 ASSERT_TRUE(lock1.HasAcquired()); |
| 37 lock1.Unlock(); | 39 lock1.Unlock(); |
| 38 ASSERT_FALSE(lock1.HasAcquired()); | 40 ASSERT_FALSE(lock1.HasAcquired()); |
| 39 lock1.Lock(); | 41 lock1.Lock(); |
| 40 ASSERT_TRUE(lock1.HasAcquired()); | 42 ASSERT_TRUE(lock1.HasAcquired()); |
| 41 } | 43 } |
| 44 |
| 45 // Tests basic functionality and verifies that the lock file is deleted after |
| 46 // use. |
| 47 TEST_F(FirefoxProfileLockTest, ProfileLock) { |
| 48 std::wstring test_path; |
| 49 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); |
| 50 FilePath lock_file_path = FilePath::FromWStringHack(test_path); |
| 51 FileAutoDeleter deleter(lock_file_path); |
| 52 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); |
| 53 |
| 54 scoped_ptr<FirefoxProfileLock> lock; |
| 55 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); |
| 56 EXPECT_FALSE(file_util::PathExists(lock_file_path)); |
| 57 lock.reset(new FirefoxProfileLock(test_path)); |
| 58 EXPECT_TRUE(lock->HasAcquired()); |
| 59 EXPECT_TRUE(file_util::PathExists(lock_file_path)); |
| 60 lock->Unlock(); |
| 61 EXPECT_FALSE(lock->HasAcquired()); |
| 62 |
| 63 // In the posix code, we don't delete the file when releasing the lock. |
| 64 #if !defined(OS_POSIX) |
| 65 EXPECT_FALSE(file_util::PathExists(lock_file_path)); |
| 66 #endif // !defined(OS_POSIX) |
| 67 lock->Lock(); |
| 68 EXPECT_TRUE(lock->HasAcquired()); |
| 69 EXPECT_TRUE(file_util::PathExists(lock_file_path)); |
| 70 lock->Lock(); |
| 71 EXPECT_TRUE(lock->HasAcquired()); |
| 72 lock->Unlock(); |
| 73 EXPECT_FALSE(lock->HasAcquired()); |
| 74 // In the posix code, we don't delete the file when releasing the lock. |
| 75 #if !defined(OS_POSIX) |
| 76 EXPECT_FALSE(file_util::PathExists(lock_file_path)); |
| 77 #endif // !defined(OS_POSIX) |
| 78 } |
| 79 |
| 80 // If for some reason the lock file is left behind by the previous owner, we |
| 81 // should still be able to lock it, at least in the Windows implementation. |
| 82 TEST_F(FirefoxProfileLockTest, ProfileLockOrphaned) { |
| 83 std::wstring test_path; |
| 84 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); |
| 85 FilePath lock_file_path = FilePath::FromWStringHack(test_path); |
| 86 FileAutoDeleter deleter(lock_file_path); |
| 87 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); |
| 88 |
| 89 // Create the orphaned lock file. |
| 90 FILE* lock_file = file_util::OpenFile(lock_file_path, "w"); |
| 91 ASSERT_TRUE(lock_file); |
| 92 file_util::CloseFile(lock_file); |
| 93 EXPECT_TRUE(file_util::PathExists(lock_file_path)); |
| 94 |
| 95 scoped_ptr<FirefoxProfileLock> lock; |
| 96 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); |
| 97 lock.reset(new FirefoxProfileLock(test_path)); |
| 98 EXPECT_TRUE(lock->HasAcquired()); |
| 99 lock->Unlock(); |
| 100 EXPECT_FALSE(lock->HasAcquired()); |
| 101 } |
| 102 |
| 103 // This is broken on POSIX since the same process is allowed to reacquire a |
| 104 // lock. |
| 105 #if !defined(OS_POSIX) |
| 106 // Tests two locks contending for the same lock file. |
| 107 TEST_F(FirefoxProfileLockTest, ProfileLockContention) { |
| 108 std::wstring test_path; |
| 109 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); |
| 110 FileAutoDeleter deleter(FilePath::FromWStringHack(test_path)); |
| 111 |
| 112 scoped_ptr<FirefoxProfileLock> lock1; |
| 113 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get()); |
| 114 lock1.reset(new FirefoxProfileLock(test_path)); |
| 115 EXPECT_TRUE(lock1->HasAcquired()); |
| 116 |
| 117 scoped_ptr<FirefoxProfileLock> lock2; |
| 118 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get()); |
| 119 lock2.reset(new FirefoxProfileLock(test_path)); |
| 120 EXPECT_FALSE(lock2->HasAcquired()); |
| 121 |
| 122 lock1->Unlock(); |
| 123 EXPECT_FALSE(lock1->HasAcquired()); |
| 124 |
| 125 lock2->Lock(); |
| 126 EXPECT_TRUE(lock2->HasAcquired()); |
| 127 lock2->Unlock(); |
| 128 EXPECT_FALSE(lock2->HasAcquired()); |
| 129 } |
| 130 #endif |
| OLD | NEW |