blob: 3ea9cbded70a1445d83f7c87becd9e31328aceea [file] [log] [blame]
Elliott Hughes063cfb22012-10-25 20:55:23 -07001/*
2 * Copyright (C) 2012 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 <gtest/gtest.h>
18
19#include <dirent.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <limits.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070023#include <sys/cdefs.h>
Elliott Hughes063cfb22012-10-25 20:55:23 -070024#include <sys/stat.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28#include <algorithm>
29#include <set>
30#include <string>
31
Elliott Hughes063cfb22012-10-25 20:55:23 -070032static void CheckProcSelf(std::set<std::string>& names) {
33 // We have a good idea of what should be in /proc/self.
34 ASSERT_TRUE(names.find(".") != names.end());
35 ASSERT_TRUE(names.find("..") != names.end());
36 ASSERT_TRUE(names.find("cmdline") != names.end());
37 ASSERT_TRUE(names.find("fd") != names.end());
38 ASSERT_TRUE(names.find("stat") != names.end());
39}
40
Elliott Hughesdb1ea342014-01-17 18:42:49 -080041template <typename DirEntT>
42void ScanEntries(DirEntT** entries, int entry_count,
43 std::set<std::string>& name_set, std::vector<std::string>& name_list) {
44 for (size_t i = 0; i < static_cast<size_t>(entry_count); ++i) {
45 name_set.insert(entries[i]->d_name);
46 name_list.push_back(entries[i]->d_name);
47 free(entries[i]);
48 }
49 free(entries);
50}
51
52TEST(dirent, scandir_scandir64) {
Elliott Hughes063cfb22012-10-25 20:55:23 -070053 // Get everything from /proc/self...
54 dirent** entries;
Yi Kong32bc0fc2018-08-02 17:31:13 -070055 int entry_count = scandir("/proc/self", &entries, nullptr, alphasort);
Elliott Hughes063cfb22012-10-25 20:55:23 -070056 ASSERT_GE(entry_count, 0);
57
Elliott Hughesdb1ea342014-01-17 18:42:49 -080058 dirent64** entries64;
Yi Kong32bc0fc2018-08-02 17:31:13 -070059 int entry_count64 = scandir64("/proc/self", &entries64, nullptr, alphasort64);
Elliott Hughesdb1ea342014-01-17 18:42:49 -080060 ASSERT_EQ(entry_count, entry_count64);
61
Elliott Hughes063cfb22012-10-25 20:55:23 -070062 // Turn the directory entries into a set and vector of the names.
63 std::set<std::string> name_set;
64 std::vector<std::string> unsorted_name_list;
Elliott Hughesdb1ea342014-01-17 18:42:49 -080065 ScanEntries(entries, entry_count, name_set, unsorted_name_list);
Elliott Hughes063cfb22012-10-25 20:55:23 -070066
67 // No duplicates.
68 ASSERT_EQ(name_set.size(), unsorted_name_list.size());
69
70 // All entries sorted.
71 std::vector<std::string> sorted_name_list(unsorted_name_list);
72 std::sort(sorted_name_list.begin(), sorted_name_list.end());
73 ASSERT_EQ(sorted_name_list, unsorted_name_list);
74
Elliott Hughesdb1ea342014-01-17 18:42:49 -080075 // scandir64 returned the same results as scandir.
76 std::set<std::string> name_set64;
77 std::vector<std::string> unsorted_name_list64;
78 ScanEntries(entries64, entry_count64, name_set64, unsorted_name_list64);
79 ASSERT_EQ(name_set, name_set64);
80 ASSERT_EQ(unsorted_name_list, unsorted_name_list64);
81
Elliott Hughes063cfb22012-10-25 20:55:23 -070082 CheckProcSelf(name_set);
83}
84
Elliott Hughes6331e802015-10-27 11:10:36 -070085TEST(dirent, scandirat_scandirat64) {
Colin Cross4c5595c2021-08-16 15:51:59 -070086#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes6331e802015-10-27 11:10:36 -070087 // Get everything from /proc/self...
88 dirent** entries;
Yi Kong32bc0fc2018-08-02 17:31:13 -070089 int entry_count = scandir("/proc/self", &entries, nullptr, alphasort);
Elliott Hughes6331e802015-10-27 11:10:36 -070090 ASSERT_GE(entry_count, 0);
91
92 int proc_fd = open("/proc", O_DIRECTORY);
93 ASSERT_NE(-1, proc_fd);
94
95 dirent** entries_at;
Yi Kong32bc0fc2018-08-02 17:31:13 -070096 int entry_count_at = scandirat(proc_fd, "self", &entries_at, nullptr, alphasort);
Elliott Hughes6331e802015-10-27 11:10:36 -070097 ASSERT_EQ(entry_count, entry_count_at);
98
99 dirent64** entries_at64;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700100 int entry_count_at64 = scandirat64(proc_fd, "self", &entries_at64, nullptr, alphasort64);
Elliott Hughes6331e802015-10-27 11:10:36 -0700101 ASSERT_EQ(entry_count, entry_count_at64);
102
103 close(proc_fd);
104
105 // scandirat and scandirat64 should return the same results as scandir.
106 std::set<std::string> name_set, name_set_at, name_set_at64;
107 std::vector<std::string> unsorted_name_list, unsorted_name_list_at, unsorted_name_list_at64;
108 ScanEntries(entries, entry_count, name_set, unsorted_name_list);
109 ScanEntries(entries_at, entry_count_at, name_set_at, unsorted_name_list_at);
110 ScanEntries(entries_at64, entry_count_at64, name_set_at64, unsorted_name_list_at64);
111
112 ASSERT_EQ(name_set, name_set_at);
113 ASSERT_EQ(name_set, name_set_at64);
114 ASSERT_EQ(unsorted_name_list, unsorted_name_list_at);
115 ASSERT_EQ(unsorted_name_list, unsorted_name_list_at64);
Colin Cross7da20342021-07-28 11:18:11 -0700116#else
117 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
118#endif
Elliott Hughes6331e802015-10-27 11:10:36 -0700119}
120
Elliott Hughes7cebf832020-08-12 14:25:41 -0700121static int is_version_filter(const dirent* de) {
122 return !strcmp(de->d_name, "version");
123}
124
125TEST(dirent, scandir_filter) {
126 dirent** entries;
127 errno = 0;
128 ASSERT_EQ(1, scandir("/proc", &entries, is_version_filter, nullptr));
129 ASSERT_STREQ("version", entries[0]->d_name);
130 free(entries);
131}
132
Elliott Hughes6331e802015-10-27 11:10:36 -0700133TEST(dirent, scandir_ENOENT) {
134 dirent** entries;
135 errno = 0;
136 ASSERT_EQ(-1, scandir("/does-not-exist", &entries, nullptr, nullptr));
137 ASSERT_EQ(ENOENT, errno);
138}
139
140TEST(dirent, scandir64_ENOENT) {
141 dirent64** entries;
142 errno = 0;
143 ASSERT_EQ(-1, scandir64("/does-not-exist", &entries, nullptr, nullptr));
144 ASSERT_EQ(ENOENT, errno);
145}
146
147TEST(dirent, scandirat_ENOENT) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700148#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes6331e802015-10-27 11:10:36 -0700149 int root_fd = open("/", O_DIRECTORY | O_RDONLY);
150 ASSERT_NE(-1, root_fd);
151 dirent** entries;
152 errno = 0;
153 ASSERT_EQ(-1, scandirat(root_fd, "does-not-exist", &entries, nullptr, nullptr));
154 ASSERT_EQ(ENOENT, errno);
155 close(root_fd);
Colin Cross7da20342021-07-28 11:18:11 -0700156#else
157 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
158#endif
Elliott Hughes6331e802015-10-27 11:10:36 -0700159}
160
161TEST(dirent, scandirat64_ENOENT) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700162#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes6331e802015-10-27 11:10:36 -0700163 int root_fd = open("/", O_DIRECTORY | O_RDONLY);
164 ASSERT_NE(-1, root_fd);
165 dirent64** entries;
166 errno = 0;
167 ASSERT_EQ(-1, scandirat64(root_fd, "does-not-exist", &entries, nullptr, nullptr));
168 ASSERT_EQ(ENOENT, errno);
169 close(root_fd);
Colin Cross7da20342021-07-28 11:18:11 -0700170#else
171 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
172#endif
Elliott Hughes6331e802015-10-27 11:10:36 -0700173}
174
Elliott Hughes063cfb22012-10-25 20:55:23 -0700175TEST(dirent, fdopendir_invalid) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700176 ASSERT_TRUE(fdopendir(-1) == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800177 ASSERT_EQ(EBADF, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700178
179 int fd = open("/dev/null", O_RDONLY);
180 ASSERT_NE(fd, -1);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700181 ASSERT_TRUE(fdopendir(fd) == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800182 ASSERT_EQ(ENOTDIR, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700183 close(fd);
184}
185
186TEST(dirent, fdopendir) {
187 int fd = open("/proc/self", O_RDONLY);
188 DIR* d = fdopendir(fd);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700189 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700190 dirent* e = readdir(d);
191 ASSERT_STREQ(e->d_name, ".");
192 ASSERT_EQ(closedir(d), 0);
193
194 // fdopendir(3) took ownership, so closedir(3) closed our fd.
195 ASSERT_EQ(close(fd), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800196 ASSERT_EQ(EBADF, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700197}
198
199TEST(dirent, opendir_invalid) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700200 ASSERT_TRUE(opendir("/does/not/exist") == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800201 ASSERT_EQ(ENOENT, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700202
Yi Kong32bc0fc2018-08-02 17:31:13 -0700203 ASSERT_TRUE(opendir("/dev/null") == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800204 ASSERT_EQ(ENOTDIR, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700205}
206
207TEST(dirent, opendir) {
208 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700209 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700210 dirent* e = readdir(d);
211 ASSERT_STREQ(e->d_name, ".");
212 ASSERT_EQ(closedir(d), 0);
213}
214
215TEST(dirent, closedir_invalid) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700216 DIR* d = nullptr;
Elliott Hughes063cfb22012-10-25 20:55:23 -0700217 ASSERT_EQ(closedir(d), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800218 ASSERT_EQ(EINVAL, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700219}
220
221TEST(dirent, closedir) {
222 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700223 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700224 ASSERT_EQ(closedir(d), 0);
225}
226
227TEST(dirent, readdir) {
228 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700229 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700230 std::set<std::string> name_set;
231 errno = 0;
232 dirent* e;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700233 while ((e = readdir(d)) != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700234 name_set.insert(e->d_name);
235 }
236 // Reading to the end of the directory is not an error.
237 // readdir(3) returns NULL, but leaves errno as 0.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800238 ASSERT_EQ(0, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700239 ASSERT_EQ(closedir(d), 0);
240
241 CheckProcSelf(name_set);
242}
243
Colin Cross7da20342021-07-28 11:18:11 -0700244TEST(dirent, readdir64_smoke) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800245 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700246 ASSERT_TRUE(d != nullptr);
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800247 std::set<std::string> name_set;
248 errno = 0;
249 dirent64* e;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700250 while ((e = readdir64(d)) != nullptr) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800251 name_set.insert(e->d_name);
252 }
253 // Reading to the end of the directory is not an error.
254 // readdir64(3) returns NULL, but leaves errno as 0.
255 ASSERT_EQ(0, errno);
256 ASSERT_EQ(closedir(d), 0);
257
258 CheckProcSelf(name_set);
259}
260
Elliott Hughes063cfb22012-10-25 20:55:23 -0700261TEST(dirent, readdir_r) {
262 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700263 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700264 std::set<std::string> name_set;
265 errno = 0;
266 dirent storage;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700267 dirent* e = nullptr;
268 while (readdir_r(d, &storage, &e) == 0 && e != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700269 name_set.insert(e->d_name);
270 }
271 // Reading to the end of the directory is not an error.
272 // readdir_r(3) returns NULL, but leaves errno as 0.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800273 ASSERT_EQ(0, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700274 ASSERT_EQ(closedir(d), 0);
275
276 CheckProcSelf(name_set);
277}
278
Colin Cross7da20342021-07-28 11:18:11 -0700279TEST(dirent, readdir64_r_smoke) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800280 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700281 ASSERT_TRUE(d != nullptr);
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800282 std::set<std::string> name_set;
283 errno = 0;
284 dirent64 storage;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700285 dirent64* e = nullptr;
286 while (readdir64_r(d, &storage, &e) == 0 && e != nullptr) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800287 name_set.insert(e->d_name);
288 }
289 // Reading to the end of the directory is not an error.
290 // readdir64_r(3) returns NULL, but leaves errno as 0.
291 ASSERT_EQ(0, errno);
292 ASSERT_EQ(closedir(d), 0);
293
294 CheckProcSelf(name_set);
295}
296
Elliott Hughes063cfb22012-10-25 20:55:23 -0700297TEST(dirent, rewinddir) {
298 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700299 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700300
301 // Get all the names once...
302 std::vector<std::string> pass1;
303 dirent* e;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700304 while ((e = readdir(d)) != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700305 pass1.push_back(e->d_name);
306 }
307
308 // ...rewind...
309 rewinddir(d);
310
311 // ...and get all the names again.
312 std::vector<std::string> pass2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700313 while ((e = readdir(d)) != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700314 pass2.push_back(e->d_name);
315 }
316
317 ASSERT_EQ(closedir(d), 0);
318
319 // We should have seen the same names in the same order both times.
320 ASSERT_EQ(pass1.size(), pass2.size());
321 for (size_t i = 0; i < pass1.size(); ++i) {
322 ASSERT_EQ(pass1[i], pass2[i]);
323 }
324}
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800325
326TEST(dirent, seekdir_telldir) {
327 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700328 ASSERT_TRUE(d != nullptr);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800329 std::vector<long> offset_list;
330 std::vector<std::string> name_list;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700331 dirent* e = nullptr;
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800332
333 offset_list.push_back(telldir(d));
334 ASSERT_EQ(0L, offset_list.back());
335
Yi Kong32bc0fc2018-08-02 17:31:13 -0700336 while ((e = readdir(d)) != nullptr) {
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800337 name_list.push_back(e->d_name);
338 offset_list.push_back(telldir(d));
339 // Make sure telldir() point to the next entry.
340 ASSERT_EQ(e->d_off, offset_list.back());
341 }
342
343 long end_offset = telldir(d);
344 // telldir() should not pass the end of the file.
345 ASSERT_EQ(offset_list.back(), end_offset);
346 offset_list.pop_back();
347
348 for (size_t i = 0; i < offset_list.size(); ++i) {
349 seekdir(d, offset_list[i]);
350 ASSERT_EQ(offset_list[i], telldir(d));
351 e = readdir(d);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700352 ASSERT_TRUE(e != nullptr);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800353 ASSERT_STREQ(name_list[i].c_str(), e->d_name);
354 }
355 for (int i = static_cast<int>(offset_list.size()) - 1; i >= 0; --i) {
356 seekdir(d, offset_list[i]);
357 ASSERT_EQ(offset_list[i], telldir(d));
358 e = readdir(d);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700359 ASSERT_TRUE(e != nullptr);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800360 ASSERT_STREQ(name_list[i].c_str(), e->d_name);
361 }
362
363 // Seek to the end, read NULL.
364 seekdir(d, end_offset);
365 ASSERT_EQ(end_offset, telldir(d));
366 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700367 ASSERT_EQ(nullptr, readdir(d));
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800368 ASSERT_EQ(0, errno);
369
370 ASSERT_EQ(0, closedir(d));
371}