2016-09-25 17:23:39 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-05-31 13:43:32 -07:00
|
|
|
/* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */
|
2016-09-25 17:23:39 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
2018-04-29 23:33:19 -07:00
|
|
|
#include "wpi/Base64.h"
|
|
|
|
|
#include "wpi/SmallString.h"
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
namespace wpi {
|
|
|
|
|
|
|
|
|
|
struct Base64TestParam {
|
|
|
|
|
int plain_len;
|
|
|
|
|
const char* plain;
|
|
|
|
|
const char* encoded;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const Base64TestParam& param) {
|
|
|
|
|
os << "Base64TestParam(Len: " << param.plain_len << ", "
|
|
|
|
|
<< "Plain: \"" << param.plain << "\", "
|
|
|
|
|
<< "Encoded: \"" << param.encoded << "\")";
|
|
|
|
|
return os;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Base64Test : public ::testing::TestWithParam<Base64TestParam> {
|
|
|
|
|
protected:
|
2018-04-29 23:33:19 -07:00
|
|
|
StringRef GetPlain() {
|
2016-09-25 17:23:39 -07:00
|
|
|
if (GetParam().plain_len < 0)
|
2018-04-29 23:33:19 -07:00
|
|
|
return StringRef(GetParam().plain);
|
2016-09-25 17:23:39 -07:00
|
|
|
else
|
2018-04-29 23:33:19 -07:00
|
|
|
return StringRef(GetParam().plain, GetParam().plain_len);
|
2016-09-25 17:23:39 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-13 00:55:56 -07:00
|
|
|
TEST_P(Base64Test, EncodeStdString) {
|
2016-09-25 17:23:39 -07:00
|
|
|
std::string s;
|
|
|
|
|
Base64Encode(GetPlain(), &s);
|
|
|
|
|
ASSERT_EQ(GetParam().encoded, s);
|
2017-08-13 00:55:56 -07:00
|
|
|
|
|
|
|
|
// text already in s
|
|
|
|
|
Base64Encode(GetPlain(), &s);
|
|
|
|
|
ASSERT_EQ(GetParam().encoded, s);
|
2016-09-25 17:23:39 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-13 00:55:56 -07:00
|
|
|
TEST_P(Base64Test, EncodeSmallString) {
|
2018-04-29 23:33:19 -07:00
|
|
|
SmallString<128> buf;
|
2017-08-13 00:55:56 -07:00
|
|
|
ASSERT_EQ(GetParam().encoded, Base64Encode(GetPlain(), buf));
|
|
|
|
|
// reuse buf
|
|
|
|
|
ASSERT_EQ(GetParam().encoded, Base64Encode(GetPlain(), buf));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_P(Base64Test, DecodeStdString) {
|
2016-09-25 17:23:39 -07:00
|
|
|
std::string s;
|
2018-04-29 23:33:19 -07:00
|
|
|
StringRef encoded = GetParam().encoded;
|
2016-09-25 17:23:39 -07:00
|
|
|
EXPECT_EQ(encoded.size(), Base64Decode(encoded, &s));
|
|
|
|
|
ASSERT_EQ(GetPlain(), s);
|
2017-08-13 00:55:56 -07:00
|
|
|
|
|
|
|
|
// text already in s
|
|
|
|
|
Base64Decode(encoded, &s);
|
|
|
|
|
ASSERT_EQ(GetPlain(), s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_P(Base64Test, DecodeSmallString) {
|
2018-04-29 23:33:19 -07:00
|
|
|
SmallString<128> buf;
|
|
|
|
|
StringRef encoded = GetParam().encoded;
|
2017-10-21 20:31:20 -07:00
|
|
|
size_t len;
|
2018-04-29 23:33:19 -07:00
|
|
|
StringRef plain = Base64Decode(encoded, &len, buf);
|
2017-08-13 00:55:56 -07:00
|
|
|
EXPECT_EQ(encoded.size(), len);
|
|
|
|
|
ASSERT_EQ(GetPlain(), plain);
|
|
|
|
|
|
|
|
|
|
// reuse buf
|
|
|
|
|
plain = Base64Decode(encoded, &len, buf);
|
|
|
|
|
ASSERT_EQ(GetPlain(), plain);
|
2016-09-25 17:23:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Base64TestParam sample[] = {
|
|
|
|
|
{-1, "Send reinforcements", "U2VuZCByZWluZm9yY2VtZW50cw=="},
|
|
|
|
|
{-1, "Now is the time for all good coders\n to learn C++",
|
|
|
|
|
"Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKIHRvIGxlYXJuIEMrKw=="},
|
|
|
|
|
{-1,
|
|
|
|
|
"This is line one\nThis is line two\nThis is line three\nAnd so on...\n",
|
|
|
|
|
"VGhpcyBpcyBsaW5lIG9uZQpUaGlzIGlzIGxpbmUgdHdvClRoaXMgaXMgbGluZSB0aHJlZQpBb"
|
|
|
|
|
"mQgc28gb24uLi4K"},
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-31 13:43:32 -07:00
|
|
|
INSTANTIATE_TEST_SUITE_P(Base64Sample, Base64Test, ::testing::ValuesIn(sample));
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
static Base64TestParam standard[] = {
|
|
|
|
|
{0, "", ""},
|
|
|
|
|
{1, "\0", "AA=="},
|
|
|
|
|
{2, "\0\0", "AAA="},
|
|
|
|
|
{3, "\0\0\0", "AAAA"},
|
|
|
|
|
{1, "\377", "/w=="},
|
|
|
|
|
{2, "\377\377", "//8="},
|
|
|
|
|
{3, "\377\377\377", "////"},
|
|
|
|
|
{2, "\xff\xef", "/+8="},
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-31 13:43:32 -07:00
|
|
|
INSTANTIATE_TEST_SUITE_P(Base64Standard, Base64Test,
|
|
|
|
|
::testing::ValuesIn(standard));
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
} // namespace wpi
|