2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/tap.hpp>
|
2017-12-22 12:37:04 +11:00
|
|
|
|
2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/encode/base.hpp>
|
2017-12-22 12:37:04 +11:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
static constexpr char input[] = "foobar";
|
|
|
|
|
|
|
|
|
2018-01-17 20:10:26 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-12-22 12:37:04 +11:00
|
|
|
// test vectors from rfc4648
|
|
|
|
template <int Size>
|
|
|
|
struct output { static const char *value[std::size (input)]; };
|
|
|
|
|
2018-01-17 20:10:26 +11:00
|
|
|
|
2017-12-22 12:37:04 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
const char* output<64>::value[] = {
|
2018-01-17 20:10:26 +11:00
|
|
|
"",
|
|
|
|
"Zg==",
|
|
|
|
"Zm8=",
|
|
|
|
"Zm9v",
|
|
|
|
"Zm9vYg==",
|
|
|
|
"Zm9vYmE=",
|
|
|
|
"Zm9vYmFy",
|
2017-12-22 12:37:04 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-01-17 20:10:26 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-12-22 12:37:04 +11:00
|
|
|
template <>
|
|
|
|
const char* output<32>::value[] {
|
2018-01-17 20:10:26 +11:00
|
|
|
"",
|
|
|
|
"MY======",
|
|
|
|
"MZXQ====",
|
|
|
|
"MZXW6===",
|
|
|
|
"MZXW6YQ=",
|
|
|
|
"MZXW6YTB",
|
|
|
|
"MZXW6YTBOI======"
|
2017-12-22 12:37:04 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-01-17 20:10:26 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-12-22 12:37:04 +11:00
|
|
|
#if 0
|
|
|
|
template <>
|
|
|
|
const char* output<32h>::value[] {
|
|
|
|
"", "CO======", "CPNG====", "CPNMU===", "CPNMUOG=", "CPNMUOJ1", "CPNMUOJ1E8======",
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <>
|
|
|
|
const char *output<16>::value[] = {
|
2018-01-17 20:10:26 +11:00
|
|
|
"",
|
|
|
|
"66",
|
|
|
|
"666F",
|
|
|
|
"666F6F",
|
|
|
|
"666F6F62",
|
|
|
|
"666F6F6261",
|
|
|
|
"666F6F626172"
|
2017-12-22 12:37:04 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <int Size>
|
|
|
|
const auto output_v = output<Size>::value;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <int Size>
|
|
|
|
void
|
2018-08-05 14:42:02 +10:00
|
|
|
test_size (cruft::TAP::logger &tap)
|
2017-12-22 12:37:04 +11:00
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < std::size (input); ++i) {
|
2018-08-05 14:42:02 +10:00
|
|
|
const auto fragment = cruft::make_view (input, input + i);
|
2017-12-22 12:37:04 +11:00
|
|
|
|
|
|
|
std::string encoded;
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::encode::base<Size>::encode (
|
2017-12-22 12:37:04 +11:00
|
|
|
std::back_inserter (encoded),
|
|
|
|
fragment
|
|
|
|
);
|
|
|
|
|
2018-08-13 14:51:33 +10:00
|
|
|
tap.expect_eq (
|
|
|
|
encoded,
|
|
|
|
output_v<Size>[i],
|
2021-04-13 16:05:08 +10:00
|
|
|
"{} character base{} encode; '{}' is '{}'", i, Size, encoded, output_v<Size>[i]
|
2017-12-22 12:37:04 +11:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
std::string decoded;
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::encode::base<Size>::decode (
|
2017-12-22 12:37:04 +11:00
|
|
|
std::back_inserter (decoded),
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::make_view (encoded)
|
2017-12-22 12:37:04 +11:00
|
|
|
);
|
|
|
|
|
2021-04-13 16:05:08 +10:00
|
|
|
tap.expect_eq (decoded, fragment, "{} character base{} decode, '{}'", i, Size, decoded);
|
2017-12-22 12:37:04 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
main ()
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2017-12-22 12:37:04 +11:00
|
|
|
|
|
|
|
test_size<16> (tap);
|
|
|
|
test_size<32> (tap);
|
|
|
|
test_size<64> (tap);
|
|
|
|
|
|
|
|
return tap.status ();
|
|
|
|
}
|