comparison 3rdparty/vmime/tests/parser/streamContentHandlerTest.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/outputStreamAdapter.hpp"
27
28
29 VMIME_TEST_SUITE_BEGIN(streamContentHandlerTest)
30
31 VMIME_TEST_LIST_BEGIN
32 VMIME_TEST(testIsEmpty)
33 VMIME_TEST(testGetLength)
34 VMIME_TEST(testIsEncoded)
35 VMIME_TEST(testGetLength_Encoded)
36 VMIME_TEST(testExtract)
37 VMIME_TEST(testExtract_Encoded)
38 VMIME_TEST(testExtractRaw_Encoded)
39 VMIME_TEST(testGenerate)
40 VMIME_TEST(testGenerate_Encoded)
41 VMIME_TEST_LIST_END
42
43
44 void testIsEmpty()
45 {
46 vmime::streamContentHandler cth;
47
48 VASSERT_TRUE("empty", cth.isEmpty());
49 }
50
51 void testGetLength()
52 {
53 vmime::string data("Test Data");
54 vmime::shared_ptr <vmime::utility::inputStream> stream =
55 vmime::make_shared <vmime::utility::inputStreamStringAdapter>(data);
56
57 vmime::streamContentHandler cth(stream, data.length());
58
59 VASSERT_FALSE("empty", cth.isEmpty());
60 VASSERT_EQ("length", 9, cth.getLength());
61 }
62
63 void testIsEncoded()
64 {
65 vmime::string data("Test Data");
66 vmime::shared_ptr <vmime::utility::inputStream> stream =
67 vmime::make_shared <vmime::utility::inputStreamStringAdapter>(data);
68
69 vmime::streamContentHandler cth(stream, data.length());
70
71 VASSERT_FALSE("encoded", cth.isEncoded());
72 VASSERT_EQ("encoding", vmime::contentHandler::NO_ENCODING, cth.getEncoding());
73
74
75 vmime::string data2("Zm9vEjRWYmFy=");
76 vmime::shared_ptr <vmime::utility::inputStream> stream2 =
77 vmime::make_shared <vmime::utility::inputStreamStringAdapter>(data2);
78
79 vmime::streamContentHandler cth2(stream2, data2.length(), vmime::encoding("base64"));
80
81 VASSERT_TRUE("encoded", cth2.isEncoded());
82 VASSERT_EQ("encoding", "base64", cth2.getEncoding().generate());
83 }
84
85 void testGetLength_Encoded()
86 {
87 vmime::string data("foo=12=34=56bar");
88 vmime::shared_ptr <vmime::utility::inputStream> stream =
89 vmime::make_shared <vmime::utility::inputStreamStringAdapter>(data);
90
91 vmime::streamContentHandler cth(stream, data.length(), vmime::encoding("quoted-printable"));
92
93 // Reported length should be the length of encoded data
94 VASSERT_EQ("length", 15, cth.getLength());
95 }
96
97 void testExtract()
98 {
99 vmime::string data("Test Data");
100 vmime::shared_ptr <vmime::utility::inputStream> stream =
101 vmime::make_shared <vmime::utility::inputStreamStringAdapter>(data);
102
103 vmime::streamContentHandler cth(stream, data.length());
104
105 std::ostringstream oss;
106 vmime::utility::outputStreamAdapter osa(oss);
107
108 cth.extract(osa);
109
110 VASSERT_EQ("extract", "Test Data", oss.str());
111 }
112
113 void testExtract_Encoded()
114 {
115 vmime::string data
116 ("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODk=");
117 vmime::shared_ptr <vmime::utility::inputStream> stream =
118 vmime::make_shared <vmime::utility::inputStreamStringAdapter>(data);
119
120 vmime::streamContentHandler cth(stream, data.length(), vmime::encoding("base64"));
121
122 std::ostringstream oss;
123 vmime::utility::outputStreamAdapter osa(oss);
124
125 cth.extract(osa);
126
127 // Data should be decoded from B64
128 VASSERT_EQ("extract",
129 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", oss.str());
130 }
131
132 void testExtractRaw_Encoded()
133 {
134 vmime::string data
135 ("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODk=");
136 vmime::shared_ptr <vmime::utility::inputStream> stream =
137 vmime::make_shared <vmime::utility::inputStreamStringAdapter>(data);
138
139 vmime::streamContentHandler cth(stream, data.length(), vmime::encoding("base64"));
140
141 std::ostringstream oss;
142 vmime::utility::outputStreamAdapter osa(oss);
143
144 cth.extractRaw(osa);
145
146 // Data should not be decoded
147 VASSERT_EQ("extractRaw",
148 "QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODk=", oss.str());
149 }
150
151 void testGenerate()
152 {
153 vmime::string data("foo\x12\x34\x56 bar");
154 vmime::shared_ptr <vmime::utility::inputStream> stream =
155 vmime::make_shared <vmime::utility::inputStreamStringAdapter>(data);
156
157 vmime::streamContentHandler cth(stream, data.length());
158
159 std::ostringstream oss;
160 vmime::utility::outputStreamAdapter osa(oss);
161
162 cth.generate(osa, vmime::encoding("base64"));
163
164 // Data should be encoded to B64
165 VASSERT_EQ("generate", "Zm9vEjRWIGJhcg==", oss.str());
166 }
167
168 void testGenerate_Encoded()
169 {
170 vmime::string data("foo=12=34=56bar");
171 vmime::shared_ptr <vmime::utility::inputStream> stream =
172 vmime::make_shared <vmime::utility::inputStreamStringAdapter>(data);
173
174 vmime::streamContentHandler cth(stream, data.length(), vmime::encoding("quoted-printable"));
175
176 std::ostringstream oss;
177 vmime::utility::outputStreamAdapter osa(oss);
178
179 cth.generate(osa, vmime::encoding("base64"));
180
181 // Data should be reencoded from QP to B64
182 VASSERT_EQ("generate", "Zm9vEjRWYmFy", oss.str());
183 }
184
185 VMIME_TEST_SUITE_END