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.
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/path.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <string>
|
|
|
|
|
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/strings/string_view.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace file = ::sapi::file;
|
2020-04-29 21:45:14 +08:00
|
|
|
using ::testing::Pair;
|
|
|
|
using ::testing::StrEq;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
TEST(PathTest, ArgumentTypes) {
|
|
|
|
// JoinPath must be able to accept arguments that are compatible with
|
|
|
|
// absl::string_view. So test a few of them here.
|
|
|
|
const char char_array[] = "a";
|
|
|
|
const char* char_ptr = "b";
|
|
|
|
std::string string_type = "c";
|
|
|
|
absl::string_view sp_type = "d";
|
|
|
|
|
|
|
|
EXPECT_THAT(file::JoinPath(char_array, char_ptr, string_type, sp_type),
|
|
|
|
StrEq("a/b/c/d"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PathTest, JoinPath) {
|
|
|
|
EXPECT_THAT(file::JoinPath("/foo", "bar"), StrEq("/foo/bar"));
|
|
|
|
EXPECT_THAT(file::JoinPath("foo", "bar"), StrEq("foo/bar"));
|
|
|
|
EXPECT_THAT(file::JoinPath("foo", "/bar"), StrEq("foo/bar"));
|
|
|
|
EXPECT_THAT(file::JoinPath("/foo", "/bar"), StrEq("/foo/bar"));
|
|
|
|
|
|
|
|
EXPECT_THAT(file::JoinPath("", "/bar"), StrEq("/bar"));
|
|
|
|
EXPECT_THAT(file::JoinPath("", "bar"), StrEq("bar"));
|
|
|
|
EXPECT_THAT(file::JoinPath("/foo", ""), StrEq("/foo"));
|
|
|
|
|
|
|
|
EXPECT_THAT(file::JoinPath("/foo/bar/baz/", "/blah/blink/biz"),
|
|
|
|
StrEq("/foo/bar/baz/blah/blink/biz"));
|
|
|
|
|
|
|
|
EXPECT_THAT(file::JoinPath("/foo", "bar", "baz"), StrEq("/foo/bar/baz"));
|
|
|
|
EXPECT_THAT(file::JoinPath("foo", "bar", "baz"), StrEq("foo/bar/baz"));
|
|
|
|
EXPECT_THAT(file::JoinPath("/foo", "bar", "baz", "blah"),
|
|
|
|
StrEq("/foo/bar/baz/blah"));
|
|
|
|
EXPECT_THAT(file::JoinPath("/foo", "bar", "/baz", "blah"),
|
|
|
|
StrEq("/foo/bar/baz/blah"));
|
|
|
|
EXPECT_THAT(file::JoinPath("/foo", "/bar/", "/baz", "blah"),
|
|
|
|
StrEq("/foo/bar/baz/blah"));
|
|
|
|
EXPECT_THAT(file::JoinPath("/foo", "/bar/", "baz", "blah"),
|
|
|
|
StrEq("/foo/bar/baz/blah"));
|
|
|
|
|
|
|
|
EXPECT_THAT(file::JoinPath("/", "a"), StrEq("/a"));
|
|
|
|
EXPECT_THAT(file::JoinPath(), StrEq(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PathTest, SplitPath) {
|
|
|
|
// We cannot write the type directly within the EXPECT, because the ',' breaks
|
|
|
|
// the macro.
|
2020-04-29 21:45:14 +08:00
|
|
|
EXPECT_THAT(file::SplitPath("/hello/"), Pair(StrEq("/hello"), StrEq("")));
|
|
|
|
EXPECT_THAT(file::SplitPath("/hello"), Pair(StrEq("/"), StrEq("hello")));
|
|
|
|
EXPECT_THAT(file::SplitPath("hello/world"),
|
|
|
|
Pair(StrEq("hello"), StrEq("world")));
|
|
|
|
EXPECT_THAT(file::SplitPath("hello/"), Pair(StrEq("hello"), StrEq("")));
|
|
|
|
EXPECT_THAT(file::SplitPath("world"), Pair(StrEq(""), StrEq("world")));
|
|
|
|
EXPECT_THAT(file::SplitPath("/"), Pair(StrEq("/"), StrEq("")));
|
|
|
|
EXPECT_THAT(file::SplitPath(""), Pair(StrEq(""), StrEq("")));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(PathTest, CleanPath) {
|
|
|
|
EXPECT_THAT(file::CleanPath(""), StrEq("."));
|
|
|
|
EXPECT_THAT(file::CleanPath("x"), StrEq("x"));
|
|
|
|
EXPECT_THAT(file::CleanPath("/a/b/c/d"), StrEq("/a/b/c/d"));
|
|
|
|
EXPECT_THAT(file::CleanPath("/a/b/c/d/"), StrEq("/a/b/c/d"));
|
|
|
|
EXPECT_THAT(file::CleanPath("/a//b"), StrEq("/a/b"));
|
|
|
|
EXPECT_THAT(file::CleanPath("//a//b/"), StrEq("/a/b"));
|
|
|
|
EXPECT_THAT(file::CleanPath("/.."), StrEq("/"));
|
|
|
|
EXPECT_THAT(file::CleanPath("/././././"), StrEq("/"));
|
|
|
|
EXPECT_THAT(file::CleanPath("/a/b/.."), StrEq("/a"));
|
|
|
|
EXPECT_THAT(file::CleanPath("/a/b/../../.."), StrEq("/"));
|
|
|
|
EXPECT_THAT(file::CleanPath("//a//b/..////../..//"), StrEq("/"));
|
|
|
|
EXPECT_THAT(file::CleanPath("//a//../x//"), StrEq("/x"));
|
|
|
|
EXPECT_THAT(file::CleanPath("../../a/b/../c"), StrEq("../../a/c"));
|
|
|
|
EXPECT_THAT(file::CleanPath("../../a/b/../c/../.."), StrEq("../.."));
|
|
|
|
EXPECT_THAT(file::CleanPath("foo/../../../bar"), StrEq("../../bar"));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace sandbox2
|