// Copyright (c) 2005-2007 Hartmut Kaiser (hartmut.kaiser@gmail.com) // Copyright (c) 2005-2007 Andre Merzky (andre@merzky.net) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include using boost::unit_test_framework::test_suite; #include "test_saga.hpp" /////////////////////////////////////////////////////////////////////////////// namespace test_url { struct test_data { char const *uri; // URI to parse */ bool isvalid; // is this URI expected to be valid? char const *error_msg; // if not: expected error message char const *scheme; // if yes: expected schema char const *authority; // if yes: expected authority char const *host; // if yes: expected host int port; // if yes: expected port char const *path; // if yes: expected path char const *user_info; // if yes: expected user_info char const *query; // if yes: expected query char const *fragment; // if yes: expected fragment }; struct test_data const uris_totest[] = { { "/bar/foo/", true, NULL, NULL, NULL, NULL, 0, "/bar/foo/", NULL, NULL, NULL, }, { "bar/foo/", true, NULL, NULL, NULL, NULL, 0, "bar/foo/", NULL, NULL, NULL, }, { "file:////bar/foo/", true, NULL, "file", NULL, NULL, 0, "/bar/foo/", NULL, NULL, NULL, }, { "file:///bar/foo/", true, NULL, "file", NULL, NULL, 0, "bar/foo/", NULL, NULL, NULL, }, { "file://host//bar/foo/", true, NULL, "file", "host", "host", 0, "/bar/foo/", NULL, NULL, NULL, }, { "file://host/bar/foo/", true, NULL, "file", "host", "host", 0, "bar/foo/", NULL, NULL, NULL, }, { "file:////bar/foo/", true, NULL, "file", NULL, NULL, 0, "/bar/foo/", NULL, NULL, NULL, }, { "file:///bar/foo/", true, NULL, "file", NULL, NULL, 0, "bar/foo/", NULL, NULL, NULL, }, { "?foo=rugue&bar", true, NULL, NULL, NULL, NULL, 0, NULL, NULL, "foo=rugue&bar", NULL, }, { ";foo=rugue&bar", true, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, }, { "#fragment", true, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, "fragment", }, { "http:#frag", true, NULL, "http", NULL, NULL, 0, NULL, NULL, NULL, "frag", }, { "bar/foo/", true, NULL, NULL, NULL, NULL, 0, "bar/foo/", NULL, NULL, NULL, }, { "../bar/../foo/", true, NULL, NULL, NULL, NULL, 0, "../bar/../foo/", NULL, NULL, NULL, }, { "http:/bar/foo", true, NULL, "http", NULL, NULL, 0, "/bar/foo", NULL, NULL, NULL, }, { "http:bar/foo", true, NULL, "http", NULL, NULL, 0, "bar/foo", NULL, NULL, NULL, }, { "http://www.ceic.com/", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, NULL, NULL, NULL, NULL, }, { "HTTP://www.ceic.com/", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, NULL, NULL, NULL, NULL, }, { "HTTP://www.CEIC.com/", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, NULL, NULL, NULL, NULL, }, { "http://www.ceic.com./", true, NULL, "http", "www.ceic.com.", "www.ceic.com.", 0, NULL, NULL, NULL, NULL, }, { "http://www.ceic.com:8400/", true, NULL, "http", "www.ceic.com:8400", "www.ceic.com", 8400, NULL, NULL, NULL, NULL, }, { "http://www.ceic.com:80/", true, NULL, "http", "www.ceic.com:80", "www.ceic.com", 80, NULL, NULL, NULL, NULL, }, { "http://www.ceic.com:8400/cgi.pl?foo", true, NULL, "http", "www.ceic.com:8400", "www.ceic.com", 8400, "cgi.pl", NULL, "foo", NULL, }, { "http://www.ceic.com:8400/cgi.pl;bar?foo", true, NULL, "http", "www.ceic.com:8400", "www.ceic.com", 8400, "cgi.pl", NULL, "foo", NULL, }, { "http://www.ceic.com:8400/foo/bar/nitz.html#frob", true, NULL, "http", "www.ceic.com:8400", "www.ceic.com", 8400, "foo/bar/nitz.html", NULL, NULL, "frob", }, { "http://loic:foobar@www.ceic.com:8400/foo/bar/nitz.html#frob", true, NULL, "http", "loic:foobar@www.ceic.com:8400", "www.ceic.com", 8400, "foo/bar/nitz.html", "loic:foobar", NULL, "frob", }, { "http://john:lec%7erre@www.ceic.com/", true, NULL, "http", "john:lec~rre@www.ceic.com", "www.ceic.com", 0, NULL, "john:lec~rre", NULL, NULL, }, { "http://redford@www.ceic.com:8400/", true, NULL, "http", "redford@www.ceic.com:8400", "www.ceic.com", 8400, NULL, "redford", NULL, NULL, }, { "http://redford@www.ceic.com:8400/:foo&'bar@=/dir/file.html", true, NULL, "http", "redford@www.ceic.com:8400", "www.ceic.com", 8400, ":foo&'bar@=/dir/file.html", "redford", NULL, NULL, }, { "http://red%2fgros@www.ceic.com:8400/", true, NULL, "http", "red/gros@www.ceic.com:8400", "www.ceic.com", 8400, NULL, "red/gros", NULL, NULL, }, { "http://www.ceic.com:8400/cgi-bin/script?bar/foo#ta/g", true, NULL, "http", "www.ceic.com:8400", "www.ceic.com", 8400, "cgi-bin/script", NULL, "bar/foo", "ta/g", }, { "http://www.ceic.com../cgi-bin/script", true, NULL, "http", "www.ceic.com..", "www.ceic.com..", 0, "cgi-bin/script", NULL, NULL, NULL, }, { "http://www.ceic.com/dir//file.html", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "dir//file.html", NULL, NULL, NULL, }, { "http://www.ceic.com/%2F/dir/", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "//dir/", NULL, NULL, NULL, }, { "http://www.ceic.com/dir/../file", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "dir/../file", NULL, NULL, NULL, }, { "http://www.ceic.com/dir/./file", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "dir/./file", NULL, NULL, NULL, }, { "http://www.ceic.com/./file", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "./file", NULL, NULL, NULL, }, { "http://www.ceic.com/file/.", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "file/.", NULL, NULL, NULL, }, { "http://www.ceic.com/file/./", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "file/./", NULL, NULL, NULL, }, { "http://www.ceic.com/dir1/dir2/..", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "dir1/dir2/..", NULL, NULL, NULL, }, { "http://www.ceic.com/file/../dir/../../foo", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "file/../dir/../../foo", NULL, NULL, NULL, }, { "/../foo.html%20http://w3.austin.ibm.com/", true, NULL, NULL, NULL, NULL, 0, "/../foo.html http://w3.austin.ibm.com/", NULL, NULL, NULL, }, { "/bar/../foo.html%20http://w3.austin.ibm.com/", true, NULL, NULL, NULL, NULL, 0, "/bar/../foo.html http://w3.austin.ibm.com/", NULL, NULL, NULL, }, { "http://www.ceic.com/file?a+%2B+b", true, NULL, "http", "www.ceic.com", "www.ceic.com", 0, "file", NULL, "a+++b", NULL, }, { "document.html%20http://www.senat.fr/", true, NULL, NULL, NULL, NULL, 0, "document.html http://www.senat.fr/", NULL, NULL, NULL, }, { "#frag%20http://www.senat.fr/", true, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, "frag http://www.senat.fr/", }, { "/document.html%20http://www.senat.fr/iway/", true, NULL, NULL, NULL, NULL, 0, "/document.html http://www.senat.fr/iway/", NULL, NULL, NULL, }, { "//www.curie.u-psud.fr/umr146/%20http://www.curie.fr/", true, NULL, NULL, "www.curie.u-psud.fr", "www.curie.u-psud.fr", 0, "umr146/ http://www.curie.fr/", NULL, NULL, NULL, }, { "http://www.foo.bar/bar//foo/file//", true, NULL, "http", "www.foo.bar", "www.foo.bar", 0, "bar//foo/file//", NULL, NULL, NULL, }, { "/bar/////bbobo/grunt.html%20http://www.pire.org/", true, NULL, NULL, NULL, NULL, 0, "/bar/////bbobo/grunt.html http://www.pire.org/", NULL, NULL, NULL, }, { "ftp://www.foo.bar/bar/", true, NULL, "ftp", "www.foo.bar", "www.foo.bar", 0, "bar/", NULL, NULL, NULL, }, { "ftp://www.foo.bar/bar?foo/bar", true, NULL, "ftp", "www.foo.bar", "www.foo.bar", 0, "bar", NULL, "foo/bar", NULL, }, { "ftp://ftp@www.foo.bar/bar?foo/bar", true, NULL, "ftp", "ftp@www.foo.bar", "www.foo.bar", 0, "bar", "ftp", "foo/bar", NULL, }, { "ftp://anonymous@www.foo.bar/bar?foo/bar", true, NULL, "ftp", "anonymous@www.foo.bar", "www.foo.bar", 0, "bar", "anonymous", "foo/bar", NULL, }, { "ftp://anonymous:pass@www.foo.bar/bar?foo/bar", true, NULL, "ftp", "anonymous:pass@www.foo.bar", "www.foo.bar", 0, "bar", "anonymous:pass", "foo/bar", NULL, }, { "gsiftp://www.foo.bar/bar/", true, NULL, "gsiftp", "www.foo.bar", "www.foo.bar", 0, "bar/", NULL, NULL, NULL, }, { "gsiftp://www.foo.bar/bar?foo/bar", true, NULL, "gsiftp", "www.foo.bar", "www.foo.bar", 0, "bar", NULL, "foo/bar", NULL, }, { "gsiftp://ftp@www.foo.bar/bar?foo/bar", true, NULL, "gsiftp", "ftp@www.foo.bar", "www.foo.bar", 0, "bar", "ftp", "foo/bar", NULL, }, { "gsiftp://anonymous@www.foo.bar:8211/bar?foo/bar", true, NULL, "gsiftp", "anonymous@www.foo.bar:8211", "www.foo.bar", 8211, "bar", "anonymous", "foo/bar", NULL, }, { "gsiftp://anonymous:pass@www.foo.bar:8211/bar?foo/bar", true, NULL, "gsiftp", "anonymous:pass@www.foo.bar:8211", "www.foo.bar", 8211, "bar", "anonymous:pass", "foo/bar", NULL, }, { "mailto:loic@ceic.com", true, NULL, "mailto", NULL, NULL, 0, "loic@ceic.com", NULL, NULL, NULL, }, { "file:/dir/a/b/c.html", true, NULL, "file", NULL, NULL, 0, "/dir/a/b/c.html", NULL, NULL, NULL, }, { "file://hostname/dir/a/b/c.html", true, NULL, "file", "hostname", "hostname", 0, "dir/a/b/c.html", NULL, NULL, NULL, }, { "file://locAlhost/dir/a/b/c.html", true, NULL, "file", "localhost", "localhost", 0, "dir/a/b/c.html", NULL, NULL, NULL, }, { "file:///dir/a/b/c.html", true, NULL, "file", NULL, NULL, 0, "dir/a/b/c.html", NULL, NULL, NULL, }, { "file://dir/a/b/c.html#frag", true, NULL, "file", "dir", "dir", 0, "a/b/c.html", NULL, NULL, "frag", }, { "file:/dir/a/b?/c.html", true, NULL, "file", NULL, NULL, 0, "/dir/a/b", NULL, "/c.html", NULL, }, { "gopher://host:10/Tfoo%09bar%09foobar", true, NULL, "gopher", "host:10", "host", 10, "Tfoo\tbar\tfoobar", NULL, NULL, NULL, }, { "gopher://host", true, NULL, "gopher", "host", "host", 0, NULL, NULL, NULL, NULL, }, { "gopher://host/Tfoo%09bar", true, NULL, "gopher", "host", "host", 0, "Tfoo\tbar", NULL, NULL, NULL, }, { "gopher://host/Tfoo", true, NULL, "gopher", "host", "host", 0, "Tfoo", NULL, NULL, NULL, }, { "gopher://host/T", true, NULL, "gopher", "host", "host", 0, "T", NULL, NULL, NULL, }, { "news:alt.adoptive.parenting", true, NULL, "news", NULL, NULL, 0, "alt.adoptive.parenting", NULL, NULL, NULL, }, { "news:EFGJG4.7A@deshaw.com", true, NULL, "news", NULL, NULL, 0, "EFGJG4.7A@deshaw.com", NULL, NULL, NULL, }, { "news://binky.capnet.state.tx.us/5rb1or$67v@news.jumpnet.com", true, NULL, "news", "binky.capnet.state.tx.us", "binky.capnet.state.tx.us", 0, "5rb1or$67v@news.jumpnet.com", NULL, NULL, NULL, }, { "news://adenine.c.dna.affrc.go.jp/misc.transport.air-industry.cargo", true, NULL, "news", "adenine.c.dna.affrc.go.jp", "adenine.c.dna.affrc.go.jp", 0, "misc.transport.air-industry.cargo", NULL, NULL, NULL, }, { "snews://binky.capnet.state.tx.us/5rb1or$67v@news.jumpnet.com", true, NULL, "snews", "binky.capnet.state.tx.us", "binky.capnet.state.tx.us", 0, "5rb1or$67v@news.jumpnet.com", NULL, NULL, NULL, }, { "nntp://news.ceic.com/alt.binaries/234", true, NULL, "nntp", "news.ceic.com", "news.ceic.com", 0, "alt.binaries/234", NULL, NULL, NULL, }, { "nntp://news.ceic.com:8080/alt.binaries/234", true, NULL, "nntp", "news.ceic.com:8080", "news.ceic.com", 8080, "alt.binaries/234", NULL, NULL, NULL, }, { "nntp://news.ceic.com:119/alt.binaries/234", true, NULL, "nntp", "news.ceic.com:119", "news.ceic.com", 119, "alt.binaries/234", NULL, NULL, NULL, }, { "wais://wais.ceic.com/base", true, NULL, "wais", "wais.ceic.com", "wais.ceic.com", 0, "base", NULL, NULL, NULL, }, { "wais://wais.ceic.com:210/base?query", true, NULL, "wais", "wais.ceic.com:210", "wais.ceic.com", 210, "base", NULL, "query", NULL, }, { "wais://wais.ceic.com/base/wtype/wpath", true, NULL, "wais", "wais.ceic.com", "wais.ceic.com", 0, "base/wtype/wpath", NULL, NULL, NULL, }, { "wais://wais.ceic.com:210/base/wtype/wpath", true, NULL, "wais", "wais.ceic.com:210", "wais.ceic.com", 210, "base/wtype/wpath", NULL, NULL, NULL, }, { "wais://wais.ceic.com//base/wtype/wpath", true, NULL, "wais", "wais.ceic.com", "wais.ceic.com", 0, "/base/wtype/wpath", NULL, NULL, NULL, }, { "wais://wais.ceic.com:8080//base/wtype/wpath", true, NULL, "wais", "wais.ceic.com:8080", "wais.ceic.com", 8080, "/base/wtype/wpath", NULL, NULL, NULL, }, { "wais://usr:pass@wais.ceic.com:210//base/wtype/wpath", true, NULL, "wais", "usr:pass@wais.ceic.com:210", "wais.ceic.com", 210, "/base/wtype/wpath", "usr:pass", NULL, NULL, }, { NULL, false, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, }, }; /////////////////////////////////////////////////////////////////////////////// // invalid url's struct test_data const invalid_uris_totest[] = { { "http://:8080/", false, "saga::url: uri_cannonicalize: http://:8080/ has null netloc", NULL, NULL, NULL, 8080, NULL, NULL, NULL, NULL, }, { "http://loic:foo34@/#frag", false, "saga::url: cannonicalize: in http://#frag, passwd cannot be set in relative uri", NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, }, { "http://redgros@www.c%2feic.com:8400/", false, NULL, "http", "redgros@www.c%2feic.com:8400", "www.c%2feic.com", 8400, NULL, NULL, NULL, NULL, }, // { "ttp://www.ceic.com/", false, // "saga::url: unknown scheme ttp found in uri ttp://www.ceic.com/", // NULL, NULL, NULL, 0, // NULL, NULL, NULL, NULL, }, // { "http://../dir/", false, // "saga::url: uri_cannonicalize: http://../dir/ has null netloc", // NULL, NULL, NULL, 0, // NULL, NULL, NULL, NULL, }, // The following testcase uses non-ASCII characters in the URI, so that this // verify_tostring test would fail (the saga::url used herein are initialised // from ASCII). // { "http://www.ceic.com/ŠtŠ/", true, NULL, // "http", "www.ceic.com", "www.ceic.com", 80, // "%A9t%A9/", NULL, NULL, NULL, }, { "http://www.ceic.com/t/", true, NULL, "http", "www.ceic.com", "www.ceic.com", 80, "%01t%03/", NULL, NULL, NULL, }, // { "news://news.foo.com/", false, // "saga::url: news_parse: empty group or article in news://news.foo.com/", // NULL, NULL, NULL, 0, // NULL, NULL, NULL, NULL, }, // { "nntp://news.ceic.com/alt.binaries", false, // "saga::url: nntp_parse: empty article in nntp://news.ceic.com/alt.binaries", // NULL, NULL, NULL, 0, // NULL, NULL, NULL, NULL, }, // { "nntp://news.ceic.com/", false, // "saga::url: nntp_parse: empty group in nntp://news.ceic.com/", // NULL, NULL, NULL, 0, // NULL, NULL, NULL, NULL, }, // { "nntp:///alt.binaries/234", false, // "saga::url: nntp_parse: empty host in nntp:///alt.binaries/234", // NULL, NULL, NULL, 0, // NULL, NULL, NULL, NULL, }, // { "wais://wais.ceic.com/base/wtype/wpath?query", false, // "saga::url: wais_parse: unexpected wtype or wpath with query in " // "wais://wais.ceic.com/base?query", // NULL, NULL, NULL, 0, // NULL, NULL, NULL, NULL, }, // { "wais://wais.ceic.com/base/wtype", false, // "saga::url: wais_parse: missing wpath in wais://wais.ceic.com/base/wtype", // NULL, NULL, NULL, 0, // NULL, NULL, NULL, NULL, }, // { "nntp://news.ceic.com///alt.binaries/234", false, NULL, // "nntp", "news.ceic.com", "news.ceic.com", 0, // "//alt.binaries/234", NULL, NULL, NULL, }, { NULL, false, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, }, }; /////////////////////////////////////////////////////////////////////////// void url_scheme_tests() { for (int index = 0; NULL != uris_totest[index].uri; ++index) { saga::url u(uris_totest[index].uri); if (NULL != uris_totest[index].scheme) { BOOST_CHECK_EQUAL(u.get_scheme(), uris_totest[index].scheme); } else { BOOST_CHECK_EQUAL(u.get_scheme(), ""); } } } void url_authority_tests() { for (int index = 0; NULL != uris_totest[index].uri; ++index) { saga::url u(uris_totest[index].uri); if (NULL != uris_totest[index].authority) { BOOST_CHECK_EQUAL(u.get_authority(), uris_totest[index].authority); } else { BOOST_CHECK_EQUAL(u.get_authority(), ""); } } } void url_userinfo_tests() { for (int index = 0; NULL != uris_totest[index].uri; ++index) { saga::url u(uris_totest[index].uri); if (NULL != uris_totest[index].user_info) { BOOST_CHECK_EQUAL(u.get_userinfo(), uris_totest[index].user_info); } else { BOOST_CHECK_EQUAL(u.get_userinfo(), ""); } } } void url_hostname_tests() { for (int index = 0; NULL != uris_totest[index].uri; ++index) { saga::url u(uris_totest[index].uri); if (NULL != uris_totest[index].host) { BOOST_CHECK_EQUAL(u.get_host(), uris_totest[index].host); } else { BOOST_CHECK_EQUAL(u.get_host(), ""); } } } void url_port_tests() { for (int index = 0; NULL != uris_totest[index].uri; ++index) { saga::url u(uris_totest[index].uri); if (0 != uris_totest[index].port) { BOOST_CHECK_EQUAL(u.get_port(), uris_totest[index].port); } else { BOOST_CHECK_EQUAL(u.get_port(), -1); } } } void url_path_tests() { for (int index = 0; NULL != uris_totest[index].uri; ++index) { saga::url u(uris_totest[index].uri); if (NULL != uris_totest[index].path) { BOOST_CHECK_EQUAL(u.get_path(), uris_totest[index].path); } else { BOOST_CHECK_EQUAL(u.get_path(), ""); } } } void url_fragment_tests() { for (int index = 0; NULL != uris_totest[index].uri; ++index) { saga::url u(uris_totest[index].uri); if (NULL != uris_totest[index].fragment) { BOOST_CHECK_EQUAL(u.get_fragment(), uris_totest[index].fragment); } else { BOOST_CHECK_EQUAL(u.get_fragment(), ""); } } } void url_query_tests() { for (int index = 0; NULL != uris_totest[index].uri; ++index) { saga::url u(uris_totest[index].uri); if (NULL != uris_totest[index].query) { BOOST_CHECK_EQUAL(u.get_query(), uris_totest[index].query); } else { BOOST_CHECK_EQUAL(u.get_query(), ""); } } } static std::string build_url(int index) { std::string result; if (NULL != uris_totest[index].scheme) { result += uris_totest[index].scheme; result += ':'; } if (NULL != uris_totest[index].authority) { result += "//"; result += uris_totest[index].authority; result += '/'; } if (NULL != uris_totest[index].path) { result += uris_totest[index].path; } if (NULL != uris_totest[index].query) { result += '?'; result += uris_totest[index].query; } if (NULL != uris_totest[index].fragment) { result += '#'; result += uris_totest[index].fragment; } return result; } void url_tests() { for (int index = 0; NULL != uris_totest[index].uri; ++index) { saga::url u(uris_totest[index].uri); BOOST_CHECK_EQUAL(u.get_url(), build_url(index)); } } /////////////////////////////////////////////////////////////////////////// void url_invalid_tests() { for (int index = 0; NULL != invalid_uris_totest[index].uri; ++index) { saga::url u; #if BOOST_VERSION <= 103301 BOOST_CHECK_THROW((u = saga::url(invalid_uris_totest[index].uri)), std::exception); #else BOOST_CHECK_THROW((u = saga::url(invalid_uris_totest[index].uri)), saga::exception); #endif } } /////////////////////////////////////////////////////////////////////////////// } // namespace test_url