blob: f5ae5b1b5e5c183011ed8de2edb866c2af582159 [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
Nan Zhoud5c80ad2022-07-11 01:16:31 +000015#include <gtest/gtest.h> // IWYU pragma: keep
16// IWYU pragma: no_include <gtest/gtest-message.h>
17// IWYU pragma: no_include <gtest/gtest-test-part.h>
18// IWYU pragma: no_include "gtest/gtest_pred_impl.h"
Jonathan Doman5beaf842020-08-14 11:23:33 -070019
Ed Tanous22ce5452022-01-11 10:50:23 -080020namespace crow::utility
21{
22namespace
23{
24
Ed Tanous47488a92023-06-26 18:19:33 -070025using ::crow::utility::getParameterTag;
Nan Zhoubf8ab7a2022-06-26 23:43:28 +000026
Jonathan Doman5beaf842020-08-14 11:23:33 -070027TEST(Utility, Base64DecodeAuthString)
28{
29 std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
30 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080031 EXPECT_TRUE(base64Decode(authString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070032 EXPECT_EQ(result, "usern4me:passw0rd");
33}
34
35TEST(Utility, Base64DecodeNonAscii)
36{
37 std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
38 std::string result;
Ed Tanous22ce5452022-01-11 10:50:23 -080039 EXPECT_FALSE(base64Decode(junkString, result));
Jonathan Doman5beaf842020-08-14 11:23:33 -070040}
Adriana Kobylakd830ff52021-01-27 14:15:27 -060041
42TEST(Utility, Base64EncodeString)
43{
44 using namespace std::string_literals;
45 std::string encoded;
46
Ed Tanous22ce5452022-01-11 10:50:23 -080047 encoded = base64encode("");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060048 EXPECT_EQ(encoded, "");
49
Ed Tanous22ce5452022-01-11 10:50:23 -080050 encoded = base64encode("f");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060051 EXPECT_EQ(encoded, "Zg==");
52
Ed Tanous22ce5452022-01-11 10:50:23 -080053 encoded = base64encode("f0");
Adriana Kobylakd830ff52021-01-27 14:15:27 -060054 EXPECT_EQ(encoded, "ZjA=");
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, "ZjAA");
58
Ed Tanous22ce5452022-01-11 10:50:23 -080059 encoded = base64encode("f0\0 "s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060060 EXPECT_EQ(encoded, "ZjAAIA==");
61
Ed Tanous22ce5452022-01-11 10:50:23 -080062 encoded = base64encode("f0\0 B"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060063 EXPECT_EQ(encoded, "ZjAAIEI=");
64
Ed Tanous22ce5452022-01-11 10:50:23 -080065 encoded = base64encode("f0\0 Ba"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060066 EXPECT_EQ(encoded, "ZjAAIEJh");
67
Ed Tanous22ce5452022-01-11 10:50:23 -080068 encoded = base64encode("f0\0 Bar"s);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060069 EXPECT_EQ(encoded, "ZjAAIEJhcg==");
70}
71
Ed Tanousee192c02023-12-13 10:49:58 -080072TEST(Utility, Base64Encoder)
73{
74 using namespace std::string_literals;
75 std::string data = "f0\0 Bar"s;
76 for (size_t chunkSize = 1; chunkSize < 6; chunkSize++)
77 {
78 std::string_view testString(data);
79 std::string out;
80 Base64Encoder encoder;
81 while (!testString.empty())
82 {
83 size_t thisChunk = std::min(testString.size(), chunkSize);
84 encoder.encode(testString.substr(0, thisChunk), out);
85 testString.remove_prefix(thisChunk);
86 }
87
88 encoder.finalize(out);
89 EXPECT_EQ(out, "ZjAAIEJhcg==");
90 }
91}
92
Adriana Kobylakd830ff52021-01-27 14:15:27 -060093TEST(Utility, Base64EncodeDecodeString)
94{
95 using namespace std::string_literals;
96 std::string data("Data fr\0m 90 reading a \nFile"s);
Ed Tanous22ce5452022-01-11 10:50:23 -080097 std::string encoded = base64encode(data);
Adriana Kobylakd830ff52021-01-27 14:15:27 -060098 std::string decoded;
Ed Tanous22ce5452022-01-11 10:50:23 -080099 EXPECT_TRUE(base64Decode(encoded, decoded));
Adriana Kobylakd830ff52021-01-27 14:15:27 -0600100 EXPECT_EQ(data, decoded);
101}
Nan Zhou1d8782e2021-11-29 22:23:18 -0800102
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100103TEST(Utility, readUrlSegments)
104{
Ed Tanous6fd29552023-10-04 09:40:14 -0700105 boost::system::result<boost::urls::url_view> parsed =
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100106 boost::urls::parse_relative_ref("/redfish/v1/Chassis#/Fans/0/Reading");
107
108 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", "Chassis"));
109
110 EXPECT_FALSE(readUrlSegments(*parsed, "FOOBAR", "v1", "Chassis"));
111
112 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", "v1"));
113
114 EXPECT_FALSE(
115 readUrlSegments(*parsed, "redfish", "v1", "Chassis", "FOOBAR"));
116
117 std::string out1;
118 std::string out2;
119 std::string out3;
120 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", "v1", std::ref(out1)));
121 EXPECT_EQ(out1, "Chassis");
122
123 out1 = out2 = out3 = "";
124 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), std::ref(out2),
125 std::ref(out3)));
126 EXPECT_EQ(out1, "redfish");
127 EXPECT_EQ(out2, "v1");
128 EXPECT_EQ(out3, "Chassis");
129
130 out1 = out2 = out3 = "";
131 EXPECT_TRUE(readUrlSegments(*parsed, "redfish", std::ref(out1), "Chassis"));
132 EXPECT_EQ(out1, "v1");
133
134 out1 = out2 = out3 = "";
135 EXPECT_TRUE(readUrlSegments(*parsed, std::ref(out1), "v1", std::ref(out2)));
136 EXPECT_EQ(out1, "redfish");
137 EXPECT_EQ(out2, "Chassis");
138
139 EXPECT_FALSE(readUrlSegments(*parsed, "too", "short"));
140
141 EXPECT_FALSE(readUrlSegments(*parsed, "too", "long", "too", "long"));
142
143 EXPECT_FALSE(
144 readUrlSegments(*parsed, std::ref(out1), "v2", std::ref(out2)));
145
146 EXPECT_FALSE(readUrlSegments(*parsed, "redfish", std::ref(out1),
147 std::ref(out2), std::ref(out3)));
148
149 parsed = boost::urls::parse_relative_ref("/absolute/url");
150 EXPECT_TRUE(readUrlSegments(*parsed, "absolute", "url"));
151
152 parsed = boost::urls::parse_relative_ref("not/absolute/url");
153 EXPECT_FALSE(readUrlSegments(*parsed, "not", "absolute", "url"));
Ed Tanous7f8d8fa2022-08-19 07:00:38 -0700154
155 parsed = boost::urls::parse_relative_ref("/excellent/path");
156
157 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", "path", OrMorePaths()));
158 EXPECT_TRUE(readUrlSegments(*parsed, "excellent", OrMorePaths()));
159 EXPECT_TRUE(readUrlSegments(*parsed, OrMorePaths()));
Szymon Dompkeca1600c2022-03-03 14:42:52 +0100160}
161
Ed Tanous4456f082022-03-08 18:01:18 -0800162TEST(Router, ParameterTagging)
163{
Ed Tanous15a42df2023-02-09 18:08:23 -0800164 EXPECT_EQ(1, getParameterTag("<str>"));
165 EXPECT_EQ(1, getParameterTag("<string>"));
Ed Tanousd9e89df2024-03-27 14:08:59 -0700166 EXPECT_EQ(1, getParameterTag("<path>"));
167 EXPECT_EQ(2, getParameterTag("<str>/<str>"));
168 EXPECT_EQ(2, getParameterTag("<string>/<string>"));
169 EXPECT_EQ(2, getParameterTag("<path>/<path>"));
Ed Tanous4456f082022-03-08 18:01:18 -0800170}
171
Ed Tanous71f2db72022-05-25 12:28:09 -0700172TEST(URL, JsonEncoding)
173{
Ed Tanous71f2db72022-05-25 12:28:09 -0700174 std::string urlString = "/foo";
Nan Zhoubf8ab7a2022-06-26 23:43:28 +0000175 EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
176 EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
Ed Tanous71f2db72022-05-25 12:28:09 -0700177}
178
Willy Tuc6bcedc2022-09-27 05:36:59 +0000179TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters)
180{
Ed Tanousef4c65b2023-04-24 15:28:50 -0700181 boost::urls::url url("/redfish/v1/foo");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000182 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo");
183
184 appendUrlPieces(url, "bar");
185 EXPECT_EQ(std::string_view(url.data(), url.size()), "/redfish/v1/foo/bar");
186
Ed Tanous8ece0e42024-01-02 13:16:50 -0800187 appendUrlPieces(url, "/", "bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000188 EXPECT_EQ(std::string_view(url.data(), url.size()),
Ed Tanous8ece0e42024-01-02 13:16:50 -0800189 "/redfish/v1/foo/bar/%2F/bad&string");
Willy Tuc6bcedc2022-09-27 05:36:59 +0000190}
191
Ed Tanous22ce5452022-01-11 10:50:23 -0800192} // namespace
193} // namespace crow::utility