From 414f5801672f1b9d8ec5e0469db4fa539e169453 Mon Sep 17 00:00:00 2001 From: Yoichi Osato Date: Wed, 4 Sep 2019 10:43:58 +0000 Subject: [PATCH] [WebSocket] Add test for WebSocketChannel::ReadFrames() on closing. This test adds procedure when we receive close frame while there are pending frames. Bug: 865001 Change-Id: I0ac7b406004139ab48b67985cccff8cbf3d70313 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1775685 Reviewed-by: Yoichi Osato Reviewed-by: Adam Rice Reviewed-by: Yutaka Hirano Commit-Queue: Yoichi Osato Cr-Commit-Position: refs/heads/master@{#693098} --- net/websockets/websocket_channel.cc | 1 - net/websockets/websocket_channel_test.cc | 35 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/net/websockets/websocket_channel.cc b/net/websockets/websocket_channel.cc index 2119f3c1be832c..204f4a1986aa8d 100644 --- a/net/websockets/websocket_channel.cc +++ b/net/websockets/websocket_channel.cc @@ -585,7 +585,6 @@ ChannelState WebSocketChannel::ReadFrames() { return CHANNEL_ALIVE; } - // TODO(yoichio): Add test for this case. if (!InClosingState() && has_received_close_frame_) { DCHECK(!event_interface_->HasPendingDataFrames()); // We've been waiting for the client to consume the frames before diff --git a/net/websockets/websocket_channel_test.cc b/net/websockets/websocket_channel_test.cc index 67aa5c8dde9958..748306c2a24961 100644 --- a/net/websockets/websocket_channel_test.cc +++ b/net/websockets/websocket_channel_test.cc @@ -1078,6 +1078,41 @@ TEST_F(WebSocketChannelEventInterfaceTest, CloseAfterHandshake) { CreateChannelAndConnectSuccessfully(); } +// Do not close until browser has sent all pending frames. +TEST_F(WebSocketChannelEventInterfaceTest, ShouldCloseWhileNoDataFrames) { + auto stream = std::make_unique(); + static const InitFrame frames[] = { + {FINAL_FRAME, WebSocketFrameHeader::kOpCodeClose, NOT_MASKED, + CLOSE_DATA(SERVER_ERROR, "Internal Server Error")}}; + stream->PrepareReadFrames(ReadableFakeWebSocketStream::SYNC, OK, frames); + stream->PrepareReadFramesError(ReadableFakeWebSocketStream::SYNC, + ERR_CONNECTION_CLOSED); + set_stream(std::move(stream)); + Checkpoint checkpoint; + { + InSequence s; + EXPECT_CALL(*event_interface_, OnAddChannelResponse(_, _)); + EXPECT_CALL(*event_interface_, OnSendFlowControlQuotaAdded(_)); + EXPECT_CALL(*event_interface_, HasPendingDataFrames()) + .WillOnce(Return(false)) + .WillOnce(Return(true)) + .WillOnce(Return(true)); + EXPECT_CALL(checkpoint, Call(1)); +#if DCHECK_IS_ON() + EXPECT_CALL(*event_interface_, HasPendingDataFrames()) + .WillOnce(Return(false)); +#endif + EXPECT_CALL(*event_interface_, OnClosingHandshake()); + EXPECT_CALL(*event_interface_, + OnDropChannel(true, kWebSocketErrorInternalServerError, + "Internal Server Error")); + } + + CreateChannelAndConnectSuccessfully(); + checkpoint.Call(1); + ASSERT_EQ(CHANNEL_DELETED, channel_->ReadFrames()); +} + // A remote server could close the connection immediately after sending the // handshake response (most likely a bug in the server). TEST_F(WebSocketChannelEventInterfaceTest, ConnectionCloseAfterHandshake) {