Chromium Code Reviews| Index: sandbox/linux/tests/unit_tests.h |
| diff --git a/sandbox/linux/tests/unit_tests.h b/sandbox/linux/tests/unit_tests.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e8a51f004b8476c2522ca9a720c99130d45572bc |
| --- /dev/null |
| +++ b/sandbox/linux/tests/unit_tests.h |
| @@ -0,0 +1,68 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |
| +#define SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |
| + |
| +#include <setjmp.h> |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
:(
|
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/basictypes.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| + |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
Please add namespace sandbox {
|
| +// Define a new test case that runs inside of a death test. This is necessary, |
| +// as most of our tests by definition make global and irreversible changes to |
| +// the system (i.e. they install a sandbox). GTest provides death tests as a |
| +// tool to isolate global changes from the rest of the tests. |
| +#define SANDBOX_TEST(test_case_name, test_name) \ |
| + bool TEST_##test_name(); \ |
| + TEST(test_case_name, test_name) { \ |
| + UnitTests::RunTestInProcess(TEST_##test_name); \ |
| + } \ |
| + bool TEST_##test_name() |
| + |
| +// Simple assertion macro that is compatible with running inside of a death |
| +// test. We unfortunately cannot use any of the GTest macros. |
| +#define SANDBOX_STR(x) #x |
| +#define SANDBOX_ASSERT(expr) \ |
| + ((expr) \ |
| + ? 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
|
| + : UnitTests::AssertionFailure(SANDBOX_STR(expr), __FILE__, __LINE__)) |
| + |
| +// This macro can be used to check whether the code in "block" triggers a |
| +// SANDBOX_DIE() fatal error. This is currently only supported for unittests |
| +// that test the seccomp BPF sandbox. |
| +#define SANDBOX_ASSERT_DEATH(block, err) \ |
| + do { \ |
| + UnitTests::ClearLastError(); \ |
| + if (!setjmp(UnitTests::JumpEnv())) { \ |
| + 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
|
| + ({ block; }); \ |
| + } \ |
| + SANDBOX_ASSERT(UnitTests::LastError() == (err)); \ |
| + } while (false) |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
Note: you can also create macro blocks with ({ }).
|
| + |
| +class UnitTests { |
|
jln (very slow on Chromium)
2012/08/14 01:48:13
Please add a comment explaining that this is not t
|
| + public: |
| + typedef bool (*Test)(void); |
| + |
| + static void RunTestInProcess(Test test); |
| + static void CaptureError(const char *msg, const char *file, int line); |
| + static void AssertionFailure(const char *expr, const char *file, int line); |
| + |
| + 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
|
| + static void ClearLastError() { last_error_.clear(); } |
| + 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().
|
| + |
| + private: |
| + static std::string last_error_; |
| + static jmp_buf jmp_env_; |
| + |
| + DISALLOW_IMPLICIT_CONSTRUCTORS(UnitTests); |
| +}; |
| + |
| +#endif // SANDBOX_LINUX_TESTS_UNIT_TESTS_H__ |