OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sandbox/linux/services/thread_helpers.h" | 5 #include "sandbox/linux/services/thread_helpers.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 int fd_; | 52 int fd_; |
53 DISALLOW_COPY_AND_ASSIGN(ScopedProc); | 53 DISALLOW_COPY_AND_ASSIGN(ScopedProc); |
54 }; | 54 }; |
55 | 55 |
56 TEST(ThreadHelpers, IsSingleThreadedBasic) { | 56 TEST(ThreadHelpers, IsSingleThreadedBasic) { |
57 ScopedProc proc_fd; | 57 ScopedProc proc_fd; |
58 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); | 58 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded()); | 59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded()); |
60 | 60 |
61 base::Thread thread("sandbox_tests"); | 61 base::Thread thread("sandbox_tests"); |
62 ASSERT_TRUE(thread.Start()); | 62 ASSERT_TRUE(ThreadHelpers::StartThreadAndWatchProcFS(proc_fd.fd(), &thread)); |
63 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); | 63 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded()); | 64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded()); |
65 // Explicitly stop the thread here to not pollute the next test. | 65 // Explicitly stop the thread here to not pollute the next test. |
66 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread)); | 66 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread)); |
67 } | 67 } |
68 | 68 |
69 SANDBOX_TEST(ThreadHelpers, AssertSingleThreaded) { | 69 SANDBOX_TEST(ThreadHelpers, AssertSingleThreaded) { |
70 ScopedProc proc_fd; | 70 ScopedProc proc_fd; |
71 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); | 71 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); | 72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); |
73 | 73 |
74 ThreadHelpers::AssertSingleThreaded(proc_fd.fd()); | 74 ThreadHelpers::AssertSingleThreaded(proc_fd.fd()); |
75 ThreadHelpers::AssertSingleThreaded(); | 75 ThreadHelpers::AssertSingleThreaded(); |
76 } | 76 } |
77 | 77 |
78 TEST(ThreadHelpers, IsSingleThreadedIterated) { | 78 TEST(ThreadHelpers, IsSingleThreadedIterated) { |
79 ScopedProc proc_fd; | 79 ScopedProc proc_fd; |
80 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); | 80 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
81 | 81 |
82 // Iterate to check for race conditions. | 82 // Iterate to check for race conditions. |
83 for (int i = 0; i < GetRaceTestIterations(); ++i) { | 83 for (int i = 0; i < GetRaceTestIterations(); ++i) { |
84 base::Thread thread("sandbox_tests"); | 84 base::Thread thread("sandbox_tests"); |
85 ASSERT_TRUE(thread.Start()); | 85 ASSERT_TRUE( |
| 86 ThreadHelpers::StartThreadAndWatchProcFS(proc_fd.fd(), &thread)); |
86 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); | 87 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
87 // Explicitly stop the thread here to not pollute the next test. | 88 // Explicitly stop the thread here to not pollute the next test. |
88 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread)); | 89 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread)); |
89 } | 90 } |
90 } | 91 } |
91 | 92 |
92 TEST(ThreadHelpers, IsSingleThreadedStartAndStop) { | 93 TEST(ThreadHelpers, IsSingleThreadedStartAndStop) { |
93 ScopedProc proc_fd; | 94 ScopedProc proc_fd; |
94 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); | 95 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
95 | 96 |
96 base::Thread thread("sandbox_tests"); | 97 base::Thread thread("sandbox_tests"); |
97 // This is testing for a race condition, so iterate. | 98 // This is testing for a race condition, so iterate. |
98 // Manually, this has been tested with more that 1M iterations. | 99 // Manually, this has been tested with more that 1M iterations. |
99 for (int i = 0; i < GetRaceTestIterations(); ++i) { | 100 for (int i = 0; i < GetRaceTestIterations(); ++i) { |
100 ASSERT_TRUE(thread.Start()); | 101 ASSERT_TRUE( |
| 102 ThreadHelpers::StartThreadAndWatchProcFS(proc_fd.fd(), &thread)); |
101 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); | 103 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
102 | 104 |
103 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread)); | 105 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(proc_fd.fd(), &thread)); |
104 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); | 106 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(proc_fd.fd())); |
105 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); | 107 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); |
106 } | 108 } |
107 } | 109 } |
108 | 110 |
109 SANDBOX_TEST(ThreadHelpers, AssertSingleThreadedAfterThreadStopped) { | 111 SANDBOX_TEST(ThreadHelpers, AssertSingleThreadedAfterThreadStopped) { |
| 112 ScopedProc proc_fd; |
110 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); | 113 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); |
111 | 114 |
112 base::Thread thread1("sandbox_tests"); | 115 base::Thread thread1("sandbox_tests"); |
113 base::Thread thread2("sandbox_tests"); | 116 base::Thread thread2("sandbox_tests"); |
114 | 117 |
115 for (int i = 0; i < GetRaceTestIterations(); ++i) { | 118 for (int i = 0; i < GetRaceTestIterations(); ++i) { |
116 SANDBOX_ASSERT(thread1.Start()); | 119 SANDBOX_ASSERT( |
117 SANDBOX_ASSERT(thread2.Start()); | 120 ThreadHelpers::StartThreadAndWatchProcFS(proc_fd.fd(), &thread1)); |
| 121 SANDBOX_ASSERT( |
| 122 ThreadHelpers::StartThreadAndWatchProcFS(proc_fd.fd(), &thread2)); |
118 SANDBOX_ASSERT(!ThreadHelpers::IsSingleThreaded()); | 123 SANDBOX_ASSERT(!ThreadHelpers::IsSingleThreaded()); |
119 | 124 |
120 thread1.Stop(); | 125 thread1.Stop(); |
121 thread2.Stop(); | 126 thread2.Stop(); |
122 // This will wait on /proc/ to reflect the state of threads in the | 127 // This will wait on /proc/ to reflect the state of threads in the |
123 // process. | 128 // process. |
124 ThreadHelpers::AssertSingleThreaded(); | 129 ThreadHelpers::AssertSingleThreaded(); |
125 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); | 130 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded()); |
126 } | 131 } |
127 } | 132 } |
(...skipping 10 matching lines...) Expand all Loading... |
138 SANDBOX_ASSERT(thread1.Start()); | 143 SANDBOX_ASSERT(thread1.Start()); |
139 ThreadHelpers::AssertSingleThreaded(); | 144 ThreadHelpers::AssertSingleThreaded(); |
140 } | 145 } |
141 #endif // !defined(NDEBUG) | 146 #endif // !defined(NDEBUG) |
142 | 147 |
143 #endif // !defined(THREAD_SANITIZER) | 148 #endif // !defined(THREAD_SANITIZER) |
144 | 149 |
145 } // namespace | 150 } // namespace |
146 | 151 |
147 } // namespace sandbox | 152 } // namespace sandbox |
OLD | NEW |