| 1 |
/* |
| 2 |
* Test File System Helpers |
| 3 |
*/ |
| 4 |
|
| 5 |
#undef NDEBUG |
| 6 |
#include <c-stdaux.h> |
| 7 |
#include <dirent.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <sys/types.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include "util/fs.h" |
| 12 |
#include "util/string.h" |
| 13 |
|
| 14 |
static void test_dir_list(void) { |
| 15 |
_c_cleanup_(c_closedirp) DIR *dir = NULL; |
| 16 |
_c_cleanup_(fs_dirlist_freep) FsDirlist *list = NULL; |
| 17 |
const char *req[] = { |
| 18 |
"bin", |
| 19 |
"dev", |
| 20 |
"etc", |
| 21 |
"lib", |
| 22 |
"proc", |
| 23 |
"run", |
| 24 |
"sys", |
| 25 |
"tmp", |
| 26 |
"usr", |
| 27 |
"var", |
| 28 |
}; |
| 29 |
size_t i, pos; |
| 30 |
int r; |
| 31 |
|
| 32 |
dir = opendir("/"); |
| 33 |
c_assert(dir); |
| 34 |
|
| 35 |
r = fs_dir_list(dir, &list, 0); |
| 36 |
c_assert(!r); |
| 37 |
c_assert(list); |
| 38 |
c_assert(list->n_entries > 0); |
| 39 |
|
| 40 |
/* Verify all expected entries are found in order. */ |
| 41 |
pos = 0; |
| 42 |
for (i = 0; i < list->n_entries; ++i) { |
| 43 |
if (pos >= C_ARRAY_SIZE(req)) |
| 44 |
break; |
| 45 |
|
| 46 |
if (string_equal(list->entries[i]->d_name, req[pos])) |
| 47 |
++pos; |
| 48 |
} |
| 49 |
c_assert(pos == C_ARRAY_SIZE(req)); |
| 50 |
} |
| 51 |
|
| 52 |
int main(int argc, char **argv) { |
| 53 |
test_dir_list(); |
| 54 |
return 0; |
| 55 |
} |