diff options
author | Wojtek Kosior <wk@koszkonutek-tmp.pl.eu.org> | 2021-04-30 00:33:56 +0200 |
---|---|---|
committer | Wojtek Kosior <wk@koszkonutek-tmp.pl.eu.org> | 2021-04-30 00:33:56 +0200 |
commit | aa4d426b4d3527d7e166df1a05058c9a4a0f6683 (patch) | |
tree | 4ff17ce8b89a2321b9d0ed4bcfc37c447bcb6820 /vmime-master/tests/utility | |
download | smtps-and-pop3s-console-program-master.tar.gz smtps-and-pop3s-console-program-master.zip |
Diffstat (limited to 'vmime-master/tests/utility')
-rw-r--r-- | vmime-master/tests/utility/datetimeUtilsTest.cpp | 157 | ||||
-rw-r--r-- | vmime-master/tests/utility/encoder/b64EncoderTest.cpp | 168 | ||||
-rw-r--r-- | vmime-master/tests/utility/encoder/encoderFactoryTest.cpp | 63 | ||||
-rw-r--r-- | vmime-master/tests/utility/encoder/encoderTestUtils.hpp | 82 | ||||
-rw-r--r-- | vmime-master/tests/utility/encoder/qpEncoderTest.cpp | 275 | ||||
-rw-r--r-- | vmime-master/tests/utility/filteredStreamTest.cpp | 341 | ||||
-rw-r--r-- | vmime-master/tests/utility/outputStreamByteArrayAdapterTest.cpp | 82 | ||||
-rw-r--r-- | vmime-master/tests/utility/outputStreamSocketAdapterTest.cpp | 87 | ||||
-rw-r--r-- | vmime-master/tests/utility/outputStreamStringAdapterTest.cpp | 84 | ||||
-rw-r--r-- | vmime-master/tests/utility/parserInputStreamAdapterTest.cpp | 51 | ||||
-rw-r--r-- | vmime-master/tests/utility/pathTest.cpp | 356 | ||||
-rw-r--r-- | vmime-master/tests/utility/seekableInputStreamRegionAdapterTest.cpp | 176 | ||||
-rw-r--r-- | vmime-master/tests/utility/stringUtilsTest.cpp | 214 | ||||
-rw-r--r-- | vmime-master/tests/utility/urlTest.cpp | 312 |
14 files changed, 2448 insertions, 0 deletions
diff --git a/vmime-master/tests/utility/datetimeUtilsTest.cpp b/vmime-master/tests/utility/datetimeUtilsTest.cpp new file mode 100644 index 0000000..77ce242 --- /dev/null +++ b/vmime-master/tests/utility/datetimeUtilsTest.cpp @@ -0,0 +1,157 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/dateTime.hpp" +#include "vmime/utility/datetimeUtils.hpp" + + +VMIME_TEST_SUITE_BEGIN(datetimeUtilsTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testIsLeapYear) + VMIME_TEST(testGetDaysInMonth) + VMIME_TEST(testGetDaysInMonthLeapYear) + VMIME_TEST(testToUniversalTime) + VMIME_TEST(testToLocalTime) + VMIME_TEST(testGetDayOfWeek) + VMIME_TEST(testGetWeekOfYear) + VMIME_TEST_LIST_END + + + typedef vmime::utility::datetimeUtils datetimeUtils; + + + void testIsLeapYear() { + + VASSERT_EQ("1", false, datetimeUtils::isLeapYear(1999)); + VASSERT_EQ("2", false, datetimeUtils::isLeapYear(1800)); + VASSERT_EQ("3", false, datetimeUtils::isLeapYear(1900)); + VASSERT_EQ("4", false, datetimeUtils::isLeapYear(2100)); + VASSERT_EQ("5", false, datetimeUtils::isLeapYear(2200)); + + VASSERT_EQ("6", true, datetimeUtils::isLeapYear(1996)); + VASSERT_EQ("7", true, datetimeUtils::isLeapYear(2000)); + } + + void testGetDaysInMonth() { + + VASSERT_EQ("1", 31, datetimeUtils::getDaysInMonth(2006, 1)); + VASSERT_EQ("2", 28, datetimeUtils::getDaysInMonth(2006, 2)); + VASSERT_EQ("3", 31, datetimeUtils::getDaysInMonth(2006, 3)); + VASSERT_EQ("4", 30, datetimeUtils::getDaysInMonth(2006, 4)); + VASSERT_EQ("5", 31, datetimeUtils::getDaysInMonth(2006, 5)); + VASSERT_EQ("6", 30, datetimeUtils::getDaysInMonth(2006, 6)); + VASSERT_EQ("7", 31, datetimeUtils::getDaysInMonth(2006, 7)); + VASSERT_EQ("8", 31, datetimeUtils::getDaysInMonth(2006, 8)); + VASSERT_EQ("9", 30, datetimeUtils::getDaysInMonth(2006, 9)); + VASSERT_EQ("10", 31, datetimeUtils::getDaysInMonth(2006, 10)); + VASSERT_EQ("11", 30, datetimeUtils::getDaysInMonth(2006, 11)); + VASSERT_EQ("12", 31, datetimeUtils::getDaysInMonth(2006, 12)); + } + + void testGetDaysInMonthLeapYear() { + + VASSERT_EQ("1", 31, datetimeUtils::getDaysInMonth(2004, 1)); + VASSERT_EQ("2", 29, datetimeUtils::getDaysInMonth(2004, 2)); + VASSERT_EQ("3", 31, datetimeUtils::getDaysInMonth(2004, 3)); + VASSERT_EQ("4", 30, datetimeUtils::getDaysInMonth(2004, 4)); + VASSERT_EQ("5", 31, datetimeUtils::getDaysInMonth(2004, 5)); + VASSERT_EQ("6", 30, datetimeUtils::getDaysInMonth(2004, 6)); + VASSERT_EQ("7", 31, datetimeUtils::getDaysInMonth(2004, 7)); + VASSERT_EQ("8", 31, datetimeUtils::getDaysInMonth(2004, 8)); + VASSERT_EQ("9", 30, datetimeUtils::getDaysInMonth(2004, 9)); + VASSERT_EQ("10", 31, datetimeUtils::getDaysInMonth(2004, 10)); + VASSERT_EQ("11", 30, datetimeUtils::getDaysInMonth(2004, 11)); + VASSERT_EQ("12", 31, datetimeUtils::getDaysInMonth(2004, 12)); + } + + void testToUniversalTime() { + + const vmime::datetime local(2005, 12, 2, 12, 34, 56, -789); + + const vmime::datetime gmt = datetimeUtils::toUniversalTime(local); + + // 789 is 13 hours, 9 minutes later + VASSERT_EQ("1", 2005, gmt.getYear()); + VASSERT_EQ("2", 12, gmt.getMonth()); + VASSERT_EQ("3", 3, gmt.getDay()); + VASSERT_EQ("4", 1, gmt.getHour()); + VASSERT_EQ("5", 43, gmt.getMinute()); + VASSERT_EQ("6", 56, gmt.getSecond()); + VASSERT_EQ("7", 0, gmt.getZone()); + } + + void testToLocalTime() { + + const vmime::datetime date(2005, 12, 2, 12, 34, 56, -120); // GMT-2 + + const vmime::datetime local = datetimeUtils::toLocalTime(date, 120); // GMT+2 + + VASSERT_EQ("1", 2005, local.getYear()); + VASSERT_EQ("2", 12, local.getMonth()); + VASSERT_EQ("3", 2, local.getDay()); + VASSERT_EQ("4", 16, local.getHour()); + VASSERT_EQ("5", 34, local.getMinute()); + VASSERT_EQ("6", 56, local.getSecond()); + VASSERT_EQ("7", 120, local.getZone()); + } + + void testGetDayOfWeek() { + + VASSERT_EQ("1", vmime::datetime::WEDNESDAY, datetimeUtils::getDayOfWeek(1969, 12, 31)); + VASSERT_EQ("2", vmime::datetime::FRIDAY, datetimeUtils::getDayOfWeek(1976, 4, 9)); + VASSERT_EQ("3", vmime::datetime::TUESDAY, datetimeUtils::getDayOfWeek(1987, 6, 23)); + VASSERT_EQ("4", vmime::datetime::SATURDAY, datetimeUtils::getDayOfWeek(1990, 1, 13)); + VASSERT_EQ("5", vmime::datetime::MONDAY, datetimeUtils::getDayOfWeek(1999, 9, 20)); + VASSERT_EQ("6", vmime::datetime::THURSDAY, datetimeUtils::getDayOfWeek(2003, 2, 27)); + VASSERT_EQ("7", vmime::datetime::SATURDAY, datetimeUtils::getDayOfWeek(2005, 11, 19)); + VASSERT_EQ("8", vmime::datetime::WEDNESDAY, datetimeUtils::getDayOfWeek(2012, 5, 16)); + VASSERT_EQ("9", vmime::datetime::FRIDAY, datetimeUtils::getDayOfWeek(2027, 3, 12)); + } + + void testGetWeekOfYear() { + + VASSERT_EQ("1.1", 52, datetimeUtils::getWeekOfYear(2003, 12, 27)); + VASSERT_EQ("1.2", 52, datetimeUtils::getWeekOfYear(2003, 12, 28)); + VASSERT_EQ("1.3", 1, datetimeUtils::getWeekOfYear(2003, 12, 29, true)); + VASSERT_EQ("1.4", 53, datetimeUtils::getWeekOfYear(2003, 12, 29, false)); + VASSERT_EQ("1.5", 1, datetimeUtils::getWeekOfYear(2004, 1, 4)); + VASSERT_EQ("1.6", 2, datetimeUtils::getWeekOfYear(2004, 1, 5)); + VASSERT_EQ("1.7", 2, datetimeUtils::getWeekOfYear(2004, 1, 11)); + + VASSERT_EQ("2.1", 52, datetimeUtils::getWeekOfYear(2004, 12, 26)); + VASSERT_EQ("2.2", 53, datetimeUtils::getWeekOfYear(2004, 12, 27)); + VASSERT_EQ("2.3", 53, datetimeUtils::getWeekOfYear(2005, 1, 2)); + VASSERT_EQ("2.4", 1, datetimeUtils::getWeekOfYear(2005, 1, 3)); + VASSERT_EQ("2.5", 1, datetimeUtils::getWeekOfYear(2005, 1, 4)); + VASSERT_EQ("2.6", 2, datetimeUtils::getWeekOfYear(2005, 1, 11)); + + VASSERT_EQ("3.1", 9, datetimeUtils::getWeekOfYear(2027, 3, 7)); + VASSERT_EQ("3.2", 10, datetimeUtils::getWeekOfYear(2027, 3, 8)); + VASSERT_EQ("3.3", 10, datetimeUtils::getWeekOfYear(2027, 3, 14)); + VASSERT_EQ("3.4", 11, datetimeUtils::getWeekOfYear(2027, 3, 15)); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/encoder/b64EncoderTest.cpp b/vmime-master/tests/utility/encoder/b64EncoderTest.cpp new file mode 100644 index 0000000..36fa61a --- /dev/null +++ b/vmime-master/tests/utility/encoder/b64EncoderTest.cpp @@ -0,0 +1,168 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "encoderTestUtils.hpp" + + +VMIME_TEST_SUITE_BEGIN(b64EncoderTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testBase64) + VMIME_TEST_LIST_END + + + void testBase64() { + + static const vmime::string testSuites[] = { + + // Test 1 + "", + + "", + + // Test 2 + "A", + + "QQ==", + + // Test 3 + "AB", + + "QUI=", + + // Test 4 + "ABC", + + "QUJD", + + // Test 5 + "foo", + + "Zm9v", + + // Test 6 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + + "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAx" + "MjM0NTY3ODk=", + + // Test 7 + vmime::string( + "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" + "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" + "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" + "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" + "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" + "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" + "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" + "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" + "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" + "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" + "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" + "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" + "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" + "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" + "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" + "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", + 256), + + "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1" + "Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWpr" + "bG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6Ch" + "oqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX" + "2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==" + }; + + + for (unsigned int i = 0 ; i < sizeof(testSuites) / sizeof(testSuites[0]) / 2 ; ++i) { + + const vmime::string decoded = testSuites[i * 2]; + const vmime::string encoded = testSuites[i * 2 + 1]; + + std::ostringstream oss; + oss << "[Base64] Test " << (i + 1) << ": "; + + // Encoding + VASSERT_EQ(oss.str() + "encoding", encoded, encode("base64", decoded)); + + // Decoding + VASSERT_EQ(oss.str() + "decoding", decoded, decode("base64", encoded)); + + // Multiple and successive encoding/decoding + VASSERT_EQ( + oss.str() + "multiple1", + decoded, + decode("base64", + encode("base64", decoded)) + ); + + VASSERT_EQ( + oss.str() + "multiple2", + decoded, + decode("base64", + decode("base64", + encode("base64", + encode("base64", decoded)))) + ); + + VASSERT_EQ( + oss.str() + "multiple3", + decoded, + decode("base64", + decode("base64", + decode("base64", + encode("base64", + encode("base64", + encode("base64", decoded)))))) + ); + + VASSERT_EQ( + oss.str() + "multiple4", + decoded, + decode("base64", + decode("base64", + decode("base64", + decode("base64", + encode("base64", + encode("base64", + encode("base64", + encode("base64", decoded)))))))) + ); + + VASSERT( + oss.str() + "encoded size", + getEncoder("base64")->getEncodedSize(decoded.length()) + >= encode("base64", decoded).length() + ); + + VASSERT( + oss.str() + "decoded size", + getEncoder("base64")->getDecodedSize(encoded.length()) + >= decode("base64", encoded).length() + ); + } + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/encoder/encoderFactoryTest.cpp b/vmime-master/tests/utility/encoder/encoderFactoryTest.cpp new file mode 100644 index 0000000..916706c --- /dev/null +++ b/vmime-master/tests/utility/encoder/encoderFactoryTest.cpp @@ -0,0 +1,63 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/encoder/noopEncoder.hpp" + + + +VMIME_TEST_SUITE_BEGIN(encoderFactoryTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testNoDefaultEncoder) + VMIME_TEST(testDefaultEncoder) + VMIME_TEST_LIST_END + + + void testNoDefaultEncoder() { + + vmime::shared_ptr <vmime::utility::encoder::encoderFactory> ef = + vmime::utility::encoder::encoderFactory::getInstance(); + + VASSERT_THROW( + "no default encoder", + ef->create("non-existing-encoding"), + vmime::exceptions::no_encoder_available + ); + } + + void testDefaultEncoder() { + + vmime::shared_ptr <vmime::utility::encoder::encoderFactory> ef = + vmime::utility::encoder::encoderFactory::getInstance(); + + ef->setDefaultEncoder(vmime::make_shared <vmime::utility::encoder::noopEncoder>()); + + VASSERT_NO_THROW( + "default encoder", + ef->create("non-existing-encoding") + ); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/encoder/encoderTestUtils.hpp b/vmime-master/tests/utility/encoder/encoderTestUtils.hpp new file mode 100644 index 0000000..cc1141c --- /dev/null +++ b/vmime-master/tests/utility/encoder/encoderTestUtils.hpp @@ -0,0 +1,82 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + + +// Helper function to obtain an encoder given its name +static vmime::shared_ptr <vmime::utility::encoder::encoder> getEncoder( + const vmime::string& name, + int maxLineLength = 0, + const vmime::propertySet props = vmime::propertySet() +) { + + vmime::shared_ptr <vmime::utility::encoder::encoder> enc = + vmime::utility::encoder::encoderFactory::getInstance()->create(name); + + enc->getProperties() = props; + + if (maxLineLength != 0) { + enc->getProperties()["maxlinelength"] = maxLineLength; + } + + return enc; +} + + +// Encoding helper function +static const vmime::string encode( + const vmime::string& name, const vmime::string& in, + int maxLineLength = 0, + const vmime::propertySet props = vmime::propertySet() +) { + + vmime::shared_ptr <vmime::utility::encoder::encoder> enc = getEncoder(name, maxLineLength, props); + + vmime::utility::inputStreamStringAdapter vin(in); + + std::ostringstream out; + vmime::utility::outputStreamAdapter vout(out); + + enc->encode(vin, vout); + + return (out.str()); +} + + +// Decoding helper function +static const vmime::string decode( + const vmime::string& name, + const vmime::string& in, + int maxLineLength = 0 +) { + + vmime::shared_ptr <vmime::utility::encoder::encoder> enc = getEncoder(name, maxLineLength); + + vmime::utility::inputStreamStringAdapter vin(in); + + std::ostringstream out; + vmime::utility::outputStreamAdapter vout(out); + + enc->decode(vin, vout); + + return (out.str()); +} diff --git a/vmime-master/tests/utility/encoder/qpEncoderTest.cpp b/vmime-master/tests/utility/encoder/qpEncoderTest.cpp new file mode 100644 index 0000000..e476947 --- /dev/null +++ b/vmime-master/tests/utility/encoder/qpEncoderTest.cpp @@ -0,0 +1,275 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "encoderTestUtils.hpp" + + +VMIME_TEST_SUITE_BEGIN(qpEncoderTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testQuotedPrintable) + VMIME_TEST(testQuotedPrintable_SoftLineBreaks) + VMIME_TEST(testQuotedPrintable_HardLineBreakEncode) + VMIME_TEST(testQuotedPrintable_HardLineBreakDecode) + VMIME_TEST(testQuotedPrintable_CRLF) + VMIME_TEST(testQuotedPrintable_RFC2047) + VMIME_TEST_LIST_END + + + void testQuotedPrintable() { + + static const vmime::string testSuites[] = { + + // Test 1 + "", + + "", + + // Test 2 + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", + + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", + + // Test 3 + "0123456789012345678901234567890123456789012345678901234567890123456789012" + "3456789012345678901234567890123456789012345678901234567890123456789012345" + "6789", + + "0123456789012345678901234567890123456789012345678901234567890123456789012=\r\n" + "3456789012345678901234567890123456789012345678901234567890123456789012345=\r\n" + "6789", + + // Test 4 + vmime::string( + "\x89\xe8\x24\x04\x2f\xe8\xff\xfb\xeb\xff\x90\xd7\x74\x8d\x00\x26\x89\x55" + "\x83\xe5\x08\xec\x04\xc7\xa0\x24\x05\xa2\xe8\x08\x43\xee\x00\x00\xc0\x85" + "\x0a\x74\xec\x89\xc3\x5d\xb6\x8d\x00\x00\x00\x00\x04\xc7\xa8\x24\x05\xa2" + "\xe8\x08\x43\xd4\x00\x00\xe8\xeb\xf6\x89\x89\x55\x81\xe5\xa8\xec\x00\x00" + "\x89\x00\xfc\x75\x75\x8b\x89\x08\xf8\x5d\xb9\xe8\xff\xff\x83\xff\x14\xfe" + "\x47\x74\xc0\x31\x9d\x8d\xff\x68\xff\xff\x85\x89\xff\x68\xff\xff\x85\x8d" + "\xff\x6c\xff\xff\x04\x89\xe8\x24\xfa\x50\xff\xff\x45\xc7\x00\xec\x00\x00" + "\x31\x00\x89\xc0\x24\x44\x89\x08\x24\x5c\x89\x04\x24\x34\x87\xe8\xff\xf6" + "\x89\xff\x24\x34\x2f\xe8\xff\xf9\x8b\xff\xf8\x5d\x75\x8b\x89\xfc\x5d\xec" + "\xbe\xc3\x00\x13\x00\x00\xe7\xeb\xb6\x8d\x00\x00\x00\x00\x89\x55\x57\xe5" + "\x53\x56\xec\x81\x01\xdc\x00\x00\x45\xbb\x05\x5c\x8b\x08\x0c\x55\xe4\x83" + "\x8b\xf0\x89\x02\x24\x5c\xc7\x04\x24\x04\x00\x06\x00\x00\xec\xa3\x05\xa9" + "\xe8\x08\xf7\x2a\xff\xff\x04\xc7\x46\x24\x05\x5c\xb9\x08\x5c\x50\x08\x05" + "\x4c\x89\x04\x24\xf5\xe8\xff\xf7\xc7\xff\x24\x04\x5c\x46\x08\x05\xe9\xe8" + "\xff\xf8\xc7\xff\x24\x04\x1d\x70\x08\x05\x55\xe8\x00\xbb\xb8\x00\x00\x01" + "\x00\x00\xd2\x31\x08\xa3\x05\xa7\xb8\x08\x00\x01\x00\x00\x0c\xa3\x05\xa7", + 18 * 16), + + "=89=E8$=04/=E8=FF=FB=EB=FF=90=D7t=8D=00&=89U=83=E5=08=EC=04=C7=A0$=05=A2=E8=\r\n" + "=08C=EE=00=00=C0=85=0At=EC=89=C3]=B6=8D=00=00=00=00=04=C7=A8$=05=A2=E8=08=\r\n" + "C=D4=00=00=E8=EB=F6=89=89U=81=E5=A8=EC=00=00=89=00=FCuu=8B=89=08=F8]=B9=E8=\r\n" + "=FF=FF=83=FF=14=FEGt=C01=9D=8D=FFh=FF=FF=85=89=FFh=FF=FF=85=8D=FFl=FF=FF=04=\r\n" + "=89=E8$=FAP=FF=FFE=C7=00=EC=00=001=00=89=C0$D=89=08$\\=89=04$4=87=E8=FF=F6=\r\n" + "=89=FF$4/=E8=FF=F9=8B=FF=F8]u=8B=89=FC]=EC=BE=C3=00=13=00=00=E7=EB=B6=8D=00=\r\n" + "=00=00=00=89UW=E5SV=EC=81=01=DC=00=00E=BB=05\\=8B=08=0CU=E4=83=8B=F0=89=02=\r\n" + "$\\=C7=04$=04=00=06=00=00=EC=A3=05=A9=E8=08=F7*=FF=FF=04=C7F$=05\\=B9=08\\P=08=\r\n" + "=05L=89=04$=F5=E8=FF=F7=C7=FF$=04\\F=08=05=E9=E8=FF=F8=C7=FF$=04=1Dp=08=05=\r\n" + "U=E8=00=BB=B8=00=00=01=00=00=D21=08=A3=05=A7=B8=08=00=01=00=00=0C=A3=05=A7=\r\n" + }; + + + for (unsigned int i = 0 ; i < sizeof(testSuites) / sizeof(testSuites[0]) / 2 ; ++i) { + + const vmime::string decoded = testSuites[i * 2]; + const vmime::string encoded = testSuites[i * 2 + 1]; + + std::ostringstream oss; + oss << "[QP] Test " << (i + 1) << ": "; + + // Encoding + VASSERT_EQ(oss.str() + "encoding", encoded, encode("quoted-printable", decoded, 74)); + + // Decoding + VASSERT_EQ(oss.str() + "decoding", decoded, decode("quoted-printable", encoded, 74)); + + // Multiple and successive encoding/decoding + VASSERT_EQ( + oss.str() + "multiple1", + decoded, + decode("quoted-printable", + encode("quoted-printable", decoded)) + ); + + VASSERT_EQ( + oss.str() + "multiple2", + decoded, + decode("quoted-printable", + decode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", decoded)))) + ); + + VASSERT_EQ( + oss.str() + "multiple3", + decoded, + decode("quoted-printable", + decode("quoted-printable", + decode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", decoded)))))) + ); + + VASSERT_EQ( + oss.str() + "multiple4", + decoded, + decode("quoted-printable", + decode("quoted-printable", + decode("quoted-printable", + decode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", + encode("quoted-printable", decoded)))))))) + ); + + VASSERT( + oss.str() + "encoded size", + getEncoder("quoted-printable")->getEncodedSize(decoded.length()) + >= encode("quoted-printable", decoded).length() + ); + + VASSERT( + oss.str() + "decoded size", + getEncoder("quoted-printable")->getDecodedSize(encoded.length()) + >= decode("quoted-printable", encoded).length() + ); + } + } + + /** Tests Soft Line Breaks (RFC-2047/6.7(5). */ + void testQuotedPrintable_SoftLineBreaks() { + + VASSERT_EQ( + "1", + "Now's the time=\r\n" + " for all folk =\r\n" + "to come to the=\r\n" + " aid of their =\r\n" + "country.", + encode( + "quoted-printable", + "Now's the time for all folk " + "to come to the aid of their country.", + 15 + ) + ); + } + + void testQuotedPrintable_HardLineBreakEncode() { + + const std::string data = + "If you believe that truth=beauty," + " then surely mathematics\r\nis the most" + " beautiful branch of philosophy."; + + const std::string expected = + "If you believe that truth=3Dbeauty=\r\n" + ", then surely mathematics\r\n" + "is the most beautiful branch of ph=\r\n" + "ilosophy."; + + vmime::propertySet encProps; + encProps["text"] = true; + + VASSERT_EQ("1", expected, encode("quoted-printable", data, 35, encProps)); + } + + void testQuotedPrintable_HardLineBreakDecode() { + + const std::string expected = + "If you believe that truth=beauty," + " then surely mathematics\r\nis the most" + " beautiful branch of philosophy."; + + const std::string data = + "If you believe that truth=3Dbeauty=\r\n" + ", then surely mathematics\r\n" + "is the most beautiful branch of ph=\r\n" + "ilosophy."; + + VASSERT_EQ("1", expected, decode("quoted-printable", data, 35)); + } + + + /** In text mode, ensure line breaks in QP-encoded text are represented + * by a CRLF sequence, as per RFC-2047/6.7(4). */ + void testQuotedPrintable_CRLF() { + + vmime::propertySet encProps; + + // in "text" mode + encProps["text"] = true; + VASSERT_EQ( + "text", + "line1\r\nline2", + encode("quoted-printable", "line1\r\nline2", 80, encProps) + ); + + // in "binary" mode + encProps["text"] = false; + VASSERT_EQ( + "binary", + "line1=0D=0Aline2", + encode("quoted-printable", "line1\r\nline2", 80, encProps) + ); + } + + void testQuotedPrintable_RFC2047() { + + /* + * The RFC (http://tools.ietf.org/html/rfc2047#section-5) says: + * + * In this case the set of characters that may be used in a "Q"-encoded + * 'encoded-word' is restricted to: <upper and lower case ASCII + * letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_" + * (underscore, ASCII 95.)>. An 'encoded-word' that appears within a + * 'phrase' MUST be separated from any adjacent 'word', 'text' or + * 'special' by 'linear-white-space'. + */ + + vmime::propertySet encProps; + encProps["rfc2047"] = true; + + // Ensure 'especials' are encoded + VASSERT_EQ("especials.1", "=2C", encode("quoted-printable", ",", 10, encProps)); + VASSERT_EQ("especials.2", "=3B", encode("quoted-printable", ";", 10, encProps)); + VASSERT_EQ("especials.3", "=3A", encode("quoted-printable", ":", 10, encProps)); + VASSERT_EQ("especials.4", "=5F", encode("quoted-printable", "_", 10, encProps)); + VASSERT_EQ("especials.5", "=40", encode("quoted-printable", "@", 10, encProps)); + VASSERT_EQ("especials.6", "=28", encode("quoted-printable", "(", 10, encProps)); + VASSERT_EQ("especials.7", "=29", encode("quoted-printable", ")", 10, encProps)); + VASSERT_EQ("especials.8", "=3C", encode("quoted-printable", "<", 10, encProps)); + VASSERT_EQ("especials.9", "=3E", encode("quoted-printable", ">", 10, encProps)); + VASSERT_EQ("especials.10", "=5B", encode("quoted-printable", "[", 10, encProps)); + VASSERT_EQ("especials.11", "=5D", encode("quoted-printable", "]", 10, encProps)); + VASSERT_EQ("especials.12", "=22", encode("quoted-printable", "\"", 10, encProps)); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/filteredStreamTest.cpp b/vmime-master/tests/utility/filteredStreamTest.cpp new file mode 100644 index 0000000..ff3fb6a --- /dev/null +++ b/vmime-master/tests/utility/filteredStreamTest.cpp @@ -0,0 +1,341 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/filteredStream.hpp" +#include "vmime/utility/stringUtils.hpp" + + +VMIME_TEST_SUITE_BEGIN(filteredStreamTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testDotFilteredInputStream) + VMIME_TEST(testDotFilteredOutputStream) + VMIME_TEST(testCRLFToLFFilteredOutputStream) + VMIME_TEST(testStopSequenceFilteredInputStream1) + VMIME_TEST(testStopSequenceFilteredInputStreamN_2) + VMIME_TEST(testStopSequenceFilteredInputStreamN_3) + VMIME_TEST(testLFToCRLFFilteredOutputStream_Global) + VMIME_TEST(testLFToCRLFFilteredOutputStream_Edge) + VMIME_TEST_LIST_END + + + class chunkInputStream : public vmime::utility::inputStream { + + private: + + std::vector <std::string> m_chunks; + size_t m_index; + + public: + + chunkInputStream() : m_index(0) { } + + void addChunk(const std::string& chunk) { m_chunks.push_back(chunk); } + + bool eof() const { return (m_index >= m_chunks.size()); } + void reset() { m_index = 0; } + + vmime::size_t read(vmime::byte_t* const data, const vmime::size_t /* count */) { + + if (eof()) { + return 0; + } + + const std::string chunk = m_chunks[m_index]; + + // Warning: 'count' should be larger than chunk length. + // This is OK for our tests. + std::copy(chunk.begin(), chunk.end(), data); + + ++m_index; + + return chunk.length(); + } + + vmime::size_t skip(const vmime::size_t /* count */) { + + // Not supported + return 0; + } + }; + + + const std::string readWhole(vmime::utility::inputStream& is) { + + vmime::byte_t buffer[256]; + std::string whole; + + while (!is.eof()) { + + const vmime::size_t read = is.read(buffer, sizeof(buffer)); + + whole += vmime::utility::stringUtils::makeStringFromBytes(buffer, read); + } + + return whole; + } + + + // dotFilteredInputStream + + void testDotFilteredInputStreamHelper( + const std::string& number, + const std::string& expected, + const std::string& c1, + const std::string& c2 = "", + const std::string& c3 = "", + const std::string& c4 = "" + ) { + + chunkInputStream cis; + cis.addChunk(c1); + if (!c2.empty()) cis.addChunk(c2); + if (!c3.empty()) cis.addChunk(c3); + if (!c4.empty()) cis.addChunk(c4); + + vmime::utility::dotFilteredInputStream is(cis); + + std::ostringstream oss; + vmime::utility::outputStreamAdapter os(oss); + + vmime::utility::bufferedStreamCopy(is, os); + + VASSERT_EQ(number, expected, oss.str()); + } + + void testDotFilteredInputStream() { + + testDotFilteredInputStreamHelper("1", "foo\n.bar", "foo\n..bar"); + testDotFilteredInputStreamHelper("2", "foo\n.bar", "foo\n", "..bar"); + testDotFilteredInputStreamHelper("3", "foo\n.bar", "foo\n.", ".bar"); + testDotFilteredInputStreamHelper("4", "foo\n.bar", "foo\n..", "bar"); + testDotFilteredInputStreamHelper("5", "foo\n.bar", "foo\n", ".", ".bar"); + testDotFilteredInputStreamHelper("6", "foo\n.bar", "foo\n", ".", ".", "bar"); + + testDotFilteredInputStreamHelper("7", "\x0d\x0a.", "\x0d\x0a.."); + testDotFilteredInputStreamHelper("8", "\x0d\x0a.\x0d\x0a", "\x0d\x0a..\x0d\x0a"); + testDotFilteredInputStreamHelper("9", "\x0d\x0a.\x0d\x0a.", "\x0d\x0a..\x0d\x0a."); + testDotFilteredInputStreamHelper("10", "\x0d\x0a.\x0d\x0a.\x0d\x0ax", "\x0d\x0a..\x0d\x0a.\x0d\x0ax"); + testDotFilteredInputStreamHelper("11", "this is the first line\x0d\x0a.\x0d\x0aone dot\x0d\x0a..\x0d\x0atwo dots\x0d\x0a...\x0d\x0athree... \x0d\x0a.\x0d\x0a.\x0d\x0a", "this is the first line\x0d\x0a..\x0d\x0aone dot\x0d\x0a...\x0d\x0atwo dots\x0d\x0a....\x0d\x0athree... \x0d\x0a..\x0d\x0a.\x0d\x0a"); + } + + // dotFilteredOutputStream + // CRLFToLFFilteredOutputStream + + template <typename FILTER> + void testFilteredOutputStreamHelper( + const std::string& number, + const std::string& expected, + const std::string& c1, + const std::string& c2 = "", + const std::string& c3 = "", + const std::string& c4 = "" + ) { + + std::ostringstream oss; + vmime::utility::outputStreamAdapter os(oss); + + FILTER fos(os); + + fos.write(c1.data(), c1.length()); + if (!c2.empty()) fos.write(c2.data(), c2.length()); + if (!c3.empty()) fos.write(c3.data(), c3.length()); + if (!c4.empty()) fos.write(c4.data(), c4.length()); + + VASSERT_EQ(number, expected, oss.str()); + } + + void testDotFilteredOutputStream() { + + typedef vmime::utility::dotFilteredOutputStream FILTER; + + testFilteredOutputStreamHelper<FILTER>("1", "foo\n..bar", "foo\n.bar"); + testFilteredOutputStreamHelper<FILTER>("2", "foo\n..bar", "foo\n", ".bar"); + testFilteredOutputStreamHelper<FILTER>("3", "foo\n..bar", "foo", "\n.bar"); + testFilteredOutputStreamHelper<FILTER>("4", "foo\n..bar", "foo", "\n", ".bar"); + testFilteredOutputStreamHelper<FILTER>("5", "foo\n..bar", "foo", "\n", ".", "bar"); + + testFilteredOutputStreamHelper<FILTER>("6", "..\nfoobar", ".\nfoobar"); + testFilteredOutputStreamHelper<FILTER>("7", "..\r\nfoobar", ".\r\nfoobar"); + testFilteredOutputStreamHelper<FILTER>("8", "..\r\nfoobar", ".\r", "\nfoobar"); + testFilteredOutputStreamHelper<FILTER>("9", ".foobar", ".foobar"); + testFilteredOutputStreamHelper<FILTER>("10", ".foobar", ".", "foobar"); + + testFilteredOutputStreamHelper<FILTER>("11", "this is the first line\x0d\x0a...\x0d\x0aone dot\x0d\x0a....\x0d\x0atwo dots\x0d\x0a.....\x0d\x0athree... \x0d\x0a...\x0d\x0a..\x0d\x0a", "this is the first line\x0d\x0a..\x0d\x0aone dot\x0d\x0a...\x0d\x0atwo dots\x0d\x0a....\x0d\x0athree... \x0d\x0a..\x0d\x0a.\x0d\x0a"); + } + + void testCRLFToLFFilteredOutputStream() { + + typedef vmime::utility::CRLFToLFFilteredOutputStream FILTER; + + testFilteredOutputStreamHelper<FILTER>("1", "foo\nbar", "foo\r\nbar"); + testFilteredOutputStreamHelper<FILTER>("2", "foo\nbar", "foo\r\n", "bar"); + testFilteredOutputStreamHelper<FILTER>("3", "foo\nbar", "foo\r", "\nbar"); + testFilteredOutputStreamHelper<FILTER>("4", "foo\nbar", "foo", "\r\nbar"); + testFilteredOutputStreamHelper<FILTER>("5", "foo\nbar", "foo", "\r", "\nbar"); + testFilteredOutputStreamHelper<FILTER>("6", "foo\nbar", "foo", "\r", "\n", "bar"); + testFilteredOutputStreamHelper<FILTER>("7", "foo\nba\nr", "foo\r", "\nba\r\nr"); + } + + // stopSequenceFilteredInputStream + + template <int N> + void testStopSequenceFISHelper( + const std::string& number, + const std::string& sequence, + const std::string& expected, + const std::string& c1, + const std::string& c2 = "", + const std::string& c3 = "", + const std::string& c4 = "", + const std::string& c5 = "" + ) { + + chunkInputStream cis; + cis.addChunk(c1); + if (!c2.empty()) cis.addChunk(c2); + if (!c3.empty()) cis.addChunk(c3); + if (!c4.empty()) cis.addChunk(c4); + if (!c5.empty()) cis.addChunk(c5); + + vmime::utility::stopSequenceFilteredInputStream <N> is(cis, sequence.data()); + + VASSERT_EQ(number, expected, readWhole(is)); + } + + void testStopSequenceFilteredInputStream1() { + + testStopSequenceFISHelper <1>("1", "x", "foo", "fooxbar"); + testStopSequenceFISHelper <1>("2", "x", "foo", "foox", "bar"); + testStopSequenceFISHelper <1>("3", "x", "foo", "foo", "x", "bar"); + testStopSequenceFISHelper <1>("4", "x", "foo", "fo", "o", "x", "bar"); + testStopSequenceFISHelper <1>("5", "x", "foo", "fo", "o", "x", "b", "ar"); + + testStopSequenceFISHelper <1>("6", "x", "foobar", "fo", "o", "b", "ar"); + testStopSequenceFISHelper <1>("7", "x", "foobar", "foo", "bar"); + testStopSequenceFISHelper <1>("8", "x", "foobar", "foo", "b", "ar"); + + testStopSequenceFISHelper <1>("9", "x", "foobar", "foobar"); + testStopSequenceFISHelper <1>("10", "x", "foobar", "foobarx"); + + testStopSequenceFISHelper <1>("11", "x", "", ""); + testStopSequenceFISHelper <1>("12", "x", "", "x"); + testStopSequenceFISHelper <1>("13", "x", "", "", "x"); + } + + void testStopSequenceFilteredInputStreamN_2() { + + testStopSequenceFISHelper <2>("1", "xy", "foo", "fooxybar"); + testStopSequenceFISHelper <2>("2", "xy", "foo", "foox", "ybar"); + testStopSequenceFISHelper <2>("3", "xy", "foo", "foox", "y", "bar"); + testStopSequenceFISHelper <2>("4", "xy", "foo", "foo", "x", "ybar"); + testStopSequenceFISHelper <2>("5", "xy", "foo", "foo", "xy", "bar"); + testStopSequenceFISHelper <2>("6", "xy", "foo", "foo", "x", "y", "bar"); + + testStopSequenceFISHelper <2>("7", "xy", "fooxbar", "foox", "bar"); + testStopSequenceFISHelper <2>("8", "xy", "fooxbar", "foo", "xbar"); + testStopSequenceFISHelper <2>("9", "xy", "fooxbar", "foo", "x", "bar"); + testStopSequenceFISHelper <2>("10", "xy", "foobarx", "foo", "barx"); + + testStopSequenceFISHelper <2>("11", "xy", "foobar", "foobarxy"); + testStopSequenceFISHelper <2>("12", "xy", "foobar", "foo", "barxy"); + testStopSequenceFISHelper <2>("13", "xy", "foobar", "foo", "bar", "xy"); + + testStopSequenceFISHelper <2>("14", "xy", "", ""); + testStopSequenceFISHelper <2>("15", "xy", "x", "x"); + testStopSequenceFISHelper <2>("16", "xy", "", "xy"); + testStopSequenceFISHelper <2>("17", "xy", "", "x", "y"); + } + + void testStopSequenceFilteredInputStreamN_3() { + + testStopSequenceFISHelper <3>("1", "xyz", "foo", "fooxyzbar"); + testStopSequenceFISHelper <3>("2", "xyz", "foo", "foox", "yzbar"); + testStopSequenceFISHelper <3>("3", "xyz", "foo", "foox", "y", "zbar"); + testStopSequenceFISHelper <3>("4", "xyz", "foo", "foox", "yz", "bar"); + testStopSequenceFISHelper <3>("5", "xyz", "foo", "foo", "xyz", "bar"); + testStopSequenceFISHelper <3>("6", "xyz", "foo", "foo", "xy", "zbar"); + testStopSequenceFISHelper <3>("7", "xyz", "foo", "foo", "x", "y", "zbar"); + testStopSequenceFISHelper <3>("8", "xyz", "foo", "foo", "x", "y", "z", "bar"); + testStopSequenceFISHelper <3>("9", "xyz", "foo", "fooxy", "z", "bar"); + + testStopSequenceFISHelper <3>("10", "xyz", "fooxybar", "foox", "y", "bar"); + testStopSequenceFISHelper <3>("11", "xyz", "fooxybar", "fooxy", "bar"); + testStopSequenceFISHelper <3>("12", "xyz", "fooxybar", "fo", "ox", "y", "bar"); + testStopSequenceFISHelper <3>("13", "xyz", "fooxybar", "fo", "o", "x", "y", "bar"); + testStopSequenceFISHelper <3>("14", "xyz", "fooxybar", "foo", "x", "ybar"); + testStopSequenceFISHelper <3>("15", "xyz", "fooxybar", "foo", "xybar"); + + testStopSequenceFISHelper <3>("16", "xyz", "xfoxoxybxar", "xfoxo", "xybxar"); + testStopSequenceFISHelper <3>("17", "xyz", "xfoxoxybxarx", "xfoxo", "xybxarx"); + testStopSequenceFISHelper <3>("18", "xyz", "xfoxoxybxarxy", "xfoxo", "xybxarxy"); + + testStopSequenceFISHelper <3>("19", "xyz", "", ""); + testStopSequenceFISHelper <3>("20", "xyz", "x", "x"); + testStopSequenceFISHelper <3>("21", "xyz", "xy", "xy"); + testStopSequenceFISHelper <3>("22", "xyz", "", "xyz"); + testStopSequenceFISHelper <3>("23", "xyz", "", "x", "yz"); + testStopSequenceFISHelper <3>("24", "xyz", "", "x", "y", "z"); + } + + + // LFToCRLFFilteredOutputStream + + void testLFToCRLFFilteredOutputStream_Global() { + + typedef vmime::utility::LFToCRLFFilteredOutputStream FILTER; + + testFilteredOutputStreamHelper<FILTER>("1", "ABC\r\nDEF", "ABC\nDEF"); + testFilteredOutputStreamHelper<FILTER>("2", "ABC\r\nDEF", "ABC\rDEF"); + testFilteredOutputStreamHelper<FILTER>("3", "\r\n\r\nAB\r\n\r\nA\r\nB\r\n", "\n\nAB\n\nA\nB\n"); + testFilteredOutputStreamHelper<FILTER>("4", "ABCDE\r\nF", "ABCDE\nF"); + testFilteredOutputStreamHelper<FILTER>("5", "ABCDE\r\nF", "ABCDE\r\nF"); + testFilteredOutputStreamHelper<FILTER>("6", "\r\n\r\n\r\n", "\n\n\n"); + testFilteredOutputStreamHelper<FILTER>("7", "\r\n\r\n\r\n", "\r\r\n\n"); + testFilteredOutputStreamHelper<FILTER>("8", "\r\n\r\n\r\n\r\n", "\r\r\r\r"); + testFilteredOutputStreamHelper<FILTER>("9", "\r\n\r\n\r\n\r\n", "\n\n\n\n"); + testFilteredOutputStreamHelper<FILTER>("10", "\r\n\r\n\r\n", "\r\n\n\n"); + testFilteredOutputStreamHelper<FILTER>("11", "\r\n\r\n\r\n\r\n", "\n\n\n\r\n"); + } + + void testLFToCRLFFilteredOutputStream_Edge() { + + typedef vmime::utility::LFToCRLFFilteredOutputStream FILTER; + + testFilteredOutputStreamHelper<FILTER>("1", "\r\n\r\n", "\r", "\r"); + testFilteredOutputStreamHelper<FILTER>("2", "\r\n\r\n", "\r", "\n\r"); + testFilteredOutputStreamHelper<FILTER>("3", "ABC\r\n\r\n", "ABC\r", "\n\r"); + testFilteredOutputStreamHelper<FILTER>("4", "ABC\r\n\r\n\r\n", "ABC\r", "\n\r", "\n\n"); + testFilteredOutputStreamHelper<FILTER>("5", "\r\n\r\n", "\n", "\n"); + testFilteredOutputStreamHelper<FILTER>("6", "\r\n\r\n", "\r\n\r\n"); + testFilteredOutputStreamHelper<FILTER>("7", "\r\n\r\n", "\r\n\r", "\n"); + + testFilteredOutputStreamHelper<FILTER>("8", "A\r\nB\r\nC\r\nD", "A\rB", "\nC\r\nD"); + testFilteredOutputStreamHelper<FILTER>("9", "\r\nA\r\nB\r\nC\r\nD", "\rA\r", "B\nC\r\nD"); + testFilteredOutputStreamHelper<FILTER>("10", "\r\nA\r\nB\r\nC\r\nD", "\nA\r", "B\nC\r\nD"); + testFilteredOutputStreamHelper<FILTER>("11", "\r\nA\r\nB\r\nC\r\nD\r\n", "\nA\rB", "\nC\r\nD\r"); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/outputStreamByteArrayAdapterTest.cpp b/vmime-master/tests/utility/outputStreamByteArrayAdapterTest.cpp new file mode 100644 index 0000000..07a0f4b --- /dev/null +++ b/vmime-master/tests/utility/outputStreamByteArrayAdapterTest.cpp @@ -0,0 +1,82 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/outputStreamByteArrayAdapter.hpp" + + +VMIME_TEST_SUITE_BEGIN(outputStreamByteArrayAdapterTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testWrite) + VMIME_TEST(testWriteBinary) + VMIME_TEST(testWriteCRLF) + VMIME_TEST_LIST_END + + + void testWrite() { + + vmime::byteArray bytes; + + vmime::utility::outputStreamByteArrayAdapter stream(bytes); + stream << "some data"; + stream.flush(); + + VASSERT_EQ("Write 1", 0, memcmp("some data", &bytes[0], 9)); + + stream.write("more data", 9); + + VASSERT_EQ("Write 2", 0, memcmp("some datamore data", &bytes[0], 18)); + } + + void testWriteBinary() { + + const char binaryData[] = + "\xc5\x9a\xc3\xb8\xc9\xb1\xc9\x9b\x20\xc9\x93\xc9\xa8\xc9\xb2\xc9" + "\x91\xc5\x95\xc9\xa3\x20\xc9\x96\xc9\x90\xca\x88\xc9\x92"; + + vmime::byteArray bytes; + + vmime::utility::outputStreamByteArrayAdapter stream(bytes); + stream.write(binaryData, sizeof(binaryData)); + stream.flush(); + + VASSERT_EQ("Write", 0, memcmp(binaryData, &bytes[0], sizeof(binaryData))); + } + + void testWriteCRLF() { + + vmime::byteArray bytes; + + vmime::utility::outputStreamByteArrayAdapter stream(bytes); + stream << "some data"; + stream.flush(); + + stream << "\nmore\r\ndata\r"; + stream.flush(); + + VASSERT_EQ("Write", 0, memcmp("some data\nmore\r\ndata\r", &bytes[0], 21)); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/outputStreamSocketAdapterTest.cpp b/vmime-master/tests/utility/outputStreamSocketAdapterTest.cpp new file mode 100644 index 0000000..a869ed7 --- /dev/null +++ b/vmime-master/tests/utility/outputStreamSocketAdapterTest.cpp @@ -0,0 +1,87 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/outputStreamSocketAdapter.hpp" + + +VMIME_TEST_SUITE_BEGIN(outputStreamSocketAdapterTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testWrite) + VMIME_TEST(testWriteBinary) + VMIME_TEST(testWriteCRLF) + VMIME_TEST_LIST_END + + + void testWrite() { + + vmime::shared_ptr <testSocket> socket = vmime::make_shared <testSocket>(); + + vmime::utility::outputStreamSocketAdapter stream(*socket); + stream << "some data"; + stream.flush(); + + vmime::string buffer; + socket->localReceive(buffer); + + VASSERT_EQ("Write", "some data", buffer); + } + + void testWriteBinary() { + + const char binaryData[] = + "\xc5\x9a\xc3\xb8\xc9\xb1\xc9\x9b\x20\xc9\x93\xc9\xa8\xc9\xb2\xc9" + "\x91\xc5\x95\xc9\xa3\x20\xc9\x96\xc9\x90\xca\x88\xc9\x92"; + + vmime::shared_ptr <testSocket> socket = vmime::make_shared <testSocket>(); + + vmime::utility::outputStreamSocketAdapter stream(*socket); + stream.write(binaryData, sizeof(binaryData)); + stream.flush(); + + vmime::string buffer; + socket->localReceive(buffer); + + VASSERT_EQ("Write", 0, memcmp(binaryData, buffer.data(), sizeof(binaryData))); + } + + void testWriteCRLF() { + + vmime::shared_ptr <testSocket> socket = vmime::make_shared <testSocket>(); + + vmime::utility::outputStreamSocketAdapter stream(*socket); + stream << "some data"; + stream.flush(); + + stream << "\nmore\r\ndata\r"; + stream.flush(); + + vmime::string buffer; + socket->localReceive(buffer); + + VASSERT_EQ("Write", "some data\nmore\r\ndata\r", buffer); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/outputStreamStringAdapterTest.cpp b/vmime-master/tests/utility/outputStreamStringAdapterTest.cpp new file mode 100644 index 0000000..3de8f8b --- /dev/null +++ b/vmime-master/tests/utility/outputStreamStringAdapterTest.cpp @@ -0,0 +1,84 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/outputStreamStringAdapter.hpp" + + +VMIME_TEST_SUITE_BEGIN(outputStreamStringAdapterTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testWrite) + VMIME_TEST(testWriteBinary) + VMIME_TEST(testWriteCRLF) + VMIME_TEST_LIST_END + + + void testWrite() { + + vmime::string str("initial data"); + + vmime::utility::outputStreamStringAdapter stream(str); + stream << "additional data"; + stream.flush(); + + VASSERT_EQ("Write 1 len", 27, str.length()); + VASSERT_EQ("Write 1 data", "initial dataadditional data", str); + + stream.write("more data"); + + VASSERT_EQ("Write 2 len", 36, str.length()); + VASSERT_EQ("Write 2 data", "initial dataadditional datamore data", str); + } + + void testWriteBinary() { + + const vmime::byte_t binaryData[] = + "\xc5\x9a\xc3\xb8\xc9\xb1\xc9\x9b\x20\xc9\x93\xc9\xa8\xc9\xb2\xc9" + "\x91\xc5\x95\xc9\xa3\x20\xc9\x96\xc9\x90\xca\x88\xc9\x92"; + + vmime::string str; + + vmime::utility::outputStreamStringAdapter stream(str); + stream.write(binaryData, sizeof(binaryData)); + stream.flush(); + + VASSERT_EQ("Write", 0, memcmp(binaryData, str.data(), sizeof(binaryData))); + } + + void testWriteCRLF() { + + vmime::string str; + + vmime::utility::outputStreamStringAdapter stream(str); + stream << "some data"; + stream.flush(); + + stream << "\nmore\r\ndata\r"; + stream.flush(); + + VASSERT_EQ("Write", "some data\nmore\r\ndata\r", str); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/parserInputStreamAdapterTest.cpp b/vmime-master/tests/utility/parserInputStreamAdapterTest.cpp new file mode 100644 index 0000000..4bc310c --- /dev/null +++ b/vmime-master/tests/utility/parserInputStreamAdapterTest.cpp @@ -0,0 +1,51 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/parserInputStreamAdapter.hpp" + + +VMIME_TEST_SUITE_BEGIN(parserInputStreamAdapterTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testEndlessLoopBufferSize) + VMIME_TEST_LIST_END + + + void testEndlessLoopBufferSize() { + + static const unsigned int BUFFER_SIZE = 4096; // same as in parserInputStreamAdapter::findNext() + + vmime::string str(BUFFER_SIZE, 'X'); + + vmime::shared_ptr <vmime::utility::inputStreamStringAdapter> iss = + vmime::make_shared <vmime::utility::inputStreamStringAdapter>(str); + + vmime::shared_ptr <vmime::utility::parserInputStreamAdapter> parser = + vmime::make_shared <vmime::utility::parserInputStreamAdapter>(iss); + + VASSERT_EQ("Not found", vmime::string::npos, parser->findNext("token")); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/pathTest.cpp b/vmime-master/tests/utility/pathTest.cpp new file mode 100644 index 0000000..e6227f3 --- /dev/null +++ b/vmime-master/tests/utility/pathTest.cpp @@ -0,0 +1,356 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/path.hpp" + + +VMIME_TEST_SUITE_BEGIN(utilityPathTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testConstruct1) + VMIME_TEST(testConstruct2) + VMIME_TEST(testConstruct3) + VMIME_TEST(testConstruct4) + + VMIME_TEST(testAppendComponent) + + VMIME_TEST(testOperatorDiv1) + VMIME_TEST(testOperatorDiv2) + + VMIME_TEST(testOperatorDivEqual1) + VMIME_TEST(testOperatorDivEqual2) + + VMIME_TEST(testGetParent) + + VMIME_TEST(testComparison) + + VMIME_TEST(testGetLastComponent) + + VMIME_TEST(testIsDirectParentOf) + VMIME_TEST(testIsParentOf) + VMIME_TEST(testIsParentOf_EquivalentCharset) + + VMIME_TEST(testRenameParent) + + VMIME_TEST(testFromString) + VMIME_TEST(testFromString_IgnoreLeadingOrTrailingSep) + VMIME_TEST(testToString) + VMIME_TEST_LIST_END + + + typedef vmime::utility::path path; + typedef vmime::utility::path::component comp; + + + void testConstruct1() { + + VASSERT_EQ("1", true, path().isEmpty()); + VASSERT_EQ("2", 0, path().getSize()); + } + + void testConstruct2() { + + path p(comp("foo")); + + VASSERT_EQ("1", false, p.isEmpty()); + VASSERT_EQ("2", 1, p.getSize()); + VASSERT_EQ("3", "foo", p.getComponentAt(0).getBuffer()); + } + + void testAppendComponent() { + + path p; + + VASSERT_EQ("1", 0, p.getSize()); + + comp c("foo"); + p.appendComponent(c); + + VASSERT_EQ("2", 1, p.getSize()); + VASSERT_EQ("3", c.getBuffer(), p.getComponentAt(0).getBuffer()); + } + + void testConstruct3() { + + path p1; + p1.appendComponent(comp("foo")); + p1.appendComponent(comp("bar")); + + path p2(p1); + + VASSERT_EQ("1", 2, p2.getSize()); + VASSERT_EQ("2", "foo", p2.getComponentAt(0).getBuffer()); + VASSERT_EQ("3", "bar", p2.getComponentAt(1).getBuffer()); + } + + void testConstruct4() { + + // Same as path::path(const component&) + path p("foo"); + + VASSERT_EQ("1", false, p.isEmpty()); + VASSERT_EQ("2", 1, p.getSize()); + VASSERT_EQ("3", "foo", p.getComponentAt(0).getBuffer()); + } + + void testOperatorDiv1() { + + path p1; + p1.appendComponent(comp("foo")); + p1.appendComponent(comp("bar")); + + path p2; + p2.appendComponent(comp("baz")); + + path p3 = p1 / p2; + + VASSERT_EQ("1", 3, p3.getSize()); + VASSERT_EQ("2", p1.getComponentAt(0).getBuffer(), p3.getComponentAt(0).getBuffer()); + VASSERT_EQ("3", p1.getComponentAt(1).getBuffer(), p3.getComponentAt(1).getBuffer()); + VASSERT_EQ("4", p2.getComponentAt(0).getBuffer(), p3.getComponentAt(2).getBuffer()); + } + + void testOperatorDiv2() { + + path p1; + p1.appendComponent(comp("foo")); + p1.appendComponent(comp("bar")); + + comp c("baz"); + + path p2 = p1 / c; + + VASSERT_EQ("1", 3, p2.getSize()); + VASSERT_EQ("2", p1.getComponentAt(0).getBuffer(), p2.getComponentAt(0).getBuffer()); + VASSERT_EQ("3", p1.getComponentAt(1).getBuffer(), p2.getComponentAt(1).getBuffer()); + VASSERT_EQ("4", c.getBuffer(), p2.getComponentAt(2).getBuffer()); + } + + void testOperatorDivEqual1() { + + path p1; + p1.appendComponent(comp("foo")); + p1.appendComponent(comp("bar")); + + path p2; + p2.appendComponent(comp("baz")); + + path p3(p1); + p3 /= p2; + + VASSERT_EQ("1", 3, p3.getSize()); + VASSERT_EQ("2", p1.getComponentAt(0).getBuffer(), p3.getComponentAt(0).getBuffer()); + VASSERT_EQ("3", p1.getComponentAt(1).getBuffer(), p3.getComponentAt(1).getBuffer()); + VASSERT_EQ("4", p2.getComponentAt(0).getBuffer(), p3.getComponentAt(2).getBuffer()); + } + + void testOperatorDivEqual2() { + + path p1; + p1.appendComponent(comp("foo")); + p1.appendComponent(comp("bar")); + + comp c("baz"); + + path p2(p1); + p2 /= c; + + VASSERT_EQ("1", 3, p2.getSize()); + VASSERT_EQ("2", p1.getComponentAt(0).getBuffer(), p2.getComponentAt(0).getBuffer()); + VASSERT_EQ("3", p1.getComponentAt(1).getBuffer(), p2.getComponentAt(1).getBuffer()); + VASSERT_EQ("4", c.getBuffer(), p2.getComponentAt(2).getBuffer()); + } + + void testGetParent() { + + path p1; + path p1p = p1.getParent(); + + VASSERT_EQ("1", true, p1p.isEmpty()); + + path p2; + p2.appendComponent(comp("foo")); + p2.appendComponent(comp("bar")); + + path p2p = p2.getParent(); + + VASSERT_EQ("2", 1, p2p.getSize()); + VASSERT_EQ("3", p2.getComponentAt(0).getBuffer(), p2p.getComponentAt(0).getBuffer()); + } + + void testComparison() { + + path p1; + p1.appendComponent(comp("foo")); + p1.appendComponent(comp("bar")); + + path p2; + p2.appendComponent(comp("foo")); + p2.appendComponent(comp("bar")); + + path p3; + p3.appendComponent(comp("foo")); + p3.appendComponent(comp("bar")); + p3.appendComponent(comp("baz")); + + VASSERT_EQ("1", true, p1 == p2); + VASSERT_EQ("2", false, p1 == p3); + + VASSERT_EQ("3", false, p1 != p2); + VASSERT_EQ("4", true, p1 != p3); + + VASSERT_EQ("5", true, p3.getParent() == p1); + } + + void testGetLastComponent() { + + path p1; + p1.appendComponent(comp("foo")); + p1.appendComponent(comp("bar")); + p1.appendComponent(comp("baz")); + + VASSERT_EQ("1", "baz", p1.getLastComponent().getBuffer()); + VASSERT_EQ("2", "bar", p1.getParent().getLastComponent().getBuffer()); + VASSERT_EQ("3", "foo", p1.getParent().getParent().getLastComponent().getBuffer()); + } + + void testIsDirectParentOf() { + + path p1; + p1.appendComponent(comp("foo")); + + path p2; + p2.appendComponent(comp("foo")); + p2.appendComponent(comp("bar")); + + path p3; + p3.appendComponent(comp("foo")); + p3.appendComponent(comp("bar")); + p3.appendComponent(comp("baz")); + + VASSERT_EQ("1", true, p1.isDirectParentOf(p2)); + VASSERT_EQ("2", true, p2.isDirectParentOf(p3)); + VASSERT_EQ("3", false, p1.isDirectParentOf(p3)); + VASSERT_EQ("4", false, p2.isDirectParentOf(p1)); + } + + void testIsParentOf() { + + path p1; + p1.appendComponent(comp("foo")); + + path p2; + p2.appendComponent(comp("foo")); + p2.appendComponent(comp("bar")); + + path p3; + p3.appendComponent(comp("foo")); + p3.appendComponent(comp("bar")); + p3.appendComponent(comp("baz")); + + VASSERT_EQ("1", true, p1.isParentOf(p2)); + VASSERT_EQ("2", true, p2.isParentOf(p3)); + VASSERT_EQ("3", true, p1.isParentOf(p3)); + VASSERT_EQ("4", false, p2.isParentOf(p1)); + } + + void testIsParentOf_EquivalentCharset() { + + path p1; + p1.appendComponent(comp("foo", "us-ascii")); + + path p2; + p2.appendComponent(comp("foo", "utf-8")); + p2.appendComponent(comp("bar")); + p2.appendComponent(comp("baz")); + + VASSERT_EQ("1", true, p1.isParentOf(p2)); + } + + void testRenameParent() { + + path p1; + p1.appendComponent(comp("a")); + p1.appendComponent(comp("b")); + p1.appendComponent(comp("c")); + p1.appendComponent(comp("d")); + + path p2; + p2.appendComponent(comp("a")); + p2.appendComponent(comp("b")); + + path p3; + p3.appendComponent(comp("x")); + p3.appendComponent(comp("y")); + p3.appendComponent(comp("z")); + + path p(p1); + p.renameParent(p2, p3); + + VASSERT_EQ("1", 5, p.getSize()); + VASSERT_EQ("2", "x", p.getComponentAt(0).getBuffer()); + VASSERT_EQ("3", "y", p.getComponentAt(1).getBuffer()); + VASSERT_EQ("4", "z", p.getComponentAt(2).getBuffer()); + VASSERT_EQ("5", "c", p.getComponentAt(3).getBuffer()); + VASSERT_EQ("6", "d", p.getComponentAt(4).getBuffer()); + } + + void testFromString() { + + path p = path::fromString("ab/cde/f", "/", vmime::charset("my-charset")); + + VASSERT_EQ("count", 3, p.getSize()); + VASSERT_EQ("buffer1", "ab", p.getComponentAt(0).getBuffer()); + VASSERT_EQ("charset1", "my-charset", p.getComponentAt(0).getCharset().getName()); + VASSERT_EQ("buffer2", "cde", p.getComponentAt(1).getBuffer()); + VASSERT_EQ("charset2", "my-charset", p.getComponentAt(1).getCharset().getName()); + VASSERT_EQ("buffer3", "f", p.getComponentAt(2).getBuffer()); + VASSERT_EQ("charset3", "my-charset", p.getComponentAt(2).getCharset().getName()); + } + + void testFromString_IgnoreLeadingOrTrailingSep() { + + path p = path::fromString("//ab/cde/f////", "/", vmime::charset("my-charset")); + + VASSERT_EQ("count", 3, p.getSize()); + VASSERT_EQ("buffer1", "ab", p.getComponentAt(0).getBuffer()); + VASSERT_EQ("charset1", "my-charset", p.getComponentAt(0).getCharset().getName()); + VASSERT_EQ("buffer2", "cde", p.getComponentAt(1).getBuffer()); + VASSERT_EQ("charset2", "my-charset", p.getComponentAt(1).getCharset().getName()); + VASSERT_EQ("buffer3", "f", p.getComponentAt(2).getBuffer()); + VASSERT_EQ("charset3", "my-charset", p.getComponentAt(2).getCharset().getName()); + } + + void testToString() { + + path p; + p.appendComponent(comp("ab")); + p.appendComponent(comp("cde")); + p.appendComponent(comp("f")); + + VASSERT_EQ("string", "ab/cde/f", p.toString("/", vmime::charset("us-ascii"))); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/seekableInputStreamRegionAdapterTest.cpp b/vmime-master/tests/utility/seekableInputStreamRegionAdapterTest.cpp new file mode 100644 index 0000000..b099cd7 --- /dev/null +++ b/vmime-master/tests/utility/seekableInputStreamRegionAdapterTest.cpp @@ -0,0 +1,176 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/inputStreamStringAdapter.hpp" +#include "vmime/utility/seekableInputStreamRegionAdapter.hpp" +#include "vmime/utility/stringUtils.hpp" + + +using namespace vmime::utility; + + +VMIME_TEST_SUITE_BEGIN(seekableInputStreamRegionAdapterTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testInitialPosition) + VMIME_TEST(testSeekAndGetPosition) + VMIME_TEST(testRead) + VMIME_TEST(testSkip) + VMIME_TEST(testReset) + VMIME_TEST(testOwnPosition) + VMIME_TEST_LIST_END + + + vmime::shared_ptr <seekableInputStreamRegionAdapter> createStream( + vmime::shared_ptr <seekableInputStream>* underlyingStream = NULL + ) { + + vmime::string buffer("THIS IS A TEST BUFFER"); + + vmime::shared_ptr <seekableInputStream> strStream = + vmime::make_shared <inputStreamStringAdapter>(buffer); + + vmime::shared_ptr <seekableInputStreamRegionAdapter> rgnStream = + vmime::make_shared <seekableInputStreamRegionAdapter>(strStream, 10, 11); + + if (underlyingStream) { + *underlyingStream = strStream; + } + + return rgnStream; + } + + void testInitialPosition() { + + vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream(); + + VASSERT_EQ("Pos", 0, stream->getPosition()); + VASSERT_FALSE("EOF", stream->eof()); + } + + void testSeekAndGetPosition() { + + vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream(); + + stream->seek(5); + + VASSERT_EQ("Pos 1", 5, stream->getPosition()); + VASSERT_FALSE("EOF 1", stream->eof()); + + stream->seek(20); + + VASSERT_EQ("Pos 2", 11, stream->getPosition()); + VASSERT_TRUE("EOF 2", stream->eof()); + } + + void testRead() { + + vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream(); + + stream->seek(5); + + vmime::byte_t buffer[100]; + std::fill(vmime::begin(buffer), vmime::end(buffer), 0); + vmime::size_t read = stream->read(buffer, 6); + + VASSERT_EQ("Pos", 11, stream->getPosition()); + VASSERT_EQ("Read", 6, read); + VASSERT_TRUE("EOF", stream->eof()); + VASSERT_EQ("Buffer", "BUFFER", vmime::utility::stringUtils::makeStringFromBytes(buffer, 6)); + } + + void testSkip() { + + vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream(); + + stream->skip(5); + + VASSERT_EQ("Pos 1", 5, stream->getPosition()); + VASSERT_FALSE("EOF 1", stream->eof()); + + vmime::byte_t buffer[100]; + std::fill(vmime::begin(buffer), vmime::end(buffer), 0); + vmime::size_t read = stream->read(buffer, 3); + + VASSERT_EQ("Pos 2", 8, stream->getPosition()); + VASSERT_EQ("Read", 3, read); + VASSERT_FALSE("EOF 2", stream->eof()); + VASSERT_EQ("Buffer", "BUF", vmime::utility::stringUtils::makeStringFromBytes(buffer, 3)); + + stream->skip(50); + + VASSERT_EQ("Pos 3", 11, stream->getPosition()); + VASSERT_TRUE("EOF 3", stream->eof()); + } + + void testReset() { + + vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream(); + + stream->skip(100); + stream->reset(); + + VASSERT_EQ("Pos", 0, stream->getPosition()); + VASSERT_FALSE("EOF", stream->eof()); + } + + void testOwnPosition() { + + // seekableInputStreamRegionAdapter should keep track of its own position + // in the underlying stream, and not be affected by possible seek/read + // operations on it... + vmime::shared_ptr <seekableInputStream> ustream; + vmime::shared_ptr <seekableInputStreamRegionAdapter> stream = createStream(&ustream); + + stream->seek(5); + + vmime::byte_t buffer1[100]; + std::fill(vmime::begin(buffer1), vmime::end(buffer1), 0); + + VASSERT_EQ("Read 1", 7, ustream->read(buffer1, 7)); + + vmime::byte_t buffer2[100]; + std::fill(vmime::begin(buffer2), vmime::end(buffer2), 0); + + VASSERT_EQ("Read 2", 6, stream->read(buffer2, 6)); + + VASSERT_EQ( + "Buffer 1", + "THIS IS", + vmime::utility::stringUtils::makeStringFromBytes(buffer1, 7) + ); + + VASSERT_EQ( + "Buffer 2", + "BUFFER", + vmime::utility::stringUtils::makeStringFromBytes(buffer2, 6) + ); + + // ...but the underlying stream position is affected by read operations + // from the region adapter (FIXME?) + VASSERT_EQ("Pos", 21, ustream->getPosition()); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/stringUtilsTest.cpp b/vmime-master/tests/utility/stringUtilsTest.cpp new file mode 100644 index 0000000..6c2e18f --- /dev/null +++ b/vmime-master/tests/utility/stringUtilsTest.cpp @@ -0,0 +1,214 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/stringUtils.hpp" + + +VMIME_TEST_SUITE_BEGIN(stringUtilsTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testMakeStringFromBytes) + VMIME_TEST(testAppendBytesToString) + + VMIME_TEST(testIsStringEqualNoCase1) + VMIME_TEST(testIsStringEqualNoCase2) + VMIME_TEST(testIsStringEqualNoCase3) + + VMIME_TEST(testToLower) + + VMIME_TEST(testTrim) + + VMIME_TEST(testCountASCIIChars) + + VMIME_TEST(testUnquote) + + VMIME_TEST(testIsValidHostname) + VMIME_TEST(testIsValidFQDN) + VMIME_TEST_LIST_END + + + typedef vmime::utility::stringUtils stringUtils; + + + void testMakeStringFromBytes() { + + vmime::byte_t bytes[] = { 0x12, 0x34, 0x56, 0x78 }; + vmime::string str = vmime::utility::stringUtils::makeStringFromBytes(bytes, 3); + + VASSERT_EQ("length", 3, str.length()); + VASSERT_EQ("byte1", '\x12', str[0]); + VASSERT_EQ("byte2", '\x34', str[1]); + VASSERT_EQ("byte3", '\x56', str[2]); + } + + void testAppendBytesToString() { + + vmime::byte_t bytes[] = { 0x42, 0x56, 0x12, 0x00, 'f', 'o', 'o' }; + + vmime::string str = "test"; + vmime::utility::stringUtils::appendBytesToString(str, bytes, 7); + + VASSERT_EQ("length", 4 + 7, str.length()); + VASSERT_EQ("byte1", 't', str[0]); + VASSERT_EQ("byte2", 'e', str[1]); + VASSERT_EQ("byte3", 's', str[2]); + VASSERT_EQ("byte4", 't', str[3]); + VASSERT_EQ("byte5", '\x42', str[4]); + VASSERT_EQ("byte6", '\x56', str[5]); + VASSERT_EQ("byte7", '\x12', str[6]); + VASSERT_EQ("byte8", '\0', str[7]); + VASSERT_EQ("byte9", 'f', str[8]); + VASSERT_EQ("byte10", 'o', str[9]); + VASSERT_EQ("byte11", 'o', str[10]); + } + + void testIsStringEqualNoCase1() { + + VASSERT_EQ("1", true, stringUtils::isStringEqualNoCase(vmime::string("foo"), "foo", 3)); + VASSERT_EQ("2", true, stringUtils::isStringEqualNoCase(vmime::string("FOo"), "foo", 3)); + + VASSERT_EQ("3", false, stringUtils::isStringEqualNoCase(vmime::string("foo"), "FOo", 3)); + VASSERT_EQ("4", false, stringUtils::isStringEqualNoCase(vmime::string("foo"), "bar", 3)); + + VASSERT_EQ("5", false, stringUtils::isStringEqualNoCase(vmime::string("foO"), "bar", 3)); + VASSERT_EQ("6", false, stringUtils::isStringEqualNoCase(vmime::string("foOO"), "barO", 4)); + VASSERT_EQ("7", false, stringUtils::isStringEqualNoCase(vmime::string("foOO"), "ba", 2)); + + VASSERT_EQ("8", true, stringUtils::isStringEqualNoCase(vmime::string("FOoooo"), "foo", 3)); + } + + void testIsStringEqualNoCase2() { + + VASSERT_EQ("1", true, stringUtils::isStringEqualNoCase(vmime::string("foo"), vmime::string("foo"))); + VASSERT_EQ("2", true, stringUtils::isStringEqualNoCase(vmime::string("FOo"), vmime::string("foo"))); + VASSERT_EQ("3", true, stringUtils::isStringEqualNoCase(vmime::string("foO"), vmime::string("FOo"))); + + VASSERT_EQ("4", false, stringUtils::isStringEqualNoCase(vmime::string("foO"), vmime::string("bar"))); + VASSERT_EQ("5", false, stringUtils::isStringEqualNoCase(vmime::string("foOO"), vmime::string("barO"))); + } + + void testIsStringEqualNoCase3() { + + vmime::string str1("FooBar"); + + VASSERT_EQ("1", true, stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "foobar", 6)); + VASSERT_EQ("2", false, stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "FooBar", 6)); + VASSERT_EQ("3", true, stringUtils::isStringEqualNoCase(str1.begin(), str1.end(), "fooBar", 3)); + VASSERT_EQ("4", false, stringUtils::isStringEqualNoCase(str1.begin(), str1.begin() + 3, "fooBar", 6)); + VASSERT_EQ("5", false, stringUtils::isStringEqualNoCase(str1.begin(), str1.begin() + 3, "bar", 3)); + VASSERT_EQ("6", false, stringUtils::isStringEqualNoCase(str1.begin(), str1.begin() + 6, "barbar", 6)); + } + + void testToLower() { + + VASSERT_EQ("1", "foo", stringUtils::toLower("FOO")); + VASSERT_EQ("2", "foo", stringUtils::toLower("foO")); + VASSERT_EQ("3", "foo", stringUtils::toLower("foo")); + } + + void testTrim() { + + VASSERT_EQ("1", "foo", stringUtils::trim(" foo")); + VASSERT_EQ("2", "foo", stringUtils::trim("\t\tfoo")); + VASSERT_EQ("3", "foo", stringUtils::trim(" \t \tfoo")); + VASSERT_EQ("4", "foo", stringUtils::trim(" \r\n\tfoo")); + + VASSERT_EQ("5", "foo", stringUtils::trim("foo ")); + VASSERT_EQ("6", "foo", stringUtils::trim("foo\t\t")); + VASSERT_EQ("7", "foo", stringUtils::trim("foo \t \t")); + VASSERT_EQ("8", "foo", stringUtils::trim("foo \r\n\t")); + + VASSERT_EQ( "9", "foo", stringUtils::trim("foo ")); + VASSERT_EQ("10", "foo", stringUtils::trim(" foo ")); + VASSERT_EQ("11", "foo", stringUtils::trim(" foo\t\t")); + VASSERT_EQ("12", "foo", stringUtils::trim("\tfoo \r \t")); + VASSERT_EQ("13", "foo", stringUtils::trim("\r \tfoo \n\t")); + } + + void testCountASCIIChars() { + + vmime::string str1("foo"); + VASSERT_EQ( + "1", + static_cast <vmime::size_t>(3), + stringUtils::countASCIIchars(str1.begin(), str1.end()) + ); + + vmime::string str2("f=?oo"); + VASSERT_EQ( + "2", + static_cast <vmime::size_t>(3 + 1), + stringUtils::countASCIIchars(str2.begin(), str2.end()) + ); + + vmime::string str3("foo\x7f"); + VASSERT_EQ( + "3", + static_cast <vmime::size_t>(4), + stringUtils::countASCIIchars(str3.begin(), str3.end()) + ); + + vmime::string str4("foo\x80"); + VASSERT_EQ( + "4", + static_cast <vmime::size_t>(3), + stringUtils::countASCIIchars(str4.begin(), str4.end()) + ); + } + + void testUnquote() { + + VASSERT_EQ("1", "quoted", stringUtils::unquote("\"quoted\"")); // "quoted" + VASSERT_EQ("2", "\"not quoted", stringUtils::unquote("\"not quoted")); // "not quoted + VASSERT_EQ("3", "not quoted\"", stringUtils::unquote("not quoted\"")); // not quoted" + VASSERT_EQ("4", "quoted with \"escape\"", stringUtils::unquote("\"quoted with \\\"escape\\\"\"")); // "quoted with \"escape\"" + } + + void testIsValidHostname() { + + VASSERT_TRUE ("1", stringUtils::isValidHostname("localhost")); + VASSERT_TRUE ("2", stringUtils::isValidHostname("localhost.localdomain")); + VASSERT_TRUE ("3", stringUtils::isValidHostname("example.com")); + VASSERT_TRUE ("4", stringUtils::isValidHostname("host.example.com")); + VASSERT_FALSE("5", stringUtils::isValidHostname(".example.com")); + VASSERT_FALSE("6", stringUtils::isValidHostname(".-example.com")); + VASSERT_FALSE("7", stringUtils::isValidHostname(".example-.com")); + VASSERT_FALSE("8", stringUtils::isValidHostname(".exa--mple.com")); + VASSERT_FALSE("9", stringUtils::isValidHostname("-example.com")); + } + + void testIsValidFQDN() { + + VASSERT_FALSE("1", stringUtils::isValidFQDN("localhost")); + VASSERT_FALSE("2", stringUtils::isValidFQDN("localhost.localdomain")); + VASSERT_FALSE("3", stringUtils::isValidFQDN("example.com")); + VASSERT_TRUE ("4", stringUtils::isValidFQDN("host.example.com")); + VASSERT_FALSE("5", stringUtils::isValidFQDN(".example.com")); + VASSERT_FALSE("6", stringUtils::isValidFQDN(".-example.com")); + VASSERT_FALSE("7", stringUtils::isValidFQDN(".example-.com")); + VASSERT_FALSE("8", stringUtils::isValidFQDN(".exa--mple.com")); + } + +VMIME_TEST_SUITE_END diff --git a/vmime-master/tests/utility/urlTest.cpp b/vmime-master/tests/utility/urlTest.cpp new file mode 100644 index 0000000..36820a6 --- /dev/null +++ b/vmime-master/tests/utility/urlTest.cpp @@ -0,0 +1,312 @@ +// +// VMime library (http://www.vmime.org) +// Copyright (C) 2002 Vincent Richard <vincent@vmime.org> +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 3 of +// the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// Linking this library statically or dynamically with other modules is making +// a combined work based on this library. Thus, the terms and conditions of +// the GNU General Public License cover the whole combination. +// + +#include "tests/testUtils.hpp" + +#include "vmime/utility/url.hpp" +#include "vmime/utility/urlUtils.hpp" + + +VMIME_TEST_SUITE_BEGIN(urlTest) + + VMIME_TEST_LIST_BEGIN + VMIME_TEST(testParse1) + VMIME_TEST(testParse2) + VMIME_TEST(testParse3) + VMIME_TEST(testParse4) + VMIME_TEST(testParse5) + VMIME_TEST(testGenerate) + VMIME_TEST(testUtilsEncode) + VMIME_TEST(testUtilsDecode) + VMIME_TEST(testUtilsDecodeSpecialCases) + VMIME_TEST(testUtilsEncodeReservedChars) + VMIME_TEST(testUtilsEncodeUnsafeChars) + VMIME_TEST_LIST_END + + + static bool parseHelper(vmime::utility::url& u, const vmime::string& str) { + + try { + + u = vmime::utility::url(str); + + } catch (vmime::exceptions::malformed_url) { + + return false; + } + + return true; + } + + + void testParse1() { + + // Test some valid constructions + vmime::utility::url u1("", ""); + + VASSERT_EQ("1.1", true, parseHelper(u1, "protocol://user:password@host:12345/path/")); + VASSERT_EQ("1.2", "protocol", u1.getProtocol()); + VASSERT_EQ("1.3", "user", u1.getUsername()); + VASSERT_EQ("1.4", "password", u1.getPassword()); + VASSERT_EQ("1.5", "host", u1.getHost()); + VASSERT_EQ("1.6", 12345, u1.getPort()); + VASSERT_EQ("1.7", "/path/", u1.getPath()); + + vmime::utility::url u2("", ""); + + VASSERT_EQ("2.1", true, parseHelper(u2, "protocol://user@host:12345/path/")); + VASSERT_EQ("2.2", "protocol", u2.getProtocol()); + VASSERT_EQ("2.3", "user", u2.getUsername()); + VASSERT_EQ("2.4", "", u2.getPassword()); + VASSERT_EQ("2.5", "host", u2.getHost()); + VASSERT_EQ("2.6", 12345, u2.getPort()); + VASSERT_EQ("2.7", "/path/", u2.getPath()); + + vmime::utility::url u3("", ""); + + VASSERT_EQ("3.1", true, parseHelper(u3, "protocol://host:12345/path/")); + VASSERT_EQ("3.2", "protocol", u3.getProtocol()); + VASSERT_EQ("3.3", "", u3.getUsername()); + VASSERT_EQ("3.4", "", u3.getPassword()); + VASSERT_EQ("3.5", "host", u3.getHost()); + VASSERT_EQ("3.6", 12345, u3.getPort()); + VASSERT_EQ("3.7", "/path/", u3.getPath()); + + vmime::utility::url u4("", ""); + + VASSERT_EQ("4.1", true, parseHelper(u4, "protocol://host/path/")); + VASSERT_EQ("4.2", "protocol", u4.getProtocol()); + VASSERT_EQ("4.3", "", u4.getUsername()); + VASSERT_EQ("4.4", "", u4.getPassword()); + VASSERT_EQ("4.5", "host", u4.getHost()); + VASSERT_EQ("4.6", vmime::utility::url::UNSPECIFIED_PORT, u4.getPort()); + VASSERT_EQ("4.7", "/path/", u4.getPath()); + + vmime::utility::url u5("", ""); + + VASSERT_EQ("5.1", true, parseHelper(u5, "protocol://host/")); + VASSERT_EQ("5.2", "protocol", u5.getProtocol()); + VASSERT_EQ("5.3", "", u5.getUsername()); + VASSERT_EQ("5.4", "", u5.getPassword()); + VASSERT_EQ("5.5", "host", u5.getHost()); + VASSERT_EQ("5.6", vmime::utility::url::UNSPECIFIED_PORT, u4.getPort()); + VASSERT_EQ("5.7", "", u5.getPath()); + + vmime::utility::url u6("", ""); + + VASSERT_EQ("6.1", true, parseHelper(u4, "protocol://host/path/file")); + VASSERT_EQ("6.2", "protocol", u4.getProtocol()); + VASSERT_EQ("6.3", "", u4.getUsername()); + VASSERT_EQ("6.4", "", u4.getPassword()); + VASSERT_EQ("6.5", "host", u4.getHost()); + VASSERT_EQ("6.6", vmime::utility::url::UNSPECIFIED_PORT, u4.getPort()); + VASSERT_EQ("6.7", "/path/file", u4.getPath()); + } + + void testParse2() { + + // Now, test some ill-formed URLs + + // -- missing protocol + vmime::utility::url u1("", ""); + VASSERT_EQ("1", false, parseHelper(u1, "://host")); + + // -- port can contain only digits + vmime::utility::url u2("", ""); + VASSERT_EQ("2", false, parseHelper(u2, "proto://host:abc123")); + + // -- no host specified + vmime::utility::url u3("", ""); + VASSERT_EQ("3", false, parseHelper(u3, "proto:///path")); + + // -- no protocol separator (://) + vmime::utility::url u4("", ""); + VASSERT_EQ("4", false, parseHelper(u4, "protohost/path")); + } + + void testParse3() { + + // Test decoding + vmime::utility::url u1("", ""); + + VASSERT_EQ("1.1", true, parseHelper(u1, "pro%12to://user%34:pass%56word@ho%78st:12345/pa%abth/")); + VASSERT_EQ("1.2", "pro%12to", u1.getProtocol()); // protocol should not be decoded + VASSERT_EQ("1.3", "user\x34", u1.getUsername()); + VASSERT_EQ("1.4", "pass\x56word", u1.getPassword()); + VASSERT_EQ("1.5", "ho\x78st", u1.getHost()); + VASSERT_EQ("1.6", 12345, u1.getPort()); + VASSERT_EQ("1.7", "/pa\xabth/", u1.getPath()); + } + + void testParse4() { + + // Test parameters + vmime::utility::url u1("", ""); + + VASSERT_EQ("1.1", true, parseHelper(u1, "proto://host/path?p1=v1&p2=v2")); + VASSERT_EQ("1.2", "v1", u1.getParams()["p1"]); + VASSERT_EQ("1.3", "v2", u1.getParams()["p2"]); + VASSERT_EQ("1.4", "/path", u1.getPath()); + + vmime::utility::url u2("", ""); + + VASSERT_EQ("2.1", true, parseHelper(u2, "proto://host/path?p1=v1&p2")); + VASSERT_EQ("2.2", "v1", u2.getParams()["p1"]); + VASSERT_EQ("2.3", "p2", u2.getParams()["p2"]); + VASSERT_EQ("2.4", "/path", u2.getPath()); + + vmime::utility::url u3("", ""); + + VASSERT_EQ("3.1", true, parseHelper(u3, "proto://host/?p1=v1&p2=v2")); + VASSERT_EQ("3.2", "v1", u3.getParams()["p1"]); + VASSERT_EQ("3.3", "v2", u3.getParams()["p2"]); + VASSERT_EQ("3.4", "", u3.getPath()); + + vmime::utility::url u4("", ""); + + VASSERT_EQ("4.1", true, parseHelper(u4, "proto://host/path?p1=%3D&%3D=v2")); + VASSERT_EQ("4.2", "=", u4.getParams()["p1"]); + VASSERT_EQ("4.3", "v2", u4.getParams()["="]); + VASSERT_EQ("4.4", "/path", u4.getPath()); + } + + // '@' symbol in the username part + void testParse5() { + + vmime::utility::url u1("", ""); + + VASSERT_EQ("1", true, parseHelper(u1, "imap://account@myserver.com:password@myserver.com")); + VASSERT_EQ("2", "account@myserver.com", u1.getUsername()); + VASSERT_EQ("3", "password", u1.getPassword()); + VASSERT_EQ("4", "myserver.com", u1.getHost()); + } + + void testGenerate() { + + vmime::utility::url u1("proto", "host", 12345, "path", "user", "password"); + VASSERT_EQ( + "1", + "proto://user:password@host:12345/path", + static_cast <vmime::string>(u1) + ); + + vmime::utility::url u2("proto", "host"); + VASSERT_EQ("2", "proto://host", static_cast <vmime::string>(u2)); + + vmime::utility::url u3("proto", "host"); + u3.getParams()["p1"] = "v1"; + VASSERT_EQ( + "3.1", + "proto://host/?p1=v1", + static_cast <vmime::string>(u3) + ); + u3.getParams()["p2"] = "v2"; + VASSERT_EQ( + "3.2", + "proto://host/?p1=v1&p2=v2", + static_cast <vmime::string>(u3) + ); + + // Test special characters + u3.getParams().clear(); + u3.getParams()["&"] = "="; + VASSERT_EQ( + "3.3", + "proto://host/?%26=%3D", + static_cast <vmime::string>(u3) + ); + } + + void testUtilsEncode() { + + VASSERT_EQ("1", "%01", vmime::utility::urlUtils::encode("\x01")); + VASSERT_EQ("2", "%20", vmime::utility::urlUtils::encode(" ")); + VASSERT_EQ("3", "%FF", vmime::utility::urlUtils::encode("\xff")); + VASSERT_EQ("4", "a", vmime::utility::urlUtils::encode("a")); + } + + void testUtilsDecode() { + + for (int i = 0 ; i < 255 ; ++i) { + + std::ostringstream ossTest; + ossTest << "%" << "0123456789ABCDEF"[i / 16] + << "0123456789ABCDEF"[i % 16]; + + std::ostringstream ossNum; + ossNum << i; + + vmime::string res; + res += static_cast <unsigned char>(i); + + VASSERT_EQ( + ossNum.str(), + res, + vmime::utility::urlUtils::decode(ossTest.str()) + ); + } + + } + + void testUtilsDecodeSpecialCases() { + + // Bug #1656547: segfault with '%' at the end of the string + VASSERT_EQ("1.1", "sadfsda%", vmime::utility::urlUtils::decode("sadfsda%")); + VASSERT_EQ("1.2", "sadfsda\x05", vmime::utility::urlUtils::decode("sadfsda%5")); + VASSERT_EQ("1.3", "sadfsda\x42", vmime::utility::urlUtils::decode("sadfsda%42")); + } + + void testUtilsEncodeReservedChars() { + + VASSERT_EQ("1", "%24", vmime::utility::urlUtils::encode("$")); + VASSERT_EQ("2", "%26", vmime::utility::urlUtils::encode("&")); + VASSERT_EQ("3", "%2B", vmime::utility::urlUtils::encode("+")); + VASSERT_EQ("4", "%2C", vmime::utility::urlUtils::encode(",")); + VASSERT_EQ("5", "%2F", vmime::utility::urlUtils::encode("/")); + VASSERT_EQ("6", "%3A", vmime::utility::urlUtils::encode(":")); + VASSERT_EQ("7", "%3B", vmime::utility::urlUtils::encode(";")); + VASSERT_EQ("8", "%3D", vmime::utility::urlUtils::encode("=")); + VASSERT_EQ("9", "%3F", vmime::utility::urlUtils::encode("?")); + VASSERT_EQ("10", "%40", vmime::utility::urlUtils::encode("@")); + } + + void testUtilsEncodeUnsafeChars() { + + VASSERT_EQ("1", "%20", vmime::utility::urlUtils::encode(" ")); + VASSERT_EQ("2", "%22", vmime::utility::urlUtils::encode("\"")); + VASSERT_EQ("3", "%3C", vmime::utility::urlUtils::encode("<")); + VASSERT_EQ("4", "%3E", vmime::utility::urlUtils::encode(">")); + VASSERT_EQ("5", "%23", vmime::utility::urlUtils::encode("#")); + VASSERT_EQ("6", "%25", vmime::utility::urlUtils::encode("%")); + VASSERT_EQ("7", "%7B", vmime::utility::urlUtils::encode("{")); + VASSERT_EQ("8", "%7D", vmime::utility::urlUtils::encode("}")); + VASSERT_EQ("9", "%7C", vmime::utility::urlUtils::encode("|")); + VASSERT_EQ("10", "%5C", vmime::utility::urlUtils::encode("\\")); + VASSERT_EQ("11", "%5E", vmime::utility::urlUtils::encode("^")); + VASSERT_EQ("12", "%7E", vmime::utility::urlUtils::encode("~")); + VASSERT_EQ("13", "%5B", vmime::utility::urlUtils::encode("[")); + VASSERT_EQ("14", "%5D", vmime::utility::urlUtils::encode("]")); + VASSERT_EQ("15", "%60", vmime::utility::urlUtils::encode("`")); + } + +VMIME_TEST_SUITE_END |