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