blob: 7f85d13ea0143677f1364fbceeec97dffb22594a [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.
Tao Baoac3d1ed2017-07-23 00:01:02 -070029 ASSERT_EQ(-1, mkdir_recursively("", 0755, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080030 ASSERT_EQ(ENOENT, errno);
31
32 // Requesting to strip the name with no slash present.
Tao Baoac3d1ed2017-07-23 00:01:02 -070033 ASSERT_EQ(-1, mkdir_recursively("abc", 0755, true, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080034 ASSERT_EQ(ENOENT, errno);
35
36 // Creating a dir that already exists.
37 TemporaryDir td;
Tao Baoac3d1ed2017-07-23 00:01:02 -070038 ASSERT_EQ(0, mkdir_recursively(td.path, 0755, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080039
40 // "///" is a valid dir.
Tao Baoac3d1ed2017-07-23 00:01:02 -070041 ASSERT_EQ(0, mkdir_recursively("///", 0755, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080042
43 // Request to create a dir, but a file with the same name already exists.
44 TemporaryFile tf;
Tao Baoac3d1ed2017-07-23 00:01:02 -070045 ASSERT_EQ(-1, mkdir_recursively(tf.path, 0755, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080046 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;
Tao Baoac3d1ed2017-07-23 00:01:02 -070054 ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080055
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";
Tao Baoac3d1ed2017-07-23 00:01:02 -070072 ASSERT_EQ(0, mkdir_recursively(path, 0755, true, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080073
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
Tao Baoac3d1ed2017-07-23 00:01:02 -070086TEST(DirUtilTest, create_mode) {
Tao Baof19295c2016-12-22 09:28:03 -080087 TemporaryDir td;
88 std::string prefix(td.path);
89 std::string path = prefix + "/a/b";
Tao Baof19295c2016-12-22 09:28:03 -080090 constexpr mode_t mode = 0751;
Tao Baoac3d1ed2017-07-23 00:01:02 -070091 ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080092
Tao Baoac3d1ed2017-07-23 00:01:02 -070093 // Verify the mode for "../a/b".
Tao Baof19295c2016-12-22 09:28:03 -080094 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
Tao Baoac3d1ed2017-07-23 00:01:02 -0700100 // Verify the mode for "../a".
Tao Baof19295c2016-12-22 09:28:03 -0800101 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}