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