|
25 | 25 | #include <stdexcept>
|
26 | 26 | #include <string>
|
27 | 27 | #include <vector>
|
| 28 | +#include <unordered_map> |
28 | 29 |
|
29 | 30 | // LSST headers
|
30 | 31 | #include "lsst/log/Log.h"
|
@@ -79,7 +80,22 @@ std::vector<std::string> const char2hex_lower = {
|
79 | 80 | "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df",
|
80 | 81 | "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef",
|
81 | 82 | "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff"};
|
| 83 | + |
| 84 | +std::unordered_map<std::string, std::string> const str2base64 = { |
| 85 | + {"0", "MA=="}, |
| 86 | + {"01", "MDE="}, |
| 87 | + {"012", "MDEy"}, |
| 88 | + {"0123", "MDEyMw=="}, |
| 89 | + {"01234", "MDEyMzQ="}, |
| 90 | + {"012345", "MDEyMzQ1"}, |
| 91 | + {"0123456", "MDEyMzQ1Ng=="}, |
| 92 | + {"01234567", "MDEyMzQ1Njc="}, |
| 93 | + {"012345678", "MDEyMzQ1Njc4"}, |
| 94 | + {"0123456789", "MDEyMzQ1Njc4OQ=="}, |
| 95 | + {"!@#$$\%\%^^&&**(())_)(**&&&", "IUAjJCQlJV5eJiYqKigoKSlfKSgqKiYmJg=="}}; |
| 96 | + |
82 | 97 | } // namespace
|
| 98 | + |
83 | 99 | BOOST_AUTO_TEST_SUITE(Suite)
|
84 | 100 |
|
85 | 101 | BOOST_AUTO_TEST_CASE(SplitStringTest) {
|
@@ -425,4 +441,35 @@ BOOST_AUTO_TEST_CASE(StringCaseTranslationTest) {
|
425 | 441 | BOOST_CHECK_EQUAL(util::String::toUpper("Mixed_Case"), "MIXED_CASE");
|
426 | 442 | }
|
427 | 443 |
|
| 444 | +BOOST_AUTO_TEST_CASE(ToBase64Test) { |
| 445 | + LOGS_INFO("ToBase64Test test begins"); |
| 446 | + |
| 447 | + // Null pointer is treated as an illegal input. |
| 448 | + BOOST_CHECK_THROW(util::String::toBase64(nullptr, 0), std::invalid_argument); |
| 449 | + |
| 450 | + // This test ensures that the empty string is always returned for the empty |
| 451 | + // input regardleass. |
| 452 | + char const empty[] = ""; |
| 453 | + BOOST_CHECK_EQUAL(util::String::toBase64(empty, 0), std::string()); |
| 454 | + |
| 455 | + for (auto const& [str, b64] : ::str2base64) { |
| 456 | + BOOST_CHECK_EQUAL(util::String::toBase64(str), b64); |
| 457 | + } |
| 458 | +} |
| 459 | + |
| 460 | +BOOST_AUTO_TEST_CASE(FromBase64Test) { |
| 461 | + LOGS_INFO("FromBase64Test test begins"); |
| 462 | + |
| 463 | + // Make sure the result is empty if no input beyond the optional |
| 464 | + // prefix is present. |
| 465 | + std::string const empty; |
| 466 | + BOOST_CHECK_EQUAL(util::String::fromBase64(empty), std::string()); |
| 467 | + |
| 468 | + for (auto const& [str, b64] : ::str2base64) { |
| 469 | + std::string const decoded = util::String::fromBase64(b64); |
| 470 | + BOOST_CHECK_EQUAL(decoded.size(), str.size()); |
| 471 | + BOOST_CHECK_EQUAL(decoded, str); |
| 472 | + } |
| 473 | +} |
| 474 | + |
428 | 475 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments