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 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 5 | namespace crow::utility |
| 6 | { |
| 7 | namespace |
| 8 | { |
| 9 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 10 | TEST(Utility, Base64DecodeAuthString) |
| 11 | { |
| 12 | std::string authString("dXNlcm40bWU6cGFzc3cwcmQ="); |
| 13 | std::string result; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 14 | EXPECT_TRUE(base64Decode(authString, result)); |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 15 | EXPECT_EQ(result, "usern4me:passw0rd"); |
| 16 | } |
| 17 | |
| 18 | TEST(Utility, Base64DecodeNonAscii) |
| 19 | { |
| 20 | std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33"); |
| 21 | std::string result; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 22 | EXPECT_FALSE(base64Decode(junkString, result)); |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 23 | } |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 24 | |
| 25 | TEST(Utility, Base64EncodeString) |
| 26 | { |
| 27 | using namespace std::string_literals; |
| 28 | std::string encoded; |
| 29 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 30 | encoded = base64encode(""); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 31 | EXPECT_EQ(encoded, ""); |
| 32 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 33 | encoded = base64encode("f"); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 34 | EXPECT_EQ(encoded, "Zg=="); |
| 35 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 36 | encoded = base64encode("f0"); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 37 | EXPECT_EQ(encoded, "ZjA="); |
| 38 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 39 | encoded = base64encode("f0\0"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 40 | EXPECT_EQ(encoded, "ZjAA"); |
| 41 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 42 | encoded = base64encode("f0\0 "s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 43 | EXPECT_EQ(encoded, "ZjAAIA=="); |
| 44 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 45 | encoded = base64encode("f0\0 B"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 46 | EXPECT_EQ(encoded, "ZjAAIEI="); |
| 47 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 48 | encoded = base64encode("f0\0 Ba"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 49 | EXPECT_EQ(encoded, "ZjAAIEJh"); |
| 50 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 51 | encoded = base64encode("f0\0 Bar"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 52 | EXPECT_EQ(encoded, "ZjAAIEJhcg=="); |
| 53 | } |
| 54 | |
| 55 | TEST(Utility, Base64EncodeDecodeString) |
| 56 | { |
| 57 | using namespace std::string_literals; |
| 58 | std::string data("Data fr\0m 90 reading a \nFile"s); |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 59 | std::string encoded = base64encode(data); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 60 | std::string decoded; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 61 | EXPECT_TRUE(base64Decode(encoded, decoded)); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 62 | EXPECT_EQ(data, decoded); |
| 63 | } |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 64 | |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 65 | TEST(Utility, GetDateTimeStdtime) |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 66 | { |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 67 | using crow::utility::getDateTimeStdtime; |
| 68 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 69 | // some time before the epoch |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 70 | EXPECT_EQ(getDateTimeStdtime(std::time_t{-1234567}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 71 | "1969-12-17T17:03:53+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 72 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 73 | // epoch |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 74 | EXPECT_EQ(getDateTimeStdtime(std::time_t{0}), "1970-01-01T00:00:00+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 75 | |
| 76 | // Limits |
| 77 | EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::max()), |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 78 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 79 | EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::min()), |
| 80 | "1970-01-01T00:00:00+00:00"); |
| 81 | } |
| 82 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 83 | TEST(Utility, GetDateTimeUint) |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 84 | { |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 85 | EXPECT_EQ(getDateTimeUint(uint64_t{1638312095}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 86 | "2021-11-30T22:41:35+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 87 | // some time in the future, beyond 2038 |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 88 | EXPECT_EQ(getDateTimeUint(uint64_t{41638312095}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 89 | "3289-06-18T21:48:15+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 90 | // the maximum time we support |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 91 | EXPECT_EQ(getDateTimeUint(uint64_t{253402300799}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 92 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 93 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 94 | // returns the maximum Redfish date |
| 95 | EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::max()), |
| 96 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 97 | |
| 98 | EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::min()), |
| 99 | "1970-01-01T00:00:00+00:00"); |
| 100 | } |
| 101 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 102 | TEST(Utility, GetDateTimeUintMs) |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 103 | { |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 104 | // returns the maximum Redfish date |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 105 | EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::max()), |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 106 | "9999-12-31T23:59:59.999000+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 107 | EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()), |
| 108 | "1970-01-01T00:00:00+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 109 | } |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame^] | 110 | } // namespace |
| 111 | } // namespace crow::utility |