xref: /openbmc/bmcweb/test/http/utility_test.cpp (revision 5ebb9d33)
1 #include "bmcweb_config.h"
2 
3 #include "utility.hpp"
4 
5 #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 
17 #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"
21 
22 namespace crow::utility
23 {
24 namespace
25 {
26 
27 using ::crow::utility::getParameterTag;
28 
29 TEST(Utility, Base64DecodeAuthString)
30 {
31     std::string authString("dXNlcm40bWU6cGFzc3cwcmQ=");
32     std::string result;
33     EXPECT_TRUE(base64Decode(authString, result));
34     EXPECT_EQ(result, "usern4me:passw0rd");
35 }
36 
37 TEST(Utility, Base64DecodeNonAscii)
38 {
39     std::string junkString("\xff\xee\xdd\xcc\x01\x11\x22\x33");
40     std::string result;
41     EXPECT_FALSE(base64Decode(junkString, result));
42 }
43 
44 TEST(Utility, Base64EncodeString)
45 {
46     using namespace std::string_literals;
47     std::string encoded;
48 
49     encoded = base64encode("");
50     EXPECT_EQ(encoded, "");
51 
52     encoded = base64encode("f");
53     EXPECT_EQ(encoded, "Zg==");
54 
55     encoded = base64encode("f0");
56     EXPECT_EQ(encoded, "ZjA=");
57 
58     encoded = base64encode("f0\0"s);
59     EXPECT_EQ(encoded, "ZjAA");
60 
61     encoded = base64encode("f0\0 "s);
62     EXPECT_EQ(encoded, "ZjAAIA==");
63 
64     encoded = base64encode("f0\0 B"s);
65     EXPECT_EQ(encoded, "ZjAAIEI=");
66 
67     encoded = base64encode("f0\0 Ba"s);
68     EXPECT_EQ(encoded, "ZjAAIEJh");
69 
70     encoded = base64encode("f0\0 Bar"s);
71     EXPECT_EQ(encoded, "ZjAAIEJhcg==");
72 }
73 
74 TEST(Utility, Base64EncodeDecodeString)
75 {
76     using namespace std::string_literals;
77     std::string data("Data fr\0m 90 reading a \nFile"s);
78     std::string encoded = base64encode(data);
79     std::string decoded;
80     EXPECT_TRUE(base64Decode(encoded, decoded));
81     EXPECT_EQ(data, decoded);
82 }
83 
84 TEST(Utility, readUrlSegments)
85 {
86     boost::system::result<boost::urls::url_view> parsed =
87         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"));
135 
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()));
141 }
142 
143 TEST(Router, ParameterTagging)
144 {
145     EXPECT_EQ(1, getParameterTag("<str>"));
146     EXPECT_EQ(1, getParameterTag("<string>"));
147     EXPECT_EQ(2, getParameterTag("<path>"));
148 }
149 
150 TEST(URL, JsonEncoding)
151 {
152     std::string urlString = "/foo";
153     EXPECT_EQ(nlohmann::json(boost::urls::url(urlString)), urlString);
154     EXPECT_EQ(nlohmann::json(boost::urls::url_view(urlString)), urlString);
155 }
156 
157 TEST(AppendUrlFromPieces, PiecesAreAppendedViaDelimiters)
158 {
159     boost::urls::url url("/redfish/v1/foo");
160     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 
165     appendUrlPieces(url, "/", "bad&tring");
166     EXPECT_EQ(std::string_view(url.data(), url.size()),
167               "/redfish/v1/foo/bar/%2F/bad&tring");
168 }
169 
170 } // namespace
171 } // namespace crow::utility
172