1
/***********************************************************************
2
* Copyright (c) 2009, Secure Endpoints Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
12
* - Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in
14
* the documentation and/or other materials provided with the
17
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28
* OF THE POSSIBILITY OF SUCH DAMAGE.
30
**********************************************************************/
43
/* Note that we create a known directory structure in a subdirectory
44
of the current directory to run our tests. */
46
#define TESTDIR "dirent-test-dir"
48
const char * dir_entries[] = {
56
"A filename with spaces"
59
const char * entries_begin_with_C[] = {
67
const char * entries_end_with_A[] = {
73
const int n_dir_entries = sizeof(dir_entries)/sizeof(dir_entries[0]);
75
int teardown_test(void);
77
void fail_test(const char * reason, ...)
81
va_start(args, reason);
82
vfprintf(stderr, reason, args);
85
fprintf(stderr, " : errno = %d (%s)\n", errno, strerror(errno));
90
void fail_test_nf(const char * format, ...)
94
fprintf(stderr, "FAIL:");
96
va_start(args, format);
97
vfprintf(stderr, format, args);
100
fprintf(stderr, " : errno = %d (%s)\n", errno, strerror(errno));
103
int touch(const char * filename)
107
fd = _open(filename, _O_CREAT, _S_IREAD| _S_IWRITE);
119
fprintf(stderr, "Creating test directory %s ...\n", TESTDIR);
122
fail_test("Can't create test directory \"" TESTDIR "\"");
125
fail_test("Can't change to test directory");
127
for (i=0; i < n_dir_entries; i++) {
128
if (touch(dir_entries[i]))
129
fail_test("Can't create test file '%s'", dir_entries[i]);
132
fprintf(stderr, "Done with test setup.\n");
137
int teardown_test(void)
139
char dirname[_MAX_PATH];
143
printf ("Begin cleanup...\n");
145
if (_getcwd(dirname, sizeof(dirname)/sizeof(char)) != NULL &&
147
(len = strlen(dirname)) > sizeof(TESTDIR)/sizeof(char) &&
149
!strcmp(dirname + len + 1 - sizeof(TESTDIR)/sizeof(char), TESTDIR)) {
154
/* did we create the directory? */
156
if (!_rmdir( TESTDIR )) {
157
fprintf(stderr, "Removed test directory\n");
160
if (errno == ENOTEMPTY) {
161
if (_chdir(TESTDIR)) {
162
fprintf(stderr, "Can't change to test directory. Aborting cleanup.\n");
173
fprintf(stderr, "Cleaning up test directory %s ...\n", TESTDIR);
175
for (i=0; i < n_dir_entries; i++) {
176
if (_unlink(dir_entries[i])) {
177
/* if the test setup failed, we expect this to happen for
178
at least some files */
183
fprintf(stderr, "Can't escape test directory. Giving in.\n");
187
if (_rmdir( TESTDIR )) {
188
fprintf(stderr, "Can't remove test directory.\n");
192
printf("Cleaned up test directory\n");
196
int check_list(const char * filespec, const char ** list, int n, int expect_dot_and_dotdot)
205
d = opendir(filespec);
207
fail_test_nf("opendir failed for [%s]", filespec);
211
printf("Checking filespec [%s]... ", filespec);
214
while ((e = readdir(d)) != NULL) {
217
if (expect_dot_and_dotdot &&
218
(!strcmp(e->d_name, ".") ||
219
!strcmp(e->d_name, "..")))
222
for (i=0; i < n; i++) {
223
if (!strcmp(list[i], e->d_name))
228
fail_test_nf("Found unexpected entry [%s]", e->d_name);
234
fail_test_nf("Unexpected number of entries [%d]. Expected %d", n_found, n);
247
fail_test_nf("closedir() failed");
257
/* assumes that the test directory has been set up and we have
258
changed into the test directory. */
260
check_list("*", dir_entries, n_dir_entries + 2, 1);
261
check_list("*.*", dir_entries, n_dir_entries + 2, 1);
262
check_list("C*", entries_begin_with_C, sizeof(entries_begin_with_C)/sizeof(entries_begin_with_C[0]), 0);
263
check_list("*A", entries_end_with_A, sizeof(entries_end_with_A)/sizeof(entries_end_with_A[0]), 0);
268
int main(int argc, char ** argv)