Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
test_codec_base01.cpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <cassert>
25#include <cinttypes>
26#include <cstring>
27
29
30#include <jau/base_codec.hpp>
31
32using namespace jau::int_literals;
33
35 const int base = aspec.base();
36 REQUIRE( 1 < base );
37
38 const char min_cp = aspec[0]; // minimum code-point
39 const char max_cp = aspec[base-1]; // maximum code-point
40
41 const int min = (int)jau::codec::base::decode(std::string()+min_cp, aspec);
42 const int max = (int)jau::codec::base::decode(std::string()+max_cp+max_cp+max_cp, aspec);
43 const int max_s = (int)jau::codec::base::decode(std::string()+max_cp, aspec);
44
45 const double machine_epsilon = std::numeric_limits<double>::epsilon();
46 REQUIRE(0 == min);
47 REQUIRE(base-1 == max_s);
48 REQUIRE( std::abs( std::pow(base, 3)-1 - max ) <= machine_epsilon );
49
50 const std::string r1_min = jau::codec::base::encode(0, aspec, 3);
51 const std::string r1_min_s = jau::codec::base::encode(0, aspec);
52 REQUIRE(std::string()+min_cp+min_cp+min_cp == r1_min);
53 REQUIRE(std::string()+min_cp == r1_min_s);
54
55 const std::string r1_max = jau::codec::base::encode(base-1, aspec, 3);
56 const std::string r1_max_s = jau::codec::base::encode(base-1, aspec);
57 REQUIRE(std::string()+min_cp+min_cp+max_cp == r1_max);
58 REQUIRE(std::string()+max_cp == r1_max_s);
59
60 const std::string r3_max = jau::codec::base::encode((int)std::pow(base, 3)-1, aspec, 3);
61 REQUIRE(std::string()+max_cp+max_cp+max_cp == r3_max);
62
63 fprintf(stderr, "Test32Bit base %d, %s: [%d .. %d] <-> ['%s' .. '%s'], %d years (max/365d) \n",
64 base, aspec.to_string().c_str(), min, max, jau::codec::base::encode(min, aspec).c_str(), jau::codec::base::encode(max, aspec).c_str(), (max/365));
65
66 REQUIRE(0 == jau::codec::base::decode(std::string()+min_cp+min_cp+min_cp, aspec));
67 REQUIRE(std::string()+min_cp == jau::codec::base::encode(0, aspec));
68 REQUIRE(std::string()+min_cp+min_cp+min_cp == jau::codec::base::encode(0, aspec, 3));
69
70 REQUIRE(max == jau::codec::base::decode(std::string()+max_cp+max_cp+max_cp, aspec));
71 REQUIRE(std::string()+max_cp+max_cp+max_cp == jau::codec::base::encode(max, aspec, 3));
72 REQUIRE(max_s == jau::codec::base::decode(std::string()+max_cp, aspec));
73 REQUIRE(std::string()+min_cp+min_cp+max_cp == jau::codec::base::encode(max_s, aspec, 3));
74
75 {
76 const int64_t v0_d = jau::codec::base::decode(r1_max, aspec);
77 const std::string v1_s = jau::codec::base::encode(base-1, aspec, 3);
78 REQUIRE(r1_max == v1_s);
79 REQUIRE(base-1 == v0_d);
80 }
81 {
82 const int64_t v0_d = jau::codec::base::decode(r3_max, aspec);
83 const std::string v1_s = jau::codec::base::encode(max, aspec, 3);
84 REQUIRE(r3_max == v1_s);
85 REQUIRE(max == v0_d);
86 }
87 for(int iter=min; iter<=max; ++iter) {
88 const std::string rad = jau::codec::base::encode(iter, aspec, 3);
89 const int64_t dec = jau::codec::base::decode(rad, aspec);
90#if 0
91 fprintf(stderr, "test base %d: iter %d, rad '%s' %03d %03d %03d, dec %zd\n",
92 base, iter, rad.c_str(), (int)(0xFF & rad[0]), (int)(0xFF & rad[1]), (int)(0xFF & rad[2]), (ssize_t)dec);
93#endif
94 REQUIRE(iter == dec);
95 }
96 static jau::codec::base::natural86_alphabet natural86_alphabet;
97 if( natural86_alphabet == aspec ) {
98 // Test 0-9 ..
99 fprintf(stderr, "Natural 0-9: ");
100 for(int iter=0; iter<=9; ++iter) {
101 const std::string rad = jau::codec::base::encode(iter, aspec);
102 fprintf(stderr, "%s, ", rad.c_str());
103 const char c = (char)('0'+iter);
104 REQUIRE(std::string()+c == rad);
105 }
106 fprintf(stderr, "\n");
107 }
108}
109
110static void testRadix_int64(const jau::codec::base::alphabet& aspec, const int64_t test_min, const int64_t test_max) {
111 const int int64_max_enc_width = 11; // 9223372036854775807 == '7__________' (base 64, natural)
112 const int base = aspec.base();
113 REQUIRE( 1 < base );
114
115 const char min_cp = aspec[0]; // minimum code-point
116 const char max_cp = aspec[base-1]; // maximum code-point
117
118 const std::string max_radix = jau::codec::base::encode(std::numeric_limits<int64_t>::max(), aspec, int64_max_enc_width);
119
120 const int64_t min = jau::codec::base::decode(std::string()+min_cp, aspec);
121 const int64_t max = jau::codec::base::decode(max_radix, aspec);
122 const int64_t max_s = jau::codec::base::decode(std::string()+max_cp, aspec);
123
124 REQUIRE(0 == min);
125 REQUIRE(base-1 == max_s);
127
128 const std::string r1_min = jau::codec::base::encode(0, aspec, int64_max_enc_width);
129 const std::string r1_min_s = jau::codec::base::encode(0, aspec);
130 REQUIRE(std::string()+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp == r1_min);
131 REQUIRE(std::string()+min_cp == r1_min_s);
132
133 const std::string r1_max = jau::codec::base::encode(base-1, aspec, int64_max_enc_width);
134 const std::string r1_max_s = jau::codec::base::encode(base-1, aspec);
135 REQUIRE(std::string()+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+min_cp+max_cp == r1_max);
136 REQUIRE(std::string()+max_cp == r1_max_s);
137
138 fprintf(stderr, "int (sz %zu) == int32_t (sz %zu): %d, int (sz %zu) == int64_t (sz %zu): %d\n",
139 sizeof(int), sizeof(int32_t), std::is_same_v<int, std::int32_t>,
140 sizeof(int), sizeof(int64_t), std::is_same_v<int, std::int64_t>);
141
142 fprintf(stderr, "Test64bit base %d, %s: [%" PRIi64 " .. %" PRIi64 "] <-> ['%s' .. '%s'], %" PRIi64 " years (max/365d) \n",
143 base, aspec.to_string().c_str(),
144 min, max, jau::codec::base::encode(min, aspec).c_str(), jau::codec::base::encode(max, aspec).c_str(), (max/365));
145
146 fprintf(stderr, "- range: [%" PRIi64 " .. %" PRIi64 "] <-> ['%s' .. '%s']\n",
147 test_min, test_max, jau::codec::base::encode(test_min, aspec).c_str(), jau::codec::base::encode(test_max, aspec).c_str());
148
149 REQUIRE(0 == jau::codec::base::decode(std::string()+min_cp+min_cp+min_cp, aspec));
150 REQUIRE(std::string()+min_cp == jau::codec::base::encode(0, aspec));
151
152 {
153 const int64_t v0_d = jau::codec::base::decode(r1_max, aspec);
154 const std::string v1_s = jau::codec::base::encode(base-1, aspec, int64_max_enc_width);
155 REQUIRE(r1_max == v1_s);
156 REQUIRE(base-1 == v0_d);
157 }
158 for(int64_t iter=std::max(0_i64, test_min-1); iter<test_max; ) {
159 ++iter;
160 const std::string rad = jau::codec::base::encode(iter, aspec, int64_max_enc_width);
161 const int64_t dec = jau::codec::base::decode(rad, aspec);
162#if 0
163 fprintf(stderr, "test base %d: iter %" PRIi64 ", rad '%s', dec %" PRIi64 "\n", base, iter, rad.c_str(), dec);
164#endif
165 REQUIRE(iter == dec);
166 }
167}
168
171 testRadix_int64(aspec, 0x7fffff00_i64, 0x80000100_i64);
172 testRadix_int64(aspec, 0xFFFFFFF0_i64, 0x100000010_i64);
173 testRadix_int64(aspec, 0x7FFFFFFFFFFFFFF0_i64, 0x7FFFFFFFFFFFFFFF_i64);
174 // testRadix_int64(aspec, 0x0_i64, 0x7FFFFFFFFFFFFFFF_i64);
175}
176
179 testRadix_int64(aspec, 0x7fffff00_i64, 0x80000100_i64);
180 testRadix_int64(aspec, 0xFFFFFFF0_i64, 0x100000010_i64);
181 testRadix_int64(aspec, 0x7FFFFFFFFFFFFFF0_i64, 0x7FFFFFFFFFFFFFFF_i64);
182 // testRadix_int64(aspec, 0x0_i64, 0x7FFFFFFFFFFFFFFF_i64);
183}
184
185TEST_CASE( "Integer Base 38 Encoding Test 01", "[integer][type]" ) {
187}
188
189TEST_CASE( "Integer Base 64 Encoding Test 02", "[integer][type]" ) {
193}
194
195TEST_CASE( "Integer Base 86 Encoding Test 03", "[integer][type]" ) {
198}
199
201 private:
202 static inline constexpr const std::string_view data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
203
204 static int s_code_point(const char c) noexcept {
205 if ('A' <= c && c <= 'Z') {
206 return c - 'A';
207 } else if ('a' <= c && c <= 'z') {
208 return c - 'a' + 26;
209 } else if ('0' <= c && c <= '9') {
210 return c - '0' + 52;
211 } else if ('+' == c) {
212 return 62;
213 } else if ('/' == c) {
214 return 63;
215 } else {
216 return -1;
217 }
218 }
219
220 public:
222 : alphabet("base64", 64, data, 0, s_code_point) {}
223};
224
225static void testBinaryBase64() {
228 base64_alphabet_nopadding aspec_nopadding;
229
230 // Test Vectors taken from `base64` [RFC 4648](https://www.rfc-editor.org/rfc/rfc4648.html)
231 {
232 std::vector<uint8_t> octets = { };
233 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
234 REQUIRE( "" == encstr);
235 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
236 REQUIRE( octets == dec_octets );
237 }
238 {
239 std::vector<uint8_t> octets = { 'f' };
240 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
241 REQUIRE( "Zg==" == encstr);
242 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
243 REQUIRE( octets == dec_octets );
244 }
245 {
246 std::vector<uint8_t> octets = { 'f', 'o' };
247 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
248 REQUIRE( "Zm8=" == encstr);
249 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
250 REQUIRE( octets == dec_octets );
251 }
252 {
253 std::vector<uint8_t> octets = { 'f', 'o', 'o' };
254 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
255 REQUIRE( "Zm9v" == encstr);
256 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
257 REQUIRE( octets == dec_octets );
258 }
259 {
260 std::vector<uint8_t> octets = { 'f', 'o', 'o', 'b' };
261 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
262 REQUIRE( "Zm9vYg==" == encstr);
263 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
264 REQUIRE( octets == dec_octets );
265 }
266 {
267 std::vector<uint8_t> octets = { 'f', 'o', 'o', 'b', 'a' };
268 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
269 REQUIRE( "Zm9vYmE=" == encstr);
270 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
271 REQUIRE( octets == dec_octets );
272 }
273
274 // Further encoding tests
275 {
276 std::vector<uint8_t> octets = { 'a' };
277 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
278 REQUIRE( "YQ==" == encstr);
279 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
280 REQUIRE( octets == dec_octets );
281 }
282 {
283 std::vector<uint8_t> octets = { 'a', 'b' };
284 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
285 REQUIRE( "YWI=" == encstr);
286 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
287 REQUIRE( octets == dec_octets );
288 }
289 {
290 std::vector<uint8_t> octets = { 'a', 'b', 'c' };
291 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
292 REQUIRE( "YWJj" == encstr);
293 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
294 REQUIRE( octets == dec_octets );
295 }
296 {
297 std::vector<uint8_t> octets = { 'a', 'b', 'c', 'd' };
298 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
299 REQUIRE( "YWJjZA==" == encstr);
300 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
301 REQUIRE( octets == dec_octets );
302 }
303 {
304 std::vector<uint8_t> octets = { 'a', 'b', 'c', 'd', 'e' };
305 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
306 REQUIRE( "YWJjZGU=" == encstr);
307 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
308 REQUIRE( octets == dec_octets );
309 }
310 {
311 std::vector<uint8_t> octets = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
312 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
313 REQUIRE( "YWJjZGVmZw==" == encstr);
314 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
315 REQUIRE( octets == dec_octets );
316 }
317 {
318 // Test no-padding accept and error, double padding dropped '=='
319 std::vector<uint8_t> octets = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
320 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec_nopadding);
321 REQUIRE( "YWJjZGVmZw" == encstr);
322 {
323 // accept no padding
324 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec_nopadding);
325 REQUIRE( octets == dec_octets );
326 }
327 {
328 // not accepting lack of padding
329 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
330 REQUIRE( dec_octets.empty() );
331 }
332 }
333 {
334 // Test no-padding accept and error, double padding dropped '=='
335 std::vector<uint8_t> octets = { 'a' };
336 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec_nopadding);
337 REQUIRE( "YQ" == encstr);
338 {
339 // accept no padding
340 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec_nopadding);
341 REQUIRE( octets == dec_octets );
342 }
343 {
344 // not accepting lack of padding
345 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
346 REQUIRE( dec_octets.empty() );
347 }
348 }
349 {
350 // Test no-padding accept and error, single padding dropped '='
351 std::vector<uint8_t> octets = { 'a', 'b', 'c', 'd', 'e' };
352 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec_nopadding);
353 REQUIRE( "YWJjZGU" == encstr);
354 {
355 // accept no padding
356 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec_nopadding);
357 REQUIRE( octets == dec_octets );
358 }
359 {
360 // not accepting lack of padding
361 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
362 REQUIRE( dec_octets.empty() );
363 }
364 }
365 {
366 // Test no-padding accept and error, single padding dropped '='
367 std::vector<uint8_t> octets = { 'a', 'b' };
368 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec_nopadding);
369 REQUIRE( "YWI" == encstr);
370 {
371 // accept no padding
372 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec_nopadding);
373 REQUIRE( octets == dec_octets );
374 }
375 {
376 // not accepting lack of padding
377 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
378 REQUIRE( dec_octets.empty() );
379 }
380 }
381 {
382 // Test no-padding accept and error, zero padding dropped
383 std::vector<uint8_t> octets = { 'a', 'b', 'c' };
384 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec_nopadding);
385 REQUIRE( "YWJj" == encstr);
386 {
387 // accept no padding
388 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec_nopadding);
389 REQUIRE( octets == dec_octets );
390 }
391 {
392 // accept no padding
393 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
394 REQUIRE( octets == dec_octets );
395 }
396 }
397 {
398 std::string in_str = "aaaaaaaaaaaaaaaaa"; // a17
399 std::vector<uint8_t> octets(in_str.begin(), in_str.end());
400 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
401 REQUIRE( "YWFhYWFhYWFhYWFhYWFhYWE=" == encstr);
402 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
403 REQUIRE( octets == dec_octets );
404 }
405
406 {
407 // Test code-points 63 and 64 of base64
408 std::vector<uint8_t> octets = { 0x03, 0xef, 0xff, 0xf9 };
409 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
410 REQUIRE( "A+//+Q==" == encstr);
411 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
412 REQUIRE( octets == dec_octets );
413 }
414 {
415 // Test code-points 63 and 64 of base64url
416 std::vector<uint8_t> octets = { 0x03, 0xef, 0xff, 0xf9 };
417 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec_url);
418 REQUIRE( "A-__-Q==" == encstr);
419 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec_url);
420 REQUIRE( octets == dec_octets );
421 }
422
423 {
424 std::string in_str = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fivteen sixteen seventeen eighteen nineteen twenty twenty-one";
425 std::string exp_encstr = "b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVuIGVsZXZlbiB0"
426 "d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVuIHNldmVudGVlbiBlaWdodGVl"
427 "biBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
428 std::vector<uint8_t> octets(in_str.begin(), in_str.end());
429 std::string encstr = jau::codec::base::encode64(octets.data(), octets.size(), aspec);
430 REQUIRE( exp_encstr == encstr);
431 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
432 REQUIRE( octets == dec_octets );
433 }
434 {
435 std::string in_str = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fivteen sixteen seventeen eighteen nineteen twenty twenty-one";
436 std::string exp_encstr = "b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVuIGVsZXZlbiB0\n"
437 "d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVuIHNldmVudGVlbiBlaWdodGVl\n"
438 "biBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
439 std::vector<uint8_t> octets(in_str.begin(), in_str.end());
440 std::string encstr = jau::codec::base::encode64_mime(octets.data(), octets.size(), aspec);
441 REQUIRE( exp_encstr == encstr);
442 std::vector<uint8_t> dec_octets = jau::codec::base::decode64_lf(encstr, aspec);
443 REQUIRE( octets == dec_octets );
444 }
445 {
446 std::string in_str = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fivteen sixteen seventeen eighteen nineteen twenty twenty-one";
447 std::string exp_encstr = "b25lIHR3byB0aHJlZSBmb3VyIGZpdmUgc2l4IHNldmVuIGVpZ2h0IG5pbmUgdGVu\n"
448 "IGVsZXZlbiB0d2VsdmUgdGhpcnRlZW4gZm91cnRlZW4gZml2dGVlbiBzaXh0ZWVu\n"
449 "IHNldmVudGVlbiBlaWdodGVlbiBuaW5ldGVlbiB0d2VudHkgdHdlbnR5LW9uZQ==";
450 std::vector<uint8_t> octets(in_str.begin(), in_str.end());
451 std::string encstr = jau::codec::base::encode64_pem(octets.data(), octets.size(), aspec);
452 REQUIRE( exp_encstr == encstr);
453 std::vector<uint8_t> dec_octets = jau::codec::base::decode64_lf(encstr, aspec);
454 REQUIRE( octets == dec_octets );
455 }
456
457 // Erroneous coded string in decoding
458 {
459 std::string encstr = "!@#$%^&*()"; // non-alphebet error
460 std::vector<uint8_t> dec_octets = jau::codec::base::decode64(encstr, aspec);
461 REQUIRE( dec_octets.empty() );
462 }
463
464}
465
466TEST_CASE( "Binary Base 64 Encoding Test 11", "[binary][type]" ) {
468}
Base Alphabet Specification providing the alphabet for encode() and decode().
Definition: base_codec.hpp:56
constexpr int base() const noexcept
The fixed base used for this alphabet.
Definition: base_codec.hpp:75
std::string to_string() const noexcept
Definition: base_codec.hpp:89
alphabet(std::string _name, int _base, std::string_view _symbols, char _padding64, code_point_func _cpf) noexcept
Definition: base_codec.hpp:68
Safe base 38 alphabet with ASCII code-point sorting order.
Definition: base_codec.hpp:319
Safe base 64 alphabet with ASCII code-point sorting order.
Definition: base_codec.hpp:359
Base 86 alphabet with ASCII code-point sorting order.
Definition: base_codec.hpp:398
Safe canonical base64 alphabet, without ASCII code-point sorting order.
Definition: base_codec.hpp:125
Safe canonical base64url alphabet, without ASCII code-point sorting order.
Definition: base_codec.hpp:170
Natural base 86 alphabet, without ASCII code-point sorting order.
Definition: base_codec.hpp:255
std::string encode(int num, const alphabet &aspec, const unsigned int min_width=0) noexcept
Encodes a given positive decimal number to a symbolic string representing a given alphabet and its ba...
Definition: base_codec.cpp:33
std::string encode64_mime(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
Definition: base_codec.hpp:578
std::string encode64(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
Definition: base_codec.cpp:90
std::vector< uint8_t > decode64(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
Definition: base_codec.cpp:141
std::string encode64_pem(const void *in_octets, size_t in_len, const alphabet &aspec) noexcept
Encodes given octets using the given alphabet and fixed base 64 encoding according to base64 RFC 4648...
Definition: base_codec.hpp:560
std::vector< uint8_t > decode64_lf(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing using given alphabet and fixed base 64 to octets accordi...
Definition: base_codec.hpp:595
int64_t decode(const std::string_view &str, const alphabet &aspec) noexcept
Decodes a given symbolic string representing a given alphabet and its base to a positive decimal numb...
Definition: base_codec.cpp:72
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
Definition: base_math.hpp:177
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
Definition: base_math.hpp:191
mp::BigInt pow(mp::BigInt b, mp::BigInt e)
Definition: big_int.hpp:1560
constexpr T abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
Definition: base_math.hpp:155
static void testIntegerBase86(const jau::codec::base::alphabet &aspec)
static void testIntegerBase64(const jau::codec::base::alphabet &aspec)
TEST_CASE("Integer Base 38 Encoding Test 01", "[integer][type]")
static void testBinaryBase64()
static void testRadix_int64(const jau::codec::base::alphabet &aspec, const int64_t test_min, const int64_t test_max)
static void testRadix_3digits_int32(const jau::codec::base::alphabet &aspec)