1 /*
2 * Copyright 2016 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 <errno.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20
21 #include <string>
22
23 #include <android-base/test_utils.h>
24 #include <gtest/gtest.h>
25 #include <otautil/DirUtil.h>
26
TEST(DirUtilTest,create_invalid)27 TEST(DirUtilTest, create_invalid) {
28 // Requesting to create an empty dir is invalid.
29 ASSERT_EQ(-1, mkdir_recursively("", 0755, false, nullptr));
30 ASSERT_EQ(ENOENT, errno);
31
32 // Requesting to strip the name with no slash present.
33 ASSERT_EQ(-1, mkdir_recursively("abc", 0755, true, nullptr));
34 ASSERT_EQ(ENOENT, errno);
35
36 // Creating a dir that already exists.
37 TemporaryDir td;
38 ASSERT_EQ(0, mkdir_recursively(td.path, 0755, false, nullptr));
39
40 // "///" is a valid dir.
41 ASSERT_EQ(0, mkdir_recursively("///", 0755, false, nullptr));
42
43 // Request to create a dir, but a file with the same name already exists.
44 TemporaryFile tf;
45 ASSERT_EQ(-1, mkdir_recursively(tf.path, 0755, false, nullptr));
46 ASSERT_EQ(ENOTDIR, errno);
47 }
48
TEST(DirUtilTest,create_smoke)49 TEST(DirUtilTest, create_smoke) {
50 TemporaryDir td;
51 std::string prefix(td.path);
52 std::string path = prefix + "/a/b";
53 constexpr mode_t mode = 0755;
54 ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr));
55
56 // Verify.
57 struct stat sb;
58 ASSERT_EQ(0, stat(path.c_str(), &sb)) << strerror(errno);
59 ASSERT_TRUE(S_ISDIR(sb.st_mode));
60 constexpr mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
61 ASSERT_EQ(mode, sb.st_mode & mask);
62
63 // Clean up.
64 ASSERT_EQ(0, rmdir((prefix + "/a/b").c_str()));
65 ASSERT_EQ(0, rmdir((prefix + "/a").c_str()));
66 }
67
TEST(DirUtilTest,create_strip_filename)68 TEST(DirUtilTest, create_strip_filename) {
69 TemporaryDir td;
70 std::string prefix(td.path);
71 std::string path = prefix + "/a/b";
72 ASSERT_EQ(0, mkdir_recursively(path, 0755, true, nullptr));
73
74 // Verify that "../a" exists but not "../a/b".
75 struct stat sb;
76 ASSERT_EQ(0, stat((prefix + "/a").c_str(), &sb)) << strerror(errno);
77 ASSERT_TRUE(S_ISDIR(sb.st_mode));
78
79 ASSERT_EQ(-1, stat(path.c_str(), &sb));
80 ASSERT_EQ(ENOENT, errno);
81
82 // Clean up.
83 ASSERT_EQ(0, rmdir((prefix + "/a").c_str()));
84 }
85
TEST(DirUtilTest,create_mode)86 TEST(DirUtilTest, create_mode) {
87 TemporaryDir td;
88 std::string prefix(td.path);
89 std::string path = prefix + "/a/b";
90 constexpr mode_t mode = 0751;
91 ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr));
92
93 // Verify the mode for "../a/b".
94 struct stat sb;
95 ASSERT_EQ(0, stat(path.c_str(), &sb)) << strerror(errno);
96 ASSERT_TRUE(S_ISDIR(sb.st_mode));
97 constexpr mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
98 ASSERT_EQ(mode, sb.st_mode & mask);
99
100 // Verify the mode for "../a".
101 ASSERT_EQ(0, stat((prefix + "/a").c_str(), &sb)) << strerror(errno);
102 ASSERT_TRUE(S_ISDIR(sb.st_mode));
103 ASSERT_EQ(mode, sb.st_mode & mask);
104
105 // Clean up.
106 ASSERT_EQ(0, rmdir((prefix + "/a/b").c_str()));
107 ASSERT_EQ(0, rmdir((prefix + "/a").c_str()));
108 }
109