blob: 957c13d2db37a945debf0b0c36a16f93d0528a4c [file] [log] [blame]
Ed Tanous11baefe2022-02-09 12:14:12 -08001#include "bmcweb_config.h"
2
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include "utility.hpp"
Jonathan Doman5beaf842020-08-14 11:23:33 -07004
Nan Zhou26500f22022-06-26 23:35:30 +00005#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 Zhoud5c80ad2022-07-11 01:16:31 +000017#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 Doman5beaf842020-08-14 11:23:33 -070021
Ed Tanous22ce5452022-01-11 10:50:23 -080022namespace crow::utility
23{
24namespace
25{
26
Nan Zhoubf8ab7a2022-06-26 23:43:28 +000027using ::crow::black_magic::getParameterTag;
28
Jonathan Doman5beaf842020-08-14 11:23:33 -070029TEST(Utility, Base64DecodeAuthString)
30{
31 std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
32 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080033 EXPECT_TRUE(base64Decode(authString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070034 EXPECT_EQ(result, "usern4me:passw0rd");
35}
36
37TEST(Utility, Base64DecodeNonAscii)
38{
39 std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
40 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080041 EXPECT_FALSE(base64Decode(junkString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070042}
Adriana Kobylakd830ff52021-01-27 14:15:27 -060043
44TEST(Utility, Base64EncodeString)
45{
46 using namespace std::string_literals;
47 std::string encoded;
48
Ed Tanous22ce5452022-01-11 10:50:23 -080049 encoded = base64encode("");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060050 EXPECT_EQ(encoded, "");
51
Ed Tanous22ce5452022-01-11 10:50:23 -080052 encoded = base64encode("f");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060053 EXPECT_EQ(encoded, "Zg==");
54
Ed Tanous22ce5452022-01-11 10:50:23 -080055 encoded = base64encode("f0");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060056 EXPECT_EQ(encoded, "ZjA=");
57
Ed Tanous22ce5452022-01-11 10:50:23 -080058 encoded = base64encode("f0\0"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060059 EXPECT_EQ(encoded, "ZjAA");
60
Ed Tanous22ce5452022-01-11 10:50:23 -080061 encoded = base64encode("f0\0 "s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060062 EXPECT_EQ(encoded, "ZjAAIA==");
63
Ed Tanous22ce5452022-01-11 10:50:23 -080064 encoded = base64encode("f0\0 B"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060065 EXPECT_EQ(encoded, "ZjAAIEI=");
66
Ed Tanous22ce5452022-01-11 10:50:23 -080067 encoded = base64encode("f0\0 Ba"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060068 EXPECT_EQ(encoded, "ZjAAIEJh");
69
Ed Tanous22ce5452022-01-11 10:50:23 -080070 encoded = base64encode("f0\0 Bar"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060071 EXPECT_EQ(encoded, "ZjAAIEJhcg==");
72}
73
74TEST(Utility, Base64EncodeDecodeString)
75{
76 using namespace std::string_literals;
77 std::string data("Data fr\0m 90 reading a \nFile"s);
Ed Tanous22ce5452022-01-11 10:50:23 -080078 std::string encoded = base64encode(data);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060079 std::string decoded;
Ed Tanous22ce5452022-01-11 10:50:23 -080080 EXPECT_TRUE(base64Decode(encoded, decoded));
Adriana Kobylakd830ff52021-01-27 14:15:27 -060081 EXPECT_EQ(data, decoded);
82}
Nan Zhou1d8782e2021-11-29 22:23:18 -080083
Ed Tanouseae855c2021-10-26 11:26:02 -070084TEST(Utility, UrlFromPieces)
85{
Ed Tanouseae855c2021-10-26 11:26:02 -070086 boost::urls::url url = urlFromPieces("redfish", "v1", "foo");
Ed Tanous079360a2022-06-29 10:05:19 -070087 EXPECT_EQ(url.buffer(), "/redfish/v1/foo");
Ed Tanouseae855c2021-10-26 11:26:02 -070088
89 url = urlFromPieces("/", "badString");
Ed Tanous079360a2022-06-29 10:05:19 -070090 EXPECT_EQ(url.buffer(), "/%2F/badString");
Ed Tanouseae855c2021-10-26 11:26:02 -070091
92 url = urlFromPieces("bad?tring");
Ed Tanous079360a2022-06-29 10:05:19 -070093 EXPECT_EQ(url.buffer(), "/bad%3Ftring");
Ed Tanouseae855c2021-10-26 11:26:02 -070094
95 url = urlFromPieces("/", "bad&tring");
Ed Tanous079360a2022-06-29 10:05:19 -070096 EXPECT_EQ(url.buffer(), "/%2F/bad&tring");
Willy Tueddfc432022-09-26 16:46:38 +000097
98 EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2F/bad&tring");
99
100 url = urlFromPieces("my-user");
101 EXPECT_EQ(std::string_view(url.data(), url.size()), "/my-user");
102
103 url = urlFromPieces("my_user");
104 EXPECT_EQ(std::string_view(url.data(), url.size()), "/my_user");
105
106 url = urlFromPieces("my_93user");
107 EXPECT_EQ(std::string_view(url.data(), url.size()), "/my_93user");
108
109 // The following characters will be converted to ASCII number
110 // `[{]}\|"<>/?#%^
111 url =
112 urlFromPieces("~1234567890-_=+qwertyuiopasdfghjklzxcvbnm;:',.!@$&*()");
113 EXPECT_EQ(std::string_view(url.data(), url.size()),
114 "/~1234567890-_=+qwertyuiopasdfghjklzxcvbnm;:',.!@$&*()");
Ed Tanouseae855c2021-10-26 11:26:02 -0700115}
Ed Tanous11baefe2022-02-09 12:14:12 -0800116
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100117TEST(Utility, readUrlSegments)
118{
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100119 boost::urls::result<boost::urls::url_view> parsed =
120 boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading");
121
122 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis"));
123
124 EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis"));
125
126 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1"));
127
128 EXPECT_FALSE(
129 readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR"));
130
131 std::string out1;
132 std::string out2;
133 std::string out3;
134 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1)));
135 EXPECT_EQ(out1, "Chassis");
136
137 out1 = out2 = out3 = "";
138 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2),
139 std::ref(out3)));
140 EXPECT_EQ(out1, "redfish");
141 EXPECT_EQ(out2, "v1");
142 EXPECT_EQ(out3, "Chassis");
143
144 out1 = out2 = out3 = "";
145 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis"));
146 EXPECT_EQ(out1, "v1");
147
148 out1 = out2 = out3 = "";
149 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2)));
150 EXPECT_EQ(out1, "redfish");
151 EXPECT_EQ(out2, "Chassis");
152
153 EXPECT_FALSE(readUrlSegments(*parsed, "too", "short"));
154
155 EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long"));
156
157 EXPECT_FALSE(
158 readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2)));
159
160 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1),
161 std::ref(out2), std::ref(out3)));
162
163 parsed = boost::urls::parse_relative_ref("/absolute/url");
164 EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url"));
165
166 parsed = boost::urls::parse_relative_ref("not/absolute/url");
167 EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url"));
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700168
169 parsed = boost::urls::parse_relative_ref("/excellent/path");
170
171 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", "path", OrMorePaths()));
172 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", OrMorePaths()));
173 EXPECT_TRUE(readUrlSegments(*parsed, OrMorePaths()));
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100174}
175
Ed Tanous11baefe2022-02-09 12:14:12 -0800176TEST(Utility, ValidateAndSplitUrlPositive)
177{
Ed Tanous11baefe2022-02-09 12:14:12 -0800178 std::string host;
179 std::string urlProto;
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800180 uint16_t port = 0;
Ed Tanous11baefe2022-02-09 12:14:12 -0800181 std::string path;
182 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar", urlProto, host,
183 port, path));
184 EXPECT_EQ(host, "foo.com");
185 EXPECT_EQ(urlProto, "https");
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800186 EXPECT_EQ(port, 18080);
Ed Tanous11baefe2022-02-09 12:14:12 -0800187
188 EXPECT_EQ(path, "/bar");
189
190 // query string
191 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar?foobar=1",
192 urlProto, host, port, path));
193 EXPECT_EQ(path, "/bar?foobar=1");
194
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800195 // fragment
196 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar#frag", urlProto,
197 host, port, path));
198 EXPECT_EQ(path, "/bar#frag");
199
Ed Tanous11baefe2022-02-09 12:14:12 -0800200 // Missing port
201 ASSERT_TRUE(
202 validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path));
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800203 EXPECT_EQ(port, 443);
Ed Tanous11baefe2022-02-09 12:14:12 -0800204
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800205 // Missing path defaults to "/"
Ed Tanous11baefe2022-02-09 12:14:12 -0800206 ASSERT_TRUE(
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800207 validateAndSplitUrl("https://foo.com/", urlProto, host, port, path));
208 EXPECT_EQ(path, "/");
209
210 // If http push eventing is allowed, allow http and pick a default port of
211 // 80, if it's not, parse should fail.
212 ASSERT_EQ(
213 validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path),
214 bmcwebInsecureEnableHttpPushStyleEventing);
215 if constexpr (bmcwebInsecureEnableHttpPushStyleEventing)
216 {
217 EXPECT_EQ(port, 80);
218 }
Ed Tanous11baefe2022-02-09 12:14:12 -0800219}
220
Ed Tanous4456f082022-03-08 18:01:18 -0800221TEST(Router, ParameterTagging)
222{
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000223 EXPECT_EQ(6 * 6 + 6 * 3 + 2, getParameterTag("<uint><double><int>"));
224 EXPECT_EQ(1, getParameterTag("<int>"));
225 EXPECT_EQ(2, getParameterTag("<uint>"));
226 EXPECT_EQ(3, getParameterTag("<float>"));
227 EXPECT_EQ(3, getParameterTag("<double>"));
228 EXPECT_EQ(4, getParameterTag("<str>"));
229 EXPECT_EQ(4, getParameterTag("<string>"));
230 EXPECT_EQ(5, getParameterTag("<path>"));
231 EXPECT_EQ(6 * 6 + 6 + 1, getParameterTag("<int><int><int>"));
232 EXPECT_EQ(6 * 6 + 6 + 2, getParameterTag("<uint><int><int>"));
233 EXPECT_EQ(6 * 6 + 6 * 3 + 2, getParameterTag("<uint><double><int>"));
Ed Tanous4456f082022-03-08 18:01:18 -0800234}
235
Ed Tanous71f2db72022-05-25 12:28:09 -0700236TEST(URL, JsonEncoding)
237{
Ed Tanous71f2db72022-05-25 12:28:09 -0700238 std::string urlString = "/foo";
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000239 EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
240 EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
Ed Tanous71f2db72022-05-25 12:28:09 -0700241}
242
Willy Tuc6bcedc2022-09-27 05:36:59 +0000243TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters)
244{
245 boost::urls::url url = urlFromPieces("redfish", "v1", "foo");
246 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
247
248 appendUrlPieces(url, "bar");
249 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo/bar");
250
251 appendUrlPieces(url, "/", "bad&tring");
252 EXPECT_EQ(std::string_view(url.data(), url.size()),
Ed Tanous079360a2022-06-29 10:05:19 -0700253 "/redfish/v1/foo/bar/%2F/bad&tring");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000254}
255
Ed Tanous22ce5452022-01-11 10:50:23 -0800256} // namespace
257} // namespace crow::utility