Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(512)

Side by Side Diff: sandbox/linux/services/thread_helpers_unittests.cc

Issue 893993004: Linux sandbox: Provide AssertSingleThreaded() helper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 ~ScopedProcSelfTask() { PCHECK(0 == IGNORE_EINTR(close(fd_))); } 45 ~ScopedProcSelfTask() { PCHECK(0 == IGNORE_EINTR(close(fd_))); }
46 46
47 int fd() { return fd_; } 47 int fd() { return fd_; }
48 48
49 private: 49 private:
50 int fd_; 50 int fd_;
51 DISALLOW_COPY_AND_ASSIGN(ScopedProcSelfTask); 51 DISALLOW_COPY_AND_ASSIGN(ScopedProcSelfTask);
52 }; 52 };
53 53
54 #if defined(THREAD_SANITIZER)
55 // These tests fail under ThreadSanitizer, see http://crbug.com/342305 54 // These tests fail under ThreadSanitizer, see http://crbug.com/342305
56 #define MAYBE_IsSingleThreadedBasic DISABLED_IsSingleThreadedBasic 55 #if !defined(THREAD_SANITIZER)
57 #define MAYBE_IsSingleThreadedIterated DISABLED_IsSingleThreadedIterated
58 #define MAYBE_IsSingleThreadedStartAndStop DISABLED_IsSingleThreadedStartAndStop
59 #else
60 #define MAYBE_IsSingleThreadedBasic IsSingleThreadedBasic
61 #define MAYBE_IsSingleThreadedIterated IsSingleThreadedIterated
62 #define MAYBE_IsSingleThreadedStartAndStop IsSingleThreadedStartAndStop
63 #endif
64 56
65 TEST(ThreadHelpers, MAYBE_IsSingleThreadedBasic) { 57 TEST(ThreadHelpers, IsSingleThreadedBasic) {
66 ScopedProcSelfTask task; 58 ScopedProcSelfTask task;
67 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); 59 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd()));
68 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(-1)); 60 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(-1));
69 61
70 base::Thread thread("sandbox_tests"); 62 base::Thread thread("sandbox_tests");
71 ASSERT_TRUE(thread.Start()); 63 ASSERT_TRUE(thread.Start());
72 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); 64 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd()));
73 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(-1)); 65 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(-1));
74 // Explicitly stop the thread here to not pollute the next test. 66 // Explicitly stop the thread here to not pollute the next test.
75 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread)); 67 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread));
76 } 68 }
77 69
78 TEST(ThreadHelpers, MAYBE_IsSingleThreadedIterated) { 70 SANDBOX_TEST(ThreadHelpers, AssertSingleThreaded) {
71 ScopedProcSelfTask task;
72 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(task.fd()));
73 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(-1));
74
75 ThreadHelpers::AssertSingleThreaded(task.fd());
76 ThreadHelpers::AssertSingleThreaded(-1);
77 }
78
79 TEST(ThreadHelpers, IsSingleThreadedIterated) {
79 ScopedProcSelfTask task; 80 ScopedProcSelfTask task;
80 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); 81 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd()));
81 82
82 // Iterate to check for race conditions. 83 // Iterate to check for race conditions.
83 for (int i = 0; i < GetRaceTestIterations(); ++i) { 84 for (int i = 0; i < GetRaceTestIterations(); ++i) {
84 base::Thread thread("sandbox_tests"); 85 base::Thread thread("sandbox_tests");
85 ASSERT_TRUE(thread.Start()); 86 ASSERT_TRUE(thread.Start());
86 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); 87 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.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(task.fd(), &thread)); 89 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread));
89 } 90 }
90 } 91 }
91 92
92 TEST(ThreadHelpers, MAYBE_IsSingleThreadedStartAndStop) { 93 TEST(ThreadHelpers, IsSingleThreadedStartAndStop) {
93 ScopedProcSelfTask task; 94 ScopedProcSelfTask task;
94 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); 95 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.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(thread.Start());
101 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); 102 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd()));
102 103
103 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread)); 104 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread));
104 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); 105 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd()));
105 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); 106 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle()));
106 } 107 }
107 } 108 }
108 109
110 SANDBOX_TEST(ThreadHelpers, AssertSingleThreadedAfterThreadStopped) {
111 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(-1));
112
113 base::Thread thread1("sandbox_tests");
114 base::Thread thread2("sandbox_tests");
115
116 for (int i = 0; i < GetRaceTestIterations(); ++i) {
117 SANDBOX_ASSERT(thread1.Start());
118 SANDBOX_ASSERT(thread2.Start());
119 SANDBOX_ASSERT(!ThreadHelpers::IsSingleThreaded(-1));
120
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(-1);
126 SANDBOX_ASSERT(ThreadHelpers::IsSingleThreaded(-1));
127 }
128 }
129
130 SANDBOX_DEATH_TEST(
131 ThreadHelpers,
132 AssertSingleThreadedDies,
133 DEATH_MESSAGE(
134 ThreadHelpers::GetAssertSingleThreadedErrorMessageForTests())) {
135 base::Thread thread1("sandbox_tests");
136 SANDBOX_ASSERT(thread1.Start());
137 ThreadHelpers::AssertSingleThreaded(-1);
138 }
139
140 #endif // !defined(THREAD_SANITIZER)
141
109 } // namespace 142 } // namespace
110 143
111 } // namespace sandbox 144 } // namespace sandbox
OLDNEW
« sandbox/linux/services/thread_helpers.cc ('K') | « sandbox/linux/services/thread_helpers.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698