blob: 99aecb965b814375ce86d311f2a73a3506d2f951 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
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
12namespace wpi {
13
14struct Base64TestParam {
15 int plain_len;
16 const char* plain;
17 const char* encoded;
18};
19
20std::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
27class 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
37TEST_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
47TEST_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
54TEST_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
65TEST_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
78static 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
88INSTANTIATE_TEST_CASE_P(Base64Sample, Base64Test,
89 ::testing::ValuesIn(sample), );
90
91static Base64TestParam standard[] = {
92 {0, "", ""},
93 {1, "\0", "AA=="},
94 {2, "\0\0", "AAA="},
95 {3, "\0\0\0", "AAAA"},
96 {1, "\377", "/w=="},
97 {2, "\377\377", "//8="},
98 {3, "\377\377\377", "////"},
99 {2, "\xff\xef", "/+8="},
100};
101
102INSTANTIATE_TEST_CASE_P(Base64Standard, Base64Test,
103 ::testing::ValuesIn(standard), );
104
105} // namespace wpi