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

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

Issue 10833044: Refactored ErrorCode into it's own class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unittest (and new framework to make this possible) Created 8 years, 4 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 | Annotate | Revision Log
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 "testing/gtest/include/gtest/gtest.h" 5 #include <stdio.h>
6 #include <sys/resource.h>
7 #include <sys/time.h>
8
9 #include "base/file_util.h"
10 #include "sandbox/linux/seccomp-bpf/die.h"
11 #include "sandbox/linux/tests/unit_tests.h"
12
13 // Allow us to intercept fatal errors in the seccomp BPF sandbox and write
14 // unittests that check whether they were raised as expected. This code is
15 // harmless when used with any of the other sandboxes. But of course,
16 // SANDBOX_ASSERT_DEATH() is only useable for seccomp BPF.
17 using playground2::Die;
18
19 void UnitTests::RunTestInProcess(UnitTests::Test test) {
20 // Runs a test in a sub-process. This is necessary for most of the code
21 // in the BPF sandbox, as it potentially makes global state changes and as
22 // it also tends to raise fatal errors, if the code has been used in an
23 // insecure manner.
24 int fds[2];
25 ASSERT_EQ(pipe(fds), 0);
26
27 pid_t pid;
28 ASSERT_GE((pid = fork()), 0);
29 if (!pid) {
30 // In child process
31 // Set up the error handler that is used by the BPF sandbox so that it
32 // logs messages to our pipe. It should include file names and line numbers
33 // but no stack traces.
34 // Die::LogToStderr() does what we want to do.
35 SANDBOX_ASSERT(dup2(fds[1], 2) == 2);
36 SANDBOX_ASSERT(!close(fds[0]));
37 SANDBOX_ASSERT(!close(fds[1]));
38 Die custom_handler(Die::LogToStderr);
39
40 // Disable core files. They are not very useful for our individual test
41 // cases.
42 struct rlimit no_core = { 0 };
43 setrlimit(RLIMIT_CORE, &no_core);
44
45 SANDBOX_ASSERT(test());
46 _exit(1);
47 }
48
49 HANDLE_EINTR(close(fds[1]));
50 std::vector<char> msg;
51 ssize_t rc;
52 do {
53 const int kCapacity = 256;
54 size_t len = msg.size();
55 msg.resize(len + kCapacity);
56 rc = HANDLE_EINTR(read(fds[0], &msg[len], kCapacity));
57 msg.resize(len + std::max(rc, static_cast<ssize_t>(0)));
58 } while (rc > 0);
59 HANDLE_EINTR(close(fds[0]));
60 EXPECT_EQ(msg.size(), 0u) << std::string(msg.begin(), msg.end());
61
62 int status;
63 HANDLE_EINTR(waitpid(pid, &status, 0));
64 }
65
66 void UnitTests::CaptureError(const char *msg, const char *file, int line) {
67 if (msg) {
68 last_error_ = msg;
69 } else {
70 last_error_.clear();
71 }
72
73 // The use of longjmp() can cause us to not destroy objects that have
74 // gone out of scope. This is OK. We only ever use this code in cases
75 // were we want to test that a fatal error has been raised. And the
76 // code that raises these errors isn't prepared to deal with clean up
77 // after the error anyway.
78 // Leaking of resources is fine in this case, because we only ever do
79 // so from within sub-processes; and the sub-processes either die themselves
80 // upon test failure, or they call _exit() (note the underscore).
81 longjmp(jmp_env_, 1);
82 }
83
84 void UnitTests::AssertionFailure(const char *expr, const char *file, int line){
jln (very slow on Chromium) 2012/08/14 01:48:13 Style Missing space before "{"
85 fprintf(stderr, "%s:%d:%s\n", file, line, expr);
86 fflush(stderr);
87 _exit(1);
88 }
89
6 int main(int argc, char *argv[]) { 90 int main(int argc, char *argv[]) {
jln (very slow on Chromium) 2012/08/14 01:48:13 Please split this out to main.cc
7 testing::InitGoogleTest(&argc, argv); 91 testing::InitGoogleTest(&argc, argv);
8 // Always go through re-execution for death tests. 92 // Always go through re-execution for death tests.
9 // This makes gtest only marginally slower for us and has the 93 // This makes gtest only marginally slower for us and has the
10 // additional side effect of getting rid of gtest warnings about fork() 94 // additional side effect of getting rid of gtest warnings about fork()
11 // safety. 95 // safety.
12 ::testing::FLAGS_gtest_death_test_style = "threadsafe"; 96 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
13 return RUN_ALL_TESTS(); 97 return RUN_ALL_TESTS();
14 } 98 }
99
100 std::string UnitTests::last_error_;
101 jmp_buf UnitTests::jmp_env_;
OLDNEW
« sandbox/linux/tests/unit_tests.h ('K') | « sandbox/linux/tests/unit_tests.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698