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 | |
Ed Tanous | 47488a9 | 2023-06-26 18:19:33 -0700 | [diff] [blame] | 27 | using ::crow::utility::getParameterTag; |
Nan Zhou | bf8ab7a | 2022-06-26 23:43:28 +0000 | [diff] [blame] | 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 | |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 74 | TEST(Utility, Base64Encoder) |
| 75 | { |
| 76 | using namespace std::string_literals; |
| 77 | std::string data = "f0\0 Bar"s; |
| 78 | for (size_t chunkSize = 1; chunkSize < 6; chunkSize++) |
| 79 | { |
| 80 | std::string_view testString(data); |
| 81 | std::string out; |
| 82 | Base64Encoder encoder; |
| 83 | while (!testString.empty()) |
| 84 | { |
| 85 | size_t thisChunk = std::min(testString.size(), chunkSize); |
| 86 | encoder.encode(testString.substr(0, thisChunk), out); |
| 87 | testString.remove_prefix(thisChunk); |
| 88 | } |
| 89 | |
| 90 | encoder.finalize(out); |
| 91 | EXPECT_EQ(out, "ZjAAIEJhcg=="); |
| 92 | } |
| 93 | } |
| 94 | |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 95 | TEST(Utility, Base64EncodeDecodeString) |
| 96 | { |
| 97 | using namespace std::string_literals; |
| 98 | std::string data("Data fr\0m 90 reading a \nFile"s); |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 99 | std::string encoded = base64encode(data); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 100 | std::string decoded; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 101 | EXPECT_TRUE(base64Decode(encoded, decoded)); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 102 | EXPECT_EQ(data, decoded); |
| 103 | } |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 104 | |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 105 | TEST(Utility, readUrlSegments) |
| 106 | { |
Ed Tanous | 6fd2955 | 2023-10-04 09:40:14 -0700 | [diff] [blame] | 107 | boost::system::result<boost::urls::url_view> parsed = |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 108 | boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading"); |
| 109 | |
| 110 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis")); |
| 111 | |
| 112 | EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis")); |
| 113 | |
| 114 | EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1")); |
| 115 | |
| 116 | EXPECT_FALSE( |
| 117 | readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR")); |
| 118 | |
| 119 | std::string out1; |
| 120 | std::string out2; |
| 121 | std::string out3; |
| 122 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1))); |
| 123 | EXPECT_EQ(out1, "Chassis"); |
| 124 | |
| 125 | out1 = out2 = out3 = ""; |
| 126 | EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2), |
| 127 | std::ref(out3))); |
| 128 | EXPECT_EQ(out1, "redfish"); |
| 129 | EXPECT_EQ(out2, "v1"); |
| 130 | EXPECT_EQ(out3, "Chassis"); |
| 131 | |
| 132 | out1 = out2 = out3 = ""; |
| 133 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis")); |
| 134 | EXPECT_EQ(out1, "v1"); |
| 135 | |
| 136 | out1 = out2 = out3 = ""; |
| 137 | EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2))); |
| 138 | EXPECT_EQ(out1, "redfish"); |
| 139 | EXPECT_EQ(out2, "Chassis"); |
| 140 | |
| 141 | EXPECT_FALSE(readUrlSegments(*parsed, "too", "short")); |
| 142 | |
| 143 | EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long")); |
| 144 | |
| 145 | EXPECT_FALSE( |
| 146 | readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2))); |
| 147 | |
| 148 | EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1), |
| 149 | std::ref(out2), std::ref(out3))); |
| 150 | |
| 151 | parsed = boost::urls::parse_relative_ref("/absolute/url"); |
| 152 | EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url")); |
| 153 | |
| 154 | parsed = boost::urls::parse_relative_ref("not/absolute/url"); |
| 155 | EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url")); |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 156 | |
| 157 | parsed = boost::urls::parse_relative_ref("/excellent/path"); |
| 158 | |
| 159 | EXPECT_TRUE(readUrlSegments(*parsed, "excellent", "path", OrMorePaths())); |
| 160 | EXPECT_TRUE(readUrlSegments(*parsed, "excellent", OrMorePaths())); |
| 161 | EXPECT_TRUE(readUrlSegments(*parsed, OrMorePaths())); |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Ed Tanous | 4456f08 | 2022-03-08 18:01:18 -0800 | [diff] [blame] | 164 | TEST(Router, ParameterTagging) |
| 165 | { |
Ed Tanous | 15a42df | 2023-02-09 18:08:23 -0800 | [diff] [blame] | 166 | EXPECT_EQ(1, getParameterTag("<str>")); |
| 167 | EXPECT_EQ(1, getParameterTag("<string>")); |
| 168 | EXPECT_EQ(2, getParameterTag("<path>")); |
Ed Tanous | 4456f08 | 2022-03-08 18:01:18 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 171 | TEST(URL, JsonEncoding) |
| 172 | { |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 173 | std::string urlString = "/foo"; |
Nan Zhou | bf8ab7a | 2022-06-26 23:43:28 +0000 | [diff] [blame] | 174 | EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString); |
| 175 | EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString); |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 178 | TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters) |
| 179 | { |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 180 | boost::urls::url url("/redfish/v1/foo"); |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 181 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo"); |
| 182 | |
| 183 | appendUrlPieces(url, "bar"); |
| 184 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo/bar"); |
| 185 | |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 186 | appendUrlPieces(url, "/", "bad&string"); |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 187 | EXPECT_EQ(std::string_view(url.data(), url.size()), |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 188 | "/redfish/v1/foo/bar/%2F/bad&string"); |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 191 | } // namespace |
| 192 | } // namespace crow::utility |