blob: 4dd111a7034fd6d89ca08104ad5639f5e12af1be [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
Mark Salyzyn8b54bc52018-11-14 15:18:30 -080023#include <android-base/file.h>
Tao Baof19295c2016-12-22 09:28:03 -080024#include <gtest/gtest.h>
Tao Bao17054c02018-05-03 22:41:23 -070025
26#include "otautil/dirutil.h"
Tao Baof19295c2016-12-22 09:28:03 -080027
28TEST(DirUtilTest, create_invalid) {
29 // Requesting to create an empty dir is invalid.
Tao Baoac3d1ed2017-07-23 00:01:02 -070030 ASSERT_EQ(-1, mkdir_recursively("", 0755, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080031 ASSERT_EQ(ENOENT, errno);
32
33 // Requesting to strip the name with no slash present.
Tao Baoac3d1ed2017-07-23 00:01:02 -070034 ASSERT_EQ(-1, mkdir_recursively("abc", 0755, true, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080035 ASSERT_EQ(ENOENT, errno);
36
37 // Creating a dir that already exists.
38 TemporaryDir td;
Tao Baoac3d1ed2017-07-23 00:01:02 -070039 ASSERT_EQ(0, mkdir_recursively(td.path, 0755, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080040
41 // "///" is a valid dir.
Tao Baoac3d1ed2017-07-23 00:01:02 -070042 ASSERT_EQ(0, mkdir_recursively("///", 0755, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080043
44 // Request to create a dir, but a file with the same name already exists.
45 TemporaryFile tf;
Tao Baoac3d1ed2017-07-23 00:01:02 -070046 ASSERT_EQ(-1, mkdir_recursively(tf.path, 0755, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080047 ASSERT_EQ(ENOTDIR, errno);
48}
49
50TEST(DirUtilTest, create_smoke) {
51 TemporaryDir td;
52 std::string prefix(td.path);
53 std::string path = prefix + "/a/b";
54 constexpr mode_t mode = 0755;
Tao Baoac3d1ed2017-07-23 00:01:02 -070055 ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080056
57 // Verify.
58 struct stat sb;
59 ASSERT_EQ(0, stat(path.c_str(), &sb)) << strerror(errno);
60 ASSERT_TRUE(S_ISDIR(sb.st_mode));
61 constexpr mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
62 ASSERT_EQ(mode, sb.st_mode & mask);
63
64 // Clean up.
65 ASSERT_EQ(0, rmdir((prefix + "/a/b").c_str()));
66 ASSERT_EQ(0, rmdir((prefix + "/a").c_str()));
67}
68
69TEST(DirUtilTest, create_strip_filename) {
70 TemporaryDir td;
71 std::string prefix(td.path);
72 std::string path = prefix + "/a/b";
Tao Baoac3d1ed2017-07-23 00:01:02 -070073 ASSERT_EQ(0, mkdir_recursively(path, 0755, true, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080074
75 // Verify that "../a" exists but not "../a/b".
76 struct stat sb;
77 ASSERT_EQ(0, stat((prefix + "/a").c_str(), &sb)) << strerror(errno);
78 ASSERT_TRUE(S_ISDIR(sb.st_mode));
79
80 ASSERT_EQ(-1, stat(path.c_str(), &sb));
81 ASSERT_EQ(ENOENT, errno);
82
83 // Clean up.
84 ASSERT_EQ(0, rmdir((prefix + "/a").c_str()));
85}
86
Tao Baoac3d1ed2017-07-23 00:01:02 -070087TEST(DirUtilTest, create_mode) {
Tao Baof19295c2016-12-22 09:28:03 -080088 TemporaryDir td;
89 std::string prefix(td.path);
90 std::string path = prefix + "/a/b";
Tao Baof19295c2016-12-22 09:28:03 -080091 constexpr mode_t mode = 0751;
Tao Baoac3d1ed2017-07-23 00:01:02 -070092 ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr));
Tao Baof19295c2016-12-22 09:28:03 -080093
Tao Baoac3d1ed2017-07-23 00:01:02 -070094 // Verify the mode for "../a/b".
Tao Baof19295c2016-12-22 09:28:03 -080095 struct stat sb;
96 ASSERT_EQ(0, stat(path.c_str(), &sb)) << strerror(errno);
97 ASSERT_TRUE(S_ISDIR(sb.st_mode));
98 constexpr mode_t mask = S_IRWXU | S_IRWXG | S_IRWXO;
99 ASSERT_EQ(mode, sb.st_mode & mask);
100
Tao Baoac3d1ed2017-07-23 00:01:02 -0700101 // Verify the mode for "../a".
Tao Baof19295c2016-12-22 09:28:03 -0800102 ASSERT_EQ(0, stat((prefix + "/a").c_str(), &sb)) << strerror(errno);
103 ASSERT_TRUE(S_ISDIR(sb.st_mode));
104 ASSERT_EQ(mode, sb.st_mode & mask);
105
106 // Clean up.
107 ASSERT_EQ(0, rmdir((prefix + "/a/b").c_str()));
108 ASSERT_EQ(0, rmdir((prefix + "/a").c_str()));
109}