Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / sandbox / linux / services / thread_helpers_unittests.cc
blob7357a0cfa7cd24dd3814c9a8f1803e4d669ae9a8
1 // Copyright 2014 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 "sandbox/linux/services/thread_helpers.h"
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/process/process_metrics.h"
17 #include "base/threading/platform_thread.h"
18 #include "base/threading/thread.h"
19 #include "build/build_config.h"
20 #include "sandbox/linux/tests/unit_tests.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using base::PlatformThread;
25 namespace sandbox {
27 namespace {
29 // These tests fail under ThreadSanitizer, see http://crbug.com/342305
30 #if !defined(THREAD_SANITIZER)
32 int GetRaceTestIterations() {
33 if (IsRunningOnValgrind()) {
34 return 2;
35 } else {
36 return 1000;
40 class ScopedProc {
41 public:
42 ScopedProc() : fd_(-1) {
43 fd_ = open("/proc/", O_RDONLY | O_DIRECTORY);
44 CHECK_LE(0, fd_);
47 ~ScopedProc() { PCHECK(0 == IGNORE_EINTR(close(fd_))); }
49 int fd() { return fd_; }
51 private:
52 int fd_;
53 DISALLOW_COPY_AND_ASSIGN(ScopedProc);
56 TEST(ThreadHelpers, IsSingleThreadedBasic) {
57 ScopedProc proc_fd;
58 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded());
61 base::Thread thread("sandbox_tests");
62 ASSERT_TRUE(thread.Start());
63 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded());
65 // Explicitly stop the thread here to not pollute the next test.
66 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread));
69 SANDBOX_TEST(ThreadHelpers, AssertSingleThreaded) {
70 ScopedProc proc_fd;
71 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
74 ThreadHelpers::AssertSingleThreaded(proc_fd.fd());
75 ThreadHelpers::AssertSingleThreaded();
78 TEST(ThreadHelpers, IsSingleThreadedIterated) {
79 ScopedProc proc_fd;
80 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
82 // Iterate to check for race conditions.
83 for (int i = 0; i < GetRaceTestIterations(); ++i) {
84 base::Thread thread("sandbox_tests");
85 ASSERT_TRUE(thread.Start());
86 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
87 // Explicitly stop the thread here to not pollute the next test.
88 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread));
92 TEST(ThreadHelpers, IsSingleThreadedStartAndStop) {
93 ScopedProc proc_fd;
94 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
96 base::Thread thread("sandbox_tests");
97 // This is testing for a race condition, so iterate.
98 // Manually, this has been tested with more that 1M iterations.
99 for (int i = 0; i < GetRaceTestIterations(); ++i) {
100 ASSERT_TRUE(thread.Start());
101 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
103 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread));
104 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
105 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
109 SANDBOX_TEST(ThreadHelpers, AssertSingleThreadedAfterThreadStopped) {
110 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
112 base::Thread thread1("sandbox_tests");
113 base::Thread thread2("sandbox_tests");
115 for (int i = 0; i < GetRaceTestIterations(); ++i) {
116 SANDBOX_ASSERT(thread1.Start());
117 SANDBOX_ASSERT(thread2.Start());
118 SANDBOX_ASSERT(!ThreadHelpers::IsSingleThreaded());
120 thread1.Stop();
121 thread2.Stop();
122 // This will wait on /proc/ to reflect the state of threads in the
123 // process.
124 ThreadHelpers::AssertSingleThreaded();
125 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
129 // Only run this test in Debug mode, where AssertSingleThreaded() will return
130 // in less than 64ms.
131 #if !defined(NDEBUG)
132 SANDBOX_DEATH_TEST(
133 ThreadHelpers,
134 AssertSingleThreadedDies,
135 DEATH_MESSAGE(
136 ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests())) {
137 base::Thread thread1("sandbox_tests");
138 SANDBOX_ASSERT(thread1.Start());
139 ThreadHelpers::AssertSingleThreaded();
141 #endif // !defined(NDEBUG)
143 #endif // !defined(THREAD_SANITIZER)
145 } // namespace
147 } // namespace sandbox