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/macros.h" | 9 #include "base/macros.h" |
9 #include "build/build_config.h" | 10 #include "build/build_config.h" |
10 #include "sandbox/linux/seccomp-bpf/bpf_tester_compatibility_delegate.h" | 11 #include "sandbox/linux/seccomp-bpf/bpf_tester_compatibility_delegate.h" |
11 #include "sandbox/linux/tests/unit_tests.h" | 12 #include "sandbox/linux/tests/unit_tests.h" |
12 | 13 |
13 namespace sandbox { | 14 namespace sandbox { |
14 | 15 |
15 // 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 |
16 // in a sub-process, under a seccomp-bpf policy specified in | 17 // in a sub-process, under a seccomp-bpf policy specified in |
17 // |bpf_policy_class_name| without failing on configurations that are allowed | 18 // |bpf_policy_class_name| without failing on configurations that are allowed |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 #define BPF_ASSERT SANDBOX_ASSERT | 60 #define BPF_ASSERT SANDBOX_ASSERT |
60 #define BPF_ASSERT_EQ(x, y) BPF_ASSERT((x) == (y)) | 61 #define BPF_ASSERT_EQ(x, y) BPF_ASSERT((x) == (y)) |
61 #define BPF_ASSERT_NE(x, y) BPF_ASSERT((x) != (y)) | 62 #define BPF_ASSERT_NE(x, y) BPF_ASSERT((x) != (y)) |
62 #define BPF_ASSERT_LT(x, y) BPF_ASSERT((x) < (y)) | 63 #define BPF_ASSERT_LT(x, y) BPF_ASSERT((x) < (y)) |
63 #define BPF_ASSERT_GT(x, y) BPF_ASSERT((x) > (y)) | 64 #define BPF_ASSERT_GT(x, y) BPF_ASSERT((x) > (y)) |
64 #define BPF_ASSERT_LE(x, y) BPF_ASSERT((x) <= (y)) | 65 #define BPF_ASSERT_LE(x, y) BPF_ASSERT((x) <= (y)) |
65 #define BPF_ASSERT_GE(x, y) BPF_ASSERT((x) >= (y)) | 66 #define BPF_ASSERT_GE(x, y) BPF_ASSERT((x) >= (y)) |
66 | 67 |
67 // 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 |
68 // BPF_TEST_D and BPF_TEST_C. | 69 // BPF_TEST_D and BPF_TEST_C. |
69 // The |policy| parameter should be a SyscallEvaluator function pointer | 70 // The |policy| parameter should be a SandboxBPFPolicy subclass. |
70 // (which is now a deprecated way of expressing policies). | 71 // BPF_TEST() takes a C++ data type as an fourth parameter. A variable |
71 // BPF_TEST() takes a C++ data type as an optional fourth parameter. If | 72 // of this type will be allocated and a pointer to it will be |
72 // present, this sets up a variable that can be accessed as "BPF_AUX". This | 73 // available within the test function as "BPF_AUX". The pointer will |
73 // variable will be passed as an argument to the "policy" function. Policies | 74 // also be passed as an argument to the policy's constructor. Policies |
74 // would typically use it as an argument to SandboxBPF::Trap(), if they want to | 75 // would typically use it as an argument to SandboxBPF::Trap(), if |
75 // communicate data between the BPF_TEST() and a Trap() function. The life-time | 76 // they want to communicate data between the BPF_TEST() and a Trap() |
76 // of this object is the same as the life-time of the process running under the | 77 // function. The life-time of this object is the same as the life-time |
77 // seccomp-bpf policy. | 78 // of the process running under the seccomp-bpf policy. |
78 // The type specified in |aux| and the last parameter of the policy function | 79 // |aux| must not be void. |
79 // must be compatible. |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) \ |
81 BPF_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS(), policy, aux) | 81 BPF_DEATH_TEST(test_case_name, test_name, DEATH_SUCCESS(), policy, aux) |
82 | 82 |
83 // A BPF_DEATH_TEST is just the same as a BPF_TEST, but it assumes that the | 83 // A BPF_DEATH_TEST is just the same as a BPF_TEST, but it assumes that the |
84 // test will fail with a particular known error condition. Use the DEATH_XXX() | 84 // test will fail with a particular known error condition. Use the DEATH_XXX() |
85 // macros from unit_tests.h to specify the expected error condition. | 85 // macros from unit_tests.h to specify the expected error condition. |
86 #define BPF_DEATH_TEST(test_case_name, test_name, death, policy, aux) \ | 86 #define BPF_DEATH_TEST(test_case_name, test_name, death, policy, aux) \ |
87 void BPF_TEST_##test_name( \ | 87 void BPF_TEST_##test_name(aux* BPF_AUX); \ |
88 sandbox::BPFTesterCompatibilityDelegate<aux>::AuxType* BPF_AUX); \ | 88 TEST(test_case_name, DISABLE_ON_TSAN(test_name)) { \ |
89 TEST(test_case_name, DISABLE_ON_TSAN(test_name)) { \ | 89 sandbox::SandboxBPFTestRunner bpf_test_runner( \ |
90 sandbox::SandboxBPFTestRunner bpf_test_runner( \ | 90 new sandbox::BPFTesterCompatibilityDelegate<policy, aux>( \ |
91 new sandbox::BPFTesterCompatibilityDelegate<aux>(BPF_TEST_##test_name, \ | 91 BPF_TEST_##test_name)); \ |
92 policy)); \ | 92 sandbox::UnitTests::RunTestInProcess(&bpf_test_runner, death); \ |
93 sandbox::UnitTests::RunTestInProcess(&bpf_test_runner, death); \ | 93 } \ |
94 } \ | 94 void BPF_TEST_##test_name(aux* BPF_AUX) |
95 void BPF_TEST_##test_name( \ | |
96 sandbox::BPFTesterCompatibilityDelegate<aux>::AuxType* BPF_AUX) | |
97 | 95 |
98 // This class takes a simple function pointer as a constructor parameter and a | 96 // This class takes a simple function pointer as a constructor parameter and a |
99 // class name as a template parameter to implement the BPFTesterDelegate | 97 // class name as a template parameter to implement the BPFTesterDelegate |
100 // interface which can be used to build BPF unittests with | 98 // interface which can be used to build BPF unittests with |
101 // the SandboxBPFTestRunner class. | 99 // the SandboxBPFTestRunner class. |
102 template <class PolicyClass> | 100 template <class PolicyClass> |
103 class BPFTesterSimpleDelegate : public BPFTesterDelegate { | 101 class BPFTesterSimpleDelegate : public BPFTesterDelegate { |
104 public: | 102 public: |
105 explicit BPFTesterSimpleDelegate(void (*test_function)(void)) | 103 explicit BPFTesterSimpleDelegate(void (*test_function)(void)) |
106 : test_function_(test_function) {} | 104 : test_function_(test_function) {} |
107 virtual ~BPFTesterSimpleDelegate() {} | 105 virtual ~BPFTesterSimpleDelegate() {} |
108 | 106 |
109 virtual scoped_ptr<SandboxBPFPolicy> GetSandboxBPFPolicy() OVERRIDE { | 107 virtual scoped_ptr<SandboxBPFPolicy> GetSandboxBPFPolicy() OVERRIDE { |
110 return scoped_ptr<SandboxBPFPolicy>(new PolicyClass()); | 108 return scoped_ptr<SandboxBPFPolicy>(new PolicyClass()); |
111 } | 109 } |
112 virtual void RunTestFunction() OVERRIDE { | 110 virtual void RunTestFunction() OVERRIDE { |
113 DCHECK(test_function_); | 111 DCHECK(test_function_); |
114 test_function_(); | 112 test_function_(); |
115 } | 113 } |
116 | 114 |
117 private: | 115 private: |
118 void (*test_function_)(void); | 116 void (*test_function_)(void); |
119 DISALLOW_COPY_AND_ASSIGN(BPFTesterSimpleDelegate); | 117 DISALLOW_COPY_AND_ASSIGN(BPFTesterSimpleDelegate); |
120 }; | 118 }; |
121 | 119 |
122 } // namespace sandbox | 120 } // namespace sandbox |
123 | 121 |
124 #endif // SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ | 122 #endif // SANDBOX_LINUX_SECCOMP_BPF_BPF_TESTS_H__ |
OLD | NEW |