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 static const int kExpectedValue = 42; | |
15 | |
16 void UnitTests::RunTestInProcess(UnitTests::Test test) { | |
17 // Runs a test in a sub-process. This is necessary for most of the code | |
18 // in the BPF sandbox, as it potentially makes global state changes and as | |
19 // it also tends to raise fatal errors, if the code has been used in an | |
20 // insecure manner. | |
21 int fds[2]; | |
22 ASSERT_EQ(0, pipe(fds)); | |
23 | |
24 pid_t pid; | |
25 ASSERT_LT(0, (pid = fork())); | |
jln (very slow on Chromium)
2012/08/24 18:30:07
For GE, there is no "expected" value, so the previ
| |
26 if (!pid) { | |
27 // In child process | |
28 // Redirect stderr to our pipe. This way, we can capture all error | |
29 // messages, if we decide we want to do so in our tests. | |
30 SANDBOX_ASSERT(dup2(fds[1], 2) == 2); | |
31 SANDBOX_ASSERT(!close(fds[0])); | |
32 SANDBOX_ASSERT(!close(fds[1])); | |
33 | |
34 // Disable core files. They are not very useful for our individual test | |
35 // cases. | |
36 struct rlimit no_core = { 0 }; | |
37 setrlimit(RLIMIT_CORE, &no_core); | |
38 | |
39 test(); | |
40 _exit(kExpectedValue); | |
41 } | |
42 | |
43 (void)HANDLE_EINTR(close(fds[1])); | |
44 std::vector<char> msg; | |
45 ssize_t rc; | |
46 do { | |
47 const unsigned int kCapacity = 256; | |
48 size_t len = msg.size(); | |
49 msg.resize(len + kCapacity); | |
50 rc = HANDLE_EINTR(read(fds[0], &msg[len], kCapacity)); | |
51 msg.resize(len + std::max(rc, static_cast<ssize_t>(0))); | |
52 } while (rc > 0); | |
53 (void)HANDLE_EINTR(close(fds[0])); | |
54 | |
55 // If we have read something, an error occurred and we want to print a | |
56 // message. As far as I can tell, gtest doesn't give us a good way to | |
57 // control what we are writing, so just make a best effort to print | |
58 // something intelligible. | |
59 bool completed_successfully = (msg.size() == 0u); | |
60 EXPECT_TRUE(completed_successfully) | |
jln (very slow on Chromium)
2012/08/24 18:30:07
This variable name will be in the GTest output. It
| |
61 << "Actual test failure: " << std::string(msg.begin(), msg.end()); | |
jln (very slow on Chromium)
2012/08/24 18:30:07
Nit: this should be indented 4 spaces. I personall
| |
62 | |
63 int status = 0; | |
64 EXPECT_EQ(pid, HANDLE_EINTR(waitpid(pid, &status, 0))); | |
jln (very slow on Chromium)
2012/08/24 18:30:07
Given how macros work, this gives an absolutely im
| |
65 bool terminated_normally = WIFEXITED(status); | |
66 EXPECT_TRUE(terminated_normally); | |
jln (very slow on Chromium)
2012/08/24 18:30:07
This one should really be an ASSERT ?
| |
67 int exit_status = WEXITSTATUS(status); | |
68 EXPECT_EQ(kExpectedValue, exit_status); | |
jln (very slow on Chromium)
2012/08/24 18:30:07
Same here, ASSERT?
| |
14 } | 69 } |
70 | |
71 void UnitTests::AssertionFailure(const char *expr, const char *file, | |
72 int line) { | |
73 fprintf(stderr, "%s:%d:%s\n", file, line, expr); | |
74 fflush(stderr); | |
75 _exit(1); | |
76 } | |
77 | |
78 } // namespace | |
OLD | NEW |