1//
2// Base64Test.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "Base64Test.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Base64Encoder.h"
15#include "Poco/Base64Decoder.h"
16#include "Poco/Exception.h"
17#include <sstream>
18
19
20using Poco::Base64Encoder;
21using Poco::Base64Decoder;
22using Poco::DataFormatException;
23
24
25Base64Test::Base64Test(const std::string& rName): CppUnit::TestCase(rName)
26{
27}
28
29
30Base64Test::~Base64Test()
31{
32}
33
34
35void Base64Test::testEncoder()
36{
37 {
38 std::ostringstream str;
39 Base64Encoder encoder(str);
40 encoder << std::string("\00\01\02\03\04\05", 6);
41 encoder.close();
42 assertTrue (str.str() == "AAECAwQF");
43 }
44 {
45 std::ostringstream str;
46 Base64Encoder encoder(str);
47 encoder << std::string("\00\01\02\03", 4);
48 encoder.close();
49 assertTrue (str.str() == "AAECAw==");
50 }
51 {
52 std::ostringstream str;
53 Base64Encoder encoder(str);
54 encoder << "ABCDEF";
55 encoder.close();
56 assertTrue (str.str() == "QUJDREVG");
57 }
58 {
59 std::ostringstream str;
60 Base64Encoder encoder(str);
61 encoder << "!@#$%^&*()_~<>";
62 encoder.close();
63 assertTrue (str.str() == "IUAjJCVeJiooKV9+PD4=");
64 }
65}
66
67
68void Base64Test::testEncoderURL()
69{
70 {
71 std::ostringstream str;
72 Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING);
73 encoder << std::string("\00\01\02\03\04\05", 6);
74 encoder.close();
75 assertTrue (str.str() == "AAECAwQF");
76 }
77 {
78 std::ostringstream str;
79 Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING);
80 encoder << std::string("\00\01\02\03", 4);
81 encoder.close();
82 assertTrue (str.str() == "AAECAw==");
83 }
84 {
85 std::ostringstream str;
86 Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING);
87 encoder << "ABCDEF";
88 encoder.close();
89 assertTrue (str.str() == "QUJDREVG");
90 }
91 {
92 std::ostringstream str;
93 Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING);
94 encoder << "!@#$%^&*()_~<>";
95 encoder.close();
96 assertTrue (str.str() == "IUAjJCVeJiooKV9-PD4=");
97 }
98}
99
100
101void Base64Test::testEncoderNoPadding()
102{
103 {
104 std::ostringstream str;
105 Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
106 encoder << std::string("\00\01\02\03\04\05", 6);
107 encoder.close();
108 assertTrue (str.str() == "AAECAwQF");
109 }
110 {
111 std::ostringstream str;
112 Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
113 encoder << std::string("\00\01\02\03", 4);
114 encoder.close();
115 assertTrue (str.str() == "AAECAw");
116 }
117 {
118 std::ostringstream str;
119 Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
120 encoder << "ABCDEF";
121 encoder.close();
122 assertTrue (str.str() == "QUJDREVG");
123 }
124 {
125 std::ostringstream str;
126 Base64Encoder encoder(str, Poco::BASE64_URL_ENCODING | Poco::BASE64_NO_PADDING);
127 encoder << "!@#$%^&*()_~<>";
128 encoder.close();
129 assertTrue (str.str() == "IUAjJCVeJiooKV9-PD4");
130 }
131}
132
133
134void Base64Test::testDecoder()
135{
136 {
137 std::istringstream istr("AAECAwQF");
138 Base64Decoder decoder(istr);
139 assertTrue (decoder.good() && decoder.get() == 0);
140 assertTrue (decoder.good() && decoder.get() == 1);
141 assertTrue (decoder.good() && decoder.get() == 2);
142 assertTrue (decoder.good() && decoder.get() == 3);
143 assertTrue (decoder.good() && decoder.get() == 4);
144 assertTrue (decoder.good() && decoder.get() == 5);
145 assertTrue (decoder.good() && decoder.get() == -1);
146 }
147 {
148 std::istringstream istr("AAECAwQ=");
149 Base64Decoder decoder(istr);
150 assertTrue (decoder.good() && decoder.get() == 0);
151 assertTrue (decoder.good() && decoder.get() == 1);
152 assertTrue (decoder.good() && decoder.get() == 2);
153 assertTrue (decoder.good() && decoder.get() == 3);
154 assertTrue (decoder.good() && decoder.get() == 4);
155 assertTrue (decoder.good() && decoder.get() == -1);
156 }
157 {
158 std::istringstream istr("AAECAw==");
159 Base64Decoder decoder(istr);
160 assertTrue (decoder.good() && decoder.get() == 0);
161 assertTrue (decoder.good() && decoder.get() == 1);
162 assertTrue (decoder.good() && decoder.get() == 2);
163 assertTrue (decoder.good() && decoder.get() == 3);
164 assertTrue (decoder.good() && decoder.get() == -1);
165 }
166 {
167 std::istringstream istr("QUJDREVG");
168 Base64Decoder decoder(istr);
169 std::string s;
170 decoder >> s;
171 assertTrue (s == "ABCDEF");
172 assertTrue (decoder.eof());
173 assertTrue (!decoder.fail());
174 }
175 {
176 std::istringstream istr("QUJ\r\nDRE\r\nVG");
177 Base64Decoder decoder(istr);
178 std::string s;
179 decoder >> s;
180 assertTrue (s == "ABCDEF");
181 assertTrue (decoder.eof());
182 assertTrue (!decoder.fail());
183 }
184 {
185 std::istringstream istr("QUJD#REVG");
186 Base64Decoder decoder(istr);
187 std::string s;
188 try
189 {
190 decoder >> s;
191 assertTrue (decoder.bad());
192 }
193 catch (DataFormatException&)
194 {
195 }
196 assertTrue (!decoder.eof());
197 }
198}
199
200
201void Base64Test::testDecoderURL()
202{
203 {
204 std::istringstream istr("AAECAwQF");
205 Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
206 assertTrue (decoder.good() && decoder.get() == 0);
207 assertTrue (decoder.good() && decoder.get() == 1);
208 assertTrue (decoder.good() && decoder.get() == 2);
209 assertTrue (decoder.good() && decoder.get() == 3);
210 assertTrue (decoder.good() && decoder.get() == 4);
211 assertTrue (decoder.good() && decoder.get() == 5);
212 assertTrue (decoder.good() && decoder.get() == -1);
213 }
214 {
215 std::istringstream istr("AAECAwQ=");
216 Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
217 assertTrue (decoder.good() && decoder.get() == 0);
218 assertTrue (decoder.good() && decoder.get() == 1);
219 assertTrue (decoder.good() && decoder.get() == 2);
220 assertTrue (decoder.good() && decoder.get() == 3);
221 assertTrue (decoder.good() && decoder.get() == 4);
222 assertTrue (decoder.good() && decoder.get() == -1);
223 }
224 {
225 std::istringstream istr("AAECAw==");
226 Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
227 assertTrue (decoder.good() && decoder.get() == 0);
228 assertTrue (decoder.good() && decoder.get() == 1);
229 assertTrue (decoder.good() && decoder.get() == 2);
230 assertTrue (decoder.good() && decoder.get() == 3);
231 assertTrue (decoder.good() && decoder.get() == -1);
232 }
233 {
234 std::istringstream istr("QUJDREVG");
235 Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
236 std::string s;
237 decoder >> s;
238 assertTrue (s == "ABCDEF");
239 assertTrue (decoder.eof());
240 assertTrue (!decoder.fail());
241 }
242 {
243 std::istringstream istr("QUJ\r\nDRE\r\nVG");
244 Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
245 try
246 {
247 std::string s;
248 decoder >> s;
249 assertTrue (decoder.bad());
250 }
251 catch (DataFormatException&)
252 {
253 }
254 }
255 {
256 std::istringstream istr("QUJD#REVG");
257 Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
258 std::string s;
259 try
260 {
261 decoder >> s;
262 assertTrue (decoder.bad());
263 }
264 catch (DataFormatException&)
265 {
266 }
267 assertTrue (!decoder.eof());
268 }
269 {
270 std::istringstream istr("IUAjJCVeJiooKV9-PD4=");
271 Base64Decoder decoder(istr, Poco::BASE64_URL_ENCODING);
272 std::string s;
273 decoder >> s;
274 assertTrue (s == "!@#$%^&*()_~<>");
275 assertTrue (decoder.eof());
276 assertTrue (!decoder.fail());
277 }
278}
279
280
281void Base64Test::testDecoderNoPadding()
282{
283 {
284 std::istringstream istr("AAECAwQF");
285 Base64Decoder decoder(istr, Poco::BASE64_NO_PADDING);
286 assertTrue (decoder.good() && decoder.get() == 0);
287 assertTrue (decoder.good() && decoder.get() == 1);
288 assertTrue (decoder.good() && decoder.get() == 2);
289 assertTrue (decoder.good() && decoder.get() == 3);
290 assertTrue (decoder.good() && decoder.get() == 4);
291 assertTrue (decoder.good() && decoder.get() == 5);
292 assertTrue (decoder.good() && decoder.get() == -1);
293 }
294 {
295 std::istringstream istr("AAECAwQ");
296 Base64Decoder decoder(istr, Poco::BASE64_NO_PADDING);
297 assertTrue (decoder.good() && decoder.get() == 0);
298 assertTrue (decoder.good() && decoder.get() == 1);
299 assertTrue (decoder.good() && decoder.get() == 2);
300 assertTrue (decoder.good() && decoder.get() == 3);
301 assertTrue (decoder.good() && decoder.get() == 4);
302 assertTrue (decoder.good() && decoder.get() == -1);
303 }
304 {
305 std::istringstream istr("AAECAw");
306 Base64Decoder decoder(istr, Poco::BASE64_NO_PADDING);
307 assertTrue (decoder.good() && decoder.get() == 0);
308 assertTrue (decoder.good() && decoder.get() == 1);
309 assertTrue (decoder.good() && decoder.get() == 2);
310 assertTrue (decoder.good() && decoder.get() == 3);
311 assertTrue (decoder.good() && decoder.get() == -1);
312 }
313}
314
315
316void Base64Test::testEncodeDecode()
317{
318 {
319 std::stringstream str;
320 Base64Encoder encoder(str);
321 encoder << "The quick brown fox ";
322 encoder << "jumped over the lazy dog.";
323 encoder.close();
324 Base64Decoder decoder(str);
325 std::string s;
326 int c = decoder.get();
327 while (c != -1) { s += char(c); c = decoder.get(); }
328 assertTrue (s == "The quick brown fox jumped over the lazy dog.");
329 }
330 {
331 std::string src;
332 for (int i = 0; i < 255; ++i) src += char(i);
333 std::stringstream str;
334 Base64Encoder encoder(str);
335 encoder.write(src.data(), (std::streamsize) src.size());
336 encoder.close();
337 Base64Decoder decoder(str);
338 std::string s;
339 int c = decoder.get();
340 while (c != -1) { s += char(c); c = decoder.get(); }
341 assertTrue (s == src);
342 }
343}
344
345
346void Base64Test::setUp()
347{
348}
349
350
351void Base64Test::tearDown()
352{
353}
354
355
356CppUnit::Test* Base64Test::suite()
357{
358 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("Base64Test");
359
360 CppUnit_addTest(pSuite, Base64Test, testEncoder);
361 CppUnit_addTest(pSuite, Base64Test, testEncoderURL);
362 CppUnit_addTest(pSuite, Base64Test, testEncoderNoPadding);
363 CppUnit_addTest(pSuite, Base64Test, testDecoder);
364 CppUnit_addTest(pSuite, Base64Test, testDecoderURL);
365 CppUnit_addTest(pSuite, Base64Test, testDecoderNoPadding);
366 CppUnit_addTest(pSuite, Base64Test, testEncodeDecode);
367
368 return pSuite;
369}
370