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 | |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 130 | TEST(Utility, readUrlSegments) |
| 131 | { |
| 132 | using crow::utility::readUrlSegments; |
| 133 | |
| 134 | boost::urls::result<boost::urls::url_view> parsed = |
| 135 | boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading"); |
| 136 | |
| 137 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis")); |
| 138 | |
| 139 | EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis")); |
| 140 | |
| 141 | EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1")); |
| 142 | |
| 143 | EXPECT_FALSE( |
| 144 | readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR")); |
| 145 | |
| 146 | std::string out1; |
| 147 | std::string out2; |
| 148 | std::string out3; |
| 149 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1))); |
| 150 | EXPECT_EQ(out1, "Chassis"); |
| 151 | |
| 152 | out1 = out2 = out3 = ""; |
| 153 | EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2), |
| 154 | std::ref(out3))); |
| 155 | EXPECT_EQ(out1, "redfish"); |
| 156 | EXPECT_EQ(out2, "v1"); |
| 157 | EXPECT_EQ(out3, "Chassis"); |
| 158 | |
| 159 | out1 = out2 = out3 = ""; |
| 160 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis")); |
| 161 | EXPECT_EQ(out1, "v1"); |
| 162 | |
| 163 | out1 = out2 = out3 = ""; |
| 164 | EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2))); |
| 165 | EXPECT_EQ(out1, "redfish"); |
| 166 | EXPECT_EQ(out2, "Chassis"); |
| 167 | |
| 168 | EXPECT_FALSE(readUrlSegments(*parsed, "too", "short")); |
| 169 | |
| 170 | EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long")); |
| 171 | |
| 172 | EXPECT_FALSE( |
| 173 | readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2))); |
| 174 | |
| 175 | EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1), |
| 176 | std::ref(out2), std::ref(out3))); |
| 177 | |
| 178 | parsed = boost::urls::parse_relative_ref("/absolute/url"); |
| 179 | EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url")); |
| 180 | |
| 181 | parsed = boost::urls::parse_relative_ref("not/absolute/url"); |
| 182 | EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url")); |
| 183 | } |
| 184 | |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 185 | TEST(Utility, ValidateAndSplitUrlPositive) |
| 186 | { |
| 187 | using crow::utility::validateAndSplitUrl; |
| 188 | std::string host; |
| 189 | std::string urlProto; |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 190 | uint16_t port = 0; |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 191 | std::string path; |
| 192 | ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar", urlProto, host, |
| 193 | port, path)); |
| 194 | EXPECT_EQ(host, "foo.com"); |
| 195 | EXPECT_EQ(urlProto, "https"); |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 196 | EXPECT_EQ(port, 18080); |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 197 | |
| 198 | EXPECT_EQ(path, "/bar"); |
| 199 | |
| 200 | // query string |
| 201 | ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar?foobar=1", |
| 202 | urlProto, host, port, path)); |
| 203 | EXPECT_EQ(path, "/bar?foobar=1"); |
| 204 | |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 205 | // fragment |
| 206 | ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar#frag", urlProto, |
| 207 | host, port, path)); |
| 208 | EXPECT_EQ(path, "/bar#frag"); |
| 209 | |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 210 | // Missing port |
| 211 | ASSERT_TRUE( |
| 212 | validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path)); |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 213 | EXPECT_EQ(port, 443); |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 214 | |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 215 | // Missing path defaults to "/" |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 216 | ASSERT_TRUE( |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 217 | validateAndSplitUrl("https://foo.com/", urlProto, host, port, path)); |
| 218 | EXPECT_EQ(path, "/"); |
| 219 | |
| 220 | // If http push eventing is allowed, allow http and pick a default port of |
| 221 | // 80, if it's not, parse should fail. |
| 222 | ASSERT_EQ( |
| 223 | validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path), |
| 224 | bmcwebInsecureEnableHttpPushStyleEventing); |
| 225 | if constexpr (bmcwebInsecureEnableHttpPushStyleEventing) |
| 226 | { |
| 227 | EXPECT_EQ(port, 80); |
| 228 | } |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Ed Tanous | 4456f08 | 2022-03-08 18:01:18 -0800 | [diff] [blame] | 231 | TEST(Router, ParameterTagging) |
| 232 | { |
| 233 | EXPECT_EQ(6 * 6 + 6 * 3 + 2, |
| 234 | crow::black_magic::getParameterTag("<uint><double><int>")); |
| 235 | EXPECT_EQ(1, crow::black_magic::getParameterTag("<int>")); |
| 236 | EXPECT_EQ(2, crow::black_magic::getParameterTag("<uint>")); |
| 237 | EXPECT_EQ(3, crow::black_magic::getParameterTag("<float>")); |
| 238 | EXPECT_EQ(3, crow::black_magic::getParameterTag("<double>")); |
| 239 | EXPECT_EQ(4, crow::black_magic::getParameterTag("<str>")); |
| 240 | EXPECT_EQ(4, crow::black_magic::getParameterTag("<string>")); |
| 241 | EXPECT_EQ(5, crow::black_magic::getParameterTag("<path>")); |
| 242 | EXPECT_EQ(6 * 6 + 6 + 1, |
| 243 | crow::black_magic::getParameterTag("<int><int><int>")); |
| 244 | EXPECT_EQ(6 * 6 + 6 + 2, |
| 245 | crow::black_magic::getParameterTag("<uint><int><int>")); |
| 246 | EXPECT_EQ(6 * 6 + 6 * 3 + 2, |
| 247 | crow::black_magic::getParameterTag("<uint><double><int>")); |
| 248 | } |
| 249 | |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame^] | 250 | TEST(URL, JsonEncoding) |
| 251 | { |
| 252 | using nlohmann::json; |
| 253 | |
| 254 | std::string urlString = "/foo"; |
| 255 | EXPECT_EQ(json(boost::urls::url(urlString)), urlString); |
| 256 | EXPECT_EQ(json(boost::urls::url_view(urlString)), urlString); |
| 257 | } |
| 258 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 259 | } // namespace |
| 260 | } // namespace crow::utility |