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

Side by Side Diff: sandbox/linux/tests/unit_tests.cc

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 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 (c) 2012 The Chromium Authors. All rights reserved. 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 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 <fcntl.h> 5 #include <fcntl.h>
6 #include <poll.h> 6 #include <poll.h>
7 #include <signal.h> 7 #include <signal.h>
8 #include <stdio.h> 8 #include <stdio.h>
9 #include <sys/resource.h> 9 #include <sys/resource.h>
10 #include <sys/types.h>
11 #include <sys/wait.h>
10 #include <sys/time.h> 12 #include <sys/time.h>
11 #include <time.h> 13 #include <time.h>
12 #include <unistd.h> 14 #include <unistd.h>
13 15
14 #include "base/debug/leak_annotations.h" 16 #include "base/debug/leak_annotations.h"
15 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
16 #include "base/posix/eintr_wrapper.h" 18 #include "base/posix/eintr_wrapper.h"
17 #include "base/third_party/valgrind/valgrind.h" 19 #include "base/third_party/valgrind/valgrind.h"
18 #include "build/build_config.h" 20 #include "build/build_config.h"
19 #include "sandbox/linux/tests/unit_tests.h" 21 #include "sandbox/linux/tests/unit_tests.h"
20 22
23 // Specifically, PNaCl toolchain does not have this flag.
24 #if !defined(POLLRDHUP)
25 #define POLLRDHUP 0x2000
26 #endif
27
21 namespace { 28 namespace {
22 std::string TestFailedMessage(const std::string& msg) { 29 std::string TestFailedMessage(const std::string& msg) {
23 return msg.empty() ? std::string() : "Actual test failure: " + msg; 30 return msg.empty() ? std::string() : "Actual test failure: " + msg;
24 } 31 }
25 32
26 int GetSubProcessTimeoutTimeInSeconds() { 33 int GetSubProcessTimeoutTimeInSeconds() {
27 // 10s ought to be enough for anybody. 34 // Previously 10s, but that timed out (just) on Chromecast.
28 return 10; 35 return 12;
29 } 36 }
30 37
31 // Returns the number of threads of the current process or -1. 38 // Returns the number of threads of the current process or -1.
32 int CountThreads() { 39 int CountThreads() {
33 struct stat task_stat; 40 struct stat task_stat;
34 int task_d = stat("/proc/self/task", &task_stat); 41 int task_d = stat("/proc/self/task", &task_stat);
35 // task_stat.st_nlink should be the number of tasks + 2 (accounting for 42 // task_stat.st_nlink should be the number of tasks + 2 (accounting for
36 // "." and "..". 43 // "." and "..".
37 if (task_d != 0 || task_stat.st_nlink < 3) 44 if (task_d != 0 || task_stat.st_nlink < 3)
38 return -1; 45 return -1;
(...skipping 21 matching lines...) Expand all
60 #endif 67 #endif
61 } 68 }
62 69
63 // TODO(jln): figure out why base/.../dynamic_annotations.h's 70 // TODO(jln): figure out why base/.../dynamic_annotations.h's
64 // RunningOnValgrind() cannot link. 71 // RunningOnValgrind() cannot link.
65 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; } 72 bool IsRunningOnValgrind() { return RUNNING_ON_VALGRIND; }
66 73
67 static const int kExpectedValue = 42; 74 static const int kExpectedValue = 42;
68 static const int kIgnoreThisTest = 43; 75 static const int kIgnoreThisTest = 43;
69 static const int kExitWithAssertionFailure = 1; 76 static const int kExitWithAssertionFailure = 1;
77 #if !defined(OS_NACL_NONSFI)
70 static const int kExitForTimeout = 2; 78 static const int kExitForTimeout = 2;
79 #endif
71 80
72 #if !defined(OS_ANDROID) 81 #if defined(SANDBOX_USES_BASE_TEST_SUITE)
73 // This is due to StackDumpSignalHandler() performing _exit(1). 82 // This is due to StackDumpSignalHandler() performing _exit(1).
74 // TODO(jln): get rid of the collision with kExitWithAssertionFailure. 83 // TODO(jln): get rid of the collision with kExitWithAssertionFailure.
75 const int kExitAfterSIGSEGV = 1; 84 const int kExitAfterSIGSEGV = 1;
76 #endif 85 #endif
77 86
87 // PNaCl toolchain's signal ABIs are incompatible with Linux's.
88 // So, for simplicity, just drop the "timeout" feature from unittest framework
89 // with relying on the buildbot's timeout feature.
90 #if !defined(OS_NACL_NONSFI)
78 static void SigAlrmHandler(int) { 91 static void SigAlrmHandler(int) {
79 const char failure_message[] = "Timeout reached!\n"; 92 const char failure_message[] = "Timeout reached!\n";
80 // Make sure that we never block here. 93 // Make sure that we never block here.
81 if (!fcntl(2, F_SETFL, O_NONBLOCK)) { 94 if (!fcntl(2, F_SETFL, O_NONBLOCK)) {
82 ignore_result(write(2, failure_message, sizeof(failure_message) - 1)); 95 ignore_result(write(2, failure_message, sizeof(failure_message) - 1));
83 } 96 }
84 _exit(kExitForTimeout); 97 _exit(kExitForTimeout);
85 } 98 }
86 99
87 // Set a timeout with a handler that will automatically fail the 100 // Set a timeout with a handler that will automatically fail the
(...skipping 11 matching lines...) Expand all
99 // is expecting to handle SIGALRM. 112 // is expecting to handle SIGALRM.
100 SANDBOX_ASSERT((old_act.sa_flags & SA_SIGINFO) == 0); 113 SANDBOX_ASSERT((old_act.sa_flags & SA_SIGINFO) == 0);
101 SANDBOX_ASSERT(old_act.sa_handler == SIG_DFL); 114 SANDBOX_ASSERT(old_act.sa_handler == SIG_DFL);
102 sigset_t sigalrm_set; 115 sigset_t sigalrm_set;
103 SANDBOX_ASSERT(sigemptyset(&sigalrm_set) == 0); 116 SANDBOX_ASSERT(sigemptyset(&sigalrm_set) == 0);
104 SANDBOX_ASSERT(sigaddset(&sigalrm_set, SIGALRM) == 0); 117 SANDBOX_ASSERT(sigaddset(&sigalrm_set, SIGALRM) == 0);
105 SANDBOX_ASSERT(sigprocmask(SIG_UNBLOCK, &sigalrm_set, NULL) == 0); 118 SANDBOX_ASSERT(sigprocmask(SIG_UNBLOCK, &sigalrm_set, NULL) == 0);
106 SANDBOX_ASSERT(alarm(time_in_seconds) == 0); // There should be no previous 119 SANDBOX_ASSERT(alarm(time_in_seconds) == 0); // There should be no previous
107 // alarm. 120 // alarm.
108 } 121 }
122 #endif // !defined(OS_NACL_NONSFI)
109 123
110 // Runs a test in a sub-process. This is necessary for most of the code 124 // Runs a test in a sub-process. This is necessary for most of the code
111 // in the BPF sandbox, as it potentially makes global state changes and as 125 // in the BPF sandbox, as it potentially makes global state changes and as
112 // it also tends to raise fatal errors, if the code has been used in an 126 // it also tends to raise fatal errors, if the code has been used in an
113 // insecure manner. 127 // insecure manner.
114 void UnitTests::RunTestInProcess(SandboxTestRunner* test_runner, 128 void UnitTests::RunTestInProcess(SandboxTestRunner* test_runner,
115 DeathCheck death, 129 DeathCheck death,
116 const void* death_aux) { 130 const void* death_aux) {
117 CHECK(test_runner); 131 CHECK(test_runner);
118 // We need to fork(), so we can't be multi-threaded, as threads could hold 132 // We need to fork(), so we can't be multi-threaded, as threads could hold
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 // In child process 170 // In child process
157 // Redirect stderr to our pipe. This way, we can capture all error 171 // Redirect stderr to our pipe. This way, we can capture all error
158 // messages, if we decide we want to do so in our tests. 172 // messages, if we decide we want to do so in our tests.
159 SANDBOX_ASSERT(dup2(fds[1], 2) == 2); 173 SANDBOX_ASSERT(dup2(fds[1], 2) == 2);
160 SANDBOX_ASSERT(!close(fds[0])); 174 SANDBOX_ASSERT(!close(fds[0]));
161 SANDBOX_ASSERT(!close(fds[1])); 175 SANDBOX_ASSERT(!close(fds[1]));
162 176
163 // Don't set a timeout if running on Valgrind, since it's generally much 177 // Don't set a timeout if running on Valgrind, since it's generally much
164 // slower. 178 // slower.
165 if (!IsRunningOnValgrind()) { 179 if (!IsRunningOnValgrind()) {
180 #if !defined(OS_NACL_NONSFI)
166 SetProcessTimeout(GetSubProcessTimeoutTimeInSeconds()); 181 SetProcessTimeout(GetSubProcessTimeoutTimeInSeconds());
182 #endif
167 } 183 }
168 184
169 // Disable core files. They are not very useful for our individual test 185 // Disable core files. They are not very useful for our individual test
170 // cases. 186 // cases.
171 struct rlimit no_core = {0}; 187 struct rlimit no_core = {0};
172 setrlimit(RLIMIT_CORE, &no_core); 188 setrlimit(RLIMIT_CORE, &no_core);
173 189
174 test_runner->Run(); 190 test_runner->Run();
175 if (test_runner->ShouldCheckForLeaks()) { 191 if (test_runner->ShouldCheckForLeaks()) {
176 #if defined(LEAK_SANITIZER) 192 #if defined(LEAK_SANITIZER)
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 #endif 291 #endif
276 EXPECT_FALSE(subprocess_exited_without_matching_message) << details; 292 EXPECT_FALSE(subprocess_exited_without_matching_message) << details;
277 } 293 }
278 294
279 void UnitTests::DeathSEGVMessage(int status, 295 void UnitTests::DeathSEGVMessage(int status,
280 const std::string& msg, 296 const std::string& msg,
281 const void* aux) { 297 const void* aux) {
282 std::string details(TestFailedMessage(msg)); 298 std::string details(TestFailedMessage(msg));
283 const char* expected_msg = static_cast<const char*>(aux); 299 const char* expected_msg = static_cast<const char*>(aux);
284 300
285 #if defined(OS_ANDROID) 301 #if !defined(SANDBOX_USES_BASE_TEST_SUITE)
286 const bool subprocess_got_sigsegv = 302 const bool subprocess_got_sigsegv =
287 WIFSIGNALED(status) && (SIGSEGV == WTERMSIG(status)); 303 WIFSIGNALED(status) && (SIGSEGV == WTERMSIG(status));
288 #else 304 #else
305 // This hack is required when a signal handler is installed
306 // for SEGV that will _exit(1).
289 const bool subprocess_got_sigsegv = 307 const bool subprocess_got_sigsegv =
290 WIFEXITED(status) && (kExitAfterSIGSEGV == WEXITSTATUS(status)); 308 WIFEXITED(status) && (kExitAfterSIGSEGV == WEXITSTATUS(status));
291 #endif 309 #endif
292 310
293 ASSERT_TRUE(subprocess_got_sigsegv) << "Exit status: " << status 311 ASSERT_TRUE(subprocess_got_sigsegv) << "Exit status: " << status
294 << " " << details; 312 << " " << details;
295 313
296 bool subprocess_exited_without_matching_message = 314 bool subprocess_exited_without_matching_message =
297 msg.find(expected_msg) == std::string::npos; 315 msg.find(expected_msg) == std::string::npos;
298 EXPECT_FALSE(subprocess_exited_without_matching_message) << details; 316 EXPECT_FALSE(subprocess_exited_without_matching_message) << details;
(...skipping 28 matching lines...) Expand all
327 fflush(stderr); 345 fflush(stderr);
328 _exit(kExitWithAssertionFailure); 346 _exit(kExitWithAssertionFailure);
329 } 347 }
330 348
331 void UnitTests::IgnoreThisTest() { 349 void UnitTests::IgnoreThisTest() {
332 fflush(stderr); 350 fflush(stderr);
333 _exit(kIgnoreThisTest); 351 _exit(kIgnoreThisTest);
334 } 352 }
335 353
336 } // namespace 354 } // namespace
OLDNEW
« no previous file with comments | « sandbox/linux/tests/main.cc ('k') | services/url_response_disk_cache/url_response_disk_cache_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698