blob: 36725efd602e3cc5133125c5f2e1753f741a687f [file] [log] [blame]
Ed Tanous11baefe2022-02-09 12:14:12 -08001
Ed Tanous04e438c2020-10-03 08:06:26 -07002#include "utility.hpp"
Jonathan Doman5beaf842020-08-14 11:23:33 -07003
Ed Tanousf0b59af2024-03-20 13:38:04 -07004#include <boost/system/result.hpp>
5#include <boost/url/parse.hpp>
Nan Zhou26500f22022-06-26 23:35:30 +00006#include <boost/url/url.hpp>
7#include <boost/url/url_view.hpp>
Nan Zhou26500f22022-06-26 23:35:30 +00008
Ed Tanousf0b59af2024-03-20 13:38:04 -07009#include <algorithm>
Nan Zhou26500f22022-06-26 23:35:30 +000010#include <ctime>
11#include <functional>
Nan Zhou26500f22022-06-26 23:35:30 +000012#include <string>
13#include <string_view>
14
Ed Tanous478b7ad2024-07-15 19:11:54 -070015#include <gtest/gtest.h>
Jonathan Doman5beaf842020-08-14 11:23:33 -070016
Ed Tanous22ce5452022-01-11 10:50:23 -080017namespace crow::utility
18{
19namespace
20{
21
Ed Tanous47488a92023-06-26 18:19:33 -070022using ::crow::utility::getParameterTag;
Nan Zhoubf8ab7a2022-06-26 23:43:28 +000023
Jonathan Doman5beaf842020-08-14 11:23:33 -070024TEST(Utility, Base64DecodeAuthString)
25{
26 std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
27 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080028 EXPECT_TRUE(base64Decode(authString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070029 EXPECT_EQ(result, "usern4me:passw0rd");
30}
31
32TEST(Utility, Base64DecodeNonAscii)
33{
34 std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
35 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080036 EXPECT_FALSE(base64Decode(junkString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070037}
Adriana Kobylakd830ff52021-01-27 14:15:27 -060038
39TEST(Utility, Base64EncodeString)
40{
41 using namespace std::string_literals;
42 std::string encoded;
43
Ed Tanous22ce5452022-01-11 10:50:23 -080044 encoded = base64encode("");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060045 EXPECT_EQ(encoded, "");
46
Ed Tanous22ce5452022-01-11 10:50:23 -080047 encoded = base64encode("f");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060048 EXPECT_EQ(encoded, "Zg==");
49
Ed Tanous22ce5452022-01-11 10:50:23 -080050 encoded = base64encode("f0");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060051 EXPECT_EQ(encoded, "ZjA=");
52
Ed Tanous22ce5452022-01-11 10:50:23 -080053 encoded = base64encode("f0\0"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060054 EXPECT_EQ(encoded, "ZjAA");
55
Ed Tanous22ce5452022-01-11 10:50:23 -080056 encoded = base64encode("f0\0 "s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060057 EXPECT_EQ(encoded, "ZjAAIA==");
58
Ed Tanous22ce5452022-01-11 10:50:23 -080059 encoded = base64encode("f0\0 B"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060060 EXPECT_EQ(encoded, "ZjAAIEI=");
61
Ed Tanous22ce5452022-01-11 10:50:23 -080062 encoded = base64encode("f0\0 Ba"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060063 EXPECT_EQ(encoded, "ZjAAIEJh");
64
Ed Tanous22ce5452022-01-11 10:50:23 -080065 encoded = base64encode("f0\0 Bar"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060066 EXPECT_EQ(encoded, "ZjAAIEJhcg==");
67}
68
Ed Tanousee192c02023-12-13 10:49:58 -080069TEST(Utility, Base64Encoder)
70{
71 using namespace std::string_literals;
72 std::string data = "f0\0 Bar"s;
73 for (size_t chunkSize = 1; chunkSize < 6; chunkSize++)
74 {
75 std::string_view testString(data);
76 std::string out;
77 Base64Encoder encoder;
78 while (!testString.empty())
79 {
80 size_t thisChunk = std::min(testString.size(), chunkSize);
81 encoder.encode(testString.substr(0, thisChunk), out);
82 testString.remove_prefix(thisChunk);
83 }
84
85 encoder.finalize(out);
86 EXPECT_EQ(out, "ZjAAIEJhcg==");
87 }
88}
89
Adriana Kobylakd830ff52021-01-27 14:15:27 -060090TEST(Utility, Base64EncodeDecodeString)
91{
92 using namespace std::string_literals;
93 std::string data("Data fr\0m 90 reading a \nFile"s);
Ed Tanous22ce5452022-01-11 10:50:23 -080094 std::string encoded = base64encode(data);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060095 std::string decoded;
Ed Tanous22ce5452022-01-11 10:50:23 -080096 EXPECT_TRUE(base64Decode(encoded, decoded));
Adriana Kobylakd830ff52021-01-27 14:15:27 -060097 EXPECT_EQ(data, decoded);
98}
Nan Zhou1d8782e2021-11-29 22:23:18 -080099
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100100TEST(Utility, readUrlSegments)
101{
Ed Tanous6fd29552023-10-04 09:40:14 -0700102 boost::system::result<boost::urls::url_view> parsed =
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100103 boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading");
104
105 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis"));
106
107 EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis"));
108
109 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1"));
110
111 EXPECT_FALSE(
112 readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR"));
113
114 std::string out1;
115 std::string out2;
116 std::string out3;
117 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1)));
118 EXPECT_EQ(out1, "Chassis");
119
120 out1 = out2 = out3 = "";
121 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2),
122 std::ref(out3)));
123 EXPECT_EQ(out1, "redfish");
124 EXPECT_EQ(out2, "v1");
125 EXPECT_EQ(out3, "Chassis");
126
127 out1 = out2 = out3 = "";
128 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis"));
129 EXPECT_EQ(out1, "v1");
130
131 out1 = out2 = out3 = "";
132 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2)));
133 EXPECT_EQ(out1, "redfish");
134 EXPECT_EQ(out2, "Chassis");
135
136 EXPECT_FALSE(readUrlSegments(*parsed, "too", "short"));
137
138 EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long"));
139
140 EXPECT_FALSE(
141 readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2)));
142
143 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1),
144 std::ref(out2), std::ref(out3)));
145
146 parsed = boost::urls::parse_relative_ref("/absolute/url");
147 EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url"));
148
149 parsed = boost::urls::parse_relative_ref("not/absolute/url");
150 EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url"));
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700151
152 parsed = boost::urls::parse_relative_ref("/excellent/path");
153
154 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", "path", OrMorePaths()));
155 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", OrMorePaths()));
156 EXPECT_TRUE(readUrlSegments(*parsed, OrMorePaths()));
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100157}
158
Ed Tanous4456f082022-03-08 18:01:18 -0800159TEST(Router, ParameterTagging)
160{
Ed Tanous15a42df2023-02-09 18:08:23 -0800161 EXPECT_EQ(1, getParameterTag("<str>"));
162 EXPECT_EQ(1, getParameterTag("<string>"));
Ed Tanousd9e89df2024-03-27 14:08:59 -0700163 EXPECT_EQ(1, getParameterTag("<path>"));
164 EXPECT_EQ(2, getParameterTag("<str>/<str>"));
165 EXPECT_EQ(2, getParameterTag("<string>/<string>"));
166 EXPECT_EQ(2, getParameterTag("<path>/<path>"));
Ed Tanous4456f082022-03-08 18:01:18 -0800167}
168
Ed Tanous71f2db72022-05-25 12:28:09 -0700169TEST(URL, JsonEncoding)
170{
Ed Tanous71f2db72022-05-25 12:28:09 -0700171 std::string urlString = "/foo";
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000172 EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
173 EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
Ed Tanous71f2db72022-05-25 12:28:09 -0700174}
175
Willy Tuc6bcedc2022-09-27 05:36:59 +0000176TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters)
177{
Ed Tanousef4c65b2023-04-24 15:28:50 -0700178 boost::urls::url url("/redfish/v1/foo");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000179 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
180
181 appendUrlPieces(url, "bar");
182 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo/bar");
183
Ed Tanous8ece0e42024-01-02 13:16:50 -0800184 appendUrlPieces(url, "/", "bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000185 EXPECT_EQ(std::string_view(url.data(), url.size()),
Ed Tanous8ece0e42024-01-02 13:16:50 -0800186 "/redfish/v1/foo/bar/%2F/bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000187}
188
Ed Tanous22ce5452022-01-11 10:50:23 -0800189} // namespace
190} // namespace crow::utility