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 #include "sandboxed_api/testing.h"
16*ec63e07aSXin Li
17*ec63e07aSXin Li #include <cstdlib>
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"
22*ec63e07aSXin Li #include "sandboxed_api/sandbox2/allow_all_syscalls.h"
23*ec63e07aSXin Li #include "sandboxed_api/sandbox2/policybuilder.h"
24*ec63e07aSXin Li #include "sandboxed_api/util/path.h"
25*ec63e07aSXin Li
26*ec63e07aSXin Li namespace sapi {
27*ec63e07aSXin Li
CreateDefaultPermissiveTestPolicy(absl::string_view bin_path)28*ec63e07aSXin Li sandbox2::PolicyBuilder CreateDefaultPermissiveTestPolicy(
29*ec63e07aSXin Li absl::string_view bin_path) {
30*ec63e07aSXin Li sandbox2::PolicyBuilder builder;
31*ec63e07aSXin Li // Don't restrict the syscalls at all.
32*ec63e07aSXin Li builder.DefaultAction(sandbox2::AllowAllSyscalls());
33*ec63e07aSXin Li if (sapi::host_os::IsAndroid()) {
34*ec63e07aSXin Li builder.DisableNamespaces();
35*ec63e07aSXin Li return builder;
36*ec63e07aSXin Li }
37*ec63e07aSXin Li if (IsCoverageRun()) {
38*ec63e07aSXin Li builder.AddDirectory(getenv("COVERAGE_DIR"), /*is_ro=*/false);
39*ec63e07aSXin Li }
40*ec63e07aSXin Li if constexpr (sapi::sanitizers::IsAny()) {
41*ec63e07aSXin Li builder.AddLibrariesForBinary(bin_path);
42*ec63e07aSXin Li }
43*ec63e07aSXin Li if constexpr (sapi::sanitizers::IsAny()) {
44*ec63e07aSXin Li builder.AddDirectory("/proc");
45*ec63e07aSXin Li }
46*ec63e07aSXin Li builder.AllowTcMalloc();
47*ec63e07aSXin Li return builder;
48*ec63e07aSXin Li }
49*ec63e07aSXin Li
GetTestTempPath(absl::string_view name)50*ec63e07aSXin Li std::string GetTestTempPath(absl::string_view name) {
51*ec63e07aSXin Li // When using Bazel, the environment variable TEST_TMPDIR is guaranteed to be
52*ec63e07aSXin Li // set.
53*ec63e07aSXin Li // See https://bazel.build/reference/test-encyclopedia#initial-conditions for
54*ec63e07aSXin Li // details.
55*ec63e07aSXin Li const char* test_tmpdir = getenv("TEST_TMPDIR");
56*ec63e07aSXin Li return file::JoinPath(test_tmpdir ? test_tmpdir : ".", name);
57*ec63e07aSXin Li }
58*ec63e07aSXin Li
GetTestSourcePath(absl::string_view name)59*ec63e07aSXin Li std::string GetTestSourcePath(absl::string_view name) {
60*ec63e07aSXin Li // Like in GetTestTempPath(), when using Bazel, the environment variable
61*ec63e07aSXin Li // TEST_SRCDIR is guaranteed to be set.
62*ec63e07aSXin Li const char* test_srcdir = getenv("TEST_SRCDIR");
63*ec63e07aSXin Li return file::JoinPath(test_srcdir ? test_srcdir : ".",
64*ec63e07aSXin Li "com_google_sandboxed_api/sandboxed_api", name);
65*ec63e07aSXin Li }
66*ec63e07aSXin Li
67*ec63e07aSXin Li } // namespace sapi
68