xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/util/path_test.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 #include "sandboxed_api/util/path.h"
16 
17 #include <string>
18 
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "absl/strings/string_view.h"
22 
23 namespace sandbox2 {
24 namespace {
25 
26 namespace file = ::sapi::file;
27 using ::testing::Pair;
28 using ::testing::StrEq;
29 
TEST(PathTest,ArgumentTypes)30 TEST(PathTest, ArgumentTypes) {
31   // JoinPath must be able to accept arguments that are compatible with
32   // absl::string_view. So test a few of them here.
33   const char char_array[] = "a";
34   const char* char_ptr = "b";
35   std::string string_type = "c";
36   absl::string_view sp_type = "d";
37 
38   EXPECT_THAT(file::JoinPath(char_array, char_ptr, string_type, sp_type),
39               StrEq("a/b/c/d"));
40 }
41 
TEST(PathTest,JoinPath)42 TEST(PathTest, JoinPath) {
43   EXPECT_THAT(file::JoinPath("/foo", "bar"), StrEq("/foo/bar"));
44   EXPECT_THAT(file::JoinPath("foo", "bar"), StrEq("foo/bar"));
45   EXPECT_THAT(file::JoinPath("foo", "/bar"), StrEq("foo/bar"));
46   EXPECT_THAT(file::JoinPath("/foo", "/bar"), StrEq("/foo/bar"));
47 
48   EXPECT_THAT(file::JoinPath("", "/bar"), StrEq("/bar"));
49   EXPECT_THAT(file::JoinPath("", "bar"), StrEq("bar"));
50   EXPECT_THAT(file::JoinPath("/foo", ""), StrEq("/foo"));
51 
52   EXPECT_THAT(file::JoinPath("/foo/bar/baz/", "/blah/blink/biz"),
53               StrEq("/foo/bar/baz/blah/blink/biz"));
54 
55   EXPECT_THAT(file::JoinPath("/foo", "bar", "baz"), StrEq("/foo/bar/baz"));
56   EXPECT_THAT(file::JoinPath("foo", "bar", "baz"), StrEq("foo/bar/baz"));
57   EXPECT_THAT(file::JoinPath("/foo", "bar", "baz", "blah"),
58               StrEq("/foo/bar/baz/blah"));
59   EXPECT_THAT(file::JoinPath("/foo", "bar", "/baz", "blah"),
60               StrEq("/foo/bar/baz/blah"));
61   EXPECT_THAT(file::JoinPath("/foo", "/bar/", "/baz", "blah"),
62               StrEq("/foo/bar/baz/blah"));
63   EXPECT_THAT(file::JoinPath("/foo", "/bar/", "baz", "blah"),
64               StrEq("/foo/bar/baz/blah"));
65 
66   EXPECT_THAT(file::JoinPath("/", "a"), StrEq("/a"));
67   EXPECT_THAT(file::JoinPath(), StrEq(""));
68 }
69 
TEST(PathTest,SplitPath)70 TEST(PathTest, SplitPath) {
71   // We cannot write the type directly within the EXPECT, because the ',' breaks
72   // the macro.
73   EXPECT_THAT(file::SplitPath("/hello/"), Pair(StrEq("/hello"), StrEq("")));
74   EXPECT_THAT(file::SplitPath("/hello"), Pair(StrEq("/"), StrEq("hello")));
75   EXPECT_THAT(file::SplitPath("hello/world"),
76               Pair(StrEq("hello"), StrEq("world")));
77   EXPECT_THAT(file::SplitPath("hello/"), Pair(StrEq("hello"), StrEq("")));
78   EXPECT_THAT(file::SplitPath("world"), Pair(StrEq(""), StrEq("world")));
79   EXPECT_THAT(file::SplitPath("/"), Pair(StrEq("/"), StrEq("")));
80   EXPECT_THAT(file::SplitPath(""), Pair(StrEq(""), StrEq("")));
81 }
82 
TEST(PathTest,CleanPath)83 TEST(PathTest, CleanPath) {
84   EXPECT_THAT(file::CleanPath(""), StrEq("."));
85   EXPECT_THAT(file::CleanPath("x"), StrEq("x"));
86   EXPECT_THAT(file::CleanPath("/a/b/c/d"), StrEq("/a/b/c/d"));
87   EXPECT_THAT(file::CleanPath("/a/b/c/d/"), StrEq("/a/b/c/d"));
88   EXPECT_THAT(file::CleanPath("/a//b"), StrEq("/a/b"));
89   EXPECT_THAT(file::CleanPath("//a//b/"), StrEq("/a/b"));
90   EXPECT_THAT(file::CleanPath("/.."), StrEq("/"));
91   EXPECT_THAT(file::CleanPath("/././././"), StrEq("/"));
92   EXPECT_THAT(file::CleanPath("/a/b/.."), StrEq("/a"));
93   EXPECT_THAT(file::CleanPath("/a/b/../../.."), StrEq("/"));
94   EXPECT_THAT(file::CleanPath("//a//b/..////../..//"), StrEq("/"));
95   EXPECT_THAT(file::CleanPath("//a//../x//"), StrEq("/x"));
96   EXPECT_THAT(file::CleanPath("../../a/b/../c"), StrEq("../../a/c"));
97   EXPECT_THAT(file::CleanPath("../../a/b/../c/../.."), StrEq("../.."));
98   EXPECT_THAT(file::CleanPath("foo/../../../bar"), StrEq("../../bar"));
99 }
100 
101 }  // namespace
102 }  // namespace sandbox2
103