1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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(CuTest *tc)
32 rv = apr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
33 CuAssertIntEquals(tc, APR_SUCCESS, rv);
35 rv = apr_stat(&finfo, "data/testdir", APR_FINFO_TYPE, p);
36 CuAssertIntEquals(tc, APR_SUCCESS, rv);
37 CuAssertIntEquals(tc, APR_DIR, finfo.filetype);
40 static void test_mkdir_recurs(CuTest *tc)
45 rv = apr_dir_make_recursive("data/one/two/three",
46 APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
47 CuAssertIntEquals(tc, APR_SUCCESS, rv);
49 rv = apr_stat(&finfo, "data/one", APR_FINFO_TYPE, p);
50 CuAssertIntEquals(tc, APR_SUCCESS, rv);
51 CuAssertIntEquals(tc, APR_DIR, finfo.filetype);
53 rv = apr_stat(&finfo, "data/one/two", APR_FINFO_TYPE, p);
54 CuAssertIntEquals(tc, APR_SUCCESS, rv);
55 CuAssertIntEquals(tc, APR_DIR, finfo.filetype);
57 rv = apr_stat(&finfo, "data/one/two/three", APR_FINFO_TYPE, p);
58 CuAssertIntEquals(tc, APR_SUCCESS, rv);
59 CuAssertIntEquals(tc, APR_DIR, finfo.filetype);
62 static void test_remove(CuTest *tc)
67 rv = apr_dir_remove("data/testdir", p);
68 CuAssertIntEquals(tc, APR_SUCCESS, rv);
70 rv = apr_stat(&finfo, "data/testdir", APR_FINFO_TYPE, p);
71 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
74 static void test_removeall_fail(CuTest *tc)
78 rv = apr_dir_remove("data/one", p);
79 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOTEMPTY(rv));
82 static void test_removeall(CuTest *tc)
86 rv = apr_dir_remove("data/one/two/three", p);
87 CuAssertIntEquals(tc, APR_SUCCESS, rv);
89 rv = apr_dir_remove("data/one/two", p);
90 CuAssertIntEquals(tc, APR_SUCCESS, rv);
92 rv = apr_dir_remove("data/one", p);
93 CuAssertIntEquals(tc, APR_SUCCESS, rv);
96 static void test_remove_notthere(CuTest *tc)
100 rv = apr_dir_remove("data/notthere", p);
101 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
104 static void test_mkdir_twice(CuTest *tc)
108 rv = apr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
109 CuAssertIntEquals(tc, APR_SUCCESS, rv);
111 rv = apr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
112 CuAssertIntEquals(tc, 1, APR_STATUS_IS_EEXIST(rv));
114 rv = apr_dir_remove("data/testdir", p);
115 CuAssertIntEquals(tc, APR_SUCCESS, rv);
118 static void test_opendir(CuTest *tc)
123 rv = apr_dir_open(&dir, "data", p);
124 CuAssertIntEquals(tc, APR_SUCCESS, rv);
128 static void test_opendir_notthere(CuTest *tc)
133 rv = apr_dir_open(&dir, "notthere", p);
134 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
137 static void test_closedir(CuTest *tc)
142 rv = apr_dir_open(&dir, "data", p);
143 CuAssertIntEquals(tc, APR_SUCCESS, rv);
144 rv = apr_dir_close(dir);
145 CuAssertIntEquals(tc, APR_SUCCESS, rv);
148 static void test_rewind(CuTest *tc)
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 CuAssertStrEquals(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(CuTest *tc)
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 CuAssertIntEquals(tc, APR_SUCCESS, rv);
180 rv = apr_dir_make("dir2", APR_OS_DEFAULT, p);
181 CuAssertIntEquals(tc, APR_SUCCESS, rv);
182 rv = apr_file_open(&thefile, "dir1/file1",
183 APR_READ | APR_WRITE | APR_CREATE, APR_OS_DEFAULT, p);
184 CuAssertIntEquals(tc, APR_SUCCESS, rv);
185 rv = apr_file_close(thefile);
186 CuAssertIntEquals(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 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOTEMPTY(rv));
194 /* Read `.' and `..' out of dir2. */
195 rv = apr_dir_open(&this_dir, "dir2", p);
196 CuAssertIntEquals(tc, APR_SUCCESS, rv);
197 rv = apr_dir_read(&finfo, finfo_flags, this_dir);
198 CuAssertIntEquals(tc, APR_SUCCESS, rv);
199 rv = apr_dir_read(&finfo, finfo_flags, this_dir);
200 CuAssertIntEquals(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 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
209 rv = apr_dir_close(this_dir);
210 CuAssertIntEquals(tc, APR_SUCCESS, rv);
213 rv = apr_file_remove("dir1/file1", p);
214 CuAssertIntEquals(tc, APR_SUCCESS, rv);
215 rv = apr_dir_remove("dir1", p);
216 CuAssertIntEquals(tc, APR_SUCCESS, rv);
217 rv = apr_dir_remove("dir2", p);
218 CuAssertIntEquals(tc, APR_SUCCESS, rv);
222 static void test_rmkdir_nocwd(CuTest *tc)
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 CuAssert(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 CuNotImpl(tc, "cannot remove in-use directory");
253 CuSuite *testdir(void)
255 CuSuite *suite = CuSuiteNew("Directory");
257 SUITE_ADD_TEST(suite, test_mkdir);
258 SUITE_ADD_TEST(suite, test_mkdir_recurs);
259 SUITE_ADD_TEST(suite, test_remove);
260 SUITE_ADD_TEST(suite, test_removeall_fail);
261 SUITE_ADD_TEST(suite, test_removeall);
262 SUITE_ADD_TEST(suite, test_remove_notthere);
263 SUITE_ADD_TEST(suite, test_mkdir_twice);
264 SUITE_ADD_TEST(suite, test_rmkdir_nocwd);
266 SUITE_ADD_TEST(suite, test_rewind);
268 SUITE_ADD_TEST(suite, test_opendir);
269 SUITE_ADD_TEST(suite, test_opendir_notthere);
270 SUITE_ADD_TEST(suite, test_closedir);
271 SUITE_ADD_TEST(suite, test_uncleared_errno);