| 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 682 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 MOCK_CONST_METHOD0(GetSubProtocol, std::string()); | 693 MOCK_CONST_METHOD0(GetSubProtocol, std::string()); |
| 694 MOCK_CONST_METHOD0(GetExtensions, std::string()); | 694 MOCK_CONST_METHOD0(GetExtensions, std::string()); |
| 695 MOCK_METHOD0(AsWebSocketStream, WebSocketStream*()); | 695 MOCK_METHOD0(AsWebSocketStream, WebSocketStream*()); |
| 696 }; | 696 }; |
| 697 | 697 |
| 698 struct ArgumentCopyingWebSocketStreamCreator { | 698 struct ArgumentCopyingWebSocketStreamCreator { |
| 699 std::unique_ptr<WebSocketStreamRequest> Create( | 699 std::unique_ptr<WebSocketStreamRequest> Create( |
| 700 const GURL& socket_url, | 700 const GURL& socket_url, |
| 701 const std::vector<std::string>& requested_subprotocols, | 701 const std::vector<std::string>& requested_subprotocols, |
| 702 const url::Origin& origin, | 702 const url::Origin& origin, |
| 703 const GURL& first_party_for_cookies, |
| 703 const std::string& additional_headers, | 704 const std::string& additional_headers, |
| 704 URLRequestContext* url_request_context, | 705 URLRequestContext* url_request_context, |
| 705 const BoundNetLog& net_log, | 706 const BoundNetLog& net_log, |
| 706 std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { | 707 std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { |
| 707 this->socket_url = socket_url; | 708 this->socket_url = socket_url; |
| 708 this->requested_subprotocols = requested_subprotocols; | 709 this->requested_subprotocols = requested_subprotocols; |
| 709 this->origin = origin; | 710 this->origin = origin; |
| 711 this->first_party_for_cookies = first_party_for_cookies; |
| 710 this->url_request_context = url_request_context; | 712 this->url_request_context = url_request_context; |
| 711 this->net_log = net_log; | 713 this->net_log = net_log; |
| 712 this->connect_delegate = std::move(connect_delegate); | 714 this->connect_delegate = std::move(connect_delegate); |
| 713 return base::WrapUnique(new WebSocketStreamRequest); | 715 return base::WrapUnique(new WebSocketStreamRequest); |
| 714 } | 716 } |
| 715 | 717 |
| 716 GURL socket_url; | 718 GURL socket_url; |
| 717 url::Origin origin; | 719 url::Origin origin; |
| 720 GURL first_party_for_cookies; |
| 718 std::vector<std::string> requested_subprotocols; | 721 std::vector<std::string> requested_subprotocols; |
| 719 URLRequestContext* url_request_context; | 722 URLRequestContext* url_request_context; |
| 720 BoundNetLog net_log; | 723 BoundNetLog net_log; |
| 721 std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate; | 724 std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate; |
| 722 }; | 725 }; |
| 723 | 726 |
| 724 // Converts a std::string to a std::vector<char>. For test purposes, it is | 727 // Converts a std::string to a std::vector<char>. For test purposes, it is |
| 725 // convenient to be able to specify data as a string, but the | 728 // convenient to be able to specify data as a string, but the |
| 726 // WebSocketEventInterface requires the vector<char> type. | 729 // WebSocketEventInterface requires the vector<char> type. |
| 727 std::vector<char> AsVector(const std::string& s) { | 730 std::vector<char> AsVector(const std::string& s) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 740 protected: | 743 protected: |
| 741 WebSocketChannelTest() : stream_(new FakeWebSocketStream) {} | 744 WebSocketChannelTest() : stream_(new FakeWebSocketStream) {} |
| 742 | 745 |
| 743 // Creates a new WebSocketChannel and connects it, using the settings stored | 746 // Creates a new WebSocketChannel and connects it, using the settings stored |
| 744 // in |connect_data_|. | 747 // in |connect_data_|. |
| 745 void CreateChannelAndConnect() { | 748 void CreateChannelAndConnect() { |
| 746 channel_.reset(new WebSocketChannel(CreateEventInterface(), | 749 channel_.reset(new WebSocketChannel(CreateEventInterface(), |
| 747 &connect_data_.url_request_context)); | 750 &connect_data_.url_request_context)); |
| 748 channel_->SendAddChannelRequestForTesting( | 751 channel_->SendAddChannelRequestForTesting( |
| 749 connect_data_.socket_url, connect_data_.requested_subprotocols, | 752 connect_data_.socket_url, connect_data_.requested_subprotocols, |
| 750 connect_data_.origin, "", | 753 connect_data_.origin, connect_data_.first_party_for_cookies, "", |
| 751 base::Bind(&ArgumentCopyingWebSocketStreamCreator::Create, | 754 base::Bind(&ArgumentCopyingWebSocketStreamCreator::Create, |
| 752 base::Unretained(&connect_data_.creator))); | 755 base::Unretained(&connect_data_.creator))); |
| 753 } | 756 } |
| 754 | 757 |
| 755 // Same as CreateChannelAndConnect(), but calls the on_success callback as | 758 // Same as CreateChannelAndConnect(), but calls the on_success callback as |
| 756 // well. This method is virtual so that subclasses can also set the stream. | 759 // well. This method is virtual so that subclasses can also set the stream. |
| 757 virtual void CreateChannelAndConnectSuccessfully() { | 760 virtual void CreateChannelAndConnectSuccessfully() { |
| 758 CreateChannelAndConnect(); | 761 CreateChannelAndConnect(); |
| 759 // Most tests aren't concerned with flow control from the renderer, so allow | 762 // Most tests aren't concerned with flow control from the renderer, so allow |
| 760 // MAX_INT quota units. | 763 // MAX_INT quota units. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 773 // assigning to stream_. class T must be a subclass of WebSocketStream or you | 776 // assigning to stream_. class T must be a subclass of WebSocketStream or you |
| 774 // will have unpleasant compile errors. | 777 // will have unpleasant compile errors. |
| 775 template <class T> | 778 template <class T> |
| 776 void set_stream(std::unique_ptr<T> stream) { | 779 void set_stream(std::unique_ptr<T> stream) { |
| 777 stream_ = std::move(stream); | 780 stream_ = std::move(stream); |
| 778 } | 781 } |
| 779 | 782 |
| 780 // A struct containing the data that will be used to connect the channel. | 783 // A struct containing the data that will be used to connect the channel. |
| 781 // Grouped for readability. | 784 // Grouped for readability. |
| 782 struct ConnectData { | 785 struct ConnectData { |
| 783 ConnectData() : socket_url("ws://ws/"), origin(GURL("http://ws")) {} | 786 ConnectData() |
| 787 : socket_url("ws://ws/"), |
| 788 origin(GURL("http://ws")), |
| 789 first_party_for_cookies("http://ws/") {} |
| 784 | 790 |
| 785 // URLRequestContext object. | 791 // URLRequestContext object. |
| 786 URLRequestContext url_request_context; | 792 URLRequestContext url_request_context; |
| 787 | 793 |
| 788 // URL to (pretend to) connect to. | 794 // URL to (pretend to) connect to. |
| 789 GURL socket_url; | 795 GURL socket_url; |
| 790 // Requested protocols for the request. | 796 // Requested protocols for the request. |
| 791 std::vector<std::string> requested_subprotocols; | 797 std::vector<std::string> requested_subprotocols; |
| 792 // Origin of the request | 798 // Origin of the request |
| 793 url::Origin origin; | 799 url::Origin origin; |
| 800 // First party for cookies for the request. |
| 801 GURL first_party_for_cookies; |
| 794 | 802 |
| 795 // A fake WebSocketStreamCreator that just records its arguments. | 803 // A fake WebSocketStreamCreator that just records its arguments. |
| 796 ArgumentCopyingWebSocketStreamCreator creator; | 804 ArgumentCopyingWebSocketStreamCreator creator; |
| 797 }; | 805 }; |
| 798 ConnectData connect_data_; | 806 ConnectData connect_data_; |
| 799 | 807 |
| 800 // The channel we are testing. Not initialised until SetChannel() is called. | 808 // The channel we are testing. Not initialised until SetChannel() is called. |
| 801 std::unique_ptr<WebSocketChannel> channel_; | 809 std::unique_ptr<WebSocketChannel> channel_; |
| 802 | 810 |
| 803 // A mock or fake stream for tests that need one. | 811 // A mock or fake stream for tests that need one. |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 998 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); | 1006 EXPECT_CALL(*mock_stream_, GetSubProtocol()).Times(AnyNumber()); |
| 999 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); | 1007 EXPECT_CALL(*mock_stream_, GetExtensions()).Times(AnyNumber()); |
| 1000 } | 1008 } |
| 1001 }; | 1009 }; |
| 1002 | 1010 |
| 1003 // Simple test that everything that should be passed to the creator function is | 1011 // Simple test that everything that should be passed to the creator function is |
| 1004 // passed to the creator function. | 1012 // passed to the creator function. |
| 1005 TEST_F(WebSocketChannelTest, EverythingIsPassedToTheCreatorFunction) { | 1013 TEST_F(WebSocketChannelTest, EverythingIsPassedToTheCreatorFunction) { |
| 1006 connect_data_.socket_url = GURL("ws://example.com/test"); | 1014 connect_data_.socket_url = GURL("ws://example.com/test"); |
| 1007 connect_data_.origin = url::Origin(GURL("http://example.com")); | 1015 connect_data_.origin = url::Origin(GURL("http://example.com")); |
| 1016 connect_data_.first_party_for_cookies = GURL("http://example.com/"); |
| 1008 connect_data_.requested_subprotocols.push_back("Sinbad"); | 1017 connect_data_.requested_subprotocols.push_back("Sinbad"); |
| 1009 | 1018 |
| 1010 CreateChannelAndConnect(); | 1019 CreateChannelAndConnect(); |
| 1011 | 1020 |
| 1012 const ArgumentCopyingWebSocketStreamCreator& actual = connect_data_.creator; | 1021 const ArgumentCopyingWebSocketStreamCreator& actual = connect_data_.creator; |
| 1013 | 1022 |
| 1014 EXPECT_EQ(&connect_data_.url_request_context, actual.url_request_context); | 1023 EXPECT_EQ(&connect_data_.url_request_context, actual.url_request_context); |
| 1015 | 1024 |
| 1016 EXPECT_EQ(connect_data_.socket_url, actual.socket_url); | 1025 EXPECT_EQ(connect_data_.socket_url, actual.socket_url); |
| 1017 EXPECT_EQ(connect_data_.requested_subprotocols, | 1026 EXPECT_EQ(connect_data_.requested_subprotocols, |
| 1018 actual.requested_subprotocols); | 1027 actual.requested_subprotocols); |
| 1019 EXPECT_EQ(connect_data_.origin.Serialize(), actual.origin.Serialize()); | 1028 EXPECT_EQ(connect_data_.origin.Serialize(), actual.origin.Serialize()); |
| 1029 EXPECT_EQ(connect_data_.first_party_for_cookies, |
| 1030 actual.first_party_for_cookies); |
| 1020 } | 1031 } |
| 1021 | 1032 |
| 1022 // Verify that calling SendFlowControl before the connection is established does | 1033 // Verify that calling SendFlowControl before the connection is established does |
| 1023 // not cause a crash. | 1034 // not cause a crash. |
| 1024 TEST_F(WebSocketChannelTest, SendFlowControlDuringHandshakeOkay) { | 1035 TEST_F(WebSocketChannelTest, SendFlowControlDuringHandshakeOkay) { |
| 1025 CreateChannelAndConnect(); | 1036 CreateChannelAndConnect(); |
| 1026 ASSERT_TRUE(channel_); | 1037 ASSERT_TRUE(channel_); |
| 1027 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(65536)); | 1038 ASSERT_EQ(CHANNEL_ALIVE, channel_->SendFlowControl(65536)); |
| 1028 } | 1039 } |
| 1029 | 1040 |
| (...skipping 2463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3493 | 3504 |
| 3494 channel_->SendFrame( | 3505 channel_->SendFrame( |
| 3495 true, WebSocketFrameHeader::kOpCodeText, | 3506 true, WebSocketFrameHeader::kOpCodeText, |
| 3496 std::vector<char>(static_cast<size_t>(kMessageSize), 'a')); | 3507 std::vector<char>(static_cast<size_t>(kMessageSize), 'a')); |
| 3497 int new_send_quota = channel_->current_send_quota(); | 3508 int new_send_quota = channel_->current_send_quota(); |
| 3498 EXPECT_EQ(kMessageSize, initial_send_quota - new_send_quota); | 3509 EXPECT_EQ(kMessageSize, initial_send_quota - new_send_quota); |
| 3499 } | 3510 } |
| 3500 | 3511 |
| 3501 } // namespace | 3512 } // namespace |
| 3502 } // namespace net | 3513 } // namespace net |
| OLD | NEW |