Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 1 | #include "utility.hpp" |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 2 | |
| 3 | #include "gmock/gmock.h" |
| 4 | |
| 5 | TEST(Utility, Base64DecodeAuthString) |
| 6 | { |
| 7 | std::string authString("dXNlcm40bWU6cGFzc3cwcmQ="); |
| 8 | std::string result; |
| 9 | EXPECT_TRUE(crow::utility::base64Decode(authString, result)); |
| 10 | EXPECT_EQ(result, "usern4me:passw0rd"); |
| 11 | } |
| 12 | |
| 13 | TEST(Utility, Base64DecodeNonAscii) |
| 14 | { |
| 15 | std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33"); |
| 16 | std::string result; |
| 17 | EXPECT_FALSE(crow::utility::base64Decode(junkString, result)); |
| 18 | } |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 19 | |
| 20 | TEST(Utility, Base64EncodeString) |
| 21 | { |
| 22 | using namespace std::string_literals; |
| 23 | std::string encoded; |
| 24 | |
| 25 | encoded = crow::utility::base64encode(""); |
| 26 | EXPECT_EQ(encoded, ""); |
| 27 | |
| 28 | encoded = crow::utility::base64encode("f"); |
| 29 | EXPECT_EQ(encoded, "Zg=="); |
| 30 | |
| 31 | encoded = crow::utility::base64encode("f0"); |
| 32 | EXPECT_EQ(encoded, "ZjA="); |
| 33 | |
| 34 | encoded = crow::utility::base64encode("f0\0"s); |
| 35 | EXPECT_EQ(encoded, "ZjAA"); |
| 36 | |
| 37 | encoded = crow::utility::base64encode("f0\0 "s); |
| 38 | EXPECT_EQ(encoded, "ZjAAIA=="); |
| 39 | |
| 40 | encoded = crow::utility::base64encode("f0\0 B"s); |
| 41 | EXPECT_EQ(encoded, "ZjAAIEI="); |
| 42 | |
| 43 | encoded = crow::utility::base64encode("f0\0 Ba"s); |
| 44 | EXPECT_EQ(encoded, "ZjAAIEJh"); |
| 45 | |
| 46 | encoded = crow::utility::base64encode("f0\0 Bar"s); |
| 47 | EXPECT_EQ(encoded, "ZjAAIEJhcg=="); |
| 48 | } |
| 49 | |
| 50 | TEST(Utility, Base64EncodeDecodeString) |
| 51 | { |
| 52 | using namespace std::string_literals; |
| 53 | std::string data("Data fr\0m 90 reading a \nFile"s); |
| 54 | std::string encoded = crow::utility::base64encode(data); |
| 55 | std::string decoded; |
| 56 | EXPECT_TRUE(crow::utility::base64Decode(encoded, decoded)); |
| 57 | EXPECT_EQ(data, decoded); |
| 58 | } |