Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 3 | #include "async_resp.hpp" |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 4 | #include "http/http_connection.hpp" |
| 5 | #include "http/http_request.hpp" |
| 6 | #include "http/http_response.hpp" |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 7 | #include "http_connect_types.hpp" |
| 8 | #include "test_stream.hpp" |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 9 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 10 | #include <boost/asio/buffer.hpp> |
| 11 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 12 | #include <boost/asio/ssl/context.hpp> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 13 | #include <boost/asio/steady_timer.hpp> |
| 14 | #include <boost/beast/_experimental/test/stream.hpp> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 15 | #include <boost/beast/http/field.hpp> |
| 16 | #include <boost/beast/http/verb.hpp> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 17 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 18 | #include <chrono> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 19 | #include <functional> |
| 20 | #include <memory> |
| 21 | #include <string> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 22 | #include <utility> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 23 | |
| 24 | #include "gtest/gtest.h" |
| 25 | namespace crow |
| 26 | { |
| 27 | |
| 28 | struct FakeHandler |
| 29 | { |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 30 | template <typename Adaptor> |
Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 31 | static void handleUpgrade( |
| 32 | const std::shared_ptr<Request>& /*req*/, |
| 33 | const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 34 | Adaptor&& /*adaptor*/) |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 35 | { |
| 36 | // Handle Upgrade should never be called |
| 37 | EXPECT_FALSE(true); |
| 38 | } |
| 39 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 40 | void handle(const std::shared_ptr<Request>& req, |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 41 | const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/) |
| 42 | { |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 43 | EXPECT_EQ(req->method(), boost::beast::http::verb::get); |
| 44 | EXPECT_EQ(req->target(), "/"); |
| 45 | EXPECT_EQ(req->getHeaderValue(boost::beast::http::field::host), |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 46 | "openbmc_project.xyz"); |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 47 | EXPECT_FALSE(req->keepAlive()); |
| 48 | EXPECT_EQ(req->version(), 11); |
| 49 | EXPECT_EQ(req->body(), ""); |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 50 | |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 51 | called = true; |
| 52 | } |
| 53 | bool called = false; |
| 54 | }; |
| 55 | |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 56 | struct ClockFake |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 57 | { |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 58 | bool wascalled = false; |
| 59 | std::string getDateStr() |
| 60 | { |
| 61 | wascalled = true; |
| 62 | return "TestTime"; |
| 63 | } |
| 64 | }; |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 65 | |
| 66 | TEST(http_connection, RequestPropogates) |
| 67 | { |
| 68 | boost::asio::io_context io; |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 69 | ClockFake clock; |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 70 | TestStream stream(io); |
| 71 | TestStream out(io); |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 72 | stream.connect(out); |
| 73 | |
| 74 | out.write_some(boost::asio::buffer( |
| 75 | "GET / HTTP/1.1\r\nHost: openbmc_project.xyz\r\nConnection: close\r\n\r\n")); |
| 76 | FakeHandler handler; |
| 77 | boost::asio::steady_timer timer(io); |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 78 | std::function<std::string()> date( |
| 79 | std::bind_front(&ClockFake::getDateStr, &clock)); |
Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 80 | |
| 81 | boost::asio::ssl::context context{boost::asio::ssl::context::tls}; |
| 82 | std::shared_ptr<crow::Connection<TestStream, FakeHandler>> conn = |
| 83 | std::make_shared<crow::Connection<TestStream, FakeHandler>>( |
| 84 | &handler, HttpType::HTTP, std::move(timer), date, |
| 85 | boost::asio::ssl::stream<TestStream>(std::move(stream), context)); |
| 86 | conn->disableAuth(); |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 87 | conn->start(); |
| 88 | io.run_for(std::chrono::seconds(1000)); |
| 89 | EXPECT_TRUE(handler.called); |
| 90 | std::string outStr = out.str(); |
| 91 | |
| 92 | std::string expected = |
| 93 | "HTTP/1.1 200 OK\r\n" |
| 94 | "Connection: close\r\n" |
| 95 | "Strict-Transport-Security: max-age=31536000; includeSubdomains\r\n" |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 96 | "Pragma: no-cache\r\n" |
| 97 | "Cache-Control: no-store, max-age=0\r\n" |
| 98 | "X-Content-Type-Options: nosniff\r\n" |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 99 | "Date: TestTime\r\n" |
| 100 | "Content-Length: 0\r\n\r\n"; |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 101 | EXPECT_EQ(outStr, expected); |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 102 | EXPECT_TRUE(clock.wascalled); |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | } // namespace crow |