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_SANITIZERS_AND_COVERAGE can be used in tests to skip running 25 // a given test (by emitting 'return') when running under one of the sanitizers 26 // (ASan, MSan, TSan) or under code coverage. Example: 27 // 28 // TEST(Foo, Bar) { 29 // SKIP_SANITIZERS_AND_COVERAGE; 30 // [...] 31 // } 32 // 33 // The reason for this is because Bazel options are inherited to binaries in 34 // data dependencies and cannot be per-target, which means when running a test 35 // with a sanitizer or coverage, the sandboxee as data dependency will also be 36 // compiled with sanitizer or coverage, which creates a lot of side effects and 37 // violates the sandbox policy prepared for the test. 38 // See b/7981124. // google3-only(internal reference) 39 // In other words, those tests cannot work under sanitizers or coverage, so we 40 // skip them in such situation using this macro. 41 // 42 // The downside of this approach is that no coverage will be collected. 43 // To still have coverage, pre-compile sandboxees and add them as test data, 44 // then there will be no need to skip tests. 45 #define SKIP_SANITIZERS_AND_COVERAGE \ 46 do { \ 47 if (sapi::sanitizers::IsAny() || sapi::IsCoverageRun()) { \ 48 return; \ 49 } \ 50 } while (0) 51 52 #define SKIP_SANITIZERS \ 53 do { \ 54 if (sapi::sanitizers::IsAny()) { \ 55 return; \ 56 } \ 57 } while (0) 58 59 namespace sapi { 60 61 sandbox2::PolicyBuilder CreateDefaultPermissiveTestPolicy( 62 absl::string_view bin_path); 63 64 // Returns a writable path usable in tests. If the name argument is specified, 65 // returns a name under that path. This can then be used for creating temporary 66 // test files and/or directories. 67 std::string GetTestTempPath(absl::string_view name = {}); 68 69 // Returns a filename relative to the sandboxed_api directory at the root of the 70 // source tree. Use this to access data files in tests. 71 std::string GetTestSourcePath(absl::string_view name); 72 73 } // namespace sapi 74 75 #endif // SANDBOXED_API_TESTING_H_ 76