| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "chrome/browser/importer/firefox2_importer.h" | 10 #include "chrome/browser/importer/firefox2_importer.h" |
| 11 #include "chrome/browser/importer/firefox_importer_utils.h" | 11 #include "chrome/browser/importer/firefox_importer_utils.h" |
| 12 #include "chrome/browser/importer/firefox_profile_lock.h" | 12 #include "chrome/browser/importer/firefox_profile_lock.h" |
| 13 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/test/file_test_utils.h" |
| 14 | 15 |
| 15 using base::Time; | 16 using base::Time; |
| 16 | 17 |
| 17 TEST(FirefoxImporterTest, Firefox2NSS3Decryptor) { | 18 TEST(FirefoxImporterTest, Firefox2NSS3Decryptor) { |
| 18 std::wstring nss_path; | 19 std::wstring nss_path; |
| 19 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path)); | 20 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path)); |
| 20 file_util::AppendToPath(&nss_path, L"firefox2_nss"); | 21 file_util::AppendToPath(&nss_path, L"firefox2_nss"); |
| 21 std::wstring db_path; | 22 std::wstring db_path; |
| 22 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path)); | 23 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path)); |
| 23 file_util::AppendToPath(&db_path, L"firefox2_profile"); | 24 file_util::AppendToPath(&db_path, L"firefox2_profile"); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 EXPECT_EQ(L"", post_data); | 142 EXPECT_EQ(L"", post_data); |
| 142 EXPECT_TRUE(Time() == add_date); | 143 EXPECT_TRUE(Time() == add_date); |
| 143 } | 144 } |
| 144 | 145 |
| 145 // Tests basic functionality and verifies that the lock file is deleted after | 146 // Tests basic functionality and verifies that the lock file is deleted after |
| 146 // use. | 147 // use. |
| 147 TEST(FirefoxImporterTest, ProfileLock) { | 148 TEST(FirefoxImporterTest, ProfileLock) { |
| 148 std::wstring test_path; | 149 std::wstring test_path; |
| 149 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); | 150 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); |
| 150 FilePath lock_file_path = FilePath::FromWStringHack(test_path); | 151 FilePath lock_file_path = FilePath::FromWStringHack(test_path); |
| 152 FileAutoDeleter deleter(lock_file_path); |
| 151 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); | 153 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); |
| 152 | 154 |
| 153 scoped_ptr<FirefoxProfileLock> lock; | 155 scoped_ptr<FirefoxProfileLock> lock; |
| 154 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | 156 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); |
| 155 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | 157 EXPECT_FALSE(file_util::PathExists(lock_file_path)); |
| 156 lock.reset(new FirefoxProfileLock(test_path)); | 158 lock.reset(new FirefoxProfileLock(test_path)); |
| 157 EXPECT_TRUE(lock->HasAcquired()); | 159 EXPECT_TRUE(lock->HasAcquired()); |
| 158 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | 160 EXPECT_TRUE(file_util::PathExists(lock_file_path)); |
| 159 lock->Unlock(); | 161 lock->Unlock(); |
| 160 EXPECT_FALSE(lock->HasAcquired()); | 162 EXPECT_FALSE(lock->HasAcquired()); |
| 161 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | 163 EXPECT_FALSE(file_util::PathExists(lock_file_path)); |
| 162 lock->Lock(); | 164 lock->Lock(); |
| 163 EXPECT_TRUE(lock->HasAcquired()); | 165 EXPECT_TRUE(lock->HasAcquired()); |
| 164 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | 166 EXPECT_TRUE(file_util::PathExists(lock_file_path)); |
| 165 lock->Lock(); | 167 lock->Lock(); |
| 166 EXPECT_TRUE(lock->HasAcquired()); | 168 EXPECT_TRUE(lock->HasAcquired()); |
| 167 lock->Unlock(); | 169 lock->Unlock(); |
| 168 EXPECT_FALSE(lock->HasAcquired()); | 170 EXPECT_FALSE(lock->HasAcquired()); |
| 169 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | 171 EXPECT_FALSE(file_util::PathExists(lock_file_path)); |
| 170 } | 172 } |
| 171 | 173 |
| 172 // If for some reason the lock file is left behind by the previous owner, we | 174 // If for some reason the lock file is left behind by the previous owner, we |
| 173 // should still be able to lock it, at least in the Windows implementation. | 175 // should still be able to lock it, at least in the Windows implementation. |
| 174 TEST(FirefoxImporterTest, ProfileLockOrphaned) { | 176 TEST(FirefoxImporterTest, ProfileLockOrphaned) { |
| 175 std::wstring test_path; | 177 std::wstring test_path; |
| 176 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); | 178 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); |
| 177 FilePath lock_file_path = FilePath::FromWStringHack(test_path); | 179 FilePath lock_file_path = FilePath::FromWStringHack(test_path); |
| 180 FileAutoDeleter deleter(lock_file_path); |
| 178 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); | 181 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); |
| 179 | 182 |
| 180 // Create the orphaned lock file. | 183 // Create the orphaned lock file. |
| 181 FILE* lock_file = file_util::OpenFile(lock_file_path, "w"); | 184 FILE* lock_file = file_util::OpenFile(lock_file_path, "w"); |
| 182 ASSERT_TRUE(lock_file); | 185 ASSERT_TRUE(lock_file); |
| 183 file_util::CloseFile(lock_file); | 186 file_util::CloseFile(lock_file); |
| 184 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | 187 EXPECT_TRUE(file_util::PathExists(lock_file_path)); |
| 185 | 188 |
| 186 scoped_ptr<FirefoxProfileLock> lock; | 189 scoped_ptr<FirefoxProfileLock> lock; |
| 187 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | 190 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); |
| 188 lock.reset(new FirefoxProfileLock(test_path)); | 191 lock.reset(new FirefoxProfileLock(test_path)); |
| 189 EXPECT_TRUE(lock->HasAcquired()); | 192 EXPECT_TRUE(lock->HasAcquired()); |
| 190 lock->Unlock(); | 193 lock->Unlock(); |
| 191 EXPECT_FALSE(lock->HasAcquired()); | 194 EXPECT_FALSE(lock->HasAcquired()); |
| 192 } | 195 } |
| 193 | 196 |
| 194 // Tests two locks contending for the same lock file. | 197 // Tests two locks contending for the same lock file. |
| 195 TEST(FirefoxImporterTest, ProfileLockContention) { | 198 TEST(FirefoxImporterTest, ProfileLockContention) { |
| 196 std::wstring test_path; | 199 std::wstring test_path; |
| 197 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); | 200 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); |
| 201 FileAutoDeleter deleter(FilePath::FromWStringHack(test_path)); |
| 198 | 202 |
| 199 scoped_ptr<FirefoxProfileLock> lock1; | 203 scoped_ptr<FirefoxProfileLock> lock1; |
| 200 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get()); | 204 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get()); |
| 201 lock1.reset(new FirefoxProfileLock(test_path)); | 205 lock1.reset(new FirefoxProfileLock(test_path)); |
| 202 EXPECT_TRUE(lock1->HasAcquired()); | 206 EXPECT_TRUE(lock1->HasAcquired()); |
| 203 | 207 |
| 204 scoped_ptr<FirefoxProfileLock> lock2; | 208 scoped_ptr<FirefoxProfileLock> lock2; |
| 205 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get()); | 209 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get()); |
| 206 lock2.reset(new FirefoxProfileLock(test_path)); | 210 lock2.reset(new FirefoxProfileLock(test_path)); |
| 207 EXPECT_FALSE(lock2->HasAcquired()); | 211 EXPECT_FALSE(lock2->HasAcquired()); |
| 208 | 212 |
| 209 lock1->Unlock(); | 213 lock1->Unlock(); |
| 210 EXPECT_FALSE(lock1->HasAcquired()); | 214 EXPECT_FALSE(lock1->HasAcquired()); |
| 211 | 215 |
| 212 lock2->Lock(); | 216 lock2->Lock(); |
| 213 EXPECT_TRUE(lock2->HasAcquired()); | 217 EXPECT_TRUE(lock2->HasAcquired()); |
| 214 lock2->Unlock(); | 218 lock2->Unlock(); |
| 215 EXPECT_FALSE(lock2->HasAcquired()); | 219 EXPECT_FALSE(lock2->HasAcquired()); |
| 216 } | 220 } |
| OLD | NEW |