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 "google_apis/gcm/engine/connection_handler_impl.h" | 5 #include "google_apis/gcm/engine/connection_handler_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 net::StreamSocket* BuildSocket(const ReadList& read_list, | 111 net::StreamSocket* BuildSocket(const ReadList& read_list, |
112 const WriteList& write_list); | 112 const WriteList& write_list); |
113 | 113 |
114 // Pump |message_loop_|, resetting |run_loop_| after completion. | 114 // Pump |message_loop_|, resetting |run_loop_| after completion. |
115 void PumpLoop(); | 115 void PumpLoop(); |
116 | 116 |
117 ConnectionHandlerImpl* connection_handler() { | 117 ConnectionHandlerImpl* connection_handler() { |
118 return connection_handler_.get(); | 118 return connection_handler_.get(); |
119 } | 119 } |
120 base::MessageLoop* message_loop() { return &message_loop_; }; | 120 base::MessageLoop* message_loop() { return &message_loop_; }; |
121 net::DelayedSocketData* data_provider() { return data_provider_.get(); } | 121 net::StaticSocketDataProvider* data_provider() { |
| 122 return data_provider_.get(); |
| 123 } |
122 int last_error() const { return last_error_; } | 124 int last_error() const { return last_error_; } |
123 | 125 |
124 // Initialize the connection handler, setting |dst_proto| as the destination | 126 // Initialize the connection handler, setting |dst_proto| as the destination |
125 // for any received messages. | 127 // for any received messages. |
126 void Connect(ScopedMessage* dst_proto); | 128 void Connect(ScopedMessage* dst_proto); |
127 | 129 |
128 // Runs the message loop until a message is received. | 130 // Runs the message loop until a message is received. |
129 void WaitForMessage(); | 131 void WaitForMessage(); |
130 | 132 |
131 private: | 133 private: |
132 void ReadContinuation(ScopedMessage* dst_proto, ScopedMessage new_proto); | 134 void ReadContinuation(ScopedMessage* dst_proto, ScopedMessage new_proto); |
133 void WriteContinuation(); | 135 void WriteContinuation(); |
134 void ConnectionContinuation(int error); | 136 void ConnectionContinuation(int error); |
135 | 137 |
136 // SocketStreams and their data provider. | 138 // SocketStreams and their data provider. |
137 ReadList mock_reads_; | 139 ReadList mock_reads_; |
138 WriteList mock_writes_; | 140 WriteList mock_writes_; |
139 scoped_ptr<net::DelayedSocketData> data_provider_; | 141 scoped_ptr<net::StaticSocketDataProvider> data_provider_; |
140 | 142 |
141 // The connection handler being tested. | 143 // The connection handler being tested. |
142 scoped_ptr<ConnectionHandlerImpl> connection_handler_; | 144 scoped_ptr<ConnectionHandlerImpl> connection_handler_; |
143 | 145 |
144 // The last connection error received. | 146 // The last connection error received. |
145 int last_error_; | 147 int last_error_; |
146 | 148 |
147 // net:: components. | 149 // net:: components. |
148 scoped_ptr<net::StreamSocket> socket_; | 150 scoped_ptr<net::StreamSocket> socket_; |
149 net::MockClientSocketFactory socket_factory_; | 151 net::MockClientSocketFactory socket_factory_; |
(...skipping 12 matching lines...) Expand all Loading... |
162 | 164 |
163 GCMConnectionHandlerImplTest::~GCMConnectionHandlerImplTest() { | 165 GCMConnectionHandlerImplTest::~GCMConnectionHandlerImplTest() { |
164 } | 166 } |
165 | 167 |
166 net::StreamSocket* GCMConnectionHandlerImplTest::BuildSocket( | 168 net::StreamSocket* GCMConnectionHandlerImplTest::BuildSocket( |
167 const ReadList& read_list, | 169 const ReadList& read_list, |
168 const WriteList& write_list) { | 170 const WriteList& write_list) { |
169 mock_reads_ = read_list; | 171 mock_reads_ = read_list; |
170 mock_writes_ = write_list; | 172 mock_writes_ = write_list; |
171 data_provider_.reset( | 173 data_provider_.reset( |
172 new net::DelayedSocketData(0, | 174 new net::StaticSocketDataProvider( |
173 &(mock_reads_[0]), mock_reads_.size(), | 175 &(mock_reads_[0]), mock_reads_.size(), |
174 &(mock_writes_[0]), mock_writes_.size())); | 176 &(mock_writes_[0]), mock_writes_.size())); |
175 socket_factory_.AddSocketDataProvider(data_provider_.get()); | 177 socket_factory_.AddSocketDataProvider(data_provider_.get()); |
176 | 178 |
177 socket_ = socket_factory_.CreateTransportClientSocket( | 179 socket_ = socket_factory_.CreateTransportClientSocket( |
178 address_list_, NULL, net::NetLog::Source()); | 180 address_list_, NULL, net::NetLog::Source()); |
179 socket_->Connect(net::CompletionCallback()); | 181 socket_->Connect(net::CompletionCallback()); |
180 | 182 |
181 run_loop_.reset(new base::RunLoop()); | 183 run_loop_.reset(new base::RunLoop()); |
182 PumpLoop(); | 184 PumpLoop(); |
183 | 185 |
184 DCHECK(socket_->IsConnected()); | 186 DCHECK(socket_->IsConnected()); |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
564 | 566 |
565 ScopedMessage received_message; | 567 ScopedMessage received_message; |
566 Connect(&received_message); | 568 Connect(&received_message); |
567 WaitForMessage(); // The login send. | 569 WaitForMessage(); // The login send. |
568 WaitForMessage(); // The login response. | 570 WaitForMessage(); // The login response. |
569 received_message.reset(); | 571 received_message.reset(); |
570 WaitForMessage(); // Should time out. | 572 WaitForMessage(); // Should time out. |
571 EXPECT_FALSE(received_message.get()); | 573 EXPECT_FALSE(received_message.get()); |
572 EXPECT_EQ(net::ERR_TIMED_OUT, last_error()); | 574 EXPECT_EQ(net::ERR_TIMED_OUT, last_error()); |
573 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 575 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
574 | |
575 // Finish the socket read. Should have no effect. | |
576 data_provider()->ForceNextRead(); | |
577 } | 576 } |
578 | 577 |
579 // Receive a message with zero data bytes. | 578 // Receive a message with zero data bytes. |
580 TEST_F(GCMConnectionHandlerImplTest, RecvMsgNoData) { | 579 TEST_F(GCMConnectionHandlerImplTest, RecvMsgNoData) { |
581 std::string handshake_request = EncodeHandshakeRequest(); | 580 std::string handshake_request = EncodeHandshakeRequest(); |
582 WriteList write_list(1, net::MockWrite(net::ASYNC, | 581 WriteList write_list(1, net::MockWrite(net::ASYNC, |
583 handshake_request.c_str(), | 582 handshake_request.c_str(), |
584 handshake_request.size())); | 583 handshake_request.size())); |
585 std::string handshake_response = EncodeHandshakeResponse(); | 584 std::string handshake_response = EncodeHandshakeResponse(); |
586 | 585 |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
800 WaitForMessage(); // The login send. | 799 WaitForMessage(); // The login send. |
801 WaitForMessage(); // The login response. | 800 WaitForMessage(); // The login response. |
802 WaitForMessage(); // The data message. | 801 WaitForMessage(); // The data message. |
803 ASSERT_TRUE(received_message.get()); | 802 ASSERT_TRUE(received_message.get()); |
804 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); | 803 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); |
805 EXPECT_EQ(net::OK, last_error()); | 804 EXPECT_EQ(net::OK, last_error()); |
806 } | 805 } |
807 | 806 |
808 } // namespace | 807 } // namespace |
809 } // namespace gcm | 808 } // namespace gcm |
OLD | NEW |