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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
7 #include "base/process_util.h" | 7 #include "base/process_util.h" |
8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "chrome/browser/importer/firefox_profile_lock.h" | 11 #include "chrome/browser/importer/firefox_profile_lock.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
13 #include "chrome/test/file_test_utils.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
15 | 14 |
16 class FirefoxProfileLockTest : public testing::Test { | 15 class FirefoxProfileLockTest : public testing::Test { |
17 protected: | 16 protected: |
18 virtual void SetUp() { | 17 virtual void SetUp() { |
19 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 18 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
20 } | 19 } |
21 | 20 |
22 ScopedTempDir temp_dir_; | 21 ScopedTempDir temp_dir_; |
23 }; | 22 }; |
24 | 23 |
25 TEST_F(FirefoxProfileLockTest, LockTest) { | 24 TEST_F(FirefoxProfileLockTest, LockTest) { |
26 FirefoxProfileLock lock1(temp_dir_.path()); | 25 FirefoxProfileLock lock1(temp_dir_.path()); |
27 ASSERT_TRUE(lock1.HasAcquired()); | 26 ASSERT_TRUE(lock1.HasAcquired()); |
28 lock1.Unlock(); | 27 lock1.Unlock(); |
29 ASSERT_FALSE(lock1.HasAcquired()); | 28 ASSERT_FALSE(lock1.HasAcquired()); |
30 lock1.Lock(); | 29 lock1.Lock(); |
31 ASSERT_TRUE(lock1.HasAcquired()); | 30 ASSERT_TRUE(lock1.HasAcquired()); |
32 } | 31 } |
33 | 32 |
34 // Tests basic functionality and verifies that the lock file is deleted after | 33 // Tests basic functionality and verifies that the lock file is deleted after |
35 // use. | 34 // use. |
36 TEST_F(FirefoxProfileLockTest, ProfileLock) { | 35 TEST_F(FirefoxProfileLockTest, ProfileLock) { |
37 FilePath test_path; | 36 FilePath test_path = temp_dir_.path(); |
38 ASSERT_TRUE(file_util::CreateNewTempDirectory( | 37 FilePath lock_file_path = test_path.Append(FirefoxProfileLock::kLockFileName); |
39 FILE_PATH_LITERAL("firefox_profile"), &test_path)); | |
40 FilePath lock_file_path = test_path; | |
41 FileAutoDeleter deleter(lock_file_path); | |
42 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); | |
43 | 38 |
44 scoped_ptr<FirefoxProfileLock> lock; | 39 scoped_ptr<FirefoxProfileLock> lock; |
45 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | 40 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); |
46 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | 41 EXPECT_FALSE(file_util::PathExists(lock_file_path)); |
47 lock.reset(new FirefoxProfileLock(test_path)); | 42 lock.reset(new FirefoxProfileLock(test_path)); |
48 EXPECT_TRUE(lock->HasAcquired()); | 43 EXPECT_TRUE(lock->HasAcquired()); |
49 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | 44 EXPECT_TRUE(file_util::PathExists(lock_file_path)); |
50 lock->Unlock(); | 45 lock->Unlock(); |
51 EXPECT_FALSE(lock->HasAcquired()); | 46 EXPECT_FALSE(lock->HasAcquired()); |
52 | 47 |
(...skipping 10 matching lines...) Expand all Loading... |
63 EXPECT_FALSE(lock->HasAcquired()); | 58 EXPECT_FALSE(lock->HasAcquired()); |
64 // 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. |
65 #if !defined(OS_POSIX) | 60 #if !defined(OS_POSIX) |
66 EXPECT_FALSE(file_util::PathExists(lock_file_path)); | 61 EXPECT_FALSE(file_util::PathExists(lock_file_path)); |
67 #endif // !defined(OS_POSIX) | 62 #endif // !defined(OS_POSIX) |
68 } | 63 } |
69 | 64 |
70 // 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 |
71 // 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. |
72 TEST_F(FirefoxProfileLockTest, ProfileLockOrphaned) { | 67 TEST_F(FirefoxProfileLockTest, ProfileLockOrphaned) { |
73 FilePath test_path; | 68 FilePath test_path = temp_dir_.path(); |
74 ASSERT_TRUE(file_util::CreateNewTempDirectory( | 69 FilePath lock_file_path = test_path.Append(FirefoxProfileLock::kLockFileName); |
75 FILE_PATH_LITERAL("firefox_profile"), &test_path)); | |
76 FilePath lock_file_path = test_path; | |
77 FileAutoDeleter deleter(lock_file_path); | |
78 lock_file_path = lock_file_path.Append(FirefoxProfileLock::kLockFileName); | |
79 | 70 |
80 // Create the orphaned lock file. | 71 // Create the orphaned lock file. |
81 FILE* lock_file = file_util::OpenFile(lock_file_path, "w"); | 72 FILE* lock_file = file_util::OpenFile(lock_file_path, "w"); |
82 ASSERT_TRUE(lock_file); | 73 ASSERT_TRUE(lock_file); |
83 file_util::CloseFile(lock_file); | 74 file_util::CloseFile(lock_file); |
84 EXPECT_TRUE(file_util::PathExists(lock_file_path)); | 75 EXPECT_TRUE(file_util::PathExists(lock_file_path)); |
85 | 76 |
86 scoped_ptr<FirefoxProfileLock> lock; | 77 scoped_ptr<FirefoxProfileLock> lock; |
87 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); | 78 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock.get()); |
88 lock.reset(new FirefoxProfileLock(test_path)); | 79 lock.reset(new FirefoxProfileLock(test_path)); |
89 EXPECT_TRUE(lock->HasAcquired()); | 80 EXPECT_TRUE(lock->HasAcquired()); |
90 lock->Unlock(); | 81 lock->Unlock(); |
91 EXPECT_FALSE(lock->HasAcquired()); | 82 EXPECT_FALSE(lock->HasAcquired()); |
92 } | 83 } |
93 | 84 |
94 // This is broken on POSIX since the same process is allowed to reacquire a | 85 // This is broken on POSIX since the same process is allowed to reacquire a |
95 // lock. | 86 // lock. |
96 #if !defined(OS_POSIX) | 87 #if !defined(OS_POSIX) |
97 // Tests two locks contending for the same lock file. | 88 // Tests two locks contending for the same lock file. |
98 TEST_F(FirefoxProfileLockTest, ProfileLockContention) { | 89 TEST_F(FirefoxProfileLockTest, ProfileLockContention) { |
99 FilePath test_path; | 90 FilePath test_path = temp_dir_.path(); |
100 ASSERT_TRUE(file_util::CreateNewTempDirectory( | |
101 FILE_PATH_LITERAL("firefox_profile"), &test_path)); | |
102 FileAutoDeleter deleter(test_path); | |
103 | 91 |
104 scoped_ptr<FirefoxProfileLock> lock1; | 92 scoped_ptr<FirefoxProfileLock> lock1; |
105 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get()); | 93 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock1.get()); |
106 lock1.reset(new FirefoxProfileLock(test_path)); | 94 lock1.reset(new FirefoxProfileLock(test_path)); |
107 EXPECT_TRUE(lock1->HasAcquired()); | 95 EXPECT_TRUE(lock1->HasAcquired()); |
108 | 96 |
109 scoped_ptr<FirefoxProfileLock> lock2; | 97 scoped_ptr<FirefoxProfileLock> lock2; |
110 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get()); | 98 EXPECT_EQ(static_cast<FirefoxProfileLock*>(NULL), lock2.get()); |
111 lock2.reset(new FirefoxProfileLock(test_path)); | 99 lock2.reset(new FirefoxProfileLock(test_path)); |
112 EXPECT_FALSE(lock2->HasAcquired()); | 100 EXPECT_FALSE(lock2->HasAcquired()); |
113 | 101 |
114 lock1->Unlock(); | 102 lock1->Unlock(); |
115 EXPECT_FALSE(lock1->HasAcquired()); | 103 EXPECT_FALSE(lock1->HasAcquired()); |
116 | 104 |
117 lock2->Lock(); | 105 lock2->Lock(); |
118 EXPECT_TRUE(lock2->HasAcquired()); | 106 EXPECT_TRUE(lock2->HasAcquired()); |
119 lock2->Unlock(); | 107 lock2->Unlock(); |
120 EXPECT_FALSE(lock2->HasAcquired()); | 108 EXPECT_FALSE(lock2->HasAcquired()); |
121 } | 109 } |
122 #endif | 110 #endif |
OLD | NEW |