Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 1 | #include "bmcweb_config.h" |
| 2 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "utility.hpp" |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 4 | |
| 5 | #include "gmock/gmock.h" |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 6 | #include "gtest/gtest.h" |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 7 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 8 | namespace crow::utility |
| 9 | { |
| 10 | namespace |
| 11 | { |
| 12 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 13 | TEST(Utility, Base64DecodeAuthString) |
| 14 | { |
| 15 | std::string authString("dXNlcm40bWU6cGFzc3cwcmQ="); |
| 16 | std::string result; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 17 | EXPECT_TRUE(base64Decode(authString, result)); |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 18 | EXPECT_EQ(result, "usern4me:passw0rd"); |
| 19 | } |
| 20 | |
| 21 | TEST(Utility, Base64DecodeNonAscii) |
| 22 | { |
| 23 | std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33"); |
| 24 | std::string result; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 25 | EXPECT_FALSE(base64Decode(junkString, result)); |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 26 | } |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 27 | |
| 28 | TEST(Utility, Base64EncodeString) |
| 29 | { |
| 30 | using namespace std::string_literals; |
| 31 | std::string encoded; |
| 32 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 33 | encoded = base64encode(""); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 34 | EXPECT_EQ(encoded, ""); |
| 35 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 36 | encoded = base64encode("f"); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 37 | EXPECT_EQ(encoded, "Zg=="); |
| 38 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 39 | encoded = base64encode("f0"); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 40 | EXPECT_EQ(encoded, "ZjA="); |
| 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, "ZjAA"); |
| 44 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 45 | encoded = base64encode("f0\0 "s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 46 | EXPECT_EQ(encoded, "ZjAAIA=="); |
| 47 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 48 | encoded = base64encode("f0\0 B"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 49 | EXPECT_EQ(encoded, "ZjAAIEI="); |
| 50 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 51 | encoded = base64encode("f0\0 Ba"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 52 | EXPECT_EQ(encoded, "ZjAAIEJh"); |
| 53 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 54 | encoded = base64encode("f0\0 Bar"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 55 | EXPECT_EQ(encoded, "ZjAAIEJhcg=="); |
| 56 | } |
| 57 | |
| 58 | TEST(Utility, Base64EncodeDecodeString) |
| 59 | { |
| 60 | using namespace std::string_literals; |
| 61 | std::string data("Data fr\0m 90 reading a \nFile"s); |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 62 | std::string encoded = base64encode(data); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 63 | std::string decoded; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 64 | EXPECT_TRUE(base64Decode(encoded, decoded)); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 65 | EXPECT_EQ(data, decoded); |
| 66 | } |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 67 | |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 68 | TEST(Utility, GetDateTimeStdtime) |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 69 | { |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 70 | using crow::utility::getDateTimeStdtime; |
| 71 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 72 | // some time before the epoch |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 73 | EXPECT_EQ(getDateTimeStdtime(std::time_t{-1234567}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 74 | "1969-12-17T17:03:53+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 75 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 76 | // epoch |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 77 | 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] | 78 | |
| 79 | // Limits |
| 80 | EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::max()), |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 81 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 82 | EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::min()), |
| 83 | "1970-01-01T00:00:00+00:00"); |
| 84 | } |
| 85 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 86 | TEST(Utility, GetDateTimeUint) |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 87 | { |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 88 | EXPECT_EQ(getDateTimeUint(uint64_t{1638312095}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 89 | "2021-11-30T22:41:35+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 90 | // some time in the future, beyond 2038 |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 91 | EXPECT_EQ(getDateTimeUint(uint64_t{41638312095}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 92 | "3289-06-18T21:48:15+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 93 | // the maximum time we support |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 94 | EXPECT_EQ(getDateTimeUint(uint64_t{253402300799}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 95 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 96 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 97 | // returns the maximum Redfish date |
| 98 | EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::max()), |
| 99 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 100 | |
| 101 | EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::min()), |
| 102 | "1970-01-01T00:00:00+00:00"); |
| 103 | } |
| 104 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 105 | TEST(Utility, GetDateTimeUintMs) |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 106 | { |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 107 | // returns the maximum Redfish date |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 108 | EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::max()), |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 109 | "9999-12-31T23:59:59.999000+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 110 | EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()), |
| 111 | "1970-01-01T00:00:00+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 112 | } |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 113 | |
| 114 | TEST(Utility, UrlFromPieces) |
| 115 | { |
| 116 | using crow::utility::urlFromPieces; |
| 117 | boost::urls::url url = urlFromPieces("redfish", "v1", "foo"); |
| 118 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo"); |
| 119 | |
| 120 | url = urlFromPieces("/", "badString"); |
| 121 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/badString"); |
| 122 | |
| 123 | url = urlFromPieces("bad?tring"); |
| 124 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/bad%3ftring"); |
| 125 | |
| 126 | url = urlFromPieces("/", "bad&tring"); |
| 127 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/bad&tring"); |
| 128 | } |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 129 | |
| 130 | TEST(Utility, ValidateAndSplitUrlPositive) |
| 131 | { |
| 132 | using crow::utility::validateAndSplitUrl; |
| 133 | std::string host; |
| 134 | std::string urlProto; |
| 135 | std::string port; |
| 136 | std::string path; |
| 137 | ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar", urlProto, host, |
| 138 | port, path)); |
| 139 | EXPECT_EQ(host, "foo.com"); |
| 140 | EXPECT_EQ(urlProto, "https"); |
| 141 | EXPECT_EQ(port, "18080"); |
| 142 | |
| 143 | EXPECT_EQ(path, "/bar"); |
| 144 | |
| 145 | // query string |
| 146 | ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar?foobar=1", |
| 147 | urlProto, host, port, path)); |
| 148 | EXPECT_EQ(path, "/bar?foobar=1"); |
| 149 | |
| 150 | // Missing port |
| 151 | ASSERT_TRUE( |
| 152 | validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path)); |
| 153 | EXPECT_EQ(port, "443"); |
| 154 | |
| 155 | // If http push eventing is allowed, allow http, if it's not, parse should |
| 156 | // fail. |
| 157 | #ifdef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING |
| 158 | ASSERT_TRUE( |
| 159 | validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path)); |
| 160 | EXPECT_EQ(port, "80"); |
| 161 | #else |
| 162 | ASSERT_FALSE( |
| 163 | validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path)); |
| 164 | #endif |
| 165 | } |
| 166 | |
Ed Tanous | 4456f08 | 2022-03-08 18:01:18 -0800 | [diff] [blame^] | 167 | TEST(Router, ParameterTagging) |
| 168 | { |
| 169 | EXPECT_EQ(6 * 6 + 6 * 3 + 2, |
| 170 | crow::black_magic::getParameterTag("<uint><double><int>")); |
| 171 | EXPECT_EQ(1, crow::black_magic::getParameterTag("<int>")); |
| 172 | EXPECT_EQ(2, crow::black_magic::getParameterTag("<uint>")); |
| 173 | EXPECT_EQ(3, crow::black_magic::getParameterTag("<float>")); |
| 174 | EXPECT_EQ(3, crow::black_magic::getParameterTag("<double>")); |
| 175 | EXPECT_EQ(4, crow::black_magic::getParameterTag("<str>")); |
| 176 | EXPECT_EQ(4, crow::black_magic::getParameterTag("<string>")); |
| 177 | EXPECT_EQ(5, crow::black_magic::getParameterTag("<path>")); |
| 178 | EXPECT_EQ(6 * 6 + 6 + 1, |
| 179 | crow::black_magic::getParameterTag("<int><int><int>")); |
| 180 | EXPECT_EQ(6 * 6 + 6 + 2, |
| 181 | crow::black_magic::getParameterTag("<uint><int><int>")); |
| 182 | EXPECT_EQ(6 * 6 + 6 * 3 + 2, |
| 183 | crow::black_magic::getParameterTag("<uint><double><int>")); |
| 184 | } |
| 185 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 186 | } // namespace |
| 187 | } // namespace crow::utility |