blob: e62032c685449ac566fd7b63d7952a6bdc385162 [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}