1 //
2 // Copyright (C) 2023 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "common/libs/utils/files_test_helper.h"
17 
18 namespace cuttlefish {
19 
TEST_P(EmulateAbsolutePathBase,NoHomeNoPwd)20 TEST_P(EmulateAbsolutePathBase, NoHomeNoPwd) {
21   const bool follow_symlink = false;
22   auto emulated_absolute_path =
23       EmulateAbsolutePath({.current_working_dir = std::nullopt,
24                            .home_dir = std::nullopt,
25                            .path_to_convert = input_path_,
26                            .follow_symlink = follow_symlink});
27 
28   ASSERT_TRUE(emulated_absolute_path.ok())
29       << emulated_absolute_path.error().Trace();
30   ASSERT_EQ(*emulated_absolute_path, expected_path_);
31 }
32 
33 INSTANTIATE_TEST_SUITE_P(
34     CommonUtilsTest, EmulateAbsolutePathBase,
35     testing::Values(InputOutput{.path_to_convert_ = "/", .expected_ = "/"},
36                     InputOutput{.path_to_convert_ = "", .expected_ = ""},
37                     InputOutput{.path_to_convert_ = "/a/b/c/",
38                                 .expected_ = "/a/b/c"},
39                     InputOutput{.path_to_convert_ = "/a", .expected_ = "/a"}));
40 
TEST_P(EmulateAbsolutePathWithPwd,NoHomeYesPwd)41 TEST_P(EmulateAbsolutePathWithPwd, NoHomeYesPwd) {
42   const bool follow_symlink = false;
43   auto emulated_absolute_path =
44       EmulateAbsolutePath({.current_working_dir = current_dir_,
45                            .home_dir = "/a/b/c",
46                            .path_to_convert = input_path_,
47                            .follow_symlink = follow_symlink});
48 
49   ASSERT_TRUE(emulated_absolute_path.ok())
50       << emulated_absolute_path.error().Trace();
51   ASSERT_EQ(*emulated_absolute_path, expected_path_);
52 }
53 
54 INSTANTIATE_TEST_SUITE_P(
55     CommonUtilsTest, EmulateAbsolutePathWithPwd,
56     testing::Values(InputOutput{.working_dir_ = "/x/y/z",
57                                 .path_to_convert_ = "",
58                                 .expected_ = ""},
59                     InputOutput{.working_dir_ = "/x/y/z",
60                                 .path_to_convert_ = "a",
61                                 .expected_ = "/x/y/z/a"},
62                     InputOutput{.working_dir_ = "/x/y/z",
63                                 .path_to_convert_ = ".",
64                                 .expected_ = "/x/y/z"},
65                     InputOutput{.working_dir_ = "/x/y/z",
66                                 .path_to_convert_ = "..",
67                                 .expected_ = "/x/y"},
68                     InputOutput{.working_dir_ = "/x/y/z",
69                                 .path_to_convert_ = "./k/../../t/./q",
70                                 .expected_ = "/x/y/t/q"}));
71 
TEST_P(EmulateAbsolutePathWithHome,YesHomeNoPwd)72 TEST_P(EmulateAbsolutePathWithHome, YesHomeNoPwd) {
73   const bool follow_symlink = false;
74   auto emulated_absolute_path =
75       EmulateAbsolutePath({.current_working_dir = std::nullopt,
76                            .home_dir = home_dir_,
77                            .path_to_convert = input_path_,
78                            .follow_symlink = follow_symlink});
79 
80   ASSERT_TRUE(emulated_absolute_path.ok())
81       << emulated_absolute_path.error().Trace();
82   ASSERT_EQ(*emulated_absolute_path, expected_path_);
83 }
84 
85 INSTANTIATE_TEST_SUITE_P(
86     CommonUtilsTest, EmulateAbsolutePathWithHome,
87     testing::Values(InputOutput{.home_dir_ = "/x/y/z",
88                                 .path_to_convert_ = "~",
89                                 .expected_ = "/x/y/z"},
90                     InputOutput{.home_dir_ = "/x/y/z",
91                                 .path_to_convert_ = "~/a",
92                                 .expected_ = "/x/y/z/a"},
93                     InputOutput{.home_dir_ = "/x/y/z",
94                                 .path_to_convert_ = "~/.",
95                                 .expected_ = "/x/y/z"},
96                     InputOutput{.home_dir_ = "/x/y/z",
97                                 .path_to_convert_ = "~/..",
98                                 .expected_ = "/x/y"},
99                     InputOutput{.home_dir_ = "/x/y/z",
100                                 .path_to_convert_ = "~/k/../../t/./q",
101                                 .expected_ = "/x/y/t/q"}));
102 
103 }  // namespace cuttlefish
104