blob: 8eef93f4a3e1da38fa12a8847d441fa8b1f4caf8 [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");
87 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
88
89 url = urlFromPieces("/", "badString");
90 EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/badString");
91
92 url = urlFromPieces("bad?tring");
93 EXPECT_EQ(std::string_view(url.data(), url.size()), "/bad%3ftring");
94
95 url = urlFromPieces("/", "bad&tring");
96 EXPECT_EQ(std::string_view(url.data(), url.size()), "/%2f/bad&tring");
97}
Ed Tanous11baefe2022-02-09 12:14:12 -080098
Szymon Dompkeca1600c2022-03-03 14:42:52 +010099TEST(Utility, readUrlSegments)
100{
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100101 boost::urls::result<boost::urls::url_view> parsed =
102 boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading");
103
104 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis"));
105
106 EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis"));
107
108 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1"));
109
110 EXPECT_FALSE(
111 readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR"));
112
113 std::string out1;
114 std::string out2;
115 std::string out3;
116 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1)));
117 EXPECT_EQ(out1, "Chassis");
118
119 out1 = out2 = out3 = "";
120 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2),
121 std::ref(out3)));
122 EXPECT_EQ(out1, "redfish");
123 EXPECT_EQ(out2, "v1");
124 EXPECT_EQ(out3, "Chassis");
125
126 out1 = out2 = out3 = "";
127 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis"));
128 EXPECT_EQ(out1, "v1");
129
130 out1 = out2 = out3 = "";
131 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2)));
132 EXPECT_EQ(out1, "redfish");
133 EXPECT_EQ(out2, "Chassis");
134
135 EXPECT_FALSE(readUrlSegments(*parsed, "too", "short"));
136
137 EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long"));
138
139 EXPECT_FALSE(
140 readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2)));
141
142 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1),
143 std::ref(out2), std::ref(out3)));
144
145 parsed = boost::urls::parse_relative_ref("/absolute/url");
146 EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url"));
147
148 parsed = boost::urls::parse_relative_ref("not/absolute/url");
149 EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url"));
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700150
151 parsed = boost::urls::parse_relative_ref("/excellent/path");
152
153 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", "path", OrMorePaths()));
154 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", OrMorePaths()));
155 EXPECT_TRUE(readUrlSegments(*parsed, OrMorePaths()));
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100156}
157
Ed Tanous11baefe2022-02-09 12:14:12 -0800158TEST(Utility, ValidateAndSplitUrlPositive)
159{
Ed Tanous11baefe2022-02-09 12:14:12 -0800160 std::string host;
161 std::string urlProto;
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800162 uint16_t port = 0;
Ed Tanous11baefe2022-02-09 12:14:12 -0800163 std::string path;
164 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar", urlProto, host,
165 port, path));
166 EXPECT_EQ(host, "foo.com");
167 EXPECT_EQ(urlProto, "https");
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800168 EXPECT_EQ(port, 18080);
Ed Tanous11baefe2022-02-09 12:14:12 -0800169
170 EXPECT_EQ(path, "/bar");
171
172 // query string
173 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar?foobar=1",
174 urlProto, host, port, path));
175 EXPECT_EQ(path, "/bar?foobar=1");
176
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800177 // fragment
178 ASSERT_TRUE(validateAndSplitUrl("https://foo.com:18080/bar#frag", urlProto,
179 host, port, path));
180 EXPECT_EQ(path, "/bar#frag");
181
Ed Tanous11baefe2022-02-09 12:14:12 -0800182 // Missing port
183 ASSERT_TRUE(
184 validateAndSplitUrl("https://foo.com/bar", urlProto, host, port, path));
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800185 EXPECT_EQ(port, 443);
Ed Tanous11baefe2022-02-09 12:14:12 -0800186
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800187 // Missing path defaults to "/"
Ed Tanous11baefe2022-02-09 12:14:12 -0800188 ASSERT_TRUE(
Ed Tanouseb1c47d2022-02-09 11:47:27 -0800189 validateAndSplitUrl("https://foo.com/", urlProto, host, port, path));
190 EXPECT_EQ(path, "/");
191
192 // If http push eventing is allowed, allow http and pick a default port of
193 // 80, if it's not, parse should fail.
194 ASSERT_EQ(
195 validateAndSplitUrl("http://foo.com/bar", urlProto, host, port, path),
196 bmcwebInsecureEnableHttpPushStyleEventing);
197 if constexpr (bmcwebInsecureEnableHttpPushStyleEventing)
198 {
199 EXPECT_EQ(port, 80);
200 }
Ed Tanous11baefe2022-02-09 12:14:12 -0800201}
202
Ed Tanous4456f082022-03-08 18:01:18 -0800203TEST(Router, ParameterTagging)
204{
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000205 EXPECT_EQ(6 * 6 + 6 * 3 + 2, getParameterTag("<uint><double><int>"));
206 EXPECT_EQ(1, getParameterTag("<int>"));
207 EXPECT_EQ(2, getParameterTag("<uint>"));
208 EXPECT_EQ(3, getParameterTag("<float>"));
209 EXPECT_EQ(3, getParameterTag("<double>"));
210 EXPECT_EQ(4, getParameterTag("<str>"));
211 EXPECT_EQ(4, getParameterTag("<string>"));
212 EXPECT_EQ(5, getParameterTag("<path>"));
213 EXPECT_EQ(6 * 6 + 6 + 1, getParameterTag("<int><int><int>"));
214 EXPECT_EQ(6 * 6 + 6 + 2, getParameterTag("<uint><int><int>"));
215 EXPECT_EQ(6 * 6 + 6 * 3 + 2, getParameterTag("<uint><double><int>"));
Ed Tanous4456f082022-03-08 18:01:18 -0800216}
217
Ed Tanous71f2db72022-05-25 12:28:09 -0700218TEST(URL, JsonEncoding)
219{
Ed Tanous71f2db72022-05-25 12:28:09 -0700220 std::string urlString = "/foo";
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000221 EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
222 EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
Ed Tanous71f2db72022-05-25 12:28:09 -0700223}
224
Ed Tanous22ce5452022-01-11 10:50:23 -0800225} // namespace
226} // namespace crow::utility