Home | History | Annotate | Download | only in importer
      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.
      4 
      5 #include "base/files/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"
     13 
     14 class FirefoxProfileLockTest : public testing::Test {
     15  protected:
     16   virtual void SetUp() {
     17     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
     18   }
     19 
     20   base::ScopedTempDir temp_dir_;
     21 };
     22 
     23 TEST_F(FirefoxProfileLockTest, LockTest) {
     24   FirefoxProfileLock lock1(temp_dir_.path());
     25   ASSERT_TRUE(lock1.HasAcquired());
     26   lock1.Unlock();
     27   ASSERT_FALSE(lock1.HasAcquired());
     28   lock1.Lock();
     29   ASSERT_TRUE(lock1.HasAcquired());
     30 }
     31 
     32 // Tests basic functionality and verifies that the lock file is deleted after
     33 // use.
     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);
     38 
     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));
     45   lock->Unlock();
     46   EXPECT_FALSE(lock->HasAcquired());
     47 
     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)
     52   lock->Lock();
     53   EXPECT_TRUE(lock->HasAcquired());
     54   EXPECT_TRUE(base::PathExists(lock_file_path));
     55   lock->Lock();
     56   EXPECT_TRUE(lock->HasAcquired());
     57   lock->Unlock();
     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)
     63 }
     64 
     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);
     71 
     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));
     77 
     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());
     82   lock->Unlock();
     83   EXPECT_FALSE(lock->HasAcquired());
     84 }
     85 
     86 // This is broken on POSIX since the same process is allowed to reacquire a
     87 // lock.
     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();
     92 
     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());
     97 
     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());
    102 
    103   lock1->Unlock();
    104   EXPECT_FALSE(lock1->HasAcquired());
    105 
    106   lock2->Lock();
    107   EXPECT_TRUE(lock2->HasAcquired());
    108   lock2->Unlock();
    109   EXPECT_FALSE(lock2->HasAcquired());
    110 }
    111 #endif
    112