| 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.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" |
| 11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
| 12 #include "google/protobuf/io/coded_stream.h" | 12 #include "google/protobuf/io/coded_stream.h" |
| 13 #include "google/protobuf/io/zero_copy_stream_impl_lite.h" | 13 #include "google/protobuf/io/zero_copy_stream_impl_lite.h" |
| 14 #include "google_apis/gcm/base/mcs_util.h" | 14 #include "google_apis/gcm/base/mcs_util.h" |
| 15 #include "google_apis/gcm/base/socket_stream.h" | 15 #include "google_apis/gcm/base/socket_stream.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // Build a serialized data message stanza protobuf. | 90 // Build a serialized data message stanza protobuf. |
| 91 std::string BuildDataMessage(const std::string& from, | 91 std::string BuildDataMessage(const std::string& from, |
| 92 const std::string& category) { | 92 const std::string& category) { |
| 93 std::string result; | 93 std::string result; |
| 94 mcs_proto::DataMessageStanza data_message; | 94 mcs_proto::DataMessageStanza data_message; |
| 95 data_message.set_from(from); | 95 data_message.set_from(from); |
| 96 data_message.set_category(category); | 96 data_message.set_category(category); |
| 97 return data_message.SerializeAsString(); | 97 return data_message.SerializeAsString(); |
| 98 } | 98 } |
| 99 | 99 |
| 100 class GCMConnectionHandlerTest : public testing::Test { | 100 class GCMConnectionHandlerImplTest : public testing::Test { |
| 101 public: | 101 public: |
| 102 GCMConnectionHandlerTest(); | 102 GCMConnectionHandlerImplTest(); |
| 103 virtual ~GCMConnectionHandlerTest(); | 103 virtual ~GCMConnectionHandlerImplTest(); |
| 104 | 104 |
| 105 net::StreamSocket* BuildSocket(const ReadList& read_list, | 105 net::StreamSocket* BuildSocket(const ReadList& read_list, |
| 106 const WriteList& write_list); | 106 const WriteList& write_list); |
| 107 | 107 |
| 108 // Pump |message_loop_|, resetting |run_loop_| after completion. | 108 // Pump |message_loop_|, resetting |run_loop_| after completion. |
| 109 void PumpLoop(); | 109 void PumpLoop(); |
| 110 | 110 |
| 111 ConnectionHandler* connection_handler() { return &connection_handler_; } | 111 ConnectionHandlerImpl* connection_handler() { |
| 112 return connection_handler_.get(); |
| 113 } |
| 112 base::MessageLoop* message_loop() { return &message_loop_; }; | 114 base::MessageLoop* message_loop() { return &message_loop_; }; |
| 113 net::DelayedSocketData* data_provider() { return data_provider_.get(); } | 115 net::DelayedSocketData* data_provider() { return data_provider_.get(); } |
| 114 int last_error() const { return last_error_; } | 116 int last_error() const { return last_error_; } |
| 115 | 117 |
| 116 // Initialize the connection handler, setting |dst_proto| as the destination | 118 // Initialize the connection handler, setting |dst_proto| as the destination |
| 117 // for any received messages. | 119 // for any received messages. |
| 118 void Connect(ScopedMessage* dst_proto); | 120 void Connect(ScopedMessage* dst_proto); |
| 119 | 121 |
| 120 // Runs the message loop until a message is received. | 122 // Runs the message loop until a message is received. |
| 121 void WaitForMessage(); | 123 void WaitForMessage(); |
| 122 | 124 |
| 123 private: | 125 private: |
| 124 void ReadContinuation(ScopedMessage* dst_proto, ScopedMessage new_proto); | 126 void ReadContinuation(ScopedMessage* dst_proto, ScopedMessage new_proto); |
| 125 void WriteContinuation(); | 127 void WriteContinuation(); |
| 126 void ConnectionContinuation(int error); | 128 void ConnectionContinuation(int error); |
| 127 | 129 |
| 128 // SocketStreams and their data provider. | 130 // SocketStreams and their data provider. |
| 129 ReadList mock_reads_; | 131 ReadList mock_reads_; |
| 130 WriteList mock_writes_; | 132 WriteList mock_writes_; |
| 131 scoped_ptr<net::DelayedSocketData> data_provider_; | 133 scoped_ptr<net::DelayedSocketData> data_provider_; |
| 132 scoped_ptr<SocketInputStream> socket_input_stream_; | 134 scoped_ptr<SocketInputStream> socket_input_stream_; |
| 133 scoped_ptr<SocketOutputStream> socket_output_stream_; | 135 scoped_ptr<SocketOutputStream> socket_output_stream_; |
| 134 | 136 |
| 135 // The connection handler being tested. | 137 // The connection handler being tested. |
| 136 ConnectionHandler connection_handler_; | 138 scoped_ptr<ConnectionHandlerImpl> connection_handler_; |
| 137 | 139 |
| 138 // The last connection error received. | 140 // The last connection error received. |
| 139 int last_error_; | 141 int last_error_; |
| 140 | 142 |
| 141 // net:: components. | 143 // net:: components. |
| 142 scoped_ptr<net::StreamSocket> socket_; | 144 scoped_ptr<net::StreamSocket> socket_; |
| 143 net::MockClientSocketFactory socket_factory_; | 145 net::MockClientSocketFactory socket_factory_; |
| 144 net::AddressList address_list_; | 146 net::AddressList address_list_; |
| 145 | 147 |
| 146 base::MessageLoopForIO message_loop_; | 148 base::MessageLoopForIO message_loop_; |
| 147 scoped_ptr<base::RunLoop> run_loop_; | 149 scoped_ptr<base::RunLoop> run_loop_; |
| 148 }; | 150 }; |
| 149 | 151 |
| 150 GCMConnectionHandlerTest::GCMConnectionHandlerTest() | 152 GCMConnectionHandlerImplTest::GCMConnectionHandlerImplTest() |
| 151 : connection_handler_(TestTimeouts::tiny_timeout()), | 153 : last_error_(0) { |
| 152 last_error_(0) { | |
| 153 net::IPAddressNumber ip_number; | 154 net::IPAddressNumber ip_number; |
| 154 net::ParseIPLiteralToNumber("127.0.0.1", &ip_number); | 155 net::ParseIPLiteralToNumber("127.0.0.1", &ip_number); |
| 155 address_list_ = net::AddressList::CreateFromIPAddress(ip_number, kMCSPort); | 156 address_list_ = net::AddressList::CreateFromIPAddress(ip_number, kMCSPort); |
| 156 } | 157 } |
| 157 | 158 |
| 158 GCMConnectionHandlerTest::~GCMConnectionHandlerTest() { | 159 GCMConnectionHandlerImplTest::~GCMConnectionHandlerImplTest() { |
| 159 } | 160 } |
| 160 | 161 |
| 161 net::StreamSocket* GCMConnectionHandlerTest::BuildSocket( | 162 net::StreamSocket* GCMConnectionHandlerImplTest::BuildSocket( |
| 162 const ReadList& read_list, | 163 const ReadList& read_list, |
| 163 const WriteList& write_list) { | 164 const WriteList& write_list) { |
| 164 mock_reads_ = read_list; | 165 mock_reads_ = read_list; |
| 165 mock_writes_ = write_list; | 166 mock_writes_ = write_list; |
| 166 data_provider_.reset( | 167 data_provider_.reset( |
| 167 new net::DelayedSocketData(0, | 168 new net::DelayedSocketData(0, |
| 168 &(mock_reads_[0]), mock_reads_.size(), | 169 &(mock_reads_[0]), mock_reads_.size(), |
| 169 &(mock_writes_[0]), mock_writes_.size())); | 170 &(mock_writes_[0]), mock_writes_.size())); |
| 170 socket_factory_.AddSocketDataProvider(data_provider_.get()); | 171 socket_factory_.AddSocketDataProvider(data_provider_.get()); |
| 171 | 172 |
| 172 socket_ = socket_factory_.CreateTransportClientSocket( | 173 socket_ = socket_factory_.CreateTransportClientSocket( |
| 173 address_list_, NULL, net::NetLog::Source()); | 174 address_list_, NULL, net::NetLog::Source()); |
| 174 socket_->Connect(net::CompletionCallback()); | 175 socket_->Connect(net::CompletionCallback()); |
| 175 | 176 |
| 176 run_loop_.reset(new base::RunLoop()); | 177 run_loop_.reset(new base::RunLoop()); |
| 177 PumpLoop(); | 178 PumpLoop(); |
| 178 | 179 |
| 179 DCHECK(socket_->IsConnected()); | 180 DCHECK(socket_->IsConnected()); |
| 180 return socket_.get(); | 181 return socket_.get(); |
| 181 } | 182 } |
| 182 | 183 |
| 183 void GCMConnectionHandlerTest::PumpLoop() { | 184 void GCMConnectionHandlerImplTest::PumpLoop() { |
| 184 run_loop_->RunUntilIdle(); | 185 run_loop_->RunUntilIdle(); |
| 185 run_loop_.reset(new base::RunLoop()); | 186 run_loop_.reset(new base::RunLoop()); |
| 186 } | 187 } |
| 187 | 188 |
| 188 void GCMConnectionHandlerTest::Connect( | 189 void GCMConnectionHandlerImplTest::Connect( |
| 189 ScopedMessage* dst_proto) { | 190 ScopedMessage* dst_proto) { |
| 190 connection_handler_.Init( | 191 connection_handler_.reset(new ConnectionHandlerImpl( |
| 191 socket_.Pass(), | 192 TestTimeouts::tiny_timeout(), |
| 192 *BuildLoginRequest(kAuthId, kAuthToken), | 193 base::Bind(&GCMConnectionHandlerImplTest::ReadContinuation, |
| 193 base::Bind(&GCMConnectionHandlerTest::ReadContinuation, | 194 base::Unretained(this), |
| 194 base::Unretained(this), | 195 dst_proto), |
| 195 dst_proto), | 196 base::Bind(&GCMConnectionHandlerImplTest::WriteContinuation, |
| 196 base::Bind(&GCMConnectionHandlerTest::WriteContinuation, | 197 base::Unretained(this)), |
| 197 base::Unretained(this)), | 198 base::Bind(&GCMConnectionHandlerImplTest::ConnectionContinuation, |
| 198 base::Bind(&GCMConnectionHandlerTest::ConnectionContinuation, | 199 base::Unretained(this)))); |
| 199 base::Unretained(this))); | 200 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 201 connection_handler_->Init(*BuildLoginRequest(kAuthId, kAuthToken), |
| 202 socket_.Pass()); |
| 200 } | 203 } |
| 201 | 204 |
| 202 void GCMConnectionHandlerTest::ReadContinuation( | 205 void GCMConnectionHandlerImplTest::ReadContinuation( |
| 203 ScopedMessage* dst_proto, | 206 ScopedMessage* dst_proto, |
| 204 ScopedMessage new_proto) { | 207 ScopedMessage new_proto) { |
| 205 *dst_proto = new_proto.Pass(); | 208 *dst_proto = new_proto.Pass(); |
| 206 run_loop_->Quit(); | 209 run_loop_->Quit(); |
| 207 } | 210 } |
| 208 | 211 |
| 209 void GCMConnectionHandlerTest::WaitForMessage() { | 212 void GCMConnectionHandlerImplTest::WaitForMessage() { |
| 210 run_loop_->Run(); | 213 run_loop_->Run(); |
| 211 run_loop_.reset(new base::RunLoop()); | 214 run_loop_.reset(new base::RunLoop()); |
| 212 } | 215 } |
| 213 | 216 |
| 214 void GCMConnectionHandlerTest::WriteContinuation() { | 217 void GCMConnectionHandlerImplTest::WriteContinuation() { |
| 215 run_loop_->Quit(); | 218 run_loop_->Quit(); |
| 216 } | 219 } |
| 217 | 220 |
| 218 void GCMConnectionHandlerTest::ConnectionContinuation(int error) { | 221 void GCMConnectionHandlerImplTest::ConnectionContinuation(int error) { |
| 219 last_error_ = error; | 222 last_error_ = error; |
| 220 run_loop_->Quit(); | 223 run_loop_->Quit(); |
| 221 } | 224 } |
| 222 | 225 |
| 223 // Initialize the connection handler and ensure the handshake completes | 226 // Initialize the connection handler and ensure the handshake completes |
| 224 // successfully. | 227 // successfully. |
| 225 TEST_F(GCMConnectionHandlerTest, Init) { | 228 TEST_F(GCMConnectionHandlerImplTest, Init) { |
| 226 std::string handshake_request = EncodeHandshakeRequest(); | 229 std::string handshake_request = EncodeHandshakeRequest(); |
| 227 WriteList write_list(1, net::MockWrite(net::ASYNC, | 230 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 228 handshake_request.c_str(), | 231 handshake_request.c_str(), |
| 229 handshake_request.size())); | 232 handshake_request.size())); |
| 230 std::string handshake_response = EncodeHandshakeResponse(); | 233 std::string handshake_response = EncodeHandshakeResponse(); |
| 231 ReadList read_list(1, net::MockRead(net::ASYNC, | 234 ReadList read_list(1, net::MockRead(net::ASYNC, |
| 232 handshake_response.c_str(), | 235 handshake_response.c_str(), |
| 233 handshake_response.size())); | 236 handshake_response.size())); |
| 234 BuildSocket(read_list, write_list); | 237 BuildSocket(read_list, write_list); |
| 235 | 238 |
| 236 ScopedMessage received_message; | 239 ScopedMessage received_message; |
| 237 EXPECT_FALSE(connection_handler()->CanSendMessage()); | |
| 238 Connect(&received_message); | 240 Connect(&received_message); |
| 239 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 241 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 240 WaitForMessage(); // The login send. | 242 WaitForMessage(); // The login send. |
| 241 WaitForMessage(); // The login response. | 243 WaitForMessage(); // The login response. |
| 242 ASSERT_TRUE(received_message.get()); | 244 ASSERT_TRUE(received_message.get()); |
| 243 EXPECT_EQ(BuildLoginResponse(), received_message->SerializeAsString()); | 245 EXPECT_EQ(BuildLoginResponse(), received_message->SerializeAsString()); |
| 244 EXPECT_TRUE(connection_handler()->CanSendMessage()); | 246 EXPECT_TRUE(connection_handler()->CanSendMessage()); |
| 245 } | 247 } |
| 246 | 248 |
| 247 // Simulate the handshake response returning an older version. Initialization | 249 // Simulate the handshake response returning an older version. Initialization |
| 248 // should fail. | 250 // should fail. |
| 249 TEST_F(GCMConnectionHandlerTest, InitFailedVersionCheck) { | 251 TEST_F(GCMConnectionHandlerImplTest, InitFailedVersionCheck) { |
| 250 std::string handshake_request = EncodeHandshakeRequest(); | 252 std::string handshake_request = EncodeHandshakeRequest(); |
| 251 WriteList write_list(1, net::MockWrite(net::ASYNC, | 253 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 252 handshake_request.c_str(), | 254 handshake_request.c_str(), |
| 253 handshake_request.size())); | 255 handshake_request.size())); |
| 254 std::string handshake_response = EncodeHandshakeResponse(); | 256 std::string handshake_response = EncodeHandshakeResponse(); |
| 255 // Overwrite the version byte. | 257 // Overwrite the version byte. |
| 256 handshake_response[0] = 37; | 258 handshake_response[0] = 37; |
| 257 ReadList read_list(1, net::MockRead(net::ASYNC, | 259 ReadList read_list(1, net::MockRead(net::ASYNC, |
| 258 handshake_response.c_str(), | 260 handshake_response.c_str(), |
| 259 handshake_response.size())); | 261 handshake_response.size())); |
| 260 BuildSocket(read_list, write_list); | 262 BuildSocket(read_list, write_list); |
| 261 | 263 |
| 262 ScopedMessage received_message; | 264 ScopedMessage received_message; |
| 263 Connect(&received_message); | 265 Connect(&received_message); |
| 264 WaitForMessage(); // The login send. | 266 WaitForMessage(); // The login send. |
| 265 WaitForMessage(); // The login response. Should result in a connection error. | 267 WaitForMessage(); // The login response. Should result in a connection error. |
| 266 EXPECT_FALSE(received_message.get()); | 268 EXPECT_FALSE(received_message.get()); |
| 267 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 269 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 268 EXPECT_EQ(net::ERR_FAILED, last_error()); | 270 EXPECT_EQ(net::ERR_FAILED, last_error()); |
| 269 } | 271 } |
| 270 | 272 |
| 271 // Attempt to initialize, but receive no server response, resulting in a time | 273 // Attempt to initialize, but receive no server response, resulting in a time |
| 272 // out. | 274 // out. |
| 273 TEST_F(GCMConnectionHandlerTest, InitTimeout) { | 275 TEST_F(GCMConnectionHandlerImplTest, InitTimeout) { |
| 274 std::string handshake_request = EncodeHandshakeRequest(); | 276 std::string handshake_request = EncodeHandshakeRequest(); |
| 275 WriteList write_list(1, net::MockWrite(net::ASYNC, | 277 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 276 handshake_request.c_str(), | 278 handshake_request.c_str(), |
| 277 handshake_request.size())); | 279 handshake_request.size())); |
| 278 ReadList read_list(1, net::MockRead(net::SYNCHRONOUS, | 280 ReadList read_list(1, net::MockRead(net::SYNCHRONOUS, |
| 279 net::ERR_IO_PENDING)); | 281 net::ERR_IO_PENDING)); |
| 280 BuildSocket(read_list, write_list); | 282 BuildSocket(read_list, write_list); |
| 281 | 283 |
| 282 ScopedMessage received_message; | 284 ScopedMessage received_message; |
| 283 Connect(&received_message); | 285 Connect(&received_message); |
| 284 WaitForMessage(); // The login send. | 286 WaitForMessage(); // The login send. |
| 285 WaitForMessage(); // The login response. Should result in a connection error. | 287 WaitForMessage(); // The login response. Should result in a connection error. |
| 286 EXPECT_FALSE(received_message.get()); | 288 EXPECT_FALSE(received_message.get()); |
| 287 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 289 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 288 EXPECT_EQ(net::ERR_TIMED_OUT, last_error()); | 290 EXPECT_EQ(net::ERR_TIMED_OUT, last_error()); |
| 289 } | 291 } |
| 290 | 292 |
| 291 // Attempt to initialize, but receive an incomplete server response, resulting | 293 // Attempt to initialize, but receive an incomplete server response, resulting |
| 292 // in a time out. | 294 // in a time out. |
| 293 TEST_F(GCMConnectionHandlerTest, InitIncompleteTimeout) { | 295 TEST_F(GCMConnectionHandlerImplTest, InitIncompleteTimeout) { |
| 294 std::string handshake_request = EncodeHandshakeRequest(); | 296 std::string handshake_request = EncodeHandshakeRequest(); |
| 295 WriteList write_list(1, net::MockWrite(net::ASYNC, | 297 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 296 handshake_request.c_str(), | 298 handshake_request.c_str(), |
| 297 handshake_request.size())); | 299 handshake_request.size())); |
| 298 std::string handshake_response = EncodeHandshakeResponse(); | 300 std::string handshake_response = EncodeHandshakeResponse(); |
| 299 ReadList read_list; | 301 ReadList read_list; |
| 300 read_list.push_back(net::MockRead(net::ASYNC, | 302 read_list.push_back(net::MockRead(net::ASYNC, |
| 301 handshake_response.c_str(), | 303 handshake_response.c_str(), |
| 302 handshake_response.size() / 2)); | 304 handshake_response.size() / 2)); |
| 303 read_list.push_back(net::MockRead(net::SYNCHRONOUS, | 305 read_list.push_back(net::MockRead(net::SYNCHRONOUS, |
| 304 net::ERR_IO_PENDING)); | 306 net::ERR_IO_PENDING)); |
| 305 BuildSocket(read_list, write_list); | 307 BuildSocket(read_list, write_list); |
| 306 | 308 |
| 307 ScopedMessage received_message; | 309 ScopedMessage received_message; |
| 308 Connect(&received_message); | 310 Connect(&received_message); |
| 309 WaitForMessage(); // The login send. | 311 WaitForMessage(); // The login send. |
| 310 WaitForMessage(); // The login response. Should result in a connection error. | 312 WaitForMessage(); // The login response. Should result in a connection error. |
| 311 EXPECT_FALSE(received_message.get()); | 313 EXPECT_FALSE(received_message.get()); |
| 312 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 314 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 313 EXPECT_EQ(net::ERR_TIMED_OUT, last_error()); | 315 EXPECT_EQ(net::ERR_TIMED_OUT, last_error()); |
| 314 } | 316 } |
| 315 | 317 |
| 316 // Reinitialize the connection handler after failing to initialize. | 318 // Reinitialize the connection handler after failing to initialize. |
| 317 TEST_F(GCMConnectionHandlerTest, ReInit) { | 319 TEST_F(GCMConnectionHandlerImplTest, ReInit) { |
| 318 std::string handshake_request = EncodeHandshakeRequest(); | 320 std::string handshake_request = EncodeHandshakeRequest(); |
| 319 WriteList write_list(1, net::MockWrite(net::ASYNC, | 321 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 320 handshake_request.c_str(), | 322 handshake_request.c_str(), |
| 321 handshake_request.size())); | 323 handshake_request.size())); |
| 322 ReadList read_list(1, net::MockRead(net::SYNCHRONOUS, | 324 ReadList read_list(1, net::MockRead(net::SYNCHRONOUS, |
| 323 net::ERR_IO_PENDING)); | 325 net::ERR_IO_PENDING)); |
| 324 BuildSocket(read_list, write_list); | 326 BuildSocket(read_list, write_list); |
| 325 | 327 |
| 326 ScopedMessage received_message; | 328 ScopedMessage received_message; |
| 327 Connect(&received_message); | 329 Connect(&received_message); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 340 Connect(&received_message); | 342 Connect(&received_message); |
| 341 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 343 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 342 WaitForMessage(); // The login send. | 344 WaitForMessage(); // The login send. |
| 343 WaitForMessage(); // The login response. | 345 WaitForMessage(); // The login response. |
| 344 ASSERT_TRUE(received_message.get()); | 346 ASSERT_TRUE(received_message.get()); |
| 345 EXPECT_EQ(BuildLoginResponse(), received_message->SerializeAsString()); | 347 EXPECT_EQ(BuildLoginResponse(), received_message->SerializeAsString()); |
| 346 EXPECT_TRUE(connection_handler()->CanSendMessage()); | 348 EXPECT_TRUE(connection_handler()->CanSendMessage()); |
| 347 } | 349 } |
| 348 | 350 |
| 349 // Verify that messages can be received after initialization. | 351 // Verify that messages can be received after initialization. |
| 350 TEST_F(GCMConnectionHandlerTest, RecvMsg) { | 352 TEST_F(GCMConnectionHandlerImplTest, RecvMsg) { |
| 351 std::string handshake_request = EncodeHandshakeRequest(); | 353 std::string handshake_request = EncodeHandshakeRequest(); |
| 352 WriteList write_list(1, net::MockWrite(net::ASYNC, | 354 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 353 handshake_request.c_str(), | 355 handshake_request.c_str(), |
| 354 handshake_request.size())); | 356 handshake_request.size())); |
| 355 std::string handshake_response = EncodeHandshakeResponse(); | 357 std::string handshake_response = EncodeHandshakeResponse(); |
| 356 | 358 |
| 357 std::string data_message_proto = BuildDataMessage(kDataMsgFrom, | 359 std::string data_message_proto = BuildDataMessage(kDataMsgFrom, |
| 358 kDataMsgCategory); | 360 kDataMsgCategory); |
| 359 std::string data_message_pkt = | 361 std::string data_message_pkt = |
| 360 EncodePacket(kDataMessageStanzaTag, data_message_proto); | 362 EncodePacket(kDataMessageStanzaTag, data_message_proto); |
| 361 ReadList read_list; | 363 ReadList read_list; |
| 362 read_list.push_back(net::MockRead(net::ASYNC, | 364 read_list.push_back(net::MockRead(net::ASYNC, |
| 363 handshake_response.c_str(), | 365 handshake_response.c_str(), |
| 364 handshake_response.size())); | 366 handshake_response.size())); |
| 365 read_list.push_back(net::MockRead(net::ASYNC, | 367 read_list.push_back(net::MockRead(net::ASYNC, |
| 366 data_message_pkt.c_str(), | 368 data_message_pkt.c_str(), |
| 367 data_message_pkt.size())); | 369 data_message_pkt.size())); |
| 368 BuildSocket(read_list, write_list); | 370 BuildSocket(read_list, write_list); |
| 369 | 371 |
| 370 ScopedMessage received_message; | 372 ScopedMessage received_message; |
| 371 Connect(&received_message); | 373 Connect(&received_message); |
| 372 WaitForMessage(); // The login send. | 374 WaitForMessage(); // The login send. |
| 373 WaitForMessage(); // The login response. | 375 WaitForMessage(); // The login response. |
| 374 WaitForMessage(); // The data message. | 376 WaitForMessage(); // The data message. |
| 375 ASSERT_TRUE(received_message.get()); | 377 ASSERT_TRUE(received_message.get()); |
| 376 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); | 378 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); |
| 377 } | 379 } |
| 378 | 380 |
| 379 // Verify that if two messages arrive at once, they're treated appropriately. | 381 // Verify that if two messages arrive at once, they're treated appropriately. |
| 380 TEST_F(GCMConnectionHandlerTest, Recv2Msgs) { | 382 TEST_F(GCMConnectionHandlerImplTest, Recv2Msgs) { |
| 381 std::string handshake_request = EncodeHandshakeRequest(); | 383 std::string handshake_request = EncodeHandshakeRequest(); |
| 382 WriteList write_list(1, net::MockWrite(net::ASYNC, | 384 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 383 handshake_request.c_str(), | 385 handshake_request.c_str(), |
| 384 handshake_request.size())); | 386 handshake_request.size())); |
| 385 std::string handshake_response = EncodeHandshakeResponse(); | 387 std::string handshake_response = EncodeHandshakeResponse(); |
| 386 | 388 |
| 387 std::string data_message_proto = BuildDataMessage(kDataMsgFrom, | 389 std::string data_message_proto = BuildDataMessage(kDataMsgFrom, |
| 388 kDataMsgCategory); | 390 kDataMsgCategory); |
| 389 std::string data_message_proto2 = BuildDataMessage(kDataMsgFrom2, | 391 std::string data_message_proto2 = BuildDataMessage(kDataMsgFrom2, |
| 390 kDataMsgCategory2); | 392 kDataMsgCategory2); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 407 WaitForMessage(); // The first data message. | 409 WaitForMessage(); // The first data message. |
| 408 ASSERT_TRUE(received_message.get()); | 410 ASSERT_TRUE(received_message.get()); |
| 409 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); | 411 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); |
| 410 received_message.reset(); | 412 received_message.reset(); |
| 411 WaitForMessage(); // The second data message. | 413 WaitForMessage(); // The second data message. |
| 412 ASSERT_TRUE(received_message.get()); | 414 ASSERT_TRUE(received_message.get()); |
| 413 EXPECT_EQ(data_message_proto2, received_message->SerializeAsString()); | 415 EXPECT_EQ(data_message_proto2, received_message->SerializeAsString()); |
| 414 } | 416 } |
| 415 | 417 |
| 416 // Receive a long (>128 bytes) message. | 418 // Receive a long (>128 bytes) message. |
| 417 TEST_F(GCMConnectionHandlerTest, RecvLongMsg) { | 419 TEST_F(GCMConnectionHandlerImplTest, RecvLongMsg) { |
| 418 std::string handshake_request = EncodeHandshakeRequest(); | 420 std::string handshake_request = EncodeHandshakeRequest(); |
| 419 WriteList write_list(1, net::MockWrite(net::ASYNC, | 421 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 420 handshake_request.c_str(), | 422 handshake_request.c_str(), |
| 421 handshake_request.size())); | 423 handshake_request.size())); |
| 422 std::string handshake_response = EncodeHandshakeResponse(); | 424 std::string handshake_response = EncodeHandshakeResponse(); |
| 423 | 425 |
| 424 std::string data_message_proto = | 426 std::string data_message_proto = |
| 425 BuildDataMessage(kDataMsgFromLong, kDataMsgCategoryLong); | 427 BuildDataMessage(kDataMsgFromLong, kDataMsgCategoryLong); |
| 426 std::string data_message_pkt = | 428 std::string data_message_pkt = |
| 427 EncodePacket(kDataMessageStanzaTag, data_message_proto); | 429 EncodePacket(kDataMessageStanzaTag, data_message_proto); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 438 ScopedMessage received_message; | 440 ScopedMessage received_message; |
| 439 Connect(&received_message); | 441 Connect(&received_message); |
| 440 WaitForMessage(); // The login send. | 442 WaitForMessage(); // The login send. |
| 441 WaitForMessage(); // The login response. | 443 WaitForMessage(); // The login response. |
| 442 WaitForMessage(); // The data message. | 444 WaitForMessage(); // The data message. |
| 443 ASSERT_TRUE(received_message.get()); | 445 ASSERT_TRUE(received_message.get()); |
| 444 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); | 446 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); |
| 445 } | 447 } |
| 446 | 448 |
| 447 // Receive two long (>128 bytes) message. | 449 // Receive two long (>128 bytes) message. |
| 448 TEST_F(GCMConnectionHandlerTest, Recv2LongMsgs) { | 450 TEST_F(GCMConnectionHandlerImplTest, Recv2LongMsgs) { |
| 449 std::string handshake_request = EncodeHandshakeRequest(); | 451 std::string handshake_request = EncodeHandshakeRequest(); |
| 450 WriteList write_list(1, net::MockWrite(net::ASYNC, | 452 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 451 handshake_request.c_str(), | 453 handshake_request.c_str(), |
| 452 handshake_request.size())); | 454 handshake_request.size())); |
| 453 std::string handshake_response = EncodeHandshakeResponse(); | 455 std::string handshake_response = EncodeHandshakeResponse(); |
| 454 | 456 |
| 455 std::string data_message_proto = | 457 std::string data_message_proto = |
| 456 BuildDataMessage(kDataMsgFromLong, kDataMsgCategoryLong); | 458 BuildDataMessage(kDataMsgFromLong, kDataMsgCategoryLong); |
| 457 std::string data_message_proto2 = | 459 std::string data_message_proto2 = |
| 458 BuildDataMessage(kDataMsgFromLong2, kDataMsgCategoryLong2); | 460 BuildDataMessage(kDataMsgFromLong2, kDataMsgCategoryLong2); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 477 ASSERT_TRUE(received_message.get()); | 479 ASSERT_TRUE(received_message.get()); |
| 478 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); | 480 EXPECT_EQ(data_message_proto, received_message->SerializeAsString()); |
| 479 received_message.reset(); | 481 received_message.reset(); |
| 480 WaitForMessage(); // The second data message. | 482 WaitForMessage(); // The second data message. |
| 481 ASSERT_TRUE(received_message.get()); | 483 ASSERT_TRUE(received_message.get()); |
| 482 EXPECT_EQ(data_message_proto2, received_message->SerializeAsString()); | 484 EXPECT_EQ(data_message_proto2, received_message->SerializeAsString()); |
| 483 } | 485 } |
| 484 | 486 |
| 485 // Simulate a message where the end of the data does not arrive in time and the | 487 // Simulate a message where the end of the data does not arrive in time and the |
| 486 // read times out. | 488 // read times out. |
| 487 TEST_F(GCMConnectionHandlerTest, ReadTimeout) { | 489 TEST_F(GCMConnectionHandlerImplTest, ReadTimeout) { |
| 488 std::string handshake_request = EncodeHandshakeRequest(); | 490 std::string handshake_request = EncodeHandshakeRequest(); |
| 489 WriteList write_list(1, net::MockWrite(net::ASYNC, | 491 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 490 handshake_request.c_str(), | 492 handshake_request.c_str(), |
| 491 handshake_request.size())); | 493 handshake_request.size())); |
| 492 std::string handshake_response = EncodeHandshakeResponse(); | 494 std::string handshake_response = EncodeHandshakeResponse(); |
| 493 | 495 |
| 494 std::string data_message_proto = BuildDataMessage(kDataMsgFrom, | 496 std::string data_message_proto = BuildDataMessage(kDataMsgFrom, |
| 495 kDataMsgCategory); | 497 kDataMsgCategory); |
| 496 std::string data_message_pkt = | 498 std::string data_message_pkt = |
| 497 EncodePacket(kDataMessageStanzaTag, data_message_proto); | 499 EncodePacket(kDataMessageStanzaTag, data_message_proto); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 520 WaitForMessage(); // Should time out. | 522 WaitForMessage(); // Should time out. |
| 521 EXPECT_FALSE(received_message.get()); | 523 EXPECT_FALSE(received_message.get()); |
| 522 EXPECT_EQ(net::ERR_TIMED_OUT, last_error()); | 524 EXPECT_EQ(net::ERR_TIMED_OUT, last_error()); |
| 523 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 525 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 524 | 526 |
| 525 // Finish the socket read. Should have no effect. | 527 // Finish the socket read. Should have no effect. |
| 526 data_provider()->ForceNextRead(); | 528 data_provider()->ForceNextRead(); |
| 527 } | 529 } |
| 528 | 530 |
| 529 // Receive a message with zero data bytes. | 531 // Receive a message with zero data bytes. |
| 530 TEST_F(GCMConnectionHandlerTest, RecvMsgNoData) { | 532 TEST_F(GCMConnectionHandlerImplTest, RecvMsgNoData) { |
| 531 std::string handshake_request = EncodeHandshakeRequest(); | 533 std::string handshake_request = EncodeHandshakeRequest(); |
| 532 WriteList write_list(1, net::MockWrite(net::ASYNC, | 534 WriteList write_list(1, net::MockWrite(net::ASYNC, |
| 533 handshake_request.c_str(), | 535 handshake_request.c_str(), |
| 534 handshake_request.size())); | 536 handshake_request.size())); |
| 535 std::string handshake_response = EncodeHandshakeResponse(); | 537 std::string handshake_response = EncodeHandshakeResponse(); |
| 536 | 538 |
| 537 std::string data_message_pkt = EncodePacket(kHeartbeatPingTag, ""); | 539 std::string data_message_pkt = EncodePacket(kHeartbeatPingTag, ""); |
| 538 ASSERT_EQ(data_message_pkt.size(), 2U); | 540 ASSERT_EQ(data_message_pkt.size(), 2U); |
| 539 ReadList read_list; | 541 ReadList read_list; |
| 540 read_list.push_back(net::MockRead(net::ASYNC, | 542 read_list.push_back(net::MockRead(net::ASYNC, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 551 WaitForMessage(); // The login response. | 553 WaitForMessage(); // The login response. |
| 552 received_message.reset(); | 554 received_message.reset(); |
| 553 WaitForMessage(); // The heartbeat ping. | 555 WaitForMessage(); // The heartbeat ping. |
| 554 EXPECT_TRUE(received_message.get()); | 556 EXPECT_TRUE(received_message.get()); |
| 555 EXPECT_EQ(GetMCSProtoTag(*received_message), kHeartbeatPingTag); | 557 EXPECT_EQ(GetMCSProtoTag(*received_message), kHeartbeatPingTag); |
| 556 EXPECT_EQ(net::OK, last_error()); | 558 EXPECT_EQ(net::OK, last_error()); |
| 557 EXPECT_TRUE(connection_handler()->CanSendMessage()); | 559 EXPECT_TRUE(connection_handler()->CanSendMessage()); |
| 558 } | 560 } |
| 559 | 561 |
| 560 // Send a message after performing the handshake. | 562 // Send a message after performing the handshake. |
| 561 TEST_F(GCMConnectionHandlerTest, SendMsg) { | 563 TEST_F(GCMConnectionHandlerImplTest, SendMsg) { |
| 562 mcs_proto::DataMessageStanza data_message; | 564 mcs_proto::DataMessageStanza data_message; |
| 563 data_message.set_from(kDataMsgFrom); | 565 data_message.set_from(kDataMsgFrom); |
| 564 data_message.set_category(kDataMsgCategory); | 566 data_message.set_category(kDataMsgCategory); |
| 565 std::string handshake_request = EncodeHandshakeRequest(); | 567 std::string handshake_request = EncodeHandshakeRequest(); |
| 566 std::string data_message_pkt = | 568 std::string data_message_pkt = |
| 567 EncodePacket(kDataMessageStanzaTag, data_message.SerializeAsString()); | 569 EncodePacket(kDataMessageStanzaTag, data_message.SerializeAsString()); |
| 568 WriteList write_list; | 570 WriteList write_list; |
| 569 write_list.push_back(net::MockWrite(net::ASYNC, | 571 write_list.push_back(net::MockWrite(net::ASYNC, |
| 570 handshake_request.c_str(), | 572 handshake_request.c_str(), |
| 571 handshake_request.size())); | 573 handshake_request.size())); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 585 WaitForMessage(); // The login send. | 587 WaitForMessage(); // The login send. |
| 586 WaitForMessage(); // The login response. | 588 WaitForMessage(); // The login response. |
| 587 EXPECT_TRUE(connection_handler()->CanSendMessage()); | 589 EXPECT_TRUE(connection_handler()->CanSendMessage()); |
| 588 connection_handler()->SendMessage(data_message); | 590 connection_handler()->SendMessage(data_message); |
| 589 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 591 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 590 WaitForMessage(); // The message send. | 592 WaitForMessage(); // The message send. |
| 591 EXPECT_TRUE(connection_handler()->CanSendMessage()); | 593 EXPECT_TRUE(connection_handler()->CanSendMessage()); |
| 592 } | 594 } |
| 593 | 595 |
| 594 // Attempt to send a message after the socket is disconnected due to a timeout. | 596 // Attempt to send a message after the socket is disconnected due to a timeout. |
| 595 TEST_F(GCMConnectionHandlerTest, SendMsgSocketDisconnected) { | 597 TEST_F(GCMConnectionHandlerImplTest, SendMsgSocketDisconnected) { |
| 596 std::string handshake_request = EncodeHandshakeRequest(); | 598 std::string handshake_request = EncodeHandshakeRequest(); |
| 597 WriteList write_list; | 599 WriteList write_list; |
| 598 write_list.push_back(net::MockWrite(net::ASYNC, | 600 write_list.push_back(net::MockWrite(net::ASYNC, |
| 599 handshake_request.c_str(), | 601 handshake_request.c_str(), |
| 600 handshake_request.size())); | 602 handshake_request.size())); |
| 601 std::string handshake_response = EncodeHandshakeResponse(); | 603 std::string handshake_response = EncodeHandshakeResponse(); |
| 602 ReadList read_list; | 604 ReadList read_list; |
| 603 read_list.push_back(net::MockRead(net::ASYNC, | 605 read_list.push_back(net::MockRead(net::ASYNC, |
| 604 handshake_response.c_str(), | 606 handshake_response.c_str(), |
| 605 handshake_response.size())); | 607 handshake_response.size())); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 617 data_message.set_category(kDataMsgCategory); | 619 data_message.set_category(kDataMsgCategory); |
| 618 connection_handler()->SendMessage(data_message); | 620 connection_handler()->SendMessage(data_message); |
| 619 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 621 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 620 WaitForMessage(); // The message send. Should result in an error | 622 WaitForMessage(); // The message send. Should result in an error |
| 621 EXPECT_FALSE(connection_handler()->CanSendMessage()); | 623 EXPECT_FALSE(connection_handler()->CanSendMessage()); |
| 622 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, last_error()); | 624 EXPECT_EQ(net::ERR_CONNECTION_CLOSED, last_error()); |
| 623 } | 625 } |
| 624 | 626 |
| 625 } // namespace | 627 } // namespace |
| 626 } // namespace gcm | 628 } // namespace gcm |
| OLD | NEW |