OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/importer/firefox_profile_lock.h" | 5 #include "chrome/browser/importer/firefox_profile_lock.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 class FirefoxProfileLockTest : public testing::Test { | 16 class FirefoxProfileLockTest : public testing::Test { |
17 protected: | 17 protected: |
18 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 18 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
19 | 19 |
20 base::ScopedTempDir temp_dir_; | 20 base::ScopedTempDir temp_dir_; |
21 }; | 21 }; |
22 | 22 |
23 TEST_F(FirefoxProfileLockTest, LockTest) { | 23 TEST_F(FirefoxProfileLockTest, LockTest) { |
24 FirefoxProfileLock lock1(temp_dir_.path()); | 24 FirefoxProfileLock lock1(temp_dir_.GetPath()); |
25 ASSERT_TRUE(lock1.HasAcquired()); | 25 ASSERT_TRUE(lock1.HasAcquired()); |
26 lock1.Unlock(); | 26 lock1.Unlock(); |
27 ASSERT_FALSE(lock1.HasAcquired()); | 27 ASSERT_FALSE(lock1.HasAcquired()); |
28 lock1.Lock(); | 28 lock1.Lock(); |
29 ASSERT_TRUE(lock1.HasAcquired()); | 29 ASSERT_TRUE(lock1.HasAcquired()); |
30 } | 30 } |
31 | 31 |
32 // Tests basic functionality and verifies that the lock file is deleted after | 32 // Tests basic functionality and verifies that the lock file is deleted after |
33 // use. | 33 // use. |
34 TEST_F(FirefoxProfileLockTest, ProfileLock) { | 34 TEST_F(FirefoxProfileLockTest, ProfileLock) { |
35 base::FilePath test_path = temp_dir_.path(); | 35 base::FilePath test_path = temp_dir_.GetPath(); |
36 base::FilePath lock_file_path = | 36 base::FilePath lock_file_path = |
37 test_path.Append(FirefoxProfileLock::kLockFileName); | 37 test_path.Append(FirefoxProfileLock::kLockFileName); |
38 | 38 |
39 std::unique_ptr<FirefoxProfileLock> lock; | 39 std::unique_ptr<FirefoxProfileLock> lock; |
40 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | 40 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); |
41 EXPECT_FALSE(base::PathExists(lock_file_path)); | 41 EXPECT_FALSE(base::PathExists(lock_file_path)); |
42 lock.reset(new FirefoxProfileLock(test_path)); | 42 lock.reset(new FirefoxProfileLock(test_path)); |
43 EXPECT_TRUE(lock->HasAcquired()); | 43 EXPECT_TRUE(lock->HasAcquired()); |
44 EXPECT_TRUE(base::PathExists(lock_file_path)); | 44 EXPECT_TRUE(base::PathExists(lock_file_path)); |
45 lock->Unlock(); | 45 lock->Unlock(); |
(...skipping 12 matching lines...) Expand all Loading... |
58 EXPECT_FALSE(lock->HasAcquired()); | 58 EXPECT_FALSE(lock->HasAcquired()); |
59 // In the posix code, we don't delete the file when releasing the lock. | 59 // In the posix code, we don't delete the file when releasing the lock. |
60 #if !defined(OS_POSIX) | 60 #if !defined(OS_POSIX) |
61 EXPECT_FALSE(base::PathExists(lock_file_path)); | 61 EXPECT_FALSE(base::PathExists(lock_file_path)); |
62 #endif // !defined(OS_POSIX) | 62 #endif // !defined(OS_POSIX) |
63 } | 63 } |
64 | 64 |
65 // If for some reason the lock file is left behind by the previous owner, we | 65 // If for some reason the lock file is left behind by the previous owner, we |
66 // should still be able to lock it, at least in the Windows implementation. | 66 // should still be able to lock it, at least in the Windows implementation. |
67 TEST_F(FirefoxProfileLockTest, ProfileLockOrphaned) { | 67 TEST_F(FirefoxProfileLockTest, ProfileLockOrphaned) { |
68 base::FilePath test_path = temp_dir_.path(); | 68 base::FilePath test_path = temp_dir_.GetPath(); |
69 base::FilePath lock_file_path = | 69 base::FilePath lock_file_path = |
70 test_path.Append(FirefoxProfileLock::kLockFileName); | 70 test_path.Append(FirefoxProfileLock::kLockFileName); |
71 | 71 |
72 // Create the orphaned lock file. | 72 // Create the orphaned lock file. |
73 FILE* lock_file = base::OpenFile(lock_file_path, "w"); | 73 FILE* lock_file = base::OpenFile(lock_file_path, "w"); |
74 ASSERT_TRUE(lock_file); | 74 ASSERT_TRUE(lock_file); |
75 base::CloseFile(lock_file); | 75 base::CloseFile(lock_file); |
76 EXPECT_TRUE(base::PathExists(lock_file_path)); | 76 EXPECT_TRUE(base::PathExists(lock_file_path)); |
77 | 77 |
78 std::unique_ptr<FirefoxProfileLock> lock; | 78 std::unique_ptr<FirefoxProfileLock> lock; |
79 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | 79 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); |
80 lock.reset(new FirefoxProfileLock(test_path)); | 80 lock.reset(new FirefoxProfileLock(test_path)); |
81 EXPECT_TRUE(lock->HasAcquired()); | 81 EXPECT_TRUE(lock->HasAcquired()); |
82 lock->Unlock(); | 82 lock->Unlock(); |
83 EXPECT_FALSE(lock->HasAcquired()); | 83 EXPECT_FALSE(lock->HasAcquired()); |
84 } | 84 } |
85 | 85 |
86 // This is broken on POSIX since the same process is allowed to reacquire a | 86 // This is broken on POSIX since the same process is allowed to reacquire a |
87 // lock. | 87 // lock. |
88 #if !defined(OS_POSIX) | 88 #if !defined(OS_POSIX) |
89 // Tests two locks contending for the same lock file. | 89 // Tests two locks contending for the same lock file. |
90 TEST_F(FirefoxProfileLockTest, ProfileLockContention) { | 90 TEST_F(FirefoxProfileLockTest, ProfileLockContention) { |
91 base::FilePath test_path = temp_dir_.path(); | 91 base::FilePath test_path = temp_dir_.GetPath(); |
92 | 92 |
93 std::unique_ptr<FirefoxProfileLock> lock1; | 93 std::unique_ptr<FirefoxProfileLock> lock1; |
94 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get()); | 94 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get()); |
95 lock1.reset(new FirefoxProfileLock(test_path)); | 95 lock1.reset(new FirefoxProfileLock(test_path)); |
96 EXPECT_TRUE(lock1->HasAcquired()); | 96 EXPECT_TRUE(lock1->HasAcquired()); |
97 | 97 |
98 std::unique_ptr<FirefoxProfileLock> lock2; | 98 std::unique_ptr<FirefoxProfileLock> lock2; |
99 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get()); | 99 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get()); |
100 lock2.reset(new FirefoxProfileLock(test_path)); | 100 lock2.reset(new FirefoxProfileLock(test_path)); |
101 EXPECT_FALSE(lock2->HasAcquired()); | 101 EXPECT_FALSE(lock2->HasAcquired()); |
102 | 102 |
103 lock1->Unlock(); | 103 lock1->Unlock(); |
104 EXPECT_FALSE(lock1->HasAcquired()); | 104 EXPECT_FALSE(lock1->HasAcquired()); |
105 | 105 |
106 lock2->Lock(); | 106 lock2->Lock(); |
107 EXPECT_TRUE(lock2->HasAcquired()); | 107 EXPECT_TRUE(lock2->HasAcquired()); |
108 lock2->Unlock(); | 108 lock2->Unlock(); |
109 EXPECT_FALSE(lock2->HasAcquired()); | 109 EXPECT_FALSE(lock2->HasAcquired()); |
110 } | 110 } |
111 #endif | 111 #endif |
OLD | NEW |