Skip to content

Commit

Permalink
Complete lock behavior and add unittest
Browse files Browse the repository at this point in the history
Auditor: @garrettr
  • Loading branch information
darkdh committed May 15, 2018
1 parent 1f41916 commit 3e68ed7
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
13 changes: 9 additions & 4 deletions browser/importer/chrome_profile_lock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
ChromeProfileLock::ChromeProfileLock(
const base::FilePath& user_data_dir)
: lock_acquired_(false),
process_singleton_(user_data_dir,
user_data_dir_(user_data_dir),
process_singleton_(new ProcessSingleton(user_data_dir,
base::Bind(&ChromeProfileLock::NotificationCallback,
base::Unretained(this))) {
base::Unretained(this)))) {
Lock();
}

Expand All @@ -23,13 +24,17 @@ ChromeProfileLock::~ChromeProfileLock() {
void ChromeProfileLock::Lock() {
if (HasAcquired())
return;
lock_acquired_ = process_singleton_.Create();
lock_acquired_ = process_singleton_->Create();
}

void ChromeProfileLock::Unlock() {
if (!HasAcquired())
return;
process_singleton_.Cleanup();
process_singleton_->Cleanup();
process_singleton_.reset(new ProcessSingleton(user_data_dir_,
base::Bind(&ChromeProfileLock::NotificationCallback,
base::Unretained(this))));
lock_acquired_ = false;
}

bool ChromeProfileLock::HasAcquired() {
Expand Down
3 changes: 2 additions & 1 deletion browser/importer/chrome_profile_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class ChromeProfileLock {

private:
bool lock_acquired_;
ProcessSingleton process_singleton_;
base::FilePath user_data_dir_;
std::unique_ptr<ProcessSingleton> process_singleton_;

bool NotificationCallback(const base::CommandLine& command_line,
const base::FilePath& current_directory);
Expand Down
110 changes: 110 additions & 0 deletions browser/importer/chrome_profile_lock_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/importer/chrome_profile_lock.h"

#if defined(OS_POSIX)
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

#include <memory>

#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/string_util.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "testing/gtest/include/gtest/gtest.h"

#if defined(OS_WIN)
const char kLockFile[] = "lockfile";
#endif

class ChromeProfileLockTest : public testing::Test {
public:
ChromeProfileLockTest ()
: task_runner_(base::MakeRefCounted<base::TestSimpleTaskRunner>()),
thread_task_runner_handle_override_(
base::ThreadTaskRunnerHandle::OverrideForTesting(task_runner_)) {}
~ChromeProfileLockTest () override {
task_runner_->RunUntilIdle();
}
protected:
void SetUp() override {
testing::Test::SetUp();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
user_data_path_ = temp_dir_.GetPath();
#if defined(OS_POSIX)
lock_file_path_ =
user_data_path_.Append(chrome::kSingletonLockFilename);
#elif defined(OS_WIN)
lock_file_path_ = user_data_path_.AppendASCII(kLockFile);
#else
#error unsupport platforms
#endif
}

void LockFileExists(bool expect);

base::ScopedTempDir temp_dir_;
base::FilePath user_data_path_;
base::FilePath lock_file_path_;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
base::ScopedClosureRunner thread_task_runner_handle_override_;
};

void ChromeProfileLockTest::LockFileExists(bool expect) {
#if defined(OS_POSIX)
struct stat statbuf;
if (expect) {
ASSERT_EQ(0, lstat(lock_file_path_.value().c_str(), &statbuf));
ASSERT_TRUE(S_ISLNK(statbuf.st_mode));
char buf[PATH_MAX];
ssize_t len = readlink(lock_file_path_.value().c_str(), buf, PATH_MAX);
ASSERT_GT(len, 0);
} else {
ASSERT_GT(0, lstat(lock_file_path_.value().c_str(), &statbuf));
}
#elif defined(OS_WIN)
if (expect)
EXPECT_TRUE(base::PathExists(lock_file_path_));
else
EXPECT_FALSE(base::PathExists(lock_file_path_));
#endif
}

TEST_F(ChromeProfileLockTest, LockTest) {
ChromeProfileLock lock(user_data_path_);
ASSERT_TRUE(lock.HasAcquired());
lock.Unlock();
ASSERT_FALSE(lock.HasAcquired());
lock.Lock();
ASSERT_TRUE(lock.HasAcquired());
}

// Tests basic functionality and verifies that the lock file is deleted after
// use.
TEST_F(ChromeProfileLockTest, ProfileLock) {
std::unique_ptr<ChromeProfileLock> lock;
EXPECT_EQ(static_cast<ChromeProfileLock*>(NULL), lock.get());
LockFileExists(false);
lock.reset(new ChromeProfileLock(user_data_path_));
EXPECT_TRUE(lock->HasAcquired());
LockFileExists(true);
lock->Unlock();
EXPECT_FALSE(lock->HasAcquired());

lock->Lock();
EXPECT_TRUE(lock->HasAcquired());
LockFileExists(true);
lock->Lock();
EXPECT_TRUE(lock->HasAcquired());
lock->Unlock();
EXPECT_FALSE(lock->HasAcquired());
}
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ test("brave_unit_tests") {
"//brave/common/importer/brave_mock_importer_bridge.h",
"//chrome/common/importer/mock_importer_bridge.cc",
"//chrome/common/importer/mock_importer_bridge.h",
"../browser/importer/chrome_profile_lock_unittest.cc",
"../utility/importer/chrome_importer_unittest.cc",
]

Expand Down

0 comments on commit 3e68ed7

Please sign in to comment.