annotate 3rdparty/vmime/tests/parser/charsetTest.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1 //
ferencd@0 2 // VMime library (http://www.vmime.org)
ferencd@0 3 // Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
ferencd@0 4 //
ferencd@0 5 // This program is free software; you can redistribute it and/or
ferencd@0 6 // modify it under the terms of the GNU General Public License as
ferencd@0 7 // published by the Free Software Foundation; either version 3 of
ferencd@0 8 // the License, or (at your option) any later version.
ferencd@0 9 //
ferencd@0 10 // This program is distributed in the hope that it will be useful,
ferencd@0 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
ferencd@0 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ferencd@0 13 // General Public License for more details.
ferencd@0 14 //
ferencd@0 15 // You should have received a copy of the GNU General Public License along
ferencd@0 16 // with this program; if not, write to the Free Software Foundation, Inc.,
ferencd@0 17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ferencd@0 18 //
ferencd@0 19 // Linking this library statically or dynamically with other modules is making
ferencd@0 20 // a combined work based on this library. Thus, the terms and conditions of
ferencd@0 21 // the GNU General Public License cover the whole combination.
ferencd@0 22 //
ferencd@0 23
ferencd@0 24 #include "tests/testUtils.hpp"
ferencd@0 25
ferencd@0 26 #include "charsetTestSuites.hpp"
ferencd@0 27
ferencd@0 28
ferencd@0 29 VMIME_TEST_SUITE_BEGIN(charsetTest)
ferencd@0 30
ferencd@0 31 VMIME_TEST_LIST_BEGIN
ferencd@0 32 // Test valid input
ferencd@0 33 VMIME_TEST(testConvertStringValid)
ferencd@0 34 VMIME_TEST(testConvertStreamValid)
ferencd@0 35 VMIME_TEST(testEncodingHebrew1255)
ferencd@0 36
ferencd@0 37 // IDNA
ferencd@0 38 VMIME_TEST(testEncodeIDNA)
ferencd@0 39 VMIME_TEST(testDecodeIDNA)
ferencd@0 40
ferencd@0 41 VMIME_TEST(testUTF7Support)
ferencd@0 42 VMIME_TEST_LIST_END
ferencd@0 43
ferencd@0 44
ferencd@0 45 void testConvertStringValid()
ferencd@0 46 {
ferencd@0 47 for (unsigned int i = 0 ; i < charsetTestSuitesCount ; ++i)
ferencd@0 48 {
ferencd@0 49 const charsetTestSuiteStruct& entry = charsetTestSuites[i];
ferencd@0 50
ferencd@0 51 std::ostringstream testName;
ferencd@0 52 testName << i << ": " << entry.fromCharset << " -> " << entry.toCharset;
ferencd@0 53
ferencd@0 54 const unsigned long inLength = (entry.fromLength == 0 ? strlen(entry.fromBytes) : entry.fromLength);
ferencd@0 55 vmime::string in(entry.fromBytes, entry.fromBytes + inLength);
ferencd@0 56
ferencd@0 57 const unsigned long outLength = (entry.toLength == 0 ? strlen(entry.toBytes) : entry.toLength);
ferencd@0 58 vmime::string expectedOut(entry.toBytes, entry.toBytes + outLength);
ferencd@0 59
ferencd@0 60 vmime::string actualOut;
ferencd@0 61
ferencd@0 62 vmime::charset::convert
ferencd@0 63 (in, actualOut, entry.fromCharset, entry.toCharset);
ferencd@0 64
ferencd@0 65 VASSERT_EQ(testName.str(), toHex(expectedOut), toHex(actualOut));
ferencd@0 66 }
ferencd@0 67 }
ferencd@0 68
ferencd@0 69 void testConvertStreamValid()
ferencd@0 70 {
ferencd@0 71 for (unsigned int i = 0 ; i < charsetTestSuitesCount ; ++i)
ferencd@0 72 {
ferencd@0 73 const charsetTestSuiteStruct& entry = charsetTestSuites[i];
ferencd@0 74
ferencd@0 75 std::ostringstream testName;
ferencd@0 76 testName << i << ": " << entry.fromCharset << " -> " << entry.toCharset;
ferencd@0 77
ferencd@0 78 const unsigned long inLength = (entry.fromLength == 0 ? strlen(entry.fromBytes) : entry.fromLength);
ferencd@0 79 vmime::string in(entry.fromBytes, entry.fromBytes + inLength);
ferencd@0 80
ferencd@0 81 const unsigned long outLength = (entry.toLength == 0 ? strlen(entry.toBytes) : entry.toLength);
ferencd@0 82 vmime::string expectedOut(entry.toBytes, entry.toBytes + outLength);
ferencd@0 83
ferencd@0 84 vmime::string actualOut;
ferencd@0 85 vmime::utility::outputStreamStringAdapter os(actualOut);
ferencd@0 86
ferencd@0 87 vmime::utility::inputStreamStringAdapter is(in);
ferencd@0 88
ferencd@0 89 vmime::charset::convert
ferencd@0 90 (is, os, entry.fromCharset, entry.toCharset);
ferencd@0 91
ferencd@0 92 os.flush();
ferencd@0 93
ferencd@0 94 VASSERT_EQ(testName.str(), toHex(expectedOut), toHex(actualOut));
ferencd@0 95 }
ferencd@0 96 }
ferencd@0 97
ferencd@0 98 void testEncodingHebrew1255()
ferencd@0 99 {
ferencd@0 100 // hewbrew string in windows-1255 charset
ferencd@0 101 const char data[] = "\xe9\xf9\xf7\xf8\xe9\xf9\xf8\xf7\xe9\xe9\xf9";
ferencd@0 102 vmime::word w = vmime::word(data, "windows-1255");
ferencd@0 103 vmime::string encoded = w.generate();
ferencd@0 104 // less than 60% ascii, base64 received
ferencd@0 105 VASSERT_EQ("1", "=?windows-1255?B?6fn3+On5+Pfp6fk=?=", encoded);
ferencd@0 106 }
ferencd@0 107
ferencd@0 108 static const vmime::string convertHelper
ferencd@0 109 (const vmime::string& in, const vmime::charset& csrc, const vmime::charset& cdest)
ferencd@0 110 {
ferencd@0 111 vmime::string out;
ferencd@0 112 vmime::charset::convert(in, out, csrc, cdest);
ferencd@0 113
ferencd@0 114 return out;
ferencd@0 115 }
ferencd@0 116
ferencd@0 117 void testEncodeIDNA()
ferencd@0 118 {
ferencd@0 119 VASSERT_EQ("1", "xn--espaol-zwa", convertHelper("español", "utf-8", "idna"));
ferencd@0 120
ferencd@0 121 // Tests from ICANN
ferencd@0 122 VASSERT_EQ("2.1", "xn--hxajbheg2az3al", convertHelper("παράδειγμα", "utf-8", "idna"));
ferencd@0 123 VASSERT_EQ("2.2", "xn--jxalpdlp", convertHelper("δοκιμή", "utf-8", "idna"));
ferencd@0 124
ferencd@0 125 VASSERT_EQ("3.1", "xn--mgbh0fb", convertHelper("مثال", "utf-8", "idna"));
ferencd@0 126 VASSERT_EQ("3.2", "xn--kgbechtv", convertHelper("إختبار", "utf-8", "idna"));
ferencd@0 127 }
ferencd@0 128
ferencd@0 129 void testDecodeIDNA()
ferencd@0 130 {
ferencd@0 131 VASSERT_EQ("1", "español", convertHelper("xn--espaol-zwa", "idna", "utf-8"));
ferencd@0 132
ferencd@0 133 // Tests from ICANN
ferencd@0 134 VASSERT_EQ("2.1", "παράδειγμα", convertHelper("xn--hxajbheg2az3al", "idna", "utf-8"));
ferencd@0 135 VASSERT_EQ("2.2", "δοκιμή", convertHelper("xn--jxalpdlp", "idna", "utf-8"));
ferencd@0 136
ferencd@0 137 VASSERT_EQ("3.1", "مثال", convertHelper("xn--mgbh0fb", "idna", "utf-8"));
ferencd@0 138 VASSERT_EQ("3.2", "إختبار", convertHelper("xn--kgbechtv", "idna", "utf-8"));
ferencd@0 139 }
ferencd@0 140
ferencd@0 141 void testUTF7Support()
ferencd@0 142 {
ferencd@0 143 // Ensure UTF-7 is supported, because it is used for IMAP
ferencd@0 144 VASSERT_EQ("1", "VMime +- UTF-7 encoding", convertHelper("VMime + UTF-7 encoding", "utf-8", "utf-7"));
ferencd@0 145 VASSERT_EQ("2", "f+APg-o", convertHelper("\x66\xc3\xb8\x6f", "utf-8", "utf-7"));
ferencd@0 146 }
ferencd@0 147
ferencd@0 148 VMIME_TEST_SUITE_END
ferencd@0 149