diff options
Diffstat (limited to 'Swiften/Base/UnitTest')
-rw-r--r-- | Swiften/Base/UnitTest/ByteArrayTest.cpp | 65 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/DateTimeTest.cpp | 55 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/IDGeneratorTest.cpp | 51 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/LRUCacheTest.cpp | 117 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/LogTest.cpp | 49 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/PathTest.cpp | 30 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/SimpleIDGeneratorTest.cpp | 53 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/StringTest.cpp | 261 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/URLTest.cpp | 324 |
9 files changed, 668 insertions, 337 deletions
diff --git a/Swiften/Base/UnitTest/ByteArrayTest.cpp b/Swiften/Base/UnitTest/ByteArrayTest.cpp index ecd0439..f1732ae 100644 --- a/Swiften/Base/UnitTest/ByteArrayTest.cpp +++ b/Swiften/Base/UnitTest/ByteArrayTest.cpp @@ -1,56 +1,43 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include <cppunit/extensions/HelperMacros.h> -#include <cppunit/extensions/TestFactoryRegistry.h> - -#include <Swiften/Base/ByteArray.h> #include <boost/lexical_cast.hpp> -using namespace Swift; +#include <gtest/gtest.h> -class ByteArrayTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(ByteArrayTest); - CPPUNIT_TEST(testGetData_NoData); - CPPUNIT_TEST(testToString); - CPPUNIT_TEST(testToString_NullTerminated); - CPPUNIT_TEST(testToString_TwoNullTerminated); - CPPUNIT_TEST(testToString_AllNull); - CPPUNIT_TEST_SUITE_END(); +#include <Swiften/Base/ByteArray.h> - public: - void testGetData_NoData() { - ByteArray testling; +using namespace Swift; - CPPUNIT_ASSERT_EQUAL(reinterpret_cast<const char*>(NULL), reinterpret_cast<const char*>(vecptr(testling))); - } +TEST(ByteArrayTest, testGetData_NoData) { + ByteArray testling; - void testToString() { - ByteArray testling(createByteArray("abcde")); + ASSERT_EQ(reinterpret_cast<const char*>(NULL), reinterpret_cast<const char*>(vecptr(testling))); +} - CPPUNIT_ASSERT_EQUAL(std::string("abcde"), byteArrayToString(testling)); - } +TEST(ByteArrayTest, testToString) { + ByteArray testling(createByteArray("abcde")); - void testToString_NullTerminated() { - ByteArray testling(createByteArray("abcde\0", 6)); + ASSERT_EQ(std::string("abcde"), byteArrayToString(testling)); +} - CPPUNIT_ASSERT_EQUAL(std::string("abcde"), byteArrayToString(testling)); - } +TEST(ByteArrayTest, testToString_NullTerminated) { + ByteArray testling(createByteArray("abcde\0", 6)); - void testToString_TwoNullTerminated() { - ByteArray testling(createByteArray("abcde\0\0", 7)); + ASSERT_EQ(std::string("abcde"), byteArrayToString(testling)); +} - CPPUNIT_ASSERT_EQUAL(std::string("abcde"), byteArrayToString(testling)); - } +TEST(ByteArrayTest, testToString_TwoNullTerminated) { + ByteArray testling(createByteArray("abcde\0\0", 7)); - void testToString_AllNull() { - ByteArray testling(createByteArray("\0\0", 2)); + ASSERT_EQ(std::string("abcde"), byteArrayToString(testling)); +} - CPPUNIT_ASSERT_EQUAL(std::string(""), byteArrayToString(testling)); - } -}; +TEST(ByteArrayTest, testToString_AllNull) { + ByteArray testling(createByteArray("\0\0", 2)); -CPPUNIT_TEST_SUITE_REGISTRATION(ByteArrayTest); + ASSERT_EQ(std::string(""), byteArrayToString(testling)); +} diff --git a/Swiften/Base/UnitTest/DateTimeTest.cpp b/Swiften/Base/UnitTest/DateTimeTest.cpp index 8c3903a..6a82d96 100644 --- a/Swiften/Base/UnitTest/DateTimeTest.cpp +++ b/Swiften/Base/UnitTest/DateTimeTest.cpp @@ -1,43 +1,52 @@ /* - * Copyright (c) 2011 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2011-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ -#include <cppunit/extensions/HelperMacros.h> -#include <cppunit/extensions/TestFactoryRegistry.h> #include <string> + #include <boost/date_time/posix_time/posix_time.hpp> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/extensions/TestFactoryRegistry.h> + #include <Swiften/Base/DateTime.h> using namespace Swift; class DateTimeTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(DateTimeTest); - CPPUNIT_TEST(testStringToDateTime_UTC); - CPPUNIT_TEST(testStringToDateTime_WithTimezone); - CPPUNIT_TEST(testDateTimeToString); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(DateTimeTest); + CPPUNIT_TEST(testStringToDateTime_UTC); + CPPUNIT_TEST(testStringToDateTime_WithTimezone); + CPPUNIT_TEST(testDateTimeToString); + CPPUNIT_TEST(testDateTimeToLocalStringNotThrowingException); + CPPUNIT_TEST_SUITE_END(); + + public: + void testStringToDateTime_UTC() { + boost::posix_time::ptime time = stringToDateTime("1969-07-21T02:56:15Z"); + + CPPUNIT_ASSERT_EQUAL(std::string("1969-07-21T02:56:15"), boost::posix_time::to_iso_extended_string(time)); + } - public: - void testStringToDateTime_UTC() { - boost::posix_time::ptime time = stringToDateTime("1969-07-21T02:56:15Z"); + void testStringToDateTime_WithTimezone() { + boost::posix_time::ptime time = stringToDateTime("1969-07-20T21:56:15-05:00"); - CPPUNIT_ASSERT_EQUAL(std::string("1969-07-21T02:56:15"), boost::posix_time::to_iso_extended_string(time)); - } + CPPUNIT_ASSERT_EQUAL(std::string("1969-07-21T02:56:15"), boost::posix_time::to_iso_extended_string(time)); + } - void testStringToDateTime_WithTimezone() { - boost::posix_time::ptime time = stringToDateTime("1969-07-20T21:56:15-05:00"); + void testDateTimeToString() { + boost::posix_time::ptime time = stringToDateTime("1969-07-20T21:56:15-05:00"); - CPPUNIT_ASSERT_EQUAL(std::string("1969-07-21T02:56:15"), boost::posix_time::to_iso_extended_string(time)); - } + CPPUNIT_ASSERT_EQUAL(std::string("1969-07-21T02:56:15Z"), dateTimeToString(time)); + } - void testDateTimeToString() { - boost::posix_time::ptime time = stringToDateTime("1969-07-20T21:56:15-05:00"); + void testDateTimeToLocalStringNotThrowingException() { + boost::posix_time::ptime time = stringToDateTime("1954-07-20T21:56:15-05:00"); - CPPUNIT_ASSERT_EQUAL(std::string("1969-07-21T02:56:15Z"), dateTimeToString(time)); - } + CPPUNIT_ASSERT_EQUAL(std::string(""), dateTimeToLocalString(time)); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(DateTimeTest); diff --git a/Swiften/Base/UnitTest/IDGeneratorTest.cpp b/Swiften/Base/UnitTest/IDGeneratorTest.cpp index 610138f..08bd48b 100644 --- a/Swiften/Base/UnitTest/IDGeneratorTest.cpp +++ b/Swiften/Base/UnitTest/IDGeneratorTest.cpp @@ -1,12 +1,13 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ +#include <set> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <set> #include <Swiften/Base/IDGenerator.h> @@ -14,27 +15,27 @@ using namespace Swift; class IDGeneratorTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(IDGeneratorTest); - CPPUNIT_TEST(testGenerate); - CPPUNIT_TEST_SUITE_END(); - - public: - IDGeneratorTest() {} - - void setUp() { - generatedIDs_.clear(); - } - - void testGenerate() { - IDGenerator testling; - for (unsigned int i = 0; i < 26*4; ++i) { - std::string id = testling.generateID(); - CPPUNIT_ASSERT(generatedIDs_.insert(id).second); - } - } - - private: - std::set<std::string> generatedIDs_; + CPPUNIT_TEST_SUITE(IDGeneratorTest); + CPPUNIT_TEST(testGenerate); + CPPUNIT_TEST_SUITE_END(); + + public: + IDGeneratorTest() {} + + void setUp() { + generatedIDs_.clear(); + } + + void testGenerate() { + IDGenerator testling; + for (unsigned int i = 0; i < 26*4; ++i) { + std::string id = testling.generateID(); + CPPUNIT_ASSERT(generatedIDs_.insert(id).second); + } + } + + private: + std::set<std::string> generatedIDs_; }; CPPUNIT_TEST_SUITE_REGISTRATION(IDGeneratorTest); diff --git a/Swiften/Base/UnitTest/LRUCacheTest.cpp b/Swiften/Base/UnitTest/LRUCacheTest.cpp new file mode 100644 index 0000000..7d54c5c --- /dev/null +++ b/Swiften/Base/UnitTest/LRUCacheTest.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (c) 2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <string> + +#include <boost/optional.hpp> + +#include <Swiften/Base/LogSerializers.h> +#include <Swiften/Base/LRUCache.h> + +#include <gtest/gtest.h> // This has to go after Swiften/Base/LogSerializers.h. + +using namespace Swift; +namespace b = boost; + +TEST(LRUCacheTest, testCacheLimit) { + LRUCache<std::string, std::string, 3> testling; + + testling.insert("A", "AA"); + testling.insert("B", "BB"); + testling.insert("C", "CC"); + + ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A")); + ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B")); + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C")); + ASSERT_EQ(b::optional<std::string>(), testling.get("D")); + + testling.insert("D", "DD"); + + ASSERT_EQ(b::optional<std::string>(), testling.get("A")); + ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B")); + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C")); + ASSERT_EQ(b::optional<std::string>("DD"), testling.get("D")); +} + +TEST(LRUCacheTest, testMoveRecentToFrontOnGet) { + LRUCache<std::string, std::string, 3> testling; + + testling.insert("A", "AA"); + testling.insert("B", "BB"); + testling.insert("C", "CC"); + + ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A")); + ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B")); + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C")); + ASSERT_EQ(b::optional<std::string>(), testling.get("D")); + ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A")); + + testling.insert("D", "DD"); + + ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A")); + ASSERT_EQ(b::optional<std::string>(), testling.get("B")); + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C")); + ASSERT_EQ(b::optional<std::string>("DD"), testling.get("D")); +} + +TEST(LRUCacheTest, testMoveRecentToFrontOnReinsert) { + LRUCache<std::string, std::string, 3> testling; + + testling.insert("A", "AA"); + testling.insert("B", "BB"); + testling.insert("C", "CC"); + + ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A")); + ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B")); + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C")); + ASSERT_EQ(b::optional<std::string>(), testling.get("D")); + + testling.insert("B", "BB"); + + ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A")); + ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B")); + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C")); + ASSERT_EQ(b::optional<std::string>(), testling.get("D")); + + testling.insert("D", "DD"); + + ASSERT_EQ(b::optional<std::string>(), testling.get("A")); + ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B")); + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C")); + ASSERT_EQ(b::optional<std::string>("DD"), testling.get("D")); +} + +TEST(LRUCacheTest, testCacheReturnsValuesPreviouslyInserted) { + LRUCache<std::string, std::string, 3> testling; + + testling.insert("A", "AA"); + testling.insert("B", "BB"); + testling.insert("C", "CC"); + + ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A")); + ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B")); + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C")); +} + +TEST(LRUCacheTest, testCacheMissFunctionIsUsedOnCacheMiss) { + LRUCache<std::string, std::string, 3> testling; + + testling.insert("A", "AA"); + testling.insert("B", "BB"); + + ASSERT_EQ(b::optional<std::string>("AA"), testling.get("A")); + ASSERT_EQ(b::optional<std::string>("BB"), testling.get("B")); + + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C", [](const std::string&) { + return boost::optional<std::string>(std::string("CC")); + })); + ASSERT_EQ(b::optional<std::string>("CC"), testling.get("C")); + + ASSERT_EQ(b::optional<std::string>(), testling.get("D", [](const std::string&) { + return boost::optional<std::string>(); + })); + ASSERT_EQ(b::optional<std::string>(), testling.get("D")); +} diff --git a/Swiften/Base/UnitTest/LogTest.cpp b/Swiften/Base/UnitTest/LogTest.cpp new file mode 100644 index 0000000..5d710db --- /dev/null +++ b/Swiften/Base/UnitTest/LogTest.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include <vector> + +#include <boost/algorithm/string/predicate.hpp> + +#include <gtest/gtest.h> + +#include <Swiften/Base/Log.h> + +using namespace Swift; + +struct LogEntry { + LogEntry(Log::Severity severity, std::string file, int line, std::string function, std::string message) : severity(severity), file(std::move(file)), line(line), function(std::move(function)), message(std::move(message)) {} + + Log::Severity severity; + std::string file; + int line; + std::string function; + std::string message; +}; + +// Helper class to set the logging callback. Using this class to set it will ensure the +// logCallback is reset to empty (its default state) after each test. +class LogCallbackSetter { +public: + LogCallbackSetter(Log::Callback callback) { + Log::setLogCallback(callback); + } + ~LogCallbackSetter() { + Log::setLogCallback({}); + } +}; + +TEST(LogTest, testCallback) { + std::vector<LogEntry> logEntries; + LogCallbackSetter callbackSetter = {[&](Log::Severity severity, const std::string& file, int line, const std::string& function, const std::string& message) { + logEntries.emplace_back(severity, file, line, function, message); + }}; + + SWIFT_LOG(error) << "An error"; + ASSERT_EQ(1, logEntries.size()); + ASSERT_EQ(Log::error, logEntries[0].severity); + ASSERT_EQ("An error", logEntries[0].message); +} diff --git a/Swiften/Base/UnitTest/PathTest.cpp b/Swiften/Base/UnitTest/PathTest.cpp index f5f99e7..dd2233b 100644 --- a/Swiften/Base/UnitTest/PathTest.cpp +++ b/Swiften/Base/UnitTest/PathTest.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2013 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2013 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #include <cppunit/extensions/HelperMacros.h> @@ -13,23 +13,23 @@ using namespace Swift; class PathTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(PathTest); - CPPUNIT_TEST(testStringToPath); - CPPUNIT_TEST(testPathToString); - CPPUNIT_TEST_SUITE_END(); + CPPUNIT_TEST_SUITE(PathTest); + CPPUNIT_TEST(testStringToPath); + CPPUNIT_TEST(testPathToString); + CPPUNIT_TEST_SUITE_END(); - public: - void testStringToPath() { + public: + void testStringToPath() { #ifdef SWIFTEN_PLATFORM_WINDOWS - CPPUNIT_ASSERT(std::wstring(L"tron\xe7on") == stringToPath("tron\xc3\xa7on").native()); + CPPUNIT_ASSERT(std::wstring(L"tron\xe7on") == stringToPath("tron\xc3\xa7on").native()); #else - CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), stringToPath("tron\xc3\xa7on").native()); + CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), stringToPath("tron\xc3\xa7on").native()); #endif - } + } - void testPathToString() { - CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), pathToString(stringToPath("tron\xc3\xa7on"))); - } + void testPathToString() { + CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), pathToString(stringToPath("tron\xc3\xa7on"))); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(PathTest); diff --git a/Swiften/Base/UnitTest/SimpleIDGeneratorTest.cpp b/Swiften/Base/UnitTest/SimpleIDGeneratorTest.cpp index 80aff02..9b49c0a 100644 --- a/Swiften/Base/UnitTest/SimpleIDGeneratorTest.cpp +++ b/Swiften/Base/UnitTest/SimpleIDGeneratorTest.cpp @@ -1,40 +1,41 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ +#include <set> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <set> -#include "Swiften/Base/SimpleIDGenerator.h" +#include <Swiften/Base/SimpleIDGenerator.h> using namespace Swift; class SimpleIDGeneratorTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(SimpleIDGeneratorTest); - CPPUNIT_TEST(testGenerate); - CPPUNIT_TEST_SUITE_END(); - - public: - SimpleIDGeneratorTest() {} - - void setUp() { - generatedIDs_.clear(); - } - - void testGenerate() { - SimpleIDGenerator testling; - for (unsigned int i = 0; i < 26*4; ++i) { - std::string id = testling.generateID(); - CPPUNIT_ASSERT(generatedIDs_.insert(id).second); - } - } - - private: - std::set<std::string> generatedIDs_; + CPPUNIT_TEST_SUITE(SimpleIDGeneratorTest); + CPPUNIT_TEST(testGenerate); + CPPUNIT_TEST_SUITE_END(); + + public: + SimpleIDGeneratorTest() {} + + void setUp() { + generatedIDs_.clear(); + } + + void testGenerate() { + SimpleIDGenerator testling; + for (unsigned int i = 0; i < 26*4; ++i) { + std::string id = testling.generateID(); + CPPUNIT_ASSERT(generatedIDs_.insert(id).second); + } + } + + private: + std::set<std::string> generatedIDs_; }; CPPUNIT_TEST_SUITE_REGISTRATION(SimpleIDGeneratorTest); diff --git a/Swiften/Base/UnitTest/StringTest.cpp b/Swiften/Base/UnitTest/StringTest.cpp index ffca98a..889c9c7 100644 --- a/Swiften/Base/UnitTest/StringTest.cpp +++ b/Swiften/Base/UnitTest/StringTest.cpp @@ -1,128 +1,171 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2016 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ +#include <string> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> -#include <string> -#include <Swiften/Base/String.h> #include <Swiften/Base/Platform.h> +#include <Swiften/Base/String.h> + +#include <boost/format.hpp> using namespace Swift; -class StringTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(StringTest); - CPPUNIT_TEST(testGetUnicodeCodePoints); - CPPUNIT_TEST(testGetSplittedAtFirst); - CPPUNIT_TEST(testGetSplittedAtFirst_CharacterAtBegin); - CPPUNIT_TEST(testGetSplittedAtFirst_CharacterAtEnd); - CPPUNIT_TEST(testGetSplittedAtFirst_NoSuchCharacter); - CPPUNIT_TEST(testReplaceAll); - CPPUNIT_TEST(testReplaceAll_LastChar); - CPPUNIT_TEST(testReplaceAll_ConsecutiveChars); - CPPUNIT_TEST(testReplaceAll_MatchingReplace); - CPPUNIT_TEST(testSplit); + class StringTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(StringTest); + CPPUNIT_TEST(testGetUnicodeCodePoints); + CPPUNIT_TEST(testGetSplittedAtFirst); + CPPUNIT_TEST(testGetSplittedAtFirst_CharacterAtBegin); + CPPUNIT_TEST(testGetSplittedAtFirst_CharacterAtEnd); + CPPUNIT_TEST(testGetSplittedAtFirst_NoSuchCharacter); + CPPUNIT_TEST(testReplaceAll); + CPPUNIT_TEST(testReplaceAll_LastChar); + CPPUNIT_TEST(testReplaceAll_ConsecutiveChars); + CPPUNIT_TEST(testReplaceAll_MatchingReplace); + CPPUNIT_TEST(testIsValidXMPPCharacter); + CPPUNIT_TEST(testSanitizeXMPPString); + CPPUNIT_TEST(testSplit); #ifdef SWIFTEN_PLATFORM_WINDOWS - CPPUNIT_TEST(testConvertWStringToString); - CPPUNIT_TEST(testConvertStringToWString); + CPPUNIT_TEST(testConvertWStringToString); + CPPUNIT_TEST(testConvertStringToWString); #endif - CPPUNIT_TEST_SUITE_END(); - - public: - void testGetUnicodeCodePoints() { - std::string testling("$\xc2\xa2\xe2\x82\xac\xf4\x8a\xaf\x8d"); - std::vector<unsigned int> points = String::getUnicodeCodePoints(testling); - - CPPUNIT_ASSERT_EQUAL(0x24U, points[0]); - CPPUNIT_ASSERT_EQUAL(0xA2U, points[1]); - CPPUNIT_ASSERT_EQUAL(0x20ACU, points[2]); - CPPUNIT_ASSERT_EQUAL(0x10ABCDU, points[3]); - } - - void testGetSplittedAtFirst() { - std::string testling("ab@cd@ef"); - - std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, '@'); - CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.first); - CPPUNIT_ASSERT_EQUAL(std::string("cd@ef"), result.second); - } - - void testGetSplittedAtFirst_CharacterAtBegin() { - std::string testling(" ab"); - - std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, ' '); - CPPUNIT_ASSERT(result.first.empty()); - CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.second); - } - - void testGetSplittedAtFirst_CharacterAtEnd() { - std::string testling("ab@"); - - std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, '@'); - CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.first); - CPPUNIT_ASSERT(result.second.empty()); - } - - void testGetSplittedAtFirst_NoSuchCharacter() { - std::string testling("ab"); - - std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, '@'); - CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.first); - CPPUNIT_ASSERT(result.second.empty()); - } - - void testReplaceAll() { - std::string testling("abcbd"); - - String::replaceAll(testling, 'b', "xyz"); - - CPPUNIT_ASSERT_EQUAL(std::string("axyzcxyzd"), testling); - } - - void testReplaceAll_LastChar() { - std::string testling("abc"); - - String::replaceAll(testling, 'c', "xyz"); - - CPPUNIT_ASSERT_EQUAL(std::string("abxyz"), testling); - } - - void testReplaceAll_ConsecutiveChars() { - std::string testling("abbc"); - - String::replaceAll(testling, 'b',"xyz"); - - CPPUNIT_ASSERT_EQUAL(std::string("axyzxyzc"), testling); - } - - void testReplaceAll_MatchingReplace() { - std::string testling("abc"); - - String::replaceAll(testling, 'b',"bbb"); - - CPPUNIT_ASSERT_EQUAL(std::string("abbbc"), testling); - } - - void testSplit() { - std::vector<std::string> result = String::split("abc def ghi", ' '); + CPPUNIT_TEST_SUITE_END(); - CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(result.size())); - CPPUNIT_ASSERT_EQUAL(std::string("abc"), result[0]); - CPPUNIT_ASSERT_EQUAL(std::string("def"), result[1]); - CPPUNIT_ASSERT_EQUAL(std::string("ghi"), result[2]); - } + public: + void testGetUnicodeCodePoints() { + std::string testling("$\xc2\xa2\xe2\x82\xac\xf4\x8a\xaf\x8d"); + std::vector<unsigned int> points = String::getUnicodeCodePoints(testling); + + CPPUNIT_ASSERT_EQUAL(0x24U, points[0]); + CPPUNIT_ASSERT_EQUAL(0xA2U, points[1]); + CPPUNIT_ASSERT_EQUAL(0x20ACU, points[2]); + CPPUNIT_ASSERT_EQUAL(0x10ABCDU, points[3]); + } + + void testGetSplittedAtFirst() { + std::string testling("ab@cd@ef"); + + std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, '@'); + CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.first); + CPPUNIT_ASSERT_EQUAL(std::string("cd@ef"), result.second); + } + + void testGetSplittedAtFirst_CharacterAtBegin() { + std::string testling(" ab"); + + std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, ' '); + CPPUNIT_ASSERT(result.first.empty()); + CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.second); + } + + void testGetSplittedAtFirst_CharacterAtEnd() { + std::string testling("ab@"); + + std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, '@'); + CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.first); + CPPUNIT_ASSERT(result.second.empty()); + } + + void testGetSplittedAtFirst_NoSuchCharacter() { + std::string testling("ab"); + + std::pair<std::string,std::string> result = String::getSplittedAtFirst(testling, '@'); + CPPUNIT_ASSERT_EQUAL(std::string("ab"), result.first); + CPPUNIT_ASSERT(result.second.empty()); + } + + void testReplaceAll() { + std::string testling("abcbd"); + + String::replaceAll(testling, 'b', "xyz"); + + CPPUNIT_ASSERT_EQUAL(std::string("axyzcxyzd"), testling); + } + + void testReplaceAll_LastChar() { + std::string testling("abc"); + + String::replaceAll(testling, 'c', "xyz"); + + CPPUNIT_ASSERT_EQUAL(std::string("abxyz"), testling); + } + + void testReplaceAll_ConsecutiveChars() { + std::string testling("abbc"); + + String::replaceAll(testling, 'b',"xyz"); + + CPPUNIT_ASSERT_EQUAL(std::string("axyzxyzc"), testling); + } + + void testReplaceAll_MatchingReplace() { + std::string testling("abc"); + + String::replaceAll(testling, 'b',"bbb"); + + CPPUNIT_ASSERT_EQUAL(std::string("abbbc"), testling); + } + + void testIsValidXMPPCharacter() { + const std::uint32_t testCharacters[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, + 0x20, 0x7F, 0x80, 0x84, 0x85, 0xFF }; + const auto testLength = sizeof(testCharacters) / sizeof(std::uint32_t); + const bool expectedValid[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 1, 1 }; + static_assert(testLength == sizeof(expectedValid), "size of test data must match"); + + for (std::size_t i = 0; i != testLength; ++i) { + const auto c = testCharacters[i]; + CPPUNIT_ASSERT_EQUAL_MESSAGE(boost::str(boost::format("While testing at idx=%d: 0x%02x") % i % c), expectedValid[i], String::isValidXMPPCharacter(c)); + } + } + + void testSanitizeXMPPString() { + std::vector<std::pair<std::string, std::string>> testData = { + { "\0", "" }, + { std::string("\0\t", 3), "\t" }, + { "", "" }, + { std::string("\0", 1) , std::string() }, + { std::string("\0blah\0", 6) , std::string("blah", 4) }, + { "z\xC3\x9F\xE6\xB0\xB4\xF0\x9D\x84\x8B" , "z\xC3\x9F\xE6\xB0\xB4\xF0\x9D\x84\x8B" }, // or in u8 notation: u8"z\u00df\u6c34\U0001d10b" + { "\x7FT\t\x0c\xff\xfeT", "T\tT" }, + { "\x01Q\x0BW\x81T", "QWT" }, + { "\xF0\x9F\x98\x83" "ABC" "\xE2\xBE\xA6", "\xF0\x9F\x98\x83" "ABC" "\xE2\xBE\xA6" } + }; + + for (std::size_t i = 0; i != testData.size(); ++i) { + const auto & t = testData[i]; + const auto actual = String::sanitizeXMPPString(t.first); + CPPUNIT_ASSERT_EQUAL_MESSAGE(boost::str(boost::format("While testing string idx=%d") % i), std::string(actual), t.second); + } + } + + void testSplit() { + std::vector<std::string> result = String::split("abc def ghi", ' '); + + CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(result.size())); + CPPUNIT_ASSERT_EQUAL(std::string("abc"), result[0]); + CPPUNIT_ASSERT_EQUAL(std::string("def"), result[1]); + CPPUNIT_ASSERT_EQUAL(std::string("ghi"), result[2]); + } #ifdef SWIFTEN_PLATFORM_WINDOWS - void testConvertWStringToString() { - CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), convertWStringToString(std::wstring(L"tron\xe7on"))); - } + void testConvertWStringToString() { + CPPUNIT_ASSERT_EQUAL(std::string("tron\xc3\xa7on"), convertWStringToString(std::wstring(L"tron\xe7on"))); + } - void testConvertStringToWString() { - CPPUNIT_ASSERT(std::wstring(L"tron\xe7on") == convertStringToWString(std::string("tron\xc3\xa7on"))); - } + void testConvertStringToWString() { + CPPUNIT_ASSERT(std::wstring(L"tron\xe7on") == convertStringToWString(std::string("tron\xc3\xa7on"))); + } #endif }; diff --git a/Swiften/Base/UnitTest/URLTest.cpp b/Swiften/Base/UnitTest/URLTest.cpp index e82321f..da9f15c 100644 --- a/Swiften/Base/UnitTest/URLTest.cpp +++ b/Swiften/Base/UnitTest/URLTest.cpp @@ -1,114 +1,238 @@ /* - * Copyright (c) 2012 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2012-2018 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ +#include <boost/lexical_cast.hpp> + #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> #include <Swiften/Base/URL.h> -#include <boost/lexical_cast.hpp> using namespace Swift; class URLTest : public CppUnit::TestFixture { - CPPUNIT_TEST_SUITE(URLTest); - CPPUNIT_TEST(testFromString); - CPPUNIT_TEST(testFromString_WithoutPath); - CPPUNIT_TEST(testFromString_WithRootPath); - CPPUNIT_TEST(testFromString_WithPort); - CPPUNIT_TEST(testFromString_WithPortOnePartPath); - CPPUNIT_TEST(testFromString_WithPortWithoutPath); - CPPUNIT_TEST(testFromString_WithUserInfo); - CPPUNIT_TEST(testFromString_NonASCIIHost); - CPPUNIT_TEST(testFromString_NonASCIIPath); - CPPUNIT_TEST(testToString); - CPPUNIT_TEST(testToString_WithPort); - CPPUNIT_TEST_SUITE_END(); - - public: - void testFromString() { - URL url = URL::fromString("http://foo.bar/baz/bam"); - - CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); - CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT(!url.getPort()); - CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); - } - - void testFromString_WithoutPath() { - URL url = URL::fromString("http://foo.bar"); - - CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); - CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT(!url.getPort()); - CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); - } - - void testFromString_WithRootPath() { - URL url = URL::fromString("http://foo.bar/"); - - CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); - CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT(!url.getPort()); - CPPUNIT_ASSERT_EQUAL(std::string("/"), url.getPath()); - } - - void testFromString_WithPort() { - URL url = URL::fromString("http://foo.bar:1234/baz/bam"); - - CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); - CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(1234, *url.getPort()); - CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); - } - - void testFromString_WithPortOnePartPath() { - URL url = URL::fromString("http://foo.bar:11440/http-bind/"); - - CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); - CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(11440, *url.getPort()); - CPPUNIT_ASSERT_EQUAL(std::string("/http-bind/"), url.getPath()); - } - - void testFromString_WithPortWithoutPath() { - URL url = URL::fromString("http://foo.bar:1234"); - - CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); - CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(1234, *url.getPort()); - CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); - } - - void testFromString_WithUserInfo() { - URL url = URL::fromString("http://user:pass@foo.bar/baz/bam"); - - CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); - CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); - CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); - } - - void testFromString_NonASCIIHost() { - URL url = URL::fromString("http://www.tron%C3%A7on.be/baz/bam"); - - CPPUNIT_ASSERT_EQUAL(std::string("www.tron\xc3\xa7on.be"), url.getHost()); - } - - void testFromString_NonASCIIPath() { - URL url = URL::fromString("http://foo.bar/baz/tron%C3%A7on/bam"); - - CPPUNIT_ASSERT_EQUAL(std::string("/baz/tron\xc3\xa7on/bam"), url.getPath()); - } - - void testToString() { - CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar/baz/bam"), URL("http", "foo.bar", "/baz/bam").toString()); - } - - void testToString_WithPort() { - CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar:1234/baz/bam"), URL("http", "foo.bar", 1234, "/baz/bam").toString()); - } + CPPUNIT_TEST_SUITE(URLTest); + CPPUNIT_TEST(testFromString); + CPPUNIT_TEST(testFromString_WithoutPath); + CPPUNIT_TEST(testFromString_WithRootPath); + CPPUNIT_TEST(testFromString_WithPort); + CPPUNIT_TEST(testFromString_WithPortOnePartPath); + CPPUNIT_TEST(testFromString_WithPortWithoutPath); + CPPUNIT_TEST(testFromString_WithUserInfo); + CPPUNIT_TEST(testFromString_NonASCIIHost); + CPPUNIT_TEST(testFromString_NonASCIIPath); + CPPUNIT_TEST(testFromString_IPv4Address); + CPPUNIT_TEST(testFromString_IPv4AddressWithPort); + CPPUNIT_TEST(testFromString_IPv6Address); + CPPUNIT_TEST(testFromString_IPv6AddressWithPort); + CPPUNIT_TEST(testToString); + CPPUNIT_TEST(testToString_WithPort); + CPPUNIT_TEST(test_FromString_ToString_IPv6RFC2732); + CPPUNIT_TEST_SUITE_END(); + + public: + void testFromString() { + URL url = URL::fromString("http://foo.bar/baz/bam"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); + CPPUNIT_ASSERT(!url.getPort()); + CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); + } + + void testFromString_WithoutPath() { + URL url = URL::fromString("http://foo.bar"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); + CPPUNIT_ASSERT(!url.getPort()); + CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); + } + + void testFromString_WithRootPath() { + URL url = URL::fromString("http://foo.bar/"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); + CPPUNIT_ASSERT(!url.getPort()); + CPPUNIT_ASSERT_EQUAL(std::string("/"), url.getPath()); + } + + void testFromString_WithPort() { + URL url = URL::fromString("http://foo.bar:1234/baz/bam"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(1234), *url.getPort()); + CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); + } + + void testFromString_WithPortOnePartPath() { + URL url = URL::fromString("http://foo.bar:11440/http-bind/"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(11440), *url.getPort()); + CPPUNIT_ASSERT_EQUAL(std::string("/http-bind/"), url.getPath()); + } + + void testFromString_WithPortWithoutPath() { + URL url = URL::fromString("http://foo.bar:1234"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(1234), *url.getPort()); + CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); + } + + void testFromString_WithUserInfo() { + URL url = URL::fromString("http://user:pass@foo.bar/baz/bam"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("foo.bar"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(std::string("/baz/bam"), url.getPath()); + } + + void testFromString_NonASCIIHost() { + URL url = URL::fromString("http://www.tron%C3%A7on.be/baz/bam"); + + CPPUNIT_ASSERT_EQUAL(std::string("www.tron\xc3\xa7on.be"), url.getHost()); + } + + void testFromString_NonASCIIPath() { + URL url = URL::fromString("http://foo.bar/baz/tron%C3%A7on/bam"); + + CPPUNIT_ASSERT_EQUAL(std::string("/baz/tron\xc3\xa7on/bam"), url.getPath()); + } + + void testFromString_IPv4Address() { + URL url = URL::fromString("http://127.0.0.1/foobar"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(std::string("/foobar"), url.getPath()); + } + + void testFromString_IPv4AddressWithPort() { + URL url = URL::fromString("http://127.0.0.1:12345/foobar"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("127.0.0.1"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(12345), url.getPort().get_value_or(0)); + CPPUNIT_ASSERT_EQUAL(std::string("/foobar"), url.getPath()); + } + + void testFromString_IPv6Address() { + URL url = URL::fromString("http://[fdf8:f53b:82e4::53]/foobar"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("fdf8:f53b:82e4::53"), url.getHost()); + } + + void testFromString_IPv6AddressWithPort() { + URL url = URL::fromString("http://[fdf8:f53b:82e4::53]:12435/foobar"); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("fdf8:f53b:82e4::53"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(12435), url.getPort().get_value_or(0)); + } + + void test_FromString_ToString_IPv6RFC2732() { + { + const char* testVector = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html"; + URL url = URL::fromString(testVector); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(80), url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); + + CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); + } + + { + const char* testVector = "http://[1080:0:0:0:8:800:200C:417A]/index.html"; + URL url = URL::fromString(testVector); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("1080:0:0:0:8:800:200C:417A"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); + + CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); + } + + { + const char* testVector = "http://[3ffe:2a00:100:7031::1]"; + URL url = URL::fromString(testVector); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("3ffe:2a00:100:7031::1"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(std::string(""), url.getPath()); + + CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); + } + + { + const char* testVector = "http://[1080::8:800:200C:417A]/foo"; + URL url = URL::fromString(testVector); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("1080::8:800:200C:417A"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(std::string("/foo"), url.getPath()); + + CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); + } + + { + const char* testVector = "http://[::192.9.5.5]/ipng"; + URL url = URL::fromString(testVector); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("::192.9.5.5"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(std::string("/ipng"), url.getPath()); + + CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); + } + + { + const char* testVector = "http://[::FFFF:129.144.52.38]:80/index.html"; + URL url = URL::fromString(testVector); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("::FFFF:129.144.52.38"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(80), url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(std::string("/index.html"), url.getPath()); + + CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); + } + + { + const char* testVector = "http://[2010:836B:4179::836B:4179]"; + URL url = URL::fromString(testVector); + + CPPUNIT_ASSERT_EQUAL(std::string("http"), url.getScheme()); + CPPUNIT_ASSERT_EQUAL(std::string("2010:836B:4179::836B:4179"), url.getHost()); + CPPUNIT_ASSERT_EQUAL(static_cast<unsigned short>(2), url.getPort().get_value_or(2)); + CPPUNIT_ASSERT_EQUAL(std::string(), url.getPath()); + + CPPUNIT_ASSERT_EQUAL(std::string(testVector), url.toString()); + } + } + + void testToString() { + CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar/baz/bam"), URL("http", "foo.bar", "/baz/bam").toString()); + } + + void testToString_WithPort() { + CPPUNIT_ASSERT_EQUAL(std::string("http://foo.bar:1234/baz/bam"), URL("http", "foo.bar", 1234, "/baz/bam").toString()); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(URLTest); |