| 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 #ifndef SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ | 5 #ifndef SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ |
| 6 #define SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ | 6 #define SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "sandbox/linux/seccomp-bpf/bpf_tester_compatibility_delegate.h" | 11 #include "sandbox/linux/seccomp-bpf/bpf_tester_compatibility_delegate.h" |
| 12 #include "sandbox/linux/tests/unit_tests.h" | 12 #include "sandbox/linux/tests/unit_tests.h" |
| 13 | 13 |
| 14 namespace sandbox { | 14 namespace sandbox { |
| 15 | 15 |
| 16 // BPF_TEST_C() is a special version of SANDBOX_TEST(). It runs a test function | 16 // BPF_TEST_C() is a special version of SANDBOX_TEST(). It runs a test function |
| 17 // in a sub-process, under a seccomp-bpf policy specified in | 17 // in a sub-process, under a seccomp-bpf policy specified in |
| 18 // |bpf_policy_class_name| without failing on configurations that are allowed | 18 // |bpf_policy_class_name| without failing on configurations that are allowed |
| 19 // to not support seccomp-bpf in their kernels. | 19 // to not support seccomp-bpf in their kernels. |
| 20 // This is the preferred format for new BPF tests. |bpf_policy_class_name| is a | 20 // This is the preferred format for new BPF tests. |bpf_policy_class_name| is a |
| 21 // class name (which will be default-constructed) that implements the | 21 // class name (which will be default-constructed) that implements the |
| 22 // SandboxBPFDSLPolicy interface. | 22 // Policy interface. |
| 23 // The test function's body can simply follow. Test functions should use | 23 // The test function's body can simply follow. Test functions should use |
| 24 // the BPF_ASSERT macros defined below, not GTEST's macros. The use of | 24 // the BPF_ASSERT macros defined below, not GTEST's macros. The use of |
| 25 // CHECK* macros is supported but less robust. | 25 // CHECK* macros is supported but less robust. |
| 26 #define BPF_TEST_C(test_case_name, test_name, bpf_policy_class_name) \ | 26 #define BPF_TEST_C(test_case_name, test_name, bpf_policy_class_name) \ |
| 27 BPF_DEATH_TEST_C( \ | 27 BPF_DEATH_TEST_C( \ |
| 28 test_case_name, test_name, DEATH_SUCCESS(), bpf_policy_class_name) | 28 test_case_name, test_name, DEATH_SUCCESS(), bpf_policy_class_name) |
| 29 | 29 |
| 30 // Identical to BPF_TEST_C but allows to specify the nature of death. | 30 // Identical to BPF_TEST_C but allows to specify the nature of death. |
| 31 #define BPF_DEATH_TEST_C( \ | 31 #define BPF_DEATH_TEST_C( \ |
| 32 test_case_name, test_name, death, bpf_policy_class_name) \ | 32 test_case_name, test_name, death, bpf_policy_class_name) \ |
| (...skipping 27 matching lines...) Expand all Loading... |
| 60 #define BPF_ASSERT SANDBOX_ASSERT | 60 #define BPF_ASSERT SANDBOX_ASSERT |
| 61 #define BPF_ASSERT_EQ(x, y) BPF_ASSERT((x) == (y)) | 61 #define BPF_ASSERT_EQ(x, y) BPF_ASSERT((x) == (y)) |
| 62 #define BPF_ASSERT_NE(x, y) BPF_ASSERT((x) != (y)) | 62 #define BPF_ASSERT_NE(x, y) BPF_ASSERT((x) != (y)) |
| 63 #define BPF_ASSERT_LT(x, y) BPF_ASSERT((x) < (y)) | 63 #define BPF_ASSERT_LT(x, y) BPF_ASSERT((x) < (y)) |
| 64 #define BPF_ASSERT_GT(x, y) BPF_ASSERT((x) > (y)) | 64 #define BPF_ASSERT_GT(x, y) BPF_ASSERT((x) > (y)) |
| 65 #define BPF_ASSERT_LE(x, y) BPF_ASSERT((x) <= (y)) | 65 #define BPF_ASSERT_LE(x, y) BPF_ASSERT((x) <= (y)) |
| 66 #define BPF_ASSERT_GE(x, y) BPF_ASSERT((x) >= (y)) | 66 #define BPF_ASSERT_GE(x, y) BPF_ASSERT((x) >= (y)) |
| 67 | 67 |
| 68 // This form of BPF_TEST is now discouraged (but still allowed) in favor of | 68 // This form of BPF_TEST is now discouraged (but still allowed) in favor of |
| 69 // BPF_TEST_D and BPF_TEST_C. | 69 // BPF_TEST_D and BPF_TEST_C. |
| 70 // The |policy| parameter should be a SandboxBPFDSLPolicy subclass. | 70 // The |policy| parameter should be a Policy subclass. |
| 71 // BPF_TEST() takes a C++ data type as an fourth parameter. A variable | 71 // BPF_TEST() takes a C++ data type as an fourth parameter. A variable |
| 72 // of this type will be allocated and a pointer to it will be | 72 // of this type will be allocated and a pointer to it will be |
| 73 // available within the test function as "BPF_AUX". The pointer will | 73 // available within the test function as "BPF_AUX". The pointer will |
| 74 // also be passed as an argument to the policy's constructor. Policies | 74 // also be passed as an argument to the policy's constructor. Policies |
| 75 // would typically use it as an argument to SandboxBPF::Trap(), if | 75 // would typically use it as an argument to SandboxBPF::Trap(), if |
| 76 // they want to communicate data between the BPF_TEST() and a Trap() | 76 // they want to communicate data between the BPF_TEST() and a Trap() |
| 77 // function. The life-time of this object is the same as the life-time | 77 // function. The life-time of this object is the same as the life-time |
| 78 // of the process running under the seccomp-bpf policy. | 78 // of the process running under the seccomp-bpf policy. |
| 79 // |aux| must not be void. | 79 // |aux| must not be void. |
| 80 #define BPF_TEST(test_case_name, test_name, policy, aux) \ | 80 #define BPF_TEST(test_case_name, test_name, policy, aux) \ |
| (...skipping 16 matching lines...) Expand all Loading... |
| 97 // class name as a template parameter to implement the BPFTesterDelegate | 97 // class name as a template parameter to implement the BPFTesterDelegate |
| 98 // interface which can be used to build BPF unittests with | 98 // interface which can be used to build BPF unittests with |
| 99 // the SandboxBPFTestRunner class. | 99 // the SandboxBPFTestRunner class. |
| 100 template <class PolicyClass> | 100 template <class PolicyClass> |
| 101 class BPFTesterSimpleDelegate : public BPFTesterDelegate { | 101 class BPFTesterSimpleDelegate : public BPFTesterDelegate { |
| 102 public: | 102 public: |
| 103 explicit BPFTesterSimpleDelegate(void (*test_function)(void)) | 103 explicit BPFTesterSimpleDelegate(void (*test_function)(void)) |
| 104 : test_function_(test_function) {} | 104 : test_function_(test_function) {} |
| 105 virtual ~BPFTesterSimpleDelegate() {} | 105 virtual ~BPFTesterSimpleDelegate() {} |
| 106 | 106 |
| 107 virtual scoped_ptr<bpf_dsl::SandboxBPFDSLPolicy> GetSandboxBPFPolicy() | 107 virtual scoped_ptr<bpf_dsl::Policy> GetSandboxBPFPolicy() override { |
| 108 override { | 108 return scoped_ptr<bpf_dsl::Policy>(new PolicyClass()); |
| 109 return scoped_ptr<bpf_dsl::SandboxBPFDSLPolicy>(new PolicyClass()); | |
| 110 } | 109 } |
| 111 virtual void RunTestFunction() override { | 110 virtual void RunTestFunction() override { |
| 112 DCHECK(test_function_); | 111 DCHECK(test_function_); |
| 113 test_function_(); | 112 test_function_(); |
| 114 } | 113 } |
| 115 | 114 |
| 116 private: | 115 private: |
| 117 void (*test_function_)(void); | 116 void (*test_function_)(void); |
| 118 DISALLOW_COPY_AND_ASSIGN(BPFTesterSimpleDelegate); | 117 DISALLOW_COPY_AND_ASSIGN(BPFTesterSimpleDelegate); |
| 119 }; | 118 }; |
| 120 | 119 |
| 121 } // namespace sandbox | 120 } // namespace sandbox |
| 122 | 121 |
| 123 #endif // SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ | 122 #endif // SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ |
| OLD | NEW |