blob: 1155a4e902e9210231906cbf06284dfebac5eea3 [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>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include <algorithm>
28#include <set>
29#include <string>
30
Elliott Hughes063cfb22012-10-25 20:55:23 -070031static void CheckProcSelf(std::set<std::string>& names) {
32 // We have a good idea of what should be in /proc/self.
33 ASSERT_TRUE(names.find(".") != names.end());
34 ASSERT_TRUE(names.find("..") != names.end());
35 ASSERT_TRUE(names.find("cmdline") != names.end());
36 ASSERT_TRUE(names.find("fd") != names.end());
37 ASSERT_TRUE(names.find("stat") != names.end());
38}
39
Elliott Hughesdb1ea342014-01-17 18:42:49 -080040template <typename DirEntT>
41void ScanEntries(DirEntT** entries, int entry_count,
42 std::set<std::string>& name_set, std::vector<std::string>& name_list) {
43 for (size_t i = 0; i < static_cast<size_t>(entry_count); ++i) {
44 name_set.insert(entries[i]->d_name);
45 name_list.push_back(entries[i]->d_name);
46 free(entries[i]);
47 }
48 free(entries);
49}
50
51TEST(dirent, scandir_scandir64) {
Elliott Hughes063cfb22012-10-25 20:55:23 -070052 // Get everything from /proc/self...
53 dirent** entries;
Yi Kong32bc0fc2018-08-02 17:31:13 -070054 int entry_count = scandir("/proc/self", &entries, nullptr, alphasort);
Elliott Hughes063cfb22012-10-25 20:55:23 -070055 ASSERT_GE(entry_count, 0);
56
Elliott Hughesdb1ea342014-01-17 18:42:49 -080057 dirent64** entries64;
Yi Kong32bc0fc2018-08-02 17:31:13 -070058 int entry_count64 = scandir64("/proc/self", &entries64, nullptr, alphasort64);
Elliott Hughesdb1ea342014-01-17 18:42:49 -080059 ASSERT_EQ(entry_count, entry_count64);
60
Elliott Hughes063cfb22012-10-25 20:55:23 -070061 // Turn the directory entries into a set and vector of the names.
62 std::set<std::string> name_set;
63 std::vector<std::string> unsorted_name_list;
Elliott Hughesdb1ea342014-01-17 18:42:49 -080064 ScanEntries(entries, entry_count, name_set, unsorted_name_list);
Elliott Hughes063cfb22012-10-25 20:55:23 -070065
66 // No duplicates.
67 ASSERT_EQ(name_set.size(), unsorted_name_list.size());
68
69 // All entries sorted.
70 std::vector<std::string> sorted_name_list(unsorted_name_list);
71 std::sort(sorted_name_list.begin(), sorted_name_list.end());
72 ASSERT_EQ(sorted_name_list, unsorted_name_list);
73
Elliott Hughesdb1ea342014-01-17 18:42:49 -080074 // scandir64 returned the same results as scandir.
75 std::set<std::string> name_set64;
76 std::vector<std::string> unsorted_name_list64;
77 ScanEntries(entries64, entry_count64, name_set64, unsorted_name_list64);
78 ASSERT_EQ(name_set, name_set64);
79 ASSERT_EQ(unsorted_name_list, unsorted_name_list64);
80
Elliott Hughes063cfb22012-10-25 20:55:23 -070081 CheckProcSelf(name_set);
82}
83
Elliott Hughes6331e802015-10-27 11:10:36 -070084TEST(dirent, scandirat_scandirat64) {
Colin Cross7da20342021-07-28 11:18:11 -070085#if !defined(MUSL)
Elliott Hughes6331e802015-10-27 11:10:36 -070086 // Get everything from /proc/self...
87 dirent** entries;
Yi Kong32bc0fc2018-08-02 17:31:13 -070088 int entry_count = scandir("/proc/self", &entries, nullptr, alphasort);
Elliott Hughes6331e802015-10-27 11:10:36 -070089 ASSERT_GE(entry_count, 0);
90
91 int proc_fd = open("/proc", O_DIRECTORY);
92 ASSERT_NE(-1, proc_fd);
93
94 dirent** entries_at;
Yi Kong32bc0fc2018-08-02 17:31:13 -070095 int entry_count_at = scandirat(proc_fd, "self", &entries_at, nullptr, alphasort);
Elliott Hughes6331e802015-10-27 11:10:36 -070096 ASSERT_EQ(entry_count, entry_count_at);
97
98 dirent64** entries_at64;
Yi Kong32bc0fc2018-08-02 17:31:13 -070099 int entry_count_at64 = scandirat64(proc_fd, "self", &entries_at64, nullptr, alphasort64);
Elliott Hughes6331e802015-10-27 11:10:36 -0700100 ASSERT_EQ(entry_count, entry_count_at64);
101
102 close(proc_fd);
103
104 // scandirat and scandirat64 should return the same results as scandir.
105 std::set<std::string> name_set, name_set_at, name_set_at64;
106 std::vector<std::string> unsorted_name_list, unsorted_name_list_at, unsorted_name_list_at64;
107 ScanEntries(entries, entry_count, name_set, unsorted_name_list);
108 ScanEntries(entries_at, entry_count_at, name_set_at, unsorted_name_list_at);
109 ScanEntries(entries_at64, entry_count_at64, name_set_at64, unsorted_name_list_at64);
110
111 ASSERT_EQ(name_set, name_set_at);
112 ASSERT_EQ(name_set, name_set_at64);
113 ASSERT_EQ(unsorted_name_list, unsorted_name_list_at);
114 ASSERT_EQ(unsorted_name_list, unsorted_name_list_at64);
Colin Cross7da20342021-07-28 11:18:11 -0700115#else
116 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
117#endif
Elliott Hughes6331e802015-10-27 11:10:36 -0700118}
119
Elliott Hughes7cebf832020-08-12 14:25:41 -0700120static int is_version_filter(const dirent* de) {
121 return !strcmp(de->d_name, "version");
122}
123
124TEST(dirent, scandir_filter) {
125 dirent** entries;
126 errno = 0;
127 ASSERT_EQ(1, scandir("/proc", &entries, is_version_filter, nullptr));
128 ASSERT_STREQ("version", entries[0]->d_name);
129 free(entries);
130}
131
Elliott Hughes6331e802015-10-27 11:10:36 -0700132TEST(dirent, scandir_ENOENT) {
133 dirent** entries;
134 errno = 0;
135 ASSERT_EQ(-1, scandir("/does-not-exist", &entries, nullptr, nullptr));
136 ASSERT_EQ(ENOENT, errno);
137}
138
139TEST(dirent, scandir64_ENOENT) {
140 dirent64** entries;
141 errno = 0;
142 ASSERT_EQ(-1, scandir64("/does-not-exist", &entries, nullptr, nullptr));
143 ASSERT_EQ(ENOENT, errno);
144}
145
146TEST(dirent, scandirat_ENOENT) {
Colin Cross7da20342021-07-28 11:18:11 -0700147#if !defined(MUSL)
Elliott Hughes6331e802015-10-27 11:10:36 -0700148 int root_fd = open("/", O_DIRECTORY | O_RDONLY);
149 ASSERT_NE(-1, root_fd);
150 dirent** entries;
151 errno = 0;
152 ASSERT_EQ(-1, scandirat(root_fd, "does-not-exist", &entries, nullptr, nullptr));
153 ASSERT_EQ(ENOENT, errno);
154 close(root_fd);
Colin Cross7da20342021-07-28 11:18:11 -0700155#else
156 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
157#endif
Elliott Hughes6331e802015-10-27 11:10:36 -0700158}
159
160TEST(dirent, scandirat64_ENOENT) {
Colin Cross7da20342021-07-28 11:18:11 -0700161#if !defined(MUSL)
Elliott Hughes6331e802015-10-27 11:10:36 -0700162 int root_fd = open("/", O_DIRECTORY | O_RDONLY);
163 ASSERT_NE(-1, root_fd);
164 dirent64** entries;
165 errno = 0;
166 ASSERT_EQ(-1, scandirat64(root_fd, "does-not-exist", &entries, nullptr, nullptr));
167 ASSERT_EQ(ENOENT, errno);
168 close(root_fd);
Colin Cross7da20342021-07-28 11:18:11 -0700169#else
170 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
171#endif
Elliott Hughes6331e802015-10-27 11:10:36 -0700172}
173
Elliott Hughes063cfb22012-10-25 20:55:23 -0700174TEST(dirent, fdopendir_invalid) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700175 ASSERT_TRUE(fdopendir(-1) == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800176 ASSERT_EQ(EBADF, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700177
178 int fd = open("/dev/null", O_RDONLY);
179 ASSERT_NE(fd, -1);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700180 ASSERT_TRUE(fdopendir(fd) == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800181 ASSERT_EQ(ENOTDIR, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700182 close(fd);
183}
184
185TEST(dirent, fdopendir) {
186 int fd = open("/proc/self", O_RDONLY);
187 DIR* d = fdopendir(fd);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700188 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700189 dirent* e = readdir(d);
190 ASSERT_STREQ(e->d_name, ".");
191 ASSERT_EQ(closedir(d), 0);
192
193 // fdopendir(3) took ownership, so closedir(3) closed our fd.
194 ASSERT_EQ(close(fd), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800195 ASSERT_EQ(EBADF, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700196}
197
198TEST(dirent, opendir_invalid) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700199 ASSERT_TRUE(opendir("/does/not/exist") == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800200 ASSERT_EQ(ENOENT, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700201
Yi Kong32bc0fc2018-08-02 17:31:13 -0700202 ASSERT_TRUE(opendir("/dev/null") == nullptr);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800203 ASSERT_EQ(ENOTDIR, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700204}
205
206TEST(dirent, opendir) {
207 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700208 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700209 dirent* e = readdir(d);
210 ASSERT_STREQ(e->d_name, ".");
211 ASSERT_EQ(closedir(d), 0);
212}
213
214TEST(dirent, closedir_invalid) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700215 DIR* d = nullptr;
Elliott Hughes063cfb22012-10-25 20:55:23 -0700216 ASSERT_EQ(closedir(d), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800217 ASSERT_EQ(EINVAL, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700218}
219
220TEST(dirent, closedir) {
221 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700222 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700223 ASSERT_EQ(closedir(d), 0);
224}
225
226TEST(dirent, readdir) {
227 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700228 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700229 std::set<std::string> name_set;
230 errno = 0;
231 dirent* e;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700232 while ((e = readdir(d)) != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700233 name_set.insert(e->d_name);
234 }
235 // Reading to the end of the directory is not an error.
236 // readdir(3) returns NULL, but leaves errno as 0.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800237 ASSERT_EQ(0, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700238 ASSERT_EQ(closedir(d), 0);
239
240 CheckProcSelf(name_set);
241}
242
Colin Cross7da20342021-07-28 11:18:11 -0700243TEST(dirent, readdir64_smoke) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800244 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700245 ASSERT_TRUE(d != nullptr);
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800246 std::set<std::string> name_set;
247 errno = 0;
248 dirent64* e;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700249 while ((e = readdir64(d)) != nullptr) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800250 name_set.insert(e->d_name);
251 }
252 // Reading to the end of the directory is not an error.
253 // readdir64(3) returns NULL, but leaves errno as 0.
254 ASSERT_EQ(0, errno);
255 ASSERT_EQ(closedir(d), 0);
256
257 CheckProcSelf(name_set);
258}
259
Elliott Hughes063cfb22012-10-25 20:55:23 -0700260TEST(dirent, readdir_r) {
261 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700262 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700263 std::set<std::string> name_set;
264 errno = 0;
265 dirent storage;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700266 dirent* e = nullptr;
267 while (readdir_r(d, &storage, &e) == 0 && e != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700268 name_set.insert(e->d_name);
269 }
270 // Reading to the end of the directory is not an error.
271 // readdir_r(3) returns NULL, but leaves errno as 0.
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800272 ASSERT_EQ(0, errno);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700273 ASSERT_EQ(closedir(d), 0);
274
275 CheckProcSelf(name_set);
276}
277
Colin Cross7da20342021-07-28 11:18:11 -0700278TEST(dirent, readdir64_r_smoke) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800279 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700280 ASSERT_TRUE(d != nullptr);
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800281 std::set<std::string> name_set;
282 errno = 0;
283 dirent64 storage;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700284 dirent64* e = nullptr;
285 while (readdir64_r(d, &storage, &e) == 0 && e != nullptr) {
Elliott Hughesdb1ea342014-01-17 18:42:49 -0800286 name_set.insert(e->d_name);
287 }
288 // Reading to the end of the directory is not an error.
289 // readdir64_r(3) returns NULL, but leaves errno as 0.
290 ASSERT_EQ(0, errno);
291 ASSERT_EQ(closedir(d), 0);
292
293 CheckProcSelf(name_set);
294}
295
Elliott Hughes063cfb22012-10-25 20:55:23 -0700296TEST(dirent, rewinddir) {
297 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700298 ASSERT_TRUE(d != nullptr);
Elliott Hughes063cfb22012-10-25 20:55:23 -0700299
300 // Get all the names once...
301 std::vector<std::string> pass1;
302 dirent* e;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700303 while ((e = readdir(d)) != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700304 pass1.push_back(e->d_name);
305 }
306
307 // ...rewind...
308 rewinddir(d);
309
310 // ...and get all the names again.
311 std::vector<std::string> pass2;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700312 while ((e = readdir(d)) != nullptr) {
Elliott Hughes063cfb22012-10-25 20:55:23 -0700313 pass2.push_back(e->d_name);
314 }
315
316 ASSERT_EQ(closedir(d), 0);
317
318 // We should have seen the same names in the same order both times.
319 ASSERT_EQ(pass1.size(), pass2.size());
320 for (size_t i = 0; i < pass1.size(); ++i) {
321 ASSERT_EQ(pass1[i], pass2[i]);
322 }
323}
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800324
325TEST(dirent, seekdir_telldir) {
326 DIR* d = opendir("/proc/self");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700327 ASSERT_TRUE(d != nullptr);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800328 std::vector<long> offset_list;
329 std::vector<std::string> name_list;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700330 dirent* e = nullptr;
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800331
332 offset_list.push_back(telldir(d));
333 ASSERT_EQ(0L, offset_list.back());
334
Yi Kong32bc0fc2018-08-02 17:31:13 -0700335 while ((e = readdir(d)) != nullptr) {
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800336 name_list.push_back(e->d_name);
337 offset_list.push_back(telldir(d));
338 // Make sure telldir() point to the next entry.
339 ASSERT_EQ(e->d_off, offset_list.back());
340 }
341
342 long end_offset = telldir(d);
343 // telldir() should not pass the end of the file.
344 ASSERT_EQ(offset_list.back(), end_offset);
345 offset_list.pop_back();
346
347 for (size_t i = 0; i < offset_list.size(); ++i) {
348 seekdir(d, offset_list[i]);
349 ASSERT_EQ(offset_list[i], telldir(d));
350 e = readdir(d);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700351 ASSERT_TRUE(e != nullptr);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800352 ASSERT_STREQ(name_list[i].c_str(), e->d_name);
353 }
354 for (int i = static_cast<int>(offset_list.size()) - 1; i >= 0; --i) {
355 seekdir(d, offset_list[i]);
356 ASSERT_EQ(offset_list[i], telldir(d));
357 e = readdir(d);
Yi Kong32bc0fc2018-08-02 17:31:13 -0700358 ASSERT_TRUE(e != nullptr);
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800359 ASSERT_STREQ(name_list[i].c_str(), e->d_name);
360 }
361
362 // Seek to the end, read NULL.
363 seekdir(d, end_offset);
364 ASSERT_EQ(end_offset, telldir(d));
365 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700366 ASSERT_EQ(nullptr, readdir(d));
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800367 ASSERT_EQ(0, errno);
368
369 ASSERT_EQ(0, closedir(d));
370}