1 /* $NetBSD: dirent-test.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
3 /***********************************************************************
4 * Copyright (c) 2009, Secure Endpoints Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 **********************************************************************/
45 /* Note that we create a known directory structure in a subdirectory
46 of the current directory to run our tests. */
48 #define TESTDIR "dirent-test-dir"
50 const char * dir_entries
[] = {
58 "A filename with spaces"
61 const char * entries_begin_with_C
[] = {
69 const char * entries_end_with_A
[] = {
75 const int n_dir_entries
= sizeof(dir_entries
)/sizeof(dir_entries
[0]);
77 int teardown_test(void);
79 void fail_test(const char * reason
, ...)
83 va_start(args
, reason
);
84 vfprintf(stderr
, reason
, args
);
87 fprintf(stderr
, " : errno = %d (%s)\n", errno
, strerror(errno
));
92 void fail_test_nf(const char * format
, ...)
96 fprintf(stderr
, "FAIL:");
98 va_start(args
, format
);
99 vfprintf(stderr
, format
, args
);
102 fprintf(stderr
, " : errno = %d (%s)\n", errno
, strerror(errno
));
105 int touch(const char * filename
)
109 fd
= _open(filename
, _O_CREAT
, _S_IREAD
| _S_IWRITE
);
121 fprintf(stderr
, "Creating test directory %s ...\n", TESTDIR
);
124 fail_test("Can't create test directory \"" TESTDIR
"\"");
127 fail_test("Can't change to test directory");
129 for (i
=0; i
< n_dir_entries
; i
++) {
130 if (touch(dir_entries
[i
]))
131 fail_test("Can't create test file '%s'", dir_entries
[i
]);
134 fprintf(stderr
, "Done with test setup.\n");
139 int teardown_test(void)
141 char dirname
[_MAX_PATH
];
145 printf ("Begin cleanup...\n");
147 if (_getcwd(dirname
, sizeof(dirname
)/sizeof(char)) != NULL
&&
149 (len
= strlen(dirname
)) > sizeof(TESTDIR
)/sizeof(char) &&
151 !strcmp(dirname
+ len
+ 1 - sizeof(TESTDIR
)/sizeof(char), TESTDIR
)) {
156 /* did we create the directory? */
158 if (!_rmdir( TESTDIR
)) {
159 fprintf(stderr
, "Removed test directory\n");
162 if (errno
== ENOTEMPTY
) {
163 if (_chdir(TESTDIR
)) {
164 fprintf(stderr
, "Can't change to test directory. Aborting cleanup.\n");
175 fprintf(stderr
, "Cleaning up test directory %s ...\n", TESTDIR
);
177 for (i
=0; i
< n_dir_entries
; i
++) {
178 if (_unlink(dir_entries
[i
])) {
179 /* if the test setup failed, we expect this to happen for
180 at least some files */
185 fprintf(stderr
, "Can't escape test directory. Giving in.\n");
189 if (_rmdir( TESTDIR
)) {
190 fprintf(stderr
, "Can't remove test directory.\n");
194 printf("Cleaned up test directory\n");
198 int check_list(const char * filespec
, const char ** list
, int n
, int expect_dot_and_dotdot
)
207 d
= opendir(filespec
);
209 fail_test_nf("opendir failed for [%s]", filespec
);
213 printf("Checking filespec [%s]... ", filespec
);
216 while ((e
= readdir(d
)) != NULL
) {
219 if (expect_dot_and_dotdot
&&
220 (!strcmp(e
->d_name
, ".") ||
221 !strcmp(e
->d_name
, "..")))
224 for (i
=0; i
< n
; i
++) {
225 if (!strcmp(list
[i
], e
->d_name
))
230 fail_test_nf("Found unexpected entry [%s]", e
->d_name
);
236 fail_test_nf("Unexpected number of entries [%d]. Expected %d", n_found
, n
);
249 fail_test_nf("closedir() failed");
259 /* assumes that the test directory has been set up and we have
260 changed into the test directory. */
262 check_list("*", dir_entries
, n_dir_entries
+ 2, 1);
263 check_list("*.*", dir_entries
, n_dir_entries
+ 2, 1);
264 check_list("C*", entries_begin_with_C
, sizeof(entries_begin_with_C
)/sizeof(entries_begin_with_C
[0]), 0);
265 check_list("*A", entries_end_with_A
, sizeof(entries_end_with_A
)/sizeof(entries_end_with_A
[0]), 0);
270 int main(int argc
, char ** argv
)