etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / roken / dirent-test.c
blob0a489c92bf604b4dbd8e5bcaa6ff79bc4dd57aef
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.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
17 * distribution.
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 **********************************************************************/
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <direct.h>
38 #include <errno.h>
39 #include <io.h>
40 #include <fcntl.h>
41 #include <sys/stat.h>
42 #include <string.h>
43 #include "dirent.h"
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[] = {
51 "A",
52 "B",
53 "C",
54 "CAA",
55 "CAAA",
56 "CABBBB",
57 "CAABBB.txt",
58 "A filename with spaces"
61 const char * entries_begin_with_C[] = {
62 "C",
63 "CAA",
64 "CAAA",
65 "CABBBB",
66 "CAABBB.txt"
69 const char * entries_end_with_A[] = {
70 "A",
71 "CAA",
72 "CAAA"
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, ...)
81 va_list args;
83 va_start(args, reason);
84 vfprintf(stderr, reason, args);
85 va_end(args);
87 fprintf(stderr, " : errno = %d (%s)\n", errno, strerror(errno));
88 teardown_test();
89 abort();
92 void fail_test_nf(const char * format, ...)
94 va_list args;
96 fprintf(stderr, "FAIL:");
98 va_start(args, format);
99 vfprintf(stderr, format, args);
100 va_end(args);
102 fprintf(stderr, " : errno = %d (%s)\n", errno, strerror(errno));
105 int touch(const char * filename)
107 int fd;
109 fd = _open(filename, _O_CREAT, _S_IREAD| _S_IWRITE);
111 if (fd == -1)
112 return -1;
114 return _close(fd);
117 int setup_test(void)
119 int i;
121 fprintf(stderr, "Creating test directory %s ...\n", TESTDIR);
123 if (_mkdir(TESTDIR))
124 fail_test("Can't create test directory \"" TESTDIR "\"");
126 if (_chdir(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");
136 return 0;
139 int teardown_test(void)
141 char dirname[_MAX_PATH];
142 size_t len;
143 int i;
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)) {
153 /* fallthrough */
155 } else {
156 /* did we create the directory? */
158 if (!_rmdir( TESTDIR )) {
159 fprintf(stderr, "Removed test directory\n");
160 return 0;
161 } else {
162 if (errno == ENOTEMPTY) {
163 if (_chdir(TESTDIR)) {
164 fprintf(stderr, "Can't change to test directory. Aborting cleanup.\n");
165 return -1;
166 } else {
167 /* fallthrough */
169 } else {
170 return -1;
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 */
184 if (_chdir("..")) {
185 fprintf(stderr, "Can't escape test directory. Giving in.\n");
186 return -1;
189 if (_rmdir( TESTDIR )) {
190 fprintf(stderr, "Can't remove test directory.\n");
191 return -1;
194 printf("Cleaned up test directory\n");
195 return 0;
198 int check_list(const char * filespec, const char ** list, int n, int expect_dot_and_dotdot)
200 DIR * d;
201 struct dirent * e;
202 int n_found = 0;
203 int i;
204 int rv = 0;
205 int retry = 1;
207 d = opendir(filespec);
208 if (d == NULL) {
209 fail_test_nf("opendir failed for [%s]", filespec);
210 return -1;
213 printf("Checking filespec [%s]... ", filespec);
215 retry:
216 while ((e = readdir(d)) != NULL) {
217 n_found ++;
219 if (expect_dot_and_dotdot &&
220 (!strcmp(e->d_name, ".") ||
221 !strcmp(e->d_name, "..")))
222 continue;
224 for (i=0; i < n; i++) {
225 if (!strcmp(list[i], e->d_name))
226 break;
229 if (i == n) {
230 fail_test_nf("Found unexpected entry [%s]", e->d_name);
231 rv = -1;
235 if (n_found != n) {
236 fail_test_nf("Unexpected number of entries [%d]. Expected %d", n_found, n);
237 rv = -1;
240 if (retry) {
241 retry = 0;
242 n_found = 0;
244 rewinddir(d);
245 goto retry;
248 if (closedir(d)) {
249 fail_test_nf("closedir() failed");
252 printf("done\n");
254 return rv;
257 int run_tests()
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);
267 return 0;
270 int main(int argc, char ** argv)
272 if (setup_test())
273 return 1;
275 run_tests();
277 teardown_test();
279 return 0;