blob: 62a95a40e3ba371e4047f9a2d6cf947987a35b65 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous7a6f0032024-01-28 09:49:03 -08003#include "file_test_utilities.hpp"
Ed Tanousb2896142024-01-31 15:25:47 -08004#include "http/http_body.hpp"
Abhilash Raju8e3f7032023-07-17 08:53:11 -05005#include "http/http_response.hpp"
Ed Tanousf0b59af2024-03-20 13:38:04 -07006#include "utility.hpp"
Abhilash Raju8e3f7032023-07-17 08:53:11 -05007
Ed Tanous8d45b9c2024-04-08 17:50:10 -07008#include <boost/beast/core/buffers_to_string.hpp>
Ed Tanousf0b59af2024-03-20 13:38:04 -07009#include <boost/beast/core/file_base.hpp>
10#include <boost/beast/core/file_posix.hpp>
11#include <boost/beast/http/message.hpp>
Ed Tanous8d45b9c2024-04-08 17:50:10 -070012#include <boost/beast/http/serializer.hpp>
Ed Tanousf0b59af2024-03-20 13:38:04 -070013#include <boost/beast/http/status.hpp>
14
15#include <cstdio>
Abhilash Raju8e3f7032023-07-17 08:53:11 -050016#include <filesystem>
Ed Tanousf0b59af2024-03-20 13:38:04 -070017#include <string>
Abhilash Raju8e3f7032023-07-17 08:53:11 -050018
19#include "gtest/gtest.h"
Ed Tanous05c27352024-10-09 17:00:37 -070020namespace crow
21{
Abhilash Raju8e3f7032023-07-17 08:53:11 -050022namespace
23{
Ed Tanous05c27352024-10-09 17:00:37 -070024void addHeaders(Response& res)
Abhilash Raju8e3f7032023-07-17 08:53:11 -050025{
26 res.addHeader("myheader", "myvalue");
27 res.keepAlive(true);
28 res.result(boost::beast::http::status::ok);
29}
Ed Tanous05c27352024-10-09 17:00:37 -070030void verifyHeaders(Response& res)
Abhilash Raju8e3f7032023-07-17 08:53:11 -050031{
32 EXPECT_EQ(res.getHeaderValue("myheader"), "myvalue");
33 EXPECT_EQ(res.keepAlive(), true);
34 EXPECT_EQ(res.result(), boost::beast::http::status::ok);
35}
36
Ed Tanousb2896142024-01-31 15:25:47 -080037std::string getData(boost::beast::http::response<bmcweb::HttpBody>& m)
Abhilash Rajub5f288d2023-11-08 22:32:44 -060038{
Ed Tanous52e31622024-01-23 16:31:11 -080039 std::string ret;
40
Ed Tanousb2896142024-01-31 15:25:47 -080041 boost::beast::http::response_serializer<bmcweb::HttpBody> sr{m};
Ed Tanous52e31622024-01-23 16:31:11 -080042 sr.split(true);
Ed Tanous5575efb2024-01-30 09:25:03 -080043 // Reads buffers into ret
Patrick Williamsbd79bce2024-08-16 15:22:20 -040044 auto reader =
45 [&sr, &ret](const boost::system::error_code& ec2, const auto& buffer) {
46 EXPECT_FALSE(ec2);
47 std::string ret2 = boost::beast::buffers_to_string(buffer);
48 sr.consume(ret2.size());
49 ret += ret2;
50 };
Ed Tanous52e31622024-01-23 16:31:11 -080051 boost::system::error_code ec;
52
53 // Read headers
Abhilash Rajub5f288d2023-11-08 22:32:44 -060054 while (!sr.is_header_done())
55 {
Ed Tanous52e31622024-01-23 16:31:11 -080056 sr.next(ec, reader);
57 EXPECT_FALSE(ec);
Abhilash Rajub5f288d2023-11-08 22:32:44 -060058 }
Ed Tanous52e31622024-01-23 16:31:11 -080059 ret.clear();
Abhilash Rajub5f288d2023-11-08 22:32:44 -060060
Ed Tanous52e31622024-01-23 16:31:11 -080061 // Read body
Abhilash Rajub5f288d2023-11-08 22:32:44 -060062 while (!sr.is_done())
63 {
Ed Tanous52e31622024-01-23 16:31:11 -080064 sr.next(ec, reader);
Abhilash Rajub5f288d2023-11-08 22:32:44 -060065 EXPECT_FALSE(ec);
66 }
67
68 return ret;
69}
Ed Tanous52e31622024-01-23 16:31:11 -080070
Abhilash Raju8e3f7032023-07-17 08:53:11 -050071TEST(HttpResponse, Headers)
72{
Ed Tanous05c27352024-10-09 17:00:37 -070073 Response res;
Abhilash Raju8e3f7032023-07-17 08:53:11 -050074 addHeaders(res);
75 verifyHeaders(res);
76}
77TEST(HttpResponse, StringBody)
78{
Ed Tanous05c27352024-10-09 17:00:37 -070079 Response res;
Abhilash Raju8e3f7032023-07-17 08:53:11 -050080 addHeaders(res);
Ed Tanous5575efb2024-01-30 09:25:03 -080081 std::string_view bodyValue = "this is my new body";
82 res.write({bodyValue.data(), bodyValue.length()});
83 EXPECT_EQ(*res.body(), bodyValue);
Abhilash Raju8e3f7032023-07-17 08:53:11 -050084 verifyHeaders(res);
85}
Ed Tanousb2896142024-01-31 15:25:47 -080086TEST(HttpResponse, HttpBody)
Abhilash Raju8e3f7032023-07-17 08:53:11 -050087{
Ed Tanous05c27352024-10-09 17:00:37 -070088 Response res;
Abhilash Raju8e3f7032023-07-17 08:53:11 -050089 addHeaders(res);
Ed Tanous5575efb2024-01-30 09:25:03 -080090 TemporaryFileHandle temporaryFile("sample text");
91 res.openFile(temporaryFile.stringPath);
Abhilash Raju8e3f7032023-07-17 08:53:11 -050092
93 verifyHeaders(res);
Abhilash Raju8e3f7032023-07-17 08:53:11 -050094}
Ed Tanousb2896142024-01-31 15:25:47 -080095TEST(HttpResponse, HttpBodyWithFd)
Abhilash Rajub5f288d2023-11-08 22:32:44 -060096{
Ed Tanous05c27352024-10-09 17:00:37 -070097 Response res;
Abhilash Rajub5f288d2023-11-08 22:32:44 -060098 addHeaders(res);
Ed Tanous5575efb2024-01-30 09:25:03 -080099 TemporaryFileHandle temporaryFile("sample text");
100 FILE* fd = fopen(temporaryFile.stringPath.c_str(), "r+");
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600101 res.openFd(fileno(fd));
102 verifyHeaders(res);
103 fclose(fd);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600104}
105
Ed Tanousb2896142024-01-31 15:25:47 -0800106TEST(HttpResponse, Base64HttpBodyWithFd)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600107{
Ed Tanous05c27352024-10-09 17:00:37 -0700108 Response res;
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600109 addHeaders(res);
Ed Tanous5575efb2024-01-30 09:25:03 -0800110 TemporaryFileHandle temporaryFile("sample text");
111 FILE* fd = fopen(temporaryFile.stringPath.c_str(), "r");
112 ASSERT_NE(fd, nullptr);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600113 res.openFd(fileno(fd), bmcweb::EncodingType::Base64);
114 verifyHeaders(res);
115 fclose(fd);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600116}
117
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500118TEST(HttpResponse, BodyTransitions)
119{
Ed Tanous05c27352024-10-09 17:00:37 -0700120 Response res;
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500121 addHeaders(res);
Ed Tanous5575efb2024-01-30 09:25:03 -0800122 TemporaryFileHandle temporaryFile("sample text");
123 res.openFile(temporaryFile.stringPath);
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500124
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500125 verifyHeaders(res);
126 res.write("body text");
127
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500128 verifyHeaders(res);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600129}
130
Ed Tanous7a6f0032024-01-28 09:49:03 -0800131std::string generateBigdata()
132{
133 std::string result;
134 while (result.size() < 10000)
135 {
136 result += "sample text";
137 }
138 return result;
139}
140
141TEST(HttpResponse, StringBodyWriterLarge)
142{
Ed Tanous05c27352024-10-09 17:00:37 -0700143 Response res;
Ed Tanous7a6f0032024-01-28 09:49:03 -0800144 std::string data = generateBigdata();
145 res.write(std::string(data));
Ed Tanous5575efb2024-01-30 09:25:03 -0800146 EXPECT_EQ(getData(res.response), data);
Ed Tanous7a6f0032024-01-28 09:49:03 -0800147}
148
Ed Tanousb2896142024-01-31 15:25:47 -0800149TEST(HttpResponse, Base64HttpBodyWriter)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600150{
Ed Tanous05c27352024-10-09 17:00:37 -0700151 Response res;
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600152 std::string data = "sample text";
Ed Tanous5575efb2024-01-30 09:25:03 -0800153 TemporaryFileHandle temporaryFile(data);
154 FILE* f = fopen(temporaryFile.stringPath.c_str(), "r+");
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600155 res.openFd(fileno(f), bmcweb::EncodingType::Base64);
Ed Tanous5575efb2024-01-30 09:25:03 -0800156 EXPECT_EQ(getData(res.response), "c2FtcGxlIHRleHQ=");
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600157}
158
Ed Tanousb2896142024-01-31 15:25:47 -0800159TEST(HttpResponse, Base64HttpBodyWriterLarge)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600160{
Ed Tanous05c27352024-10-09 17:00:37 -0700161 Response res;
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600162 std::string data = generateBigdata();
Ed Tanous5575efb2024-01-30 09:25:03 -0800163 TemporaryFileHandle temporaryFile(data);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600164
Ed Tanous5575efb2024-01-30 09:25:03 -0800165 boost::beast::file_posix file;
166 boost::system::error_code ec;
167 file.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
168 ec);
169 EXPECT_EQ(ec.value(), 0);
170 res.openFd(file.native_handle(), bmcweb::EncodingType::Base64);
Ed Tanous05c27352024-10-09 17:00:37 -0700171 EXPECT_EQ(getData(res.response), utility::base64encode(data));
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600172}
173
Ed Tanousb2896142024-01-31 15:25:47 -0800174TEST(HttpResponse, HttpBodyWriterLarge)
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600175{
Ed Tanous05c27352024-10-09 17:00:37 -0700176 Response res;
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600177 std::string data = generateBigdata();
Ed Tanous5575efb2024-01-30 09:25:03 -0800178 TemporaryFileHandle temporaryFile(data);
179
180 boost::beast::file_posix file;
181 boost::system::error_code ec;
182 file.open(temporaryFile.stringPath.c_str(), boost::beast::file_mode::read,
183 ec);
184 EXPECT_EQ(ec.value(), 0);
185 res.openFd(file.native_handle());
186 EXPECT_EQ(getData(res.response), data);
Abhilash Rajub5f288d2023-11-08 22:32:44 -0600187}
Abhilash Raju8e3f7032023-07-17 08:53:11 -0500188} // namespace
Ed Tanous05c27352024-10-09 17:00:37 -0700189} // namespace crow