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/file.h>
24 #include <gtest/gtest.h>
25 
26 #include "otautil/dirutil.h"
27 
TEST(DirUtilTest,create_invalid)28 TEST(DirUtilTest, create_invalid) {
29   // Requesting to create an empty dir is invalid.
30   ASSERT_EQ(-1, mkdir_recursively("", 0755, false, nullptr));
31   ASSERT_EQ(ENOENT, errno);
32 
33   // Requesting to strip the name with no slash present.
34   ASSERT_EQ(-1, mkdir_recursively("abc", 0755, true, nullptr));
35   ASSERT_EQ(ENOENT, errno);
36 
37   // Creating a dir that already exists.
38   TemporaryDir td;
39   ASSERT_EQ(0, mkdir_recursively(td.path, 0755, false, nullptr));
40 
41   // "///" is a valid dir.
42   ASSERT_EQ(0, mkdir_recursively("///", 0755, false, nullptr));
43 
44   // Request to create a dir, but a file with the same name already exists.
45   TemporaryFile tf;
46   ASSERT_EQ(-1, mkdir_recursively(tf.path, 0755, false, nullptr));
47   ASSERT_EQ(ENOTDIR, errno);
48 }
49 
TEST(DirUtilTest,create_smoke)50 TEST(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;
55   ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr));
56 
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 
TEST(DirUtilTest,create_strip_filename)69 TEST(DirUtilTest, create_strip_filename) {
70   TemporaryDir td;
71   std::string prefix(td.path);
72   std::string path = prefix + "/a/b";
73   ASSERT_EQ(0, mkdir_recursively(path, 0755, true, nullptr));
74 
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 
TEST(DirUtilTest,create_mode)87 TEST(DirUtilTest, create_mode) {
88   TemporaryDir td;
89   std::string prefix(td.path);
90   std::string path = prefix + "/a/b";
91   constexpr mode_t mode = 0751;
92   ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr));
93 
94   // Verify the mode for "../a/b".
95   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 
101   // Verify the mode for "../a".
102   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 }
110