| 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" | |
| 13 #include "chrome/browser/importer/nss_decryptor.h" | 12 #include "chrome/browser/importer/nss_decryptor.h" |
| 14 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 15 #include "chrome/test/file_test_utils.h" | |
| 16 | 14 |
| 17 using base::Time; | 15 using base::Time; |
| 18 | 16 |
| 19 TEST(FirefoxImporterTest, Firefox2NSS3Decryptor) { | 17 TEST(FirefoxImporterTest, Firefox2NSS3Decryptor) { |
| 20 std::wstring nss_path; | 18 std::wstring nss_path; |
| 21 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path)); | 19 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &nss_path)); |
| 22 file_util::AppendToPath(&nss_path, L"firefox2_nss"); | 20 file_util::AppendToPath(&nss_path, L"firefox2_nss"); |
| 23 std::wstring db_path; | 21 std::wstring db_path; |
| 24 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path)); | 22 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &db_path)); |
| 25 file_util::AppendToPath(&db_path, L"firefox2_profile"); | 23 file_util::AppendToPath(&db_path, L"firefox2_profile"); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 result = Firefox2Importer::ParseBookmarkFromLine( | 144 result = Firefox2Importer::ParseBookmarkFromLine( |
| 147 "<DT><A HREF=\"http://domain.com/?q=%22", | 145 "<DT><A HREF=\"http://domain.com/?q=%22", |
| 148 charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); | 146 charset, &title, &url, &favicon, &shortcut, &add_date, &post_data); |
| 149 EXPECT_FALSE(result); | 147 EXPECT_FALSE(result); |
| 150 EXPECT_EQ(L"", title); | 148 EXPECT_EQ(L"", title); |
| 151 EXPECT_EQ("", url.spec()); | 149 EXPECT_EQ("", url.spec()); |
| 152 EXPECT_EQ(L"", shortcut); | 150 EXPECT_EQ(L"", shortcut); |
| 153 EXPECT_EQ(L"", post_data); | 151 EXPECT_EQ(L"", post_data); |
| 154 EXPECT_TRUE(Time() == add_date); | 152 EXPECT_TRUE(Time() == add_date); |
| 155 } | 153 } |
| 156 | |
| 157 // Tests basic functionality and verifies that the lock file is deleted after | |
| 158 // use. | |
| 159 TEST(FirefoxImporterTest, ProfileLock) { | |
| 160 std::wstring test_path; | |
| 161 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); | |
| 162 FilePath lock_file_path = FilePath::FromWStringHack(test_path); | |
| 163 FileAutoDeleter deleter(lock_file_path); | |
| 164 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); | |
| 165 | |
| 166 scoped_ptr<FirefoxProfileLock> lock; | |
| 167 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | |
| 168 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | |
| 169 lock.reset(new FirefoxProfileLock(test_path)); | |
| 170 EXPECT_TRUE(lock->HasAcquired()); | |
| 171 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | |
| 172 lock->Unlock(); | |
| 173 EXPECT_FALSE(lock->HasAcquired()); | |
| 174 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | |
| 175 lock->Lock(); | |
| 176 EXPECT_TRUE(lock->HasAcquired()); | |
| 177 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | |
| 178 lock->Lock(); | |
| 179 EXPECT_TRUE(lock->HasAcquired()); | |
| 180 lock->Unlock(); | |
| 181 EXPECT_FALSE(lock->HasAcquired()); | |
| 182 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | |
| 183 } | |
| 184 | |
| 185 // If for some reason the lock file is left behind by the previous owner, we | |
| 186 // should still be able to lock it, at least in the Windows implementation. | |
| 187 TEST(FirefoxImporterTest, ProfileLockOrphaned) { | |
| 188 std::wstring test_path; | |
| 189 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); | |
| 190 FilePath lock_file_path = FilePath::FromWStringHack(test_path); | |
| 191 FileAutoDeleter deleter(lock_file_path); | |
| 192 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); | |
| 193 | |
| 194 // Create the orphaned lock file. | |
| 195 FILE* lock_file = file_util::OpenFile(lock_file_path, "w"); | |
| 196 ASSERT_TRUE(lock_file); | |
| 197 file_util::CloseFile(lock_file); | |
| 198 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | |
| 199 | |
| 200 scoped_ptr<FirefoxProfileLock> lock; | |
| 201 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | |
| 202 lock.reset(new FirefoxProfileLock(test_path)); | |
| 203 EXPECT_TRUE(lock->HasAcquired()); | |
| 204 lock->Unlock(); | |
| 205 EXPECT_FALSE(lock->HasAcquired()); | |
| 206 } | |
| 207 | |
| 208 // Tests two locks contending for the same lock file. | |
| 209 TEST(FirefoxImporterTest, ProfileLockContention) { | |
| 210 std::wstring test_path; | |
| 211 file_util::CreateNewTempDirectory(L"firefox_profile", &test_path); | |
| 212 FileAutoDeleter deleter(FilePath::FromWStringHack(test_path)); | |
| 213 | |
| 214 scoped_ptr<FirefoxProfileLock> lock1; | |
| 215 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get()); | |
| 216 lock1.reset(new FirefoxProfileLock(test_path)); | |
| 217 EXPECT_TRUE(lock1->HasAcquired()); | |
| 218 | |
| 219 scoped_ptr<FirefoxProfileLock> lock2; | |
| 220 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get()); | |
| 221 lock2.reset(new FirefoxProfileLock(test_path)); | |
| 222 EXPECT_FALSE(lock2->HasAcquired()); | |
| 223 | |
| 224 lock1->Unlock(); | |
| 225 EXPECT_FALSE(lock1->HasAcquired()); | |
| 226 | |
| 227 lock2->Lock(); | |
| 228 EXPECT_TRUE(lock2->HasAcquired()); | |
| 229 lock2->Unlock(); | |
| 230 EXPECT_FALSE(lock2->HasAcquired()); | |
| 231 } | |
| OLD | NEW |