| 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 | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 4 | #include "http/http2_connection.hpp" | 
|  | 5 | #include "http/http_request.hpp" | 
|  | 6 | #include "http/http_response.hpp" | 
| Ed Tanous | ebe4c57 | 2025-02-08 14:29:53 -0800 | [diff] [blame] | 7 | #include "http_connect_types.hpp" | 
| Ed Tanous | 5b90429 | 2024-04-16 11:10:17 -0700 | [diff] [blame] | 8 | #include "nghttp2_adapters.hpp" | 
| Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 9 | #include "test_stream.hpp" | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 10 |  | 
| Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 11 | #include <nghttp2/nghttp2.h> | 
|  | 12 | #include <unistd.h> | 
|  | 13 |  | 
|  | 14 | #include <boost/asio/buffer.hpp> | 
| Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 15 | #include <boost/asio/io_context.hpp> | 
| Ed Tanous | ebe4c57 | 2025-02-08 14:29:53 -0800 | [diff] [blame] | 16 | #include <boost/asio/ssl/context.hpp> | 
|  | 17 | #include <boost/asio/ssl/stream.hpp> | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 18 | #include <boost/asio/steady_timer.hpp> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 19 | #include <boost/asio/write.hpp> | 
| Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 20 | #include <boost/beast/http/field.hpp> | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 21 |  | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 22 | #include <array> | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 23 | #include <bit> | 
| Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 24 | #include <cstddef> | 
|  | 25 | #include <cstdint> | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 26 | #include <functional> | 
|  | 27 | #include <memory> | 
|  | 28 | #include <string> | 
|  | 29 | #include <string_view> | 
|  | 30 | #include <utility> | 
|  | 31 | #include <vector> | 
|  | 32 |  | 
|  | 33 | #include "gmock/gmock.h" | 
|  | 34 | #include "gtest/gtest.h" | 
|  | 35 | namespace crow | 
|  | 36 | { | 
|  | 37 |  | 
|  | 38 | namespace | 
|  | 39 | { | 
|  | 40 |  | 
|  | 41 | using ::testing::Pair; | 
|  | 42 | using ::testing::UnorderedElementsAre; | 
|  | 43 |  | 
|  | 44 | struct FakeHandler | 
|  | 45 | { | 
|  | 46 | bool called = false; | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 47 | void handle(const std::shared_ptr<Request>& req, | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 48 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
|  | 49 | { | 
|  | 50 | called = true; | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 51 | EXPECT_EQ(req->url().buffer(), "/redfish/v1/"); | 
|  | 52 | EXPECT_EQ(req->methodString(), "GET"); | 
|  | 53 | EXPECT_EQ(req->getHeaderValue(boost::beast::http::field::user_agent), | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 54 | "curl/8.5.0"); | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 55 | EXPECT_EQ(req->getHeaderValue(boost::beast::http::field::accept), | 
|  | 56 | "*/*"); | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 57 | asyncResp->res.write("StringOutput"); | 
|  | 58 | } | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | std::string getDateStr() | 
|  | 62 | { | 
|  | 63 | return "TestTime"; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | void unpackHeaders(std::string_view dataField, | 
|  | 67 | std::vector<std::pair<std::string, std::string>>& headers) | 
|  | 68 | { | 
| Ed Tanous | a93163a | 2024-03-25 11:20:18 -0700 | [diff] [blame] | 69 | nghttp2_hd_inflater_ex inflater; | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 70 |  | 
|  | 71 | while (!dataField.empty()) | 
|  | 72 | { | 
|  | 73 | nghttp2_nv nv; | 
|  | 74 | int inflateFlags = 0; | 
|  | 75 | const uint8_t* data = std::bit_cast<const uint8_t*>(dataField.data()); | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 76 | ssize_t parsed = | 
|  | 77 | inflater.hd2(&nv, &inflateFlags, data, dataField.size(), 1); | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 78 |  | 
|  | 79 | ASSERT_GT(parsed, 0); | 
|  | 80 | dataField.remove_prefix(static_cast<size_t>(parsed)); | 
|  | 81 | if ((inflateFlags & NGHTTP2_HD_INFLATE_EMIT) > 0) | 
|  | 82 | { | 
|  | 83 | const char* namePtr = std::bit_cast<const char*>(nv.name); | 
|  | 84 | std::string key(namePtr, nv.namelen); | 
|  | 85 | const char* valPtr = std::bit_cast<const char*>(nv.value); | 
|  | 86 | std::string value(valPtr, nv.valuelen); | 
|  | 87 | headers.emplace_back(key, value); | 
|  | 88 | } | 
|  | 89 | if ((inflateFlags & NGHTTP2_HD_INFLATE_FINAL) > 0) | 
|  | 90 | { | 
|  | 91 | EXPECT_EQ(inflater.endHeaders(), 0); | 
|  | 92 | break; | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 | EXPECT_TRUE(dataField.empty()); | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | TEST(http_connection, RequestPropogates) | 
|  | 99 | { | 
|  | 100 | using namespace std::literals; | 
|  | 101 | boost::asio::io_context io; | 
| Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 102 | TestStream stream(io); | 
|  | 103 | TestStream out(io); | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 104 | stream.connect(out); | 
|  | 105 | // This is a binary pre-encrypted stream captured from curl for a request to | 
|  | 106 | // curl https://localhost:18080/redfish/v1/ | 
|  | 107 | std::string_view toSend = | 
|  | 108 | // Hello | 
|  | 109 | "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n" | 
|  | 110 | // 18 byte settings frame | 
|  | 111 | "\x00\x00\x12\x04\x00\x00\x00\x00\x00" | 
|  | 112 | // Settings | 
|  | 113 | "\x00\x03\x00\x00\x00\x64\x00\x04\x00\xa0\x00\x00\x00\x02\x00\x00\x00\x00" | 
|  | 114 | // Window update frame | 
|  | 115 | "\x00\x00\x04\x08\x00\x00\x00\x00\x00" | 
|  | 116 | // Window update | 
|  | 117 | "\x3e\x7f\x00\x01" | 
|  | 118 | // Header frame END_STREAM, END_HEADERS set | 
|  | 119 | "\x00\x00\x29\x01\x05\x00\x00\x00" | 
|  | 120 | // Header payload | 
|  | 121 | "\x01\x82\x87\x41\x8b\xa0\xe4\x1d\x13\x9d\x09\xb8\x17\x80\xf0\x3f" | 
|  | 122 | "\x04\x89\x62\xc2\xc9\x29\x91\x3b\x1d\xc2\xc7\x7a\x88\x25\xb6\x50" | 
|  | 123 | "\xc3\xcb\xb6\xb8\x3f\x53\x03\x2a\x2f\x2a"sv; | 
|  | 124 |  | 
|  | 125 | boost::asio::write(out, boost::asio::buffer(toSend)); | 
|  | 126 |  | 
|  | 127 | FakeHandler handler; | 
|  | 128 | boost::asio::steady_timer timer(io); | 
|  | 129 | std::function<std::string()> date(getDateStr); | 
| Ed Tanous | ebe4c57 | 2025-02-08 14:29:53 -0800 | [diff] [blame] | 130 | boost::asio::ssl::context sslCtx(boost::asio::ssl::context::tls_server); | 
| Ed Tanous | 796ba93 | 2020-08-02 04:29:21 +0000 | [diff] [blame] | 131 | auto conn = std::make_shared<HTTP2Connection<TestStream, FakeHandler>>( | 
| Ed Tanous | ebe4c57 | 2025-02-08 14:29:53 -0800 | [diff] [blame] | 132 | boost::asio::ssl::stream<TestStream>(std::move(stream), sslCtx), | 
| Ed Tanous | 2e3cdf8 | 2025-08-01 09:49:35 -0700 | [diff] [blame] | 133 | &handler, date, HttpType::HTTP, nullptr); | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 134 | conn->start(); | 
|  | 135 |  | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 136 | std::array<std::string_view, 5> expectedPrefix = { | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 137 | // Settings frame size 13 | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 138 | "\x00\x00\x0c\x04\x00\x00\x00\x00\x00"sv, | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 139 | // 4 max concurrent streams | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 140 | "\x00\x03\x00\x00\x00\x04"sv, | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 141 | // Enable push = false | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 142 | "\x00\x02\x00\x00\x00\x00"sv, | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 143 | // Settings ACK from server to client | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 144 | "\x00\x00\x00\x04\x01\x00\x00\x00\x00"sv, | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 145 |  | 
| Ed Tanous | c056aa7 | 2024-04-14 09:57:09 -0700 | [diff] [blame] | 146 | // Start Headers frame stream 1, size 0x005f | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 147 | "\x00\x00\x5f\x01\x04\x00\x00\x00\x01"sv, | 
|  | 148 | }; | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 149 |  | 
|  | 150 | std::string_view expectedPostfix = | 
|  | 151 | // Data Frame, Length 12, Stream 1, End Stream flag set | 
|  | 152 | "\x00\x00\x0c\x00\x01\x00\x00\x00\x01" | 
|  | 153 | // The body expected | 
|  | 154 | "StringOutput"sv; | 
|  | 155 |  | 
|  | 156 | std::string_view outStr; | 
| Ed Tanous | c056aa7 | 2024-04-14 09:57:09 -0700 | [diff] [blame] | 157 | constexpr size_t headerSize = 0x05f; | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 158 |  | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 159 | size_t expectedPrefixSize = 0; | 
|  | 160 | for (const auto prefix : expectedPrefix) | 
|  | 161 | { | 
|  | 162 | expectedPrefixSize += prefix.size(); | 
|  | 163 | } | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 164 | // Run until we receive the expected amount of data | 
|  | 165 | while (outStr.size() < | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 166 | expectedPrefixSize + headerSize + expectedPostfix.size()) | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 167 | { | 
|  | 168 | io.run_one(); | 
|  | 169 | outStr = out.str(); | 
|  | 170 | } | 
|  | 171 | EXPECT_TRUE(handler.called); | 
|  | 172 |  | 
|  | 173 | // check the stream output against expected | 
| Ed Tanous | 634a5e0 | 2025-09-26 12:05:08 -0700 | [diff] [blame] | 174 | for (const auto& prefix : expectedPrefix) | 
|  | 175 | { | 
|  | 176 | EXPECT_EQ(outStr.substr(0, prefix.size()), prefix); | 
|  | 177 | outStr.remove_prefix(prefix.size()); | 
|  | 178 | } | 
|  | 179 |  | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 180 | std::vector<std::pair<std::string, std::string>> headers; | 
|  | 181 | unpackHeaders(outStr.substr(0, headerSize), headers); | 
|  | 182 | outStr.remove_prefix(headerSize); | 
|  | 183 |  | 
| Ed Tanous | c056aa7 | 2024-04-14 09:57:09 -0700 | [diff] [blame] | 184 | EXPECT_THAT(headers, | 
|  | 185 | UnorderedElementsAre( | 
|  | 186 | Pair(":status", "200"), Pair("content-length", "12"), | 
|  | 187 | Pair("strict-transport-security", | 
|  | 188 | "max-age=31536000; includeSubdomains"), | 
|  | 189 | Pair("cache-control", "no-store, max-age=0"), | 
|  | 190 | Pair("x-content-type-options", "nosniff"), | 
|  | 191 | Pair("pragma", "no-cache"), Pair("date", "TestTime"))); | 
| Ed Tanous | 52dd693 | 2024-01-29 08:34:59 -0800 | [diff] [blame] | 192 |  | 
|  | 193 | EXPECT_EQ(outStr, expectedPostfix); | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | } // namespace | 
|  | 197 | } // namespace crow |