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(pipe(fds), 0); | |
jln (very slow on Chromium)
2012/08/24 04:00:32
The true syntax is strange, it's ASSERT_EQ(expecte
| |
23 | |
24 pid_t pid; | |
25 ASSERT_GE((pid = fork()), 0); | |
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 HANDLE_EINTR(close(fds[1])); | |
jln (very slow on Chromium)
2012/08/24 04:00:32
Add (void) to silence warnings.
| |
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); | |
jln (very slow on Chromium)
2012/08/24 04:00:32
I'll address my int overflow OCD in a later CL.
| |
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 HANDLE_EINTR(close(fds[0])); | |
jln (very slow on Chromium)
2012/08/24 04:00:32
Add (void) to silence warnings.
| |
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 | |
jln (very slow on Chromium)
2012/08/24 04:00:32
Did you forget a word here ? Also add a final dot.
| |
58 EXPECT_EQ(msg.size(), 0u) << std::string(msg.begin(), msg.end()); | |
jln (very slow on Chromium)
2012/08/24 04:06:42
Currently, this will result in something such as:
jln (very slow on Chromium)
2012/08/24 04:26:21
Something such as:
bool subprocess_has_error_me
| |
59 | |
60 int status; | |
jln (very slow on Chromium)
2012/08/24 04:00:32
Please, initialize it.
| |
61 HANDLE_EINTR(waitpid(pid, &status, 0)); | |
jln (very slow on Chromium)
2012/08/24 04:00:32
Add a SANDBOX_ASSERT(waitpid() >= 0 here ?
Right
jln (very slow on Chromium)
2012/08/24 04:02:43
Err, I mean ASSERT_GE of course, don't use SANDBOX
| |
62 SANDBOX_ASSERT(WIFEXITED(status)); | |
jln (very slow on Chromium)
2012/08/24 04:00:32
We still have the same major bug as before: we're
| |
63 SANDBOX_ASSERT(WEXITSTATUS(status) == kExpectedValue); | |
jln (very slow on Chromium)
2012/08/24 04:00:32
Same here, this needs to be ASSERT_EQ().
Be carefu
jln (very slow on Chromium)
2012/08/24 04:26:21
Because of how macro expansion works, this gives a
| |
14 } | 64 } |
65 | |
66 void UnitTests::AssertionFailure(const char *expr, const char *file, | |
67 int line) { | |
68 fprintf(stderr, "%s:%d:%s\n", file, line, expr); | |
69 fflush(stderr); | |
70 _exit(1); | |
71 } | |
72 | |
73 } // namespace | |
OLD | NEW |