blob: 5e2ae4fb5e611dafc239a71306e4fb3c8f9d05a4 [file] [log] [blame]
Tao Baof19295c2016-12-22 09:28:03 -08001/*
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
27TEST(DirUtilTest, create_invalid) {
28 // Requesting to create an empty dir is invalid.
29 ASSERT_EQ(-1, dirCreateHierarchy("", 0755, nullptr, false, nullptr));
30 ASSERT_EQ(ENOENT, errno);
31
32 // Requesting to strip the name with no slash present.
33 ASSERT_EQ(-1, dirCreateHierarchy("abc", 0755, nullptr, true, nullptr));
34 ASSERT_EQ(ENOENT, errno);
35
36 // Creating a dir that already exists.
37 TemporaryDir td;
38 ASSERT_EQ(0, dirCreateHierarchy(td.path, 0755, nullptr, false, nullptr));
39
40 // "///" is a valid dir.
41 ASSERT_EQ(0, dirCreateHierarchy("///", 0755, nullptr, 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, dirCreateHierarchy(tf.path, 0755, nullptr, false, nullptr));
46 ASSERT_EQ(ENOTDIR, errno);
47}
48
49TEST(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, dirCreateHierarchy(path.c_str(), mode, nullptr, 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
68TEST(DirUtilTest, create_strip_filename) {
69 TemporaryDir td;
70 std::string prefix(td.path);
71 std::string path = prefix + "/a/b";
72 ASSERT_EQ(0, dirCreateHierarchy(path.c_str(), 0755, nullptr, 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
86TEST(DirUtilTest, create_mode_and_timestamp) {
87 TemporaryDir td;
88 std::string prefix(td.path);
89 std::string path = prefix + "/a/b";
90 // Set the timestamp to 8/1/2008.
91 constexpr struct utimbuf timestamp = { 1217592000, 1217592000 };
92 constexpr mode_t mode = 0751;
93 ASSERT_EQ(0, dirCreateHierarchy(path.c_str(), mode, &timestamp, false, nullptr));
94
95 // Verify the mode and timestamp for "../a/b".
96 struct stat sb;
97 ASSERT_EQ(0, stat(path.c_str(), &sb)) << strerror(errno);
98 ASSERT_TRUE(S_ISDIR(sb.st_mode));
99 constexpr mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
100 ASSERT_EQ(mode, sb.st_mode & mask);
101
102 timespec time;
103 time.tv_sec = 1217592000;
104 time.tv_nsec = 0;
105
106 ASSERT_EQ(time.tv_sec, static_cast<long>(sb.st_atime));
107 ASSERT_EQ(time.tv_sec, static_cast<long>(sb.st_mtime));
108
109 // Verify the mode for "../a". Note that the timestamp for intermediate directories (e.g. "../a")
110 // may not be 'timestamp' according to the current implementation.
111 ASSERT_EQ(0, stat((prefix + "/a").c_str(), &sb)) << strerror(errno);
112 ASSERT_TRUE(S_ISDIR(sb.st_mode));
113 ASSERT_EQ(mode, sb.st_mode & mask);
114
115 // Clean up.
116 ASSERT_EQ(0, rmdir((prefix + "/a/b").c_str()));
117 ASSERT_EQ(0, rmdir((prefix + "/a").c_str()));
118}
119
120TEST(DirUtilTest, unlink_invalid) {
121 // File doesn't exist.
122 ASSERT_EQ(-1, dirUnlinkHierarchy("doesntexist"));
123
124 // Nonexistent directory.
125 TemporaryDir td;
126 std::string path(td.path);
127 ASSERT_EQ(-1, dirUnlinkHierarchy((path + "/a").c_str()));
128 ASSERT_EQ(ENOENT, errno);
129}
130
131TEST(DirUtilTest, unlink_smoke) {
132 // Unlink a file.
133 TemporaryFile tf;
134 ASSERT_EQ(0, dirUnlinkHierarchy(tf.path));
135 ASSERT_EQ(-1, access(tf.path, F_OK));
136
137 TemporaryDir td;
138 std::string path(td.path);
139 constexpr mode_t mode = 0700;
140 ASSERT_EQ(0, mkdir((path + "/a").c_str(), mode));
141 ASSERT_EQ(0, mkdir((path + "/a/b").c_str(), mode));
142 ASSERT_EQ(0, mkdir((path + "/a/b/c").c_str(), mode));
143 ASSERT_EQ(0, mkdir((path + "/a/d").c_str(), mode));
144
145 // Remove "../a" recursively.
146 ASSERT_EQ(0, dirUnlinkHierarchy((path + "/a").c_str()));
147
148 // Verify it's gone.
149 ASSERT_EQ(-1, access((path + "/a").c_str(), F_OK));
150}