blob: bab21f96c047aee39fa934b877de9ec8922d6aa7 [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
5#include "gmock/gmock.h"
Ed Tanous11baefe2022-02-09 12:14:12 -08006#include "gtest/gtest.h"
Jonathan Doman5beaf842020-08-14 11:23:33 -07007
Ed Tanous22ce5452022-01-11 10:50:23 -08008namespace crow::utility
9{
10namespace
11{
12
Jonathan Doman5beaf842020-08-14 11:23:33 -070013TEST(Utility, Base64DecodeAuthString)
14{
15 std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
16 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080017 EXPECT_TRUE(base64Decode(authString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070018 EXPECT_EQ(result, "usern4me:passw0rd");
19}
20
21TEST(Utility, Base64DecodeNonAscii)
22{
23 std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
24 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080025 EXPECT_FALSE(base64Decode(junkString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070026}
Adriana Kobylakd830ff52021-01-27 14:15:27 -060027
28TEST(Utility, Base64EncodeString)
29{
30 using namespace std::string_literals;
31 std::string encoded;
32
Ed Tanous22ce5452022-01-11 10:50:23 -080033 encoded = base64encode("");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060034 EXPECT_EQ(encoded, "");
35
Ed Tanous22ce5452022-01-11 10:50:23 -080036 encoded = base64encode("f");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060037 EXPECT_EQ(encoded, "Zg==");
38
Ed Tanous22ce5452022-01-11 10:50:23 -080039 encoded = base64encode("f0");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060040 EXPECT_EQ(encoded, "ZjA=");
41
Ed Tanous22ce5452022-01-11 10:50:23 -080042 encoded = base64encode("f0\0"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060043 EXPECT_EQ(encoded, "ZjAA");
44
Ed Tanous22ce5452022-01-11 10:50:23 -080045 encoded = base64encode("f0\0 "s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060046 EXPECT_EQ(encoded, "ZjAAIA==");
47
Ed Tanous22ce5452022-01-11 10:50:23 -080048 encoded = base64encode("f0\0 B"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060049 EXPECT_EQ(encoded, "ZjAAIEI=");
50
Ed Tanous22ce5452022-01-11 10:50:23 -080051 encoded = base64encode("f0\0 Ba"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060052 EXPECT_EQ(encoded, "ZjAAIEJh");
53
Ed Tanous22ce5452022-01-11 10:50:23 -080054 encoded = base64encode("f0\0 Bar"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060055 EXPECT_EQ(encoded, "ZjAAIEJhcg==");
56}
57
58TEST(Utility, Base64EncodeDecodeString)
59{
60 using namespace std::string_literals;
61 std::string data("Data fr\0m 90 reading a \nFile"s);
Ed Tanous22ce5452022-01-11 10:50:23 -080062 std::string encoded = base64encode(data);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060063 std::string decoded;
Ed Tanous22ce5452022-01-11 10:50:23 -080064 EXPECT_TRUE(base64Decode(encoded, decoded));
Adriana Kobylakd830ff52021-01-27 14:15:27 -060065 EXPECT_EQ(data, decoded);
66}
Nan Zhou1d8782e2021-11-29 22:23:18 -080067
Ed Tanous8d4c4872022-01-11 10:50:23 -080068TEST(Utility, GetDateTimeStdtime)
Nan Zhou1d8782e2021-11-29 22:23:18 -080069{
Ed Tanous8d4c4872022-01-11 10:50:23 -080070 using crow::utility::getDateTimeStdtime;
71
Nan Zhou1d8782e2021-11-29 22:23:18 -080072 // some time before the epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080073 EXPECT_EQ(getDateTimeStdtime(std::time_t{-1234567}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080074 "1969-12-17T17:03:53+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080075
Nan Zhou1d8782e2021-11-29 22:23:18 -080076 // epoch
Ed Tanous8d4c4872022-01-11 10:50:23 -080077 EXPECT_EQ(getDateTimeStdtime(std::time_t{0}), "1970-01-01T00:00:00+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080078
79 // Limits
80 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::max()),
Ed Tanous22ce5452022-01-11 10:50:23 -080081 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080082 EXPECT_EQ(getDateTimeStdtime(std::numeric_limits<std::time_t>::min()),
83 "1970-01-01T00:00:00+00:00");
84}
85
Ed Tanous22ce5452022-01-11 10:50:23 -080086TEST(Utility, GetDateTimeUint)
Ed Tanous8d4c4872022-01-11 10:50:23 -080087{
Ed Tanous8d4c4872022-01-11 10:50:23 -080088 EXPECT_EQ(getDateTimeUint(uint64_t{1638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080089 "2021-11-30T22:41:35+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -080090 // some time in the future, beyond 2038
Ed Tanous8d4c4872022-01-11 10:50:23 -080091 EXPECT_EQ(getDateTimeUint(uint64_t{41638312095}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080092 "3289-06-18T21:48:15+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -080093 // the maximum time we support
Ed Tanous8d4c4872022-01-11 10:50:23 -080094 EXPECT_EQ(getDateTimeUint(uint64_t{253402300799}),
Nan Zhou5ae4b692021-12-14 13:30:37 -080095 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -080096
Ed Tanous22ce5452022-01-11 10:50:23 -080097 // returns the maximum Redfish date
98 EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::max()),
99 "9999-12-31T23:59:59+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -0800100
101 EXPECT_EQ(getDateTimeUint(std::numeric_limits<uint64_t>::min()),
102 "1970-01-01T00:00:00+00:00");
103}
104
Ed Tanous22ce5452022-01-11 10:50:23 -0800105TEST(Utility, GetDateTimeUintMs)
Ed Tanous8d4c4872022-01-11 10:50:23 -0800106{
Ed Tanous22ce5452022-01-11 10:50:23 -0800107 // returns the maximum Redfish date
Ed Tanous8d4c4872022-01-11 10:50:23 -0800108 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::max()),
Ed Tanous22ce5452022-01-11 10:50:23 -0800109 "9999-12-31T23:59:59.999000+00:00");
Ed Tanous8d4c4872022-01-11 10:50:23 -0800110 EXPECT_EQ(getDateTimeUintMs(std::numeric_limits<uint64_t>::min()),
111 "1970-01-01T00:00:00+00:00");
Nan Zhou1d8782e2021-11-29 22:23:18 -0800112}
Ed Tanouseae855c2021-10-26 11:26:02 -0700113
114TEST(Utility, UrlFromPieces)
115{
116 using crow::utility::urlFromPieces;
117 boost::urls::url url = urlFromPieces("redfish", "v1", "foo");
118 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
119
120 url = urlFromPieces("/", "badString");
121 EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/badString");
122
123 url = urlFromPieces("bad?tring");
124 EXPECT_EQ(std::string_view(url.data(), url.size()), "/bad%3ftring");
125
126 url = urlFromPieces("/", "bad&tring");
127 EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/bad&tring");
128}
Ed Tanous11baefe2022-02-09 12:14:12 -0800129
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100130TEST(Utility, readUrlSegments)
131{
132 using crow::utility::readUrlSegments;
133
134 boost::urls::result<boost::urls::url_view> parsed =
135 boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading");
136
137 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis"));
138
139 EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis"));
140
141 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1"));
142
143 EXPECT_FALSE(
144 readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR"));
145
146 std::string out1;
147 std::string out2;
148 std::string out3;
149 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1)));
150 EXPECT_EQ(out1, "Chassis");
151
152 out1 = out2 = out3 = "";
153 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2),
154 std::ref(out3)));
155 EXPECT_EQ(out1, "redfish");
156 EXPECT_EQ(out2, "v1");
157 EXPECT_EQ(out3, "Chassis");
158
159 out1 = out2 = out3 = "";
160 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis"));
161 EXPECT_EQ(out1, "v1");
162
163 out1 = out2 = out3 = "";
164 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2)));
165 EXPECT_EQ(out1, "redfish");
166 EXPECT_EQ(out2, "Chassis");
167
168 EXPECT_FALSE(readUrlSegments(*parsed, "too", "short"));
169
170 EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long"));
171
172 EXPECT_FALSE(
173 readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2)));
174
175 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1),
176 std::ref(out2), std::ref(out3)));
177
178 parsed = boost::urls::parse_relative_ref("/absolute/url");
179 EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url"));
180
181 parsed = boost::urls::parse_relative_ref("not/absolute/url");
182 EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url"));
183}
184
Ed Tanous11baefe2022-02-09 12:14:12 -0800185TEST(Utility, ValidateAndSplitUrlPositive)
186{
187 using crow::utility::validateAndSplitUrl;
188 std::string host;
189 std::string urlProto;
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800190 uint16_t port = 0;
Ed Tanous11baefe2022-02-09 12:14:12 -0800191 std::string path;
192 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar", urlProto, host,
193 port, path));
194 EXPECT_EQ(host, "foo.com");
195 EXPECT_EQ(urlProto, "https");
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800196 EXPECT_EQ(port, 18080);
Ed Tanous11baefe2022-02-09 12:14:12 -0800197
198 EXPECT_EQ(path, "/bar");
199
200 // query string
201 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar?foobar=1",
202 urlProto, host, port, path));
203 EXPECT_EQ(path, "/bar?foobar=1");
204
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800205 // fragment
206 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar#frag", urlProto,
207 host, port, path));
208 EXPECT_EQ(path, "/bar#frag");
209
Ed Tanous11baefe2022-02-09 12:14:12 -0800210 // Missing port
211 ASSERT_TRUE(
212 validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path));
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800213 EXPECT_EQ(port, 443);
Ed Tanous11baefe2022-02-09 12:14:12 -0800214
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800215 // Missing path defaults to "/"
Ed Tanous11baefe2022-02-09 12:14:12 -0800216 ASSERT_TRUE(
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800217 validateAndSplitUrl("https://foo.com/", urlProto, host, port, path));
218 EXPECT_EQ(path, "/");
219
220 // If http push eventing is allowed, allow http and pick a default port of
221 // 80, if it's not, parse should fail.
222 ASSERT_EQ(
223 validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path),
224 bmcwebInsecureEnableHttpPushStyleEventing);
225 if constexpr (bmcwebInsecureEnableHttpPushStyleEventing)
226 {
227 EXPECT_EQ(port, 80);
228 }
Ed Tanous11baefe2022-02-09 12:14:12 -0800229}
230
Ed Tanous4456f082022-03-08 18:01:18 -0800231TEST(Router, ParameterTagging)
232{
233 EXPECT_EQ(6 * 6 + 6 * 3 + 2,
234 crow::black_magic::getParameterTag("<uint><double><int>"));
235 EXPECT_EQ(1, crow::black_magic::getParameterTag("<int>"));
236 EXPECT_EQ(2, crow::black_magic::getParameterTag("<uint>"));
237 EXPECT_EQ(3, crow::black_magic::getParameterTag("<float>"));
238 EXPECT_EQ(3, crow::black_magic::getParameterTag("<double>"));
239 EXPECT_EQ(4, crow::black_magic::getParameterTag("<str>"));
240 EXPECT_EQ(4, crow::black_magic::getParameterTag("<string>"));
241 EXPECT_EQ(5, crow::black_magic::getParameterTag("<path>"));
242 EXPECT_EQ(6 * 6 + 6 + 1,
243 crow::black_magic::getParameterTag("<int><int><int>"));
244 EXPECT_EQ(6 * 6 + 6 + 2,
245 crow::black_magic::getParameterTag("<uint><int><int>"));
246 EXPECT_EQ(6 * 6 + 6 * 3 + 2,
247 crow::black_magic::getParameterTag("<uint><double><int>"));
248}
249
Ed Tanous22ce5452022-01-11 10:50:23 -0800250} // namespace
251} // namespace crow::utility