Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 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/test_utils.h> |
| 24 | #include <gtest/gtest.h> |
Tao Bao | 17054c0 | 2018-05-03 22:41:23 -0700 | [diff] [blame] | 25 | |
| 26 | #include "otautil/dirutil.h" |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 27 | |
| 28 | TEST(DirUtilTest, create_invalid) { |
| 29 | // Requesting to create an empty dir is invalid. |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 30 | ASSERT_EQ(-1, mkdir_recursively("", 0755, false, nullptr)); |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 31 | ASSERT_EQ(ENOENT, errno); |
| 32 | |
| 33 | // Requesting to strip the name with no slash present. |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 34 | ASSERT_EQ(-1, mkdir_recursively("abc", 0755, true, nullptr)); |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 35 | ASSERT_EQ(ENOENT, errno); |
| 36 | |
| 37 | // Creating a dir that already exists. |
| 38 | TemporaryDir td; |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 39 | ASSERT_EQ(0, mkdir_recursively(td.path, 0755, false, nullptr)); |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 40 | |
| 41 | // "///" is a valid dir. |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 42 | ASSERT_EQ(0, mkdir_recursively("///", 0755, false, nullptr)); |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 43 | |
| 44 | // Request to create a dir, but a file with the same name already exists. |
| 45 | TemporaryFile tf; |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 46 | ASSERT_EQ(-1, mkdir_recursively(tf.path, 0755, false, nullptr)); |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 47 | ASSERT_EQ(ENOTDIR, errno); |
| 48 | } |
| 49 | |
| 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; |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 55 | ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr)); |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 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 | |
| 69 | TEST(DirUtilTest, create_strip_filename) { |
| 70 | TemporaryDir td; |
| 71 | std::string prefix(td.path); |
| 72 | std::string path = prefix + "/a/b"; |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 73 | ASSERT_EQ(0, mkdir_recursively(path, 0755, true, nullptr)); |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 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 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 87 | TEST(DirUtilTest, create_mode) { |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 88 | TemporaryDir td; |
| 89 | std::string prefix(td.path); |
| 90 | std::string path = prefix + "/a/b"; |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 91 | constexpr mode_t mode = 0751; |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 92 | ASSERT_EQ(0, mkdir_recursively(path, mode, false, nullptr)); |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 93 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 94 | // Verify the mode for "../a/b". |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 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 | |
Tao Bao | ac3d1ed | 2017-07-23 00:01:02 -0700 | [diff] [blame] | 101 | // Verify the mode for "../a". |
Tao Bao | f19295c | 2016-12-22 09:28:03 -0800 | [diff] [blame] | 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 | } |