From 66754aac5adc35e91dc87ad6e627150a15e85f10 Mon Sep 17 00:00:00 2001 From: EinMByte Date: Sun, 19 Jul 2015 15:51:02 +0200 Subject: [PATCH] Tests and documentation for util::url. --- tests/Utility.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++++ util.cpp | 26 ++++++++--------- util.h | 24 ++++++++++------ 3 files changed, 100 insertions(+), 22 deletions(-) diff --git a/tests/Utility.cpp b/tests/Utility.cpp index 2790c1fd..7fabfce7 100644 --- a/tests/Utility.cpp +++ b/tests/Utility.cpp @@ -14,6 +14,78 @@ BOOST_AUTO_TEST_CASE(DecodeUrl) { BOOST_CHECK_EQUAL(urlDecode("%20"), " "); } +BOOST_AUTO_TEST_CASE(ParseUrlProtocol) +{ + BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").protocol_, "http"); + BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").protocol_, "http"); + BOOST_CHECK_EQUAL(url("ftp://user@localhost:123").protocol_, "ftp"); + BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").protocol_, "ssh"); + BOOST_CHECK_EQUAL(url("").protocol_, ""); +} + +BOOST_AUTO_TEST_CASE(ParseUrlHost) +{ + BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").host_, "127.0.0.1"); + BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").host_, "site.com"); + BOOST_CHECK_EQUAL(url("ftp://user@localhost:123").host_, "localhost"); + BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").host_, "localhost"); + BOOST_CHECK_EQUAL(url("").host_, ""); +} + +BOOST_AUTO_TEST_CASE(ParseUrlPath) +{ + BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").path_, "/asdasd"); + BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").path_, "/A/B"); + BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").path_, "/A/B/C/D"); + BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").path_, ""); + BOOST_CHECK_EQUAL(url("").path_, ""); +} +BOOST_AUTO_TEST_CASE(ParseUrlQuery) +{ + BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").query_, "qqqqqqqqqqqq"); + BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").query_, "q"); + BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").query_, "x=A"); + BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").query_, ""); + BOOST_CHECK_EQUAL(url("").query_, ""); +} + +BOOST_AUTO_TEST_CASE(ParseUrlPortStr) +{ + BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").portstr_, "7070"); + BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").portstr_, "err_port"); + BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").portstr_, "123"); + BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").portstr_, "123"); + BOOST_CHECK_EQUAL(url("").portstr_, "80"); +} + +BOOST_AUTO_TEST_CASE(ParseUrlPort) +{ + BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").port_, 7070); + BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").port_, 80); + BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").port_, 123); + BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").port_, 123); + BOOST_CHECK_EQUAL(url("").port_, 80); +} + +BOOST_AUTO_TEST_CASE(ParseUrlUser) +{ + BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").user_, ""); + BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").user_, "user"); + BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").user_, "user"); + BOOST_CHECK_EQUAL(url("SSH://@localhost:123").user_, ""); + BOOST_CHECK_EQUAL(url("SSH://user:@localhost:123").user_, "user"); + BOOST_CHECK_EQUAL(url("").user_, ""); +} + +BOOST_AUTO_TEST_CASE(ParseUrlPassword) +{ + BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").pass_, ""); + BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").pass_, "password"); + BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").pass_, ""); + BOOST_CHECK_EQUAL(url("SSH://@localhost:123").pass_, ""); + BOOST_CHECK_EQUAL(url("SSH://:password@localhost:123").pass_, "password"); + BOOST_CHECK_EQUAL(url("").pass_, ""); +} BOOST_AUTO_TEST_SUITE_END() diff --git a/util.cpp b/util.cpp index db96d964..6257da9f 100644 --- a/util.cpp +++ b/util.cpp @@ -48,12 +48,12 @@ http://stackoverflow.com/questions/15660203/inet-pton-identifier-not-found */ { switch (af) { - case AF_INET: - *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr; - return 1; - case AF_INET6: - *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr; - return 1; + case AF_INET: + *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr; + return 1; + case AF_INET6: + *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr; + return 1; } } return 0; @@ -62,15 +62,15 @@ http://stackoverflow.com/questions/15660203/inet-pton-identifier-not-found */ namespace i2p { -namespace util -{ + namespace util + { -namespace config -{ - std::map mapArgs; - std::map > mapMultiArgs; + namespace config + { + std::map mapArgs; + std::map > mapMultiArgs; - void OptionParser(int argc, const char* const argv[]) + void OptionParser(int argc, const char* const argv[]) { mapArgs.clear(); mapMultiArgs.clear(); diff --git a/util.h b/util.h index 2b70e703..91bac598 100644 --- a/util.h +++ b/util.h @@ -52,16 +52,22 @@ namespace util int httpRequestViaI2pProxy(const std::string& address, std::string &content); // return http code std::string urlDecode(const std::string& data); + /** + * Provides functionality for parsing URLs. + */ struct url { - url(const std::string& url_s); // omitted copy, ==, accessors, ... - private: - void parse(const std::string& url_s); - public: - std::string protocol_, host_, path_, query_; - std::string portstr_; - unsigned int port_; - std::string user_; - std::string pass_; + /** + * Parse a url given as a string. + */ + url(const std::string& url_s); + private: + void parse(const std::string& url_s); + public: + std::string protocol_, host_, path_, query_; + std::string portstr_; + unsigned int port_; + std::string user_; + std::string pass_; }; }