| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/renderer_host/p2p/socket_host_tcp_server.h" | 5 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h" |
| 6 | 6 |
| 7 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" | 7 #include "content/browser/renderer_host/p2p/socket_host_tcp.h" |
| 8 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" | 8 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 FakeServerSocket() | 21 FakeServerSocket() |
| 22 : listening_(false), | 22 : listening_(false), |
| 23 accept_socket_(NULL), | 23 accept_socket_(NULL), |
| 24 accept_callback_(NULL) { | 24 accept_callback_(NULL) { |
| 25 } | 25 } |
| 26 | 26 |
| 27 ~FakeServerSocket() { } | 27 ~FakeServerSocket() { } |
| 28 | 28 |
| 29 bool listening() { return listening_; } | 29 bool listening() { return listening_; } |
| 30 | 30 |
| 31 void AddIncoming(net::ClientSocket* socket) { | 31 void AddIncoming(net::StreamSocket* socket) { |
| 32 if (accept_callback_) { | 32 if (accept_callback_) { |
| 33 DCHECK(incoming_sockets_.empty()); | 33 DCHECK(incoming_sockets_.empty()); |
| 34 accept_socket_->reset(socket); | 34 accept_socket_->reset(socket); |
| 35 accept_socket_ = NULL; | 35 accept_socket_ = NULL; |
| 36 net::CompletionCallback* cb = accept_callback_; | 36 net::CompletionCallback* cb = accept_callback_; |
| 37 accept_callback_ = NULL; | 37 accept_callback_ = NULL; |
| 38 cb->Run(net::OK); | 38 cb->Run(net::OK); |
| 39 } else { | 39 } else { |
| 40 incoming_sockets_.push_back(socket); | 40 incoming_sockets_.push_back(socket); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 | 43 |
| 44 // net::ServerSocket implementation. | 44 // net::ServerSocket implementation. |
| 45 virtual int Listen(const net::IPEndPoint& address, int backlog) OVERRIDE { | 45 virtual int Listen(const net::IPEndPoint& address, int backlog) OVERRIDE { |
| 46 local_address_ = address; | 46 local_address_ = address; |
| 47 listening_ = true; | 47 listening_ = true; |
| 48 return net::OK; | 48 return net::OK; |
| 49 } | 49 } |
| 50 | 50 |
| 51 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { | 51 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE { |
| 52 *address = local_address_; | 52 *address = local_address_; |
| 53 return net::OK; | 53 return net::OK; |
| 54 } | 54 } |
| 55 | 55 |
| 56 virtual int Accept(scoped_ptr<net::ClientSocket>* socket, | 56 virtual int Accept(scoped_ptr<net::StreamSocket>* socket, |
| 57 net::CompletionCallback* callback) OVERRIDE { | 57 net::CompletionCallback* callback) OVERRIDE { |
| 58 DCHECK(socket); | 58 DCHECK(socket); |
| 59 if (!incoming_sockets_.empty()) { | 59 if (!incoming_sockets_.empty()) { |
| 60 socket->reset(incoming_sockets_.front()); | 60 socket->reset(incoming_sockets_.front()); |
| 61 incoming_sockets_.pop_front(); | 61 incoming_sockets_.pop_front(); |
| 62 return net::OK; | 62 return net::OK; |
| 63 } else { | 63 } else { |
| 64 accept_socket_ = socket; | 64 accept_socket_ = socket; |
| 65 accept_callback_ = callback; | 65 accept_callback_ = callback; |
| 66 return net::ERR_IO_PENDING; | 66 return net::ERR_IO_PENDING; |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 private: | 70 private: |
| 71 bool listening_; | 71 bool listening_; |
| 72 | 72 |
| 73 net::IPEndPoint local_address_; | 73 net::IPEndPoint local_address_; |
| 74 | 74 |
| 75 scoped_ptr<net::ClientSocket>* accept_socket_; | 75 scoped_ptr<net::StreamSocket>* accept_socket_; |
| 76 net::CompletionCallback* accept_callback_; | 76 net::CompletionCallback* accept_callback_; |
| 77 | 77 |
| 78 std::list<net::ClientSocket*> incoming_sockets_; | 78 std::list<net::StreamSocket*> incoming_sockets_; |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 } // namespace | 81 } // namespace |
| 82 | 82 |
| 83 class P2PSocketHostTcpServerTest : public testing::Test { | 83 class P2PSocketHostTcpServerTest : public testing::Test { |
| 84 protected: | 84 protected: |
| 85 virtual void SetUp() OVERRIDE { | 85 virtual void SetUp() OVERRIDE { |
| 86 socket_ = new FakeServerSocket(); | 86 socket_ = new FakeServerSocket(); |
| 87 socket_host_.reset(new P2PSocketHostTcpServer(&sender_, 0, 0)); | 87 socket_host_.reset(new P2PSocketHostTcpServer(&sender_, 0, 0)); |
| 88 socket_host_->socket_.reset(socket_); | 88 socket_host_->socket_.reset(socket_); |
| 89 | 89 |
| 90 EXPECT_CALL(sender_, Send( | 90 EXPECT_CALL(sender_, Send( |
| 91 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) | 91 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID)))) |
| 92 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); | 92 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 93 | 93 |
| 94 socket_host_->Init(ParseAddress(kTestLocalIpAddress, 0), | 94 socket_host_->Init(ParseAddress(kTestLocalIpAddress, 0), |
| 95 ParseAddress(kTestIpAddress1, kTestPort1)); | 95 ParseAddress(kTestIpAddress1, kTestPort1)); |
| 96 EXPECT_TRUE(socket_->listening()); | 96 EXPECT_TRUE(socket_->listening()); |
| 97 } | 97 } |
| 98 | 98 |
| 99 // Needed by the chilt classes because only this class is a friend | 99 // Needed by the chilt classes because only this class is a friend |
| 100 // of P2PSocketHostTcp. | 100 // of P2PSocketHostTcp. |
| 101 net::ClientSocket* GetSocketFormTcpSocketHost(P2PSocketHostTcp* host) { | 101 net::StreamSocket* GetSocketFormTcpSocketHost(P2PSocketHostTcp* host) { |
| 102 return host->socket_.get(); | 102 return host->socket_.get(); |
| 103 } | 103 } |
| 104 | 104 |
| 105 MockIPCSender sender_; | 105 MockIPCSender sender_; |
| 106 FakeServerSocket* socket_; // Owned by |socket_host_|. | 106 FakeServerSocket* socket_; // Owned by |socket_host_|. |
| 107 scoped_ptr<P2PSocketHostTcpServer> socket_host_; | 107 scoped_ptr<P2PSocketHostTcpServer> socket_host_; |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 // Accept incoming connection. | 110 // Accept incoming connection. |
| 111 TEST_F(P2PSocketHostTcpServerTest, Accept) { | 111 TEST_F(P2PSocketHostTcpServerTest, Accept) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 socket_host_->AcceptIncomingTcpConnection(addr1, kAcceptedSocketId1)); | 152 socket_host_->AcceptIncomingTcpConnection(addr1, kAcceptedSocketId1)); |
| 153 ASSERT_TRUE(new_host1.get() != NULL); | 153 ASSERT_TRUE(new_host1.get() != NULL); |
| 154 EXPECT_EQ(incoming1, GetSocketFormTcpSocketHost( | 154 EXPECT_EQ(incoming1, GetSocketFormTcpSocketHost( |
| 155 reinterpret_cast<P2PSocketHostTcp*>(new_host1.get()))); | 155 reinterpret_cast<P2PSocketHostTcp*>(new_host1.get()))); |
| 156 scoped_ptr<P2PSocketHost> new_host2( | 156 scoped_ptr<P2PSocketHost> new_host2( |
| 157 socket_host_->AcceptIncomingTcpConnection(addr2, kAcceptedSocketId2)); | 157 socket_host_->AcceptIncomingTcpConnection(addr2, kAcceptedSocketId2)); |
| 158 ASSERT_TRUE(new_host2.get() != NULL); | 158 ASSERT_TRUE(new_host2.get() != NULL); |
| 159 EXPECT_EQ(incoming2, GetSocketFormTcpSocketHost( | 159 EXPECT_EQ(incoming2, GetSocketFormTcpSocketHost( |
| 160 reinterpret_cast<P2PSocketHostTcp*>(new_host2.get()))); | 160 reinterpret_cast<P2PSocketHostTcp*>(new_host2.get()))); |
| 161 } | 161 } |
| OLD | NEW |