comparison 3rdparty/vmime/tests/parser/charsetFilteredOutputStreamTest.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 "vmime/charset.hpp"
27 #include "vmime/charsetConverter.hpp"
28
29 #include "charsetTestSuites.hpp"
30
31
32 VMIME_TEST_SUITE_BEGIN(charsetFilteredOutputStreamTest)
33
34 VMIME_TEST_LIST_BEGIN
35 VMIME_TEST(testInputBufferUnderflow)
36 VMIME_TEST(testInvalidInput1)
37 VMIME_TEST(testStreamCopy)
38 VMIME_TEST(testOneByteAtTime)
39 VMIME_TEST(testVariableInputChunk)
40 VMIME_TEST_LIST_END
41
42
43 void testInputBufferUnderflow()
44 {
45 vmime::shared_ptr <vmime::charsetConverter> cc =
46 vmime::charsetConverter::create("utf-8", "iso-8859-1");
47
48 vmime::string output;
49 vmime::utility::outputStreamStringAdapter os(output);
50 vmime::shared_ptr <vmime::utility::filteredOutputStream> cfos = cc->getFilteredOutputStream(os);
51
52 VASSERT_NOT_NULL("filteredOutputStream availability", cfos);
53
54 // føo = 66 c3 b8 6f [UTF8]
55 // føo = 66 f8 6f [latin1]
56
57 cfos->write("\x66\xc3", 2);
58
59 // Incomplete UTF-8 sequence was not converted
60 VASSERT_EQ("chunk 1", toHex("f"), toHex(output));
61
62 // Write second byte of UTF-8 sequence
63 cfos->write("\xb8\x6f", 2);
64
65 VASSERT_EQ("chunk 2", toHex("f\xf8o"), toHex(output));
66 }
67
68 void testInvalidInput1()
69 {
70 vmime::string in("foo\xab\xcd\xef bar");
71 vmime::string expectedOut("foo??? bar");
72
73 vmime::string actualOut;
74 vmime::utility::outputStreamStringAdapter osa(actualOut);
75
76 vmime::shared_ptr <vmime::charsetConverter> conv =
77 vmime::charsetConverter::create
78 (vmime::charset("utf-8"),
79 vmime::charset("iso-8859-1"));
80
81 vmime::shared_ptr <vmime::utility::charsetFilteredOutputStream> os =
82 conv->getFilteredOutputStream(osa);
83
84 VASSERT_NOT_NULL("filteredOutputStream availability", os);
85
86 vmime::utility::inputStreamStringAdapter is(in);
87
88 vmime::byte_t buffer[16];
89
90 for (int i = 0 ; !is.eof() ; ++i)
91 os->write(buffer, is.read(buffer, 1));
92
93 os->flush();
94
95 VASSERT_EQ("1", toHex(expectedOut), toHex(actualOut));
96 }
97
98 // Using 'bufferedStreamCopy'
99 void testStreamCopy()
100 {
101 for (unsigned int i = 0 ; i < charsetTestSuitesCount ; ++i)
102 {
103 const charsetTestSuiteStruct& entry = charsetTestSuites[i];
104
105 std::ostringstream testName;
106 testName << i << ": " << entry.fromCharset << " -> " << entry.toCharset;
107
108 const unsigned long inLength = (entry.fromLength == 0 ? strlen(entry.fromBytes) : entry.fromLength);
109 vmime::string in(entry.fromBytes, entry.fromBytes + inLength);
110
111 const unsigned long outLength = (entry.toLength == 0 ? strlen(entry.toBytes) : entry.toLength);
112 vmime::string expectedOut(entry.toBytes, entry.toBytes + outLength);
113
114 vmime::string actualOut;
115 vmime::utility::outputStreamStringAdapter osa(actualOut);
116
117 vmime::shared_ptr <vmime::charsetConverter> conv =
118 vmime::charsetConverter::create(entry.fromCharset, entry.toCharset);
119
120 vmime::shared_ptr <vmime::utility::charsetFilteredOutputStream> os =
121 conv->getFilteredOutputStream(osa);
122
123 VASSERT_NOT_NULL("filteredOutputStream availability", os);
124
125 vmime::utility::inputStreamStringAdapter is(in);
126
127 vmime::utility::bufferedStreamCopy(is, *os);
128
129 os->flush();
130
131 VASSERT_EQ(testName.str(), toHex(expectedOut), toHex(actualOut));
132 }
133 }
134
135 // One byte at a time
136 void testOneByteAtTime()
137 {
138 for (unsigned int i = 0 ; i < charsetTestSuitesCount ; ++i)
139 {
140 const charsetTestSuiteStruct& entry = charsetTestSuites[i];
141
142 std::ostringstream testName;
143 testName << i << ": " << entry.fromCharset << " -> " << entry.toCharset;
144
145 const unsigned long inLength = (entry.fromLength == 0 ? strlen(entry.fromBytes) : entry.fromLength);
146 vmime::string in(entry.fromBytes, entry.fromBytes + inLength);
147
148 const unsigned long outLength = (entry.toLength == 0 ? strlen(entry.toBytes) : entry.toLength);
149 vmime::string expectedOut(entry.toBytes, entry.toBytes + outLength);
150
151 vmime::string actualOut;
152 vmime::utility::outputStreamStringAdapter osa(actualOut);
153
154 vmime::shared_ptr <vmime::charsetConverter> conv =
155 vmime::charsetConverter::create(entry.fromCharset, entry.toCharset);
156
157 vmime::shared_ptr <vmime::utility::charsetFilteredOutputStream> os =
158 conv->getFilteredOutputStream(osa);
159
160 VASSERT_NOT_NULL("filteredOutputStream availability", os);
161
162 vmime::utility::inputStreamStringAdapter is(in);
163
164 vmime::byte_t buffer[16];
165
166 for (int i = 0 ; !is.eof() ; ++i)
167 os->write(buffer, is.read(buffer, 1));
168
169 os->flush();
170
171 VASSERT_EQ(testName.str(), toHex(expectedOut), toHex(actualOut));
172 }
173 }
174
175 // Variable chunks
176 void testVariableInputChunk()
177 {
178 for (unsigned int i = 0 ; i < charsetTestSuitesCount ; ++i)
179 {
180 const charsetTestSuiteStruct& entry = charsetTestSuites[i];
181
182 std::ostringstream testName;
183 testName << i << ": " << entry.fromCharset << " -> " << entry.toCharset;
184
185 const unsigned long inLength = (entry.fromLength == 0 ? strlen(entry.fromBytes) : entry.fromLength);
186 vmime::string in(entry.fromBytes, entry.fromBytes + inLength);
187
188 const unsigned long outLength = (entry.toLength == 0 ? strlen(entry.toBytes) : entry.toLength);
189 vmime::string expectedOut(entry.toBytes, entry.toBytes + outLength);
190
191 vmime::string actualOut;
192 vmime::utility::outputStreamStringAdapter osa(actualOut);
193
194 vmime::shared_ptr <vmime::charsetConverter> conv =
195 vmime::charsetConverter::create(entry.fromCharset, entry.toCharset);
196
197 vmime::shared_ptr <vmime::utility::charsetFilteredOutputStream> os =
198 conv->getFilteredOutputStream(osa);
199
200 VASSERT_NOT_NULL("filteredOutputStream availability", os);
201
202 vmime::utility::inputStreamStringAdapter is(in);
203
204 vmime::byte_t buffer[16];
205
206 for (int i = 0 ; !is.eof() ; ++i)
207 os->write(buffer, is.read(buffer, (i % 5) + 1));
208
209 os->flush();
210
211 VASSERT_EQ(testName.str(), toHex(expectedOut), toHex(actualOut));
212 }
213 }
214
215 VMIME_TEST_SUITE_END