IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / renderer_host / p2p / socket_host_tcp_server_unittest.cc
blob39b864edb63aa0525388f180cf1e1f5d54dec452
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
3 // found in the LICENSE file.
5 #include "content/browser/renderer_host/p2p/socket_host_tcp_server.h"
7 #include <list>
9 #include "content/browser/renderer_host/p2p/socket_host_tcp.h"
10 #include "content/browser/renderer_host/p2p/socket_host_test_utils.h"
11 #include "net/base/completion_callback.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using ::testing::_;
16 using ::testing::DeleteArg;
17 using ::testing::DoAll;
18 using ::testing::Return;
20 namespace {
22 class FakeServerSocket : public net::ServerSocket {
23 public:
24 FakeServerSocket()
25 : listening_(false),
26 accept_socket_(NULL) {
29 virtual ~FakeServerSocket() {}
31 bool listening() { return listening_; }
33 void AddIncoming(net::StreamSocket* socket) {
34 if (!accept_callback_.is_null()) {
35 DCHECK(incoming_sockets_.empty());
36 accept_socket_->reset(socket);
37 accept_socket_ = NULL;
39 // This copy is necessary because this implementation of ServerSocket
40 // bases logic on the null-ness of |accept_callback_| in the bound
41 // callback.
42 net::CompletionCallback cb = accept_callback_;
43 accept_callback_.Reset();
44 cb.Run(net::OK);
45 } else {
46 incoming_sockets_.push_back(socket);
50 virtual int Listen(const net::IPEndPoint& address, int backlog) OVERRIDE {
51 local_address_ = address;
52 listening_ = true;
53 return net::OK;
56 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE {
57 *address = local_address_;
58 return net::OK;
61 virtual int Accept(scoped_ptr<net::StreamSocket>* socket,
62 const net::CompletionCallback& callback) OVERRIDE {
63 DCHECK(socket);
64 if (!incoming_sockets_.empty()) {
65 socket->reset(incoming_sockets_.front());
66 incoming_sockets_.pop_front();
67 return net::OK;
68 } else {
69 accept_socket_ = socket;
70 accept_callback_ = callback;
71 return net::ERR_IO_PENDING;
75 private:
76 bool listening_;
78 net::IPEndPoint local_address_;
80 scoped_ptr<net::StreamSocket>* accept_socket_;
81 net::CompletionCallback accept_callback_;
83 std::list<net::StreamSocket*> incoming_sockets_;
86 } // namespace
88 namespace content {
90 class P2PSocketHostTcpServerTest : public testing::Test {
91 protected:
92 virtual void SetUp() OVERRIDE {
93 socket_ = new FakeServerSocket();
94 socket_host_.reset(new P2PSocketHostTcpServer(
95 &sender_, 0, P2P_SOCKET_TCP_CLIENT));
96 socket_host_->socket_.reset(socket_);
98 EXPECT_CALL(sender_, Send(
99 MatchMessage(static_cast<uint32>(P2PMsg_OnSocketCreated::ID))))
100 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
102 socket_host_->Init(ParseAddress(kTestLocalIpAddress, 0),
103 ParseAddress(kTestIpAddress1, kTestPort1));
104 EXPECT_TRUE(socket_->listening());
107 // Needed by the chilt classes because only this class is a friend
108 // of P2PSocketHostTcp.
109 net::StreamSocket* GetSocketFormTcpSocketHost(P2PSocketHostTcp* host) {
110 return host->socket_.get();
113 MockIPCSender sender_;
114 FakeServerSocket* socket_; // Owned by |socket_host_|.
115 scoped_ptr<P2PSocketHostTcpServer> socket_host_;
118 // Accept incoming connection.
119 TEST_F(P2PSocketHostTcpServerTest, Accept) {
120 FakeSocket* incoming = new FakeSocket(NULL);
121 incoming->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1));
122 net::IPEndPoint addr = ParseAddress(kTestIpAddress1, kTestPort1);
123 incoming->SetPeerAddress(addr);
125 EXPECT_CALL(sender_, Send(MatchIncomingSocketMessage(addr)))
126 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
127 socket_->AddIncoming(incoming);
129 const int kAcceptedSocketId = 1;
131 scoped_ptr<P2PSocketHost> new_host(
132 socket_host_->AcceptIncomingTcpConnection(addr, kAcceptedSocketId));
133 ASSERT_TRUE(new_host.get() != NULL);
134 EXPECT_EQ(incoming, GetSocketFormTcpSocketHost(
135 reinterpret_cast<P2PSocketHostTcp*>(new_host.get())));
138 // Accept 2 simultaneous connections.
139 TEST_F(P2PSocketHostTcpServerTest, Accept2) {
140 FakeSocket* incoming1 = new FakeSocket(NULL);
141 incoming1->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1));
142 net::IPEndPoint addr1 = ParseAddress(kTestIpAddress1, kTestPort1);
143 incoming1->SetPeerAddress(addr1);
144 FakeSocket* incoming2 = new FakeSocket(NULL);
145 incoming2->SetLocalAddress(ParseAddress(kTestLocalIpAddress, kTestPort1));
146 net::IPEndPoint addr2 = ParseAddress(kTestIpAddress2, kTestPort2);
147 incoming2->SetPeerAddress(addr2);
149 EXPECT_CALL(sender_, Send(MatchIncomingSocketMessage(addr1)))
150 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
151 EXPECT_CALL(sender_, Send(MatchIncomingSocketMessage(addr2)))
152 .WillOnce(DoAll(DeleteArg<0>(), Return(true)));
153 socket_->AddIncoming(incoming1);
154 socket_->AddIncoming(incoming2);
156 const int kAcceptedSocketId1 = 1;
157 const int kAcceptedSocketId2 = 2;
159 scoped_ptr<P2PSocketHost> new_host1(
160 socket_host_->AcceptIncomingTcpConnection(addr1, kAcceptedSocketId1));
161 ASSERT_TRUE(new_host1.get() != NULL);
162 EXPECT_EQ(incoming1, GetSocketFormTcpSocketHost(
163 reinterpret_cast<P2PSocketHostTcp*>(new_host1.get())));
164 scoped_ptr<P2PSocketHost> new_host2(
165 socket_host_->AcceptIncomingTcpConnection(addr2, kAcceptedSocketId2));
166 ASSERT_TRUE(new_host2.get() != NULL);
167 EXPECT_EQ(incoming2, GetSocketFormTcpSocketHost(
168 reinterpret_cast<P2PSocketHostTcp*>(new_host2.get())));
171 } // namespace content