blob: cde2d11be83866ff356b413648dbd6f3b68a8ee1 [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 Hughes95646e62023-09-21 14:11:19 -070032#include "utils.h"
33
Elliott Hughes063cfb22012-10-25 20:55:23 -070034static void CheckProcSelf(std::set<std::string>& names) {
35 // We have a good idea of what should be in /proc/self.
Elliott Hughesf3724772024-06-25 11:18:09 +000036 ASSERT_TRUE(names.contains("."));
37 ASSERT_TRUE(names.contains(".."));
38 ASSERT_TRUE(names.contains("cmdline"));
39 ASSERT_TRUE(names.contains("fd"));
40 ASSERT_TRUE(names.contains("stat"));
Elliott Hughes063cfb22012-10-25 20:55:23 -070041}
42
Elliott Hughesdb1ea342014-01-17 18:42:49 -080043template <typename DirEntT>
44void ScanEntries(DirEntT** entries, int entry_count,
45 std::set<std::string>& name_set, std::vector<std::string>& name_list) {
46 for (size_t i = 0; i < static_cast<size_t>(entry_count); ++i) {
47 name_set.insert(entries[i]->d_name);
48 name_list.push_back(entries[i]->d_name);
49 free(entries[i]);
50 }
51 free(entries);
52}
53
54TEST(dirent, scandir_scandir64) {
Elliott Hughes063cfb22012-10-25 20:55:23 -070055 // Get everything from /proc/self...
56 dirent** entries;
Yi Kong32bc0fc2018-08-02 17:31:13 -070057 int entry_count = scandir("/proc/self", &entries, nullptr, alphasort);
Elliott Hughes063cfb22012-10-25 20:55:23 -070058 ASSERT_GE(entry_count, 0);
59
Elliott Hughesdb1ea342014-01-17 18:42:49 -080060 dirent64** entries64;
Yi Kong32bc0fc2018-08-02 17:31:13 -070061 int entry_count64 = scandir64("/proc/self", &entries64, nullptr, alphasort64);
Elliott Hughesdb1ea342014-01-17 18:42:49 -080062 ASSERT_EQ(entry_count, entry_count64);
63
Elliott Hughes063cfb22012-10-25 20:55:23 -070064 // Turn the directory entries into a set and vector of the names.
65 std::set<std::string> name_set;
66 std::vector<std::string> unsorted_name_list;
Elliott Hughesdb1ea342014-01-17 18:42:49 -080067 ScanEntries(entries, entry_count, name_set, unsorted_name_list);
Elliott Hughes063cfb22012-10-25 20:55:23 -070068
69 // No duplicates.
70 ASSERT_EQ(name_set.size(), unsorted_name_list.size());
71
72 // All entries sorted.
73 std::vector<std::string> sorted_name_list(unsorted_name_list);
74 std::sort(sorted_name_list.begin(), sorted_name_list.end());
75 ASSERT_EQ(sorted_name_list, unsorted_name_list);
76
Elliott Hughesdb1ea342014-01-17 18:42:49 -080077 // scandir64 returned the same results as scandir.
78 std::set<std::string> name_set64;
79 std::vector<std::string> unsorted_name_list64;
80 ScanEntries(entries64, entry_count64, name_set64, unsorted_name_list64);
81 ASSERT_EQ(name_set, name_set64);
82 ASSERT_EQ(unsorted_name_list, unsorted_name_list64);
83
Elliott Hughes063cfb22012-10-25 20:55:23 -070084 CheckProcSelf(name_set);
85}
86
Elliott Hughes6331e802015-10-27 11:10:36 -070087TEST(dirent, scandirat_scandirat64) {
Colin Cross4c5595c2021-08-16 15:51:59 -070088#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes6331e802015-10-27 11:10:36 -070089 // Get everything from /proc/self...
90 dirent** entries;
Yi Kong32bc0fc2018-08-02 17:31:13 -070091 int entry_count = scandir("/proc/self", &entries, nullptr, alphasort);
Elliott Hughes6331e802015-10-27 11:10:36 -070092 ASSERT_GE(entry_count, 0);
93
94 int proc_fd = open("/proc", O_DIRECTORY);
95 ASSERT_NE(-1, proc_fd);
96
97 dirent** entries_at;
Yi Kong32bc0fc2018-08-02 17:31:13 -070098 int entry_count_at = scandirat(proc_fd, "self", &entries_at, nullptr, alphasort);
Elliott Hughes6331e802015-10-27 11:10:36 -070099 ASSERT_EQ(entry_count, entry_count_at);
100
101 dirent64** entries_at64;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700102 int entry_count_at64 = scandirat64(proc_fd, "self", &entries_at64, nullptr, alphasort64);
Elliott Hughes6331e802015-10-27 11:10:36 -0700103 ASSERT_EQ(entry_count, entry_count_at64);
104
105 close(proc_fd);
106
107 // scandirat and scandirat64 should return the same results as scandir.
108 std::set<std::string> name_set, name_set_at, name_set_at64;
109 std::vector<std::string> unsorted_name_list, unsorted_name_list_at, unsorted_name_list_at64;
110 ScanEntries(entries, entry_count, name_set, unsorted_name_list);
111 ScanEntries(entries_at, entry_count_at, name_set_at, unsorted_name_list_at);
112 ScanEntries(entries_at64, entry_count_at64, name_set_at64, unsorted_name_list_at64);
113
114 ASSERT_EQ(name_set, name_set_at);
115 ASSERT_EQ(name_set, name_set_at64);
116 ASSERT_EQ(unsorted_name_list, unsorted_name_list_at);
117 ASSERT_EQ(unsorted_name_list, unsorted_name_list_at64);
Colin Cross7da20342021-07-28 11:18:11 -0700118#else
119 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
120#endif
Elliott Hughes6331e802015-10-27 11:10:36 -0700121}
122
Elliott Hughes7cebf832020-08-12 14:25:41 -0700123static int is_version_filter(const dirent* de) {
124 return !strcmp(de->d_name, "version");
125}
126
127TEST(dirent, scandir_filter) {
128 dirent** entries;
Elliott Hughes7cebf832020-08-12 14:25:41 -0700129 ASSERT_EQ(1, scandir("/proc", &entries, is_version_filter, nullptr));
130 ASSERT_STREQ("version", entries[0]->d_name);
131 free(entries);
132}
133
Elliott Hughes6331e802015-10-27 11:10:36 -0700134TEST(dirent, scandir_ENOENT) {
135 dirent** entries;
136 errno = 0;
137 ASSERT_EQ(-1, scandir("/does-not-exist", &entries, nullptr, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700138 ASSERT_ERRNO(ENOENT);
Elliott Hughes6331e802015-10-27 11:10:36 -0700139}
140
141TEST(dirent, scandir64_ENOENT) {
142 dirent64** entries;
143 errno = 0;
144 ASSERT_EQ(-1, scandir64("/does-not-exist", &entries, nullptr, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700145 ASSERT_ERRNO(ENOENT);
Elliott Hughes6331e802015-10-27 11:10:36 -0700146}
147
148TEST(dirent, scandirat_ENOENT) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700149#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes6331e802015-10-27 11:10:36 -0700150 int root_fd = open("/", O_DIRECTORY | O_RDONLY);
151 ASSERT_NE(-1, root_fd);
152 dirent** entries;
153 errno = 0;
154 ASSERT_EQ(-1, scandirat(root_fd, "does-not-exist", &entries, nullptr, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700155 ASSERT_ERRNO(ENOENT);
Elliott Hughes6331e802015-10-27 11:10:36 -0700156 close(root_fd);
Colin Cross7da20342021-07-28 11:18:11 -0700157#else
158 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
159#endif
Elliott Hughes6331e802015-10-27 11:10:36 -0700160}
161
162TEST(dirent, scandirat64_ENOENT) {
Colin Cross4c5595c2021-08-16 15:51:59 -0700163#if !defined(ANDROID_HOST_MUSL)
Elliott Hughes6331e802015-10-27 11:10:36 -0700164 int root_fd = open("/", O_DIRECTORY | O_RDONLY);
165 ASSERT_NE(-1, root_fd);
166 dirent64** entries;
167 errno = 0;
168 ASSERT_EQ(-1, scandirat64(root_fd, "does-not-exist", &entries, nullptr, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700169 ASSERT_ERRNO(ENOENT);
Elliott Hughes6331e802015-10-27 11:10:36 -0700170 close(root_fd);
Colin Cross7da20342021-07-28 11:18:11 -0700171#else
172 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
173#endif
Elliott Hughes6331e802015-10-27 11:10:36 -0700174}
175
Elliott Hughes063cfb22012-10-25 20:55:23 -0700176TEST(dirent, fdopendir_invalid) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700177 ASSERT_TRUE(fdopendir(-1) == nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -0700178 ASSERT_ERRNO(EBADF);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700179
180 int fd = open("/dev/null", O_RDONLY);
181 ASSERT_NE(fd, -1);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700182 ASSERT_TRUE(fdopendir(fd) == nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -0700183 ASSERT_ERRNO(ENOTDIR);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700184 close(fd);
185}
186
187TEST(dirent, fdopendir) {
188 int fd = open("/proc/self", O_RDONLY);
189 DIR* d = fdopendir(fd);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700190 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700191 dirent* e = readdir(d);
192 ASSERT_STREQ(e->d_name, ".");
193 ASSERT_EQ(closedir(d), 0);
194
195 // fdopendir(3) took ownership, so closedir(3) closed our fd.
196 ASSERT_EQ(close(fd), -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700197 ASSERT_ERRNO(EBADF);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700198}
199
200TEST(dirent, opendir_invalid) {
Elliott Hughes95646e62023-09-21 14:11:19 -0700201 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700202 ASSERT_TRUE(opendir("/does/not/exist") == nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -0700203 ASSERT_ERRNO(ENOENT);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700204
Elliott Hughes95646e62023-09-21 14:11:19 -0700205 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700206 ASSERT_TRUE(opendir("/dev/null") == nullptr);
Elliott Hughes95646e62023-09-21 14:11:19 -0700207 ASSERT_ERRNO(ENOTDIR);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700208}
209
210TEST(dirent, opendir) {
211 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700212 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700213 dirent* e = readdir(d);
214 ASSERT_STREQ(e->d_name, ".");
215 ASSERT_EQ(closedir(d), 0);
216}
217
218TEST(dirent, closedir_invalid) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700219 DIR* d = nullptr;
Elliott Hughes063cfb22012-10-25 20:55:23 -0700220 ASSERT_EQ(closedir(d), -1);
Elliott Hughes95646e62023-09-21 14:11:19 -0700221 ASSERT_ERRNO(EINVAL);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700222}
223
224TEST(dirent, closedir) {
225 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700226 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700227 ASSERT_EQ(closedir(d), 0);
228}
229
230TEST(dirent, readdir) {
231 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700232 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700233 std::set<std::string> name_set;
234 errno = 0;
235 dirent* e;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700236 while ((e = readdir(d)) != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700237 name_set.insert(e->d_name);
238 }
239 // Reading to the end of the directory is not an error.
240 // readdir(3) returns NULL, but leaves errno as 0.
Elliott Hughes95646e62023-09-21 14:11:19 -0700241 ASSERT_ERRNO(0);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700242 ASSERT_EQ(closedir(d), 0);
243
244 CheckProcSelf(name_set);
245}
246
Colin Cross7da20342021-07-28 11:18:11 -0700247TEST(dirent, readdir64_smoke) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800248 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700249 ASSERT_TRUE(d != nullptr);
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800250 std::set<std::string> name_set;
251 errno = 0;
252 dirent64* e;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700253 while ((e = readdir64(d)) != nullptr) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800254 name_set.insert(e->d_name);
255 }
256 // Reading to the end of the directory is not an error.
257 // readdir64(3) returns NULL, but leaves errno as 0.
Elliott Hughes95646e62023-09-21 14:11:19 -0700258 ASSERT_ERRNO(0);
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800259 ASSERT_EQ(closedir(d), 0);
260
261 CheckProcSelf(name_set);
262}
263
Elliott Hughes063cfb22012-10-25 20:55:23 -0700264TEST(dirent, readdir_r) {
265 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700266 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700267 std::set<std::string> name_set;
268 errno = 0;
269 dirent storage;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700270 dirent* e = nullptr;
271 while (readdir_r(d, &storage, &e) == 0 && e != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700272 name_set.insert(e->d_name);
273 }
274 // Reading to the end of the directory is not an error.
275 // readdir_r(3) returns NULL, but leaves errno as 0.
Elliott Hughes95646e62023-09-21 14:11:19 -0700276 ASSERT_ERRNO(0);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700277 ASSERT_EQ(closedir(d), 0);
278
279 CheckProcSelf(name_set);
280}
281
Colin Cross7da20342021-07-28 11:18:11 -0700282TEST(dirent, readdir64_r_smoke) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800283 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700284 ASSERT_TRUE(d != nullptr);
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800285 std::set<std::string> name_set;
286 errno = 0;
287 dirent64 storage;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700288 dirent64* e = nullptr;
289 while (readdir64_r(d, &storage, &e) == 0 && e != nullptr) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800290 name_set.insert(e->d_name);
291 }
292 // Reading to the end of the directory is not an error.
293 // readdir64_r(3) returns NULL, but leaves errno as 0.
Elliott Hughes95646e62023-09-21 14:11:19 -0700294 ASSERT_ERRNO(0);
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800295 ASSERT_EQ(closedir(d), 0);
296
297 CheckProcSelf(name_set);
298}
299
Elliott Hughes063cfb22012-10-25 20:55:23 -0700300TEST(dirent, rewinddir) {
301 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700302 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700303
304 // Get all the names once...
305 std::vector<std::string> pass1;
306 dirent* e;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700307 while ((e = readdir(d)) != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700308 pass1.push_back(e->d_name);
309 }
310
311 // ...rewind...
312 rewinddir(d);
313
314 // ...and get all the names again.
315 std::vector<std::string> pass2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700316 while ((e = readdir(d)) != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700317 pass2.push_back(e->d_name);
318 }
319
320 ASSERT_EQ(closedir(d), 0);
321
322 // We should have seen the same names in the same order both times.
323 ASSERT_EQ(pass1.size(), pass2.size());
324 for (size_t i = 0; i < pass1.size(); ++i) {
325 ASSERT_EQ(pass1[i], pass2[i]);
326 }
327}
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800328
329TEST(dirent, seekdir_telldir) {
330 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700331 ASSERT_TRUE(d != nullptr);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800332 std::vector<long> offset_list;
333 std::vector<std::string> name_list;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700334 dirent* e = nullptr;
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800335
336 offset_list.push_back(telldir(d));
337 ASSERT_EQ(0L, offset_list.back());
338
Yi Kong32bc0fc2018-08-02 17:31:13 -0700339 while ((e = readdir(d)) != nullptr) {
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800340 name_list.push_back(e->d_name);
341 offset_list.push_back(telldir(d));
342 // Make sure telldir() point to the next entry.
343 ASSERT_EQ(e->d_off, offset_list.back());
344 }
345
346 long end_offset = telldir(d);
347 // telldir() should not pass the end of the file.
348 ASSERT_EQ(offset_list.back(), end_offset);
349 offset_list.pop_back();
350
351 for (size_t i = 0; i < offset_list.size(); ++i) {
352 seekdir(d, offset_list[i]);
353 ASSERT_EQ(offset_list[i], telldir(d));
354 e = readdir(d);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700355 ASSERT_TRUE(e != nullptr);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800356 ASSERT_STREQ(name_list[i].c_str(), e->d_name);
357 }
358 for (int i = static_cast<int>(offset_list.size()) - 1; i >= 0; --i) {
359 seekdir(d, offset_list[i]);
360 ASSERT_EQ(offset_list[i], telldir(d));
361 e = readdir(d);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700362 ASSERT_TRUE(e != nullptr);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800363 ASSERT_STREQ(name_list[i].c_str(), e->d_name);
364 }
365
366 // Seek to the end, read NULL.
367 seekdir(d, end_offset);
368 ASSERT_EQ(end_offset, telldir(d));
369 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700370 ASSERT_EQ(nullptr, readdir(d));
Elliott Hughes95646e62023-09-21 14:11:19 -0700371 ASSERT_ERRNO(0);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800372
373 ASSERT_EQ(0, closedir(d));
374}