xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/testing.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SANDBOXED_API_TESTING_H_
16 #define SANDBOXED_API_TESTING_H_
17 
18 #include <string>
19 
20 #include "absl/strings/string_view.h"
21 #include "sandboxed_api/config.h"  // IWYU pragma: export
22 #include "sandboxed_api/sandbox2/policybuilder.h"
23 
24 // The macro SKIP_ANDROID can be used in tests to skip running a
25 // given test (by emitting 'retrun') when running on Android. Example:
26 //
27 //    TEST(Foo, Bar) {
28 //      SKIP_ANDROID;
29 //      [...]
30 //      }
31 //
32 // The reason for this is because certain unit tests require the use of user
33 // namespaces which are not present on Android.
34 #define SKIP_ANDROID                            \
35   do {                                          \
36     if constexpr (sapi::host_os::IsAndroid()) { \
37       return;                                   \
38     }                                           \
39   } while (0)
40 
41 // The macro SKIP_SANITIZERS_AND_COVERAGE can be used in tests to skip running
42 // a given test (by emitting 'return') when running under one of the sanitizers
43 // (ASan, MSan, TSan) or under code coverage. Example:
44 //
45 //   TEST(Foo, Bar) {
46 //     SKIP_SANITIZERS_AND_COVERAGE;
47 //     [...]
48 //   }
49 //
50 // The reason for this is because Bazel options are inherited to binaries in
51 // data dependencies and cannot be per-target, which means when running a test
52 // with a sanitizer or coverage, the sandboxee as data dependency will also be
53 // compiled with sanitizer or coverage, which creates a lot of side effects and
54 // violates the sandbox policy prepared for the test.
55 // See b/7981124. // google3-only(internal reference)
56 // In other words, those tests cannot work under sanitizers or coverage, so we
57 // skip them in such situation using this macro.
58 //
59 // The downside of this approach is that no coverage will be collected.
60 // To still have coverage, pre-compile sandboxees and add them as test data,
61 // then there will be no need to skip tests.
62 #define SKIP_SANITIZERS_AND_COVERAGE                          \
63   do {                                                        \
64     if (sapi::sanitizers::IsAny() || sapi::IsCoverageRun()) { \
65       return;                                                 \
66     }                                                         \
67   } while (0)
68 
69 #define SKIP_SANITIZERS              \
70   do {                               \
71     if (sapi::sanitizers::IsAny()) { \
72       return;                        \
73     }                                \
74   } while (0)
75 
76 namespace sapi {
77 
78 sandbox2::PolicyBuilder CreateDefaultPermissiveTestPolicy(
79     absl::string_view bin_path);
80 
81 // Returns a writable path usable in tests. If the name argument is specified,
82 // returns a name under that path. This can then be used for creating temporary
83 // test files and/or directories.
84 std::string GetTestTempPath(absl::string_view name = {});
85 
86 // Returns a filename relative to the sandboxed_api directory at the root of the
87 // source tree. Use this to access data files in tests.
88 std::string GetTestSourcePath(absl::string_view name);
89 
90 }  // namespace sapi
91 
92 #endif  // SANDBOXED_API_TESTING_H_
93