rippled
Loading...
Searching...
No Matches
tests/libxrpl/basics/base64.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/base64.h>
21
22#include <doctest/doctest.h>
23
24#include <string>
25
26using namespace ripple;
27
28static void
29check(std::string const& in, std::string const& out)
30{
31 auto const encoded = base64_encode(in);
32 CHECK(encoded == out);
33 CHECK(base64_decode(encoded) == in);
34}
35
36TEST_CASE("base64")
37{
38 check("", "");
39 check("f", "Zg==");
40 check("fo", "Zm8=");
41 check("foo", "Zm9v");
42 check("foob", "Zm9vYg==");
43 check("fooba", "Zm9vYmE=");
44 check("foobar", "Zm9vYmFy");
45
46 check(
47 "Man is distinguished, not only by his reason, but by this "
48 "singular passion from "
49 "other animals, which is a lust of the mind, that by a "
50 "perseverance of delight "
51 "in the continued and indefatigable generation of knowledge, "
52 "exceeds the short "
53 "vehemence of any carnal pleasure.",
54 "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dC"
55 "BieSB0aGlz"
56 "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIG"
57 "x1c3Qgb2Yg"
58 "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aG"
59 "UgY29udGlu"
60 "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleG"
61 "NlZWRzIHRo"
62 "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=");
63
64 std::string const notBase64 = "not_base64!!";
65 std::string const truncated = "not";
66 CHECK(base64_decode(notBase64) == base64_decode(truncated));
67}
void check(bool condition, std::string const &message)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string base64_decode(std::string_view data)
std::string base64_encode(std::uint8_t const *data, std::size_t len)