Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / sandbox / linux / services / thread_helpers_unittests.cc
blob888651671d55f2cc17d457b362f22f8f715c4dda
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/basictypes.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/posix/eintr_wrapper.h"
17 #include "base/process/process_metrics.h"
18 #include "base/threading/platform_thread.h"
19 #include "base/threading/thread.h"
20 #include "build/build_config.h"
21 #include "sandbox/linux/tests/unit_tests.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using base::PlatformThread;
26 namespace sandbox {
28 namespace {
30 // These tests fail under ThreadSanitizer, see http://crbug.com/342305
31 #if !defined(THREAD_SANITIZER)
33 int GetRaceTestIterations() {
34 if (IsRunningOnValgrind()) {
35 return 2;
36 } else {
37 return 1000;
41 class ScopedProc {
42 public:
43 ScopedProc() : fd_(-1) {
44 fd_ = open("/proc/", O_RDONLY | O_DIRECTORY);
45 CHECK_LE(0, fd_);
48 ~ScopedProc() { PCHECK(0 == IGNORE_EINTR(close(fd_))); }
50 int fd() { return fd_; }
52 private:
53 int fd_;
54 DISALLOW_COPY_AND_ASSIGN(ScopedProc);
57 TEST(ThreadHelpers, IsSingleThreadedBasic) {
58 ScopedProc proc_fd;
59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
60 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded());
62 base::Thread thread("sandbox_tests");
63 ASSERT_TRUE(thread.Start());
64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
65 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded());
66 // Explicitly stop the thread here to not pollute the next test.
67 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread));
70 SANDBOX_TEST(ThreadHelpers, AssertSingleThreaded) {
71 ScopedProc proc_fd;
72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
73 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
75 ThreadHelpers::AssertSingleThreaded(proc_fd.fd());
76 ThreadHelpers::AssertSingleThreaded();
79 TEST(ThreadHelpers, IsSingleThreadedIterated) {
80 ScopedProc proc_fd;
81 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
83 // Iterate to check for race conditions.
84 for (int i = 0; i < GetRaceTestIterations(); ++i) {
85 base::Thread thread("sandbox_tests");
86 ASSERT_TRUE(thread.Start());
87 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
88 // Explicitly stop the thread here to not pollute the next test.
89 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread));
93 TEST(ThreadHelpers, IsSingleThreadedStartAndStop) {
94 ScopedProc proc_fd;
95 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
97 base::Thread thread("sandbox_tests");
98 // This is testing for a race condition, so iterate.
99 // Manually, this has been tested with more that 1M iterations.
100 for (int i = 0; i < GetRaceTestIterations(); ++i) {
101 ASSERT_TRUE(thread.Start());
102 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
104 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread));
105 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd()));
106 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
110 SANDBOX_TEST(ThreadHelpers, AssertSingleThreadedAfterThreadStopped) {
111 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
113 base::Thread thread1("sandbox_tests");
114 base::Thread thread2("sandbox_tests");
116 for (int i = 0; i < GetRaceTestIterations(); ++i) {
117 SANDBOX_ASSERT(thread1.Start());
118 SANDBOX_ASSERT(thread2.Start());
119 SANDBOX_ASSERT(!ThreadHelpers::IsSingleThreaded());
121 thread1.Stop();
122 thread2.Stop();
123 // This will wait on /proc/ to reflect the state of threads in the
124 // process.
125 ThreadHelpers::AssertSingleThreaded();
126 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded());
130 // Only run this test in Debug mode, where AssertSingleThreaded() will return
131 // in less than 64ms.
132 #if !defined(NDEBUG)
133 SANDBOX_DEATH_TEST(
134 ThreadHelpers,
135 AssertSingleThreadedDies,
136 DEATH_MESSAGE(
137 ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests())) {
138 base::Thread thread1("sandbox_tests");
139 SANDBOX_ASSERT(thread1.Start());
140 ThreadHelpers::AssertSingleThreaded();
142 #endif // !defined(NDEBUG)
144 #endif // !defined(THREAD_SANITIZER)
146 } // namespace
148 } // namespace sandbox