From 36c00fb80289cf30cb34a3fcb3c5cbed0e73a533 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 10 Sep 2021 19:41:45 +0100 Subject: Add boost-test-extra.h and use it in UriParse --- libadhocutil/boost-test-extra.h | 10 ++ libadhocutil/unittests/testUriParse.cpp | 281 +++++++++++++++++--------------- 2 files changed, 157 insertions(+), 134 deletions(-) create mode 100644 libadhocutil/boost-test-extra.h diff --git a/libadhocutil/boost-test-extra.h b/libadhocutil/boost-test-extra.h new file mode 100644 index 0000000..04db643 --- /dev/null +++ b/libadhocutil/boost-test-extra.h @@ -0,0 +1,10 @@ +#ifndef ADHOCUTIL_BOOST_TEST_EXTRA_H +#define ADHOCUTIL_BOOST_TEST_EXTRA_H + +#define BOOST_CHECK_IF(VAR, EXPR) \ + if (auto VAR = (EXPR); !VAR) { \ + BOOST_CHECK(VAR); \ + } \ + else + +#endif diff --git a/libadhocutil/unittests/testUriParse.cpp b/libadhocutil/unittests/testUriParse.cpp index 113df94..ff49e40 100644 --- a/libadhocutil/unittests/testUriParse.cpp +++ b/libadhocutil/unittests/testUriParse.cpp @@ -1,6 +1,7 @@ #define BOOST_TEST_MODULE UriParse #include +#include "boost-test-extra.h" #include "uriParse.h" #include #include @@ -12,224 +13,236 @@ BOOST_AUTO_TEST_CASE(simple) { AdHoc::Uri u("http://localhost"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE(!u.port); - BOOST_REQUIRE(!u.username); - BOOST_REQUIRE(!u.password); - BOOST_REQUIRE(!u.path); - BOOST_REQUIRE(u.query.empty()); - BOOST_REQUIRE(!u.fragment); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK(!u.port); + BOOST_CHECK(!u.username); + BOOST_CHECK(!u.password); + BOOST_CHECK(!u.path); + BOOST_CHECK(u.query.empty()); + BOOST_CHECK(!u.fragment); } BOOST_AUTO_TEST_CASE(lowerScheme) { AdHoc::Uri u("HtTP://localhost"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE(!u.port); - BOOST_REQUIRE(!u.username); - BOOST_REQUIRE(!u.password); - BOOST_REQUIRE(!u.path); - BOOST_REQUIRE(u.query.empty()); - BOOST_REQUIRE(!u.fragment); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK(!u.port); + BOOST_CHECK(!u.username); + BOOST_CHECK(!u.password); + BOOST_CHECK(!u.path); + BOOST_CHECK(u.query.empty()); + BOOST_CHECK(!u.fragment); } BOOST_AUTO_TEST_CASE(simpleTrailingSlash) { AdHoc::Uri u("ssh+git://localhost/"); - BOOST_REQUIRE_EQUAL("ssh+git", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE(!u.port); - BOOST_REQUIRE(!u.username); - BOOST_REQUIRE(!u.password); - BOOST_REQUIRE(u.path); - BOOST_REQUIRE_EQUAL("", *u.path); - BOOST_REQUIRE(u.query.empty()); - BOOST_REQUIRE(!u.fragment); + BOOST_CHECK_EQUAL("ssh+git", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK(!u.port); + BOOST_CHECK(!u.username); + BOOST_CHECK(!u.password); + BOOST_CHECK_IF(p, u.path) { + BOOST_CHECK_EQUAL("", *u.path); + } + BOOST_CHECK(u.query.empty()); + BOOST_CHECK(!u.fragment); } BOOST_AUTO_TEST_CASE(simpleWithPort) { AdHoc::Uri u("http://localhost:80"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE(u.port); - BOOST_REQUIRE_EQUAL(80, *u.port); - BOOST_REQUIRE(!u.username); - BOOST_REQUIRE(!u.password); - BOOST_REQUIRE(!u.path); - BOOST_REQUIRE(u.query.empty()); - BOOST_REQUIRE(!u.fragment); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_IF(p, u.port) { + BOOST_CHECK_EQUAL(80, *u.port); + } + BOOST_CHECK(!u.username); + BOOST_CHECK(!u.password); + BOOST_CHECK(!u.path); + BOOST_CHECK(u.query.empty()); + BOOST_CHECK(!u.fragment); } BOOST_AUTO_TEST_CASE(simpleTrailingSlashWithPort) { AdHoc::Uri u("http://localhost:80/"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE(u.port); - BOOST_REQUIRE_EQUAL(80, *u.port); - BOOST_REQUIRE(!u.username); - BOOST_REQUIRE(!u.password); - BOOST_REQUIRE(u.path); - BOOST_REQUIRE_EQUAL("", *u.path); - BOOST_REQUIRE(u.query.empty()); - BOOST_REQUIRE(!u.fragment); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_IF(p, u.port) { + BOOST_CHECK_EQUAL(80, *u.port); + } + BOOST_CHECK(!u.username); + BOOST_CHECK(!u.password); + BOOST_CHECK_IF(p, u.path) { + BOOST_CHECK_EQUAL("", *u.path); + } + BOOST_CHECK(u.query.empty()); + BOOST_CHECK(!u.fragment); } BOOST_AUTO_TEST_CASE(username) { AdHoc::Uri u("http://user@localhost"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE(u.username); - BOOST_REQUIRE_EQUAL("user", *u.username); - BOOST_REQUIRE(!u.password); - BOOST_REQUIRE(!u.path); - BOOST_REQUIRE(u.query.empty()); - BOOST_REQUIRE(!u.fragment); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_IF(p, u.username) { + BOOST_CHECK_EQUAL("user", *u.username); + } + BOOST_CHECK(!u.password); + BOOST_CHECK(!u.path); + BOOST_CHECK(u.query.empty()); + BOOST_CHECK(!u.fragment); } BOOST_AUTO_TEST_CASE(usernameAndPassword) { AdHoc::Uri u("http://user:pass@localhost"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE(u.username); - BOOST_REQUIRE_EQUAL("user", *u.username); - BOOST_REQUIRE(u.password); - BOOST_REQUIRE_EQUAL("pass", *u.password); - BOOST_REQUIRE(!u.path); - BOOST_REQUIRE(u.query.empty()); - BOOST_REQUIRE(!u.fragment); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_IF(p, u.username) { + BOOST_CHECK_EQUAL("user", *u.username); + } + BOOST_CHECK_IF(p, u.password) { + BOOST_CHECK_EQUAL("pass", *u.password); + } + BOOST_CHECK(!u.path); + BOOST_CHECK(u.query.empty()); + BOOST_CHECK(!u.fragment); } BOOST_AUTO_TEST_CASE(path) { AdHoc::Uri u("http://localhost/path/to/resource"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE(!u.username); - BOOST_REQUIRE(!u.password); - BOOST_REQUIRE(u.path); - BOOST_REQUIRE_EQUAL("path/to/resource", *u.path); - BOOST_REQUIRE(u.query.empty()); - BOOST_REQUIRE(!u.fragment); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK(!u.username); + BOOST_CHECK(!u.password); + BOOST_CHECK_IF(p, u.path) { + BOOST_CHECK_EQUAL("path/to/resource", *u.path); + } + BOOST_CHECK(u.query.empty()); + BOOST_CHECK(!u.fragment); } BOOST_AUTO_TEST_CASE(query0) { AdHoc::Uri u("http://localhost/?"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE_EQUAL(0, u.query.size()); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_EQUAL(0, u.query.size()); } BOOST_AUTO_TEST_CASE(query1) { AdHoc::Uri u("http://localhost/?var=val"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE_EQUAL(1, u.query.size()); - BOOST_REQUIRE_EQUAL("var", u.query.begin()->first); - BOOST_REQUIRE_EQUAL("val", u.query.begin()->second); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_EQUAL(1, u.query.size()); + BOOST_CHECK_EQUAL("var", u.query.begin()->first); + BOOST_CHECK_EQUAL("val", u.query.begin()->second); } BOOST_AUTO_TEST_CASE(query2) { AdHoc::Uri u("http://localhost/?var=val&name=value"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE_EQUAL(2, u.query.size()); - BOOST_REQUIRE_EQUAL("name", u.query.begin()->first); - BOOST_REQUIRE_EQUAL("value", u.query.begin()->second); - BOOST_REQUIRE_EQUAL("var", u.query.rbegin()->first); - BOOST_REQUIRE_EQUAL("val", u.query.rbegin()->second); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_EQUAL(2, u.query.size()); + BOOST_CHECK_EQUAL("name", u.query.begin()->first); + BOOST_CHECK_EQUAL("value", u.query.begin()->second); + BOOST_CHECK_EQUAL("var", u.query.rbegin()->first); + BOOST_CHECK_EQUAL("val", u.query.rbegin()->second); } BOOST_AUTO_TEST_CASE(queryMany) { AdHoc::Uri u("http://localhost/?name=val&name=value"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE_EQUAL(2, u.query.size()); - BOOST_REQUIRE_EQUAL("name", u.query.begin()->first); - BOOST_REQUIRE_EQUAL("val", u.query.begin()->second); - BOOST_REQUIRE_EQUAL("name", u.query.rbegin()->first); - BOOST_REQUIRE_EQUAL("value", u.query.rbegin()->second); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_EQUAL(2, u.query.size()); + BOOST_CHECK_EQUAL("name", u.query.begin()->first); + BOOST_CHECK_EQUAL("val", u.query.begin()->second); + BOOST_CHECK_EQUAL("name", u.query.rbegin()->first); + BOOST_CHECK_EQUAL("value", u.query.rbegin()->second); } BOOST_AUTO_TEST_CASE(queryNoValue1) { AdHoc::Uri u("http://localhost/?n1"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE_EQUAL(1, u.query.size()); - BOOST_REQUIRE_EQUAL("n1", u.query.begin()->first); - BOOST_REQUIRE_EQUAL("", u.query.begin()->second); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_EQUAL(1, u.query.size()); + BOOST_CHECK_EQUAL("n1", u.query.begin()->first); + BOOST_CHECK_EQUAL("", u.query.begin()->second); } BOOST_AUTO_TEST_CASE(queryNoValue1eq) { AdHoc::Uri u("http://localhost/?n1="); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE_EQUAL(1, u.query.size()); - BOOST_REQUIRE_EQUAL("n1", u.query.begin()->first); - BOOST_REQUIRE_EQUAL("", u.query.begin()->second); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_EQUAL(1, u.query.size()); + BOOST_CHECK_EQUAL("n1", u.query.begin()->first); + BOOST_CHECK_EQUAL("", u.query.begin()->second); } BOOST_AUTO_TEST_CASE(queryNoValue2) { AdHoc::Uri u("http://localhost/?n1=&n2"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE_EQUAL(2, u.query.size()); - BOOST_REQUIRE_EQUAL("n1", u.query.begin()->first); - BOOST_REQUIRE_EQUAL("", u.query.begin()->second); - BOOST_REQUIRE_EQUAL("n2", u.query.rbegin()->first); - BOOST_REQUIRE_EQUAL("", u.query.rbegin()->second); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK_EQUAL(2, u.query.size()); + BOOST_CHECK_EQUAL("n1", u.query.begin()->first); + BOOST_CHECK_EQUAL("", u.query.begin()->second); + BOOST_CHECK_EQUAL("n2", u.query.rbegin()->first); + BOOST_CHECK_EQUAL("", u.query.rbegin()->second); } BOOST_AUTO_TEST_CASE(fragment) { AdHoc::Uri u("http://localhost/path/to/resource#someFrag"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("localhost", u.host); - BOOST_REQUIRE(!u.username); - BOOST_REQUIRE(!u.password); - BOOST_REQUIRE(u.path); - BOOST_REQUIRE_EQUAL("path/to/resource", *u.path); - BOOST_REQUIRE(u.query.empty()); - BOOST_REQUIRE(u.fragment); - BOOST_REQUIRE_EQUAL("someFrag", *u.fragment); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("localhost", u.host); + BOOST_CHECK(!u.username); + BOOST_CHECK(!u.password); + BOOST_CHECK_IF(p, u.path) { + BOOST_CHECK_EQUAL("path/to/resource", *u.path); + } + BOOST_CHECK(u.query.empty()); + BOOST_CHECK_IF(p, u.fragment) { + BOOST_CHECK_EQUAL("someFrag", *u.fragment); + } } BOOST_AUTO_TEST_CASE(ipv6) { AdHoc::Uri u("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html"); - BOOST_REQUIRE_EQUAL("http", u.scheme); - BOOST_REQUIRE_EQUAL("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]", u.host); - BOOST_REQUIRE(u.port); - BOOST_REQUIRE_EQUAL(80, *u.port); - BOOST_REQUIRE(u.path); - BOOST_REQUIRE_EQUAL("index.html", *u.path); + BOOST_CHECK_EQUAL("http", u.scheme); + BOOST_CHECK_EQUAL("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]", u.host); + BOOST_CHECK_IF(p, u.port) { + BOOST_CHECK_EQUAL(80, *u.port); + } + BOOST_CHECK_IF(p, u.path) { + BOOST_CHECK_EQUAL("index.html", *u.path); + } } BOOST_AUTO_TEST_CASE(bad) { - BOOST_REQUIRE_THROW(AdHoc::Uri(""), AdHoc::InvalidUri); - BOOST_REQUIRE_THROW(AdHoc::Uri("localhost"), AdHoc::InvalidUri); - BOOST_REQUIRE_THROW(AdHoc::Uri("t00+p://foo"), AdHoc::InvalidUri); - BOOST_REQUIRE_THROW(AdHoc::Uri("tcp:"), AdHoc::InvalidUri); - BOOST_REQUIRE_THROW(AdHoc::Uri("http:/"), AdHoc::InvalidUri); - BOOST_REQUIRE_THROW(AdHoc::Uri("tcp://"), AdHoc::InvalidUri); - BOOST_REQUIRE_THROW(AdHoc::Uri("ftp/local"), AdHoc::InvalidUri); - BOOST_REQUIRE_THROW(AdHoc::Uri("tcp://local:"), std::bad_cast); - BOOST_REQUIRE_THROW(AdHoc::Uri("tcp://local:foo"), std::bad_cast); - BOOST_REQUIRE_THROW(AdHoc::Uri("tcp://user:pass@"), AdHoc::InvalidUri); + BOOST_CHECK_THROW(AdHoc::Uri(""), AdHoc::InvalidUri); + BOOST_CHECK_THROW(AdHoc::Uri("localhost"), AdHoc::InvalidUri); + BOOST_CHECK_THROW(AdHoc::Uri("t00+p://foo"), AdHoc::InvalidUri); + BOOST_CHECK_THROW(AdHoc::Uri("tcp:"), AdHoc::InvalidUri); + BOOST_CHECK_THROW(AdHoc::Uri("http:/"), AdHoc::InvalidUri); + BOOST_CHECK_THROW(AdHoc::Uri("tcp://"), AdHoc::InvalidUri); + BOOST_CHECK_THROW(AdHoc::Uri("ftp/local"), AdHoc::InvalidUri); + BOOST_CHECK_THROW(AdHoc::Uri("tcp://local:"), std::bad_cast); + BOOST_CHECK_THROW(AdHoc::Uri("tcp://local:foo"), std::bad_cast); + BOOST_CHECK_THROW(AdHoc::Uri("tcp://user:pass@"), AdHoc::InvalidUri); AdHoc::InvalidUri ui("message", "http://localhost"); - BOOST_REQUIRE_EQUAL("InvalidUri (message) parsing [http://localhost]", ui.what()); + BOOST_CHECK_EQUAL("InvalidUri (message) parsing [http://localhost]", ui.what()); } -- cgit v1.2.3