Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2015-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "gtest/gtest.h" |
| 9 | #include "wpi/Base64.h" |
| 10 | #include "wpi/SmallString.h" |
| 11 | |
| 12 | namespace wpi { |
| 13 | |
| 14 | struct Base64TestParam { |
| 15 | int plain_len; |
| 16 | const char* plain; |
| 17 | const char* encoded; |
| 18 | }; |
| 19 | |
| 20 | std::ostream& operator<<(std::ostream& os, const Base64TestParam& param) { |
| 21 | os << "Base64TestParam(Len: " << param.plain_len << ", " |
| 22 | << "Plain: \"" << param.plain << "\", " |
| 23 | << "Encoded: \"" << param.encoded << "\")"; |
| 24 | return os; |
| 25 | } |
| 26 | |
| 27 | class Base64Test : public ::testing::TestWithParam<Base64TestParam> { |
| 28 | protected: |
| 29 | StringRef GetPlain() { |
| 30 | if (GetParam().plain_len < 0) |
| 31 | return StringRef(GetParam().plain); |
| 32 | else |
| 33 | return StringRef(GetParam().plain, GetParam().plain_len); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | TEST_P(Base64Test, EncodeStdString) { |
| 38 | std::string s; |
| 39 | Base64Encode(GetPlain(), &s); |
| 40 | ASSERT_EQ(GetParam().encoded, s); |
| 41 | |
| 42 | // text already in s |
| 43 | Base64Encode(GetPlain(), &s); |
| 44 | ASSERT_EQ(GetParam().encoded, s); |
| 45 | } |
| 46 | |
| 47 | TEST_P(Base64Test, EncodeSmallString) { |
| 48 | SmallString<128> buf; |
| 49 | ASSERT_EQ(GetParam().encoded, Base64Encode(GetPlain(), buf)); |
| 50 | // reuse buf |
| 51 | ASSERT_EQ(GetParam().encoded, Base64Encode(GetPlain(), buf)); |
| 52 | } |
| 53 | |
| 54 | TEST_P(Base64Test, DecodeStdString) { |
| 55 | std::string s; |
| 56 | StringRef encoded = GetParam().encoded; |
| 57 | EXPECT_EQ(encoded.size(), Base64Decode(encoded, &s)); |
| 58 | ASSERT_EQ(GetPlain(), s); |
| 59 | |
| 60 | // text already in s |
| 61 | Base64Decode(encoded, &s); |
| 62 | ASSERT_EQ(GetPlain(), s); |
| 63 | } |
| 64 | |
| 65 | TEST_P(Base64Test, DecodeSmallString) { |
| 66 | SmallString<128> buf; |
| 67 | StringRef encoded = GetParam().encoded; |
| 68 | size_t len; |
| 69 | StringRef plain = Base64Decode(encoded, &len, buf); |
| 70 | EXPECT_EQ(encoded.size(), len); |
| 71 | ASSERT_EQ(GetPlain(), plain); |
| 72 | |
| 73 | // reuse buf |
| 74 | plain = Base64Decode(encoded, &len, buf); |
| 75 | ASSERT_EQ(GetPlain(), plain); |
| 76 | } |
| 77 | |
| 78 | static Base64TestParam sample[] = { |
| 79 | {-1, "Send reinforcements", "U2VuZCByZWluZm9yY2VtZW50cw=="}, |
| 80 | {-1, "Now is the time for all good coders\n to learn C++", |
| 81 | "Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwgZ29vZCBjb2RlcnMKIHRvIGxlYXJuIEMrKw=="}, |
| 82 | {-1, |
| 83 | "This is line one\nThis is line two\nThis is line three\nAnd so on...\n", |
| 84 | "VGhpcyBpcyBsaW5lIG9uZQpUaGlzIGlzIGxpbmUgdHdvClRoaXMgaXMgbGluZSB0aHJlZQpBb" |
| 85 | "mQgc28gb24uLi4K"}, |
| 86 | }; |
| 87 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 88 | INSTANTIATE_TEST_SUITE_P(Base64Sample, Base64Test, ::testing::ValuesIn(sample)); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 89 | |
| 90 | static Base64TestParam standard[] = { |
| 91 | {0, "", ""}, |
| 92 | {1, "\0", "AA=="}, |
| 93 | {2, "\0\0", "AAA="}, |
| 94 | {3, "\0\0\0", "AAAA"}, |
| 95 | {1, "\377", "/w=="}, |
| 96 | {2, "\377\377", "//8="}, |
| 97 | {3, "\377\377\377", "////"}, |
| 98 | {2, "\xff\xef", "/+8="}, |
| 99 | }; |
| 100 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 101 | INSTANTIATE_TEST_SUITE_P(Base64Standard, Base64Test, |
| 102 | ::testing::ValuesIn(standard)); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 103 | |
| 104 | } // namespace wpi |