blob: 542858d41de89e1f7d0f0811c588712ba935f9a2 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Silverman8fce7482020-01-05 13:18:21 -08004
5#include "gtest/gtest.h"
6#include "wpi/Base64.h"
7#include "wpi/SmallString.h"
8
9namespace wpi {
10
11struct Base64TestParam {
12 int plain_len;
13 const char* plain;
14 const char* encoded;
15};
16
17std::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
24class Base64Test : public ::testing::TestWithParam<Base64TestParam> {
25 protected:
Austin Schuh812d0d12021-11-04 20:16:48 -070026 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 Silverman8fce7482020-01-05 13:18:21 -080032 }
33};
34
35TEST_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
45TEST_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
52TEST_P(Base64Test, DecodeStdString) {
53 std::string s;
Austin Schuh812d0d12021-11-04 20:16:48 -070054 std::string_view encoded = GetParam().encoded;
Brian Silverman8fce7482020-01-05 13:18:21 -080055 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
63TEST_P(Base64Test, DecodeSmallString) {
64 SmallString<128> buf;
Austin Schuh812d0d12021-11-04 20:16:48 -070065 std::string_view encoded = GetParam().encoded;
Brian Silverman8fce7482020-01-05 13:18:21 -080066 size_t len;
Austin Schuh812d0d12021-11-04 20:16:48 -070067 std::string_view plain = Base64Decode(encoded, &len, buf);
Brian Silverman8fce7482020-01-05 13:18:21 -080068 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
76static 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 Schuh812d0d12021-11-04 20:16:48 -070086INSTANTIATE_TEST_SUITE_P(Base64SampleTests, Base64Test,
87 ::testing::ValuesIn(sample));
Brian Silverman8fce7482020-01-05 13:18:21 -080088
89static 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 Schuh812d0d12021-11-04 20:16:48 -0700100INSTANTIATE_TEST_SUITE_P(Base64StandardTests, Base64Test,
Brian Silverman8fce7482020-01-05 13:18:21 -0800101 ::testing::ValuesIn(standard));
102
103} // namespace wpi