openMSX
Base64_test.cc
Go to the documentation of this file.
1#include "catch.hpp"
2
3#include "Base64.hh"
4
5#include "ranges.hh"
6
7#include <bit>
8
9static void test_decode(const std::string& encoded, const std::string& decoded)
10{
11 auto buf = Base64::decode(encoded);
12 REQUIRE(buf.size() == decoded.size());
13 CHECK(ranges::equal(std::span{buf}, decoded));
14}
15
16static void test(const std::string& decoded, const std::string& encoded)
17{
18 CHECK(Base64::encode(std::span{std::bit_cast<const uint8_t*>(decoded.data()),
19 decoded.size()})
20 == encoded);
21 test_decode(encoded, decoded);
22}
23
24TEST_CASE("Base64")
25{
26 // Test vectors verified with 'base64' tool from coreutils.
27 test("", "");
28 test("a", "YQ==");
29 test("a\n", "YQo=");
30 test("ab\n", "YWIK");
31 test("abc\n", "YWJjCg==");
32 test("0123456789\n", "MDEyMzQ1Njc4OQo=");
33 test("abcdefghijklmnopqrstuvwxyz\n", "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoK");
34 test("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n",
35 "MDEyMzQ1Njc4OUFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaYWJjZGVmZ2hpamtsbW5vcHFyc3R1\n"
36 "dnd4eXoK");
37 test("111111111111111111111111111111111111111111111111111111111111111111111111111111"
38 "111111111111111111111111111111111111111111111111111111111111111111111111111111"
39 "111111111111111111111111111111111111111111111111111111111111111111111111111111"
40 "111111111111111111111111111111111111111111111\n",
41 "MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx\n"
42 "MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx\n"
43 "MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx\n"
44 "MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx\n"
45 "MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExCg==");
46
47 // Decode-only:
48 // - extra newlines don't matter
49 test_decode("M\nDEyM\nzQ1Njc\n4OQo=", "0123456789\n");
50 // - no newlines at all is fine as well
51 test_decode("MDEyMzQ1Njc4OUFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoK",
52 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n");
53}
TEST_CASE("Base64")
CHECK(m3==m3)
constexpr bool equal(InputRange1 &&range1, InputRange2 &&range2, Pred pred={}, Proj1 proj1={}, Proj2 proj2={})
Definition ranges.hh:378