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 | |
Nan Zhou | 26500f2 | 2022-06-26 23:35:30 +0000 | [diff] [blame] | 5 | #include <boost/url/error.hpp> |
| 6 | #include <boost/url/url.hpp> |
| 7 | #include <boost/url/url_view.hpp> |
| 8 | #include <nlohmann/json.hpp> |
| 9 | |
| 10 | #include <cstdint> |
| 11 | #include <ctime> |
| 12 | #include <functional> |
| 13 | #include <limits> |
| 14 | #include <string> |
| 15 | #include <string_view> |
| 16 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 17 | #include <gtest/gtest.h> // IWYU pragma: keep |
| 18 | // IWYU pragma: no_include <gtest/gtest-message.h> |
| 19 | // IWYU pragma: no_include <gtest/gtest-test-part.h> |
| 20 | // IWYU pragma: no_include "gtest/gtest_pred_impl.h" |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 21 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 22 | namespace crow::utility |
| 23 | { |
| 24 | namespace |
| 25 | { |
| 26 | |
Nan Zhou | bf8ab7a | 2022-06-26 23:43:28 +0000 | [diff] [blame] | 27 | using ::crow::black_magic::getParameterTag; |
| 28 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 29 | TEST(Utility, Base64DecodeAuthString) |
| 30 | { |
| 31 | std::string authString("dXNlcm40bWU6cGFzc3cwcmQ="); |
| 32 | std::string result; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 33 | EXPECT_TRUE(base64Decode(authString, result)); |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 34 | EXPECT_EQ(result, "usern4me:passw0rd"); |
| 35 | } |
| 36 | |
| 37 | TEST(Utility, Base64DecodeNonAscii) |
| 38 | { |
| 39 | std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33"); |
| 40 | std::string result; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 41 | EXPECT_FALSE(base64Decode(junkString, result)); |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 42 | } |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 43 | |
| 44 | TEST(Utility, Base64EncodeString) |
| 45 | { |
| 46 | using namespace std::string_literals; |
| 47 | std::string encoded; |
| 48 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 49 | encoded = base64encode(""); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 50 | EXPECT_EQ(encoded, ""); |
| 51 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 52 | encoded = base64encode("f"); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 53 | EXPECT_EQ(encoded, "Zg=="); |
| 54 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 55 | encoded = base64encode("f0"); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 56 | EXPECT_EQ(encoded, "ZjA="); |
| 57 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 58 | encoded = base64encode("f0\0"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 59 | EXPECT_EQ(encoded, "ZjAA"); |
| 60 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 61 | encoded = base64encode("f0\0 "s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 62 | EXPECT_EQ(encoded, "ZjAAIA=="); |
| 63 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 64 | encoded = base64encode("f0\0 B"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 65 | EXPECT_EQ(encoded, "ZjAAIEI="); |
| 66 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 67 | encoded = base64encode("f0\0 Ba"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 68 | EXPECT_EQ(encoded, "ZjAAIEJh"); |
| 69 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 70 | encoded = base64encode("f0\0 Bar"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 71 | EXPECT_EQ(encoded, "ZjAAIEJhcg=="); |
| 72 | } |
| 73 | |
| 74 | TEST(Utility, Base64EncodeDecodeString) |
| 75 | { |
| 76 | using namespace std::string_literals; |
| 77 | std::string data("Data fr\0m 90 reading a \nFile"s); |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 78 | std::string encoded = base64encode(data); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 79 | std::string decoded; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 80 | EXPECT_TRUE(base64Decode(encoded, decoded)); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 81 | EXPECT_EQ(data, decoded); |
| 82 | } |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 83 | |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 84 | TEST(Utility, GetDateTimeStdtime) |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 85 | { |
| 86 | // some time before the epoch |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 87 | EXPECT_EQ(getDateTimeStdtime(std::time_t{-1234567}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 88 | "1969-12-17T17:03:53+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 89 | |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 90 | // epoch |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 91 | 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] | 92 | |
| 93 | // Limits |
| 94 | EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::max()), |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 95 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 96 | EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::min()), |
| 97 | "1970-01-01T00:00:00+00:00"); |
| 98 | } |
| 99 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 100 | TEST(Utility, GetDateTimeUint) |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 101 | { |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 102 | EXPECT_EQ(getDateTimeUint(uint64_t{1638312095}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 103 | "2021-11-30T22:41:35+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 104 | // some time in the future, beyond 2038 |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 105 | EXPECT_EQ(getDateTimeUint(uint64_t{41638312095}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 106 | "3289-06-18T21:48:15+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 107 | // the maximum time we support |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 108 | EXPECT_EQ(getDateTimeUint(uint64_t{253402300799}), |
Nan Zhou | 5ae4b69 | 2021-12-14 13:30:37 -0800 | [diff] [blame] | 109 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 110 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 111 | // returns the maximum Redfish date |
| 112 | EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::max()), |
| 113 | "9999-12-31T23:59:59+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 114 | |
| 115 | EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::min()), |
| 116 | "1970-01-01T00:00:00+00:00"); |
| 117 | } |
| 118 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 119 | TEST(Utility, GetDateTimeUintMs) |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 120 | { |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 121 | // returns the maximum Redfish date |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 122 | EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::max()), |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 123 | "9999-12-31T23:59:59.999000+00:00"); |
Ed Tanous | 8d4c487 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 124 | EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()), |
| 125 | "1970-01-01T00:00:00+00:00"); |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 126 | } |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 127 | |
| 128 | TEST(Utility, UrlFromPieces) |
| 129 | { |
Ed Tanous | eae855c | 2021-10-26 11:26:02 -0700 | [diff] [blame] | 130 | boost::urls::url url = urlFromPieces("redfish", "v1", "foo"); |
| 131 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo"); |
| 132 | |
| 133 | url = urlFromPieces("/", "badString"); |
| 134 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/badString"); |
| 135 | |
| 136 | url = urlFromPieces("bad?tring"); |
| 137 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/bad%3ftring"); |
| 138 | |
| 139 | url = urlFromPieces("/", "bad&tring"); |
| 140 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/bad&tring"); |
| 141 | } |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 142 | |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 143 | TEST(Utility, readUrlSegments) |
| 144 | { |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 145 | boost::urls::result<boost::urls::url_view> parsed = |
| 146 | boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading"); |
| 147 | |
| 148 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis")); |
| 149 | |
| 150 | EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis")); |
| 151 | |
| 152 | EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1")); |
| 153 | |
| 154 | EXPECT_FALSE( |
| 155 | readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR")); |
| 156 | |
| 157 | std::string out1; |
| 158 | std::string out2; |
| 159 | std::string out3; |
| 160 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1))); |
| 161 | EXPECT_EQ(out1, "Chassis"); |
| 162 | |
| 163 | out1 = out2 = out3 = ""; |
| 164 | EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2), |
| 165 | std::ref(out3))); |
| 166 | EXPECT_EQ(out1, "redfish"); |
| 167 | EXPECT_EQ(out2, "v1"); |
| 168 | EXPECT_EQ(out3, "Chassis"); |
| 169 | |
| 170 | out1 = out2 = out3 = ""; |
| 171 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis")); |
| 172 | EXPECT_EQ(out1, "v1"); |
| 173 | |
| 174 | out1 = out2 = out3 = ""; |
| 175 | EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2))); |
| 176 | EXPECT_EQ(out1, "redfish"); |
| 177 | EXPECT_EQ(out2, "Chassis"); |
| 178 | |
| 179 | EXPECT_FALSE(readUrlSegments(*parsed, "too", "short")); |
| 180 | |
| 181 | EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long")); |
| 182 | |
| 183 | EXPECT_FALSE( |
| 184 | readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2))); |
| 185 | |
| 186 | EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1), |
| 187 | std::ref(out2), std::ref(out3))); |
| 188 | |
| 189 | parsed = boost::urls::parse_relative_ref("/absolute/url"); |
| 190 | EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url")); |
| 191 | |
| 192 | parsed = boost::urls::parse_relative_ref("not/absolute/url"); |
| 193 | EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url")); |
| 194 | } |
| 195 | |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 196 | TEST(Utility, ValidateAndSplitUrlPositive) |
| 197 | { |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 198 | std::string host; |
| 199 | std::string urlProto; |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 200 | uint16_t port = 0; |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 201 | std::string path; |
| 202 | ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar", urlProto, host, |
| 203 | port, path)); |
| 204 | EXPECT_EQ(host, "foo.com"); |
| 205 | EXPECT_EQ(urlProto, "https"); |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 206 | EXPECT_EQ(port, 18080); |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 207 | |
| 208 | EXPECT_EQ(path, "/bar"); |
| 209 | |
| 210 | // query string |
| 211 | ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar?foobar=1", |
| 212 | urlProto, host, port, path)); |
| 213 | EXPECT_EQ(path, "/bar?foobar=1"); |
| 214 | |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 215 | // fragment |
| 216 | ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar#frag", urlProto, |
| 217 | host, port, path)); |
| 218 | EXPECT_EQ(path, "/bar#frag"); |
| 219 | |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 220 | // Missing port |
| 221 | ASSERT_TRUE( |
| 222 | validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path)); |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 223 | EXPECT_EQ(port, 443); |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 224 | |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 225 | // Missing path defaults to "/" |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 226 | ASSERT_TRUE( |
Ed Tanous | eb1c47d | 2022-02-09 11:47:27 -0800 | [diff] [blame] | 227 | validateAndSplitUrl("https://foo.com/", urlProto, host, port, path)); |
| 228 | EXPECT_EQ(path, "/"); |
| 229 | |
| 230 | // If http push eventing is allowed, allow http and pick a default port of |
| 231 | // 80, if it's not, parse should fail. |
| 232 | ASSERT_EQ( |
| 233 | validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path), |
| 234 | bmcwebInsecureEnableHttpPushStyleEventing); |
| 235 | if constexpr (bmcwebInsecureEnableHttpPushStyleEventing) |
| 236 | { |
| 237 | EXPECT_EQ(port, 80); |
| 238 | } |
Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Ed Tanous | 4456f08 | 2022-03-08 18:01:18 -0800 | [diff] [blame] | 241 | TEST(Router, ParameterTagging) |
| 242 | { |
Nan Zhou | bf8ab7a | 2022-06-26 23:43:28 +0000 | [diff] [blame] | 243 | EXPECT_EQ(6 * 6 + 6 * 3 + 2, getParameterTag("<uint><double><int>")); |
| 244 | EXPECT_EQ(1, getParameterTag("<int>")); |
| 245 | EXPECT_EQ(2, getParameterTag("<uint>")); |
| 246 | EXPECT_EQ(3, getParameterTag("<float>")); |
| 247 | EXPECT_EQ(3, getParameterTag("<double>")); |
| 248 | EXPECT_EQ(4, getParameterTag("<str>")); |
| 249 | EXPECT_EQ(4, getParameterTag("<string>")); |
| 250 | EXPECT_EQ(5, getParameterTag("<path>")); |
| 251 | EXPECT_EQ(6 * 6 + 6 + 1, getParameterTag("<int><int><int>")); |
| 252 | EXPECT_EQ(6 * 6 + 6 + 2, getParameterTag("<uint><int><int>")); |
| 253 | EXPECT_EQ(6 * 6 + 6 * 3 + 2, getParameterTag("<uint><double><int>")); |
Ed Tanous | 4456f08 | 2022-03-08 18:01:18 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 256 | TEST(URL, JsonEncoding) |
| 257 | { |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 258 | std::string urlString = "/foo"; |
Nan Zhou | bf8ab7a | 2022-06-26 23:43:28 +0000 | [diff] [blame] | 259 | EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString); |
| 260 | EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString); |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 263 | } // namespace |
| 264 | } // namespace crow::utility |