| #include "quiche/http2/adapter/nghttp2_adapter.h" |
| |
| #include <memory> |
| |
| #include "quiche/http2/adapter/http2_protocol.h" |
| #include "quiche/http2/adapter/http2_visitor_interface.h" |
| #include "quiche/http2/adapter/mock_http2_visitor.h" |
| #include "quiche/http2/adapter/nghttp2.h" |
| #include "quiche/http2/adapter/nghttp2_test_utils.h" |
| #include "quiche/http2/adapter/oghttp2_util.h" |
| #include "quiche/http2/adapter/test_frame_sequence.h" |
| #include "quiche/http2/adapter/test_utils.h" |
| #include "quiche/common/platform/api/quiche_test.h" |
| |
| namespace http2 { |
| namespace adapter { |
| namespace test { |
| namespace { |
| |
| using ConnectionError = Http2VisitorInterface::ConnectionError; |
| |
| using spdy::SpdyFrameType; |
| using testing::_; |
| |
| enum FrameType { |
| DATA, |
| HEADERS, |
| PRIORITY, |
| RST_STREAM, |
| SETTINGS, |
| PUSH_PROMISE, |
| PING, |
| GOAWAY, |
| WINDOW_UPDATE, |
| CONTINUATION, |
| }; |
| |
| // This send callback assumes |source|'s pointer is a TestDataSource, and |
| // |user_data| is a Http2VisitorInterface. |
| int TestSendCallback(nghttp2_session*, nghttp2_frame* /*frame*/, |
| const uint8_t* framehd, size_t length, |
| nghttp2_data_source* source, void* user_data) { |
| auto* visitor = static_cast<Http2VisitorInterface*>(user_data); |
| // Send the frame header via the visitor. |
| ssize_t result = visitor->OnReadyToSend(ToStringView(framehd, 9)); |
| if (result == 0) { |
| return NGHTTP2_ERR_WOULDBLOCK; |
| } |
| auto* test_source = static_cast<TestDataSource*>(source->ptr); |
| absl::string_view payload = test_source->ReadNext(length); |
| // Send the frame payload via the visitor. |
| visitor->OnReadyToSend(payload); |
| return 0; |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientConstruction) { |
| testing::StrictMock<MockHttp2Visitor> visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| ASSERT_NE(nullptr, adapter); |
| EXPECT_TRUE(adapter->want_read()); |
| EXPECT_FALSE(adapter->want_write()); |
| EXPECT_FALSE(adapter->IsServerSession()); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandlesFrames) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), |
| testing::StrEq(spdy::kHttp2ConnectionHeaderPrefix)); |
| visitor.Clear(); |
| |
| EXPECT_EQ(0, adapter->GetHighestReceivedStreamId()); |
| |
| const std::string initial_frames = TestFrameSequence() |
| .ServerPreface() |
| .Ping(42) |
| .WindowUpdate(0, 1000) |
| .Serialize(); |
| testing::InSequence s; |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, 8, PING, 0)); |
| EXPECT_CALL(visitor, OnPing(42, false)); |
| EXPECT_CALL(visitor, OnFrameHeader(0, 4, WINDOW_UPDATE, 0)); |
| EXPECT_CALL(visitor, OnWindowUpdate(0, 1000)); |
| |
| const int64_t initial_result = adapter->ProcessBytes(initial_frames); |
| EXPECT_EQ(initial_frames.size(), initial_result); |
| |
| EXPECT_EQ(adapter->GetSendWindowSize(), kInitialFlowControlWindowSize + 1000); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(PING, 0, 8, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(PING, 0, 8, 0x1, 0)); |
| |
| result = adapter->Send(); |
| // Some bytes should have been serialized. |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), |
| EqualsFrames({SpdyFrameType::SETTINGS, SpdyFrameType::PING})); |
| visitor.Clear(); |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const std::vector<Header> headers2 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/two"}}); |
| |
| const std::vector<Header> headers3 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/three"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const char* kSentinel3 = "arbitrary pointer 3"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| const int32_t stream_id2 = |
| adapter->SubmitRequest(headers2, nullptr, true, nullptr); |
| ASSERT_GT(stream_id2, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id2; |
| |
| const int32_t stream_id3 = adapter->SubmitRequest( |
| headers3, nullptr, true, const_cast<char*>(kSentinel3)); |
| ASSERT_GT(stream_id3, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id3; |
| |
| const char* kSentinel2 = "arbitrary pointer 2"; |
| adapter->SetStreamUserData(stream_id2, const_cast<char*>(kSentinel2)); |
| adapter->SetStreamUserData(stream_id3, nullptr); |
| |
| // These requests did not include a body, so they do not have corresponding |
| // DataFrameSources. |
| EXPECT_EQ(adapter->sources_size(), 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id2, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id2, _, 0x5, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id3, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id3, _, 0x5, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), |
| EqualsFrames({SpdyFrameType::HEADERS, SpdyFrameType::HEADERS, |
| SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| // All streams are active and have not yet received any data, so the receive |
| // window should be at the initial value. |
| EXPECT_EQ(kInitialFlowControlWindowSize, |
| adapter->GetStreamReceiveWindowSize(stream_id1)); |
| EXPECT_EQ(kInitialFlowControlWindowSize, |
| adapter->GetStreamReceiveWindowSize(stream_id2)); |
| EXPECT_EQ(kInitialFlowControlWindowSize, |
| adapter->GetStreamReceiveWindowSize(stream_id3)); |
| |
| // Upper bound on the flow control receive window should be the initial value. |
| EXPECT_EQ(kInitialFlowControlWindowSize, |
| adapter->GetStreamReceiveWindowLimit(stream_id1)); |
| |
| // Connection has not yet received any data. |
| EXPECT_EQ(kInitialFlowControlWindowSize, adapter->GetReceiveWindowSize()); |
| |
| EXPECT_EQ(0, adapter->GetHighestReceivedStreamId()); |
| |
| EXPECT_EQ(kSentinel1, adapter->GetStreamUserData(stream_id1)); |
| EXPECT_EQ(kSentinel2, adapter->GetStreamUserData(stream_id2)); |
| EXPECT_EQ(nullptr, adapter->GetStreamUserData(stream_id3)); |
| |
| EXPECT_EQ(0, adapter->GetHpackDecoderDynamicTableSize()); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Data(1, "This is the response body.") |
| .RstStream(3, Http2ErrorCode::INTERNAL_ERROR) |
| .GoAway(5, Http2ErrorCode::ENHANCE_YOUR_CALM, "calm down!!") |
| .Serialize(); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "200")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "server", "my-fake-server")); |
| EXPECT_CALL(visitor, |
| OnHeaderForStream(1, "date", "Tue, 6 Apr 2021 12:54:01 GMT")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, 26, DATA, 0)); |
| EXPECT_CALL(visitor, OnBeginDataForStream(1, 26)); |
| EXPECT_CALL(visitor, OnDataForStream(1, "This is the response body.")); |
| EXPECT_CALL(visitor, OnFrameHeader(3, 4, RST_STREAM, 0)); |
| EXPECT_CALL(visitor, OnRstStream(3, Http2ErrorCode::INTERNAL_ERROR)); |
| EXPECT_CALL(visitor, OnCloseStream(3, Http2ErrorCode::INTERNAL_ERROR)) |
| .WillOnce( |
| [&adapter](Http2StreamId stream_id, Http2ErrorCode /*error_code*/) { |
| adapter->RemoveStream(stream_id); |
| return true; |
| }); |
| EXPECT_CALL(visitor, OnFrameHeader(0, 19, GOAWAY, 0)); |
| EXPECT_CALL(visitor, |
| OnGoAway(5, Http2ErrorCode::ENHANCE_YOUR_CALM, "calm down!!")); |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), stream_result); |
| |
| // First stream has received some data. |
| EXPECT_GT(kInitialFlowControlWindowSize, |
| adapter->GetStreamReceiveWindowSize(stream_id1)); |
| // Second stream was closed. |
| EXPECT_EQ(-1, adapter->GetStreamReceiveWindowSize(stream_id2)); |
| // Third stream has not received any data. |
| EXPECT_EQ(kInitialFlowControlWindowSize, |
| adapter->GetStreamReceiveWindowSize(stream_id3)); |
| |
| // Connection window should be the same as the first stream. |
| EXPECT_EQ(adapter->GetReceiveWindowSize(), |
| adapter->GetStreamReceiveWindowSize(stream_id1)); |
| |
| // Upper bound on the flow control receive window should still be the initial |
| // value. |
| EXPECT_EQ(kInitialFlowControlWindowSize, |
| adapter->GetStreamReceiveWindowLimit(stream_id1)); |
| |
| EXPECT_GT(adapter->GetHpackDecoderDynamicTableSize(), 0); |
| |
| // Should be 3, but this method only works for server adapters. |
| EXPECT_EQ(0, adapter->GetHighestReceivedStreamId()); |
| |
| // Even though the client recieved a GOAWAY, streams 1 and 5 are still active. |
| EXPECT_TRUE(adapter->want_read()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, 0, DATA, 1)); |
| EXPECT_CALL(visitor, OnBeginDataForStream(1, 0)); |
| EXPECT_CALL(visitor, OnEndStream(1)); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::HTTP2_NO_ERROR)) |
| .WillOnce( |
| [&adapter](Http2StreamId stream_id, Http2ErrorCode /*error_code*/) { |
| adapter->RemoveStream(stream_id); |
| return true; |
| }); |
| EXPECT_CALL(visitor, OnFrameHeader(5, 4, RST_STREAM, 0)); |
| EXPECT_CALL(visitor, OnRstStream(5, Http2ErrorCode::REFUSED_STREAM)); |
| EXPECT_CALL(visitor, OnCloseStream(5, Http2ErrorCode::REFUSED_STREAM)) |
| .WillOnce( |
| [&adapter](Http2StreamId stream_id, Http2ErrorCode /*error_code*/) { |
| adapter->RemoveStream(stream_id); |
| return true; |
| }); |
| adapter->ProcessBytes(TestFrameSequence() |
| .Data(1, "", true) |
| .RstStream(5, Http2ErrorCode::REFUSED_STREAM) |
| .Serialize()); |
| |
| // Should be 5, but this method only works for server adapters. |
| EXPECT_EQ(0, adapter->GetHighestReceivedStreamId()); |
| |
| // After receiving END_STREAM for 1 and RST_STREAM for 5, the session no |
| // longer expects reads. |
| EXPECT_FALSE(adapter->want_read()); |
| |
| // Client will not have anything else to write. |
| EXPECT_FALSE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), testing::IsEmpty()); |
| } |
| |
| TEST(NgHttp2AdapterTest, QueuingWindowUpdateAffectsWindow) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| EXPECT_EQ(adapter->GetReceiveWindowSize(), kInitialFlowControlWindowSize); |
| adapter->SubmitWindowUpdate(0, 10000); |
| EXPECT_EQ(adapter->GetReceiveWindowSize(), |
| kInitialFlowControlWindowSize + 10000); |
| |
| const std::vector<Header> headers = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| const int32_t stream_id = |
| adapter->SubmitRequest(headers, nullptr, true, nullptr); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(WINDOW_UPDATE, 0, 4, 0x0)); |
| EXPECT_CALL(visitor, OnFrameSent(WINDOW_UPDATE, 0, 4, 0x0, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| |
| EXPECT_EQ(adapter->GetStreamReceiveWindowSize(stream_id), |
| kInitialFlowControlWindowSize); |
| adapter->SubmitWindowUpdate(1, 20000); |
| EXPECT_EQ(adapter->GetStreamReceiveWindowSize(stream_id), |
| kInitialFlowControlWindowSize + 20000); |
| } |
| |
| TEST(NgHttp2AdapterTest, AckOfSettingInitialWindowSizeAffectsWindow) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers, nullptr, true, nullptr); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| |
| const std::string initial_frames = |
| TestFrameSequence().ServerPreface().Serialize(); |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0x0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| int64_t parse_result = adapter->ProcessBytes(initial_frames); |
| EXPECT_EQ(initial_frames.size(), static_cast<size_t>(parse_result)); |
| |
| EXPECT_EQ(adapter->GetStreamReceiveWindowSize(stream_id1), |
| kInitialFlowControlWindowSize); |
| adapter->SubmitSettings({{INITIAL_WINDOW_SIZE, 80000u}}); |
| // No update for the first stream, yet. |
| EXPECT_EQ(adapter->GetStreamReceiveWindowSize(stream_id1), |
| kInitialFlowControlWindowSize); |
| |
| // Ack of server's initial settings. |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| |
| // Outbound SETTINGS containing INITIAL_WINDOW_SIZE. |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 6, 0x0)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 6, 0x0, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| |
| // Still no update, as a SETTINGS ack has not yet been received. |
| EXPECT_EQ(adapter->GetStreamReceiveWindowSize(stream_id1), |
| kInitialFlowControlWindowSize); |
| |
| const std::string settings_ack = |
| TestFrameSequence().SettingsAck().Serialize(); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0x1)); |
| EXPECT_CALL(visitor, OnSettingsAck); |
| |
| parse_result = adapter->ProcessBytes(settings_ack); |
| EXPECT_EQ(settings_ack.size(), static_cast<size_t>(parse_result)); |
| |
| // Stream window has been updated. |
| EXPECT_EQ(adapter->GetStreamReceiveWindowSize(stream_id1), 80000); |
| |
| const std::vector<Header> headers2 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/two"}}); |
| const int32_t stream_id2 = |
| adapter->SubmitRequest(headers, nullptr, true, nullptr); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id2, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id2, _, 0x5, 0)); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| |
| EXPECT_EQ(adapter->GetStreamReceiveWindowSize(stream_id2), 80000); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientRejects100HeadersWithFin) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, {{":status", "100"}}, /*fin=*/false) |
| .Headers(1, {{":status", "100"}}, /*fin=*/true) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "100")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 5)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "100")); |
| EXPECT_CALL(visitor, |
| OnInvalidFrame( |
| 1, Http2VisitorInterface::InvalidFrameError::kHttpMessaging)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), static_cast<size_t>(stream_result)); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, 1, _, 0x0)); |
| EXPECT_CALL(visitor, OnFrameSent(RST_STREAM, 1, _, 0x0, 1)); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::PROTOCOL_ERROR)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS, |
| SpdyFrameType::RST_STREAM})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientRejects100HeadersWithContent) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, {{":status", "100"}}, |
| /*fin=*/false) |
| .Data(1, "We needed the final headers before data, whoops") |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "100")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, DATA, 0)); |
| EXPECT_CALL(visitor, OnBeginDataForStream(1, _)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), static_cast<size_t>(stream_result)); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, 1, _, 0x0)); |
| EXPECT_CALL(visitor, |
| OnFrameSent(RST_STREAM, 1, _, 0x0, |
| static_cast<int>(Http2ErrorCode::PROTOCOL_ERROR))); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::PROTOCOL_ERROR)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS, |
| SpdyFrameType::RST_STREAM})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientRejects100HeadersWithContentLength) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, {{":status", "100"}, {"content-length", "42"}}, |
| /*fin=*/false) |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/true) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "100")); |
| EXPECT_CALL( |
| visitor, |
| OnErrorDebug("Invalid HTTP header field was received: frame type: 1, " |
| "stream: 1, name: [content-length], value: [42]")); |
| EXPECT_CALL( |
| visitor, |
| OnInvalidFrame(1, Http2VisitorInterface::InvalidFrameError::kHttpHeader)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 5)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), static_cast<size_t>(stream_result)); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, 1, _, 0x0)); |
| EXPECT_CALL(visitor, |
| OnFrameSent(RST_STREAM, 1, _, 0x0, |
| static_cast<int>(Http2ErrorCode::PROTOCOL_ERROR))); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::PROTOCOL_ERROR)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS, |
| SpdyFrameType::RST_STREAM})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandles204WithContent) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| const std::vector<Header> headers2 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/two"}}); |
| |
| const int32_t stream_id2 = |
| adapter->SubmitRequest(headers2, nullptr, true, nullptr); |
| ASSERT_GT(stream_id2, stream_id1); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id2, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id2, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, {{":status", "204"}, {"content-length", "2"}}, |
| /*fin=*/false) |
| .Data(1, "hi") |
| .Headers(3, {{":status", "204"}}, /*fin=*/false) |
| .Data(3, "hi") |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "204")); |
| EXPECT_CALL( |
| visitor, |
| OnErrorDebug("Invalid HTTP header field was received: frame type: 1, " |
| "stream: 1, name: [content-length], value: [2]")); |
| EXPECT_CALL( |
| visitor, |
| OnInvalidFrame(1, Http2VisitorInterface::InvalidFrameError::kHttpHeader)); |
| EXPECT_CALL(visitor, OnFrameHeader(3, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(3)); |
| EXPECT_CALL(visitor, OnHeaderForStream(3, ":status", "204")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(3)); |
| EXPECT_CALL(visitor, OnFrameHeader(3, _, DATA, 0)); |
| EXPECT_CALL(visitor, OnBeginDataForStream(3, 2)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), static_cast<size_t>(stream_result)); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, 1, _, 0x0)); |
| EXPECT_CALL(visitor, |
| OnFrameSent(RST_STREAM, 1, _, 0x0, |
| static_cast<int>(Http2ErrorCode::PROTOCOL_ERROR))); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::PROTOCOL_ERROR)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, 3, _, 0x0)); |
| EXPECT_CALL(visitor, |
| OnFrameSent(RST_STREAM, 3, _, 0x0, |
| static_cast<int>(Http2ErrorCode::PROTOCOL_ERROR))); |
| EXPECT_CALL(visitor, OnCloseStream(3, Http2ErrorCode::PROTOCOL_ERROR)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), |
| EqualsFrames({SpdyFrameType::SETTINGS, SpdyFrameType::RST_STREAM, |
| SpdyFrameType::RST_STREAM})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandles304WithContent) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, {{":status", "304"}, {"content-length", "2"}}, |
| /*fin=*/false) |
| .Data(1, "hi") |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "304")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "content-length", "2")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, DATA, 0)); |
| EXPECT_CALL(visitor, OnBeginDataForStream(1, 2)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), static_cast<size_t>(stream_result)); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, 1, _, 0x0)); |
| EXPECT_CALL(visitor, |
| OnFrameSent(RST_STREAM, 1, _, 0x0, |
| static_cast<int>(Http2ErrorCode::PROTOCOL_ERROR))); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::PROTOCOL_ERROR)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS, |
| SpdyFrameType::RST_STREAM})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandles304WithContentLength) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id = |
| adapter->SubmitRequest(headers, nullptr, true, nullptr); |
| ASSERT_GT(stream_id, 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, {{":status", "304"}, {"content-length", "2"}}, |
| /*fin=*/true) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 5)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "304")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "content-length", "2")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnEndStream(1)); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::HTTP2_NO_ERROR)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), static_cast<size_t>(stream_result)); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandlesTrailers) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Data(1, "This is the response body.") |
| .Headers(1, {{"final-status", "A-OK"}}, |
| /*fin=*/true) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "200")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "server", "my-fake-server")); |
| EXPECT_CALL(visitor, |
| OnHeaderForStream(1, "date", "Tue, 6 Apr 2021 12:54:01 GMT")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, 26, DATA, 0)); |
| EXPECT_CALL(visitor, OnBeginDataForStream(1, 26)); |
| EXPECT_CALL(visitor, OnDataForStream(1, "This is the response body.")); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 5)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "final-status", "A-OK")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnEndStream(1)); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::HTTP2_NO_ERROR)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| } |
| |
| class NgHttp2AdapterDataTest : public quiche::test::QuicheTestWithParam<bool> { |
| }; |
| |
| INSTANTIATE_TEST_SUITE_P(BothValues, NgHttp2AdapterDataTest, testing::Bool()); |
| |
| TEST_P(NgHttp2AdapterDataTest, ClientSendsTrailers) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const Http2StreamId kStreamId = 1; |
| const std::string kBody = "This is an example request body."; |
| visitor.AppendPayloadForStream(kStreamId, kBody); |
| visitor.SetEndData(kStreamId, false); |
| auto body1 = std::make_unique<VisitorDataSource>(visitor, kStreamId); |
| // nghttp2 does not require that the data source indicate the end of data |
| // before trailers are enqueued. |
| |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, GetParam() ? nullptr : std::move(body1), false, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| EXPECT_EQ(stream_id1, kStreamId); |
| EXPECT_EQ(adapter->sources_size(), GetParam() ? 0 : 1); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x4)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x4, 0)); |
| EXPECT_CALL(visitor, OnFrameSent(DATA, stream_id1, _, 0x0, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, |
| EqualsFrames({SpdyFrameType::HEADERS, SpdyFrameType::DATA})); |
| visitor.Clear(); |
| |
| const std::vector<Header> trailers1 = |
| ToHeaders({{"extra-info", "Trailers are weird but good?"}}); |
| adapter->SubmitTrailer(stream_id1, trailers1); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| data = visitor.data(); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandlesMetadata) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Metadata(0, "Example connection metadata") |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Metadata(1, "Example stream metadata") |
| .Data(1, "This is the response body.", true) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, kMetadataFrameType, 4)); |
| EXPECT_CALL(visitor, OnBeginMetadataForStream(0, _)); |
| EXPECT_CALL(visitor, OnMetadataForStream(0, _)); |
| EXPECT_CALL(visitor, OnMetadataEndForStream(0)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "200")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "server", "my-fake-server")); |
| EXPECT_CALL(visitor, |
| OnHeaderForStream(1, "date", "Tue, 6 Apr 2021 12:54:01 GMT")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, kMetadataFrameType, 4)); |
| EXPECT_CALL(visitor, OnBeginMetadataForStream(1, _)); |
| EXPECT_CALL(visitor, OnMetadataForStream(1, _)); |
| EXPECT_CALL(visitor, OnMetadataEndForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, 26, DATA, 1)); |
| EXPECT_CALL(visitor, OnBeginDataForStream(1, 26)); |
| EXPECT_CALL(visitor, OnDataForStream(1, "This is the response body.")); |
| EXPECT_CALL(visitor, OnEndStream(1)); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::HTTP2_NO_ERROR)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandlesMetadataWithEmptyPayload) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id, 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Metadata(1, "") |
| .Data(1, "This is the response body.", true) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, _, _)).Times(3); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, kMetadataFrameType, 4)); |
| EXPECT_CALL(visitor, OnBeginMetadataForStream(1, _)); |
| EXPECT_CALL(visitor, OnMetadataEndForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, DATA, 1)); |
| EXPECT_CALL(visitor, OnBeginDataForStream(1, _)); |
| EXPECT_CALL(visitor, OnDataForStream(1, "This is the response body.")); |
| EXPECT_CALL(visitor, OnEndStream(1)); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::HTTP2_NO_ERROR)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), static_cast<size_t>(stream_result)); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandlesMetadataWithError) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Metadata(0, "Example connection metadata") |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Metadata(1, "Example stream metadata") |
| .Data(1, "This is the response body.", true) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, kMetadataFrameType, 4)); |
| EXPECT_CALL(visitor, OnBeginMetadataForStream(0, _)); |
| EXPECT_CALL(visitor, OnMetadataForStream(0, _)); |
| EXPECT_CALL(visitor, OnMetadataEndForStream(0)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "200")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "server", "my-fake-server")); |
| EXPECT_CALL(visitor, |
| OnHeaderForStream(1, "date", "Tue, 6 Apr 2021 12:54:01 GMT")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, kMetadataFrameType, 4)); |
| EXPECT_CALL(visitor, OnBeginMetadataForStream(1, _)); |
| EXPECT_CALL(visitor, OnMetadataForStream(1, _)) |
| .WillOnce(testing::Return(false)); |
| // Remaining frames are not processed due to the error. |
| EXPECT_CALL(visitor, OnConnectionError(ConnectionError::kParseError)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| // The false return from OnMetadataForStream() results in a connection error. |
| EXPECT_EQ(stream_result, NGHTTP2_ERR_CALLBACK_FAILURE); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| EXPECT_TRUE(adapter->want_read()); // Even after an error. Why? |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandlesHpackHeaderTableSetting) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = ToHeaders({ |
| {":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}, |
| {"x-i-do-not-like", "green eggs and ham"}, |
| {"x-i-will-not-eat-them", "here or there, in a box, with a fox"}, |
| {"x-like-them-in-a-house", "no"}, |
| {"x-like-them-with-a-mouse", "no"}, |
| }); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| visitor.Clear(); |
| |
| EXPECT_GT(adapter->GetHpackEncoderDynamicTableSize(), 100); |
| |
| const std::string stream_frames = |
| TestFrameSequence().Settings({{HEADER_TABLE_SIZE, 100u}}).Serialize(); |
| // Server preface (SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 6, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSetting(Http2Setting{HEADER_TABLE_SIZE, 100u})); |
| |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), stream_result); |
| |
| EXPECT_LE(adapter->GetHpackEncoderDynamicTableSize(), 100); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientHandlesInvalidTrailers) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Data(1, "This is the response body.") |
| .Headers(1, {{":bad-status", "9000"}}, |
| /*fin=*/true) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "200")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "server", "my-fake-server")); |
| EXPECT_CALL(visitor, |
| OnHeaderForStream(1, "date", "Tue, 6 Apr 2021 12:54:01 GMT")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, 26, DATA, 0)); |
| EXPECT_CALL(visitor, OnBeginDataForStream(1, 26)); |
| EXPECT_CALL(visitor, OnDataForStream(1, "This is the response body.")); |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 5)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL( |
| visitor, |
| OnErrorDebug("Invalid HTTP header field was received: frame type: 1, " |
| "stream: 1, name: [:bad-status], value: [9000]")); |
| EXPECT_CALL( |
| visitor, |
| OnInvalidFrame(1, Http2VisitorInterface::InvalidFrameError::kHttpHeader)); |
| |
| // Bad status trailer will cause a PROTOCOL_ERROR. The header is never |
| // delivered in an OnHeaderForStream callback. |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, stream_id1, 4, 0x0)); |
| EXPECT_CALL(visitor, OnFrameSent(RST_STREAM, stream_id1, 4, 0x0, 1)); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::PROTOCOL_ERROR)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS, |
| SpdyFrameType::RST_STREAM})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientRstStreamWhileHandlingHeaders) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Data(1, "This is the response body.") |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "200")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "server", "my-fake-server")); |
| EXPECT_CALL(visitor, |
| OnHeaderForStream(1, "date", "Tue, 6 Apr 2021 12:54:01 GMT")) |
| .WillOnce(testing::DoAll( |
| testing::InvokeWithoutArgs([&adapter]() { |
| adapter->SubmitRst(1, Http2ErrorCode::REFUSED_STREAM); |
| }), |
| testing::Return(Http2VisitorInterface::HEADER_RST_STREAM))); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, stream_id1, 4, 0x0)); |
| EXPECT_CALL(visitor, |
| OnFrameSent(RST_STREAM, stream_id1, 4, 0x0, |
| static_cast<int>(Http2ErrorCode::REFUSED_STREAM))); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::REFUSED_STREAM)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS, |
| SpdyFrameType::RST_STREAM})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientConnectionErrorWhileHandlingHeaders) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Data(1, "This is the response body.") |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "200")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "server", "my-fake-server")); |
| EXPECT_CALL(visitor, |
| OnHeaderForStream(1, "date", "Tue, 6 Apr 2021 12:54:01 GMT")) |
| .WillOnce( |
| testing::Return(Http2VisitorInterface::HEADER_CONNECTION_ERROR)); |
| // Translation to nghttp2 treats this error as a general parsing error. |
| EXPECT_CALL(visitor, OnConnectionError(ConnectionError::kParseError)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(-902 /* NGHTTP2_ERR_CALLBACK_FAILURE */, stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientConnectionErrorWhileHandlingHeadersOnly) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/true) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 5)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "200")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "server", "my-fake-server")); |
| EXPECT_CALL(visitor, |
| OnHeaderForStream(1, "date", "Tue, 6 Apr 2021 12:54:01 GMT")) |
| .WillOnce( |
| testing::Return(Http2VisitorInterface::HEADER_CONNECTION_ERROR)); |
| // Translation to nghttp2 treats this error as a general parsing error. |
| EXPECT_CALL(visitor, OnConnectionError(ConnectionError::kParseError)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(-902 /* NGHTTP2_ERR_CALLBACK_FAILURE */, stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientRejectsHeaders) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Data(1, "This is the response body.") |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)) |
| .WillOnce(testing::Return(false)); |
| // Rejecting headers leads to a connection error. |
| EXPECT_CALL(visitor, OnConnectionError(ConnectionError::kParseError)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(NGHTTP2_ERR_CALLBACK_FAILURE, stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientStartsShutdown) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| EXPECT_FALSE(adapter->want_write()); |
| |
| // No-op for a client implementation. |
| adapter->SubmitShutdownNotice(); |
| EXPECT_FALSE(adapter->want_write()); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| |
| EXPECT_EQ(visitor.data(), spdy::kHttp2ConnectionHeaderPrefix); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientReceivesGoAway) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| const std::vector<Header> headers2 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/two"}}); |
| |
| const int32_t stream_id2 = |
| adapter->SubmitRequest(headers2, nullptr, true, nullptr); |
| ASSERT_GT(stream_id2, stream_id1); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id2, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id2, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, |
| EqualsFrames({SpdyFrameType::HEADERS, SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| // Submit a pending WINDOW_UPDATE for a stream that will be closed due to |
| // GOAWAY. The WINDOW_UPDATE should not be sent. |
| adapter->SubmitWindowUpdate(3, 42); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .RstStream(1, Http2ErrorCode::ENHANCE_YOUR_CALM) |
| .GoAway(1, Http2ErrorCode::INTERNAL_ERROR, "indigestion") |
| .WindowUpdate(0, 42) |
| .WindowUpdate(1, 42) |
| .Serialize(); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| EXPECT_CALL(visitor, OnFrameHeader(1, 4, RST_STREAM, 0)); |
| EXPECT_CALL(visitor, OnRstStream(1, Http2ErrorCode::ENHANCE_YOUR_CALM)); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::ENHANCE_YOUR_CALM)); |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, GOAWAY, 0)); |
| EXPECT_CALL(visitor, |
| OnGoAway(1, Http2ErrorCode::INTERNAL_ERROR, "indigestion")); |
| EXPECT_CALL(visitor, OnCloseStream(3, Http2ErrorCode::REFUSED_STREAM)); |
| EXPECT_CALL(visitor, OnFrameHeader(0, 4, WINDOW_UPDATE, 0)); |
| EXPECT_CALL(visitor, OnWindowUpdate(0, 42)); |
| EXPECT_CALL(visitor, OnFrameHeader(1, 4, WINDOW_UPDATE, 0)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| |
| // SETTINGS ack (but only after the enqueue of the seemingly unrelated |
| // WINDOW_UPDATE). The WINDOW_UPDATE is not written. |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientReceivesMultipleGoAways) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string initial_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .GoAway(kMaxStreamId, Http2ErrorCode::INTERNAL_ERROR, "indigestion") |
| .Serialize(); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, GOAWAY, 0)); |
| EXPECT_CALL(visitor, OnGoAway(kMaxStreamId, Http2ErrorCode::INTERNAL_ERROR, |
| "indigestion")); |
| |
| const int64_t initial_result = adapter->ProcessBytes(initial_frames); |
| EXPECT_EQ(initial_frames.size(), static_cast<size_t>(initial_result)); |
| |
| // Submit a WINDOW_UPDATE for the open stream. Because the stream is below the |
| // GOAWAY's last_stream_id, it should be sent. |
| adapter->SubmitWindowUpdate(1, 42); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(WINDOW_UPDATE, 1, 4, 0x0)); |
| EXPECT_CALL(visitor, OnFrameSent(WINDOW_UPDATE, 1, 4, 0x0, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS, |
| SpdyFrameType::WINDOW_UPDATE})); |
| visitor.Clear(); |
| |
| const std::string final_frames = |
| TestFrameSequence() |
| .GoAway(0, Http2ErrorCode::INTERNAL_ERROR, "indigestion") |
| .Serialize(); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, GOAWAY, 0)); |
| EXPECT_CALL(visitor, |
| OnGoAway(0, Http2ErrorCode::INTERNAL_ERROR, "indigestion")); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::REFUSED_STREAM)); |
| |
| const int64_t final_result = adapter->ProcessBytes(final_frames); |
| EXPECT_EQ(final_frames.size(), static_cast<size_t>(final_result)); |
| |
| EXPECT_FALSE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), testing::IsEmpty()); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientReceivesMultipleGoAwaysWithIncreasingStreamId) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| auto source = std::make_unique<TestMetadataSource>(ToHeaderBlock(ToHeaders( |
| {{"query-cost", "is too darn high"}, {"secret-sauce", "hollandaise"}}))); |
| adapter->SubmitMetadata(stream_id1, 16384u, std::move(source)); |
| |
| const std::string frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .GoAway(0, Http2ErrorCode::HTTP2_NO_ERROR, "") |
| .GoAway(0, Http2ErrorCode::ENHANCE_YOUR_CALM, "") |
| .GoAway(1, Http2ErrorCode::INTERNAL_ERROR, "") |
| .Serialize(); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, GOAWAY, 0)); |
| EXPECT_CALL(visitor, OnGoAway(0, Http2ErrorCode::HTTP2_NO_ERROR, "")); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::REFUSED_STREAM)); |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, GOAWAY, 0)); |
| EXPECT_CALL(visitor, OnGoAway(0, Http2ErrorCode::ENHANCE_YOUR_CALM, "")); |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, GOAWAY, 0)); |
| EXPECT_CALL( |
| visitor, |
| OnInvalidFrame(0, Http2VisitorInterface::InvalidFrameError::kProtocol)); |
| |
| const int64_t frames_result = adapter->ProcessBytes(frames); |
| EXPECT_EQ(frames.size(), static_cast<size_t>(frames_result)); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(GOAWAY, 0, _, 0x0)); |
| EXPECT_CALL(visitor, |
| OnFrameSent(GOAWAY, 0, _, 0x0, |
| static_cast<int>(Http2ErrorCode::PROTOCOL_ERROR))); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::GOAWAY})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientReceivesGoAwayWithPendingStreams) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| // Client preface does not appear to include the mandatory SETTINGS frame. |
| EXPECT_THAT(visitor.data(), |
| testing::StrEq(spdy::kHttp2ConnectionHeaderPrefix)); |
| visitor.Clear(); |
| |
| testing::InSequence s; |
| |
| const std::string initial_frames = |
| TestFrameSequence() |
| .ServerPreface({{MAX_CONCURRENT_STREAMS, 1}}) |
| .Serialize(); |
| |
| // Server preface (SETTINGS with MAX_CONCURRENT_STREAMS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 6, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSetting); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| const int64_t initial_result = adapter->ProcessBytes(initial_frames); |
| EXPECT_EQ(initial_frames.size(), initial_result); |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| const std::vector<Header> headers2 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/two"}}); |
| |
| const int32_t stream_id2 = |
| adapter->SubmitRequest(headers2, nullptr, true, nullptr); |
| ASSERT_GT(stream_id2, stream_id1); |
| |
| // The second request should be pending because of |
| // SETTINGS_MAX_CONCURRENT_STREAMS. |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), |
| EqualsFrames({SpdyFrameType::SETTINGS, SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| // Let the client receive a GOAWAY and raise MAX_CONCURRENT_STREAMS. Even |
| // though the GOAWAY last_stream_id is higher than the pending request's |
| // stream ID, pending request should not be sent. |
| const std::string stream_frames = |
| TestFrameSequence() |
| .GoAway(kMaxStreamId, Http2ErrorCode::INTERNAL_ERROR, "indigestion") |
| .Settings({{MAX_CONCURRENT_STREAMS, 42u}}) |
| .Serialize(); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, GOAWAY, 0)); |
| EXPECT_CALL(visitor, OnGoAway(kMaxStreamId, Http2ErrorCode::INTERNAL_ERROR, |
| "indigestion")); |
| EXPECT_CALL(visitor, OnFrameHeader(0, 6, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSetting(Http2Setting{MAX_CONCURRENT_STREAMS, 42u})); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(stream_frames.size(), stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| |
| // Nghttp2 closes the pending stream on the next write attempt. |
| EXPECT_CALL(visitor, OnCloseStream(3, Http2ErrorCode::REFUSED_STREAM)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| visitor.Clear(); |
| |
| // Requests submitted after receiving the GOAWAY should not be sent. |
| const std::vector<Header> headers3 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/three"}}); |
| |
| const int32_t stream_id3 = |
| adapter->SubmitRequest(headers3, nullptr, true, nullptr); |
| ASSERT_GT(stream_id3, stream_id2); |
| |
| // Nghttp2 closes the pending stream on the next write attempt. |
| EXPECT_CALL(visitor, OnCloseStream(5, Http2ErrorCode::REFUSED_STREAM)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), testing::IsEmpty()); |
| EXPECT_FALSE(adapter->want_write()); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientFailsOnGoAway) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}); |
| |
| const char* kSentinel1 = "arbitrary pointer 1"; |
| const int32_t stream_id1 = adapter->SubmitRequest( |
| headers1, nullptr, true, const_cast<char*>(kSentinel1)); |
| ASSERT_GT(stream_id1, 0); |
| QUICHE_LOG(INFO) << "Created stream: " << stream_id1; |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .GoAway(1, Http2ErrorCode::INTERNAL_ERROR, "indigestion") |
| .Data(1, "This is the response body.") |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, ":status", "200")); |
| EXPECT_CALL(visitor, OnHeaderForStream(1, "server", "my-fake-server")); |
| EXPECT_CALL(visitor, |
| OnHeaderForStream(1, "date", "Tue, 6 Apr 2021 12:54:01 GMT")); |
| EXPECT_CALL(visitor, OnEndHeadersForStream(1)); |
| EXPECT_CALL(visitor, OnFrameHeader(0, _, GOAWAY, 0)); |
| EXPECT_CALL(visitor, |
| OnGoAway(1, Http2ErrorCode::INTERNAL_ERROR, "indigestion")) |
| .WillOnce(testing::Return(false)); |
| EXPECT_CALL(visitor, OnConnectionError(ConnectionError::kParseError)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(NGHTTP2_ERR_CALLBACK_FAILURE, stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientRejects101Response) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| const std::vector<Header> headers1 = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}, |
| {"upgrade", "new-protocol"}}); |
| |
| const int32_t stream_id1 = |
| adapter->SubmitRequest(headers1, nullptr, true, nullptr); |
| ASSERT_GT(stream_id1, 0); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id1, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id1, _, 0x5, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| absl::string_view data = visitor.data(); |
| EXPECT_THAT(data, testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| data.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(data, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| const std::string stream_frames = |
| TestFrameSequence() |
| .ServerPreface() |
| .Headers(1, |
| {{":status", "101"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Serialize(); |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(1, _, HEADERS, 4)); |
| EXPECT_CALL(visitor, OnBeginHeadersForStream(1)); |
| EXPECT_CALL( |
| visitor, |
| OnErrorDebug("Invalid HTTP header field was received: frame type: 1, " |
| "stream: 1, name: [:status], value: [101]")); |
| EXPECT_CALL( |
| visitor, |
| OnInvalidFrame(1, Http2VisitorInterface::InvalidFrameError::kHttpHeader)); |
| |
| const int64_t stream_result = adapter->ProcessBytes(stream_frames); |
| EXPECT_EQ(static_cast<int64_t>(stream_frames.size()), stream_result); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, 1, 4, 0x0)); |
| EXPECT_CALL( |
| visitor, |
| OnFrameSent(RST_STREAM, 1, 4, 0x0, |
| static_cast<uint32_t>(Http2ErrorCode::PROTOCOL_ERROR))); |
| EXPECT_CALL(visitor, OnCloseStream(1, Http2ErrorCode::PROTOCOL_ERROR)); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS, |
| SpdyFrameType::RST_STREAM})); |
| } |
| |
| TEST_P(NgHttp2AdapterDataTest, ClientSubmitRequest) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| // Client preface does not appear to include the mandatory SETTINGS frame. |
| EXPECT_THAT(visitor.data(), |
| testing::StrEq(spdy::kHttp2ConnectionHeaderPrefix)); |
| visitor.Clear(); |
| |
| const std::string initial_frames = |
| TestFrameSequence().ServerPreface().Serialize(); |
| testing::InSequence s; |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| const int64_t initial_result = adapter->ProcessBytes(initial_frames); |
| EXPECT_EQ(initial_frames.size(), initial_result); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| visitor.Clear(); |
| |
| EXPECT_EQ(0, adapter->GetHpackEncoderDynamicTableSize()); |
| EXPECT_FALSE(adapter->want_write()); |
| const char* kSentinel = ""; |
| const absl::string_view kBody = "This is an example request body."; |
| visitor.AppendPayloadForStream(1, kBody); |
| visitor.SetEndData(1, true); |
| auto body1 = std::make_unique<VisitorDataSource>(visitor, 1); |
| int stream_id = |
| adapter->SubmitRequest(ToHeaders({{":method", "POST"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}), |
| GetParam() ? nullptr : std::move(body1), false, |
| const_cast<char*>(kSentinel)); |
| ASSERT_EQ(1, stream_id); |
| EXPECT_TRUE(adapter->want_write()); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x4)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x4, 0)); |
| EXPECT_CALL(visitor, OnFrameSent(DATA, stream_id, _, 0x1, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| |
| EXPECT_EQ(kInitialFlowControlWindowSize, |
| adapter->GetStreamReceiveWindowSize(stream_id)); |
| EXPECT_EQ(kInitialFlowControlWindowSize, adapter->GetReceiveWindowSize()); |
| EXPECT_EQ(kInitialFlowControlWindowSize, |
| adapter->GetStreamReceiveWindowLimit(stream_id)); |
| |
| EXPECT_GT(adapter->GetHpackEncoderDynamicTableSize(), 0); |
| |
| // Some data was sent, so the remaining send window size should be less than |
| // the default. |
| EXPECT_LT(adapter->GetStreamSendWindowSize(stream_id), |
| kInitialFlowControlWindowSize); |
| EXPECT_GT(adapter->GetStreamSendWindowSize(stream_id), 0); |
| // Send window for a nonexistent stream is not available. |
| EXPECT_EQ(-1, adapter->GetStreamSendWindowSize(stream_id + 2)); |
| |
| EXPECT_THAT(visitor.data(), |
| EqualsFrames({SpdyFrameType::HEADERS, SpdyFrameType::DATA})); |
| EXPECT_THAT(visitor.data(), testing::HasSubstr(kBody)); |
| visitor.Clear(); |
| EXPECT_FALSE(adapter->want_write()); |
| |
| stream_id = |
| adapter->SubmitRequest(ToHeaders({{":method", "POST"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}), |
| nullptr, true, nullptr); |
| EXPECT_GT(stream_id, 0); |
| EXPECT_TRUE(adapter->want_write()); |
| const char* kSentinel2 = "arbitrary pointer 2"; |
| EXPECT_EQ(nullptr, adapter->GetStreamUserData(stream_id)); |
| adapter->SetStreamUserData(stream_id, const_cast<char*>(kSentinel2)); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x5, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::HEADERS})); |
| |
| EXPECT_EQ(kSentinel2, adapter->GetStreamUserData(stream_id)); |
| |
| // No data was sent (just HEADERS), so the remaining send window size should |
| // still be the default. |
| EXPECT_EQ(adapter->GetStreamSendWindowSize(stream_id), |
| kInitialFlowControlWindowSize); |
| } |
| |
| // This is really a test of the MakeZeroCopyDataFrameSource adapter, but I |
| // wasn't sure where else to put it. |
| TEST(NgHttp2AdapterTest, ClientSubmitRequestWithDataProvider) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| // Client preface does not appear to include the mandatory SETTINGS frame. |
| EXPECT_THAT(visitor.data(), |
| testing::StrEq(spdy::kHttp2ConnectionHeaderPrefix)); |
| visitor.Clear(); |
| |
| const std::string initial_frames = |
| TestFrameSequence().ServerPreface().Serialize(); |
| testing::InSequence s; |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| const int64_t initial_result = adapter->ProcessBytes(initial_frames); |
| EXPECT_EQ(initial_frames.size(), initial_result); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| visitor.Clear(); |
| |
| EXPECT_FALSE(adapter->want_write()); |
| const absl::string_view kBody = "This is an example request body."; |
| // This test will use TestDataSource as the source of the body payload data. |
| TestDataSource body1{kBody}; |
| // The TestDataSource is wrapped in the nghttp2_data_provider data type. |
| nghttp2_data_provider provider = body1.MakeDataProvider(); |
| nghttp2_send_data_callback send_callback = &TestSendCallback; |
| |
| // This call transforms it back into a DataFrameSource, which is compatible |
| // with the Http2Adapter API. |
| std::unique_ptr<DataFrameSource> frame_source = |
| MakeZeroCopyDataFrameSource(provider, &visitor, std::move(send_callback)); |
| int stream_id = |
| adapter->SubmitRequest(ToHeaders({{":method", "POST"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}), |
| std::move(frame_source), false, nullptr); |
| EXPECT_GT(stream_id, 0); |
| EXPECT_TRUE(adapter->want_write()); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x4)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x4, 0)); |
| EXPECT_CALL(visitor, OnFrameSent(DATA, stream_id, _, 0x1, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), |
| EqualsFrames({SpdyFrameType::HEADERS, SpdyFrameType::DATA})); |
| EXPECT_THAT(visitor.data(), testing::HasSubstr(kBody)); |
| EXPECT_FALSE(adapter->want_write()); |
| } |
| |
| // This test verifies how nghttp2 behaves when a data source becomes |
| // read-blocked. |
| TEST(NgHttp2AdapterTest, ClientSubmitRequestWithDataProviderAndReadBlock) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| const absl::string_view kBody = "This is an example request body."; |
| // This test will use TestDataSource as the source of the body payload data. |
| TestDataSource body1{kBody}; |
| body1.set_is_data_available(false); |
| // The TestDataSource is wrapped in the nghttp2_data_provider data type. |
| nghttp2_data_provider provider = body1.MakeDataProvider(); |
| nghttp2_send_data_callback send_callback = &TestSendCallback; |
| |
| // This call transforms it back into a DataFrameSource, which is compatible |
| // with the Http2Adapter API. |
| std::unique_ptr<DataFrameSource> frame_source = |
| MakeZeroCopyDataFrameSource(provider, &visitor, std::move(send_callback)); |
| int stream_id = |
| adapter->SubmitRequest(ToHeaders({{":method", "POST"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}), |
| std::move(frame_source), false, nullptr); |
| EXPECT_GT(stream_id, 0); |
| EXPECT_TRUE(adapter->want_write()); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x4)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x4, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| // Client preface does not appear to include the mandatory SETTINGS frame. |
| absl::string_view serialized = visitor.data(); |
| EXPECT_THAT(serialized, |
| testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| serialized.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(serialized, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| EXPECT_FALSE(adapter->want_write()); |
| |
| // Resume the deferred stream. |
| body1.set_is_data_available(true); |
| EXPECT_TRUE(adapter->ResumeStream(stream_id)); |
| EXPECT_TRUE(adapter->want_write()); |
| |
| EXPECT_CALL(visitor, OnFrameSent(DATA, stream_id, _, 0x1, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::DATA})); |
| EXPECT_FALSE(adapter->want_write()); |
| |
| // Stream data is done, so this stream cannot be resumed. |
| EXPECT_FALSE(adapter->ResumeStream(stream_id)); |
| EXPECT_FALSE(adapter->want_write()); |
| } |
| |
| // This test verifies how nghttp2 behaves when a data source is read block, then |
| // ends with an empty DATA frame. |
| TEST(NgHttp2AdapterTest, ClientSubmitRequestEmptyDataWithFin) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| const absl::string_view kEmptyBody = ""; |
| // This test will use TestDataSource as the source of the body payload data. |
| TestDataSource body1{kEmptyBody}; |
| body1.set_is_data_available(false); |
| // The TestDataSource is wrapped in the nghttp2_data_provider data type. |
| nghttp2_data_provider provider = body1.MakeDataProvider(); |
| nghttp2_send_data_callback send_callback = &TestSendCallback; |
| |
| // This call transforms it back into a DataFrameSource, which is compatible |
| // with the Http2Adapter API. |
| std::unique_ptr<DataFrameSource> frame_source = |
| MakeZeroCopyDataFrameSource(provider, &visitor, std::move(send_callback)); |
| int stream_id = |
| adapter->SubmitRequest(ToHeaders({{":method", "POST"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}), |
| std::move(frame_source), false, nullptr); |
| EXPECT_GT(stream_id, 0); |
| EXPECT_TRUE(adapter->want_write()); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x4)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x4, 0)); |
| |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| // Client preface does not appear to include the mandatory SETTINGS frame. |
| absl::string_view serialized = visitor.data(); |
| EXPECT_THAT(serialized, |
| testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| serialized.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(serialized, EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| EXPECT_FALSE(adapter->want_write()); |
| |
| // Resume the deferred stream. |
| body1.set_is_data_available(true); |
| EXPECT_TRUE(adapter->ResumeStream(stream_id)); |
| EXPECT_TRUE(adapter->want_write()); |
| |
| EXPECT_CALL(visitor, OnFrameSent(DATA, stream_id, 0, 0x1, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::DATA})); |
| EXPECT_FALSE(adapter->want_write()); |
| |
| // Stream data is done, so this stream cannot be resumed. |
| EXPECT_FALSE(adapter->ResumeStream(stream_id)); |
| EXPECT_FALSE(adapter->want_write()); |
| } |
| |
| // This test verifies how nghttp2 behaves when a connection becomes |
| // write-blocked. |
| TEST(NgHttp2AdapterTest, ClientSubmitRequestWithDataProviderAndWriteBlock) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| const absl::string_view kBody = "This is an example request body."; |
| // This test will use TestDataSource as the source of the body payload data. |
| TestDataSource body1{kBody}; |
| // The TestDataSource is wrapped in the nghttp2_data_provider data type. |
| nghttp2_data_provider provider = body1.MakeDataProvider(); |
| nghttp2_send_data_callback send_callback = &TestSendCallback; |
| |
| // This call transforms it back into a DataFrameSource, which is compatible |
| // with the Http2Adapter API. |
| std::unique_ptr<DataFrameSource> frame_source = |
| MakeZeroCopyDataFrameSource(provider, &visitor, std::move(send_callback)); |
| int stream_id = |
| adapter->SubmitRequest(ToHeaders({{":method", "POST"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}), |
| std::move(frame_source), false, nullptr); |
| EXPECT_GT(stream_id, 0); |
| EXPECT_TRUE(adapter->want_write()); |
| |
| visitor.set_is_write_blocked(true); |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), testing::IsEmpty()); |
| EXPECT_TRUE(adapter->want_write()); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x4)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x4, 0)); |
| EXPECT_CALL(visitor, OnFrameSent(DATA, stream_id, _, 0x1, 0)); |
| |
| visitor.set_is_write_blocked(false); |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| |
| // Client preface does not appear to include the mandatory SETTINGS frame. |
| absl::string_view serialized = visitor.data(); |
| EXPECT_THAT(serialized, |
| testing::StartsWith(spdy::kHttp2ConnectionHeaderPrefix)); |
| serialized.remove_prefix(strlen(spdy::kHttp2ConnectionHeaderPrefix)); |
| EXPECT_THAT(serialized, |
| EqualsFrames({SpdyFrameType::HEADERS, SpdyFrameType::DATA})); |
| EXPECT_FALSE(adapter->want_write()); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientReceivesDataOnClosedStream) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| int result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| // Client preface does not appear to include the mandatory SETTINGS frame. |
| EXPECT_THAT(visitor.data(), |
| testing::StrEq(spdy::kHttp2ConnectionHeaderPrefix)); |
| visitor.Clear(); |
| |
| const std::string initial_frames = |
| TestFrameSequence().ServerPreface().Serialize(); |
| testing::InSequence s; |
| |
| // Server preface (empty SETTINGS) |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| const int64_t initial_result = adapter->ProcessBytes(initial_frames); |
| EXPECT_EQ(initial_frames.size(), initial_result); |
| |
| // Client SETTINGS ack |
| EXPECT_TRUE(adapter->want_write()); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, 0, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, 0, 0x1, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::SETTINGS})); |
| visitor.Clear(); |
| |
| // Let the client open a stream with a request. |
| int stream_id = |
| adapter->SubmitRequest(ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/this/is/request/one"}}), |
| nullptr, true, nullptr); |
| EXPECT_GT(stream_id, 0); |
| |
| EXPECT_TRUE(adapter->want_write()); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_id, _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_id, _, 0x5, 0)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::HEADERS})); |
| visitor.Clear(); |
| |
| // Let the client RST_STREAM the stream it opened. |
| adapter->SubmitRst(stream_id, Http2ErrorCode::CANCEL); |
| EXPECT_TRUE(adapter->want_write()); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(RST_STREAM, stream_id, _, 0x0)); |
| EXPECT_CALL(visitor, OnFrameSent(RST_STREAM, stream_id, _, 0x0, |
| static_cast<int>(Http2ErrorCode::CANCEL))); |
| EXPECT_CALL(visitor, OnCloseStream(stream_id, Http2ErrorCode::CANCEL)); |
| |
| result = adapter->Send(); |
| EXPECT_EQ(0, result); |
| EXPECT_THAT(visitor.data(), EqualsFrames({SpdyFrameType::RST_STREAM})); |
| visitor.Clear(); |
| |
| // Let the server send a response on the stream. (It might not have received |
| // the RST_STREAM yet.) |
| const std::string response_frames = |
| TestFrameSequence() |
| .Headers(stream_id, |
| {{":status", "200"}, |
| {"server", "my-fake-server"}, |
| {"date", "Tue, 6 Apr 2021 12:54:01 GMT"}}, |
| /*fin=*/false) |
| .Data(stream_id, "This is the response body.", /*fin=*/true) |
| .Serialize(); |
| |
| // The visitor gets notified about the HEADERS frame but not the DATA frame on |
| // the closed stream. No further processing for either frame occurs. |
| EXPECT_CALL(visitor, OnFrameHeader(stream_id, _, HEADERS, 0x4)); |
| EXPECT_CALL(visitor, OnFrameHeader(stream_id, _, DATA, _)).Times(0); |
| |
| const int64_t response_result = adapter->ProcessBytes(response_frames); |
| EXPECT_EQ(response_frames.size(), response_result); |
| |
| EXPECT_FALSE(adapter->want_write()); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientQueuesRequests) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| testing::InSequence s; |
| |
| adapter->SubmitSettings({}); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x0)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x0, 0)); |
| adapter->Send(); |
| |
| const std::string initial_frames = |
| TestFrameSequence() |
| .ServerPreface({{MAX_CONCURRENT_STREAMS, 2}}) |
| .SettingsAck() |
| .Serialize(); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, 6, SETTINGS, 0x0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSetting(Http2Setting{ |
| Http2KnownSettingsId::MAX_CONCURRENT_STREAMS, 2u})); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| EXPECT_CALL(visitor, OnFrameHeader(0, 0, SETTINGS, 0x1)); |
| EXPECT_CALL(visitor, OnSettingsAck()); |
| |
| adapter->ProcessBytes(initial_frames); |
| |
| const std::vector<Header> headers = |
| ToHeaders({{":method", "GET"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/example/request"}}); |
| std::vector<int32_t> stream_ids; |
| // Start two, which hits the limit. |
| int32_t stream_id = adapter->SubmitRequest(headers, nullptr, true, nullptr); |
| stream_ids.push_back(stream_id); |
| stream_id = adapter->SubmitRequest(headers, nullptr, true, nullptr); |
| stream_ids.push_back(stream_id); |
| // Start two more, which must be queued. |
| stream_id = adapter->SubmitRequest(headers, nullptr, true, nullptr); |
| stream_ids.push_back(stream_id); |
| stream_id = adapter->SubmitRequest(headers, nullptr, true, nullptr); |
| stream_ids.push_back(stream_id); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_ids[0], _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_ids[0], _, 0x5, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_ids[1], _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_ids[1], _, 0x5, 0)); |
| |
| adapter->Send(); |
| |
| const std::string update_streams = |
| TestFrameSequence().Settings({{MAX_CONCURRENT_STREAMS, 5}}).Serialize(); |
| |
| EXPECT_CALL(visitor, OnFrameHeader(0, 6, SETTINGS, 0x0)); |
| EXPECT_CALL(visitor, OnSettingsStart()); |
| EXPECT_CALL(visitor, OnSetting(Http2Setting{ |
| Http2KnownSettingsId::MAX_CONCURRENT_STREAMS, 5u})); |
| EXPECT_CALL(visitor, OnSettingsEnd()); |
| |
| adapter->ProcessBytes(update_streams); |
| |
| stream_id = adapter->SubmitRequest(headers, nullptr, true, nullptr); |
| stream_ids.push_back(stream_id); |
| |
| EXPECT_CALL(visitor, OnBeforeFrameSent(SETTINGS, 0, _, 0x1)); |
| EXPECT_CALL(visitor, OnFrameSent(SETTINGS, 0, _, 0x1, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_ids[2], _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_ids[2], _, 0x5, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_ids[3], _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_ids[3], _, 0x5, 0)); |
| EXPECT_CALL(visitor, OnBeforeFrameSent(HEADERS, stream_ids[4], _, 0x5)); |
| EXPECT_CALL(visitor, OnFrameSent(HEADERS, stream_ids[4], _, 0x5, 0)); |
| // Header frames should all have been sent in order, regardless of any |
| // queuing. |
| |
| adapter->Send(); |
| } |
| |
| TEST(NgHttp2AdapterTest, ClientAcceptsHeadResponseWithContentLength) { |
| TestVisitor visitor; |
| auto adapter = NgHttp2Adapter::CreateClientAdapter(visitor); |
| |
| const std::vector<Header> headers = ToHeaders({{":method", "HEAD"}, |
| {":scheme", "http"}, |
| {":authority", "example.com"}, |
| {":path", "/"}}); |
| const int32_t stream_id = |
| adapter->SubmitRequest(headers, nullptr, |