forked from ReneNyffenegger/cpp-base64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
266 lines (214 loc) · 9.03 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "base64.h"
#include <iostream>
int main() {
bool all_tests_passed = true;
//
// Note: this file (test.cpp) must be encoded in UTF-8
// for the following test, otherwise, the test item
// fails.
//
const std::string orig =
"René Nyffenegger\n"
"http://www.renenyffenegger.ch\n"
"passion for data\n";
std::string encoded = base64_encode(reinterpret_cast<const unsigned char*>(orig.c_str()), orig.length());
std::string decoded = base64_decode(encoded);
if (encoded != "UmVuw6kgTnlmZmVuZWdnZXIKaHR0cDovL3d3dy5yZW5lbnlmZmVuZWdnZXIuY2gKcGFzc2lvbiBmb3IgZGF0YQo=") {
std::cout << "Encoding is wrong" << std::endl;
all_tests_passed = false;
}
if (decoded != orig) {
std::cout << "decoded != orig" << std::endl;
all_tests_passed = false;
}
// Test all possibilites of fill bytes (none, one =, two ==)
// References calculated with: https://www.base64encode.org/
std::string rest0_original = "abc";
std::string rest0_reference = "YWJj";
std::string rest0_encoded = base64_encode(reinterpret_cast<const unsigned char*>(rest0_original.c_str()),
rest0_original.length());
std::string rest0_decoded = base64_decode(rest0_encoded);
if (rest0_decoded != rest0_original) {
std::cout << "rest0_decoded != rest0_original" << std::endl;
all_tests_passed = false;
}
if (rest0_reference != rest0_encoded) {
std::cout << "rest0_reference != rest0_encoded" << std::endl;
all_tests_passed = false;
}
// std::cout << "encoded: " << rest0_encoded << std::endl;
// std::cout << "reference: " << rest0_reference << std::endl;
// std::cout << "decoded: " << rest0_decoded << std::endl << std::endl;
std::string rest1_original = "abcd";
std::string rest1_reference = "YWJjZA==";
std::string rest1_encoded = base64_encode(reinterpret_cast<const unsigned char*>(rest1_original.c_str()),
rest1_original.length());
std::string rest1_decoded = base64_decode(rest1_encoded);
if (rest1_decoded != rest1_original) {
std::cout << "rest1_decoded != rest1_original" << std::endl;
all_tests_passed = false;
}
if (rest1_reference != rest1_encoded) {
std::cout << "rest1_reference != rest1_encoded" << std::endl;
all_tests_passed = false;
}
// std::cout << "encoded: " << rest1_encoded << std::endl;
// std::cout << "reference: " << rest1_reference << std::endl;
// std::cout << "decoded: " << rest1_decoded << std::endl << std::endl;
std::string rest2_original = "abcde";
std::string rest2_reference = "YWJjZGU=";
std::string rest2_encoded = base64_encode(reinterpret_cast<const unsigned char*>(rest2_original.c_str()),
rest2_original.length());
std::string rest2_decoded = base64_decode(rest2_encoded);
if (rest2_decoded != rest2_original) {
std::cout << "rest2_decoded != rest2_original" << std::endl;
all_tests_passed = false;
}
if (rest2_reference != rest2_encoded) {
std::cout << "rest2_reference != rest2_encoded" << std::endl;
all_tests_passed = false;
}
// std::cout << "encoded: " << rest2_encoded << std::endl;
// std::cout << "reference: " << rest2_reference << std::endl;
// std::cout << "decoded: " << rest2_decoded << std::endl << std::endl;
//
// --------------------------------------------------------------
//
// Data that is 17 bytes long requires one padding byte when
// base-64 encoded. Such an encoded string could not correctly
// be decoded when encoded with «url semantics». This bug
// was discovered by https://github.com/kosniaz. The following
// test checks if this bug was fixed:
//
std::string a17_orig = "aaaaaaaaaaaaaaaaa";
std::string a17_encoded = base64_encode(a17_orig, false);
std::string a17_encoded_url = base64_encode(a17_orig, true );
if (a17_encoded != "YWFhYWFhYWFhYWFhYWFhYWE=") {
std::cout << "Failed to encode a17" << std::endl;
all_tests_passed = false;
}
if (a17_encoded_url != "YWFhYWFhYWFhYWFhYWFhYWE.") {
std::cout << "Failed to encode a17 (url)" << std::endl;
all_tests_passed = false;
}
if (base64_decode(a17_encoded_url) != a17_orig) {
std::cout << "Failed to decode a17 (url)" << std::endl;
all_tests_passed = false;
}
if (base64_decode(a17_encoded ) != a17_orig) {
std::cout << "Failed to decode a17 (no url)" << std::endl;
all_tests_passed = false;
}
// --------------------------------------------------------------
// characters 63 and 64 / URL encoding
std::string s_6364 = "\x03" "\xef" "\xff" "\xf9";
std::string s_6364_encoded = base64_encode(s_6364, false);
std::string s_6364_encoded_url = base64_encode(s_6364, true );
if (s_6364_encoded != "A+//+Q==") {
std::cout << "Failed to encode_6364 (no url)" << std::endl;
all_tests_passed = false;
}
if (s_6364_encoded_url!= "A-__-Q..") {
std::cout << "Failed to encode_6364 (url)" << std::endl;
all_tests_passed = false;
}
if (base64_decode(s_6364_encoded ) != s_6364) {
std::cout << "Failed to decode s_6364_encoded" << std::endl;
all_tests_passed = false;
}
if (base64_decode(s_6364_encoded_url) != s_6364) {
std::cout << "Failed to decode s_6364_encoded_url" << std::endl;
all_tests_passed = false;
}
std::string numbers =
"one two three four five six seven eight nine ten eleven twelve "
"thirteen fourteen fivteen sixteen seventeen eighteen nineteen "
"twenty twenty-one";
std::string encoded_mime = base64_encode_mime(numbers);
std::string encoded_mime_expeced =
"b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVuIGVsZXZlbiB0\n"
"d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVuIHNldmVudGVlbiBlaWdodGVl\n"
"biBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
if (encoded_mime != encoded_mime_expeced) {
std::cout << "Failed: base64_encode_mime s_6364_encoded" << std::endl;
all_tests_passed = false;
}
//
// Set 2nd parameter remove_linebreaks to true in order decode a
// mime encoded string:
//
std::string decoded_mime = base64_decode(encoded_mime, true);
if (decoded_mime != numbers) {
std::cout << "Failed: base64_decode(..., true)" << std::endl;
all_tests_passed = false;
}
// ----------------------------------------------
std::string unpadded_input = "YWJjZGVmZw"; // Note the 'missing' "=="
std::string unpadded_decoded = base64_decode(unpadded_input);
if (unpadded_decoded != "abcdefg") {
std::cout << "Failed to decode unpadded input " << unpadded_input << std::endl;
all_tests_passed = false;
}
unpadded_input = "YWJjZGU"; // Note the 'missing' "="
unpadded_decoded = base64_decode(unpadded_input);
if (unpadded_decoded != "abcde") {
std::cout << "Failed to decode unpadded input " << unpadded_input << std::endl;
std::cout << unpadded_decoded << std::endl;
all_tests_passed = false;
}
unpadded_input = "";
unpadded_decoded = base64_decode(unpadded_input);
if (unpadded_decoded != "") {
std::cout << "Failed to decode unpadded input " << unpadded_input << std::endl;
std::cout << unpadded_decoded << std::endl;
all_tests_passed = false;
}
unpadded_input = "YQ";
unpadded_decoded = base64_decode(unpadded_input);
if (unpadded_decoded != "a") {
std::cout << "Failed to decode unpadded input " << unpadded_input << std::endl;
std::cout << unpadded_decoded << std::endl;
all_tests_passed = false;
}
unpadded_input = "YWI";
unpadded_decoded = base64_decode(unpadded_input);
if (unpadded_decoded != "ab") {
std::cout << "Failed to decode unpadded input " << unpadded_input << std::endl;
std::cout << unpadded_decoded << std::endl;
all_tests_passed = false;
}
// --------------------------------------------------------------
//
// 2022-11-01
// Replace
// encoded_string[…] with encoded_sring.at(…)
// in
// decode()
//
try {
std::string not_null_terminated = std::string(1, 'a');
std::string not_null_decoded = base64_decode(not_null_terminated);
std::cout << "Expected a std::out_of_range exception" << std::endl;
all_tests_passed = false;
}
catch (std::out_of_range const&) {}
// --------------------------------------------------------------
#if __cplusplus >= 201703L
//
// Test the string_view interface (which required C++17)
//
std::string_view sv_orig = "foobarbaz";
std::string_view sv_encoded = base64_encode(sv_orig);
if (sv_encoded != "Zm9vYmFyYmF6") {
std::cout << "Failed to encode with string_view" << std::endl;
all_tests_passed = false;
}
std::string_view sv_decoded = base64_decode(sv_encoded);
if (sv_decoded != sv_orig) {
std::cout << "Failed to decode with string_view" << std::endl;
all_tests_passed = false;
}
#endif
if (all_tests_passed) return 0;
return 1;
}