1 | // |
2 | // Base32Test.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 "Base32Test.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Base32Encoder.h" |
15 | #include "Poco/Base32Decoder.h" |
16 | #include "Poco/Exception.h" |
17 | #include <sstream> |
18 | |
19 | |
20 | using Poco::Base32Encoder; |
21 | using Poco::Base32Decoder; |
22 | using Poco::DataFormatException; |
23 | |
24 | |
25 | Base32Test::Base32Test(const std::string& rName): CppUnit::TestCase(rName) |
26 | { |
27 | } |
28 | |
29 | |
30 | Base32Test::~Base32Test() |
31 | { |
32 | } |
33 | |
34 | |
35 | void Base32Test::testEncoder() |
36 | { |
37 | { |
38 | std::ostringstream str; |
39 | Base32Encoder encoder(str); |
40 | encoder << std::string("\00\01\02\03\04\05" , 6); |
41 | encoder.close(); |
42 | assertTrue (str.str() == "AAAQEAYEAU======" ); |
43 | } |
44 | { |
45 | std::ostringstream str; |
46 | Base32Encoder encoder(str); |
47 | encoder << std::string("\00\01\02\03" , 4); |
48 | encoder.close(); |
49 | assertTrue (str.str() == "AAAQEAY=" ); |
50 | } |
51 | { |
52 | std::ostringstream str; |
53 | Base32Encoder encoder(str, false); |
54 | encoder << "ABCDEF" ; |
55 | encoder.close(); |
56 | assertTrue (str.str() == "IFBEGRCFIY" ); |
57 | } |
58 | { |
59 | std::ostringstream str; |
60 | Base32Encoder encoder(str); |
61 | encoder << "ABCDEF" ; |
62 | encoder.close(); |
63 | assertTrue (str.str() == "IFBEGRCFIY======" ); |
64 | } |
65 | { |
66 | std::ostringstream str; |
67 | Base32Encoder encoder(str); |
68 | encoder << "ABCDE" ; |
69 | encoder.close(); |
70 | assertTrue (str.str() == "IFBEGRCF" ); |
71 | } |
72 | } |
73 | |
74 | |
75 | void Base32Test::testDecoder() |
76 | { |
77 | { |
78 | std::istringstream istr("AAAQEAYEAU======" ); |
79 | Base32Decoder decoder(istr); |
80 | assertTrue (decoder.good() && decoder.get() == 0); |
81 | assertTrue (decoder.good() && decoder.get() == 1); |
82 | assertTrue (decoder.good() && decoder.get() == 2); |
83 | assertTrue (decoder.good() && decoder.get() == 3); |
84 | assertTrue (decoder.good() && decoder.get() == 4); |
85 | assertTrue (decoder.good() && decoder.get() == 5); |
86 | assertTrue (decoder.good() && decoder.get() == -1); |
87 | } |
88 | { |
89 | std::istringstream istr("AAAQEAYE" ); |
90 | Base32Decoder decoder(istr); |
91 | assertTrue (decoder.good() && decoder.get() == 0); |
92 | assertTrue (decoder.good() && decoder.get() == 1); |
93 | assertTrue (decoder.good() && decoder.get() == 2); |
94 | assertTrue (decoder.good() && decoder.get() == 3); |
95 | assertTrue (decoder.good() && decoder.get() == 4); |
96 | assertTrue (decoder.good() && decoder.get() == -1); |
97 | } |
98 | { |
99 | std::istringstream istr("AAAQEAY=" ); |
100 | Base32Decoder decoder(istr); |
101 | assertTrue (decoder.good() && decoder.get() == 0); |
102 | assertTrue (decoder.good() && decoder.get() == 1); |
103 | assertTrue (decoder.good() && decoder.get() == 2); |
104 | assertTrue (decoder.good() && decoder.get() == 3); |
105 | assertTrue (decoder.good() && decoder.get() == -1); |
106 | } |
107 | { |
108 | std::istringstream istr("IFBEGRCFIY======" ); |
109 | Base32Decoder decoder(istr); |
110 | std::string s; |
111 | decoder >> s; |
112 | assertTrue (s == "ABCDEF" ); |
113 | assertTrue (decoder.eof()); |
114 | assertTrue (!decoder.fail()); |
115 | } |
116 | { |
117 | std::istringstream istr("QUJD#REVG" ); |
118 | Base32Decoder decoder(istr); |
119 | std::string s; |
120 | try |
121 | { |
122 | decoder >> s; |
123 | assertTrue (decoder.bad()); |
124 | } |
125 | catch (DataFormatException&) |
126 | { |
127 | } |
128 | assertTrue (!decoder.eof()); |
129 | } |
130 | } |
131 | |
132 | |
133 | void Base32Test::testEncodeDecode() |
134 | { |
135 | { |
136 | std::stringstream str; |
137 | Base32Encoder encoder(str); |
138 | encoder << "The quick brown fox " ; |
139 | encoder << "jumped over the lazy dog." ; |
140 | encoder.close(); |
141 | Base32Decoder decoder(str); |
142 | std::string s; |
143 | int c = decoder.get(); |
144 | while (c != -1) { s += char(c); c = decoder.get(); } |
145 | assertTrue (s == "The quick brown fox jumped over the lazy dog." ); |
146 | } |
147 | { |
148 | std::string src; |
149 | for (int i = 0; i < 255; ++i) src += char(i); |
150 | std::stringstream str; |
151 | Base32Encoder encoder(str); |
152 | encoder.write(src.data(), (std::streamsize) src.size()); |
153 | encoder.close(); |
154 | Base32Decoder decoder(str); |
155 | std::string s; |
156 | int c = decoder.get(); |
157 | while (c != -1) { s += char(c); c = decoder.get(); } |
158 | assertTrue (s == src); |
159 | } |
160 | } |
161 | |
162 | |
163 | void Base32Test::setUp() |
164 | { |
165 | } |
166 | |
167 | |
168 | void Base32Test::tearDown() |
169 | { |
170 | } |
171 | |
172 | |
173 | CppUnit::Test* Base32Test::suite() |
174 | { |
175 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("Base32Test" ); |
176 | |
177 | CppUnit_addTest(pSuite, Base32Test, testEncoder); |
178 | CppUnit_addTest(pSuite, Base32Test, testDecoder); |
179 | CppUnit_addTest(pSuite, Base32Test, testEncodeDecode); |
180 | |
181 | return pSuite; |
182 | } |
183 | |