blob: 1a99d6d7c520058196bebe01d1e203d2c32387b0 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous11baefe2022-02-09 12:14:12 -08003
Ed Tanous04e438c2020-10-03 08:06:26 -07004#include "utility.hpp"
Jonathan Doman5beaf842020-08-14 11:23:33 -07005
Ed Tanousf0b59af2024-03-20 13:38:04 -07006#include <boost/system/result.hpp>
7#include <boost/url/parse.hpp>
Nan Zhou26500f22022-06-26 23:35:30 +00008#include <boost/url/url.hpp>
9#include <boost/url/url_view.hpp>
Patrick Williams5900d4c2024-10-10 18:03:28 -040010#include <nlohmann/json.hpp>
Nan Zhou26500f22022-06-26 23:35:30 +000011
Ed Tanousf0b59af2024-03-20 13:38:04 -070012#include <algorithm>
Nan Zhou26500f22022-06-26 23:35:30 +000013#include <ctime>
14#include <functional>
Nan Zhou26500f22022-06-26 23:35:30 +000015#include <string>
16#include <string_view>
17
Ed Tanous478b7ad2024-07-15 19:11:54 -070018#include <gtest/gtest.h>
Jonathan Doman5beaf842020-08-14 11:23:33 -070019
Ed Tanous22ce5452022-01-11 10:50:23 -080020namespace crow::utility
21{
22namespace
23{
24
Jonathan Doman5beaf842020-08-14 11:23:33 -070025TEST(Utility, Base64DecodeAuthString)
26{
27 std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
28 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080029 EXPECT_TRUE(base64Decode(authString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070030 EXPECT_EQ(result, "usern4me:passw0rd");
31}
32
Ed Tanous80d2ef32025-02-04 09:29:02 -080033TEST(Utility, Base64DecodeUrlsafe)
34{
35 std::string result;
36 EXPECT_TRUE(base64Decode<true>("-_abcde", result));
37 EXPECT_EQ(result, "\xfb\xf6\x9b\x71\xd7");
38}
39
Jonathan Doman5beaf842020-08-14 11:23:33 -070040TEST(Utility, Base64DecodeNonAscii)
41{
42 std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
43 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080044 EXPECT_FALSE(base64Decode(junkString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070045}
Adriana Kobylakd830ff52021-01-27 14:15:27 -060046
47TEST(Utility, Base64EncodeString)
48{
49 using namespace std::string_literals;
50 std::string encoded;
51
Ed Tanous22ce5452022-01-11 10:50:23 -080052 encoded = base64encode("");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060053 EXPECT_EQ(encoded, "");
54
Ed Tanous22ce5452022-01-11 10:50:23 -080055 encoded = base64encode("f");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060056 EXPECT_EQ(encoded, "Zg==");
57
Ed Tanous22ce5452022-01-11 10:50:23 -080058 encoded = base64encode("f0");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060059 EXPECT_EQ(encoded, "ZjA=");
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, "ZjAA");
63
Ed Tanous22ce5452022-01-11 10:50:23 -080064 encoded = base64encode("f0\0 "s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060065 EXPECT_EQ(encoded, "ZjAAIA==");
66
Ed Tanous22ce5452022-01-11 10:50:23 -080067 encoded = base64encode("f0\0 B"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060068 EXPECT_EQ(encoded, "ZjAAIEI=");
69
Ed Tanous22ce5452022-01-11 10:50:23 -080070 encoded = base64encode("f0\0 Ba"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060071 EXPECT_EQ(encoded, "ZjAAIEJh");
72
Ed Tanous22ce5452022-01-11 10:50:23 -080073 encoded = base64encode("f0\0 Bar"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060074 EXPECT_EQ(encoded, "ZjAAIEJhcg==");
75}
76
Ed Tanousee192c02023-12-13 10:49:58 -080077TEST(Utility, Base64Encoder)
78{
79 using namespace std::string_literals;
80 std::string data = "f0\0 Bar"s;
81 for (size_t chunkSize = 1; chunkSize < 6; chunkSize++)
82 {
83 std::string_view testString(data);
84 std::string out;
85 Base64Encoder encoder;
86 while (!testString.empty())
87 {
88 size_t thisChunk = std::min(testString.size(), chunkSize);
89 encoder.encode(testString.substr(0, thisChunk), out);
90 testString.remove_prefix(thisChunk);
91 }
92
93 encoder.finalize(out);
94 EXPECT_EQ(out, "ZjAAIEJhcg==");
95 }
96}
97
Adriana Kobylakd830ff52021-01-27 14:15:27 -060098TEST(Utility, Base64EncodeDecodeString)
99{
100 using namespace std::string_literals;
101 std::string data("Data fr\0m 90 reading a \nFile"s);
Ed Tanous22ce5452022-01-11 10:50:23 -0800102 std::string encoded = base64encode(data);
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600103 std::string decoded;
Ed Tanous22ce5452022-01-11 10:50:23 -0800104 EXPECT_TRUE(base64Decode(encoded, decoded));
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600105 EXPECT_EQ(data, decoded);
106}
Nan Zhou1d8782e2021-11-29 22:23:18 -0800107
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100108TEST(Utility, readUrlSegments)
109{
Ed Tanous6fd29552023-10-04 09:40:14 -0700110 boost::system::result<boost::urls::url_view> parsed =
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100111 boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading");
112
113 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis"));
114
115 EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis"));
116
117 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1"));
118
119 EXPECT_FALSE(
120 readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR"));
121
122 std::string out1;
123 std::string out2;
124 std::string out3;
125 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1)));
126 EXPECT_EQ(out1, "Chassis");
127
128 out1 = out2 = out3 = "";
129 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2),
130 std::ref(out3)));
131 EXPECT_EQ(out1, "redfish");
132 EXPECT_EQ(out2, "v1");
133 EXPECT_EQ(out3, "Chassis");
134
135 out1 = out2 = out3 = "";
136 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis"));
137 EXPECT_EQ(out1, "v1");
138
139 out1 = out2 = out3 = "";
140 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2)));
141 EXPECT_EQ(out1, "redfish");
142 EXPECT_EQ(out2, "Chassis");
143
144 EXPECT_FALSE(readUrlSegments(*parsed, "too", "short"));
145
146 EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long"));
147
148 EXPECT_FALSE(
149 readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2)));
150
151 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1),
152 std::ref(out2), std::ref(out3)));
153
154 parsed = boost::urls::parse_relative_ref("/absolute/url");
155 EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url"));
156
157 parsed = boost::urls::parse_relative_ref("not/absolute/url");
158 EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url"));
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700159
160 parsed = boost::urls::parse_relative_ref("/excellent/path");
161
162 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", "path", OrMorePaths()));
163 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", OrMorePaths()));
164 EXPECT_TRUE(readUrlSegments(*parsed, OrMorePaths()));
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100165}
166
Ed Tanous4456f082022-03-08 18:01:18 -0800167TEST(Router, ParameterTagging)
168{
Ed Tanous15a42df2023-02-09 18:08:23 -0800169 EXPECT_EQ(1, getParameterTag("<str>"));
170 EXPECT_EQ(1, getParameterTag("<string>"));
Ed Tanousd9e89df2024-03-27 14:08:59 -0700171 EXPECT_EQ(1, getParameterTag("<path>"));
172 EXPECT_EQ(2, getParameterTag("<str>/<str>"));
173 EXPECT_EQ(2, getParameterTag("<string>/<string>"));
174 EXPECT_EQ(2, getParameterTag("<path>/<path>"));
Ed Tanous4456f082022-03-08 18:01:18 -0800175}
176
Ed Tanous71f2db72022-05-25 12:28:09 -0700177TEST(URL, JsonEncoding)
178{
Ed Tanous71f2db72022-05-25 12:28:09 -0700179 std::string urlString = "/foo";
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000180 EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
181 EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
Ed Tanous71f2db72022-05-25 12:28:09 -0700182}
183
Willy Tuc6bcedc2022-09-27 05:36:59 +0000184TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters)
185{
Ed Tanousef4c65b2023-04-24 15:28:50 -0700186 boost::urls::url url("/redfish/v1/foo");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000187 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
188
189 appendUrlPieces(url, "bar");
190 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo/bar");
191
Ed Tanous8ece0e42024-01-02 13:16:50 -0800192 appendUrlPieces(url, "/", "bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000193 EXPECT_EQ(std::string_view(url.data(), url.size()),
Ed Tanous8ece0e42024-01-02 13:16:50 -0800194 "/redfish/v1/foo/bar/%2F/bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000195}
196
Ed Tanous22ce5452022-01-11 10:50:23 -0800197} // namespace
198} // namespace crow::utility