| e8555c89 Andy McCall |
1 |
#include "catch.hpp" |
| 2 |
|
| 3 |
#include "Base64.hh" |
| 4 |
|
| 5 |
#include <algorithm> |
| 6 |
#include <bit> |
| 7 |
|
| 8 |
static void test_decode(const std::string& encoded, const std::string& decoded) |
| 9 |
{ |
| 10 |
auto buf = Base64::decode(encoded); |
| 11 |
REQUIRE(buf.size() == decoded.size()); |
| 12 |
CHECK(std::ranges::equal(std::span{buf}, decoded)); |
| 13 |
} |
| 14 |
|
| 15 |
static void test(const std::string& decoded, const std::string& encoded) |
| 16 |
{ |
| 17 |
CHECK(Base64::encode(std::span{std::bit_cast<const uint8_t*>(decoded.data()), |
| 18 |
decoded.size()}) |
| 19 |
== encoded); |
| 20 |
test_decode(encoded, decoded); |
| 21 |
} |
| 22 |
|
| 23 |
TEST_CASE("Base64") |
| 24 |
{ |
| 25 |
|
| 26 |
test("", ""); |
| 27 |
test("a", "YQ=="); |
| 28 |
test("a\n", "YQo="); |
| 29 |
test("ab\n", "YWIK"); |
| 30 |
test("abc\n", "YWJjCg=="); |
| 31 |
test("0123456789\n", "MDEyMzQ1Njc4OQo="); |
| 32 |
test("abcdefghijklmnopqrstuvwxyz\n", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoK"); |
| 33 |
test("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n", |
| 34 |
"MDEyMzQ1Njc4OUFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaYWJjZGVmZ2hpamtsbW5vcHFyc3R1\n" |
| 35 |
"dnd4eXoK"); |
| 36 |
test("111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 37 |
"111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 38 |
"111111111111111111111111111111111111111111111111111111111111111111111111111111" |
| 39 |
"111111111111111111111111111111111111111111111\n", |
| 40 |
"MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx\n" |
| 41 |
"MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx\n" |
| 42 |
"MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx\n" |
| 43 |
"MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx\n" |
| 44 |
"MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExCg=="); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
test_decode("M\nDEyM\nzQ1Njc\n4OQo=", "0123456789\n"); |
| 49 |
|
| 50 |
test_decode("MDEyMzQ1Njc4OUFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoK", |
| 51 |
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n"); |
| 52 |
} |