]> git.saurik.com Git - apple/hfs.git/blob - tests/cases/test-readdir.c
hfs-407.30.1.tar.gz
[apple/hfs.git] / tests / cases / test-readdir.c
1 #include <unistd.h>
2 #include <dirent.h>
3 #include <fcntl.h>
4 #include <sys/stat.h>
5
6 #include "hfs-tests.h"
7 #include "test-utils.h"
8 #include "disk-image.h"
9 #include "systemx.h"
10
11 TEST(readdir)
12
13 static disk_image_t *di;
14
15 int run_readdir(__unused test_ctx_t *ctx)
16 {
17 di = disk_image_get();
18
19 char *path;
20 asprintf(&path, "%s/readdir.data", di->mount_point);
21
22 systemx("/bin/rm", "-rf", path, NULL);
23 assert_no_err(mkdir(path, 0777));
24
25 char *file;
26 asprintf(&file, "%s/file1", path);
27 int fd;
28 int numeof;
29
30 assert_with_errno((fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0666)) >= 0);
31 assert_no_err(close(fd));
32
33 file[strlen(file) - 1] = '2';
34 assert_with_errno((fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0666)) >= 0);
35 assert_no_err(close(fd));
36
37 DIR *dir;
38
39 assert_with_errno((dir = opendir(path)) != NULL);
40
41 struct dirent entry, *dp;
42 assert_no_err(readdir_r(dir, &entry, &dp));
43 assert(!strcmp(dp->d_name, "."));
44 assert_no_err(readdir_r(dir, &entry, &dp));
45 assert(!strcmp(dp->d_name, ".."));
46 assert_no_err(readdir_r(dir, &entry, &dp));
47 assert(!strcmp(dp->d_name, "file1"));
48 assert_no_err(readdir_r(dir, &entry, &dp));
49 assert(!strcmp(dp->d_name, "file2"));
50 assert_no_err(readdir_r(dir, &entry, &dp));
51 assert(dp == NULL);
52
53 closedir(dir);
54
55 assert_with_errno((dir = opendir(path)) != NULL);
56
57 assert_no_err(readdir_r(dir, &entry, &dp));
58 assert(!strcmp(dp->d_name, "."));
59 assert_no_err(readdir_r(dir, &entry, &dp));
60 assert(!strcmp(dp->d_name, ".."));
61 assert_no_err(readdir_r(dir, &entry, &dp));
62 assert(!strcmp(dp->d_name, "file1"));
63 assert_no_err(readdir_r(dir, &entry, &dp));
64 assert(!strcmp(dp->d_name, "file2"));
65 assert_no_err(readdir_r(dir, &entry, &dp));
66 assert(dp == NULL);
67 assert_no_err(readdir_r(dir, &entry, &dp));
68 assert(dp == NULL);
69
70 unlink(file);
71
72 assert_no_err(readdir_r(dir, &entry, &dp));
73 assert(dp == NULL);
74
75 assert_no_err(closedir(dir));
76
77 assert_with_errno((dir = opendir(path)) != NULL);
78
79 assert_no_err(readdir_r(dir, &entry, &dp));
80 assert(!strcmp(dp->d_name, "."));
81 assert_no_err(readdir_r(dir, &entry, &dp));
82 assert(!strcmp(dp->d_name, ".."));
83 assert_no_err(readdir_r(dir, &entry, &dp));
84 assert(!strcmp(dp->d_name, "file1"));
85 assert_no_err(readdir_r(dir, &entry, &dp));
86 assert_no_err(readdir_r(dir, &entry, &dp));
87 assert(dp == NULL);
88 assert_no_err(readdir_r(dir, &entry, &dp));
89 assert(dp == NULL);
90
91 file[strlen(file) - 1] = '1';
92 unlink(file);
93
94 assert_no_err(readdir_r(dir, &entry, &dp));
95 assert(dp == NULL);
96
97 assert_no_err(closedir(dir));
98
99 assert_with_errno((dir = opendir(path)) != NULL);
100 assert_with_errno((fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0666)) >= 0);
101 assert_no_err(close(fd));
102
103 numeof = 0;
104 for (;;) {
105 assert_no_err(readdir_r(dir, &entry, &dp));
106 if (dp !=0)
107 continue;
108 assert_no_err(readdir_r(dir, &entry, &dp));
109 if (++numeof == 3)
110 break;
111 if (numeof == 1)
112 assert_no_err(unlink(file));
113
114 }
115 assert_no_err(closedir(dir));
116
117 return 0;
118 }