blob: 2f19fca010e68152ec3c2c11a7ffb5224e9fbc98 [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
130TEST(Utility, ValidateAndSplitUrlPositive)
131{
132 using crow::utility::validateAndSplitUrl;
133 std::string host;
134 std::string urlProto;
135 std::string port;
136 std::string path;
137 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar", urlProto, host,
138 port, path));
139 EXPECT_EQ(host, "foo.com");
140 EXPECT_EQ(urlProto, "https");
141 EXPECT_EQ(port, "18080");
142
143 EXPECT_EQ(path, "/bar");
144
145 // query string
146 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar?foobar=1",
147 urlProto, host, port, path));
148 EXPECT_EQ(path, "/bar?foobar=1");
149
150 // Missing port
151 ASSERT_TRUE(
152 validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path));
153 EXPECT_EQ(port, "443");
154
155 // If http push eventing is allowed, allow http, if it's not, parse should
156 // fail.
157#ifdef BMCWEB_INSECURE_ENABLE_HTTP_PUSH_STYLE_EVENTING
158 ASSERT_TRUE(
159 validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path));
160 EXPECT_EQ(port, "80");
161#else
162 ASSERT_FALSE(
163 validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path));
164#endif
165}
166
Ed Tanous4456f082022-03-08 18:01:18 -0800167TEST(Router, ParameterTagging)
168{
169 EXPECT_EQ(6 * 6 + 6 * 3 + 2,
170 crow::black_magic::getParameterTag("<uint><double><int>"));
171 EXPECT_EQ(1, crow::black_magic::getParameterTag("<int>"));
172 EXPECT_EQ(2, crow::black_magic::getParameterTag("<uint>"));
173 EXPECT_EQ(3, crow::black_magic::getParameterTag("<float>"));
174 EXPECT_EQ(3, crow::black_magic::getParameterTag("<double>"));
175 EXPECT_EQ(4, crow::black_magic::getParameterTag("<str>"));
176 EXPECT_EQ(4, crow::black_magic::getParameterTag("<string>"));
177 EXPECT_EQ(5, crow::black_magic::getParameterTag("<path>"));
178 EXPECT_EQ(6 * 6 + 6 + 1,
179 crow::black_magic::getParameterTag("<int><int><int>"));
180 EXPECT_EQ(6 * 6 + 6 + 2,
181 crow::black_magic::getParameterTag("<uint><int><int>"));
182 EXPECT_EQ(6 * 6 + 6 * 3 + 2,
183 crow::black_magic::getParameterTag("<uint><double><int>"));
184}
185
Ed Tanous22ce5452022-01-11 10:50:23 -0800186} // namespace
187} // namespace crow::utility