| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/quic/p2p/quic_p2p_session.h" | 5 #include "net/quic/p2p/quic_p2p_session.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char kTestSharedKey[] = "Shared key exchanged out of bound."; | 27 const char kTestSharedKey[] = "Shared key exchanged out of bound."; |
| 28 | 28 |
| 29 class FakeP2PDatagramSocket : public Socket { | 29 class FakeP2PDatagramSocket : public Socket { |
| 30 public: | 30 public: |
| 31 FakeP2PDatagramSocket() : weak_factory_(this) {} | 31 FakeP2PDatagramSocket() : weak_factory_(this) {} |
| 32 ~FakeP2PDatagramSocket() override {} | 32 ~FakeP2PDatagramSocket() override {} |
| 33 | 33 |
| 34 base::WeakPtr<FakeP2PDatagramSocket> GetWeakPtr() { |
| 35 return weak_factory_.GetWeakPtr(); |
| 36 } |
| 37 |
| 34 void ConnectWith(FakeP2PDatagramSocket* peer_socket) { | 38 void ConnectWith(FakeP2PDatagramSocket* peer_socket) { |
| 35 peer_socket_ = peer_socket->weak_factory_.GetWeakPtr(); | 39 peer_socket_ = peer_socket->GetWeakPtr(); |
| 36 peer_socket->peer_socket_ = weak_factory_.GetWeakPtr(); | 40 peer_socket->peer_socket_ = GetWeakPtr(); |
| 37 } | 41 } |
| 38 | 42 |
| 39 void SetReadError(int error) { | 43 void SetReadError(int error) { |
| 40 read_error_ = error; | 44 read_error_ = error; |
| 41 if (!read_callback_.is_null()) { | 45 if (!read_callback_.is_null()) { |
| 42 base::ResetAndReturn(&read_callback_).Run(error); | 46 base::ResetAndReturn(&read_callback_).Run(error); |
| 43 } | 47 } |
| 44 } | 48 } |
| 45 | 49 |
| 46 void SetWriteError(int error) { write_error_ = error; } | 50 void SetWriteError(int error) { write_error_ = error; } |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 net::QuicRandom::GetInstance()) { | 213 net::QuicRandom::GetInstance()) { |
| 210 // Simulate out-of-bound config handshake. | 214 // Simulate out-of-bound config handshake. |
| 211 CryptoHandshakeMessage hello_message; | 215 CryptoHandshakeMessage hello_message; |
| 212 config_.ToHandshakeMessage(&hello_message); | 216 config_.ToHandshakeMessage(&hello_message); |
| 213 std::string error_detail; | 217 std::string error_detail; |
| 214 EXPECT_EQ(QUIC_NO_ERROR, | 218 EXPECT_EQ(QUIC_NO_ERROR, |
| 215 config_.ProcessPeerHello(hello_message, CLIENT, &error_detail)); | 219 config_.ProcessPeerHello(hello_message, CLIENT, &error_detail)); |
| 216 } | 220 } |
| 217 | 221 |
| 218 void CreateSessions() { | 222 void CreateSessions() { |
| 219 socket1_ = new FakeP2PDatagramSocket(); | 223 scoped_ptr<FakeP2PDatagramSocket> socket1(new FakeP2PDatagramSocket()); |
| 220 socket2_ = new FakeP2PDatagramSocket(); | 224 scoped_ptr<FakeP2PDatagramSocket> socket2(new FakeP2PDatagramSocket()); |
| 221 socket1_->ConnectWith(socket2_); | 225 socket1->ConnectWith(socket2.get()); |
| 226 |
| 227 socket1_ = socket1->GetWeakPtr(); |
| 228 socket2_ = socket2->GetWeakPtr(); |
| 222 | 229 |
| 223 QuicP2PCryptoConfig crypto_config(kTestSharedKey); | 230 QuicP2PCryptoConfig crypto_config(kTestSharedKey); |
| 224 | 231 |
| 225 session1_ = CreateP2PSession(make_scoped_ptr(socket1_), crypto_config, | 232 session1_ = |
| 226 Perspective::IS_SERVER); | 233 CreateP2PSession(socket1.Pass(), crypto_config, Perspective::IS_SERVER); |
| 227 session2_ = CreateP2PSession(make_scoped_ptr(socket2_), crypto_config, | 234 session2_ = |
| 228 Perspective::IS_CLIENT); | 235 CreateP2PSession(socket2.Pass(), crypto_config, Perspective::IS_CLIENT); |
| 229 } | 236 } |
| 230 | 237 |
| 231 scoped_ptr<QuicP2PSession> CreateP2PSession(scoped_ptr<Socket> socket, | 238 scoped_ptr<QuicP2PSession> CreateP2PSession(scoped_ptr<Socket> socket, |
| 232 QuicP2PCryptoConfig crypto_config, | 239 QuicP2PCryptoConfig crypto_config, |
| 233 Perspective perspective) { | 240 Perspective perspective) { |
| 234 DefaultPacketWriterFactory writer_factory(socket.get()); | 241 DefaultPacketWriterFactory writer_factory(socket.get()); |
| 235 net::IPAddressNumber ip(net::kIPv4AddressSize, 0); | 242 net::IPAddressNumber ip(net::kIPv4AddressSize, 0); |
| 236 scoped_ptr<QuicConnection> quic_connection1( | 243 scoped_ptr<QuicConnection> quic_connection1( |
| 237 new QuicConnection(0, net::IPEndPoint(ip, 0), &quic_helper_, | 244 new QuicConnection(0, net::IPEndPoint(ip, 0), &quic_helper_, |
| 238 writer_factory, true /* owns_writer */, perspective, | 245 writer_factory, true /* owns_writer */, perspective, |
| 239 true /* is_secuire */, QuicSupportedVersions())); | 246 true /* is_secuire */, QuicSupportedVersions())); |
| 240 | 247 |
| 241 scoped_ptr<QuicP2PSession> result(new QuicP2PSession( | 248 scoped_ptr<QuicP2PSession> result(new QuicP2PSession( |
| 242 config_, crypto_config, quic_connection1.Pass(), socket.Pass())); | 249 config_, crypto_config, quic_connection1.Pass(), socket.Pass())); |
| 243 result->Initialize(); | 250 result->Initialize(); |
| 244 return result.Pass(); | 251 return result.Pass(); |
| 245 } | 252 } |
| 246 | 253 |
| 247 void TestStreamConnection(QuicP2PSession* from_session, | 254 void TestStreamConnection(QuicP2PSession* from_session, |
| 248 QuicP2PSession* to_session, | 255 QuicP2PSession* to_session, |
| 249 QuicStreamId expected_stream_id); | 256 QuicStreamId expected_stream_id); |
| 250 | 257 |
| 251 QuicClock quic_clock_; | 258 QuicClock quic_clock_; |
| 252 QuicConnectionHelper quic_helper_; | 259 QuicConnectionHelper quic_helper_; |
| 253 QuicConfig config_; | 260 QuicConfig config_; |
| 254 | 261 |
| 255 FakeP2PDatagramSocket* socket1_; | 262 base::WeakPtr<FakeP2PDatagramSocket> socket1_; |
| 256 scoped_ptr<QuicP2PSession> session1_; | 263 scoped_ptr<QuicP2PSession> session1_; |
| 257 | 264 |
| 258 FakeP2PDatagramSocket* socket2_; | 265 base::WeakPtr<FakeP2PDatagramSocket> socket2_; |
| 259 scoped_ptr<QuicP2PSession> session2_; | 266 scoped_ptr<QuicP2PSession> session2_; |
| 260 }; | 267 }; |
| 261 | 268 |
| 262 void QuicP2PSessionTest::OnWriteResult(int result) { | 269 void QuicP2PSessionTest::OnWriteResult(int result) { |
| 263 EXPECT_EQ(OK, result); | 270 EXPECT_EQ(OK, result); |
| 264 } | 271 } |
| 265 | 272 |
| 266 void QuicP2PSessionTest::TestStreamConnection(QuicP2PSession* from_session, | 273 void QuicP2PSessionTest::TestStreamConnection(QuicP2PSession* from_session, |
| 267 QuicP2PSession* to_session, | 274 QuicP2PSession* to_session, |
| 268 QuicStreamId expected_stream_id) { | 275 QuicStreamId expected_stream_id) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 TEST_F(QuicP2PSessionTest, ClientToServer) { | 318 TEST_F(QuicP2PSessionTest, ClientToServer) { |
| 312 CreateSessions(); | 319 CreateSessions(); |
| 313 TestStreamConnection(session2_.get(), session1_.get(), 3); | 320 TestStreamConnection(session2_.get(), session1_.get(), 3); |
| 314 } | 321 } |
| 315 | 322 |
| 316 TEST_F(QuicP2PSessionTest, ServerToClient) { | 323 TEST_F(QuicP2PSessionTest, ServerToClient) { |
| 317 CreateSessions(); | 324 CreateSessions(); |
| 318 TestStreamConnection(session1_.get(), session2_.get(), 2); | 325 TestStreamConnection(session1_.get(), session2_.get(), 2); |
| 319 } | 326 } |
| 320 | 327 |
| 328 TEST_F(QuicP2PSessionTest, DestroySocketWhenClosed) { |
| 329 CreateSessions(); |
| 330 |
| 331 // The socket must be destroyed when connection is closed. |
| 332 EXPECT_TRUE(socket1_); |
| 333 session1_->connection()->CloseConnection(net::QUIC_NO_ERROR, false); |
| 334 EXPECT_FALSE(socket1_); |
| 335 } |
| 336 |
| 321 TEST_F(QuicP2PSessionTest, TransportWriteError) { | 337 TEST_F(QuicP2PSessionTest, TransportWriteError) { |
| 322 CreateSessions(); | 338 CreateSessions(); |
| 323 | 339 |
| 324 TestP2PSessionDelegate session_delegate; | 340 TestP2PSessionDelegate session_delegate; |
| 325 session1_->SetDelegate(&session_delegate); | 341 session1_->SetDelegate(&session_delegate); |
| 326 | 342 |
| 327 QuicP2PStream* stream = session1_->CreateOutgoingDynamicStream(); | 343 QuicP2PStream* stream = session1_->CreateOutgoingDynamicStream(); |
| 328 EXPECT_TRUE(stream); | 344 EXPECT_TRUE(stream); |
| 329 TestP2PStreamDelegate stream_delegate; | 345 TestP2PStreamDelegate stream_delegate; |
| 330 stream->SetDelegate(&stream_delegate); | 346 stream->SetDelegate(&stream_delegate); |
| 331 EXPECT_EQ(2U, stream->id()); | 347 EXPECT_EQ(2U, stream->id()); |
| 332 | 348 |
| 333 socket1_->SetWriteError(ERR_INTERNET_DISCONNECTED); | 349 socket1_->SetWriteError(ERR_INTERNET_DISCONNECTED); |
| 334 | 350 |
| 335 const char kTestMessage[] = "Hi"; | 351 const char kTestMessage[] = "Hi"; |
| 336 stream->Write( | 352 stream->Write( |
| 337 std::string(kTestMessage), | 353 std::string(kTestMessage), |
| 338 base::Bind(&QuicP2PSessionTest::OnWriteResult, base::Unretained(this))); | 354 base::Bind(&QuicP2PSessionTest::OnWriteResult, base::Unretained(this))); |
| 339 | 355 |
| 340 base::RunLoop().RunUntilIdle(); | 356 base::RunLoop().RunUntilIdle(); |
| 341 | 357 |
| 342 EXPECT_TRUE(stream_delegate.is_closed()); | 358 EXPECT_TRUE(stream_delegate.is_closed()); |
| 343 EXPECT_EQ(QUIC_PACKET_WRITE_ERROR, stream_delegate.error()); | 359 EXPECT_EQ(QUIC_PACKET_WRITE_ERROR, stream_delegate.error()); |
| 344 EXPECT_TRUE(session_delegate.is_closed()); | 360 EXPECT_TRUE(session_delegate.is_closed()); |
| 345 EXPECT_EQ(QUIC_PACKET_WRITE_ERROR, session_delegate.error()); | 361 EXPECT_EQ(QUIC_PACKET_WRITE_ERROR, session_delegate.error()); |
| 362 |
| 363 // Verify that the socket was destroyed. |
| 364 EXPECT_FALSE(socket1_); |
| 346 } | 365 } |
| 347 | 366 |
| 348 TEST_F(QuicP2PSessionTest, TransportReceiveError) { | 367 TEST_F(QuicP2PSessionTest, TransportReceiveError) { |
| 349 CreateSessions(); | 368 CreateSessions(); |
| 350 | 369 |
| 351 TestP2PSessionDelegate session_delegate; | 370 TestP2PSessionDelegate session_delegate; |
| 352 session1_->SetDelegate(&session_delegate); | 371 session1_->SetDelegate(&session_delegate); |
| 353 | 372 |
| 354 QuicP2PStream* stream = session1_->CreateOutgoingDynamicStream(); | 373 QuicP2PStream* stream = session1_->CreateOutgoingDynamicStream(); |
| 355 EXPECT_TRUE(stream); | 374 EXPECT_TRUE(stream); |
| 356 TestP2PStreamDelegate stream_delegate; | 375 TestP2PStreamDelegate stream_delegate; |
| 357 stream->SetDelegate(&stream_delegate); | 376 stream->SetDelegate(&stream_delegate); |
| 358 EXPECT_EQ(2U, stream->id()); | 377 EXPECT_EQ(2U, stream->id()); |
| 359 | 378 |
| 360 socket1_->SetReadError(ERR_INTERNET_DISCONNECTED); | 379 socket1_->SetReadError(ERR_INTERNET_DISCONNECTED); |
| 361 | 380 |
| 362 base::RunLoop().RunUntilIdle(); | 381 base::RunLoop().RunUntilIdle(); |
| 363 | 382 |
| 364 EXPECT_TRUE(stream_delegate.is_closed()); | 383 EXPECT_TRUE(stream_delegate.is_closed()); |
| 365 EXPECT_EQ(QUIC_PACKET_READ_ERROR, stream_delegate.error()); | 384 EXPECT_EQ(QUIC_PACKET_READ_ERROR, stream_delegate.error()); |
| 366 EXPECT_TRUE(session_delegate.is_closed()); | 385 EXPECT_TRUE(session_delegate.is_closed()); |
| 367 EXPECT_EQ(QUIC_PACKET_READ_ERROR, session_delegate.error()); | 386 EXPECT_EQ(QUIC_PACKET_READ_ERROR, session_delegate.error()); |
| 387 |
| 388 // Verify that the socket was destroyed. |
| 389 EXPECT_FALSE(socket1_); |
| 368 } | 390 } |
| 369 | 391 |
| 370 } // namespace net | 392 } // namespace net |
| OLD | NEW |