comparison 3rdparty/vmime/tests/utility/filteredStreamTest.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/utility/filteredStream.hpp"
27 #include "vmime/utility/stringUtils.hpp"
28
29
30 VMIME_TEST_SUITE_BEGIN(filteredStreamTest)
31
32 VMIME_TEST_LIST_BEGIN
33 VMIME_TEST(testDotFilteredInputStream)
34 VMIME_TEST(testDotFilteredOutputStream)
35 VMIME_TEST(testCRLFToLFFilteredOutputStream)
36 VMIME_TEST(testStopSequenceFilteredInputStream1)
37 VMIME_TEST(testStopSequenceFilteredInputStreamN_2)
38 VMIME_TEST(testStopSequenceFilteredInputStreamN_3)
39 VMIME_TEST(testLFToCRLFFilteredOutputStream_Global)
40 VMIME_TEST(testLFToCRLFFilteredOutputStream_Edge)
41 VMIME_TEST_LIST_END
42
43
44 class chunkInputStream : public vmime::utility::inputStream
45 {
46 private:
47
48 std::vector <std::string> m_chunks;
49 size_t m_index;
50
51 public:
52
53 chunkInputStream() : m_index(0) { }
54
55 void addChunk(const std::string& chunk) { m_chunks.push_back(chunk); }
56
57 bool eof() const { return (m_index >= m_chunks.size()); }
58 void reset() { m_index = 0; }
59
60 vmime::size_t read(vmime::byte_t* const data, const vmime::size_t /* count */)
61 {
62 if (eof())
63 return 0;
64
65 const std::string chunk = m_chunks[m_index];
66
67 // Warning: 'count' should be larger than chunk length.
68 // This is OK for our tests.
69 std::copy(chunk.begin(), chunk.end(), data);
70
71 ++m_index;
72
73 return chunk.length();
74 }
75
76 vmime::size_t skip(const vmime::size_t /* count */)
77 {
78 // Not supported
79 return 0;
80 }
81 };
82
83
84 const std::string readWhole(vmime::utility::inputStream& is)
85 {
86 vmime::byte_t buffer[256];
87 std::string whole;
88
89 while (!is.eof())
90 {
91 const vmime::size_t read = is.read(buffer, sizeof(buffer));
92
93 whole += vmime::utility::stringUtils::makeStringFromBytes(buffer, read);
94 }
95
96 return (whole);
97 }
98
99
100 // dotFilteredInputStream
101
102 void testDotFilteredInputStreamHelper
103 (const std::string& number, const std::string& expected,
104 const std::string& c1, const std::string& c2 = "",
105 const std::string& c3 = "", const std::string& c4 = "")
106 {
107 chunkInputStream cis;
108 cis.addChunk(c1);
109 if (!c2.empty()) cis.addChunk(c2);
110 if (!c3.empty()) cis.addChunk(c3);
111 if (!c4.empty()) cis.addChunk(c4);
112
113 vmime::utility::dotFilteredInputStream is(cis);
114
115 std::ostringstream oss;
116 vmime::utility::outputStreamAdapter os(oss);
117
118 vmime::utility::bufferedStreamCopy(is, os);
119
120 VASSERT_EQ(number, expected, oss.str());
121 }
122
123 void testDotFilteredInputStream()
124 {
125 testDotFilteredInputStreamHelper("1", "foo\n.bar", "foo\n..bar");
126 testDotFilteredInputStreamHelper("2", "foo\n.bar", "foo\n", "..bar");
127 testDotFilteredInputStreamHelper("3", "foo\n.bar", "foo\n.", ".bar");
128 testDotFilteredInputStreamHelper("4", "foo\n.bar", "foo\n..", "bar");
129 testDotFilteredInputStreamHelper("5", "foo\n.bar", "foo\n", ".", ".bar");
130 testDotFilteredInputStreamHelper("6", "foo\n.bar", "foo\n", ".", ".", "bar");
131 }
132
133 // dotFilteredOutputStream
134 // CRLFToLFFilteredOutputStream
135
136 template <typename FILTER>
137 void testFilteredOutputStreamHelper
138 (const std::string& number, const std::string& expected,
139 const std::string& c1, const std::string& c2 = "",
140 const std::string& c3 = "", const std::string& c4 = "")
141 {
142 std::ostringstream oss;
143 vmime::utility::outputStreamAdapter os(oss);
144
145 FILTER fos(os);
146
147 fos.write(c1.data(), c1.length());
148 if (!c2.empty()) fos.write(c2.data(), c2.length());
149 if (!c3.empty()) fos.write(c3.data(), c3.length());
150 if (!c4.empty()) fos.write(c4.data(), c4.length());
151
152 VASSERT_EQ(number, expected, oss.str());
153 }
154
155 void testDotFilteredOutputStream()
156 {
157 typedef vmime::utility::dotFilteredOutputStream FILTER;
158
159 testFilteredOutputStreamHelper<FILTER>("1", "foo\n..bar", "foo\n.bar");
160 testFilteredOutputStreamHelper<FILTER>("2", "foo\n..bar", "foo\n", ".bar");
161 testFilteredOutputStreamHelper<FILTER>("3", "foo\n..bar", "foo", "\n.bar");
162 testFilteredOutputStreamHelper<FILTER>("4", "foo\n..bar", "foo", "\n", ".bar");
163 testFilteredOutputStreamHelper<FILTER>("5", "foo\n..bar", "foo", "\n", ".", "bar");
164
165 testFilteredOutputStreamHelper<FILTER>("6", "..\nfoobar", ".\nfoobar");
166 testFilteredOutputStreamHelper<FILTER>("7", "..\r\nfoobar", ".\r\nfoobar");
167 testFilteredOutputStreamHelper<FILTER>("8", "..\r\nfoobar", ".\r", "\nfoobar");
168 testFilteredOutputStreamHelper<FILTER>("9", ".foobar", ".foobar");
169 testFilteredOutputStreamHelper<FILTER>("10", ".foobar", ".", "foobar");
170 }
171
172 void testCRLFToLFFilteredOutputStream()
173 {
174 typedef vmime::utility::CRLFToLFFilteredOutputStream FILTER;
175
176 testFilteredOutputStreamHelper<FILTER>("1", "foo\nbar", "foo\r\nbar");
177 testFilteredOutputStreamHelper<FILTER>("2", "foo\nbar", "foo\r\n", "bar");
178 testFilteredOutputStreamHelper<FILTER>("3", "foo\nbar", "foo\r", "\nbar");
179 testFilteredOutputStreamHelper<FILTER>("4", "foo\nbar", "foo", "\r\nbar");
180 testFilteredOutputStreamHelper<FILTER>("5", "foo\nbar", "foo", "\r", "\nbar");
181 testFilteredOutputStreamHelper<FILTER>("6", "foo\nbar", "foo", "\r", "\n", "bar");
182 testFilteredOutputStreamHelper<FILTER>("7", "foo\nba\nr", "foo\r", "\nba\r\nr");
183 }
184
185 // stopSequenceFilteredInputStream
186
187 template <int N>
188 void testStopSequenceFISHelper
189 (const std::string& number, const std::string& sequence,
190 const std::string& expected, const std::string& c1,
191 const std::string& c2 = "", const std::string& c3 = "",
192 const std::string& c4 = "", const std::string& c5 = "")
193 {
194 chunkInputStream cis;
195 cis.addChunk(c1);
196 if (!c2.empty()) cis.addChunk(c2);
197 if (!c3.empty()) cis.addChunk(c3);
198 if (!c4.empty()) cis.addChunk(c4);
199 if (!c5.empty()) cis.addChunk(c5);
200
201 vmime::utility::stopSequenceFilteredInputStream <N> is(cis, sequence.data());
202
203 VASSERT_EQ(number, expected, readWhole(is));
204 }
205
206 void testStopSequenceFilteredInputStream1()
207 {
208 testStopSequenceFISHelper <1>("1", "x", "foo", "fooxbar");
209 testStopSequenceFISHelper <1>("2", "x", "foo", "foox", "bar");
210 testStopSequenceFISHelper <1>("3", "x", "foo", "foo", "x", "bar");
211 testStopSequenceFISHelper <1>("4", "x", "foo", "fo", "o", "x", "bar");
212 testStopSequenceFISHelper <1>("5", "x", "foo", "fo", "o", "x", "b", "ar");
213
214 testStopSequenceFISHelper <1>("6", "x", "foobar", "fo", "o", "b", "ar");
215 testStopSequenceFISHelper <1>("7", "x", "foobar", "foo", "bar");
216 testStopSequenceFISHelper <1>("8", "x", "foobar", "foo", "b", "ar");
217
218 testStopSequenceFISHelper <1>("9", "x", "foobar", "foobar");
219 testStopSequenceFISHelper <1>("10", "x", "foobar", "foobarx");
220
221 testStopSequenceFISHelper <1>("11", "x", "", "");
222 testStopSequenceFISHelper <1>("12", "x", "", "x");
223 testStopSequenceFISHelper <1>("13", "x", "", "", "x");
224 }
225
226 void testStopSequenceFilteredInputStreamN_2()
227 {
228 testStopSequenceFISHelper <2>("1", "xy", "foo", "fooxybar");
229 testStopSequenceFISHelper <2>("2", "xy", "foo", "foox", "ybar");
230 testStopSequenceFISHelper <2>("3", "xy", "foo", "foox", "y", "bar");
231 testStopSequenceFISHelper <2>("4", "xy", "foo", "foo", "x", "ybar");
232 testStopSequenceFISHelper <2>("5", "xy", "foo", "foo", "xy", "bar");
233 testStopSequenceFISHelper <2>("6", "xy", "foo", "foo", "x", "y", "bar");
234
235 testStopSequenceFISHelper <2>("7", "xy", "fooxbar", "foox", "bar");
236 testStopSequenceFISHelper <2>("8", "xy", "fooxbar", "foo", "xbar");
237 testStopSequenceFISHelper <2>("9", "xy", "fooxbar", "foo", "x", "bar");
238 testStopSequenceFISHelper <2>("10", "xy", "foobarx", "foo", "barx");
239
240 testStopSequenceFISHelper <2>("11", "xy", "foobar", "foobarxy");
241 testStopSequenceFISHelper <2>("12", "xy", "foobar", "foo", "barxy");
242 testStopSequenceFISHelper <2>("13", "xy", "foobar", "foo", "bar", "xy");
243
244 testStopSequenceFISHelper <2>("14", "xy", "", "");
245 testStopSequenceFISHelper <2>("15", "xy", "x", "x");
246 testStopSequenceFISHelper <2>("16", "xy", "", "xy");
247 testStopSequenceFISHelper <2>("17", "xy", "", "x", "y");
248 }
249
250 void testStopSequenceFilteredInputStreamN_3()
251 {
252 testStopSequenceFISHelper <3>("1", "xyz", "foo", "fooxyzbar");
253 testStopSequenceFISHelper <3>("2", "xyz", "foo", "foox", "yzbar");
254 testStopSequenceFISHelper <3>("3", "xyz", "foo", "foox", "y", "zbar");
255 testStopSequenceFISHelper <3>("4", "xyz", "foo", "foox", "yz", "bar");
256 testStopSequenceFISHelper <3>("5", "xyz", "foo", "foo", "xyz", "bar");
257 testStopSequenceFISHelper <3>("6", "xyz", "foo", "foo", "xy", "zbar");
258 testStopSequenceFISHelper <3>("7", "xyz", "foo", "foo", "x", "y", "zbar");
259 testStopSequenceFISHelper <3>("8", "xyz", "foo", "foo", "x", "y", "z", "bar");
260 testStopSequenceFISHelper <3>("9", "xyz", "foo", "fooxy", "z", "bar");
261
262 testStopSequenceFISHelper <3>("10", "xyz", "fooxybar", "foox", "y", "bar");
263 testStopSequenceFISHelper <3>("11", "xyz", "fooxybar", "fooxy", "bar");
264 testStopSequenceFISHelper <3>("12", "xyz", "fooxybar", "fo", "ox", "y", "bar");
265 testStopSequenceFISHelper <3>("13", "xyz", "fooxybar", "fo", "o", "x", "y", "bar");
266 testStopSequenceFISHelper <3>("14", "xyz", "fooxybar", "foo", "x", "ybar");
267 testStopSequenceFISHelper <3>("15", "xyz", "fooxybar", "foo", "xybar");
268
269 testStopSequenceFISHelper <3>("16", "xyz", "xfoxoxybxar", "xfoxo", "xybxar");
270 testStopSequenceFISHelper <3>("17", "xyz", "xfoxoxybxarx", "xfoxo", "xybxarx");
271 testStopSequenceFISHelper <3>("18", "xyz", "xfoxoxybxarxy", "xfoxo", "xybxarxy");
272
273 testStopSequenceFISHelper <3>("19", "xyz", "", "");
274 testStopSequenceFISHelper <3>("20", "xyz", "x", "x");
275 testStopSequenceFISHelper <3>("21", "xyz", "xy", "xy");
276 testStopSequenceFISHelper <3>("22", "xyz", "", "xyz");
277 testStopSequenceFISHelper <3>("23", "xyz", "", "x", "yz");
278 testStopSequenceFISHelper <3>("24", "xyz", "", "x", "y", "z");
279 }
280
281
282 // LFToCRLFFilteredOutputStream
283
284 void testLFToCRLFFilteredOutputStream_Global()
285 {
286 typedef vmime::utility::LFToCRLFFilteredOutputStream FILTER;
287
288 testFilteredOutputStreamHelper<FILTER>("1", "ABC\r\nDEF", "ABC\nDEF");
289 testFilteredOutputStreamHelper<FILTER>("2", "ABC\r\nDEF", "ABC\rDEF");
290 testFilteredOutputStreamHelper<FILTER>("3", "\r\n\r\nAB\r\n\r\nA\r\nB\r\n", "\n\nAB\n\nA\nB\n");
291 testFilteredOutputStreamHelper<FILTER>("4", "ABCDE\r\nF", "ABCDE\nF");
292 testFilteredOutputStreamHelper<FILTER>("5", "ABCDE\r\nF", "ABCDE\r\nF");
293 testFilteredOutputStreamHelper<FILTER>("6", "\r\n\r\n\r\n", "\n\n\n");
294 testFilteredOutputStreamHelper<FILTER>("7", "\r\n\r\n\r\n", "\r\r\n\n");
295 testFilteredOutputStreamHelper<FILTER>("8", "\r\n\r\n\r\n\r\n", "\r\r\r\r");
296 testFilteredOutputStreamHelper<FILTER>("9", "\r\n\r\n\r\n\r\n", "\n\n\n\n");
297 testFilteredOutputStreamHelper<FILTER>("10", "\r\n\r\n\r\n", "\r\n\n\n");
298 testFilteredOutputStreamHelper<FILTER>("11", "\r\n\r\n\r\n\r\n", "\n\n\n\r\n");
299 }
300
301 void testLFToCRLFFilteredOutputStream_Edge()
302 {
303 typedef vmime::utility::LFToCRLFFilteredOutputStream FILTER;
304
305 testFilteredOutputStreamHelper<FILTER>("1", "\r\n\r\n", "\r", "\r");
306 testFilteredOutputStreamHelper<FILTER>("2", "\r\n\r\n", "\r", "\n\r");
307 testFilteredOutputStreamHelper<FILTER>("3", "ABC\r\n\r\n", "ABC\r", "\n\r");
308 testFilteredOutputStreamHelper<FILTER>("4", "ABC\r\n\r\n\r\n", "ABC\r", "\n\r", "\n\n");
309 testFilteredOutputStreamHelper<FILTER>("5", "\r\n\r\n", "\n", "\n");
310 testFilteredOutputStreamHelper<FILTER>("6", "\r\n\r\n", "\r\n\r\n");
311 testFilteredOutputStreamHelper<FILTER>("7", "\r\n\r\n", "\r\n\r", "\n");
312
313 testFilteredOutputStreamHelper<FILTER>("8", "A\r\nB\r\nC\r\nD", "A\rB", "\nC\r\nD");
314 testFilteredOutputStreamHelper<FILTER>("9", "\r\nA\r\nB\r\nC\r\nD", "\rA\r", "B\nC\r\nD");
315 testFilteredOutputStreamHelper<FILTER>("10", "\r\nA\r\nB\r\nC\r\nD", "\nA\r", "B\nC\r\nD");
316 testFilteredOutputStreamHelper<FILTER>("11", "\r\nA\r\nB\r\nC\r\nD\r\n", "\nA\rB", "\nC\r\nD\r");
317 }
318
319 VMIME_TEST_SUITE_END
320