bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testdir.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include "apr_file_io.h"
21 #include "apr_file_info.h"
22 #include "apr_errno.h"
23 #include "apr_general.h"
24 #include "apr_lib.h"
25 #include "test_apr.h"
26
27 static void test_mkdir(CuTest *tc)
28 {
29     apr_status_t rv;
30     apr_finfo_t finfo;
31
32     rv = apr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
33     CuAssertIntEquals(tc, APR_SUCCESS, rv);
34
35     rv = apr_stat(&finfo, "data/testdir", APR_FINFO_TYPE, p);
36     CuAssertIntEquals(tc, APR_SUCCESS, rv);
37     CuAssertIntEquals(tc, APR_DIR, finfo.filetype);
38 }
39
40 static void test_mkdir_recurs(CuTest *tc)
41 {
42     apr_status_t rv;
43     apr_finfo_t finfo;
44
45     rv = apr_dir_make_recursive("data/one/two/three", 
46                                 APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
47     CuAssertIntEquals(tc, APR_SUCCESS, rv);
48
49     rv = apr_stat(&finfo, "data/one", APR_FINFO_TYPE, p);
50     CuAssertIntEquals(tc, APR_SUCCESS, rv);
51     CuAssertIntEquals(tc, APR_DIR, finfo.filetype);
52
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);
56
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);
60 }
61
62 static void test_remove(CuTest *tc)
63 {
64     apr_status_t rv;
65     apr_finfo_t finfo;
66
67     rv = apr_dir_remove("data/testdir", p);
68     CuAssertIntEquals(tc, APR_SUCCESS, rv);
69
70     rv = apr_stat(&finfo, "data/testdir", APR_FINFO_TYPE, p);
71     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
72 }
73
74 static void test_removeall_fail(CuTest *tc)
75 {
76     apr_status_t rv;
77
78     rv = apr_dir_remove("data/one", p);
79     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOTEMPTY(rv));
80 }
81
82 static void test_removeall(CuTest *tc)
83 {
84     apr_status_t rv;
85
86     rv = apr_dir_remove("data/one/two/three", p);
87     CuAssertIntEquals(tc, APR_SUCCESS, rv);
88
89     rv = apr_dir_remove("data/one/two", p);
90     CuAssertIntEquals(tc, APR_SUCCESS, rv);
91
92     rv = apr_dir_remove("data/one", p);
93     CuAssertIntEquals(tc, APR_SUCCESS, rv);
94 }
95
96 static void test_remove_notthere(CuTest *tc)
97 {
98     apr_status_t rv;
99
100     rv = apr_dir_remove("data/notthere", p);
101     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
102 }
103
104 static void test_mkdir_twice(CuTest *tc)
105 {
106     apr_status_t rv;
107
108     rv = apr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
109     CuAssertIntEquals(tc, APR_SUCCESS, rv);
110
111     rv = apr_dir_make("data/testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE, p);
112     CuAssertIntEquals(tc, 1, APR_STATUS_IS_EEXIST(rv));
113
114     rv = apr_dir_remove("data/testdir", p);
115     CuAssertIntEquals(tc, APR_SUCCESS, rv);
116 }
117
118 static void test_opendir(CuTest *tc)
119 {
120     apr_status_t rv;
121     apr_dir_t *dir;
122
123     rv = apr_dir_open(&dir, "data", p);
124     CuAssertIntEquals(tc, APR_SUCCESS, rv);
125     apr_dir_close(dir);
126 }
127
128 static void test_opendir_notthere(CuTest *tc)
129 {
130     apr_status_t rv;
131     apr_dir_t *dir;
132
133     rv = apr_dir_open(&dir, "notthere", p);
134     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
135 }
136
137 static void test_closedir(CuTest *tc)
138 {
139     apr_status_t rv;
140     apr_dir_t *dir;
141
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);
146 }
147
148 static void test_rewind(CuTest *tc)
149 {
150     apr_dir_t *dir;
151     apr_finfo_t first, second;
152
153     apr_assert_success(tc, "apr_dir_open failed", apr_dir_open(&dir, "data", p));
154
155     apr_assert_success(tc, "apr_dir_read failed",
156                        apr_dir_read(&first, APR_FINFO_DIRENT, dir));
157
158     apr_assert_success(tc, "apr_dir_rewind failed", apr_dir_rewind(dir));
159
160     apr_assert_success(tc, "second apr_dir_read failed",
161                        apr_dir_read(&second, APR_FINFO_DIRENT, dir));
162
163     apr_assert_success(tc, "apr_dir_close failed", apr_dir_close(dir));
164
165     CuAssertStrEquals(tc, first.name, second.name);
166 }
167
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)
171 {
172     apr_file_t *thefile = NULL;
173     apr_finfo_t finfo;
174     apr_int32_t finfo_flags = APR_FINFO_TYPE | APR_FINFO_NAME;
175     apr_dir_t *this_dir;
176     apr_status_t rv; 
177
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);
187
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));
193     
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);
201
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));
208
209     rv = apr_dir_close(this_dir);
210     CuAssertIntEquals(tc, APR_SUCCESS, rv);
211                  
212     /* Cleanup */
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);
219
220 }
221
222 static void test_rmkdir_nocwd(CuTest *tc)
223 {
224     char *cwd, *path;
225     apr_status_t rv;
226
227     apr_assert_success(tc, "make temp dir",
228                        apr_dir_make("dir3", APR_OS_DEFAULT, p));
229
230     apr_assert_success(tc, "obtain cwd", apr_filepath_get(&cwd, 0, p));
231
232     apr_assert_success(tc, "determine path to temp dir",
233                        apr_filepath_merge(&path, cwd, "dir3", 0, p));
234
235     apr_assert_success(tc, "change to temp dir", apr_filepath_set(path, p));
236
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, 
242                                         p) != APR_SUCCESS);
243     }
244
245     apr_assert_success(tc, "restore cwd", apr_filepath_set(cwd, p));
246
247     if (rv) {
248         apr_dir_remove(path, p);
249         CuNotImpl(tc, "cannot remove in-use directory");
250     }
251 }
252
253 CuSuite *testdir(void)
254 {
255     CuSuite *suite = CuSuiteNew("Directory");
256
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);
265
266     SUITE_ADD_TEST(suite, test_rewind);
267
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);
272
273     return suite;
274 }
275