upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testnames.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 "test_apr.h"
18 #include "apr_file_io.h"
19 #include "apr_file_info.h"
20 #include "apr_errno.h"
21 #include "apr_general.h"
22 #include "apr_pools.h"
23 #include "apr_lib.h"
24
25 #if WIN32
26 #define ABS_ROOT "C:/"
27 #elif defined(NETWARE)
28 #define ABS_ROOT "SYS:/"
29 #else
30 #define ABS_ROOT "/"
31 #endif
32
33 static void merge_aboveroot(CuTest *tc)
34 {
35     apr_status_t rv;
36     char *dstpath = NULL;
37     char errmsg[256];
38
39     rv = apr_filepath_merge(&dstpath, ABS_ROOT"foo", ABS_ROOT"bar", APR_FILEPATH_NOTABOVEROOT,
40                             p);
41     apr_strerror(rv, errmsg, sizeof(errmsg));
42     CuAssertIntEquals(tc, 1, APR_STATUS_IS_EABOVEROOT(rv));
43     CuAssertPtrEquals(tc, NULL, dstpath);
44     CuAssertStrEquals(tc, "The given path was above the root path", errmsg);
45 }
46
47 static void merge_belowroot(CuTest *tc)
48 {
49     apr_status_t rv;
50     char *dstpath = NULL;
51
52     rv = apr_filepath_merge(&dstpath, ABS_ROOT"foo", ABS_ROOT"foo/bar", 
53                             APR_FILEPATH_NOTABOVEROOT, p);
54     CuAssertPtrNotNull(tc, dstpath);
55     CuAssertIntEquals(tc, APR_SUCCESS, rv);
56     CuAssertStrEquals(tc, ABS_ROOT"foo/bar", dstpath);
57 }
58
59 static void merge_noflag(CuTest *tc)
60 {
61     apr_status_t rv;
62     char *dstpath = NULL;
63
64     rv = apr_filepath_merge(&dstpath, ABS_ROOT"foo", ABS_ROOT"foo/bar", 0, p);
65     CuAssertPtrNotNull(tc, dstpath);
66     CuAssertIntEquals(tc, APR_SUCCESS, rv);
67     CuAssertStrEquals(tc, ABS_ROOT"foo/bar", dstpath);
68 }
69
70 static void merge_dotdot(CuTest *tc)
71 {
72     apr_status_t rv;
73     char *dstpath = NULL;
74
75     rv = apr_filepath_merge(&dstpath, ABS_ROOT"foo/bar", "../baz", 0, p);
76     CuAssertPtrNotNull(tc, dstpath);
77     CuAssertIntEquals(tc, APR_SUCCESS, rv);
78     CuAssertStrEquals(tc, ABS_ROOT"foo/baz", dstpath);
79
80     rv = apr_filepath_merge(&dstpath, "", "../test", 0, p);
81     CuAssertIntEquals(tc, 0, APR_SUCCESS);
82     CuAssertStrEquals(tc, "../test", dstpath);
83
84     /* Very dangerous assumptions here about what the cwd is.  However, let's assume
85      * that the testall is invoked from within apr/test/ so the following test should
86      * return ../test unless a previously fixed bug remains or the developer changes
87      * the case of the test directory:
88      */
89     rv = apr_filepath_merge(&dstpath, "", "../test", APR_FILEPATH_TRUENAME, p);
90     CuAssertIntEquals(tc, 0, APR_SUCCESS);
91     CuAssertStrEquals(tc, "../test", dstpath);
92 }
93
94 static void merge_dotdot_dotdot_dotdot(CuTest *tc)
95 {
96     apr_status_t rv;
97     char *dstpath = NULL;
98
99     rv = apr_filepath_merge(&dstpath, "", 
100                             "../../..", APR_FILEPATH_TRUENAME, p);
101     CuAssertPtrNotNull(tc, dstpath);
102     CuAssertIntEquals(tc, APR_SUCCESS, rv);
103     CuAssertStrEquals(tc, "../../..", dstpath);
104
105     rv = apr_filepath_merge(&dstpath, "", 
106                             "../../../", APR_FILEPATH_TRUENAME, p);
107     CuAssertPtrNotNull(tc, dstpath);
108     CuAssertIntEquals(tc, APR_SUCCESS, rv);
109     CuAssertStrEquals(tc, "../../../", dstpath);
110 }
111
112 static void merge_secure(CuTest *tc)
113 {
114     apr_status_t rv;
115     char *dstpath = NULL;
116
117     rv = apr_filepath_merge(&dstpath, ABS_ROOT"foo/bar", "../bar/baz", 0, p);
118     CuAssertPtrNotNull(tc, dstpath);
119     CuAssertIntEquals(tc, APR_SUCCESS, rv);
120     CuAssertStrEquals(tc, ABS_ROOT"foo/bar/baz", dstpath);
121 }
122
123 static void merge_notrel(CuTest *tc)
124 {
125     apr_status_t rv;
126     char *dstpath = NULL;
127
128     rv = apr_filepath_merge(&dstpath, ABS_ROOT"foo/bar", "../baz",
129                             APR_FILEPATH_NOTRELATIVE, p);
130     CuAssertPtrNotNull(tc, dstpath);
131     CuAssertIntEquals(tc, APR_SUCCESS, rv);
132     CuAssertStrEquals(tc, ABS_ROOT"foo/baz", dstpath);
133 }
134
135 static void merge_notrelfail(CuTest *tc)
136 {
137     apr_status_t rv;
138     char *dstpath = NULL;
139     char errmsg[256];
140
141     rv = apr_filepath_merge(&dstpath, "foo/bar", "../baz", 
142                             APR_FILEPATH_NOTRELATIVE, p);
143     apr_strerror(rv, errmsg, sizeof(errmsg));
144
145     CuAssertPtrEquals(tc, NULL, dstpath);
146     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ERELATIVE(rv));
147     CuAssertStrEquals(tc, "The given path is relative", errmsg);
148 }
149
150 static void merge_notabsfail(CuTest *tc)
151 {
152     apr_status_t rv;
153     char *dstpath = NULL;
154     char errmsg[256];
155
156     rv = apr_filepath_merge(&dstpath, ABS_ROOT"foo/bar", "../baz", 
157                             APR_FILEPATH_NOTABSOLUTE, p);
158     apr_strerror(rv, errmsg, sizeof(errmsg));
159
160     CuAssertPtrEquals(tc, NULL, dstpath);
161     CuAssertIntEquals(tc, 1, APR_STATUS_IS_EABSOLUTE(rv));
162     CuAssertStrEquals(tc, "The given path is absolute", errmsg);
163 }
164
165 static void merge_notabs(CuTest *tc)
166 {
167     apr_status_t rv;
168     char *dstpath = NULL;
169
170     rv = apr_filepath_merge(&dstpath, "foo/bar", "../baz", 
171                             APR_FILEPATH_NOTABSOLUTE, p);
172
173     CuAssertPtrNotNull(tc, dstpath);
174     CuAssertIntEquals(tc, APR_SUCCESS, rv);
175     CuAssertStrEquals(tc, "foo/baz", dstpath);
176 }
177
178 static void root_absolute(CuTest *tc)
179 {
180     apr_status_t rv;
181     const char *root = NULL;
182     const char *path = ABS_ROOT"foo/bar";
183
184     rv = apr_filepath_root(&root, &path, 0, p);
185
186     CuAssertPtrNotNull(tc, root);
187     CuAssertIntEquals(tc, APR_SUCCESS, rv);
188     CuAssertStrEquals(tc, ABS_ROOT, root);
189 }
190
191 static void root_relative(CuTest *tc)
192 {
193     apr_status_t rv;
194     const char *root = NULL;
195     const char *path = "foo/bar";
196     char errmsg[256];
197
198     rv = apr_filepath_root(&root, &path, 0, p);
199     apr_strerror(rv, errmsg, sizeof(errmsg));
200
201     CuAssertPtrEquals(tc, NULL, root);
202     CuAssertIntEquals(tc, 1, APR_STATUS_IS_ERELATIVE(rv));
203     CuAssertStrEquals(tc, "The given path is relative", errmsg);
204 }
205
206
207 static void root_from_cwd_and_back(CuTest *tc)
208 {
209     apr_status_t rv;
210     const char *root = NULL;
211     const char *path = "//";
212     char *origpath;
213     char *testpath;
214
215     CuAssertIntEquals(tc, APR_SUCCESS, apr_filepath_get(&origpath, 0, p));
216     path = origpath;
217     rv = apr_filepath_root(&root, &path, APR_FILEPATH_TRUENAME, p);
218
219 #if defined(WIN32) || defined(OS2)
220     CuAssertIntEquals(tc, origpath[0], root[0]);
221     CuAssertIntEquals(tc, ':', root[1]);
222     CuAssertIntEquals(tc, '/', root[2]);
223     CuAssertIntEquals(tc, 0, root[3]);
224     CuAssertStrEquals(tc, origpath + 3, path);
225 #elif defined(NETWARE)
226     CuAssertIntEquals(tc, origpath[0], root[0]);
227     {
228     char *pt = strchr(root, ':');
229     CuAssertPtrNotNull(tc, pt);
230     CuAssertIntEquals(tc, ':', pt[0]);
231     CuAssertIntEquals(tc, '/', pt[1]);
232     CuAssertIntEquals(tc, 0, pt[2]);
233     pt = strchr(origpath, ':');
234     CuAssertPtrNotNull(tc, pt);
235     CuAssertStrEquals(tc, (pt+2), path);
236     }
237 #else
238     CuAssertIntEquals(tc, APR_SUCCESS, rv);
239     CuAssertStrEquals(tc, "/", root);
240     CuAssertStrEquals(tc, origpath + 1, path);
241 #endif
242
243     rv = apr_filepath_merge(&testpath, root, path, 
244                             APR_FILEPATH_TRUENAME
245                           | APR_FILEPATH_NOTABOVEROOT
246                           | APR_FILEPATH_NOTRELATIVE, p);
247     CuAssertIntEquals(tc, APR_SUCCESS, rv);
248     CuAssertStrEquals(tc, origpath, testpath);
249 }
250
251
252 CuSuite *testnames(void)
253 {
254     CuSuite *suite = CuSuiteNew("Path names");
255
256     SUITE_ADD_TEST(suite, merge_aboveroot);
257     SUITE_ADD_TEST(suite, merge_belowroot);
258     SUITE_ADD_TEST(suite, merge_noflag);
259     SUITE_ADD_TEST(suite, merge_dotdot);
260     SUITE_ADD_TEST(suite, merge_secure);
261     SUITE_ADD_TEST(suite, merge_notrel);
262     SUITE_ADD_TEST(suite, merge_notrelfail);
263     SUITE_ADD_TEST(suite, merge_notabs);
264     SUITE_ADD_TEST(suite, merge_notabsfail);
265     SUITE_ADD_TEST(suite, merge_dotdot_dotdot_dotdot);
266
267     SUITE_ADD_TEST(suite, root_absolute);
268     SUITE_ADD_TEST(suite, root_relative);
269     SUITE_ADD_TEST(suite, root_from_cwd_and_back);
270
271     return suite;
272 }
273