blob: 23e8ae34a264640e1fd28346e00fb05d69ce7065 [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
17#include <gtest/gtest.h>
Jonathan Doman5beaf842020-08-14 11:23:33 -070018
Ed Tanous22ce5452022-01-11 10:50:23 -080019namespace crow::utility
20{
21namespace
22{
23
Nan Zhoubf8ab7a2022-06-26 23:43:28 +000024using ::crow::black_magic::getParameterTag;
25
Jonathan Doman5beaf842020-08-14 11:23:33 -070026TEST(Utility, Base64DecodeAuthString)
27{
28 std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
29 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080030 EXPECT_TRUE(base64Decode(authString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070031 EXPECT_EQ(result, "usern4me:passw0rd");
32}
33
34TEST(Utility, Base64DecodeNonAscii)
35{
36 std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
37 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080038 EXPECT_FALSE(base64Decode(junkString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070039}
Adriana Kobylakd830ff52021-01-27 14:15:27 -060040
41TEST(Utility, Base64EncodeString)
42{
43 using namespace std::string_literals;
44 std::string encoded;
45
Ed Tanous22ce5452022-01-11 10:50:23 -080046 encoded = base64encode("");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060047 EXPECT_EQ(encoded, "");
48
Ed Tanous22ce5452022-01-11 10:50:23 -080049 encoded = base64encode("f");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060050 EXPECT_EQ(encoded, "Zg==");
51
Ed Tanous22ce5452022-01-11 10:50:23 -080052 encoded = base64encode("f0");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060053 EXPECT_EQ(encoded, "ZjA=");
54
Ed Tanous22ce5452022-01-11 10:50:23 -080055 encoded = base64encode("f0\0"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060056 EXPECT_EQ(encoded, "ZjAA");
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, "ZjAAIA==");
60
Ed Tanous22ce5452022-01-11 10:50:23 -080061 encoded = base64encode("f0\0 B"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060062 EXPECT_EQ(encoded, "ZjAAIEI=");
63
Ed Tanous22ce5452022-01-11 10:50:23 -080064 encoded = base64encode("f0\0 Ba"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060065 EXPECT_EQ(encoded, "ZjAAIEJh");
66
Ed Tanous22ce5452022-01-11 10:50:23 -080067 encoded = base64encode("f0\0 Bar"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060068 EXPECT_EQ(encoded, "ZjAAIEJhcg==");
69}
70
71TEST(Utility, Base64EncodeDecodeString)
72{
73 using namespace std::string_literals;
74 std::string data("Data fr\0m 90 reading a \nFile"s);
Ed Tanous22ce5452022-01-11 10:50:23 -080075 std::string encoded = base64encode(data);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060076 std::string decoded;
Ed Tanous22ce5452022-01-11 10:50:23 -080077 EXPECT_TRUE(base64Decode(encoded, decoded));
Adriana Kobylakd830ff52021-01-27 14:15:27 -060078 EXPECT_EQ(data, decoded);
79}
Nan Zhou1d8782e2021-11-29 22:23:18 -080080
Ed Tanous8d4c4872022-01-11 10:50:23 -080081TEST(Utility, GetDateTimeStdtime)
Nan Zhou1d8782e2021-11-29 22:23:18 -080082{
83 // some time before the epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080084 EXPECT_EQ(getDateTimeStdtime(std::time_t{-1234567}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080085 "1969-12-17T17:03:53+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080086
Nan Zhou1d8782e2021-11-29 22:23:18 -080087 // epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080088 EXPECT_EQ(getDateTimeStdtime(std::time_t{0}), "1970-01-01T00:00:00+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080089
90 // Limits
91 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::max()),
Ed Tanous22ce5452022-01-11 10:50:23 -080092 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080093 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::min()),
94 "1970-01-01T00:00:00+00:00");
95}
96
Ed Tanous22ce5452022-01-11 10:50:23 -080097TEST(Utility, GetDateTimeUint)
Ed Tanous8d4c4872022-01-11 10:50:23 -080098{
Ed Tanous8d4c4872022-01-11 10:50:23 -080099 EXPECT_EQ(getDateTimeUint(uint64_t{1638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -0800100 "2021-11-30T22:41:35+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -0800101 // some time in the future, beyond 2038
Ed Tanous8d4c4872022-01-11 10:50:23 -0800102 EXPECT_EQ(getDateTimeUint(uint64_t{41638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -0800103 "3289-06-18T21:48:15+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -0800104 // the maximum time we support
Ed Tanous8d4c4872022-01-11 10:50:23 -0800105 EXPECT_EQ(getDateTimeUint(uint64_t{253402300799}),
Nan Zhou5ae4b692021-12-14 13:30:37 -0800106 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -0800107
Ed Tanous22ce5452022-01-11 10:50:23 -0800108 // returns the maximum Redfish date
109 EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::max()),
110 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -0800111
112 EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::min()),
113 "1970-01-01T00:00:00+00:00");
114}
115
Ed Tanous22ce5452022-01-11 10:50:23 -0800116TEST(Utility, GetDateTimeUintMs)
Ed Tanous8d4c4872022-01-11 10:50:23 -0800117{
Ed Tanous22ce5452022-01-11 10:50:23 -0800118 // returns the maximum Redfish date
Ed Tanous8d4c4872022-01-11 10:50:23 -0800119 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::max()),
Ed Tanous22ce5452022-01-11 10:50:23 -0800120 "9999-12-31T23:59:59.999000+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -0800121 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()),
122 "1970-01-01T00:00:00+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -0800123}
Ed Tanouseae855c2021-10-26 11:26:02 -0700124
125TEST(Utility, UrlFromPieces)
126{
Ed Tanouseae855c2021-10-26 11:26:02 -0700127 boost::urls::url url = urlFromPieces("redfish", "v1", "foo");
128 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
129
130 url = urlFromPieces("/", "badString");
131 EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/badString");
132
133 url = urlFromPieces("bad?tring");
134 EXPECT_EQ(std::string_view(url.data(), url.size()), "/bad%3ftring");
135
136 url = urlFromPieces("/", "bad&tring");
137 EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/bad&tring");
138}
Ed Tanous11baefe2022-02-09 12:14:12 -0800139
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100140TEST(Utility, readUrlSegments)
141{
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100142 boost::urls::result<boost::urls::url_view> parsed =
143 boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading");
144
145 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis"));
146
147 EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis"));
148
149 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1"));
150
151 EXPECT_FALSE(
152 readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR"));
153
154 std::string out1;
155 std::string out2;
156 std::string out3;
157 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1)));
158 EXPECT_EQ(out1, "Chassis");
159
160 out1 = out2 = out3 = "";
161 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2),
162 std::ref(out3)));
163 EXPECT_EQ(out1, "redfish");
164 EXPECT_EQ(out2, "v1");
165 EXPECT_EQ(out3, "Chassis");
166
167 out1 = out2 = out3 = "";
168 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis"));
169 EXPECT_EQ(out1, "v1");
170
171 out1 = out2 = out3 = "";
172 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2)));
173 EXPECT_EQ(out1, "redfish");
174 EXPECT_EQ(out2, "Chassis");
175
176 EXPECT_FALSE(readUrlSegments(*parsed, "too", "short"));
177
178 EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long"));
179
180 EXPECT_FALSE(
181 readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2)));
182
183 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1),
184 std::ref(out2), std::ref(out3)));
185
186 parsed = boost::urls::parse_relative_ref("/absolute/url");
187 EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url"));
188
189 parsed = boost::urls::parse_relative_ref("not/absolute/url");
190 EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url"));
191}
192
Ed Tanous11baefe2022-02-09 12:14:12 -0800193TEST(Utility, ValidateAndSplitUrlPositive)
194{
Ed Tanous11baefe2022-02-09 12:14:12 -0800195 std::string host;
196 std::string urlProto;
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800197 uint16_t port = 0;
Ed Tanous11baefe2022-02-09 12:14:12 -0800198 std::string path;
199 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar", urlProto, host,
200 port, path));
201 EXPECT_EQ(host, "foo.com");
202 EXPECT_EQ(urlProto, "https");
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800203 EXPECT_EQ(port, 18080);
Ed Tanous11baefe2022-02-09 12:14:12 -0800204
205 EXPECT_EQ(path, "/bar");
206
207 // query string
208 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar?foobar=1",
209 urlProto, host, port, path));
210 EXPECT_EQ(path, "/bar?foobar=1");
211
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800212 // fragment
213 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar#frag", urlProto,
214 host, port, path));
215 EXPECT_EQ(path, "/bar#frag");
216
Ed Tanous11baefe2022-02-09 12:14:12 -0800217 // Missing port
218 ASSERT_TRUE(
219 validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path));
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800220 EXPECT_EQ(port, 443);
Ed Tanous11baefe2022-02-09 12:14:12 -0800221
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800222 // Missing path defaults to "/"
Ed Tanous11baefe2022-02-09 12:14:12 -0800223 ASSERT_TRUE(
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800224 validateAndSplitUrl("https://foo.com/", urlProto, host, port, path));
225 EXPECT_EQ(path, "/");
226
227 // If http push eventing is allowed, allow http and pick a default port of
228 // 80, if it's not, parse should fail.
229 ASSERT_EQ(
230 validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path),
231 bmcwebInsecureEnableHttpPushStyleEventing);
232 if constexpr (bmcwebInsecureEnableHttpPushStyleEventing)
233 {
234 EXPECT_EQ(port, 80);
235 }
Ed Tanous11baefe2022-02-09 12:14:12 -0800236}
237
Ed Tanous4456f082022-03-08 18:01:18 -0800238TEST(Router, ParameterTagging)
239{
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000240 EXPECT_EQ(6 * 6 + 6 * 3 + 2, getParameterTag("<uint><double><int>"));
241 EXPECT_EQ(1, getParameterTag("<int>"));
242 EXPECT_EQ(2, getParameterTag("<uint>"));
243 EXPECT_EQ(3, getParameterTag("<float>"));
244 EXPECT_EQ(3, getParameterTag("<double>"));
245 EXPECT_EQ(4, getParameterTag("<str>"));
246 EXPECT_EQ(4, getParameterTag("<string>"));
247 EXPECT_EQ(5, getParameterTag("<path>"));
248 EXPECT_EQ(6 * 6 + 6 + 1, getParameterTag("<int><int><int>"));
249 EXPECT_EQ(6 * 6 + 6 + 2, getParameterTag("<uint><int><int>"));
250 EXPECT_EQ(6 * 6 + 6 * 3 + 2, getParameterTag("<uint><double><int>"));
Ed Tanous4456f082022-03-08 18:01:18 -0800251}
252
Ed Tanous71f2db72022-05-25 12:28:09 -0700253TEST(URL, JsonEncoding)
254{
Ed Tanous71f2db72022-05-25 12:28:09 -0700255 std::string urlString = "/foo";
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000256 EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
257 EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
Ed Tanous71f2db72022-05-25 12:28:09 -0700258}
259
Ed Tanous22ce5452022-01-11 10:50:23 -0800260} // namespace
261} // namespace crow::utility