2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/mounts.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/status/status.h"
|
2019-05-08 09:29:51 +08:00
|
|
|
#include "absl/strings/match.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/testing.h"
|
|
|
|
#include "sandboxed_api/util/path.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/temp_file.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace file = ::sapi::file;
|
|
|
|
using ::sapi::CreateNamedTempFileAndClose;
|
|
|
|
using ::sapi::CreateTempDir;
|
|
|
|
using ::sapi::GetTestSourcePath;
|
|
|
|
using ::sapi::GetTestTempPath;
|
|
|
|
using ::sapi::IsOk;
|
|
|
|
using ::sapi::StatusIs;
|
|
|
|
using ::testing::Eq;
|
2021-04-14 17:45:15 +08:00
|
|
|
using ::testing::StrEq;
|
2021-01-14 01:25:25 +08:00
|
|
|
using ::testing::UnorderedElementsAreArray;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
constexpr size_t kTmpfsSize = 1024;
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestInvalidFilenames) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
EXPECT_THAT(mounts.AddFile(""), StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFile("a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", ""),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("", "/a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", "a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFile("/"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", "/"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2023-01-19 21:44:11 +08:00
|
|
|
EXPECT_THAT(mounts.Remove("/"), StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestAddFile) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile("/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/b"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/e"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/dd/e"), IsOk());
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", "/f"), IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestAddDir) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/a", "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/c/d", "/c/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/c/d/e", "/c/d/e"), IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestAddTmpFs) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddTmpfs("/a", kTmpfsSize), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddTmpfs("/a/b", kTmpfsSize), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/a/b/c"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/a/b/d", "/a/b/d"), IsOk());
|
|
|
|
}
|
|
|
|
|
2023-01-19 21:44:11 +08:00
|
|
|
TEST(MountTreeTest, TestRemove) {
|
|
|
|
Mounts mounts;
|
|
|
|
EXPECT_THAT(mounts.AddTmpfs("/a", kTmpfsSize), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/b/c/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/c/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/d/b/d", "/d/b/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/e/b/d", "/e/b/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.Remove("/a/b"), StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
EXPECT_THAT(mounts.Remove("/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.Remove("/b/c/d/e"), StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
EXPECT_THAT(mounts.Remove("/b/c/e"), StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
EXPECT_THAT(mounts.Remove("/b/c/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.Remove("/c"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.Remove("/d/b/d/e"), StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
EXPECT_THAT(mounts.Remove("/d/b/d"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.Remove("/e"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.Remove("/f"), StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
TEST(MountTreeTest, TestMultipleInsertionFileSymlink) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
2020-04-02 22:42:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string path,
|
2021-01-22 22:01:05 +08:00
|
|
|
CreateNamedTempFileAndClose(
|
|
|
|
file::JoinPath(GetTestTempPath(), "testdir_")));
|
2020-04-02 22:42:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string symlink_path,
|
2021-01-22 22:01:05 +08:00
|
|
|
CreateNamedTempFileAndClose(
|
|
|
|
file::JoinPath(GetTestTempPath(), "testdir_")));
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
ASSERT_THAT(unlink(symlink_path.c_str()), Eq(0));
|
|
|
|
ASSERT_THAT(symlink(path.c_str(), symlink_path.c_str()), Eq(0));
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt(path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFileAt(path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFileAt(symlink_path, "/a"), IsOk());
|
|
|
|
}
|
|
|
|
|
2023-07-18 17:44:18 +08:00
|
|
|
TEST(MountTreeTest, TestMultipleInsertionUpgradeToWritable) {
|
|
|
|
Mounts mounts;
|
|
|
|
EXPECT_THAT(mounts.AddFile("/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/a", /*is_ro=*/false), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectory("/b"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectory("/b", /*is_ro=*/false), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c", /*is_ro=*/false), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectory("/d", /*is_ro=*/false), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectory("/d"), IsOk());
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
TEST(MountTreeTest, TestMultipleInsertionDirSymlink) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
2021-01-22 22:01:05 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(
|
|
|
|
std::string path,
|
|
|
|
CreateTempDir(file::JoinPath(GetTestTempPath(), "testdir_")));
|
2020-04-02 22:42:17 +08:00
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(std::string symlink_path,
|
2021-01-22 22:01:05 +08:00
|
|
|
CreateNamedTempFileAndClose(
|
|
|
|
file::JoinPath(GetTestTempPath(), "testdir_")));
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
ASSERT_THAT(unlink(symlink_path.c_str()), Eq(0));
|
|
|
|
ASSERT_THAT(symlink(path.c_str(), symlink_path.c_str()), Eq(0));
|
2020-04-02 22:42:17 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(symlink_path, "/a"), IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(symlink_path, "/a"), IsOk());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestMultipleInsertion) {
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/d"), IsOk());
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/f", "/c"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/f", "/c"), IsOk());
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile("/c/d/e"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/f", "/c/d/e"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/f", "/c/d/e"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kFailedPrecondition));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestEvilNullByte) {
|
|
|
|
Mounts mounts;
|
|
|
|
// create the filename with a null byte this way as g4 fix forces newlines
|
|
|
|
// otherwise.
|
2020-04-02 22:42:17 +08:00
|
|
|
std::string filename = "/a/b";
|
2019-03-19 00:21:48 +08:00
|
|
|
filename[2] = '\0';
|
|
|
|
|
|
|
|
EXPECT_THAT(mounts.AddFile(filename),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt(filename, "/a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddFileAt("/a", filename),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt(filename, "/a"),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddDirectoryAt("/a", filename),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
EXPECT_THAT(mounts.AddTmpfs(filename, kTmpfsSize),
|
2020-02-28 01:23:44 +08:00
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2023-01-19 21:44:11 +08:00
|
|
|
EXPECT_THAT(mounts.Remove(filename),
|
|
|
|
StatusIs(absl::StatusCode::kInvalidArgument));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestMinimalDynamicBinary) {
|
|
|
|
Mounts mounts;
|
|
|
|
EXPECT_THAT(mounts.AddMappingsForBinary(
|
|
|
|
GetTestSourcePath("sandbox2/testcases/minimal_dynamic")),
|
|
|
|
IsOk());
|
|
|
|
EXPECT_THAT(mounts.AddFile("/lib/x86_64-linux-gnu/libc.so.6"), IsOk());
|
|
|
|
}
|
|
|
|
|
2019-05-08 09:29:51 +08:00
|
|
|
TEST(MountTreeTest, TestList) {
|
|
|
|
struct TestCase {
|
2022-03-08 20:04:15 +08:00
|
|
|
const char* path;
|
2019-05-08 09:29:51 +08:00
|
|
|
const bool is_ro;
|
|
|
|
};
|
|
|
|
// clang-format off
|
2021-09-01 16:27:56 +08:00
|
|
|
constexpr TestCase kTestCases[] = {
|
2019-05-08 09:29:51 +08:00
|
|
|
// NOTE: Directories have a trailing '/'; files don't.
|
|
|
|
{"/a/b", true},
|
|
|
|
{"/a/c/", true},
|
|
|
|
{"/a/c/d/e/f/g", true},
|
|
|
|
{"/h", true},
|
|
|
|
{"/i/j/k", false},
|
|
|
|
{"/i/l/", false},
|
|
|
|
};
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
Mounts mounts;
|
|
|
|
|
|
|
|
// Create actual directories and files on disk and selectively add
|
2022-03-08 20:04:15 +08:00
|
|
|
for (const auto& test_case : kTestCases) {
|
2019-05-08 09:29:51 +08:00
|
|
|
const auto inside_path = test_case.path;
|
|
|
|
const std::string outside_path = absl::StrCat("/some/dir/", inside_path);
|
|
|
|
if (absl::EndsWith(outside_path, "/")) {
|
|
|
|
ASSERT_THAT(
|
|
|
|
mounts.AddDirectoryAt(file::CleanPath(outside_path),
|
|
|
|
file::CleanPath(inside_path), test_case.is_ro),
|
|
|
|
IsOk());
|
|
|
|
} else {
|
|
|
|
ASSERT_THAT(
|
|
|
|
mounts.AddFileAt(file::CleanPath(outside_path),
|
|
|
|
file::CleanPath(inside_path), test_case.is_ro),
|
|
|
|
IsOk());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 17:37:28 +08:00
|
|
|
ASSERT_THAT(mounts.AddTmpfs(file::CleanPath("/d"), 1024 * 1024), IsOk());
|
|
|
|
|
2019-05-08 09:29:51 +08:00
|
|
|
std::vector<std::string> outside_entries;
|
|
|
|
std::vector<std::string> inside_entries;
|
|
|
|
mounts.RecursivelyListMounts(&outside_entries, &inside_entries);
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
EXPECT_THAT(
|
|
|
|
inside_entries,
|
|
|
|
UnorderedElementsAreArray({
|
|
|
|
"R /a/b",
|
|
|
|
"R /a/c/",
|
|
|
|
"R /a/c/d/e/f/g",
|
|
|
|
"R /h",
|
|
|
|
"W /i/j/k",
|
|
|
|
"W /i/l/",
|
2019-10-24 17:37:28 +08:00
|
|
|
"/d",
|
2019-05-08 09:29:51 +08:00
|
|
|
}));
|
|
|
|
EXPECT_THAT(
|
|
|
|
outside_entries,
|
|
|
|
UnorderedElementsAreArray({
|
|
|
|
absl::StrCat("/some/dir/", "a/b"),
|
|
|
|
absl::StrCat("/some/dir/", "a/c/"),
|
|
|
|
absl::StrCat("/some/dir/", "a/c/d/e/f/g"),
|
|
|
|
absl::StrCat("/some/dir/", "h"),
|
|
|
|
absl::StrCat("/some/dir/", "i/j/k"),
|
|
|
|
absl::StrCat("/some/dir/", "i/l/"),
|
2019-10-24 17:37:28 +08:00
|
|
|
absl::StrCat("tmpfs: size=", 1024*1024),
|
2019-05-08 09:29:51 +08:00
|
|
|
}));
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
|
2023-07-18 17:44:18 +08:00
|
|
|
TEST(MountTreeTest, TestIsWritable) {
|
|
|
|
MountTree::Node nodes[7];
|
|
|
|
MountTree::FileNode* fn0 = nodes[0].mutable_file_node();
|
|
|
|
fn0->set_writable(false);
|
|
|
|
fn0->set_outside("foo");
|
|
|
|
MountTree::FileNode* fn1 = nodes[1].mutable_file_node();
|
|
|
|
fn1->set_writable(true);
|
|
|
|
fn1->set_outside("bar");
|
|
|
|
MountTree::DirNode* dn0 = nodes[2].mutable_dir_node();
|
|
|
|
dn0->set_writable(false);
|
|
|
|
dn0->set_outside("foo");
|
|
|
|
MountTree::DirNode* dn1 = nodes[3].mutable_dir_node();
|
|
|
|
dn1->set_writable(true);
|
|
|
|
dn1->set_outside("bar");
|
|
|
|
MountTree::RootNode* rn0 = nodes[4].mutable_root_node();
|
|
|
|
rn0->set_writable(false);
|
|
|
|
MountTree::RootNode* rn1 = nodes[5].mutable_root_node();
|
|
|
|
rn1->set_writable(true);
|
|
|
|
MountTree::TmpfsNode* tn0 = nodes[6].mutable_tmpfs_node();
|
|
|
|
tn0->set_tmpfs_options("option1");
|
|
|
|
|
|
|
|
EXPECT_FALSE(internal::IsWritable(nodes[0]));
|
|
|
|
EXPECT_TRUE(internal::IsWritable(nodes[1]));
|
|
|
|
EXPECT_FALSE(internal::IsWritable(nodes[2]));
|
|
|
|
EXPECT_TRUE(internal::IsWritable(nodes[3]));
|
|
|
|
EXPECT_FALSE(internal::IsWritable(nodes[4]));
|
|
|
|
EXPECT_TRUE(internal::IsWritable(nodes[5]));
|
|
|
|
EXPECT_FALSE(internal::IsWritable(nodes[6]));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountTreeTest, TestHasSameTarget) {
|
|
|
|
MountTree::Node nodes[10];
|
|
|
|
MountTree::FileNode* fn0 = nodes[0].mutable_file_node();
|
|
|
|
fn0->set_writable(false);
|
|
|
|
fn0->set_outside("foo");
|
|
|
|
MountTree::FileNode* fn1 = nodes[1].mutable_file_node();
|
|
|
|
fn1->set_writable(true);
|
|
|
|
fn1->set_outside("foo");
|
|
|
|
MountTree::FileNode* fn2 = nodes[2].mutable_file_node();
|
|
|
|
fn2->set_writable(false);
|
|
|
|
fn2->set_outside("bar");
|
|
|
|
MountTree::DirNode* dn0 = nodes[3].mutable_dir_node();
|
|
|
|
dn0->set_writable(false);
|
|
|
|
dn0->set_outside("foo");
|
|
|
|
MountTree::DirNode* dn1 = nodes[4].mutable_dir_node();
|
|
|
|
dn1->set_writable(true);
|
|
|
|
dn1->set_outside("foo");
|
|
|
|
MountTree::DirNode* dn2 = nodes[5].mutable_dir_node();
|
|
|
|
dn2->set_writable(false);
|
|
|
|
dn2->set_outside("bar");
|
|
|
|
MountTree::TmpfsNode* tn0 = nodes[6].mutable_tmpfs_node();
|
|
|
|
tn0->set_tmpfs_options("option1");
|
|
|
|
MountTree::TmpfsNode* tn1 = nodes[7].mutable_tmpfs_node();
|
|
|
|
tn1->set_tmpfs_options("option2");
|
|
|
|
MountTree::RootNode* rn0 = nodes[8].mutable_root_node();
|
|
|
|
rn0->set_writable(false);
|
|
|
|
MountTree::RootNode* rn1 = nodes[9].mutable_root_node();
|
|
|
|
rn1->set_writable(true);
|
|
|
|
|
|
|
|
// Compare same file nodes
|
|
|
|
EXPECT_TRUE(internal::HasSameTarget(nodes[0], nodes[0]));
|
|
|
|
// Compare almost same file nodes (ro vs rw)
|
|
|
|
EXPECT_TRUE(internal::HasSameTarget(nodes[0], nodes[1]));
|
|
|
|
// Compare different file nodes
|
|
|
|
EXPECT_FALSE(internal::HasSameTarget(nodes[0], nodes[2]));
|
|
|
|
// Compare file node with dir node
|
|
|
|
EXPECT_FALSE(internal::HasSameTarget(nodes[0], nodes[3]));
|
|
|
|
|
|
|
|
// Compare same dir nodes
|
|
|
|
EXPECT_TRUE(internal::HasSameTarget(nodes[3], nodes[3]));
|
|
|
|
// Compare almost same dir nodes (ro vs rw)
|
|
|
|
EXPECT_TRUE(internal::HasSameTarget(nodes[3], nodes[4]));
|
|
|
|
// Compare different dir nodes
|
|
|
|
EXPECT_FALSE(internal::HasSameTarget(nodes[3], nodes[5]));
|
|
|
|
// Compare dir node with tmpfs node
|
|
|
|
EXPECT_FALSE(internal::HasSameTarget(nodes[3], nodes[6]));
|
|
|
|
|
|
|
|
// Compare same tmpfs nodes
|
|
|
|
EXPECT_TRUE(internal::HasSameTarget(nodes[6], nodes[6]));
|
|
|
|
// Compare different tmpfs nodes
|
|
|
|
EXPECT_FALSE(internal::HasSameTarget(nodes[6], nodes[7]));
|
|
|
|
// Compare dir node with root node
|
|
|
|
EXPECT_FALSE(internal::HasSameTarget(nodes[6], nodes[8]));
|
|
|
|
|
|
|
|
// Compare same root nodes
|
|
|
|
EXPECT_TRUE(internal::HasSameTarget(nodes[8], nodes[8]));
|
|
|
|
// Compare almost same root nodes (ro vs rw)
|
|
|
|
EXPECT_TRUE(internal::HasSameTarget(nodes[8], nodes[9]));
|
|
|
|
}
|
|
|
|
|
2022-03-08 20:04:15 +08:00
|
|
|
TEST(MountTreeTest, TestNodeEquivalence) {
|
|
|
|
MountTree::Node nodes[8];
|
|
|
|
MountTree::FileNode* fn0 = nodes[0].mutable_file_node();
|
2022-03-14 20:14:41 +08:00
|
|
|
fn0->set_writable(false);
|
2022-03-08 20:04:15 +08:00
|
|
|
fn0->set_outside("foo");
|
|
|
|
MountTree::FileNode* fn1 = nodes[1].mutable_file_node();
|
2022-03-14 20:14:41 +08:00
|
|
|
fn1->set_writable(false);
|
2022-03-08 20:04:15 +08:00
|
|
|
fn1->set_outside("bar");
|
|
|
|
MountTree::DirNode* dn0 = nodes[2].mutable_dir_node();
|
2022-03-14 20:14:41 +08:00
|
|
|
dn0->set_writable(false);
|
2022-03-08 20:04:15 +08:00
|
|
|
dn0->set_outside("foo");
|
|
|
|
MountTree::DirNode* dn1 = nodes[3].mutable_dir_node();
|
2022-03-14 20:14:41 +08:00
|
|
|
dn1->set_writable(false);
|
2022-03-08 20:04:15 +08:00
|
|
|
dn1->set_outside("bar");
|
|
|
|
MountTree::TmpfsNode* tn0 = nodes[4].mutable_tmpfs_node();
|
|
|
|
tn0->set_tmpfs_options("option1");
|
|
|
|
MountTree::TmpfsNode* tn1 = nodes[5].mutable_tmpfs_node();
|
|
|
|
tn1->set_tmpfs_options("option2");
|
|
|
|
MountTree::RootNode* rn0 = nodes[6].mutable_root_node();
|
2022-03-14 20:14:41 +08:00
|
|
|
rn0->set_writable(false);
|
2022-03-08 20:04:15 +08:00
|
|
|
MountTree::RootNode* rn1 = nodes[7].mutable_root_node();
|
2022-03-14 20:14:41 +08:00
|
|
|
rn1->set_writable(true);
|
2022-03-08 20:04:15 +08:00
|
|
|
|
2022-04-22 19:57:24 +08:00
|
|
|
for (const MountTree::Node& n : nodes) {
|
2022-03-08 20:04:15 +08:00
|
|
|
ASSERT_TRUE(n.IsInitialized());
|
|
|
|
}
|
|
|
|
// Compare same file nodes
|
|
|
|
EXPECT_TRUE(internal::IsEquivalentNode(nodes[0], nodes[0]));
|
|
|
|
// Compare with different file node
|
|
|
|
EXPECT_FALSE(internal::IsEquivalentNode(nodes[0], nodes[1]));
|
|
|
|
// compare file node with dir node
|
|
|
|
EXPECT_FALSE(internal::IsEquivalentNode(nodes[0], nodes[2]));
|
|
|
|
|
|
|
|
// Compare same dir nodes
|
|
|
|
EXPECT_TRUE(internal::IsEquivalentNode(nodes[2], nodes[2]));
|
|
|
|
// Compare with different dir node
|
|
|
|
EXPECT_FALSE(internal::IsEquivalentNode(nodes[2], nodes[3]));
|
|
|
|
// Compare dir node with tmpfs node
|
|
|
|
EXPECT_FALSE(internal::IsEquivalentNode(nodes[2], nodes[4]));
|
|
|
|
|
|
|
|
// Compare same tmpfs nodes
|
|
|
|
EXPECT_TRUE(internal::IsEquivalentNode(nodes[4], nodes[4]));
|
|
|
|
// Compare with different tmpfs nodes
|
|
|
|
EXPECT_FALSE(internal::IsEquivalentNode(nodes[4], nodes[5]));
|
|
|
|
// Compare tmpfs node with root node
|
|
|
|
EXPECT_FALSE(internal::IsEquivalentNode(nodes[4], nodes[6]));
|
|
|
|
|
|
|
|
// Compare same root nodes
|
|
|
|
EXPECT_TRUE(internal::IsEquivalentNode(nodes[6], nodes[6]));
|
|
|
|
// Compare different root node
|
|
|
|
EXPECT_FALSE(internal::IsEquivalentNode(nodes[6], nodes[7]));
|
|
|
|
// Compare root node with file node
|
|
|
|
EXPECT_FALSE(internal::IsEquivalentNode(nodes[6], nodes[0]));
|
|
|
|
}
|
|
|
|
|
2021-04-14 17:45:15 +08:00
|
|
|
TEST(MountsResolvePathTest, Files) {
|
|
|
|
Mounts mounts;
|
|
|
|
ASSERT_THAT(mounts.AddFileAt("/A", "/a"), IsOk());
|
|
|
|
ASSERT_THAT(mounts.AddFileAt("/B", "/d/b"), IsOk());
|
|
|
|
ASSERT_THAT(mounts.AddFileAt("/C/D/E", "/d/c/e/f/h"), IsOk());
|
|
|
|
std::string resolved;
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/a"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/A"));
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/b"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/B"));
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/C/D/E"));
|
|
|
|
ASSERT_THAT(mounts.ResolvePath("/f"), StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
ASSERT_THAT(mounts.ResolvePath("/d"), StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
ASSERT_THAT(mounts.ResolvePath("/d/c/e/f"),
|
|
|
|
StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
ASSERT_THAT(mounts.ResolvePath("/d/d"),
|
|
|
|
StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MountsResolvePathTest, Dirs) {
|
|
|
|
Mounts mounts;
|
|
|
|
ASSERT_THAT(mounts.AddDirectoryAt("/A", "/a"), IsOk());
|
|
|
|
ASSERT_THAT(mounts.AddDirectoryAt("/B", "/d/b"), IsOk());
|
|
|
|
ASSERT_THAT(mounts.AddDirectoryAt("/C/D/E", "/d/c/e/f/h"), IsOk());
|
|
|
|
ASSERT_THAT(mounts.AddFileAt("/J/G/H", "/d/c/e/f/h/j"), IsOk());
|
|
|
|
ASSERT_THAT(mounts.AddDirectoryAt("/K/L/M", "/d/c/e/f/h/k"), IsOk());
|
|
|
|
std::string resolved;
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/a"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/A"));
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/a/b/c/d/e"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/A/b/c/d/e"));
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/b"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/B"));
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/C/D/E"));
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h/i"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/C/D/E/i"));
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h/j"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/J/G/H"));
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h/k"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/K/L/M"));
|
|
|
|
SAPI_ASSERT_OK_AND_ASSIGN(resolved, mounts.ResolvePath("/d/c/e/f/h/k/a"));
|
|
|
|
EXPECT_THAT(resolved, StrEq("/K/L/M/a"));
|
|
|
|
ASSERT_THAT(mounts.ResolvePath("/f"), StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
ASSERT_THAT(mounts.ResolvePath("/d"), StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
ASSERT_THAT(mounts.ResolvePath("/d/c/e/f"),
|
|
|
|
StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
ASSERT_THAT(mounts.ResolvePath("/d/d"),
|
|
|
|
StatusIs(absl::StatusCode::kNotFound));
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|