blob: 092459782fc66cda80a4d5ba75283e348d7daaf3 [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>
Patrick Williams5900d4c2024-10-10 18:03:28 -04008#include <nlohmann/json.hpp>
Nan Zhou26500f22022-06-26 23:35:30 +00009
Ed Tanousf0b59af2024-03-20 13:38:04 -070010#include <algorithm>
Nan Zhou26500f22022-06-26 23:35:30 +000011#include <ctime>
12#include <functional>
Nan Zhou26500f22022-06-26 23:35:30 +000013#include <string>
14#include <string_view>
15
Ed Tanous478b7ad2024-07-15 19:11:54 -070016#include <gtest/gtest.h>
Jonathan Doman5beaf842020-08-14 11:23:33 -070017
Ed Tanous22ce5452022-01-11 10:50:23 -080018namespace crow::utility
19{
20namespace
21{
22
Ed Tanous47488a92023-06-26 18:19:33 -070023using ::crow::utility::getParameterTag;
Nan Zhoubf8ab7a2022-06-26 23:43:28 +000024
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
33TEST(Utility, Base64DecodeNonAscii)
34{
35 std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
36 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080037 EXPECT_FALSE(base64Decode(junkString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070038}
Adriana Kobylakd830ff52021-01-27 14:15:27 -060039
40TEST(Utility, Base64EncodeString)
41{
42 using namespace std::string_literals;
43 std::string encoded;
44
Ed Tanous22ce5452022-01-11 10:50:23 -080045 encoded = base64encode("");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060046 EXPECT_EQ(encoded, "");
47
Ed Tanous22ce5452022-01-11 10:50:23 -080048 encoded = base64encode("f");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060049 EXPECT_EQ(encoded, "Zg==");
50
Ed Tanous22ce5452022-01-11 10:50:23 -080051 encoded = base64encode("f0");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060052 EXPECT_EQ(encoded, "ZjA=");
53
Ed Tanous22ce5452022-01-11 10:50:23 -080054 encoded = base64encode("f0\0"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060055 EXPECT_EQ(encoded, "ZjAA");
56
Ed Tanous22ce5452022-01-11 10:50:23 -080057 encoded = base64encode("f0\0 "s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060058 EXPECT_EQ(encoded, "ZjAAIA==");
59
Ed Tanous22ce5452022-01-11 10:50:23 -080060 encoded = base64encode("f0\0 B"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060061 EXPECT_EQ(encoded, "ZjAAIEI=");
62
Ed Tanous22ce5452022-01-11 10:50:23 -080063 encoded = base64encode("f0\0 Ba"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060064 EXPECT_EQ(encoded, "ZjAAIEJh");
65
Ed Tanous22ce5452022-01-11 10:50:23 -080066 encoded = base64encode("f0\0 Bar"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060067 EXPECT_EQ(encoded, "ZjAAIEJhcg==");
68}
69
Ed Tanousee192c02023-12-13 10:49:58 -080070TEST(Utility, Base64Encoder)
71{
72 using namespace std::string_literals;
73 std::string data = "f0\0 Bar"s;
74 for (size_t chunkSize = 1; chunkSize < 6; chunkSize++)
75 {
76 std::string_view testString(data);
77 std::string out;
78 Base64Encoder encoder;
79 while (!testString.empty())
80 {
81 size_t thisChunk = std::min(testString.size(), chunkSize);
82 encoder.encode(testString.substr(0, thisChunk), out);
83 testString.remove_prefix(thisChunk);
84 }
85
86 encoder.finalize(out);
87 EXPECT_EQ(out, "ZjAAIEJhcg==");
88 }
89}
90
Adriana Kobylakd830ff52021-01-27 14:15:27 -060091TEST(Utility, Base64EncodeDecodeString)
92{
93 using namespace std::string_literals;
94 std::string data("Data fr\0m 90 reading a \nFile"s);
Ed Tanous22ce5452022-01-11 10:50:23 -080095 std::string encoded = base64encode(data);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060096 std::string decoded;
Ed Tanous22ce5452022-01-11 10:50:23 -080097 EXPECT_TRUE(base64Decode(encoded, decoded));
Adriana Kobylakd830ff52021-01-27 14:15:27 -060098 EXPECT_EQ(data, decoded);
99}
Nan Zhou1d8782e2021-11-29 22:23:18 -0800100
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100101TEST(Utility, readUrlSegments)
102{
Ed Tanous6fd29552023-10-04 09:40:14 -0700103 boost::system::result<boost::urls::url_view> parsed =
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100104 boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading");
105
106 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis"));
107
108 EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis"));
109
110 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1"));
111
112 EXPECT_FALSE(
113 readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR"));
114
115 std::string out1;
116 std::string out2;
117 std::string out3;
118 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1)));
119 EXPECT_EQ(out1, "Chassis");
120
121 out1 = out2 = out3 = "";
122 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2),
123 std::ref(out3)));
124 EXPECT_EQ(out1, "redfish");
125 EXPECT_EQ(out2, "v1");
126 EXPECT_EQ(out3, "Chassis");
127
128 out1 = out2 = out3 = "";
129 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis"));
130 EXPECT_EQ(out1, "v1");
131
132 out1 = out2 = out3 = "";
133 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2)));
134 EXPECT_EQ(out1, "redfish");
135 EXPECT_EQ(out2, "Chassis");
136
137 EXPECT_FALSE(readUrlSegments(*parsed, "too", "short"));
138
139 EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long"));
140
141 EXPECT_FALSE(
142 readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2)));
143
144 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1),
145 std::ref(out2), std::ref(out3)));
146
147 parsed = boost::urls::parse_relative_ref("/absolute/url");
148 EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url"));
149
150 parsed = boost::urls::parse_relative_ref("not/absolute/url");
151 EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url"));
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700152
153 parsed = boost::urls::parse_relative_ref("/excellent/path");
154
155 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", "path", OrMorePaths()));
156 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", OrMorePaths()));
157 EXPECT_TRUE(readUrlSegments(*parsed, OrMorePaths()));
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100158}
159
Ed Tanous4456f082022-03-08 18:01:18 -0800160TEST(Router, ParameterTagging)
161{
Ed Tanous15a42df2023-02-09 18:08:23 -0800162 EXPECT_EQ(1, getParameterTag("<str>"));
163 EXPECT_EQ(1, getParameterTag("<string>"));
Ed Tanousd9e89df2024-03-27 14:08:59 -0700164 EXPECT_EQ(1, getParameterTag("<path>"));
165 EXPECT_EQ(2, getParameterTag("<str>/<str>"));
166 EXPECT_EQ(2, getParameterTag("<string>/<string>"));
167 EXPECT_EQ(2, getParameterTag("<path>/<path>"));
Ed Tanous4456f082022-03-08 18:01:18 -0800168}
169
Ed Tanous71f2db72022-05-25 12:28:09 -0700170TEST(URL, JsonEncoding)
171{
Ed Tanous71f2db72022-05-25 12:28:09 -0700172 std::string urlString = "/foo";
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000173 EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
174 EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
Ed Tanous71f2db72022-05-25 12:28:09 -0700175}
176
Willy Tuc6bcedc2022-09-27 05:36:59 +0000177TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters)
178{
Ed Tanousef4c65b2023-04-24 15:28:50 -0700179 boost::urls::url url("/redfish/v1/foo");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000180 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
181
182 appendUrlPieces(url, "bar");
183 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo/bar");
184
Ed Tanous8ece0e42024-01-02 13:16:50 -0800185 appendUrlPieces(url, "/", "bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000186 EXPECT_EQ(std::string_view(url.data(), url.size()),
Ed Tanous8ece0e42024-01-02 13:16:50 -0800187 "/redfish/v1/foo/bar/%2F/bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000188}
189
Ed Tanous22ce5452022-01-11 10:50:23 -0800190} // namespace
191} // namespace crow::utility