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