Ed Tanous | 11baefe | 2022-02-09 12:14:12 -0800 | [diff] [blame] | 1 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 2 | #include "utility.hpp" |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 3 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 4 | #include <boost/system/result.hpp> |
| 5 | #include <boost/url/parse.hpp> |
Nan Zhou | 26500f2 | 2022-06-26 23:35:30 +0000 | [diff] [blame] | 6 | #include <boost/url/url.hpp> |
| 7 | #include <boost/url/url_view.hpp> |
Nan Zhou | 26500f2 | 2022-06-26 23:35:30 +0000 | [diff] [blame] | 8 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 9 | #include <algorithm> |
Nan Zhou | 26500f2 | 2022-06-26 23:35:30 +0000 | [diff] [blame] | 10 | #include <ctime> |
| 11 | #include <functional> |
Nan Zhou | 26500f2 | 2022-06-26 23:35:30 +0000 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <string_view> |
| 14 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 15 | #include <gtest/gtest.h> // IWYU pragma: keep |
| 16 | // IWYU pragma: no_include <gtest/gtest-message.h> |
| 17 | // IWYU pragma: no_include <gtest/gtest-test-part.h> |
| 18 | // IWYU pragma: no_include "gtest/gtest_pred_impl.h" |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 19 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 20 | namespace crow::utility |
| 21 | { |
| 22 | namespace |
| 23 | { |
| 24 | |
Ed Tanous | 47488a9 | 2023-06-26 18:19:33 -0700 | [diff] [blame] | 25 | using ::crow::utility::getParameterTag; |
Nan Zhou | bf8ab7a | 2022-06-26 23:43:28 +0000 | [diff] [blame] | 26 | |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 27 | TEST(Utility, Base64DecodeAuthString) |
| 28 | { |
| 29 | std::string authString("dXNlcm40bWU6cGFzc3cwcmQ="); |
| 30 | std::string result; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 31 | EXPECT_TRUE(base64Decode(authString, result)); |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 32 | EXPECT_EQ(result, "usern4me:passw0rd"); |
| 33 | } |
| 34 | |
| 35 | TEST(Utility, Base64DecodeNonAscii) |
| 36 | { |
| 37 | std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33"); |
| 38 | std::string result; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 39 | EXPECT_FALSE(base64Decode(junkString, result)); |
Jonathan Doman | 5beaf84 | 2020-08-14 11:23:33 -0700 | [diff] [blame] | 40 | } |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 41 | |
| 42 | TEST(Utility, Base64EncodeString) |
| 43 | { |
| 44 | using namespace std::string_literals; |
| 45 | std::string encoded; |
| 46 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 47 | encoded = base64encode(""); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 48 | EXPECT_EQ(encoded, ""); |
| 49 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 50 | encoded = base64encode("f"); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 51 | EXPECT_EQ(encoded, "Zg=="); |
| 52 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 53 | encoded = base64encode("f0"); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 54 | EXPECT_EQ(encoded, "ZjA="); |
| 55 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 56 | encoded = base64encode("f0\0"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 57 | EXPECT_EQ(encoded, "ZjAA"); |
| 58 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 59 | encoded = base64encode("f0\0 "s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 60 | EXPECT_EQ(encoded, "ZjAAIA=="); |
| 61 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 62 | encoded = base64encode("f0\0 B"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 63 | EXPECT_EQ(encoded, "ZjAAIEI="); |
| 64 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 65 | encoded = base64encode("f0\0 Ba"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 66 | EXPECT_EQ(encoded, "ZjAAIEJh"); |
| 67 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 68 | encoded = base64encode("f0\0 Bar"s); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 69 | EXPECT_EQ(encoded, "ZjAAIEJhcg=="); |
| 70 | } |
| 71 | |
Ed Tanous | ee192c0 | 2023-12-13 10:49:58 -0800 | [diff] [blame] | 72 | TEST(Utility, Base64Encoder) |
| 73 | { |
| 74 | using namespace std::string_literals; |
| 75 | std::string data = "f0\0 Bar"s; |
| 76 | for (size_t chunkSize = 1; chunkSize < 6; chunkSize++) |
| 77 | { |
| 78 | std::string_view testString(data); |
| 79 | std::string out; |
| 80 | Base64Encoder encoder; |
| 81 | while (!testString.empty()) |
| 82 | { |
| 83 | size_t thisChunk = std::min(testString.size(), chunkSize); |
| 84 | encoder.encode(testString.substr(0, thisChunk), out); |
| 85 | testString.remove_prefix(thisChunk); |
| 86 | } |
| 87 | |
| 88 | encoder.finalize(out); |
| 89 | EXPECT_EQ(out, "ZjAAIEJhcg=="); |
| 90 | } |
| 91 | } |
| 92 | |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 93 | TEST(Utility, Base64EncodeDecodeString) |
| 94 | { |
| 95 | using namespace std::string_literals; |
| 96 | std::string data("Data fr\0m 90 reading a \nFile"s); |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 97 | std::string encoded = base64encode(data); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 98 | std::string decoded; |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 99 | EXPECT_TRUE(base64Decode(encoded, decoded)); |
Adriana Kobylak | d830ff5 | 2021-01-27 14:15:27 -0600 | [diff] [blame] | 100 | EXPECT_EQ(data, decoded); |
| 101 | } |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 102 | |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 103 | TEST(Utility, readUrlSegments) |
| 104 | { |
Ed Tanous | 6fd2955 | 2023-10-04 09:40:14 -0700 | [diff] [blame] | 105 | boost::system::result<boost::urls::url_view> parsed = |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 106 | boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading"); |
| 107 | |
| 108 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis")); |
| 109 | |
| 110 | EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis")); |
| 111 | |
| 112 | EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1")); |
| 113 | |
| 114 | EXPECT_FALSE( |
| 115 | readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR")); |
| 116 | |
| 117 | std::string out1; |
| 118 | std::string out2; |
| 119 | std::string out3; |
| 120 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1))); |
| 121 | EXPECT_EQ(out1, "Chassis"); |
| 122 | |
| 123 | out1 = out2 = out3 = ""; |
| 124 | EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2), |
| 125 | std::ref(out3))); |
| 126 | EXPECT_EQ(out1, "redfish"); |
| 127 | EXPECT_EQ(out2, "v1"); |
| 128 | EXPECT_EQ(out3, "Chassis"); |
| 129 | |
| 130 | out1 = out2 = out3 = ""; |
| 131 | EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis")); |
| 132 | EXPECT_EQ(out1, "v1"); |
| 133 | |
| 134 | out1 = out2 = out3 = ""; |
| 135 | EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2))); |
| 136 | EXPECT_EQ(out1, "redfish"); |
| 137 | EXPECT_EQ(out2, "Chassis"); |
| 138 | |
| 139 | EXPECT_FALSE(readUrlSegments(*parsed, "too", "short")); |
| 140 | |
| 141 | EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long")); |
| 142 | |
| 143 | EXPECT_FALSE( |
| 144 | readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2))); |
| 145 | |
| 146 | EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1), |
| 147 | std::ref(out2), std::ref(out3))); |
| 148 | |
| 149 | parsed = boost::urls::parse_relative_ref("/absolute/url"); |
| 150 | EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url")); |
| 151 | |
| 152 | parsed = boost::urls::parse_relative_ref("not/absolute/url"); |
| 153 | EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url")); |
Ed Tanous | 7f8d8fa | 2022-08-19 07:00:38 -0700 | [diff] [blame] | 154 | |
| 155 | parsed = boost::urls::parse_relative_ref("/excellent/path"); |
| 156 | |
| 157 | EXPECT_TRUE(readUrlSegments(*parsed, "excellent", "path", OrMorePaths())); |
| 158 | EXPECT_TRUE(readUrlSegments(*parsed, "excellent", OrMorePaths())); |
| 159 | EXPECT_TRUE(readUrlSegments(*parsed, OrMorePaths())); |
Szymon Dompke | ca1600c | 2022-03-03 14:42:52 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Ed Tanous | 4456f08 | 2022-03-08 18:01:18 -0800 | [diff] [blame] | 162 | TEST(Router, ParameterTagging) |
| 163 | { |
Ed Tanous | 15a42df | 2023-02-09 18:08:23 -0800 | [diff] [blame] | 164 | EXPECT_EQ(1, getParameterTag("<str>")); |
| 165 | EXPECT_EQ(1, getParameterTag("<string>")); |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame^] | 166 | EXPECT_EQ(1, getParameterTag("<path>")); |
| 167 | EXPECT_EQ(2, getParameterTag("<str>/<str>")); |
| 168 | EXPECT_EQ(2, getParameterTag("<string>/<string>")); |
| 169 | EXPECT_EQ(2, getParameterTag("<path>/<path>")); |
Ed Tanous | 4456f08 | 2022-03-08 18:01:18 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 172 | TEST(URL, JsonEncoding) |
| 173 | { |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 174 | std::string urlString = "/foo"; |
Nan Zhou | bf8ab7a | 2022-06-26 23:43:28 +0000 | [diff] [blame] | 175 | EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString); |
| 176 | EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString); |
Ed Tanous | 71f2db7 | 2022-05-25 12:28:09 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 179 | TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters) |
| 180 | { |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 181 | boost::urls::url url("/redfish/v1/foo"); |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 182 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo"); |
| 183 | |
| 184 | appendUrlPieces(url, "bar"); |
| 185 | EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo/bar"); |
| 186 | |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 187 | appendUrlPieces(url, "/", "bad&string"); |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 188 | EXPECT_EQ(std::string_view(url.data(), url.size()), |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame] | 189 | "/redfish/v1/foo/bar/%2F/bad&string"); |
Willy Tu | c6bcedc | 2022-09-27 05:36:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Ed Tanous | 22ce545 | 2022-01-11 10:50:23 -0800 | [diff] [blame] | 192 | } // namespace |
| 193 | } // namespace crow::utility |