1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
20
#include "apr_file_io.h"
21
#include "apr_file_info.h"
22
#include "apr_errno.h"
23
#include "apr_general.h"
27
static void test_mkdir(abts_case *tc, void *data)
32
rv = apr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
33
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
35
rv = apr_stat(&finfo, "data/testdir", APR_FINFO_TYPE, p);
36
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
37
ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype);
40
static void test_mkdir_recurs(abts_case *tc, void *data)
45
rv = apr_dir_make_recursive("data/one/two/three",
46
APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
47
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
49
rv = apr_stat(&finfo, "data/one", APR_FINFO_TYPE, p);
50
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
51
ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype);
53
rv = apr_stat(&finfo, "data/one/two", APR_FINFO_TYPE, p);
54
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
55
ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype);
57
rv = apr_stat(&finfo, "data/one/two/three", APR_FINFO_TYPE, p);
58
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
59
ABTS_INT_EQUAL(tc, APR_DIR, finfo.filetype);
62
static void test_remove(abts_case *tc, void *data)
67
rv = apr_dir_remove("data/testdir", p);
68
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
70
rv = apr_stat(&finfo, "data/testdir", APR_FINFO_TYPE, p);
71
ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
74
static void test_removeall_fail(abts_case *tc, void *data)
78
rv = apr_dir_remove("data/one", p);
79
ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOTEMPTY(rv));
82
static void test_removeall(abts_case *tc, void *data)
86
rv = apr_dir_remove("data/one/two/three", p);
87
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
89
rv = apr_dir_remove("data/one/two", p);
90
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
92
rv = apr_dir_remove("data/one", p);
93
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
96
static void test_remove_notthere(abts_case *tc, void *data)
100
rv = apr_dir_remove("data/notthere", p);
101
ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
104
static void test_mkdir_twice(abts_case *tc, void *data)
108
rv = apr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
109
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
111
rv = apr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
112
ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_EEXIST(rv));
114
rv = apr_dir_remove("data/testdir", p);
115
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
118
static void test_opendir(abts_case *tc, void *data)
123
rv = apr_dir_open(&dir, "data", p);
124
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
128
static void test_opendir_notthere(abts_case *tc, void *data)
133
rv = apr_dir_open(&dir, "notthere", p);
134
ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
137
static void test_closedir(abts_case *tc, void *data)
142
rv = apr_dir_open(&dir, "data", p);
143
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
144
rv = apr_dir_close(dir);
145
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
148
static void test_rewind(abts_case *tc, void *data)
151
apr_finfo_t first, second;
153
APR_ASSERT_SUCCESS(tc, "apr_dir_open failed", apr_dir_open(&dir, "data", p));
155
APR_ASSERT_SUCCESS(tc, "apr_dir_read failed",
156
apr_dir_read(&first, APR_FINFO_DIRENT, dir));
158
APR_ASSERT_SUCCESS(tc, "apr_dir_rewind failed", apr_dir_rewind(dir));
160
APR_ASSERT_SUCCESS(tc, "second apr_dir_read failed",
161
apr_dir_read(&second, APR_FINFO_DIRENT, dir));
163
APR_ASSERT_SUCCESS(tc, "apr_dir_close failed", apr_dir_close(dir));
165
ABTS_STR_EQUAL(tc, first.name, second.name);
168
/* Test for a (fixed) bug in apr_dir_read(). This bug only happened
169
in threadless cases. */
170
static void test_uncleared_errno(abts_case *tc, void *data)
172
apr_file_t *thefile = NULL;
174
apr_int32_t finfo_flags = APR_FINFO_TYPE | APR_FINFO_NAME;
178
rv = apr_dir_make("dir1", APR_OS_DEFAULT, p);
179
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
180
rv = apr_dir_make("dir2", APR_OS_DEFAULT, p);
181
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
182
rv = apr_file_open(&thefile, "dir1/file1",
183
APR_READ | APR_WRITE | APR_CREATE, APR_OS_DEFAULT, p);
184
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
185
rv = apr_file_close(thefile);
186
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
188
/* Try to remove dir1. This should fail because it's not empty.
189
However, on a platform with threads disabled (such as FreeBSD),
190
`errno' will be set as a result. */
191
rv = apr_dir_remove("dir1", p);
192
ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOTEMPTY(rv));
194
/* Read `.' and `..' out of dir2. */
195
rv = apr_dir_open(&this_dir, "dir2", p);
196
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
197
rv = apr_dir_read(&finfo, finfo_flags, this_dir);
198
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
199
rv = apr_dir_read(&finfo, finfo_flags, this_dir);
200
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
202
/* Now, when we attempt to do a third read of empty dir2, and the
203
underlying system readdir() returns NULL, the old value of
204
errno shouldn't cause a false alarm. We should get an ENOENT
205
back from apr_dir_read, and *not* the old errno. */
206
rv = apr_dir_read(&finfo, finfo_flags, this_dir);
207
ABTS_INT_EQUAL(tc, 1, APR_STATUS_IS_ENOENT(rv));
209
rv = apr_dir_close(this_dir);
210
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
213
rv = apr_file_remove("dir1/file1", p);
214
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
215
rv = apr_dir_remove("dir1", p);
216
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
217
rv = apr_dir_remove("dir2", p);
218
ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
222
static void test_rmkdir_nocwd(abts_case *tc, void *data)
227
APR_ASSERT_SUCCESS(tc, "make temp dir",
228
apr_dir_make("dir3", APR_OS_DEFAULT, p));
230
APR_ASSERT_SUCCESS(tc, "obtain cwd", apr_filepath_get(&cwd, 0, p));
232
APR_ASSERT_SUCCESS(tc, "determine path to temp dir",
233
apr_filepath_merge(&path, cwd, "dir3", 0, p));
235
APR_ASSERT_SUCCESS(tc, "change to temp dir", apr_filepath_set(path, p));
237
rv = apr_dir_remove(path, p);
238
/* Some platforms cannot remove a directory which is in use. */
239
if (rv == APR_SUCCESS) {
240
ABTS_ASSERT(tc, "fail to create dir",
241
apr_dir_make_recursive("foobar", APR_OS_DEFAULT,
245
APR_ASSERT_SUCCESS(tc, "restore cwd", apr_filepath_set(cwd, p));
248
apr_dir_remove(path, p);
249
ABTS_NOT_IMPL(tc, "cannot remove in-use directory");
254
abts_suite *testdir(abts_suite *suite)
256
suite = ADD_SUITE(suite)
258
abts_run_test(suite, test_mkdir, NULL);
259
abts_run_test(suite, test_mkdir_recurs, NULL);
260
abts_run_test(suite, test_remove, NULL);
261
abts_run_test(suite, test_removeall_fail, NULL);
262
abts_run_test(suite, test_removeall, NULL);
263
abts_run_test(suite, test_remove_notthere, NULL);
264
abts_run_test(suite, test_mkdir_twice, NULL);
265
abts_run_test(suite, test_rmkdir_nocwd, NULL);
267
abts_run_test(suite, test_rewind, NULL);
269
abts_run_test(suite, test_opendir, NULL);
270
abts_run_test(suite, test_opendir_notthere, NULL);
271
abts_run_test(suite, test_closedir, NULL);
272
abts_run_test(suite, test_uncleared_errno, NULL);