1 #include "async_resp.hpp"
2 #include "http/http_connection.hpp"
3 #include "http/http_request.hpp"
4 #include "http/http_response.hpp"
5 
6 #include <boost/asio/buffer.hpp>
7 #include <boost/asio/io_context.hpp>
8 #include <boost/asio/steady_timer.hpp>
9 #include <boost/beast/_experimental/test/stream.hpp>
10 #include <boost/beast/http/field.hpp>
11 #include <boost/beast/http/verb.hpp>
12 
13 #include <chrono>
14 #include <functional>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 
19 #include "gtest/gtest.h"
20 namespace crow
21 {
22 
23 struct FakeHandler
24 {
25     static void
26         handleUpgrade(Request& /*req*/,
27                       const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
28                       boost::beast::test::stream&& /*adaptor*/)
29     {
30         // Handle Upgrade should never be called
31         EXPECT_FALSE(true);
32     }
33 
34     void handle(Request& req,
35                 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/)
36     {
37         EXPECT_EQ(req.method(), boost::beast::http::verb::get);
38         EXPECT_EQ(req.target(), "/");
39         EXPECT_EQ(req.getHeaderValue(boost::beast::http::field::host),
40                   "openbmc_project.xyz");
41         EXPECT_FALSE(req.keepAlive());
42         EXPECT_EQ(req.version(), 11);
43         EXPECT_EQ(req.body(), "");
44 
45         called = true;
46     }
47     bool called = false;
48 };
49 
50 struct ClockFake
51 {
52     bool wascalled = false;
53     std::string getDateStr()
54     {
55         wascalled = true;
56         return "TestTime";
57     }
58 };
59 
60 TEST(http_connection, RequestPropogates)
61 {
62     boost::asio::io_context io;
63     ClockFake clock;
64     boost::beast::test::stream stream(io);
65     boost::beast::test::stream out(io);
66     stream.connect(out);
67 
68     out.write_some(boost::asio::buffer(
69         "GET / HTTP/1.1\r\nHost: openbmc_project.xyz\r\nConnection: close\r\n\r\n"));
70     FakeHandler handler;
71     boost::asio::steady_timer timer(io);
72     std::function<std::string()> date(
73         std::bind_front(&ClockFake::getDateStr, &clock));
74     std::shared_ptr<crow::Connection<boost::beast::test::stream, FakeHandler>>
75         conn = std::make_shared<
76             crow::Connection<boost::beast::test::stream, FakeHandler>>(
77             &handler, std::move(timer), date, std::move(stream));
78     conn->start();
79     io.run_for(std::chrono::seconds(1000));
80     EXPECT_TRUE(handler.called);
81     std::string outStr = out.str();
82 
83     std::string expected =
84         "HTTP/1.1 200 OK\r\n"
85         "Connection: close\r\n"
86         "Strict-Transport-Security: max-age=31536000; includeSubdomains\r\n"
87         "X-Frame-Options: DENY\r\n"
88         "Pragma: no-cache\r\n"
89         "Cache-Control: no-store, max-age=0\r\n"
90         "X-Content-Type-Options: nosniff\r\n"
91         "Referrer-Policy: no-referrer\r\n"
92         "Permissions-Policy: accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(),camera=(),display-capture=(),document-domain=(),encrypted-media=(),fullscreen=(),gamepad=(),geolocation=(),gyroscope=(),layout-animations=(self),legacy-image-formats=(self),magnetometer=(),microphone=(),midi=(),oversized-images=(self),payment=(),picture-in-picture=(),publickey-credentials-get=(),speaker-selection=(),sync-xhr=(self),unoptimized-images=(self),unsized-media=(self),usb=(),screen-wak-lock=(),web-share=(),xr-spatial-tracking=()\r\n"
93         "X-Permitted-Cross-Domain-Policies: none\r\n"
94         "Cross-Origin-Embedder-Policy: require-corp\r\n"
95         "Cross-Origin-Opener-Policy: same-origin\r\n"
96         "Cross-Origin-Resource-Policy: same-origin\r\n"
97         "Content-Security-Policy: default-src 'none'; img-src 'self' data:; font-src 'self'; style-src 'self'; script-src 'self'; connect-src 'self' wss:; form-action 'none'; frame-ancestors 'none'; object-src 'none'; base-uri 'none'\r\n"
98         "Date: TestTime\r\n"
99         "Content-Length: 0\r\n\r\n";
100     EXPECT_EQ(outStr, expected);
101     EXPECT_TRUE(clock.wascalled);
102 }
103 
104 } // namespace crow
105