Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ | |
| 6 #define SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ | |
| 7 | |
| 8 #include <setjmp.h> | |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
:(
| |
| 9 | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 | |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
Please add namespace sandbox {
| |
| 17 // Define a new test case that runs inside of a death test. This is necessary, | |
| 18 // as most of our tests by definition make global and irreversible changes to | |
| 19 // the system (i.e. they install a sandbox). GTest provides death tests as a | |
| 20 // tool to isolate global changes from the rest of the tests. | |
| 21 #define SANDBOX_TEST(test_case_name, test_name) \ | |
| 22 bool TEST_##test_name(); \ | |
| 23 TEST(test_case_name, test_name) { \ | |
| 24 UnitTests::RunTestInProcess(TEST_##test_name); \ | |
| 25 } \ | |
| 26 bool TEST_##test_name() | |
| 27 | |
| 28 // Simple assertion macro that is compatible with running inside of a death | |
| 29 // test. We unfortunately cannot use any of the GTest macros. | |
| 30 #define SANDBOX_STR(x) #x | |
| 31 #define SANDBOX_ASSERT(expr) \ | |
| 32 ((expr) \ | |
| 33 ? static_cast<void>(0) \ | |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
Style: I think it would be nicer to have the terna
| |
| 34 : UnitTests::AssertionFailure(SANDBOX_STR(expr), __FILE__, __LINE__)) | |
| 35 | |
| 36 // This macro can be used to check whether the code in "block" triggers a | |
| 37 // SANDBOX_DIE() fatal error. This is currently only supported for unittests | |
| 38 // that test the seccomp BPF sandbox. | |
| 39 #define SANDBOX_ASSERT_DEATH(block, err) \ | |
| 40 do { \ | |
| 41 UnitTests::ClearLastError(); \ | |
| 42 if (!setjmp(UnitTests::JumpEnv())) { \ | |
| 43 playground2::Die custom_handler(UnitTests::CaptureError); \ | |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
We really shouldn't have a dependency on seccomp-b
| |
| 44 ({ block; }); \ | |
| 45 } \ | |
| 46 SANDBOX_ASSERT(UnitTests::LastError() == (err)); \ | |
| 47 } while (false) | |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
Note: you can also create macro blocks with ({ }).
| |
| 48 | |
| 49 class UnitTests { | |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
Please add a comment explaining that this is not t
| |
| 50 public: | |
| 51 typedef bool (*Test)(void); | |
| 52 | |
| 53 static void RunTestInProcess(Test test); | |
| 54 static void CaptureError(const char *msg, const char *file, int line); | |
| 55 static void AssertionFailure(const char *expr, const char *file, int line); | |
| 56 | |
| 57 static jmp_buf& JumpEnv() { return jmp_env_; } | |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
Style: the accessor should be called jmp_env().
It
| |
| 58 static void ClearLastError() { last_error_.clear(); } | |
| 59 static const std::string& LastError() { return last_error_; } | |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
Accessor should be const and called last_error().
| |
| 60 | |
| 61 private: | |
| 62 static std::string last_error_; | |
| 63 static jmp_buf jmp_env_; | |
| 64 | |
| 65 DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests); | |
| 66 }; | |
| 67 | |
| 68 #endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ | |
| OLD | NEW |