26#include <openssl/bio.h>
27#include <openssl/evp.h>
28#include <openssl/buffer.h>
37 b64 = BIO_new(BIO_f_base64());
38 bio = BIO_new(BIO_s_mem());
39 bio = BIO_push(b64, bio);
41 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
42 BIO_write(bio, text, (
int)textlen);
43 EXPECT_TRUE(BIO_flush(bio) == 1);
45 long len = BIO_get_mem_data(bio, &encdata);
51 std::string
ret(encdata, (
size_t)len);
57#include <mbedtls/base64.h>
64 outlen = 3 + 4 * (textlen + 3) / 3;
66 char *dst =
new char[outlen];
68 EXPECT_EQ(mbedtls_base64_encode(
reinterpret_cast<unsigned char *
>(dst),
71 reinterpret_cast<const unsigned char *
>(text),
74 std::string
ret(dst, olen);
83 const std::string enc = b64.
encode(text);
84 std::string dec = b64.
decode(enc);
85 std::string libenc =
ssllib_b64enc(text.c_str(), text.size());
87 EXPECT_EQ(text, dec) <<
"Encode/Decode results differ";
88 EXPECT_EQ(enc, libenc) <<
"Encode differs from Crypto lib result";
93 auto enc = b64.
encode(data, len);
95 std::unique_ptr<char[]> decdata(
new char[len]);
96 size_t decode_len = b64.
decode(decdata.get(), len, enc);
99 EXPECT_EQ(enc, libenc) <<
"Encode differs from Crypto lib result";
101 ASSERT_EQ(decode_len, len) <<
"Encode/decode length differs";
102 ASSERT_EQ(std::vector<uint8_t>(decdata.get(), decdata.get() + decode_len),
103 std::vector<uint8_t>(data, data + len))
104 <<
"Encode/Decode results differ";
110 auto enc = b64.
encode(std::string(
"abc"));
113 EXPECT_THROW(b64.
decode(buf, 2, enc), Base64::base64_decode_out_of_bound_error);
119 EXPECT_THROW(b64.
decode(dec, text), Base64::base64_decode_error);
146 b64_test(b64,
"get your kicks on ... route 66");
149 b64_test(b64,
"I want to believe...");
150 b64_test(b64,
"it was a weather balloon");
151 b64_test(b64,
"hyperspatial bypass");
154 b64_test(b64,
"there's no sunshine when she's gone");
155 b64_test(b64,
"??????????????????????");
156 b64_test(b64,
"???????????????????????");
157 b64_test(b64,
"????????????????????????");
158 b64_test(b64,
"???x>>>>>>>>>?????????????");
159 b64_test(b64,
"???x>>>>>>>>>??????????????");
160 b64_test(b64,
"???x>>>>>>>>>?????????????x>>");
168 for (
unsigned int i = 0; i < 20; i++)
170 char *data =
new char[i];
171 for (
unsigned int j = 0; j < i; j++)
173 data[j] = (char)(std::rand() & 0xff);
std::string encode(const V &data) const
size_t decode(void *data, size_t len, const std::string &str) const
void b64_test_binary(const Base64 &b64, const char *data, unsigned int len)
TEST(Base64, tooshortdest)
void b64_test_bad_decode(const Base64 &b64, const std::string &text)
void b64_test(const Base64 &b64, const std::string &text)
std::string ssllib_b64enc(const char *text, size_t textlen)