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