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 | } |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 59 | |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 60 | TEST(Utility, GetDateTimeStdtime) |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 61 | { |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 62 | using crow::utility::getDateTimeStdtime; |
| 63 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 64 | // some time before the epoch |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 65 | EXPECT_EQ(getDateTimeStdtime(std::time_t{-1234567}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 66 | "1969-12-17T17:03:53+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 67 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 68 | // epoch |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 69 | EXPECT_EQ(getDateTimeStdtime(std::time_t{0}), "1970-01-01T00:00:00+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 70 | // some time in the past after the epoch |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 71 | |
| 72 | // Limits |
| 73 | EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::max()), |
| 74 | "1969-12-31T23:59:59+00:00"); |
| 75 | EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::min()), |
| 76 | "1970-01-01T00:00:00+00:00"); |
| 77 | } |
| 78 | |
| 79 | TEST(Utility, getDateTimeUint) |
| 80 | { |
| 81 | using crow::utility::getDateTimeUint; |
| 82 | |
| 83 | EXPECT_EQ(getDateTimeUint(uint64_t{1638312095}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 84 | "2021-11-30T22:41:35+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 85 | // some time in the future, beyond 2038 |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 86 | EXPECT_EQ(getDateTimeUint(uint64_t{41638312095}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 87 | "3289-06-18T21:48:15+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 88 | // the maximum time we support |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 89 | EXPECT_EQ(getDateTimeUint(uint64_t{253402300799}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 90 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 91 | |
| 92 | // This test currently throws an exception |
| 93 | // EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::max()), ""); |
| 94 | |
| 95 | EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::min()), |
| 96 | "1970-01-01T00:00:00+00:00"); |
| 97 | } |
| 98 | |
| 99 | TEST(Utility, getDateTimeUintMs) |
| 100 | { |
| 101 | using crow::utility::getDateTimeUintMs; |
| 102 | |
| 103 | // Note, this is the wrong result, but is here to make sure that we don't |
| 104 | // have worse behavior (ie seg faults, exceptions) when this happens |
| 105 | EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::max()), |
| 106 | "1969-12-31T23:59:59.999000+00:00"); |
| 107 | |
| 108 | EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()), |
| 109 | "1970-01-01T00:00:00+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 110 | } |