blob: c774996283849c8936b757296e73a4f068614cad [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 Tanous8d4c4872022-01-11 10:50:23 -080084TEST(Utility, GetDateTimeStdtime)
Nan Zhou1d8782e2021-11-29 22:23:18 -080085{
86 // some time before the epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080087 EXPECT_EQ(getDateTimeStdtime(std::time_t{-1234567}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080088 "1969-12-17T17:03:53+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080089
Nan Zhou1d8782e2021-11-29 22:23:18 -080090 // epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080091 EXPECT_EQ(getDateTimeStdtime(std::time_t{0}), "1970-01-01T00:00:00+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080092
93 // Limits
94 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::max()),
Ed Tanous22ce5452022-01-11 10:50:23 -080095 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080096 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::min()),
97 "1970-01-01T00:00:00+00:00");
98}
99
Ed Tanous22ce5452022-01-11 10:50:23 -0800100TEST(Utility, GetDateTimeUint)
Ed Tanous8d4c4872022-01-11 10:50:23 -0800101{
Ed Tanous8d4c4872022-01-11 10:50:23 -0800102 EXPECT_EQ(getDateTimeUint(uint64_t{1638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -0800103 "2021-11-30T22:41:35+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -0800104 // some time in the future, beyond 2038
Ed Tanous8d4c4872022-01-11 10:50:23 -0800105 EXPECT_EQ(getDateTimeUint(uint64_t{41638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -0800106 "3289-06-18T21:48:15+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -0800107 // the maximum time we support
Ed Tanous8d4c4872022-01-11 10:50:23 -0800108 EXPECT_EQ(getDateTimeUint(uint64_t{253402300799}),
Nan Zhou5ae4b692021-12-14 13:30:37 -0800109 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -0800110
Ed Tanous22ce5452022-01-11 10:50:23 -0800111 // returns the maximum Redfish date
112 EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::max()),
113 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -0800114
115 EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::min()),
116 "1970-01-01T00:00:00+00:00");
117}
118
Ed Tanous22ce5452022-01-11 10:50:23 -0800119TEST(Utility, GetDateTimeUintMs)
Ed Tanous8d4c4872022-01-11 10:50:23 -0800120{
Ed Tanous22ce5452022-01-11 10:50:23 -0800121 // returns the maximum Redfish date
Ed Tanous8d4c4872022-01-11 10:50:23 -0800122 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::max()),
Ed Tanous22ce5452022-01-11 10:50:23 -0800123 "9999-12-31T23:59:59.999000+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -0800124 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()),
125 "1970-01-01T00:00:00+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -0800126}
Ed Tanouseae855c2021-10-26 11:26:02 -0700127
128TEST(Utility, UrlFromPieces)
129{
Ed Tanouseae855c2021-10-26 11:26:02 -0700130 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 Tanous11baefe2022-02-09 12:14:12 -0800142
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100143TEST(Utility, readUrlSegments)
144{
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100145 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 Tanous11baefe2022-02-09 12:14:12 -0800196TEST(Utility, ValidateAndSplitUrlPositive)
197{
Ed Tanous11baefe2022-02-09 12:14:12 -0800198 std::string host;
199 std::string urlProto;
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800200 uint16_t port = 0;
Ed Tanous11baefe2022-02-09 12:14:12 -0800201 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 Tanouseb1c47d2022-02-09 11:47:27 -0800206 EXPECT_EQ(port, 18080);
Ed Tanous11baefe2022-02-09 12:14:12 -0800207
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 Tanouseb1c47d2022-02-09 11:47:27 -0800215 // fragment
216 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar#frag", urlProto,
217 host, port, path));
218 EXPECT_EQ(path, "/bar#frag");
219
Ed Tanous11baefe2022-02-09 12:14:12 -0800220 // Missing port
221 ASSERT_TRUE(
222 validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path));
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800223 EXPECT_EQ(port, 443);
Ed Tanous11baefe2022-02-09 12:14:12 -0800224
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800225 // Missing path defaults to "/"
Ed Tanous11baefe2022-02-09 12:14:12 -0800226 ASSERT_TRUE(
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800227 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 Tanous11baefe2022-02-09 12:14:12 -0800239}
240
Ed Tanous4456f082022-03-08 18:01:18 -0800241TEST(Router, ParameterTagging)
242{
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000243 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 Tanous4456f082022-03-08 18:01:18 -0800254}
255
Ed Tanous71f2db72022-05-25 12:28:09 -0700256TEST(URL, JsonEncoding)
257{
Ed Tanous71f2db72022-05-25 12:28:09 -0700258 std::string urlString = "/foo";
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000259 EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
260 EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
Ed Tanous71f2db72022-05-25 12:28:09 -0700261}
262
Ed Tanous22ce5452022-01-11 10:50:23 -0800263} // namespace
264} // namespace crow::utility