blob: 200ed4b61160e6cc476342c09fc4a08c3cbf60a0 [file] [log] [blame]
Calin Juravled4934a72014-02-24 16:13:50 +00001/*
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 Juravled4934a72014-02-24 16:13:50 +000017#include <ftw.h>
Elliott Hughes63bd43b2014-11-18 15:57:23 -080018
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080019#include <fcntl.h>
Elliott Hughes13d79ab2016-04-15 17:40:33 -070020#include <pwd.h>
Elliott Hughes7f925092015-02-10 14:15:33 -080021#include <stdio.h>
Calin Juravled4934a72014-02-24 16:13:50 +000022#include <stdlib.h>
23#include <sys/stat.h>
Elliott Hughes7f925092015-02-10 14:15:33 -080024#include <sys/types.h>
25#include <unistd.h>
26
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080027#include <android-base/file.h>
Elliott Hughes13d79ab2016-04-15 17:40:33 -070028#include <android-base/stringprintf.h>
Elliott Hughes63bd43b2014-11-18 15:57:23 -080029#include <gtest/gtest.h>
30
Elliott Hughes7f925092015-02-10 14:15:33 -080031static void MakeTree(const char* root) {
32 char path[PATH_MAX];
33
34 snprintf(path, sizeof(path), "%s/dir", root);
Elliott Hughes0ad256c2015-04-01 12:22:40 -070035 ASSERT_EQ(0, mkdir(path, 0755)) << path;
Elliott Hughes7f925092015-02-10 14:15:33 -080036 snprintf(path, sizeof(path), "%s/dir/sub", root);
Elliott Hughes0ad256c2015-04-01 12:22:40 -070037 ASSERT_EQ(0, mkdir(path, 0555)) << path;
Elliott Hughes7f925092015-02-10 14:15:33 -080038 snprintf(path, sizeof(path), "%s/unreadable-dir", root);
Elliott Hughes0ad256c2015-04-01 12:22:40 -070039 ASSERT_EQ(0, mkdir(path, 0000)) << path;
Elliott Hughes7f925092015-02-10 14:15:33 -080040
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 Hughes13d79ab2016-04-15 17:40:33 -070044 ASSERT_EQ(0, symlink("dir/sub", path));
Elliott Hughes7f925092015-02-10 14:15:33 -080045
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
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070052void smoke_test_ftw(const char* fpath, const struct stat* sb, int tflag) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070053 ASSERT_TRUE(fpath != nullptr);
54 ASSERT_TRUE(sb != nullptr);
Elliott Hughes7f925092015-02-10 14:15:33 -080055
Elliott Hughes13d79ab2016-04-15 17:40:33 -070056 // 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 Hughes63bd43b2014-11-18 15:57:23 -080065 if (S_ISDIR(sb->st_mode)) {
Elliott Hughes13d79ab2016-04-15 17:40:33 -070066 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 Hughes63bd43b2014-11-18 15:57:23 -080071 } else if (S_ISLNK(sb->st_mode)) {
Elliott Hughes7f925092015-02-10 14:15:33 -080072 EXPECT_EQ(FTW_SL, tflag) << fpath;
Elliott Hughes63bd43b2014-11-18 15:57:23 -080073 } else {
Elliott Hughes7f925092015-02-10 14:15:33 -080074 EXPECT_EQ(FTW_F, tflag) << fpath;
Elliott Hughes63bd43b2014-11-18 15:57:23 -080075 }
Calin Juravled4934a72014-02-24 16:13:50 +000076}
77
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070078void smoke_test_nftw(const char* fpath, const struct stat* sb, int tflag, FTW* ftwbuf) {
79 smoke_test_ftw(fpath, sb, tflag);
Elliott Hughes63bd43b2014-11-18 15:57:23 -080080 ASSERT_EQ('/', fpath[ftwbuf->base - 1]) << fpath;
Calin Juravled4934a72014-02-24 16:13:50 +000081}
82
83int check_ftw(const char* fpath, const struct stat* sb, int tflag) {
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070084 smoke_test_ftw(fpath, sb, tflag);
Calin Juravled4934a72014-02-24 16:13:50 +000085 return 0;
86}
87
88int check_ftw64(const char* fpath, const struct stat64* sb, int tflag) {
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070089 smoke_test_ftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag);
Calin Juravled4934a72014-02-24 16:13:50 +000090 return 0;
91}
92
Elliott Hughes13d79ab2016-04-15 17:40:33 -070093int check_nftw(const char* fpath, const struct stat* sb, int tflag, FTW* ftwbuf) {
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070094 smoke_test_nftw(fpath, sb, tflag, ftwbuf);
Calin Juravled4934a72014-02-24 16:13:50 +000095 return 0;
96}
97
Elliott Hughes13d79ab2016-04-15 17:40:33 -070098int check_nftw64(const char* fpath, const struct stat64* sb, int tflag, FTW* ftwbuf) {
Elliott Hughes68ae6ad2020-07-21 16:11:30 -070099 smoke_test_nftw(fpath, reinterpret_cast<const struct stat*>(sb), tflag, ftwbuf);
Calin Juravled4934a72014-02-24 16:13:50 +0000100 return 0;
101}
102
103TEST(ftw, ftw) {
Elliott Hughes7f925092015-02-10 14:15:33 -0800104 TemporaryDir root;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800105 MakeTree(root.path);
106 ASSERT_EQ(0, ftw(root.path, check_ftw, 128));
Calin Juravled4934a72014-02-24 16:13:50 +0000107}
108
109TEST(ftw, ftw64) {
Elliott Hughes7f925092015-02-10 14:15:33 -0800110 TemporaryDir root;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800111 MakeTree(root.path);
112 ASSERT_EQ(0, ftw64(root.path, check_ftw64, 128));
Calin Juravled4934a72014-02-24 16:13:50 +0000113}
114
115TEST(ftw, nftw) {
Elliott Hughes7f925092015-02-10 14:15:33 -0800116 TemporaryDir root;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800117 MakeTree(root.path);
118 ASSERT_EQ(0, nftw(root.path, check_nftw, 128, 0));
Calin Juravled4934a72014-02-24 16:13:50 +0000119}
120
121TEST(ftw, nftw64) {
Elliott Hughes7f925092015-02-10 14:15:33 -0800122 TemporaryDir root;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800123 MakeTree(root.path);
124 ASSERT_EQ(0, nftw64(root.path, check_nftw64, 128, 0));
Calin Juravled4934a72014-02-24 16:13:50 +0000125}
Elliott Hughes13d79ab2016-04-15 17:40:33 -0700126
127template <typename StatT>
128static 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
133template <typename StatT>
134static int bug_28197840_nftw(const char* path, const StatT* sb, int flag, FTW*) {
135 return bug_28197840_ftw(path, sb, flag);
136}
137
138TEST(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
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800148 std::string path = android::base::StringPrintf("%s/unreadable-directory", root.path);
Elliott Hughes13d79ab2016-04-15 17:40:33 -0700149 ASSERT_EQ(0, mkdir(path.c_str(), 0000)) << path;
150
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800151 ASSERT_EQ(0, ftw(root.path, bug_28197840_ftw<struct stat>, 128));
152 ASSERT_EQ(0, ftw64(root.path, bug_28197840_ftw<struct stat64>, 128));
153 ASSERT_EQ(0, nftw(root.path, bug_28197840_nftw<struct stat>, 128, FTW_PHYS));
154 ASSERT_EQ(0, nftw64(root.path, bug_28197840_nftw<struct stat64>, 128, FTW_PHYS));
Elliott Hughes13d79ab2016-04-15 17:40:33 -0700155}
Elliott Hughes70a8f222018-05-07 16:44:13 -0700156
157template <typename StatT>
158static int null_ftw_callback(const char*, const StatT*, int) {
159 return 0;
160}
161
162template <typename StatT>
163static int null_nftw_callback(const char*, const StatT*, int, FTW*) {
164 return 0;
165}
166
167TEST(ftw, ftw_non_existent_ENOENT) {
168 errno = 0;
169 ASSERT_EQ(-1, ftw("/does/not/exist", null_ftw_callback<struct stat>, 128));
170 ASSERT_EQ(ENOENT, errno);
171 errno = 0;
172 ASSERT_EQ(-1, ftw64("/does/not/exist", null_ftw_callback<struct stat64>, 128));
173 ASSERT_EQ(ENOENT, errno);
174}
175
176TEST(ftw, nftw_non_existent_ENOENT) {
177 errno = 0;
178 ASSERT_EQ(-1, nftw("/does/not/exist", null_nftw_callback<struct stat>, 128, FTW_PHYS));
179 ASSERT_EQ(ENOENT, errno);
180 errno = 0;
181 ASSERT_EQ(-1, nftw64("/does/not/exist", null_nftw_callback<struct stat64>, 128, FTW_PHYS));
182 ASSERT_EQ(ENOENT, errno);
183}
184
185TEST(ftw, ftw_empty_ENOENT) {
186 errno = 0;
187 ASSERT_EQ(-1, ftw("", null_ftw_callback<struct stat>, 128));
188 ASSERT_EQ(ENOENT, errno);
189 errno = 0;
190 ASSERT_EQ(-1, ftw64("", null_ftw_callback<struct stat64>, 128));
191 ASSERT_EQ(ENOENT, errno);
192}
193
194TEST(ftw, nftw_empty_ENOENT) {
195 errno = 0;
196 ASSERT_EQ(-1, nftw("", null_nftw_callback<struct stat>, 128, FTW_PHYS));
197 ASSERT_EQ(ENOENT, errno);
198 errno = 0;
199 ASSERT_EQ(-1, nftw64("", null_nftw_callback<struct stat64>, 128, FTW_PHYS));
200 ASSERT_EQ(ENOENT, errno);
201}