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