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/files/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/strings/string_util.h"
8 #include "build/build_config.h"
9 #include "chrome/browser/importer/firefox_profile_lock.h"
10 #include "chrome/common/chrome_paths.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 class FirefoxProfileLockTest
: public testing::Test
{
15 void SetUp() override
{ ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir()); }
17 base::ScopedTempDir temp_dir_
;
20 TEST_F(FirefoxProfileLockTest
, LockTest
) {
21 FirefoxProfileLock
lock1(temp_dir_
.path());
22 ASSERT_TRUE(lock1
.HasAcquired());
24 ASSERT_FALSE(lock1
.HasAcquired());
26 ASSERT_TRUE(lock1
.HasAcquired());
29 // Tests basic functionality and verifies that the lock file is deleted after
31 TEST_F(FirefoxProfileLockTest
, ProfileLock
) {
32 base::FilePath test_path
= temp_dir_
.path();
33 base::FilePath lock_file_path
=
34 test_path
.Append(FirefoxProfileLock::kLockFileName
);
36 scoped_ptr
<FirefoxProfileLock
> lock
;
37 EXPECT_EQ(static_cast<FirefoxProfileLock
*>(NULL
), lock
.get());
38 EXPECT_FALSE(base::PathExists(lock_file_path
));
39 lock
.reset(new FirefoxProfileLock(test_path
));
40 EXPECT_TRUE(lock
->HasAcquired());
41 EXPECT_TRUE(base::PathExists(lock_file_path
));
43 EXPECT_FALSE(lock
->HasAcquired());
45 // In the posix code, we don't delete the file when releasing the lock.
46 #if !defined(OS_POSIX)
47 EXPECT_FALSE(base::PathExists(lock_file_path
));
48 #endif // !defined(OS_POSIX)
50 EXPECT_TRUE(lock
->HasAcquired());
51 EXPECT_TRUE(base::PathExists(lock_file_path
));
53 EXPECT_TRUE(lock
->HasAcquired());
55 EXPECT_FALSE(lock
->HasAcquired());
56 // In the posix code, we don't delete the file when releasing the lock.
57 #if !defined(OS_POSIX)
58 EXPECT_FALSE(base::PathExists(lock_file_path
));
59 #endif // !defined(OS_POSIX)
62 // If for some reason the lock file is left behind by the previous owner, we
63 // should still be able to lock it, at least in the Windows implementation.
64 TEST_F(FirefoxProfileLockTest
, ProfileLockOrphaned
) {
65 base::FilePath test_path
= temp_dir_
.path();
66 base::FilePath lock_file_path
=
67 test_path
.Append(FirefoxProfileLock::kLockFileName
);
69 // Create the orphaned lock file.
70 FILE* lock_file
= base::OpenFile(lock_file_path
, "w");
71 ASSERT_TRUE(lock_file
);
72 base::CloseFile(lock_file
);
73 EXPECT_TRUE(base::PathExists(lock_file_path
));
75 scoped_ptr
<FirefoxProfileLock
> lock
;
76 EXPECT_EQ(static_cast<FirefoxProfileLock
*>(NULL
), lock
.get());
77 lock
.reset(new FirefoxProfileLock(test_path
));
78 EXPECT_TRUE(lock
->HasAcquired());
80 EXPECT_FALSE(lock
->HasAcquired());
83 // This is broken on POSIX since the same process is allowed to reacquire a
85 #if !defined(OS_POSIX)
86 // Tests two locks contending for the same lock file.
87 TEST_F(FirefoxProfileLockTest
, ProfileLockContention
) {
88 base::FilePath test_path
= temp_dir_
.path();
90 scoped_ptr
<FirefoxProfileLock
> lock1
;
91 EXPECT_EQ(static_cast<FirefoxProfileLock
*>(NULL
), lock1
.get());
92 lock1
.reset(new FirefoxProfileLock(test_path
));
93 EXPECT_TRUE(lock1
->HasAcquired());
95 scoped_ptr
<FirefoxProfileLock
> lock2
;
96 EXPECT_EQ(static_cast<FirefoxProfileLock
*>(NULL
), lock2
.get());
97 lock2
.reset(new FirefoxProfileLock(test_path
));
98 EXPECT_FALSE(lock2
->HasAcquired());
101 EXPECT_FALSE(lock1
->HasAcquired());
104 EXPECT_TRUE(lock2
->HasAcquired());
106 EXPECT_FALSE(lock2
->HasAcquired());