Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / multi_process_lock_unittest.cc
blob95939de4f84898d79005e656ff4b0315edde3420
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/basictypes.h"
6 #include "base/environment.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/process/kill.h"
10 #include "base/rand_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/test/multiprocess_test.h"
13 #include "base/time/time.h"
14 #include "chrome/common/multi_process_lock.h"
15 #include "testing/multiprocess_func_list.h"
17 class MultiProcessLockTest : public base::MultiProcessTest {
18 public:
19 static const char kLockEnviromentVarName[];
21 class ScopedEnvironmentVariable {
22 public:
23 ScopedEnvironmentVariable(const std::string &name,
24 const std::string &value)
25 : name_(name), environment_(base::Environment::Create()) {
26 environment_->SetVar(name_.c_str(), value);
28 ~ScopedEnvironmentVariable() {
29 environment_->UnSetVar(name_.c_str());
32 private:
33 std::string name_;
34 scoped_ptr<base::Environment> environment_;
35 DISALLOW_COPY_AND_ASSIGN(ScopedEnvironmentVariable);
38 std::string GenerateLockName();
39 void ExpectLockIsLocked(const std::string &name);
40 void ExpectLockIsUnlocked(const std::string &name);
43 const char MultiProcessLockTest::kLockEnviromentVarName[]
44 = "MULTI_PROCESS_TEST_LOCK_NAME";
46 std::string MultiProcessLockTest::GenerateLockName() {
47 base::Time now = base::Time::NowFromSystemTime();
48 return base::StringPrintf("multi_process_test_lock %lf%lf",
49 now.ToDoubleT(), base::RandDouble());
52 void MultiProcessLockTest::ExpectLockIsLocked(const std::string &name) {
53 ScopedEnvironmentVariable var(kLockEnviromentVarName, name);
54 base::ProcessHandle handle = SpawnChild("MultiProcessLockTryFailMain", false);
55 ASSERT_TRUE(handle);
56 int exit_code = 0;
57 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
58 EXPECT_EQ(exit_code, 0);
61 void MultiProcessLockTest::ExpectLockIsUnlocked(
62 const std::string &name) {
63 ScopedEnvironmentVariable var(kLockEnviromentVarName, name);
64 base::ProcessHandle handle = SpawnChild("MultiProcessLockTrySucceedMain",
65 false);
66 ASSERT_TRUE(handle);
67 int exit_code = 0;
68 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code));
69 EXPECT_EQ(exit_code, 0);
72 TEST_F(MultiProcessLockTest, BasicCreationTest) {
73 // Test basic creation/destruction with no lock taken
74 std::string name = GenerateLockName();
75 scoped_ptr<MultiProcessLock> scoped(MultiProcessLock::Create(name));
76 ExpectLockIsUnlocked(name);
77 scoped.reset(NULL);
80 TEST_F(MultiProcessLockTest, LongNameTest) {
81 // Every platform has has it's own max path name size,
82 // so different checks are needed for them.
83 // POSIX: sizeof(address.sun_path) - 2
84 // Mac OS X: BOOTSTRAP_MAX_NAME_LEN
85 // Windows: MAX_PATH
86 LOG(INFO) << "Following error log due to long name is expected";
87 #if defined(OS_MACOSX)
88 std::string name("This is a name that is longer than one hundred and "
89 "twenty-eight characters to make sure that we fail appropriately on "
90 "Mac OS X when we have a path that is too long for Mac OS X to handle");
91 #elif defined(OS_POSIX)
92 std::string name("This is a name that is longer than one hundred and eight "
93 "characters to make sure that we fail appropriately on POSIX systems "
94 "when we have a path that is too long for the system to handle");
95 #elif defined(OS_WIN)
96 std::string name("This is a name that is longer than two hundred and sixty "
97 "characters to make sure that we fail appropriately on Windows when we "
98 "have a path that is too long for Windows to handle "
99 "This limitation comes from the MAX_PATH definition which is obviously "
100 "defined to be a maximum of two hundred and sixty characters ");
101 #endif
102 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(name));
103 EXPECT_FALSE(test_lock->TryLock());
106 TEST_F(MultiProcessLockTest, SimpleLock) {
107 std::string name = GenerateLockName();
108 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(name));
109 EXPECT_TRUE(test_lock->TryLock());
110 ExpectLockIsLocked(name);
111 test_lock->Unlock();
112 ExpectLockIsUnlocked(name);
115 TEST_F(MultiProcessLockTest, RecursiveLock) {
116 std::string name = GenerateLockName();
117 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(name));
118 EXPECT_TRUE(test_lock->TryLock());
119 ExpectLockIsLocked(name);
120 LOG(INFO) << "Following error log "
121 << "'MultiProcessLock is already locked' is expected";
122 EXPECT_TRUE(test_lock->TryLock());
123 ExpectLockIsLocked(name);
124 test_lock->Unlock();
125 ExpectLockIsUnlocked(name);
126 LOG(INFO) << "Following error log "
127 << "'Over-unlocked MultiProcessLock' is expected";
128 test_lock->Unlock();
129 ExpectLockIsUnlocked(name);
130 test_lock.reset();
133 TEST_F(MultiProcessLockTest, LockScope) {
134 // Check to see that lock is released when it goes out of scope.
135 std::string name = GenerateLockName();
137 scoped_ptr<MultiProcessLock> test_lock(MultiProcessLock::Create(name));
138 EXPECT_TRUE(test_lock->TryLock());
139 ExpectLockIsLocked(name);
141 ExpectLockIsUnlocked(name);
144 MULTIPROCESS_TEST_MAIN(MultiProcessLockTryFailMain) {
145 std::string name;
146 scoped_ptr<base::Environment> environment(base::Environment::Create());
147 EXPECT_TRUE(environment->GetVar(MultiProcessLockTest::kLockEnviromentVarName,
148 &name));
149 #if defined(OS_MACOSX)
150 // OS X sends out a log if a lock fails.
151 // Hopefully this will keep people from panicking about it when they
152 // are perusing the build logs.
153 LOG(INFO) << "Following error log "
154 << "\"CFMessagePort: bootstrap_register(): failed 1100 (0x44c) "
155 << "'Permission denied'\" is expected";
156 #endif // defined(OS_MACOSX)
157 scoped_ptr<MultiProcessLock> test_lock(
158 MultiProcessLock::Create(name));
159 EXPECT_FALSE(test_lock->TryLock());
160 return 0;
163 MULTIPROCESS_TEST_MAIN(MultiProcessLockTrySucceedMain) {
164 std::string name;
165 scoped_ptr<base::Environment> environment(base::Environment::Create());
166 EXPECT_TRUE(environment->GetVar(MultiProcessLockTest::kLockEnviromentVarName,
167 &name));
168 scoped_ptr<MultiProcessLock> test_lock(
169 MultiProcessLock::Create(name));
170 EXPECT_TRUE(test_lock->TryLock());
171 return 0;