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