Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 17 | #include <ftw.h> |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 18 | |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame^] | 19 | #include <pwd.h> |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 20 | #include <stdio.h> |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <sys/stat.h> |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include "TemporaryFile.h" |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 27 | |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame^] | 28 | #include <android-base/stringprintf.h> |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 29 | #include <gtest/gtest.h> |
| 30 | |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 31 | static void MakeTree(const char* root) { |
| 32 | char path[PATH_MAX]; |
| 33 | |
| 34 | snprintf(path, sizeof(path), "%s/dir", root); |
Elliott Hughes | 0ad256c | 2015-04-01 12:22:40 -0700 | [diff] [blame] | 35 | ASSERT_EQ(0, mkdir(path, 0755)) << path; |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 36 | snprintf(path, sizeof(path), "%s/dir/sub", root); |
Elliott Hughes | 0ad256c | 2015-04-01 12:22:40 -0700 | [diff] [blame] | 37 | ASSERT_EQ(0, mkdir(path, 0555)) << path; |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 38 | snprintf(path, sizeof(path), "%s/unreadable-dir", root); |
Elliott Hughes | 0ad256c | 2015-04-01 12:22:40 -0700 | [diff] [blame] | 39 | ASSERT_EQ(0, mkdir(path, 0000)) << path; |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 40 | |
| 41 | snprintf(path, sizeof(path), "%s/dangler", root); |
| 42 | ASSERT_EQ(0, symlink("/does-not-exist", path)); |
| 43 | snprintf(path, sizeof(path), "%s/symlink", root); |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame^] | 44 | ASSERT_EQ(0, symlink("dir/sub", path)); |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 45 | |
| 46 | int fd; |
| 47 | snprintf(path, sizeof(path), "%s/regular", root); |
| 48 | ASSERT_NE(-1, fd = open(path, O_CREAT|O_TRUNC, 0666)); |
| 49 | ASSERT_EQ(0, close(fd)); |
| 50 | } |
| 51 | |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 52 | void sanity_check_ftw(const char* fpath, const struct stat* sb, int tflag) { |
| 53 | ASSERT_TRUE(fpath != NULL); |
| 54 | ASSERT_TRUE(sb != NULL); |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 55 | |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame^] | 56 | // Was it a case where the struct stat we're given is meaningless? |
| 57 | if (tflag == FTW_NS || tflag == FTW_SLN) { |
| 58 | // If so, double-check that we really can't stat. |
| 59 | struct stat sb; |
| 60 | EXPECT_EQ(-1, stat(fpath, &sb)); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // Otherwise check that the struct stat matches the type flag. |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 65 | if (S_ISDIR(sb->st_mode)) { |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame^] | 66 | if (access(fpath, R_OK) == 0) { |
| 67 | EXPECT_TRUE(tflag == FTW_D || tflag == FTW_DP) << fpath << ' ' << tflag; |
| 68 | } else { |
| 69 | EXPECT_EQ(FTW_DNR, tflag) << fpath; |
| 70 | } |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 71 | } else if (S_ISLNK(sb->st_mode)) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 72 | EXPECT_EQ(FTW_SL, tflag) << fpath; |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 73 | } else { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 74 | EXPECT_EQ(FTW_F, tflag) << fpath; |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 75 | } |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame^] | 78 | void sanity_check_nftw(const char* fpath, const struct stat* sb, int tflag, FTW* ftwbuf) { |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 79 | sanity_check_ftw(fpath, sb, tflag); |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 80 | ASSERT_EQ('/', fpath[ftwbuf->base - 1]) << fpath; |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int check_ftw(const char* fpath, const struct stat* sb, int tflag) { |
| 84 | sanity_check_ftw(fpath, sb, tflag); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int check_ftw64(const char* fpath, const struct stat64* sb, int tflag) { |
| 89 | sanity_check_ftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag); |
| 90 | return 0; |
| 91 | } |
| 92 | |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame^] | 93 | int check_nftw(const char* fpath, const struct stat* sb, int tflag, FTW* ftwbuf) { |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 94 | sanity_check_nftw(fpath, sb, tflag, ftwbuf); |
| 95 | return 0; |
| 96 | } |
| 97 | |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame^] | 98 | int check_nftw64(const char* fpath, const struct stat64* sb, int tflag, FTW* ftwbuf) { |
Elliott Hughes | 63bd43b | 2014-11-18 15:57:23 -0800 | [diff] [blame] | 99 | sanity_check_nftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag, ftwbuf); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | TEST(ftw, ftw) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 104 | TemporaryDir root; |
| 105 | MakeTree(root.dirname); |
| 106 | ASSERT_EQ(0, ftw(root.dirname, check_ftw, 128)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST(ftw, ftw64) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 110 | TemporaryDir root; |
| 111 | MakeTree(root.dirname); |
| 112 | ASSERT_EQ(0, ftw64(root.dirname, check_ftw64, 128)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST(ftw, nftw) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 116 | TemporaryDir root; |
| 117 | MakeTree(root.dirname); |
| 118 | ASSERT_EQ(0, nftw(root.dirname, check_nftw, 128, 0)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | TEST(ftw, nftw64) { |
Elliott Hughes | 7f92509 | 2015-02-10 14:15:33 -0800 | [diff] [blame] | 122 | TemporaryDir root; |
| 123 | MakeTree(root.dirname); |
| 124 | ASSERT_EQ(0, nftw64(root.dirname, check_nftw64, 128, 0)); |
Calin Juravle | d4934a7 | 2014-02-24 16:13:50 +0000 | [diff] [blame] | 125 | } |
Elliott Hughes | 13d79ab | 2016-04-15 17:40:33 -0700 | [diff] [blame^] | 126 | |
| 127 | template <typename StatT> |
| 128 | static int bug_28197840_ftw(const char* path, const StatT*, int flag) { |
| 129 | EXPECT_EQ(strstr(path, "unreadable") != nullptr ? FTW_DNR : FTW_D, flag) << path; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | template <typename StatT> |
| 134 | static int bug_28197840_nftw(const char* path, const StatT* sb, int flag, FTW*) { |
| 135 | return bug_28197840_ftw(path, sb, flag); |
| 136 | } |
| 137 | |
| 138 | TEST(ftw, bug_28197840) { |
| 139 | // Drop root for this test, because root can still read directories even if |
| 140 | // permissions would imply otherwise. |
| 141 | if (getuid() == 0) { |
| 142 | passwd* pwd = getpwnam("shell"); |
| 143 | ASSERT_EQ(0, setuid(pwd->pw_uid)); |
| 144 | } |
| 145 | |
| 146 | TemporaryDir root; |
| 147 | |
| 148 | std::string path = android::base::StringPrintf("%s/unreadable-directory", root.dirname); |
| 149 | ASSERT_EQ(0, mkdir(path.c_str(), 0000)) << path; |
| 150 | |
| 151 | ASSERT_EQ(0, ftw(root.dirname, bug_28197840_ftw<struct stat>, 128)); |
| 152 | ASSERT_EQ(0, ftw64(root.dirname, bug_28197840_ftw<struct stat64>, 128)); |
| 153 | ASSERT_EQ(0, nftw(root.dirname, bug_28197840_nftw<struct stat>, 128, FTW_PHYS)); |
| 154 | ASSERT_EQ(0, nftw64(root.dirname, bug_28197840_nftw<struct stat64>, 128, FTW_PHYS)); |
| 155 | } |