OLD | NEW |
---|---|
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 "testing/gtest/include/gtest/gtest.h" | 5 #include <stdio.h> |
6 int main(int argc, char *argv[]) { | 6 #include <sys/resource.h> |
7 testing::InitGoogleTest(&argc, argv); | 7 #include <sys/time.h> |
8 // Always go through re-execution for death tests. | 8 |
9 // This makes gtest only marginally slower for us and has the | 9 #include "base/file_util.h" |
10 // additional side effect of getting rid of gtest warnings about fork() | 10 #include "sandbox/linux/tests/unit_tests.h" |
11 // safety. | 11 |
12 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; | 12 namespace sandbox { |
13 return RUN_ALL_TESTS(); | 13 |
14 void UnitTests::RunTestInProcess(UnitTests::Test test) { | |
15 // Runs a test in a sub-process. This is necessary for most of the code | |
16 // in the BPF sandbox, as it potentially makes global state changes and as | |
17 // it also tends to raise fatal errors, if the code has been used in an | |
18 // insecure manner. | |
19 int fds[2]; | |
20 ASSERT_EQ(pipe(fds), 0); | |
21 | |
22 pid_t pid; | |
23 ASSERT_GE((pid = fork()), 0); | |
24 if (!pid) { | |
25 // In child process | |
26 // Redirect stderr to our pipe. This way, we can capture all error | |
27 // messages, if we decide we want to do so in our tests. | |
28 SANDBOX_ASSERT(dup2(fds[1], 2) == 2); | |
29 SANDBOX_ASSERT(!close(fds[0])); | |
30 SANDBOX_ASSERT(!close(fds[1])); | |
31 | |
32 // Disable core files. They are not very useful for our individual test | |
33 // cases. | |
34 struct rlimit no_core = { 0 }; | |
35 setrlimit(RLIMIT_CORE, &no_core); | |
36 | |
37 SANDBOX_ASSERT(test()); | |
38 _exit(1); | |
39 } | |
40 | |
41 HANDLE_EINTR(close(fds[1])); | |
42 std::vector<char> msg; | |
43 ssize_t rc; | |
44 do { | |
45 const int kCapacity = 256; | |
jln (very slow on Chromium)
2012/08/23 22:53:09
I think we want:
1. To make msg a std::string
2.
| |
46 size_t len = msg.size(); | |
47 msg.resize(len + kCapacity); | |
48 rc = HANDLE_EINTR(read(fds[0], &msg[len], kCapacity)); | |
49 msg.resize(len + std::max(rc, static_cast<ssize_t>(0))); | |
50 } while (rc > 0); | |
51 HANDLE_EINTR(close(fds[0])); | |
52 EXPECT_EQ(msg.size(), 0u) << std::string(msg.begin(), msg.end()); | |
53 | |
54 int status; | |
55 HANDLE_EINTR(waitpid(pid, &status, 0)); | |
jln (very slow on Chromium)
2012/08/23 22:53:09
We should check for error here. Maybe RunTestInPro
| |
14 } | 56 } |
57 | |
58 void UnitTests::AssertionFailure(const char *expr, const char *file, | |
59 int line) { | |
60 fprintf(stderr, "%s:%d:%s\n", file, line, expr); | |
61 fflush(stderr); | |
62 _exit(1); | |
63 } | |
64 | |
65 } // namespace | |
OLD | NEW |