Skip to content

Commit

Permalink
[WebSocket] Add test for WebSocketChannel::ReadFrames() on closing.
Browse files Browse the repository at this point in the history
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 <yoichio@chromium.org>
Reviewed-by: Adam Rice <ricea@chromium.org>
Reviewed-by: Yutaka Hirano <yhirano@chromium.org>
Commit-Queue: Yoichi Osato <yoichio@chromium.org>
Cr-Commit-Position: refs/heads/master@{#693098}
  • Loading branch information
Yoichi Osato authored and Commit Bot committed Sep 4, 2019
1 parent 1869f50 commit 414f580
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
1 change: 0 additions & 1 deletion net/websockets/websocket_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions net/websockets/websocket_channel_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReadableFakeWebSocketStream>();
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) {
Expand Down

0 comments on commit 414f580

Please sign in to comment.