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