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
3 // found in the LICENSE file.
5 #include "base/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/path_service.h"
8 #include "base/strings/string_util.h"
9 #include "build/build_config.h"
10 #include "chrome/browser/importer/firefox_profile_lock.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 class FirefoxProfileLockTest
: public testing::Test
{
16 virtual void SetUp() {
17 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
20 base::ScopedTempDir temp_dir_
;
23 TEST_F(FirefoxProfileLockTest
, LockTest
) {
24 FirefoxProfileLock
lock1(temp_dir_
.path());
25 ASSERT_TRUE(lock1
.HasAcquired());
27 ASSERT_FALSE(lock1
.HasAcquired());
29 ASSERT_TRUE(lock1
.HasAcquired());
32 // Tests basic functionality and verifies that the lock file is deleted after
34 TEST_F(FirefoxProfileLockTest
, ProfileLock
) {
35 base::FilePath test_path
= temp_dir_
.path();
36 base::FilePath lock_file_path
=
37 test_path
.Append(FirefoxProfileLock::kLockFileName
);
39 scoped_ptr
<FirefoxProfileLock
> lock
;
40 EXPECT_EQ(static_cast<FirefoxProfileLock
*>(NULL
), lock
.get());
41 EXPECT_FALSE(base::PathExists(lock_file_path
));
42 lock
.reset(new FirefoxProfileLock(test_path
));
43 EXPECT_TRUE(lock
->HasAcquired());
44 EXPECT_TRUE(base::PathExists(lock_file_path
));
46 EXPECT_FALSE(lock
->HasAcquired());
48 // In the posix code, we don't delete the file when releasing the lock.
49 #if !defined(OS_POSIX)
50 EXPECT_FALSE(base::PathExists(lock_file_path
));
51 #endif // !defined(OS_POSIX)
53 EXPECT_TRUE(lock
->HasAcquired());
54 EXPECT_TRUE(base::PathExists(lock_file_path
));
56 EXPECT_TRUE(lock
->HasAcquired());
58 EXPECT_FALSE(lock
->HasAcquired());
59 // In the posix code, we don't delete the file when releasing the lock.
60 #if !defined(OS_POSIX)
61 EXPECT_FALSE(base::PathExists(lock_file_path
));
62 #endif // !defined(OS_POSIX)
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.
67 TEST_F(FirefoxProfileLockTest
, ProfileLockOrphaned
) {
68 base::FilePath test_path
= temp_dir_
.path();
69 base::FilePath lock_file_path
=
70 test_path
.Append(FirefoxProfileLock::kLockFileName
);
72 // Create the orphaned lock file.
73 FILE* lock_file
= base::OpenFile(lock_file_path
, "w");
74 ASSERT_TRUE(lock_file
);
75 base::CloseFile(lock_file
);
76 EXPECT_TRUE(base::PathExists(lock_file_path
));
78 scoped_ptr
<FirefoxProfileLock
> lock
;
79 EXPECT_EQ(static_cast<FirefoxProfileLock
*>(NULL
), lock
.get());
80 lock
.reset(new FirefoxProfileLock(test_path
));
81 EXPECT_TRUE(lock
->HasAcquired());
83 EXPECT_FALSE(lock
->HasAcquired());
86 // This is broken on POSIX since the same process is allowed to reacquire a
88 #if !defined(OS_POSIX)
89 // Tests two locks contending for the same lock file.
90 TEST_F(FirefoxProfileLockTest
, ProfileLockContention
) {
91 base::FilePath test_path
= temp_dir_
.path();
93 scoped_ptr
<FirefoxProfileLock
> lock1
;
94 EXPECT_EQ(static_cast<FirefoxProfileLock
*>(NULL
), lock1
.get());
95 lock1
.reset(new FirefoxProfileLock(test_path
));
96 EXPECT_TRUE(lock1
->HasAcquired());
98 scoped_ptr
<FirefoxProfileLock
> lock2
;
99 EXPECT_EQ(static_cast<FirefoxProfileLock
*>(NULL
), lock2
.get());
100 lock2
.reset(new FirefoxProfileLock(test_path
));
101 EXPECT_FALSE(lock2
->HasAcquired());
104 EXPECT_FALSE(lock1
->HasAcquired());
107 EXPECT_TRUE(lock2
->HasAcquired());
109 EXPECT_FALSE(lock2
->HasAcquired());