📄 path-test.c
字号:
/* * path-test.c -- test the path functions * * ==================================================================== * Copyright (c) 2000-2004 CollabNet. All rights reserved. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at http://subversion.tigris.org/license-1.html. * If newer versions of this license are posted there, you may use a * newer version instead, at your option. * * This software consists of voluntary contributions made by many * individuals. For exact contribution history, see the revision * history and logs, available at http://subversion.tigris.org/. * ==================================================================== */#include <stdio.h>#include <string.h>#include "svn_path.h"#include <apr_general.h>#include "../svn_test.h"/* Using a symbol, because I tried experimenting with different representations */#define SVN_EMPTY_PATH ""static svn_error_t *test_path_is_child(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ int i, j;#define NUM_TEST_PATHS 9 static const char * const paths[NUM_TEST_PATHS] = { "/foo/bar", "/foo/baz", "/foo/bar/baz", "/flu/blar/blaz", "/foo/bar/baz/bing/boom", SVN_EMPTY_PATH, "foo", ".foo", "/" }; static const char * const remainders[NUM_TEST_PATHS][NUM_TEST_PATHS] = { { 0, 0, "baz", 0, "baz/bing/boom", 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, "bing/boom", 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, "foo", ".foo", 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { "foo/bar", "foo/baz", "foo/bar/baz", "flu/blar/blaz", "foo/bar/baz/bing/boom", 0, 0, 0, 0 } }; *msg = "test svn_path_is_child"; if (msg_only) return SVN_NO_ERROR; for (i = 0; i < NUM_TEST_PATHS; i++) { for (j = 0; j < NUM_TEST_PATHS; j++) { const char *remainder; remainder = svn_path_is_child(paths[i], paths[j], pool); if (((remainder) && (! remainders[i][j])) || ((! remainder) && (remainders[i][j])) || (remainder && strcmp(remainder, remainders[i][j]))) return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_is_child (%s, %s) returned '%s' instead of '%s'", paths[i], paths[j], remainder ? remainder : "(null)", remainders[i][j] ? remainders[i][j] : "(null)" ); } }#undef NUM_TEST_PATHS return SVN_NO_ERROR;}static svn_error_t *test_path_split(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ apr_size_t i; static const char * const paths[][3] = { { "/foo/bar", "/foo", "bar" }, { "/foo/bar/ ", "/foo/bar", " " }, { "/foo", "/", "foo" }, { "foo", SVN_EMPTY_PATH, "foo" }, { ".bar", SVN_EMPTY_PATH, ".bar" }, { "/.bar", "/", ".bar" }, { "foo/bar", "foo", "bar" }, { "/foo/bar", "/foo", "bar" }, { "foo/bar", "foo", "bar" }, { "foo./.bar", "foo.", ".bar" }, { "../foo", "..", "foo" }, { SVN_EMPTY_PATH, SVN_EMPTY_PATH, SVN_EMPTY_PATH }, { "/flu\\b/\\blarg", "/flu\\b", "\\blarg" }, }; *msg = "test svn_path_split"; if (msg_only) return SVN_NO_ERROR; for (i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { const char *dir, *base_name; svn_path_split(paths[i][0], &dir, &base_name, pool); if (strcmp(dir, paths[i][1])) { return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_split (%s) returned dirname '%s' instead of '%s'", paths[i][0], dir, paths[i][1]); } if (strcmp(base_name, paths[i][2])) { return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_split (%s) returned basename '%s' instead of '%s'", paths[i][0], base_name, paths[i][2]); } } return SVN_NO_ERROR;}static svn_error_t *test_is_url(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ apr_size_t i; /* Paths to test. */ static const char * const paths[] = { "://blah/blah", "a:abb://boo/", "http://svn.collab.net/repos/svn", "scheme/with://slash/", "file:///path/to/repository", "file://", "file:/", }; /* Expected results of the tests. */ static const svn_boolean_t retvals[] = { FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, }; *msg = "test svn_path_is_url"; if (msg_only) return SVN_NO_ERROR; for (i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) { svn_boolean_t retval; retval = svn_path_is_url(paths[i]); if (retvals[i] != retval) return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_is_url (%s) returned %s instead of %s", paths[i], retval ? "TRUE" : "FALSE", retvals[i] ? "TRUE" : "FALSE"); } return SVN_NO_ERROR;}static svn_error_t *test_is_uri_safe(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ apr_size_t i; /* Paths to test. */ static const char * const paths[] = { "http://svn.collab.net/repos", "http://svn.collab.net/repos%", "http://svn.collab.net/repos%/svn", "http://svn.collab.net/repos%2g", "http://svn.collab.net/repos%2g/svn", "http://svn.collab.net/repos%%", "http://svn.collab.net/repos%%/svn", "http://svn.collab.net/repos%2a", "http://svn.collab.net/repos%2a/svn", }; /* Expected results of the tests. */ static const svn_boolean_t retvals[] = { TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE }; *msg = "test svn_path_is_uri_safe"; if (msg_only) return SVN_NO_ERROR; for (i = 0; i < (sizeof(paths) / sizeof(const char *)); i++) { svn_boolean_t retval; retval = svn_path_is_uri_safe(paths[i]); if (retvals[i] != retval) return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_is_uri_safe (%s) returned %s instead of %s", paths[i], retval ? "TRUE" : "FALSE", retvals[i] ? "TRUE" : "FALSE"); } return SVN_NO_ERROR;}static svn_error_t *test_uri_encode(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ int i; const char *paths[5][2] = { { "http://subversion.tigris.org", "http://subversion.tigris.org"}, { " special_at_beginning", "%20special_at_beginning" }, { "special_at_end ", "special_at_end%20" }, { "special in middle", "special%20in%20middle" }, { "\"Ouch!\" \"Did that hurt?\"", "%22Ouch!%22%20%20%22Did%20that%20hurt%3F%22" } }; *msg = "test svn_path_uri_[en/de]code"; if (msg_only) return SVN_NO_ERROR; for (i = 0; i < 5; i++) { const char *en_path, *de_path; /* URI-encode the path, and verify the results. */ en_path = svn_path_uri_encode(paths[i][0], pool); if (strcmp(en_path, paths[i][1])) { return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_uri_encode ('%s') returned '%s' instead of '%s'", paths[i][0], en_path, paths[i][1]); } /* URI-decode the path, and make sure we're back where we started. */ de_path = svn_path_uri_decode(en_path, pool); if (strcmp(de_path, paths[i][0])) { return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_uri_decode ('%s') returned '%s' instead of '%s'", paths[i][1], de_path, paths[i][0]); } } return SVN_NO_ERROR;}static svn_error_t *test_uri_decode(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ int i; const char *paths[3][2] = { { "http://c.r.a/s%\0008me", "http://c.r.a/s%"}, { "http://c.r.a/s%6\000me", "http://c.r.a/s%6" }, { "http://c.r.a/s%68me", "http://c.r.a/shme" }, }; *msg = "test svn_path_uri_decode with invalid escape"; if (msg_only) return SVN_NO_ERROR; for (i = 0; i < 3; i++) { const char *de_path; /* URI-decode the path, and verify the results. */ de_path = svn_path_uri_decode(paths[i][0], pool); if (strcmp(de_path, paths[i][1])) { return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_uri_decode ('%s') returned '%s' instead of '%s'", paths[i][0], de_path, paths[i][1]); } } return SVN_NO_ERROR;}static svn_error_t *test_uri_autoescape(const char **msg, svn_boolean_t msg_only, svn_test_opts_t *opts, apr_pool_t *pool){ static const char *paths[3][2] = { { "http://svn.collab.net/", "http://svn.collab.net/" }, { "file:///<>\" {}|\\^`", "file:///%3C%3E%22%20%7B%7D%7C%5C%5E%60" }, { "http://[::1]", "http://[::1]" } }; int i; *msg = "test svn_path_uri_autoescape"; if (msg_only) return SVN_NO_ERROR; for (i = 0; i < 3; ++i) { const char* uri = svn_path_uri_autoescape(paths[i][0], pool); if (strcmp(uri, paths[i][1]) != 0) return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_uri_autoescape on '%s' returned '%s' instead of '%s'", paths[i][0], uri, paths[i][1]); if (strcmp(paths[i][0], paths[i][1]) == 0 && paths[i][0] != uri) return svn_error_createf (SVN_ERR_TEST_FAILED, NULL, "svn_path_uri_autoescape on '%s' returned identical but not same" " string", paths[i][0]); } return SVN_NO_ERROR;}static svn_error_t *test_uri_from_iri(const char **msg, svn_boolean_t msg_only,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -