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.
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"
26 #define ABS_ROOT "C:/"
27 #elif defined(NETWARE)
28 #define ABS_ROOT "SYS:/"
33 static void merge_aboveroot(CuTest *tc)
39 rv = apr_filepath_merge(&dstpath, ABS_ROOT"foo", ABS_ROOT"bar", APR_FILEPATH_NOTABOVEROOT,
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);
47 static void merge_belowroot(CuTest *tc)
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);
59 static void merge_noflag(CuTest *tc)
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);
70 static void merge_dotdot(CuTest *tc)
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);
80 rv = apr_filepath_merge(&dstpath, "", "../test", 0, p);
81 CuAssertIntEquals(tc, 0, APR_SUCCESS);
82 CuAssertStrEquals(tc, "../test", dstpath);
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:
89 rv = apr_filepath_merge(&dstpath, "", "../test", APR_FILEPATH_TRUENAME, p);
90 CuAssertIntEquals(tc, 0, APR_SUCCESS);
91 CuAssertStrEquals(tc, "../test", dstpath);
94 static void merge_dotdot_dotdot_dotdot(CuTest *tc)
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);
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);
112 static void merge_secure(CuTest *tc)
115 char *dstpath = NULL;
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);
123 static void merge_notrel(CuTest *tc)
126 char *dstpath = NULL;
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);
135 static void merge_notrelfail(CuTest *tc)
138 char *dstpath = NULL;
141 rv = apr_filepath_merge(&dstpath, "foo/bar", "../baz",
142 APR_FILEPATH_NOTRELATIVE, p);
143 apr_strerror(rv, errmsg, sizeof(errmsg));
145 CuAssertPtrEquals(tc, NULL, dstpath);
146 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ERELATIVE(rv));
147 CuAssertStrEquals(tc, "The given path is relative", errmsg);
150 static void merge_notabsfail(CuTest *tc)
153 char *dstpath = NULL;
156 rv = apr_filepath_merge(&dstpath, ABS_ROOT"foo/bar", "../baz",
157 APR_FILEPATH_NOTABSOLUTE, p);
158 apr_strerror(rv, errmsg, sizeof(errmsg));
160 CuAssertPtrEquals(tc, NULL, dstpath);
161 CuAssertIntEquals(tc, 1, APR_STATUS_IS_EABSOLUTE(rv));
162 CuAssertStrEquals(tc, "The given path is absolute", errmsg);
165 static void merge_notabs(CuTest *tc)
168 char *dstpath = NULL;
170 rv = apr_filepath_merge(&dstpath, "foo/bar", "../baz",
171 APR_FILEPATH_NOTABSOLUTE, p);
173 CuAssertPtrNotNull(tc, dstpath);
174 CuAssertIntEquals(tc, APR_SUCCESS, rv);
175 CuAssertStrEquals(tc, "foo/baz", dstpath);
178 static void root_absolute(CuTest *tc)
181 const char *root = NULL;
182 const char *path = ABS_ROOT"foo/bar";
184 rv = apr_filepath_root(&root, &path, 0, p);
186 CuAssertPtrNotNull(tc, root);
187 CuAssertIntEquals(tc, APR_SUCCESS, rv);
188 CuAssertStrEquals(tc, ABS_ROOT, root);
191 static void root_relative(CuTest *tc)
194 const char *root = NULL;
195 const char *path = "foo/bar";
198 rv = apr_filepath_root(&root, &path, 0, p);
199 apr_strerror(rv, errmsg, sizeof(errmsg));
201 CuAssertPtrEquals(tc, NULL, root);
202 CuAssertIntEquals(tc, 1, APR_STATUS_IS_ERELATIVE(rv));
203 CuAssertStrEquals(tc, "The given path is relative", errmsg);
207 static void root_from_cwd_and_back(CuTest *tc)
210 const char *root = NULL;
211 const char *path = "//";
215 CuAssertIntEquals(tc, APR_SUCCESS, apr_filepath_get(&origpath, 0, p));
217 rv = apr_filepath_root(&root, &path, APR_FILEPATH_TRUENAME, p);
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]);
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);
238 CuAssertIntEquals(tc, APR_SUCCESS, rv);
239 CuAssertStrEquals(tc, "/", root);
240 CuAssertStrEquals(tc, origpath + 1, path);
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);
252 CuSuite *testnames(void)
254 CuSuite *suite = CuSuiteNew("Path names");
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);
267 SUITE_ADD_TEST(suite, root_absolute);
268 SUITE_ADD_TEST(suite, root_relative);
269 SUITE_ADD_TEST(suite, root_from_cwd_and_back);