OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/websockets/websocket_channel.h" | 5 #include "net/websockets/websocket_channel.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <string.h> | 9 #include <string.h> |
10 | 10 |
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 // |async| is SYNC, the response will be returned synchronously. |error| is | 464 // |async| is SYNC, the response will be returned synchronously. |error| is |
465 // returned directly from ReadFrames() in the synchronous case, or passed to | 465 // returned directly from ReadFrames() in the synchronous case, or passed to |
466 // the callback in the asynchronous case. |frames| will be converted to a | 466 // the callback in the asynchronous case. |frames| will be converted to a |
467 // std::vector<std::unique_ptr<WebSocketFrame>> and copied to the pointer that | 467 // std::vector<std::unique_ptr<WebSocketFrame>> and copied to the pointer that |
468 // was | 468 // was |
469 // passed to ReadFrames(). | 469 // passed to ReadFrames(). |
470 template <size_t N> | 470 template <size_t N> |
471 void PrepareReadFrames(IsSync async, | 471 void PrepareReadFrames(IsSync async, |
472 int error, | 472 int error, |
473 const InitFrame (&frames)[N]) { | 473 const InitFrame (&frames)[N]) { |
474 responses_.push_back(base::WrapUnique( | 474 responses_.push_back( |
475 new Response(async, error, CreateFrameVector(frames)))); | 475 base::MakeUnique<Response>(async, error, CreateFrameVector(frames))); |
476 } | 476 } |
477 | 477 |
478 // An alternate version of PrepareReadFrames for when we need to construct | 478 // An alternate version of PrepareReadFrames for when we need to construct |
479 // the frames manually. | 479 // the frames manually. |
480 void PrepareRawReadFrames( | 480 void PrepareRawReadFrames( |
481 IsSync async, | 481 IsSync async, |
482 int error, | 482 int error, |
483 std::vector<std::unique_ptr<WebSocketFrame>> frames) { | 483 std::vector<std::unique_ptr<WebSocketFrame>> frames) { |
484 responses_.push_back( | 484 responses_.push_back( |
485 base::WrapUnique(new Response(async, error, std::move(frames)))); | 485 base::MakeUnique<Response>(async, error, std::move(frames))); |
486 } | 486 } |
487 | 487 |
488 // Prepares a fake error response (ie. there is no data). | 488 // Prepares a fake error response (ie. there is no data). |
489 void PrepareReadFramesError(IsSync async, int error) { | 489 void PrepareReadFramesError(IsSync async, int error) { |
490 responses_.push_back(base::WrapUnique(new Response( | 490 responses_.push_back(base::MakeUnique<Response>( |
491 async, error, std::vector<std::unique_ptr<WebSocketFrame>>()))); | 491 async, error, std::vector<std::unique_ptr<WebSocketFrame>>())); |
492 } | 492 } |
493 | 493 |
494 int ReadFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, | 494 int ReadFrames(std::vector<std::unique_ptr<WebSocketFrame>>* frames, |
495 const CompletionCallback& callback) override { | 495 const CompletionCallback& callback) override { |
496 CHECK(!read_frames_pending_); | 496 CHECK(!read_frames_pending_); |
497 if (index_ >= responses_.size()) | 497 if (index_ >= responses_.size()) |
498 return ERR_IO_PENDING; | 498 return ERR_IO_PENDING; |
499 if (responses_[index_]->async == ASYNC) { | 499 if (responses_[index_]->async == ASYNC) { |
500 read_frames_pending_ = true; | 500 read_frames_pending_ = true; |
501 base::ThreadTaskRunnerHandle::Get()->PostTask( | 501 base::ThreadTaskRunnerHandle::Get()->PostTask( |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 } | 920 } |
921 | 921 |
922 private: | 922 private: |
923 // A pointer to the test fixture. Owned by the test harness; this object will | 923 // A pointer to the test fixture. Owned by the test harness; this object will |
924 // be deleted before it is. | 924 // be deleted before it is. |
925 WebSocketChannelDeletingTest* fixture_; | 925 WebSocketChannelDeletingTest* fixture_; |
926 }; | 926 }; |
927 | 927 |
928 std::unique_ptr<WebSocketEventInterface> | 928 std::unique_ptr<WebSocketEventInterface> |
929 WebSocketChannelDeletingTest::CreateEventInterface() { | 929 WebSocketChannelDeletingTest::CreateEventInterface() { |
930 return base::WrapUnique(new ChannelDeletingFakeWebSocketEventInterface(this)); | 930 return base::MakeUnique<ChannelDeletingFakeWebSocketEventInterface>(this); |
931 } | 931 } |
932 | 932 |
933 // Base class for tests which verify that EventInterface methods are called | 933 // Base class for tests which verify that EventInterface methods are called |
934 // appropriately. | 934 // appropriately. |
935 class WebSocketChannelEventInterfaceTest : public WebSocketChannelTest { | 935 class WebSocketChannelEventInterfaceTest : public WebSocketChannelTest { |
936 protected: | 936 protected: |
937 WebSocketChannelEventInterfaceTest() | 937 WebSocketChannelEventInterfaceTest() |
938 : event_interface_(new StrictMock<MockWebSocketEventInterface>) { | 938 : event_interface_(new StrictMock<MockWebSocketEventInterface>) { |
939 DefaultValue<ChannelState>::Set(CHANNEL_ALIVE); | 939 DefaultValue<ChannelState>::Set(CHANNEL_ALIVE); |
940 ON_CALL(*event_interface_, OnDropChannel(_, _, _)) | 940 ON_CALL(*event_interface_, OnDropChannel(_, _, _)) |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1189 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; | 1189 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; |
1190 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); | 1190 stream->PrepareReadFrames(ReadableFakeWebSocketStream::ASYNC, OK, frames); |
1191 set_stream(std::move(stream)); | 1191 set_stream(std::move(stream)); |
1192 deleting_ = EVENT_ON_FINISH_OPENING_HANDSHAKE; | 1192 deleting_ = EVENT_ON_FINISH_OPENING_HANDSHAKE; |
1193 | 1193 |
1194 CreateChannelAndConnectSuccessfully(); | 1194 CreateChannelAndConnectSuccessfully(); |
1195 ASSERT_TRUE(channel_); | 1195 ASSERT_TRUE(channel_); |
1196 scoped_refptr<HttpResponseHeaders> response_headers( | 1196 scoped_refptr<HttpResponseHeaders> response_headers( |
1197 new HttpResponseHeaders("")); | 1197 new HttpResponseHeaders("")); |
1198 channel_->OnFinishOpeningHandshake( | 1198 channel_->OnFinishOpeningHandshake( |
1199 base::WrapUnique(new WebSocketHandshakeResponseInfo( | 1199 base::MakeUnique<WebSocketHandshakeResponseInfo>( |
1200 GURL("http://www.example.com/"), 200, "OK", response_headers, | 1200 GURL("http://www.example.com/"), 200, "OK", response_headers, |
1201 base::Time()))); | 1201 base::Time())); |
1202 base::RunLoop().RunUntilIdle(); | 1202 base::RunLoop().RunUntilIdle(); |
1203 EXPECT_EQ(nullptr, channel_.get()); | 1203 EXPECT_EQ(nullptr, channel_.get()); |
1204 } | 1204 } |
1205 | 1205 |
1206 TEST_F(WebSocketChannelDeletingTest, FailChannelInSendFrame) { | 1206 TEST_F(WebSocketChannelDeletingTest, FailChannelInSendFrame) { |
1207 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); | 1207 set_stream(base::WrapUnique(new WriteableFakeWebSocketStream)); |
1208 deleting_ = EVENT_ON_FAIL_CHANNEL; | 1208 deleting_ = EVENT_ON_FAIL_CHANNEL; |
1209 CreateChannelAndConnectSuccessfully(); | 1209 CreateChannelAndConnectSuccessfully(); |
1210 ASSERT_TRUE(channel_); | 1210 ASSERT_TRUE(channel_); |
1211 channel_->SendFrame(true, | 1211 channel_->SendFrame(true, |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1360 CreateChannelAndConnect(); | 1360 CreateChannelAndConnect(); |
1361 } | 1361 } |
1362 | 1362 |
1363 TEST_F(WebSocketChannelEventInterfaceTest, ProtocolPassed) { | 1363 TEST_F(WebSocketChannelEventInterfaceTest, ProtocolPassed) { |
1364 EXPECT_CALL(*event_interface_, OnAddChannelResponse("Bob", "")); | 1364 EXPECT_CALL(*event_interface_, OnAddChannelResponse("Bob", "")); |
1365 EXPECT_CALL(*event_interface_, OnFlowControl(_)); | 1365 EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
1366 | 1366 |
1367 CreateChannelAndConnect(); | 1367 CreateChannelAndConnect(); |
1368 | 1368 |
1369 connect_data_.argument_saver.connect_delegate->OnSuccess( | 1369 connect_data_.argument_saver.connect_delegate->OnSuccess( |
1370 base::WrapUnique(new FakeWebSocketStream("Bob", ""))); | 1370 base::MakeUnique<FakeWebSocketStream>("Bob", "")); |
1371 } | 1371 } |
1372 | 1372 |
1373 TEST_F(WebSocketChannelEventInterfaceTest, ExtensionsPassed) { | 1373 TEST_F(WebSocketChannelEventInterfaceTest, ExtensionsPassed) { |
1374 EXPECT_CALL(*event_interface_, | 1374 EXPECT_CALL(*event_interface_, |
1375 OnAddChannelResponse("", "extension1, extension2")); | 1375 OnAddChannelResponse("", "extension1, extension2")); |
1376 EXPECT_CALL(*event_interface_, OnFlowControl(_)); | 1376 EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
1377 | 1377 |
1378 CreateChannelAndConnect(); | 1378 CreateChannelAndConnect(); |
1379 | 1379 |
1380 connect_data_.argument_saver.connect_delegate->OnSuccess( | 1380 connect_data_.argument_saver.connect_delegate->OnSuccess( |
1381 base::WrapUnique(new FakeWebSocketStream("", "extension1, extension2"))); | 1381 base::MakeUnique<FakeWebSocketStream>("", "extension1, extension2")); |
1382 } | 1382 } |
1383 | 1383 |
1384 // The first frames from the server can arrive together with the handshake, in | 1384 // The first frames from the server can arrive together with the handshake, in |
1385 // which case they will be available as soon as ReadFrames() is called the first | 1385 // which case they will be available as soon as ReadFrames() is called the first |
1386 // time. | 1386 // time. |
1387 TEST_F(WebSocketChannelEventInterfaceTest, DataLeftFromHandshake) { | 1387 TEST_F(WebSocketChannelEventInterfaceTest, DataLeftFromHandshake) { |
1388 std::unique_ptr<ReadableFakeWebSocketStream> stream( | 1388 std::unique_ptr<ReadableFakeWebSocketStream> stream( |
1389 new ReadableFakeWebSocketStream); | 1389 new ReadableFakeWebSocketStream); |
1390 static const InitFrame frames[] = { | 1390 static const InitFrame frames[] = { |
1391 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; | 1391 {FINAL_FRAME, WebSocketFrameHeader::kOpCodeText, NOT_MASKED, "HELLO"}}; |
(...skipping 2121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3513 | 3513 |
3514 channel_->SendFrame( | 3514 channel_->SendFrame( |
3515 true, WebSocketFrameHeader::kOpCodeText, | 3515 true, WebSocketFrameHeader::kOpCodeText, |
3516 std::vector<char>(static_cast<size_t>(kMessageSize), 'a')); | 3516 std::vector<char>(static_cast<size_t>(kMessageSize), 'a')); |
3517 int new_send_quota = channel_->current_send_quota(); | 3517 int new_send_quota = channel_->current_send_quota(); |
3518 EXPECT_EQ(kMessageSize, initial_send_quota - new_send_quota); | 3518 EXPECT_EQ(kMessageSize, initial_send_quota - new_send_quota); |
3519 } | 3519 } |
3520 | 3520 |
3521 } // namespace | 3521 } // namespace |
3522 } // namespace net | 3522 } // namespace net |
OLD | NEW |