123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- #include "uri_split.h"
- #include <cstring>
- #include <cppunit/extensions/HelperMacros.h>
- #include "uri_split.h"
- namespace aria2 {
- class UriSplitTest : public CppUnit::TestFixture {
- CPPUNIT_TEST_SUITE(UriSplitTest);
- CPPUNIT_TEST(testUriSplit);
- CPPUNIT_TEST(testUriSplit_fail);
- CPPUNIT_TEST_SUITE_END();
- public:
- void testUriSplit();
- void testUriSplit_fail();
- };
- CPPUNIT_TEST_SUITE_REGISTRATION(UriSplitTest);
- namespace {
- const char* fieldstr[] = {
- "USR_SCHEME", "USR_HOST", "USR_PORT", "USR_PATH", "USR_QUERY",
- "USR_FRAGMENT", "USR_USERINFO", "USR_USER", "USR_PASSWD", "USR_BASENAME"};
- } // namespace
- #define CHECK_FIELD_SET(RES, FLAGS) \
- for (int i = 0; i < USR_MAX; ++i) { \
- int mask = 1 << i; \
- if ((FLAGS)&mask) { \
- CPPUNIT_ASSERT_MESSAGE(fieldstr[i], RES.field_set& mask); \
- } \
- else { \
- CPPUNIT_ASSERT_MESSAGE(fieldstr[i], !(RES.field_set & mask)); \
- } \
- }
- namespace {
- std::string mkstr(const uri_split_result& res, int field, const char* base)
- {
- return std::string(base + res.fields[field].off, res.fields[field].len);
- }
- } // namespace
- void UriSplitTest::testUriSplit()
- {
- uri_split_result res;
- const char* uri;
- uri = "http://aria2.sf.net/path/";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH));
- CPPUNIT_ASSERT_EQUAL(std::string("http"), mkstr(res, USR_SCHEME, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
- uri = "http://user@aria2.sf.net/path/";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_USERINFO) | (1 << USR_USER));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- uri = "http://user:pass@aria2.sf.net/path/";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_USERINFO) | (1 << USR_USER) |
- (1 << USR_PASSWD));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
- // According to RFC 3986, @ in userinfo is illegal. But many people
- // have e-mail account as username and don't understand
- // percent-encoding and keep getting erros putting it in URI in
- // unecoded form. Because of this, we support @ in username.
- uri = "http://user@foo.com:pass@aria2.sf.net/path/";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_USERINFO) | (1 << USR_USER) |
- (1 << USR_PASSWD));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user@foo.com:pass"),
- mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user@foo.com"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
- // Port processed in URI_MAYBE_USER -> URI_PORT
- uri = "https://aria2.sf.net:443/path/";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
- (1 << USR_PATH));
- CPPUNIT_ASSERT_EQUAL(std::string("https"), mkstr(res, USR_SCHEME, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
- // Port processed in URI_PORT
- uri = "https://user:pass@aria2.sf.net:443/path/";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
- (1 << USR_PATH) | (1 << USR_USERINFO) |
- (1 << USR_USER) | (1 << USR_PASSWD));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
- CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
- // Port processed in URI_MAYBE_PASSWD
- uri = "https://user@aria2.sf.net:443/path/";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
- (1 << USR_PATH) | (1 << USR_USERINFO) |
- (1 << USR_USER));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2.sf.net"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/path/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
- // Port processed in URI_MAYBE_PASSWD
- uri = "http://aria2";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- uri = "http://aria2:8080";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL((uint16_t)8080, res.port);
- uri = "http://user@aria2";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
- (1 << USR_USERINFO) | (1 << USR_USER));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- uri = "http://user:@aria2";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
- (1 << USR_USERINFO) | (1 << USR_USER) |
- (1 << USR_PASSWD));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user:"), mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_PASSWD, uri));
- uri = "http://aria2/?foo#bar";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_QUERY) | (1 << USR_FRAGMENT));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
- // URI_MAYBE_USER
- uri = "http://aria2?foo";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_QUERY));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
- // URI_MAYBE_USER
- uri = "http://aria2#bar";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res,
- (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_FRAGMENT));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
- // URI_MAYBE_PASSWD
- uri = "https://aria2:443?foo";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
- (1 << USR_QUERY));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
- CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
- // URI_MAYBE_PASSWD
- uri = "https://aria2:443#bar";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
- (1 << USR_FRAGMENT));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
- CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
- // URI_PORT
- uri = "https://user:pass@aria2:443?foo";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
- (1 << USR_QUERY) | (1 << USR_USERINFO) |
- (1 << USR_USER) | (1 << USR_PASSWD));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
- CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
- // URI_PORT
- uri = "https://user:pass@aria2:443#bar";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
- (1 << USR_FRAGMENT) | (1 << USR_USERINFO) |
- (1 << USR_USER) | (1 << USR_PASSWD));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
- CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
- // URI_HOST
- uri = "http://user:pass@aria2?foo";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_QUERY) |
- (1 << USR_USERINFO) | (1 << USR_USER) |
- (1 << USR_PASSWD));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
- // URI_HOST
- uri = "http://user:pass@aria2#bar";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
- (1 << USR_FRAGMENT) | (1 << USR_USERINFO) |
- (1 << USR_USER) | (1 << USR_PASSWD));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("bar"), mkstr(res, USR_FRAGMENT, uri));
- // empty query
- uri = "http://aria2/?";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_QUERY));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_QUERY, uri));
- // empty fragment
- uri = "http://aria2/#";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_FRAGMENT));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_FRAGMENT, uri));
- // empty query and fragment
- uri = "http://aria2/?#";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_QUERY) | (1 << USR_FRAGMENT));
- CPPUNIT_ASSERT_EQUAL(std::string("aria2"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_QUERY, uri));
- CPPUNIT_ASSERT_EQUAL(std::string(""), mkstr(res, USR_FRAGMENT, uri));
- // IPv6 numeric address
- uri = "http://[::1]";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST));
- CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
- uri = "https://[::1]:443";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT));
- CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL((uint16_t)443, res.port);
- CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
- // USR_MAYBE_USER
- uri = "https://user@[::1]";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
- (1 << USR_USERINFO) | (1 << USR_USER));
- CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
- // USR_BEFORE_HOST
- uri = "https://user:pass@[::1]";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
- (1 << USR_USERINFO) | (1 << USR_USER) |
- (1 << USR_PASSWD));
- CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user"), mkstr(res, USR_USER, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("pass"), mkstr(res, USR_PASSWD, uri));
- CPPUNIT_ASSERT(res.flags & USF_IPV6ADDR);
- uri = "http://aria2/f";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_BASENAME));
- CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
- uri = "http://[::1]/f";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_BASENAME));
- CPPUNIT_ASSERT_EQUAL(std::string("::1"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
- uri = "http://[::1]:8080/f";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PORT) |
- (1 << USR_PATH) | (1 << USR_BASENAME));
- CPPUNIT_ASSERT_EQUAL((uint16_t)8080, res.port);
- CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
- uri = "https://user:pass@host/f";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) |
- (1 << USR_USERINFO) | (1 << USR_USER) |
- (1 << USR_PASSWD) | (1 << USR_PATH) |
- (1 << USR_BASENAME));
- CPPUNIT_ASSERT_EQUAL(std::string("host"), mkstr(res, USR_HOST, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("user:pass"), mkstr(res, USR_USERINFO, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("/f"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("f"), mkstr(res, USR_BASENAME, uri));
- uri = "http://aria2/index.html?foo";
- memset(&res, 0, sizeof(res));
- CPPUNIT_ASSERT_EQUAL(0, uri_split(&res, uri));
- CHECK_FIELD_SET(res, (1 << USR_SCHEME) | (1 << USR_HOST) | (1 << USR_PATH) |
- (1 << USR_QUERY) | (1 << USR_BASENAME));
- CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), mkstr(res, USR_PATH, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("index.html"),
- mkstr(res, USR_BASENAME, uri));
- CPPUNIT_ASSERT_EQUAL(std::string("foo"), mkstr(res, USR_QUERY, uri));
- }
- void UriSplitTest::testUriSplit_fail()
- {
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, ""));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "h"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:a"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:/"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http:/a"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://:host"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://@user@host"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:65536"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass?"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass@host:65536"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass@host:x"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass@host:80x"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user@"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://[]"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://[::"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user[::1]"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user[::1]x"));
- CPPUNIT_ASSERT_EQUAL(-1, uri_split(nullptr, "http://user:pass[::1]"));
- }
- } // namespace aria2
|