1 /*
2 * Copyright (C) 2020 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
17 #include <filesystem>
18 #include <string>
19
20 #include <errno.h>
21
22 #include <android-base/file.h>
23 #include <android-base/result.h>
24 #include <android-base/stringprintf.h>
25 #include <gtest/gtest.h>
26
27 #include "apexd_test_utils.h"
28 #include "apexd_utils.h"
29
30 namespace android {
31 namespace apex {
32 namespace {
33
34 using android::apex::testing::IsOk;
35 using android::base::Basename;
36 using android::base::StringPrintf;
37 using ::testing::UnorderedElementsAre;
38 using ::testing::UnorderedElementsAreArray;
39
40 // TODO(b/170327382): add unit tests for apexd_utils.h
41
TEST(ApexdUtilTest,FindFirstExistingDirectoryBothExist)42 TEST(ApexdUtilTest, FindFirstExistingDirectoryBothExist) {
43 TemporaryDir first_dir;
44 TemporaryDir second_dir;
45 auto result = FindFirstExistingDirectory(first_dir.path, second_dir.path);
46 ASSERT_TRUE(IsOk(result));
47 ASSERT_EQ(*result, first_dir.path);
48 }
49
TEST(ApexdUtilTest,FindFirstExistingDirectoryOnlyFirstExist)50 TEST(ApexdUtilTest, FindFirstExistingDirectoryOnlyFirstExist) {
51 TemporaryDir first_dir;
52 auto second_dir = "/data/local/tmp/does/not/exist";
53 auto result = FindFirstExistingDirectory(first_dir.path, second_dir);
54 ASSERT_TRUE(IsOk(result));
55 ASSERT_EQ(*result, first_dir.path);
56 }
57
TEST(ApexdUtilTest,FindFirstExistingDirectoryOnlySecondExist)58 TEST(ApexdUtilTest, FindFirstExistingDirectoryOnlySecondExist) {
59 auto first_dir = "/data/local/tmp/does/not/exist";
60 TemporaryDir second_dir;
61 auto result = FindFirstExistingDirectory(first_dir, second_dir.path);
62 ASSERT_TRUE(IsOk(result));
63 ASSERT_EQ(*result, second_dir.path);
64 }
65
TEST(ApexdUtilTest,FindFirstExistingDirectoryNoneExist)66 TEST(ApexdUtilTest, FindFirstExistingDirectoryNoneExist) {
67 auto first_dir = "/data/local/tmp/does/not/exist";
68 auto second_dir = "/data/local/tmp/also/does/not/exist";
69 auto result = FindFirstExistingDirectory(first_dir, second_dir);
70 ASSERT_FALSE(IsOk(result));
71 }
72
TEST(ApexdUtilTest,FindFirstExistingDirectoryFirstFileSecondDir)73 TEST(ApexdUtilTest, FindFirstExistingDirectoryFirstFileSecondDir) {
74 TemporaryFile first_file;
75 TemporaryDir second_dir;
76 auto result = FindFirstExistingDirectory(first_file.path, second_dir.path);
77 ASSERT_TRUE(IsOk(result));
78 ASSERT_EQ(*result, second_dir.path);
79 }
80
TEST(ApexdUtilTest,FindFirstExistingDirectoryFirstDirSecondFile)81 TEST(ApexdUtilTest, FindFirstExistingDirectoryFirstDirSecondFile) {
82 TemporaryDir first_dir;
83 TemporaryFile second_file;
84 auto result = FindFirstExistingDirectory(first_dir.path, second_file.path);
85 ASSERT_TRUE(IsOk(result));
86 ASSERT_EQ(*result, first_dir.path);
87 }
88
TEST(ApexdUtilTest,FindFirstExistingDirectoryBothFiles)89 TEST(ApexdUtilTest, FindFirstExistingDirectoryBothFiles) {
90 TemporaryFile first_file;
91 TemporaryFile second_file;
92 auto result = FindFirstExistingDirectory(first_file.path, second_file.path);
93 ASSERT_FALSE(IsOk(result));
94 }
95
TEST(ApexdUtilTest,FindFirstExistingDirectoryFirstFileSecondDoesNotExist)96 TEST(ApexdUtilTest, FindFirstExistingDirectoryFirstFileSecondDoesNotExist) {
97 TemporaryFile first_file;
98 auto second_dir = "/data/local/tmp/does/not/exist";
99 auto result = FindFirstExistingDirectory(first_file.path, second_dir);
100 ASSERT_FALSE(IsOk(result));
101 }
102
TEST(ApexdUtilTest,FindFirstExistingDirectoryFirsDoesNotExistSecondFile)103 TEST(ApexdUtilTest, FindFirstExistingDirectoryFirsDoesNotExistSecondFile) {
104 auto first_dir = "/data/local/tmp/does/not/exist";
105 TemporaryFile second_file;
106 auto result = FindFirstExistingDirectory(first_dir, second_file.path);
107 ASSERT_FALSE(IsOk(result));
108 }
109
TEST(ApexdUtilTest,MoveDir)110 TEST(ApexdUtilTest, MoveDir) {
111 namespace fs = std::filesystem;
112
113 TemporaryDir from;
114 TemporaryDir to;
115
116 TemporaryFile from_1(from.path);
117 auto from_subdir = StringPrintf("%s/subdir", from.path);
118 if (mkdir(from_subdir.c_str(), 07000) != 0) {
119 FAIL() << "Failed to mkdir " << from_subdir << " : " << strerror(errno);
120 }
121 TemporaryFile from_2(from_subdir);
122
123 auto result = MoveDir(from.path, to.path);
124 ASSERT_TRUE(IsOk(result));
125 ASSERT_TRUE(fs::is_empty(from.path));
126
127 std::vector<std::string> content;
128 for (const auto& it : fs::recursive_directory_iterator(to.path)) {
129 content.push_back(it.path());
130 }
131
132 static const std::vector<std::string> expected = {
133 StringPrintf("%s/%s", to.path, Basename(from_1.path).c_str()),
134 StringPrintf("%s/subdir", to.path),
135 StringPrintf("%s/subdir/%s", to.path, Basename(from_2.path).c_str()),
136 };
137 ASSERT_THAT(content, UnorderedElementsAreArray(expected));
138 }
139
TEST(ApexdUtilTest,MoveDirFromIsNotDirectory)140 TEST(ApexdUtilTest, MoveDirFromIsNotDirectory) {
141 TemporaryFile from;
142 TemporaryDir to;
143 ASSERT_FALSE(IsOk(MoveDir(from.path, to.path)));
144 }
145
TEST(ApexdUtilTest,MoveDirToIsNotDirectory)146 TEST(ApexdUtilTest, MoveDirToIsNotDirectory) {
147 TemporaryDir from;
148 TemporaryFile to;
149 TemporaryFile from_1(from.path);
150 ASSERT_FALSE(IsOk(MoveDir(from.path, to.path)));
151 }
152
TEST(ApexdUtilTest,MoveDirFromDoesNotExist)153 TEST(ApexdUtilTest, MoveDirFromDoesNotExist) {
154 TemporaryDir to;
155 ASSERT_FALSE(IsOk(MoveDir("/data/local/tmp/does/not/exist", to.path)));
156 }
157
TEST(ApexdUtilTest,MoveDirToDoesNotExist)158 TEST(ApexdUtilTest, MoveDirToDoesNotExist) {
159 namespace fs = std::filesystem;
160
161 TemporaryDir from;
162 TemporaryFile from_1(from.path);
163 auto from_subdir = StringPrintf("%s/subdir", from.path);
164 if (mkdir(from_subdir.c_str(), 07000) != 0) {
165 FAIL() << "Failed to mkdir " << from_subdir << " : " << strerror(errno);
166 }
167 TemporaryFile from_2(from_subdir);
168
169 ASSERT_FALSE(IsOk(MoveDir(from.path, "/data/local/tmp/does/not/exist")));
170
171 // Check that |from| directory is not empty.
172 std::vector<std::string> content;
173 for (const auto& it : fs::recursive_directory_iterator(from.path)) {
174 content.push_back(it.path());
175 }
176
177 ASSERT_THAT(content,
178 UnorderedElementsAre(from_1.path, from_subdir, from_2.path));
179 }
180
181 } // namespace
182 } // namespace apex
183 } // namespace android
184