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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <iostream> | 9 #include <iostream> |
10 #include <string> | 10 #include <string> |
(...skipping 631 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 const CompletionCallback& callback)); | 642 const CompletionCallback& callback)); |
643 MOCK_METHOD2(WriteFrames, | 643 MOCK_METHOD2(WriteFrames, |
644 int(ScopedVector<WebSocketFrame>* frames, | 644 int(ScopedVector<WebSocketFrame>* frames, |
645 const CompletionCallback& callback)); | 645 const CompletionCallback& callback)); |
646 MOCK_METHOD0(Close, void()); | 646 MOCK_METHOD0(Close, void()); |
647 MOCK_CONST_METHOD0(GetSubProtocol, std::string()); | 647 MOCK_CONST_METHOD0(GetSubProtocol, std::string()); |
648 MOCK_CONST_METHOD0(GetExtensions, std::string()); | 648 MOCK_CONST_METHOD0(GetExtensions, std::string()); |
649 MOCK_METHOD0(AsWebSocketStream, WebSocketStream*()); | 649 MOCK_METHOD0(AsWebSocketStream, WebSocketStream*()); |
650 }; | 650 }; |
651 | 651 |
652 struct ArgumentCopyingWebSocketStreamFactory { | 652 struct ArgumentCopyingWebSocketStreamCreator { |
653 scoped_ptr<WebSocketStreamRequest> Factory( | 653 scoped_ptr<WebSocketStreamRequest> Create( |
654 const GURL& socket_url, | 654 const GURL& socket_url, |
655 const std::vector<std::string>& requested_subprotocols, | 655 const std::vector<std::string>& requested_subprotocols, |
656 const GURL& origin, | 656 const GURL& origin, |
657 URLRequestContext* url_request_context, | 657 URLRequestContext* url_request_context, |
658 const BoundNetLog& net_log, | 658 const BoundNetLog& net_log, |
659 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { | 659 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { |
660 this->socket_url = socket_url; | 660 this->socket_url = socket_url; |
661 this->requested_subprotocols = requested_subprotocols; | 661 this->requested_subprotocols = requested_subprotocols; |
662 this->origin = origin; | 662 this->origin = origin; |
663 this->url_request_context = url_request_context; | 663 this->url_request_context = url_request_context; |
(...skipping 24 matching lines...) Expand all Loading... |
688 | 688 |
689 // Creates a new WebSocketChannel and connects it, using the settings stored | 689 // Creates a new WebSocketChannel and connects it, using the settings stored |
690 // in |connect_data_|. | 690 // in |connect_data_|. |
691 void CreateChannelAndConnect() { | 691 void CreateChannelAndConnect() { |
692 channel_.reset(new WebSocketChannel(CreateEventInterface(), | 692 channel_.reset(new WebSocketChannel(CreateEventInterface(), |
693 &connect_data_.url_request_context)); | 693 &connect_data_.url_request_context)); |
694 channel_->SendAddChannelRequestForTesting( | 694 channel_->SendAddChannelRequestForTesting( |
695 connect_data_.socket_url, | 695 connect_data_.socket_url, |
696 connect_data_.requested_subprotocols, | 696 connect_data_.requested_subprotocols, |
697 connect_data_.origin, | 697 connect_data_.origin, |
698 base::Bind(&ArgumentCopyingWebSocketStreamFactory::Factory, | 698 base::Bind(&ArgumentCopyingWebSocketStreamCreator::Create, |
699 base::Unretained(&connect_data_.factory))); | 699 base::Unretained(&connect_data_.creator))); |
700 } | 700 } |
701 | 701 |
702 // Same as CreateChannelAndConnect(), but calls the on_success callback as | 702 // Same as CreateChannelAndConnect(), but calls the on_success callback as |
703 // well. This method is virtual so that subclasses can also set the stream. | 703 // well. This method is virtual so that subclasses can also set the stream. |
704 virtual void CreateChannelAndConnectSuccessfully() { | 704 virtual void CreateChannelAndConnectSuccessfully() { |
705 CreateChannelAndConnect(); | 705 CreateChannelAndConnect(); |
706 connect_data_.factory.connect_delegate->OnSuccess(stream_.Pass()); | 706 connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass()); |
707 } | 707 } |
708 | 708 |
709 // Returns a WebSocketEventInterface to be passed to the WebSocketChannel. | 709 // Returns a WebSocketEventInterface to be passed to the WebSocketChannel. |
710 // This implementation returns a newly-created fake. Subclasses may return a | 710 // This implementation returns a newly-created fake. Subclasses may return a |
711 // mock instead. | 711 // mock instead. |
712 virtual scoped_ptr<WebSocketEventInterface> CreateEventInterface() { | 712 virtual scoped_ptr<WebSocketEventInterface> CreateEventInterface() { |
713 return scoped_ptr<WebSocketEventInterface>(new FakeWebSocketEventInterface); | 713 return scoped_ptr<WebSocketEventInterface>(new FakeWebSocketEventInterface); |
714 } | 714 } |
715 | 715 |
716 // This method serves no other purpose than to provide a nice syntax for | 716 // This method serves no other purpose than to provide a nice syntax for |
(...skipping 13 matching lines...) Expand all Loading... |
730 // URLRequestContext object. | 730 // URLRequestContext object. |
731 URLRequestContext url_request_context; | 731 URLRequestContext url_request_context; |
732 | 732 |
733 // URL to (pretend to) connect to. | 733 // URL to (pretend to) connect to. |
734 GURL socket_url; | 734 GURL socket_url; |
735 // Requested protocols for the request. | 735 // Requested protocols for the request. |
736 std::vector<std::string> requested_subprotocols; | 736 std::vector<std::string> requested_subprotocols; |
737 // Origin of the request | 737 // Origin of the request |
738 GURL origin; | 738 GURL origin; |
739 | 739 |
740 // A fake WebSocketStreamFactory that just records its arguments. | 740 // A fake WebSocketStreamCreator that just records its arguments. |
741 ArgumentCopyingWebSocketStreamFactory factory; | 741 ArgumentCopyingWebSocketStreamCreator creator; |
742 }; | 742 }; |
743 ConnectData connect_data_; | 743 ConnectData connect_data_; |
744 | 744 |
745 // The channel we are testing. Not initialised until SetChannel() is called. | 745 // The channel we are testing. Not initialised until SetChannel() is called. |
746 scoped_ptr<WebSocketChannel> channel_; | 746 scoped_ptr<WebSocketChannel> channel_; |
747 | 747 |
748 // A mock or fake stream for tests that need one. | 748 // A mock or fake stream for tests that need one. |
749 scoped_ptr<WebSocketStream> stream_; | 749 scoped_ptr<WebSocketStream> stream_; |
750 }; | 750 }; |
751 | 751 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
868 : mock_stream_(new StrictMock<MockWebSocketStream>) {} | 868 : mock_stream_(new StrictMock<MockWebSocketStream>) {} |
869 | 869 |
870 virtual void CreateChannelAndConnectSuccessfully() OVERRIDE { | 870 virtual void CreateChannelAndConnectSuccessfully() OVERRIDE { |
871 set_stream(mock_stream_.Pass()); | 871 set_stream(mock_stream_.Pass()); |
872 WebSocketChannelTest::CreateChannelAndConnectSuccessfully(); | 872 WebSocketChannelTest::CreateChannelAndConnectSuccessfully(); |
873 } | 873 } |
874 | 874 |
875 scoped_ptr<MockWebSocketStream> mock_stream_; | 875 scoped_ptr<MockWebSocketStream> mock_stream_; |
876 }; | 876 }; |
877 | 877 |
878 // Simple test that everything that should be passed to the factory function is | 878 // Simple test that everything that should be passed to the creator function is |
879 // passed to the factory function. | 879 // passed to the creator function. |
880 TEST_F(WebSocketChannelTest, EverythingIsPassedToTheFactoryFunction) { | 880 TEST_F(WebSocketChannelTest, EverythingIsPassedToTheCreatorFunction) { |
881 connect_data_.socket_url = GURL("ws://example.com/test"); | 881 connect_data_.socket_url = GURL("ws://example.com/test"); |
882 connect_data_.origin = GURL("http://example.com/test"); | 882 connect_data_.origin = GURL("http://example.com/test"); |
883 connect_data_.requested_subprotocols.push_back("Sinbad"); | 883 connect_data_.requested_subprotocols.push_back("Sinbad"); |
884 | 884 |
885 CreateChannelAndConnect(); | 885 CreateChannelAndConnect(); |
886 | 886 |
887 const ArgumentCopyingWebSocketStreamFactory& actual = connect_data_.factory; | 887 const ArgumentCopyingWebSocketStreamCreator& actual = connect_data_.creator; |
888 | 888 |
889 EXPECT_EQ(&connect_data_.url_request_context, actual.url_request_context); | 889 EXPECT_EQ(&connect_data_.url_request_context, actual.url_request_context); |
890 | 890 |
891 EXPECT_EQ(connect_data_.socket_url, actual.socket_url); | 891 EXPECT_EQ(connect_data_.socket_url, actual.socket_url); |
892 EXPECT_EQ(connect_data_.requested_subprotocols, | 892 EXPECT_EQ(connect_data_.requested_subprotocols, |
893 actual.requested_subprotocols); | 893 actual.requested_subprotocols); |
894 EXPECT_EQ(connect_data_.origin, actual.origin); | 894 EXPECT_EQ(connect_data_.origin, actual.origin); |
895 } | 895 } |
896 | 896 |
897 // Any WebSocketEventInterface methods can delete the WebSocketChannel and | 897 // Any WebSocketEventInterface methods can delete the WebSocketChannel and |
898 // return CHANNEL_DELETED. The WebSocketChannelDeletingTests are intended to | 898 // return CHANNEL_DELETED. The WebSocketChannelDeletingTests are intended to |
899 // verify that there are no use-after-free bugs when this happens. Problems will | 899 // verify that there are no use-after-free bugs when this happens. Problems will |
900 // probably only be found when running under Address Sanitizer or a similar | 900 // probably only be found when running under Address Sanitizer or a similar |
901 // tool. | 901 // tool. |
902 TEST_F(WebSocketChannelDeletingTest, OnAddChannelResponseFail) { | 902 TEST_F(WebSocketChannelDeletingTest, OnAddChannelResponseFail) { |
903 CreateChannelAndConnect(); | 903 CreateChannelAndConnect(); |
904 EXPECT_TRUE(channel_); | 904 EXPECT_TRUE(channel_); |
905 connect_data_.factory.connect_delegate->OnFailure( | 905 connect_data_.creator.connect_delegate->OnFailure( |
906 kWebSocketErrorNoStatusReceived); | 906 kWebSocketErrorNoStatusReceived); |
907 EXPECT_EQ(NULL, channel_.get()); | 907 EXPECT_EQ(NULL, channel_.get()); |
908 } | 908 } |
909 | 909 |
910 // Deletion is possible (due to IPC failure) even if the connect succeeds. | 910 // Deletion is possible (due to IPC failure) even if the connect succeeds. |
911 TEST_F(WebSocketChannelDeletingTest, OnAddChannelResponseSuccess) { | 911 TEST_F(WebSocketChannelDeletingTest, OnAddChannelResponseSuccess) { |
912 CreateChannelAndConnectSuccessfully(); | 912 CreateChannelAndConnectSuccessfully(); |
913 EXPECT_EQ(NULL, channel_.get()); | 913 EXPECT_EQ(NULL, channel_.get()); |
914 } | 914 } |
915 | 915 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1091 | 1091 |
1092 TEST_F(WebSocketChannelEventInterfaceTest, ConnectSuccessReported) { | 1092 TEST_F(WebSocketChannelEventInterfaceTest, ConnectSuccessReported) { |
1093 // false means success. | 1093 // false means success. |
1094 EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, "")); | 1094 EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, "")); |
1095 // OnFlowControl is always called immediately after connect to provide initial | 1095 // OnFlowControl is always called immediately after connect to provide initial |
1096 // quota to the renderer. | 1096 // quota to the renderer. |
1097 EXPECT_CALL(*event_interface_, OnFlowControl(_)); | 1097 EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
1098 | 1098 |
1099 CreateChannelAndConnect(); | 1099 CreateChannelAndConnect(); |
1100 | 1100 |
1101 connect_data_.factory.connect_delegate->OnSuccess(stream_.Pass()); | 1101 connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass()); |
1102 } | 1102 } |
1103 | 1103 |
1104 TEST_F(WebSocketChannelEventInterfaceTest, ConnectFailureReported) { | 1104 TEST_F(WebSocketChannelEventInterfaceTest, ConnectFailureReported) { |
1105 // true means failure. | 1105 // true means failure. |
1106 EXPECT_CALL(*event_interface_, OnAddChannelResponse(true, "")); | 1106 EXPECT_CALL(*event_interface_, OnAddChannelResponse(true, "")); |
1107 | 1107 |
1108 CreateChannelAndConnect(); | 1108 CreateChannelAndConnect(); |
1109 | 1109 |
1110 connect_data_.factory.connect_delegate->OnFailure( | 1110 connect_data_.creator.connect_delegate->OnFailure( |
1111 kWebSocketErrorNoStatusReceived); | 1111 kWebSocketErrorNoStatusReceived); |
1112 } | 1112 } |
1113 | 1113 |
1114 TEST_F(WebSocketChannelEventInterfaceTest, ProtocolPassed) { | 1114 TEST_F(WebSocketChannelEventInterfaceTest, ProtocolPassed) { |
1115 EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, "Bob")); | 1115 EXPECT_CALL(*event_interface_, OnAddChannelResponse(false, "Bob")); |
1116 EXPECT_CALL(*event_interface_, OnFlowControl(_)); | 1116 EXPECT_CALL(*event_interface_, OnFlowControl(_)); |
1117 | 1117 |
1118 CreateChannelAndConnect(); | 1118 CreateChannelAndConnect(); |
1119 | 1119 |
1120 connect_data_.factory.connect_delegate->OnSuccess( | 1120 connect_data_.creator.connect_delegate->OnSuccess( |
1121 scoped_ptr<WebSocketStream>(new FakeWebSocketStream("Bob", ""))); | 1121 scoped_ptr<WebSocketStream>(new FakeWebSocketStream("Bob", ""))); |
1122 } | 1122 } |
1123 | 1123 |
1124 // The first frames from the server can arrive together with the handshake, in | 1124 // The first frames from the server can arrive together with the handshake, in |
1125 // which case they will be available as soon as ReadFrames() is called the first | 1125 // which case they will be available as soon as ReadFrames() is called the first |
1126 // time. | 1126 // time. |
1127 TEST_F(WebSocketChannelEventInterfaceTest, DataLeftFromHandshake) { | 1127 TEST_F(WebSocketChannelEventInterfaceTest, DataLeftFromHandshake) { |
1128 scoped_ptr<ReadableFakeWebSocketStream> stream( | 1128 scoped_ptr<ReadableFakeWebSocketStream> stream( |
1129 new ReadableFakeWebSocketStream); | 1129 new ReadableFakeWebSocketStream); |
1130 static const InitFrame frames[] = { | 1130 static const InitFrame frames[] = { |
(...skipping 993 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2124 // Set the closing handshake timeout to a very tiny value before connecting. | 2124 // Set the closing handshake timeout to a very tiny value before connecting. |
2125 class WebSocketChannelStreamTimeoutTest : public WebSocketChannelStreamTest { | 2125 class WebSocketChannelStreamTimeoutTest : public WebSocketChannelStreamTest { |
2126 protected: | 2126 protected: |
2127 WebSocketChannelStreamTimeoutTest() {} | 2127 WebSocketChannelStreamTimeoutTest() {} |
2128 | 2128 |
2129 virtual void CreateChannelAndConnectSuccessfully() OVERRIDE { | 2129 virtual void CreateChannelAndConnectSuccessfully() OVERRIDE { |
2130 set_stream(mock_stream_.Pass()); | 2130 set_stream(mock_stream_.Pass()); |
2131 CreateChannelAndConnect(); | 2131 CreateChannelAndConnect(); |
2132 channel_->SetClosingHandshakeTimeoutForTesting( | 2132 channel_->SetClosingHandshakeTimeoutForTesting( |
2133 TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis)); | 2133 TimeDelta::FromMilliseconds(kVeryTinyTimeoutMillis)); |
2134 connect_data_.factory.connect_delegate->OnSuccess(stream_.Pass()); | 2134 connect_data_.creator.connect_delegate->OnSuccess(stream_.Pass()); |
2135 } | 2135 } |
2136 }; | 2136 }; |
2137 | 2137 |
2138 // In this case the server initiates the closing handshake with a Close | 2138 // In this case the server initiates the closing handshake with a Close |
2139 // message. WebSocketChannel responds with a matching Close message, and waits | 2139 // message. WebSocketChannel responds with a matching Close message, and waits |
2140 // for the server to close the TCP/IP connection. The server never closes the | 2140 // for the server to close the TCP/IP connection. The server never closes the |
2141 // connection, so the closing handshake times out and WebSocketChannel closes | 2141 // connection, so the closing handshake times out and WebSocketChannel closes |
2142 // the connection itself. | 2142 // the connection itself. |
2143 TEST_F(WebSocketChannelStreamTimeoutTest, ServerInitiatedCloseTimesOut) { | 2143 TEST_F(WebSocketChannelStreamTimeoutTest, ServerInitiatedCloseTimesOut) { |
2144 static const InitFrame frames[] = { | 2144 static const InitFrame frames[] = { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2233 channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK"); | 2233 channel_->StartClosingHandshake(kWebSocketNormalClosure, "OK"); |
2234 ASSERT_TRUE(read_frames); | 2234 ASSERT_TRUE(read_frames); |
2235 // Provide the "Close" message from the server. | 2235 // Provide the "Close" message from the server. |
2236 *read_frames = CreateFrameVector(frames); | 2236 *read_frames = CreateFrameVector(frames); |
2237 read_callback.Run(OK); | 2237 read_callback.Run(OK); |
2238 completion.WaitForResult(); | 2238 completion.WaitForResult(); |
2239 } | 2239 } |
2240 | 2240 |
2241 } // namespace | 2241 } // namespace |
2242 } // namespace net | 2242 } // namespace net |
OLD | NEW |