OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 #include "sandbox/linux/services/thread_helpers.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> |
| 11 #include <unistd.h> |
| 12 |
| 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 "sandbox/linux/tests/unit_tests.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 using base::PlatformThread; |
| 24 |
| 25 namespace sandbox { |
| 26 |
| 27 namespace { |
| 28 |
| 29 int GetRaceTestIterations() { |
| 30 if (IsRunningOnValgrind()) { |
| 31 return 2; |
| 32 } else { |
| 33 return 1000; |
| 34 } |
| 35 } |
| 36 |
| 37 class ScopedProcSelfTask { |
| 38 public: |
| 39 ScopedProcSelfTask() : fd_(-1) { |
| 40 fd_ = open("/proc/self/task/", O_RDONLY | O_DIRECTORY); |
| 41 CHECK_LE(0, fd_); |
| 42 } |
| 43 |
| 44 ~ScopedProcSelfTask() { PCHECK(0 == IGNORE_EINTR(close(fd_))); } |
| 45 |
| 46 int fd() { return fd_; } |
| 47 |
| 48 private: |
| 49 int fd_; |
| 50 DISALLOW_COPY_AND_ASSIGN(ScopedProcSelfTask); |
| 51 }; |
| 52 |
| 53 TEST(ThreadHelpers, IsSingleThreadedBasic) { |
| 54 ScopedProcSelfTask task; |
| 55 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); |
| 56 |
| 57 base::Thread thread("sandbox_tests"); |
| 58 ASSERT_TRUE(thread.Start()); |
| 59 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); |
| 60 } |
| 61 |
| 62 TEST(ThreadHelpers, IsSingleThreadedIterated) { |
| 63 ScopedProcSelfTask task; |
| 64 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); |
| 65 |
| 66 // Iterate to check for race conditions. |
| 67 for (int i = 0; i < GetRaceTestIterations(); ++i) { |
| 68 base::Thread thread("sandbox_tests"); |
| 69 ASSERT_TRUE(thread.Start()); |
| 70 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); |
| 71 } |
| 72 } |
| 73 |
| 74 TEST(ThreadHelpers, IsSingleThreadedStartAndStop) { |
| 75 ScopedProcSelfTask task; |
| 76 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); |
| 77 |
| 78 base::Thread thread("sandbox_tests"); |
| 79 // This is testing for a race condition, so iterate. |
| 80 // Manually, this has been tested with more that 1M iterations. |
| 81 for (int i = 0; i < GetRaceTestIterations(); ++i) { |
| 82 ASSERT_TRUE(thread.Start()); |
| 83 ASSERT_FALSE(ThreadHelpers::IsSingleThreaded(task.fd())); |
| 84 |
| 85 ASSERT_TRUE(ThreadHelpers::StopThreadAndWatchProcFS(task.fd(), &thread)); |
| 86 ASSERT_TRUE(ThreadHelpers::IsSingleThreaded(task.fd())); |
| 87 ASSERT_EQ(1, base::GetNumberOfThreads(base::GetCurrentProcessHandle())); |
| 88 } |
| 89 } |
| 90 |
| 91 } // namespace |
| 92 |
| 93 } // namespace sandbox |
OLD | NEW |