1 /*
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/cdefs.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include <algorithm>
29 #include <set>
30 #include <string>
31
CheckProcSelf(std::set<std::string> & names)32 static 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
41 template <typename DirEntT>
ScanEntries(DirEntT ** entries,int entry_count,std::set<std::string> & name_set,std::vector<std::string> & name_list)42 void 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
TEST(dirent,scandir_scandir64)52 TEST(dirent, scandir_scandir64) {
53 // Get everything from /proc/self...
54 dirent** entries;
55 int entry_count = scandir("/proc/self", &entries, nullptr, alphasort);
56 ASSERT_GE(entry_count, 0);
57
58 dirent64** entries64;
59 int entry_count64 = scandir64("/proc/self", &entries64, nullptr, alphasort64);
60 ASSERT_EQ(entry_count, entry_count64);
61
62 // 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;
65 ScanEntries(entries, entry_count, name_set, unsorted_name_list);
66
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
75 // 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
82 CheckProcSelf(name_set);
83 }
84
TEST(dirent,scandirat_scandirat64)85 TEST(dirent, scandirat_scandirat64) {
86 #if !defined(ANDROID_HOST_MUSL)
87 // Get everything from /proc/self...
88 dirent** entries;
89 int entry_count = scandir("/proc/self", &entries, nullptr, alphasort);
90 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;
96 int entry_count_at = scandirat(proc_fd, "self", &entries_at, nullptr, alphasort);
97 ASSERT_EQ(entry_count, entry_count_at);
98
99 dirent64** entries_at64;
100 int entry_count_at64 = scandirat64(proc_fd, "self", &entries_at64, nullptr, alphasort64);
101 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);
116 #else
117 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
118 #endif
119 }
120
is_version_filter(const dirent * de)121 static int is_version_filter(const dirent* de) {
122 return !strcmp(de->d_name, "version");
123 }
124
TEST(dirent,scandir_filter)125 TEST(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
TEST(dirent,scandir_ENOENT)133 TEST(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
TEST(dirent,scandir64_ENOENT)140 TEST(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
TEST(dirent,scandirat_ENOENT)147 TEST(dirent, scandirat_ENOENT) {
148 #if !defined(ANDROID_HOST_MUSL)
149 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);
156 #else
157 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
158 #endif
159 }
160
TEST(dirent,scandirat64_ENOENT)161 TEST(dirent, scandirat64_ENOENT) {
162 #if !defined(ANDROID_HOST_MUSL)
163 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);
170 #else
171 GTEST_SKIP() << "musl doesn't have scandirat or scandirat64";
172 #endif
173 }
174
TEST(dirent,fdopendir_invalid)175 TEST(dirent, fdopendir_invalid) {
176 ASSERT_TRUE(fdopendir(-1) == nullptr);
177 ASSERT_EQ(EBADF, errno);
178
179 int fd = open("/dev/null", O_RDONLY);
180 ASSERT_NE(fd, -1);
181 ASSERT_TRUE(fdopendir(fd) == nullptr);
182 ASSERT_EQ(ENOTDIR, errno);
183 close(fd);
184 }
185
TEST(dirent,fdopendir)186 TEST(dirent, fdopendir) {
187 int fd = open("/proc/self", O_RDONLY);
188 DIR* d = fdopendir(fd);
189 ASSERT_TRUE(d != nullptr);
190 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);
196 ASSERT_EQ(EBADF, errno);
197 }
198
TEST(dirent,opendir_invalid)199 TEST(dirent, opendir_invalid) {
200 ASSERT_TRUE(opendir("/does/not/exist") == nullptr);
201 ASSERT_EQ(ENOENT, errno);
202
203 ASSERT_TRUE(opendir("/dev/null") == nullptr);
204 ASSERT_EQ(ENOTDIR, errno);
205 }
206
TEST(dirent,opendir)207 TEST(dirent, opendir) {
208 DIR* d = opendir("/proc/self");
209 ASSERT_TRUE(d != nullptr);
210 dirent* e = readdir(d);
211 ASSERT_STREQ(e->d_name, ".");
212 ASSERT_EQ(closedir(d), 0);
213 }
214
TEST(dirent,closedir_invalid)215 TEST(dirent, closedir_invalid) {
216 DIR* d = nullptr;
217 ASSERT_EQ(closedir(d), -1);
218 ASSERT_EQ(EINVAL, errno);
219 }
220
TEST(dirent,closedir)221 TEST(dirent, closedir) {
222 DIR* d = opendir("/proc/self");
223 ASSERT_TRUE(d != nullptr);
224 ASSERT_EQ(closedir(d), 0);
225 }
226
TEST(dirent,readdir)227 TEST(dirent, readdir) {
228 DIR* d = opendir("/proc/self");
229 ASSERT_TRUE(d != nullptr);
230 std::set<std::string> name_set;
231 errno = 0;
232 dirent* e;
233 while ((e = readdir(d)) != nullptr) {
234 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.
238 ASSERT_EQ(0, errno);
239 ASSERT_EQ(closedir(d), 0);
240
241 CheckProcSelf(name_set);
242 }
243
TEST(dirent,readdir64_smoke)244 TEST(dirent, readdir64_smoke) {
245 DIR* d = opendir("/proc/self");
246 ASSERT_TRUE(d != nullptr);
247 std::set<std::string> name_set;
248 errno = 0;
249 dirent64* e;
250 while ((e = readdir64(d)) != nullptr) {
251 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
TEST(dirent,readdir_r)261 TEST(dirent, readdir_r) {
262 DIR* d = opendir("/proc/self");
263 ASSERT_TRUE(d != nullptr);
264 std::set<std::string> name_set;
265 errno = 0;
266 dirent storage;
267 dirent* e = nullptr;
268 while (readdir_r(d, &storage, &e) == 0 && e != nullptr) {
269 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.
273 ASSERT_EQ(0, errno);
274 ASSERT_EQ(closedir(d), 0);
275
276 CheckProcSelf(name_set);
277 }
278
TEST(dirent,readdir64_r_smoke)279 TEST(dirent, readdir64_r_smoke) {
280 DIR* d = opendir("/proc/self");
281 ASSERT_TRUE(d != nullptr);
282 std::set<std::string> name_set;
283 errno = 0;
284 dirent64 storage;
285 dirent64* e = nullptr;
286 while (readdir64_r(d, &storage, &e) == 0 && e != nullptr) {
287 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
TEST(dirent,rewinddir)297 TEST(dirent, rewinddir) {
298 DIR* d = opendir("/proc/self");
299 ASSERT_TRUE(d != nullptr);
300
301 // Get all the names once...
302 std::vector<std::string> pass1;
303 dirent* e;
304 while ((e = readdir(d)) != nullptr) {
305 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;
313 while ((e = readdir(d)) != nullptr) {
314 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 }
325
TEST(dirent,seekdir_telldir)326 TEST(dirent, seekdir_telldir) {
327 DIR* d = opendir("/proc/self");
328 ASSERT_TRUE(d != nullptr);
329 std::vector<long> offset_list;
330 std::vector<std::string> name_list;
331 dirent* e = nullptr;
332
333 offset_list.push_back(telldir(d));
334 ASSERT_EQ(0L, offset_list.back());
335
336 while ((e = readdir(d)) != nullptr) {
337 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);
352 ASSERT_TRUE(e != nullptr);
353 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);
359 ASSERT_TRUE(e != nullptr);
360 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;
367 ASSERT_EQ(nullptr, readdir(d));
368 ASSERT_EQ(0, errno);
369
370 ASSERT_EQ(0, closedir(d));
371 }
372