Mark many merge tests as skip-against-old-server.
[svn.git] / subversion / tests / libsvn_subr / path-test.c
blob44627752f56f52dd74645c51fad527326c6fda37
1 /*
2 * path-test.c -- test the path functions
4 * ====================================================================
5 * Copyright (c) 2000-2006 CollabNet. All rights reserved.
7 * This software is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at http://subversion.tigris.org/license-1.html.
10 * If newer versions of this license are posted there, you may use a
11 * newer version instead, at your option.
13 * This software consists of voluntary contributions made by many
14 * individuals. For exact contribution history, see the revision
15 * history and logs, available at http://subversion.tigris.org/.
16 * ====================================================================
19 #include <stdio.h>
20 #include <string.h>
21 #include "svn_pools.h"
22 #include "svn_path.h"
23 #include <apr_general.h>
25 #include "../svn_test.h"
27 /* Using a symbol, because I tried experimenting with different
28 representations */
29 #define SVN_EMPTY_PATH ""
31 static svn_error_t *
32 test_path_is_child(const char **msg,
33 svn_boolean_t msg_only,
34 svn_test_opts_t *opts,
35 apr_pool_t *pool)
37 int i, j;
39 /* The path checking code is platform specific, so we shouldn't run
40 the Windows path handling testcases on non-Windows platforms.
42 #define NUM_TEST_PATHS 11
44 static const char * const paths[NUM_TEST_PATHS] = {
45 "/foo/bar",
46 "/foo/bars",
47 "/foo/baz",
48 "/foo/bar/baz",
49 "/flu/blar/blaz",
50 "/foo/bar/baz/bing/boom",
51 SVN_EMPTY_PATH,
52 "foo",
53 ".foo",
54 "/",
55 "foo2",
58 static const char * const remainders[NUM_TEST_PATHS][NUM_TEST_PATHS] = {
59 { 0, 0, 0, "baz", 0, "baz/bing/boom", 0, 0, 0, 0, 0 },
60 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
61 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
62 { 0, 0, 0, 0, 0, "bing/boom", 0, 0, 0, 0, 0 },
63 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
64 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
65 { 0, 0, 0, 0, 0, 0, 0, "foo", ".foo", 0, "foo2" },
66 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
67 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
68 { "foo/bar", "foo/bars", "foo/baz", "foo/bar/baz", "flu/blar/blaz",
69 "foo/bar/baz/bing/boom", 0, 0, 0, 0, 0 },
70 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
73 *msg = "test svn_path_is_child";
75 if (msg_only)
76 return SVN_NO_ERROR;
78 for (i = 0; i < NUM_TEST_PATHS; i++)
80 for (j = 0; j < NUM_TEST_PATHS; j++)
82 const char *remainder;
84 remainder = svn_path_is_child(paths[i], paths[j], pool);
86 if (((remainder) && (! remainders[i][j]))
87 || ((! remainder) && (remainders[i][j]))
88 || (remainder && strcmp(remainder, remainders[i][j])))
89 return svn_error_createf
90 (SVN_ERR_TEST_FAILED, NULL,
91 "svn_path_is_child (%s, %s) returned '%s' instead of '%s'",
92 paths[i], paths[j],
93 remainder ? remainder : "(null)",
94 remainders[i][j] ? remainders[i][j] : "(null)" );
97 #undef NUM_TEST_PATHS
98 return SVN_NO_ERROR;
102 static svn_error_t *
103 test_path_split(const char **msg,
104 svn_boolean_t msg_only,
105 svn_test_opts_t *opts,
106 apr_pool_t *pool)
108 apr_size_t i;
110 static const char * const paths[][3] = {
111 { "/foo/bar", "/foo", "bar" },
112 { "/foo/bar/ ", "/foo/bar", " " },
113 { "/foo", "/", "foo" },
114 { "foo", SVN_EMPTY_PATH, "foo" },
115 { ".bar", SVN_EMPTY_PATH, ".bar" },
116 { "/.bar", "/", ".bar" },
117 { "foo/bar", "foo", "bar" },
118 { "/foo/bar", "/foo", "bar" },
119 { "foo/bar", "foo", "bar" },
120 { "foo./.bar", "foo.", ".bar" },
121 { "../foo", "..", "foo" },
122 { SVN_EMPTY_PATH, SVN_EMPTY_PATH, SVN_EMPTY_PATH },
123 { "/flu\\b/\\blarg", "/flu\\b", "\\blarg" },
124 { "/", "/", "/" },
127 *msg = "test svn_path_split";
129 if (msg_only)
130 return SVN_NO_ERROR;
132 for (i = 0; i < sizeof(paths) / sizeof(paths[0]); i++)
134 const char *dir, *base_name;
136 svn_path_split(paths[i][0], &dir, &base_name, pool);
137 if (strcmp(dir, paths[i][1]))
139 return svn_error_createf
140 (SVN_ERR_TEST_FAILED, NULL,
141 "svn_path_split (%s) returned dirname '%s' instead of '%s'",
142 paths[i][0], dir, paths[i][1]);
144 if (strcmp(base_name, paths[i][2]))
146 return svn_error_createf
147 (SVN_ERR_TEST_FAILED, NULL,
148 "svn_path_split (%s) returned basename '%s' instead of '%s'",
149 paths[i][0], base_name, paths[i][2]);
152 return SVN_NO_ERROR;
156 static svn_error_t *
157 test_is_url(const char **msg,
158 svn_boolean_t msg_only,
159 svn_test_opts_t *opts,
160 apr_pool_t *pool)
162 apr_size_t i;
164 /* Paths to test and their expected results. */
165 struct {
166 const char *path;
167 svn_boolean_t result;
168 } tests[] = {
169 { "", FALSE },
170 { "/blah/blah", FALSE },
171 { "//blah/blah", FALSE },
172 { "://blah/blah", FALSE },
173 { "a:abb://boo/", FALSE },
174 { "http://svn.collab.net/repos/svn", TRUE },
175 { "scheme/with", FALSE },
176 { "scheme/with:", FALSE },
177 { "scheme/with:/", FALSE },
178 { "scheme/with://", FALSE },
179 { "scheme/with://slash/", FALSE },
180 { "file:///path/to/repository", TRUE },
181 { "file://", TRUE },
182 { "file:/", FALSE },
183 { "file:", FALSE },
184 { "file", FALSE },
187 *msg = "test svn_path_is_url";
189 if (msg_only)
190 return SVN_NO_ERROR;
192 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
194 svn_boolean_t retval;
196 retval = svn_path_is_url(tests[i].path);
197 if (tests[i].result != retval)
198 return svn_error_createf
199 (SVN_ERR_TEST_FAILED, NULL,
200 "svn_path_is_url (%s) returned %s instead of %s",
201 tests[i].path, retval ? "TRUE" : "FALSE",
202 tests[i].result ? "TRUE" : "FALSE");
205 return SVN_NO_ERROR;
209 static svn_error_t *
210 test_is_uri_safe(const char **msg,
211 svn_boolean_t msg_only,
212 svn_test_opts_t *opts,
213 apr_pool_t *pool)
215 apr_size_t i;
217 /* Paths to test and their expected results. */
218 struct {
219 const char *path;
220 svn_boolean_t result;
221 } tests[] = {
222 { "http://svn.collab.net/repos", TRUE },
223 { "http://svn.collab.net/repos%", FALSE },
224 { "http://svn.collab.net/repos%/svn", FALSE },
225 { "http://svn.collab.net/repos%2g", FALSE },
226 { "http://svn.collab.net/repos%2g/svn", FALSE },
227 { "http://svn.collab.net/repos%%", FALSE },
228 { "http://svn.collab.net/repos%%/svn", FALSE },
229 { "http://svn.collab.net/repos%2a", TRUE },
230 { "http://svn.collab.net/repos%2a/svn", TRUE },
233 *msg = "test svn_path_is_uri_safe";
235 if (msg_only)
236 return SVN_NO_ERROR;
238 for (i = 0; i < (sizeof(tests) / sizeof(tests[0])); i++)
240 svn_boolean_t retval;
242 retval = svn_path_is_uri_safe(tests[i].path);
243 if (tests[i].result != retval)
244 return svn_error_createf
245 (SVN_ERR_TEST_FAILED, NULL,
246 "svn_path_is_uri_safe (%s) returned %s instead of %s",
247 tests[i].path, retval ? "TRUE" : "FALSE",
248 tests[i].result ? "TRUE" : "FALSE");
251 return SVN_NO_ERROR;
255 static svn_error_t *
256 test_uri_encode(const char **msg,
257 svn_boolean_t msg_only,
258 svn_test_opts_t *opts,
259 apr_pool_t *pool)
261 int i;
263 struct {
264 const char *path;
265 const char *result;
266 } tests[] = {
267 { "http://subversion.tigris.org",
268 "http://subversion.tigris.org"},
269 { " special_at_beginning",
270 "%20special_at_beginning" },
271 { "special_at_end ",
272 "special_at_end%20" },
273 { "special in middle",
274 "special%20in%20middle" },
275 { "\"Ouch!\" \"Did that hurt?\"",
276 "%22Ouch!%22%20%20%22Did%20that%20hurt%3F%22" }
279 *msg = "test svn_path_uri_[en/de]code";
281 if (msg_only)
282 return SVN_NO_ERROR;
284 for (i = 0; i < 5; i++)
286 const char *en_path, *de_path;
288 /* URI-encode the path, and verify the results. */
289 en_path = svn_path_uri_encode(tests[i].path, pool);
290 if (strcmp(en_path, tests[i].result))
292 return svn_error_createf
293 (SVN_ERR_TEST_FAILED, NULL,
294 "svn_path_uri_encode ('%s') returned '%s' instead of '%s'",
295 tests[i].path, en_path, tests[i].result);
298 /* URI-decode the path, and make sure we're back where we started. */
299 de_path = svn_path_uri_decode(en_path, pool);
300 if (strcmp(de_path, tests[i].path))
302 return svn_error_createf
303 (SVN_ERR_TEST_FAILED, NULL,
304 "svn_path_uri_decode ('%s') returned '%s' instead of '%s'",
305 tests[i].result, de_path, tests[i].path);
308 return SVN_NO_ERROR;
312 static svn_error_t *
313 test_uri_decode(const char **msg,
314 svn_boolean_t msg_only,
315 svn_test_opts_t *opts,
316 apr_pool_t *pool)
318 int i;
320 struct {
321 const char *path;
322 const char *result;
323 } tests[] = {
324 { "http://c.r.a/s%\0008me",
325 "http://c.r.a/s%"},
326 { "http://c.r.a/s%6\000me",
327 "http://c.r.a/s%6" },
328 { "http://c.r.a/s%68me",
329 "http://c.r.a/shme" },
332 *msg = "test svn_path_uri_decode with invalid escape";
334 if (msg_only)
335 return SVN_NO_ERROR;
337 for (i = 0; i < 3; i++)
339 const char *de_path;
341 /* URI-decode the path, and verify the results. */
342 de_path = svn_path_uri_decode(tests[i].path, pool);
343 if (strcmp(de_path, tests[i].result))
345 return svn_error_createf
346 (SVN_ERR_TEST_FAILED, NULL,
347 "svn_path_uri_decode ('%s') returned '%s' instead of '%s'",
348 tests[i].path, de_path, tests[i].result);
351 return SVN_NO_ERROR;
355 static svn_error_t *
356 test_uri_autoescape(const char **msg,
357 svn_boolean_t msg_only,
358 svn_test_opts_t *opts,
359 apr_pool_t *pool)
361 struct {
362 const char *path;
363 const char *result;
364 } tests[] = {
365 { "http://svn.collab.net/", "http://svn.collab.net/" },
366 { "file:///<>\" {}|\\^`", "file:///%3C%3E%22%20%7B%7D%7C%5C%5E%60" },
367 { "http://[::1]", "http://[::1]" }
369 int i;
371 *msg = "test svn_path_uri_autoescape";
373 if (msg_only)
374 return SVN_NO_ERROR;
376 for (i = 0; i < 3; ++i)
378 const char* uri = svn_path_uri_autoescape(tests[i].path, pool);
379 if (strcmp(uri, tests[i].result) != 0)
380 return svn_error_createf
381 (SVN_ERR_TEST_FAILED, NULL,
382 "svn_path_uri_autoescape on '%s' returned '%s' instead of '%s'",
383 tests[i].path, uri, tests[i].result);
384 if (strcmp(tests[i].path, tests[i].result) == 0
385 && tests[i].path != uri)
386 return svn_error_createf
387 (SVN_ERR_TEST_FAILED, NULL,
388 "svn_path_uri_autoescape on '%s' returned identical but not same"
389 " string", tests[i].path);
392 return SVN_NO_ERROR;
395 static svn_error_t *
396 test_uri_from_iri(const char **msg,
397 svn_boolean_t msg_only,
398 svn_test_opts_t *opts,
399 apr_pool_t *pool)
401 /* We have to code the IRIs like this because the compiler might translate
402 character and string literals outside of ASCII to some character set,
403 but here we are hard-coding UTF-8. But we all read UTF-8 codes like
404 poetry, don't we. */
405 static const char p1[] = {
406 '\x66', '\x69', '\x6C', '\x65', '\x3A', '\x2F', '\x2F', '\x2F',
407 '\x72', '\xC3', '\xA4', '\x6B', '\x73', '\x6D', '\xC3', '\xB6', '\x72',
408 '\x67', '\xC3', '\xA5', '\x73', '\0' };
409 static const char p2[] = {
410 '\x66', '\x69', '\x6C', '\x65', '\x3A', '\x2F', '\x2F', '\x2F',
411 '\x61', '\x62', '\x25', '\x32', '\x30', '\x63', '\x64', '\0' };
412 static const char *paths[2][2] = {
413 { p1,
414 "file:///r%C3%A4ksm%C3%B6rg%C3%A5s" },
415 { p2,
416 "file:///ab%20cd" }
418 int i;
420 *msg = "test svn_path_uri_from_iri";
422 if (msg_only)
423 return SVN_NO_ERROR;
425 for (i = 0; i < 2; ++i)
427 const char *uri = svn_path_uri_from_iri(paths[i][0], pool);
428 if (strcmp(paths[i][1], uri) != 0)
429 return svn_error_createf
430 (SVN_ERR_TEST_FAILED, NULL,
431 "svn_path_uri_from_iri on '%s' returned '%s' instead of '%s'",
432 paths[i][0], uri, paths[i][1]);
433 if (strcmp(paths[i][0], uri) == 0
434 && paths[i][0] != uri)
435 return svn_error_createf
436 (SVN_ERR_TEST_FAILED, NULL,
437 "svn_path_uri_from_iri on '%s' returned identical but not same"
438 " string", paths[i][0]);
441 return SVN_NO_ERROR;
444 static svn_error_t *
445 test_join(const char **msg,
446 svn_boolean_t msg_only,
447 svn_test_opts_t *opts,
448 apr_pool_t *pool)
450 int i;
451 char *result;
453 static const char * const joins[][3] = {
454 { "abc", "def", "abc/def" },
455 { "a", "def", "a/def" },
456 { "a", "d", "a/d" },
457 { "/", "d", "/d" },
458 { "/abc", "d", "/abc/d" },
459 { "/abc", "def", "/abc/def" },
460 { "/abc", "/def", "/def" },
461 { "/abc", "/d", "/d" },
462 { "/abc", "/", "/" },
463 { SVN_EMPTY_PATH, "/", "/" },
464 { "/", SVN_EMPTY_PATH, "/" },
465 { SVN_EMPTY_PATH, "abc", "abc" },
466 { "abc", SVN_EMPTY_PATH, "abc" },
467 { SVN_EMPTY_PATH, "/abc", "/abc" },
468 { SVN_EMPTY_PATH, SVN_EMPTY_PATH, SVN_EMPTY_PATH },
471 *msg = "test svn_path_join(_many)";
472 if (msg_only)
473 return SVN_NO_ERROR;
475 for (i = sizeof(joins) / sizeof(joins[0]); i--; )
477 const char *base = joins[i][0];
478 const char *comp = joins[i][1];
479 const char *expect = joins[i][2];
481 result = svn_path_join(base, comp, pool);
482 if (strcmp(result, expect))
483 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
484 "svn_path_join(\"%s\", \"%s\") returned "
485 "\"%s\". expected \"%s\"",
486 base, comp, result, expect);
488 result = svn_path_join_many(pool, base, comp, NULL);
489 if (strcmp(result, expect))
490 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
491 "svn_path_join_many(\"%s\", \"%s\") returned "
492 "\"%s\". expected \"%s\"",
493 base, comp, result, expect);
496 #define TEST_MANY(args, expect) \
497 result = svn_path_join_many args ; \
498 if (strcmp(result, expect) != 0) \
499 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL, \
500 "svn_path_join_many" #args " returns \"%s\". " \
501 "expected \"%s\"", \
502 result, expect); \
503 else
505 TEST_MANY((pool, "abc", NULL), "abc");
506 TEST_MANY((pool, "/abc", NULL), "/abc");
507 TEST_MANY((pool, "/", NULL), "/");
509 TEST_MANY((pool, "abc", "def", "ghi", NULL), "abc/def/ghi");
510 TEST_MANY((pool, "abc", "/def", "ghi", NULL), "/def/ghi");
511 TEST_MANY((pool, "/abc", "def", "ghi", NULL), "/abc/def/ghi");
512 TEST_MANY((pool, "abc", "def", "/ghi", NULL), "/ghi");
513 TEST_MANY((pool, "/", "def", "/ghi", NULL), "/ghi");
514 TEST_MANY((pool, "/", "/def", "/ghi", NULL), "/ghi");
516 TEST_MANY((pool, SVN_EMPTY_PATH, "def", "ghi", NULL), "def/ghi");
517 TEST_MANY((pool, "abc", SVN_EMPTY_PATH, "ghi", NULL), "abc/ghi");
518 TEST_MANY((pool, "abc", "def", SVN_EMPTY_PATH, NULL), "abc/def");
519 TEST_MANY((pool, SVN_EMPTY_PATH, "def", SVN_EMPTY_PATH, NULL), "def");
520 TEST_MANY((pool, SVN_EMPTY_PATH, SVN_EMPTY_PATH, "ghi", NULL), "ghi");
521 TEST_MANY((pool, "abc", SVN_EMPTY_PATH, SVN_EMPTY_PATH, NULL), "abc");
522 TEST_MANY((pool, SVN_EMPTY_PATH, "def", "/ghi", NULL), "/ghi");
523 TEST_MANY((pool, SVN_EMPTY_PATH, SVN_EMPTY_PATH, "/ghi", NULL), "/ghi");
525 TEST_MANY((pool, "/", "def", "ghi", NULL), "/def/ghi");
526 TEST_MANY((pool, "abc", "/", "ghi", NULL), "/ghi");
527 TEST_MANY((pool, "abc", "def", "/", NULL), "/");
528 TEST_MANY((pool, "/", "/", "ghi", NULL), "/ghi");
529 TEST_MANY((pool, "/", "/", "/", NULL), "/");
530 TEST_MANY((pool, "/", SVN_EMPTY_PATH, "ghi", NULL), "/ghi");
531 TEST_MANY((pool, "/", "def", SVN_EMPTY_PATH, NULL), "/def");
532 TEST_MANY((pool, SVN_EMPTY_PATH, "/", "ghi", NULL), "/ghi");
533 TEST_MANY((pool, "/", SVN_EMPTY_PATH, SVN_EMPTY_PATH, NULL), "/");
534 TEST_MANY((pool, SVN_EMPTY_PATH, "/", SVN_EMPTY_PATH, NULL), "/");
535 TEST_MANY((pool, SVN_EMPTY_PATH, SVN_EMPTY_PATH, "/", NULL), "/");
537 /* ### probably need quite a few more tests... */
539 return SVN_NO_ERROR;
543 static svn_error_t *
544 test_basename(const char **msg,
545 svn_boolean_t msg_only,
546 svn_test_opts_t *opts,
547 apr_pool_t *pool)
549 int i;
550 char *result;
552 struct {
553 const char *path;
554 const char *result;
555 } tests[] = {
556 { "abc", "abc" },
557 { "/abc", "abc" },
558 { "/abc", "abc" },
559 { "/x/abc", "abc" },
560 { "/xx/abc", "abc" },
561 { "/xx/abc", "abc" },
562 { "/xx/abc", "abc" },
563 { "a", "a" },
564 { "/a", "a" },
565 { "/b/a", "a" },
566 { "/b/a", "a" },
567 { "/", "/" },
568 { SVN_EMPTY_PATH, SVN_EMPTY_PATH },
571 *msg = "test svn_path_basename";
572 if (msg_only)
573 return SVN_NO_ERROR;
575 for (i = sizeof(tests) / sizeof(tests[0]); i--; )
577 const char *path = tests[i].path;
578 const char *expect = tests[i].result;
580 result = svn_path_basename(path, pool);
581 if (strcmp(result, expect))
582 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
583 "svn_path_basename(\"%s\") returned "
584 "\"%s\". expected \"%s\"",
585 path, result, expect);
588 return SVN_NO_ERROR;
592 static svn_error_t *
593 test_dirname(const char **msg,
594 svn_boolean_t msg_only,
595 svn_test_opts_t *opts,
596 apr_pool_t *pool)
598 int i;
599 char *result;
601 struct {
602 const char *path;
603 const char *result;
604 } tests[] = {
605 { "abc", "" },
606 { "/abc", "/" },
607 { "/x/abc", "/x" },
608 { "/xx/abc", "/xx" },
609 { "a", "" },
610 { "/a", "/" },
611 { "/b/a", "/b" },
612 { "/", "/" },
613 { SVN_EMPTY_PATH, SVN_EMPTY_PATH },
616 *msg = "test svn_path_dirname";
617 if (msg_only)
618 return SVN_NO_ERROR;
620 for (i = sizeof(tests) / sizeof(tests[0]); i--; )
622 const char *path = tests[i].path;
623 const char *expect = tests[i].result;
625 result = svn_path_dirname(path, pool);
626 if (strcmp(result, expect))
627 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
628 "svn_path_dirname(\"%s\") returned "
629 "\"%s\". expected \"%s\"",
630 path, result, expect);
633 return SVN_NO_ERROR;
637 static svn_error_t *
638 test_decompose(const char **msg,
639 svn_boolean_t msg_only,
640 svn_test_opts_t *opts,
641 apr_pool_t *pool)
643 static const char * const paths[] = {
644 "/", "/", NULL,
645 "foo", "foo", NULL,
646 "/foo", "/", "foo", NULL,
647 "/foo/bar", "/", "foo", "bar", NULL,
648 "foo/bar", "foo", "bar", NULL,
650 /* Are these canonical? Should the middle bits produce SVN_EMPTY_PATH? */
651 "foo/bar", "foo", "bar", NULL,
652 NULL,
654 int i = 0;
656 *msg = "test svn_path_decompose";
657 if (msg_only)
658 return SVN_NO_ERROR;
660 for (;;)
662 if (! paths[i])
663 break;
664 else
666 apr_array_header_t *components = svn_path_decompose(paths[i], pool);
667 int j;
668 for (j = 0; j < components->nelts; ++j)
670 const char *component = APR_ARRAY_IDX(components,
672 const char*);
673 if (! paths[i+j+1])
674 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
675 "svn_path_decompose(\"%s\") returned "
676 "unexpected component \"%s\"",
677 paths[i], component);
678 if (strcmp(component, paths[i+j+1]))
679 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
680 "svn_path_decompose(\"%s\") returned "
681 "\"%s\" expected \"%s\"",
682 paths[i], component, paths[i+j+1]);
684 if (paths[i+j+1])
685 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
686 "svn_path_decompose(\"%s\") failed "
687 "to return \"%s\"",
688 paths[i], paths[i+j+1]);
689 i += components->nelts + 2;
693 return SVN_NO_ERROR;
696 static svn_error_t *
697 test_canonicalize(const char **msg,
698 svn_boolean_t msg_only,
699 svn_test_opts_t *opts,
700 apr_pool_t *pool)
702 struct {
703 const char *path;
704 const char *result;
705 } tests[] = {
706 { "", "" },
707 { ".", "" },
708 { "/", "/" },
709 { "/.", "/" },
710 { "./", "" },
711 { "./.", "" },
712 { "//", "/" },
713 { "/////", "/" },
714 { "./././.", "" },
715 { "////././.", "/" },
716 { "foo", "foo" },
717 { ".foo", ".foo" },
718 { "foo.", "foo." },
719 { "/foo", "/foo" },
720 { "foo/", "foo" },
721 { "foo./", "foo." },
722 { "foo./.", "foo." },
723 { "foo././/.", "foo." },
724 { "/foo/bar", "/foo/bar" },
725 { "foo/..", "foo/.." },
726 { "foo/../", "foo/.." },
727 { "foo/../.", "foo/.." },
728 { "foo//.//bar", "foo/bar" },
729 { "///foo", "/foo" },
730 { "/.//./.foo", "/.foo" },
731 { ".///.foo", ".foo" },
732 { "../foo", "../foo" },
733 { "../../foo/", "../../foo" },
734 { "../../foo/..", "../../foo/.." },
735 { "/../../", "/../.." },
736 { "http://hst", "http://hst" },
737 { "http://hst/foo/../bar","http://hst/foo/../bar" },
738 { "http://hst/", "http://hst" },
739 { NULL, NULL }
741 int i;
743 *msg = "test svn_path_canonicalize";
744 if (msg_only)
745 return SVN_NO_ERROR;
747 i = 0;
748 while (tests[i].path)
750 const char *canonical = svn_path_canonicalize(tests[i].path, pool);
752 if (strcmp(canonical, tests[i].result))
753 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
754 "svn_path_canonicalize(\"%s\") returned "
755 "\"%s\" expected \"%s\"",
756 tests[i].path, canonical, tests[i].result);
757 ++i;
760 return SVN_NO_ERROR;
763 static svn_error_t *
764 test_remove_component(const char **msg,
765 svn_boolean_t msg_only,
766 svn_test_opts_t *opts,
767 apr_pool_t *pool)
769 struct {
770 const char *path;
771 const char *result;
772 } tests[] = {
773 { "", "" },
774 { "/", "/" },
775 { "foo", "" },
776 { "foo/bar", "foo" },
777 { "/foo/bar", "/foo" },
778 { "/foo", "/" },
779 { NULL, NULL }
781 int i;
782 svn_stringbuf_t *buf;
784 *msg = "test svn_path_remove_component";
785 if (msg_only)
786 return SVN_NO_ERROR;
788 buf = svn_stringbuf_create("", pool);
790 i = 0;
791 while (tests[i].path)
793 svn_stringbuf_set(buf, tests[i].path);
795 svn_path_remove_component(buf);
797 if (strcmp(buf->data, tests[i].result))
798 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
799 "svn_path_remove_component(\"%s\") returned "
800 "\"%s\" expected \"%s\"",
801 tests[i].path, buf->data, tests[i].result);
802 ++i;
805 return SVN_NO_ERROR;
808 static svn_error_t *
809 test_is_root(const char **msg,
810 svn_boolean_t msg_only,
811 svn_test_opts_t *opts,
812 apr_pool_t *pool)
814 apr_size_t i;
816 /* Paths to test and their expected results. */
817 struct {
818 const char *path;
819 svn_boolean_t result;
820 } tests[] = {
821 { "/foo/bar", FALSE },
822 { "/foo", FALSE },
823 { "/", TRUE },
824 { "", FALSE },
825 #if defined(WIN32) || defined(__CYGWIN__)
826 { "X:/foo", FALSE },
827 { "X:/", TRUE },
828 { "X:foo", FALSE },
829 { "X:", TRUE },
830 { "//srv/shr", TRUE },
831 { "//srv", TRUE },
832 { "//srv/shr/fld", FALSE },
833 #else /* WIN32 or Cygwin */
834 { "/X:foo", FALSE },
835 { "/X:", FALSE },
836 #endif /* non-WIN32 */
839 *msg = "test svn_dirent_is_root";
841 if (msg_only)
842 return SVN_NO_ERROR;
844 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
846 svn_boolean_t retval;
848 retval = svn_dirent_is_root(tests[i].path, strlen(tests[i].path));
849 if (tests[i].result != retval)
850 return svn_error_createf
851 (SVN_ERR_TEST_FAILED, NULL,
852 "svn_dirent_is_root (%s) returned %s instead of %s",
853 tests[i].path, retval ? "TRUE" : "FALSE",
854 tests[i].result ? "TRUE" : "FALSE");
857 return SVN_NO_ERROR;
860 static svn_error_t *
861 test_path_check_valid(const char **msg,
862 svn_boolean_t msg_only,
863 svn_test_opts_t *opts,
864 apr_pool_t *pool)
866 apr_size_t i;
868 /* Paths to test and their expected results. */
869 struct {
870 const char *path;
871 svn_boolean_t result;
872 } tests[] = {
873 { "/foo/bar", TRUE },
874 { "/foo", TRUE },
875 { "/", TRUE },
876 { "foo/bar", TRUE },
877 { "foo bar", TRUE },
878 { "foo\7bar", FALSE },
879 { "foo\31bar", FALSE },
880 { "\7foo\31bar", FALSE },
881 { "\7", FALSE },
882 { "", TRUE },
885 *msg = "test svn_path_check_valid";
887 if (msg_only)
888 return SVN_NO_ERROR;
890 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
892 svn_error_t *err = svn_path_check_valid(tests[i].path, pool);
893 svn_boolean_t retval = (err == SVN_NO_ERROR);
895 svn_error_clear(err);
896 if (tests[i].result != retval)
897 return svn_error_createf
898 (SVN_ERR_TEST_FAILED, NULL,
899 "svn_path_check_valid (%s) returned %s instead of %s",
900 tests[i].path, retval ? "TRUE" : "FALSE",
901 tests[i].result ? "TRUE" : "FALSE");
904 return SVN_NO_ERROR;
907 static svn_error_t *
908 test_path_is_ancestor(const char **msg,
909 svn_boolean_t msg_only,
910 svn_test_opts_t *opts,
911 apr_pool_t *pool)
913 apr_size_t i;
915 /* Paths to test and their expected results. */
916 struct {
917 const char *path1;
918 const char *path2;
919 svn_boolean_t result;
920 } tests[] = {
921 { "/foo", "/foo/bar", TRUE},
922 { "/foo/bar", "/foo/bar/", TRUE},
923 { "/", "/foo", TRUE},
924 { SVN_EMPTY_PATH, "foo", TRUE},
925 { SVN_EMPTY_PATH, ".bar", TRUE},
927 { "/.bar", "/", FALSE},
928 { "foo/bar", "foo", FALSE},
929 { "/foo/bar", "/foo", FALSE},
930 { "foo", "foo/bar", TRUE},
931 { "foo.", "foo./.bar", TRUE},
933 { "../foo", "..", FALSE},
934 { SVN_EMPTY_PATH, SVN_EMPTY_PATH, TRUE},
935 { "/", "/", TRUE},
938 *msg = "test svn_path_is_ancestor";
940 if (msg_only)
941 return SVN_NO_ERROR;
943 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
945 svn_boolean_t retval;
947 retval = svn_path_is_ancestor(tests[i].path1, tests[i].path2);
948 if (tests[i].result != retval)
949 return svn_error_createf
950 (SVN_ERR_TEST_FAILED, NULL,
951 "svn_path_is_ancestor (%s, %s) returned %s instead of %s",
952 tests[i].path1, tests[i].path2, retval ? "TRUE" : "FALSE",
953 tests[i].result ? "TRUE" : "FALSE");
955 return SVN_NO_ERROR;
958 static svn_error_t *
959 test_is_single_path_component(const char **msg,
960 svn_boolean_t msg_only,
961 svn_test_opts_t *opts,
962 apr_pool_t *pool)
964 apr_size_t i;
966 /* Paths to test and their expected results. */
967 struct {
968 const char *path;
969 svn_boolean_t result;
970 } tests[] = {
971 { "/foo/bar", FALSE },
972 { "/foo", FALSE },
973 { "/", FALSE },
974 { "foo/bar", FALSE },
975 { "foo", TRUE },
976 { ".", TRUE },
977 { "..", FALSE },
978 { "", FALSE },
981 *msg = "test svn_path_is_single_path_component";
983 if (msg_only)
984 return SVN_NO_ERROR;
986 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
988 svn_boolean_t retval;
990 retval = svn_path_is_single_path_component(tests[i].path);
991 if (tests[i].result != retval)
992 return svn_error_createf
993 (SVN_ERR_TEST_FAILED, NULL,
994 "svn_path_is_single_path_component (%s) returned %s instead of %s",
995 tests[i].path, retval ? "TRUE" : "FALSE",
996 tests[i].result ? "TRUE" : "FALSE");
999 return SVN_NO_ERROR;
1002 static svn_error_t *
1003 test_compare_paths(const char **msg,
1004 svn_boolean_t msg_only,
1005 svn_test_opts_t *opts,
1006 apr_pool_t *pool)
1008 apr_size_t i;
1010 /* Paths to test and their expected results. */
1011 struct {
1012 const char *path1;
1013 const char *path2;
1014 int result;
1015 } tests[] = {
1016 { "/foo", "/foo", 0},
1017 { "/foo/bar", "/foo/bar", 0},
1018 { "/", "/", 0},
1019 { SVN_EMPTY_PATH, SVN_EMPTY_PATH, 0},
1020 { "foo", "foo", 0},
1021 { "foo", "foo/bar", -1},
1022 { "foo/bar", "foo/boo", -1},
1023 { "boo", "foo", -1},
1024 { "foo", "boo", 1},
1025 { "foo/bar", "foo", 1},
1026 { "/", "/foo", -1},
1027 { "/foo", "/foo/bar", -1},
1028 { "/foo", "/foo/bar/boo", -1},
1029 { "foo", "/foo", 1},
1030 { "foo\xe0""bar", "foo", 1},
1033 *msg = "test svn_path_compare_paths";
1035 if (msg_only)
1036 return SVN_NO_ERROR;
1038 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
1040 int retval;
1042 retval = svn_path_compare_paths(tests[i].path1, tests[i].path2);
1043 /* tests if expected and actual result are both < 0,
1044 equal to 0 or greater than 0. */
1045 if (! (tests[i].result * retval > 0 ||
1046 (tests[i].result == 0 && retval == 0)) )
1047 return svn_error_createf
1048 (SVN_ERR_TEST_FAILED, NULL,
1049 "svn_path_compare_paths (%s, %s) returned %d instead of %d",
1050 tests[i].path1, tests[i].path2, retval, tests[i].result);
1052 return SVN_NO_ERROR;
1055 static svn_error_t *
1056 test_get_longest_ancestor(const char **msg,
1057 svn_boolean_t msg_only,
1058 svn_test_opts_t *opts,
1059 apr_pool_t *pool)
1061 apr_size_t i;
1063 /* Paths to test and their expected results. */
1064 struct {
1065 const char *path1;
1066 const char *path2;
1067 const char *result;
1068 } tests[] = {
1069 { "/foo", "/foo/bar", "/foo"},
1070 { "/foo/bar", "foo/bar", ""},
1071 { "/", "/foo", "/"},
1072 { SVN_EMPTY_PATH, "foo", SVN_EMPTY_PATH},
1073 { SVN_EMPTY_PATH, ".bar", SVN_EMPTY_PATH},
1074 { "/.bar", "/", "/"},
1075 { "foo/bar", "foo", "foo"},
1076 { "/foo/bar", "/foo", "/foo"},
1077 { "/rif", "/raf", "/"},
1078 { "foo", "foo/bar", "foo"},
1079 { "foo.", "foo./.bar", "foo."},
1080 { SVN_EMPTY_PATH, SVN_EMPTY_PATH, SVN_EMPTY_PATH},
1081 { "/", "/", "/"},
1082 { "http://test", "http://test", "http://test"},
1083 { "http://test", "http://taste", ""},
1084 { "http://test", "http://test/foo", "http://test"},
1085 { "http://test", "file://test/foo", ""},
1088 *msg = "test svn_path_get_longest_ancestor";
1090 if (msg_only)
1091 return SVN_NO_ERROR;
1093 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
1095 const char *retval;
1097 retval = svn_path_get_longest_ancestor(tests[i].path1, tests[i].path2,
1098 pool);
1100 if (strcmp(tests[i].result, retval))
1101 return svn_error_createf
1102 (SVN_ERR_TEST_FAILED, NULL,
1103 "svn_path_get_longest_ancestor (%s, %s) returned %s instead of %s",
1104 tests[i].path1, tests[i].path2, retval, tests[i].result);
1106 /* changing the order of the paths should return the same results */
1107 retval = svn_path_get_longest_ancestor(tests[i].path2, tests[i].path1,
1108 pool);
1110 if (strcmp(tests[i].result, retval))
1111 return svn_error_createf
1112 (SVN_ERR_TEST_FAILED, NULL,
1113 "svn_path_get_longest_ancestor (%s, %s) returned %s instead of %s",
1114 tests[i].path2, tests[i].path1, retval, tests[i].result);
1116 return SVN_NO_ERROR;
1120 static svn_error_t *
1121 test_splitext(const char **msg,
1122 svn_boolean_t msg_only,
1123 svn_test_opts_t *opts,
1124 apr_pool_t *pool)
1126 apr_size_t i;
1127 apr_pool_t *subpool = svn_pool_create(pool);
1129 /* Paths to test and their expected results. */
1130 struct {
1131 const char *path;
1132 const char *path_root;
1133 const char *path_ext;
1134 svn_boolean_t result;
1135 } tests[] = {
1136 { "no-ext", "no-ext", "" },
1137 { "test-file.py", "test-file.", "py" },
1138 { "period.file.ext", "period.file.", "ext" },
1139 { "multi-component/file.txt", "multi-component/file.", "txt" },
1140 { "yep.still/no-ext", "yep.still/no-ext", "" },
1141 { "folder.with/period.log", "folder.with/period.", "log" },
1142 { "period.", "period.", "" },
1143 { "file.ends-with/period.", "file.ends-with/period.", "" },
1144 { "two-periods..txt", "two-periods..", "txt" },
1145 { ".dot-file", ".dot-file", "" },
1146 { "sub/.dot-file", "sub/.dot-file", "" },
1147 { ".dot-file.withext", ".dot-file.", "withext" },
1148 { "sub/.dot-file.withext", "sub/.dot-file.", "withext" },
1149 { "", "", "" },
1152 *msg = "test svn_path_splitext";
1154 if (msg_only)
1155 return SVN_NO_ERROR;
1157 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
1159 const char *path = tests[i].path;
1160 const char *path_root;
1161 const char *path_ext;
1163 svn_pool_clear(subpool);
1165 /* First, we'll try splitting and fetching both root and
1166 extension to see if they match our expected results. */
1167 svn_path_splitext(&path_root, &path_ext, path, subpool);
1168 if ((strcmp(tests[i].path_root, path_root))
1169 || (strcmp(tests[i].path_ext, path_ext)))
1170 return svn_error_createf
1171 (SVN_ERR_TEST_FAILED, NULL,
1172 "svn_path_splitext (%s) returned ('%s', '%s') "
1173 "instead of ('%s', '%s')",
1174 tests[i].path, path_root, path_ext,
1175 tests[i].path_root, tests[i].path_ext);
1177 /* Now, let's only fetch the root. */
1178 svn_path_splitext(&path_root, NULL, path, subpool);
1179 if (strcmp(tests[i].path_root, path_root))
1180 return svn_error_createf
1181 (SVN_ERR_TEST_FAILED, NULL,
1182 "svn_path_splitext (%s) with a NULL path_ext returned '%s' "
1183 "for the path_root instead of '%s'",
1184 tests[i].path, path_root, tests[i].path_root);
1186 /* Next, let's only fetch the extension. */
1187 svn_path_splitext(NULL, &path_ext, path, subpool);
1188 if ((strcmp(tests[i].path_root, path_root))
1189 || (strcmp(tests[i].path_ext, path_ext)))
1190 return svn_error_createf
1191 (SVN_ERR_TEST_FAILED, NULL,
1192 "svn_path_splitext (%s) with a NULL path_root returned '%s' "
1193 "for the path_ext instead of '%s'",
1194 tests[i].path, path_ext, tests[i].path_ext);
1196 svn_pool_destroy(subpool);
1197 return SVN_NO_ERROR;
1201 static svn_error_t *
1202 test_compose(const char **msg,
1203 svn_boolean_t msg_only,
1204 svn_test_opts_t *opts,
1205 apr_pool_t *pool)
1207 static const char * const paths[] = {
1209 "/",
1210 "/foo",
1211 "/foo/bar",
1212 "/foo/bar/baz",
1213 "foo",
1214 "foo/bar",
1215 "foo/bar/baz",
1216 NULL,
1218 const char * const *path_ptr = paths;
1219 const char *input_path;
1221 *msg = "test svn_path_decompose";
1222 if (msg_only)
1223 return SVN_NO_ERROR;
1225 for (input_path = *path_ptr; *path_ptr; input_path = *++path_ptr)
1227 apr_array_header_t *components = svn_path_decompose(input_path, pool);
1228 const char *output_path = svn_path_compose(components, pool);
1230 if (strcmp(input_path, output_path))
1231 return svn_error_createf(SVN_ERR_TEST_FAILED, NULL,
1232 "svn_path_compose("
1233 "svn_path_decompose(\"%s\")) "
1234 "returned \"%s\" expected \"%s\"",
1235 input_path, output_path, input_path);
1238 return SVN_NO_ERROR;
1241 /* local define to support XFail-ing tests on Windows/Cygwin only */
1242 #if defined(WIN32) || defined(__CYGWIN__)
1243 #define WINDOWS_OR_CYGWIN TRUE
1244 #else
1245 #define WINDOWS_OR_CYGWIN FALSE
1246 #endif /* WIN32 or Cygwin */
1249 /* The test table. */
1251 struct svn_test_descriptor_t test_funcs[] =
1253 SVN_TEST_NULL,
1254 SVN_TEST_PASS(test_path_is_child),
1255 SVN_TEST_PASS(test_path_split),
1256 SVN_TEST_PASS(test_is_url),
1257 SVN_TEST_PASS(test_is_uri_safe),
1258 SVN_TEST_PASS(test_uri_encode),
1259 SVN_TEST_PASS(test_uri_decode),
1260 SVN_TEST_PASS(test_uri_autoescape),
1261 SVN_TEST_PASS(test_uri_from_iri),
1262 SVN_TEST_PASS(test_join),
1263 SVN_TEST_PASS(test_basename),
1264 SVN_TEST_PASS(test_dirname),
1265 SVN_TEST_PASS(test_decompose),
1266 SVN_TEST_PASS(test_canonicalize),
1267 SVN_TEST_PASS(test_remove_component),
1268 SVN_TEST_PASS(test_is_root),
1269 SVN_TEST_PASS(test_path_is_ancestor),
1270 SVN_TEST_PASS(test_path_check_valid),
1271 SVN_TEST_PASS(test_is_single_path_component),
1272 SVN_TEST_PASS(test_compare_paths),
1273 SVN_TEST_PASS(test_get_longest_ancestor),
1274 SVN_TEST_PASS(test_splitext),
1275 SVN_TEST_PASS(test_compose),
1276 SVN_TEST_NULL