blob: 5f62d3f3d11605886ef93c9013e6c821b206a507 [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
Ed Tanous47488a92023-06-26 18:19:33 -070027using ::crow::utility::getParameterTag;
Nan Zhoubf8ab7a2022-06-26 23:43:28 +000028
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
Szymon Dompkeca1600c2022-03-03 14:42:52 +010084TEST(Utility, readUrlSegments)
85{
Ed Tanous6fd29552023-10-04 09:40:14 -070086 boost::system::result<boost::urls::url_view> parsed =
Szymon Dompkeca1600c2022-03-03 14:42:52 +010087 boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading");
88
89 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis"));
90
91 EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis"));
92
93 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1"));
94
95 EXPECT_FALSE(
96 readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR"));
97
98 std::string out1;
99 std::string out2;
100 std::string out3;
101 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1)));
102 EXPECT_EQ(out1, "Chassis");
103
104 out1 = out2 = out3 = "";
105 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2),
106 std::ref(out3)));
107 EXPECT_EQ(out1, "redfish");
108 EXPECT_EQ(out2, "v1");
109 EXPECT_EQ(out3, "Chassis");
110
111 out1 = out2 = out3 = "";
112 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis"));
113 EXPECT_EQ(out1, "v1");
114
115 out1 = out2 = out3 = "";
116 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2)));
117 EXPECT_EQ(out1, "redfish");
118 EXPECT_EQ(out2, "Chassis");
119
120 EXPECT_FALSE(readUrlSegments(*parsed, "too", "short"));
121
122 EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long"));
123
124 EXPECT_FALSE(
125 readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2)));
126
127 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1),
128 std::ref(out2), std::ref(out3)));
129
130 parsed = boost::urls::parse_relative_ref("/absolute/url");
131 EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url"));
132
133 parsed = boost::urls::parse_relative_ref("not/absolute/url");
134 EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url"));
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700135
136 parsed = boost::urls::parse_relative_ref("/excellent/path");
137
138 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", "path", OrMorePaths()));
139 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", OrMorePaths()));
140 EXPECT_TRUE(readUrlSegments(*parsed, OrMorePaths()));
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100141}
142
Ed Tanous4456f082022-03-08 18:01:18 -0800143TEST(Router, ParameterTagging)
144{
Ed Tanous15a42df2023-02-09 18:08:23 -0800145 EXPECT_EQ(1, getParameterTag("<str>"));
146 EXPECT_EQ(1, getParameterTag("<string>"));
147 EXPECT_EQ(2, getParameterTag("<path>"));
Ed Tanous4456f082022-03-08 18:01:18 -0800148}
149
Ed Tanous71f2db72022-05-25 12:28:09 -0700150TEST(URL, JsonEncoding)
151{
Ed Tanous71f2db72022-05-25 12:28:09 -0700152 std::string urlString = "/foo";
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000153 EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
154 EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
Ed Tanous71f2db72022-05-25 12:28:09 -0700155}
156
Willy Tuc6bcedc2022-09-27 05:36:59 +0000157TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters)
158{
Ed Tanousef4c65b2023-04-24 15:28:50 -0700159 boost::urls::url url("/redfish/v1/foo");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000160 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
161
162 appendUrlPieces(url, "bar");
163 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo/bar");
164
Ed Tanous8ece0e42024-01-02 13:16:50 -0800165 appendUrlPieces(url, "/", "bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000166 EXPECT_EQ(std::string_view(url.data(), url.size()),
Ed Tanous8ece0e42024-01-02 13:16:50 -0800167 "/redfish/v1/foo/bar/%2F/bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000168}
169
Ed Tanous22ce5452022-01-11 10:50:23 -0800170} // namespace
171} // namespace crow::utility