Add a stub __cxa_demangle to disable LLVM's demangler.
[chromium-blink-merge.git] / ppapi / tests / test_tcp_server_socket_private.cc
blob64b5402abe0f09ec5cd896350636da653929641a
1 // Copyright (c) 2012 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 "ppapi/tests/test_tcp_server_socket_private.h"
7 #include <cstdio>
8 #include <vector>
10 #include "ppapi/cpp/pass_ref.h"
11 #include "ppapi/cpp/private/net_address_private.h"
12 #include "ppapi/cpp/private/tcp_server_socket_private.h"
13 #include "ppapi/cpp/private/tcp_socket_private.h"
14 #include "ppapi/tests/test_utils.h"
15 #include "ppapi/tests/testing_instance.h"
17 using pp::NetAddressPrivate;
18 using pp::TCPServerSocketPrivate;
19 using pp::TCPSocketPrivate;
21 REGISTER_TEST_CASE(TCPServerSocketPrivate);
23 TestTCPServerSocketPrivate::TestTCPServerSocketPrivate(
24 TestingInstance* instance) : TestCase(instance) {
27 bool TestTCPServerSocketPrivate::Init() {
28 bool tcp_server_socket_private_is_available =
29 TCPServerSocketPrivate::IsAvailable();
30 if (!tcp_server_socket_private_is_available) {
31 instance_->AppendError(
32 "PPB_TCPServerSocket_Private interface not available");
35 bool tcp_socket_private_is_available = TCPSocketPrivate::IsAvailable();
36 if (!tcp_socket_private_is_available)
37 instance_->AppendError("PPB_TCPSocket_Private interface not available");
39 bool net_address_private_is_available = NetAddressPrivate::IsAvailable();
40 if (!net_address_private_is_available)
41 instance_->AppendError("PPB_NetAddress_Private interface not available");
43 bool init_host_port = GetLocalHostPort(instance_->pp_instance(),
44 &host_, &port_);
45 if (!init_host_port)
46 instance_->AppendError("Can't init host and port");
48 return tcp_server_socket_private_is_available &&
49 tcp_socket_private_is_available &&
50 net_address_private_is_available &&
51 init_host_port &&
52 CheckTestingInterface() &&
53 EnsureRunningOverHTTP();
56 void TestTCPServerSocketPrivate::RunTests(const std::string& filter) {
57 RUN_CALLBACK_TEST(TestTCPServerSocketPrivate, Listen, filter);
58 RUN_CALLBACK_TEST(TestTCPServerSocketPrivate, Backlog, filter);
61 std::string TestTCPServerSocketPrivate::GetLocalAddress(
62 PP_NetAddress_Private* address) {
63 TCPSocketPrivate socket(instance_);
64 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
65 callback.WaitForResult(
66 socket.Connect(host_.c_str(), port_, callback.GetCallback()));
67 CHECK_CALLBACK_BEHAVIOR(callback);
68 ASSERT_EQ(PP_OK, callback.result());
69 ASSERT_TRUE(socket.GetLocalAddress(address));
70 socket.Disconnect();
71 PASS();
74 std::string TestTCPServerSocketPrivate::SyncRead(TCPSocketPrivate* socket,
75 char* buffer,
76 size_t num_bytes) {
77 while (num_bytes > 0) {
78 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
79 callback.WaitForResult(
80 socket->Read(buffer, static_cast<int32_t>(num_bytes),
81 callback.GetCallback()));
82 CHECK_CALLBACK_BEHAVIOR(callback);
83 ASSERT_TRUE(callback.result() >= 0);
84 buffer += callback.result();
85 num_bytes -= callback.result();
87 PASS();
90 std::string TestTCPServerSocketPrivate::SyncWrite(TCPSocketPrivate* socket,
91 const char* buffer,
92 size_t num_bytes) {
93 while (num_bytes > 0) {
94 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
95 callback.WaitForResult(
96 socket->Write(buffer, static_cast<int32_t>(num_bytes),
97 callback.GetCallback()));
98 CHECK_CALLBACK_BEHAVIOR(callback);
99 ASSERT_TRUE(callback.result() >= 0);
100 buffer += callback.result();
101 num_bytes -= callback.result();
103 PASS();
106 std::string TestTCPServerSocketPrivate::SyncConnect(
107 TCPSocketPrivate* socket,
108 PP_NetAddress_Private* address) {
109 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
110 callback.WaitForResult(
111 socket->ConnectWithNetAddress(address, callback.GetCallback()));
112 CHECK_CALLBACK_BEHAVIOR(callback);
113 ASSERT_EQ(PP_OK, callback.result());
114 PASS();
117 void TestTCPServerSocketPrivate::ForceConnect(TCPSocketPrivate* socket,
118 PP_NetAddress_Private* address) {
119 std::string error_message;
120 do {
121 error_message = SyncConnect(socket, address);
122 } while (!error_message.empty());
125 std::string TestTCPServerSocketPrivate::SyncListen(
126 TCPServerSocketPrivate* socket,
127 PP_NetAddress_Private* address,
128 int32_t backlog) {
129 PP_NetAddress_Private base_address;
130 ASSERT_SUBTEST_SUCCESS(GetLocalAddress(&base_address));
131 if (!NetAddressPrivate::ReplacePort(base_address, 0, address))
132 return ReportError("PPB_NetAddress_Private::ReplacePort", 0);
133 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
134 callback.WaitForResult(
135 socket->Listen(address, backlog, callback.GetCallback()));
136 CHECK_CALLBACK_BEHAVIOR(callback);
137 ASSERT_EQ(PP_OK, callback.result());
138 int32_t rv = socket->GetLocalAddress(address);
139 ASSERT_EQ(PP_OK, rv);
140 ASSERT_TRUE(NetAddressPrivate::GetPort(*address) != 0);
141 PASS();
144 std::string TestTCPServerSocketPrivate::TestListen() {
145 static const int kBacklog = 2;
147 TCPServerSocketPrivate server_socket(instance_);
148 PP_NetAddress_Private address;
149 ASSERT_SUBTEST_SUCCESS(SyncListen(&server_socket, &address, kBacklog));
151 // We can't use a blocking callback for Accept, because it will wait forever
152 // for the client to connect, since the client connects after.
153 TestCompletionCallback accept_callback(instance_->pp_instance(), PP_REQUIRED);
154 // We need to make sure there's a message loop to run accept_callback on.
155 pp::MessageLoop current_thread_loop(pp::MessageLoop::GetCurrent());
156 if (current_thread_loop.is_null() && testing_interface_->IsOutOfProcess()) {
157 current_thread_loop = pp::MessageLoop(instance_);
158 current_thread_loop.AttachToCurrentThread();
161 PP_Resource resource;
162 int32_t accept_rv = server_socket.Accept(&resource,
163 accept_callback.GetCallback());
165 TCPSocketPrivate client_socket(instance_);
166 ForceConnect(&client_socket, &address);
168 PP_NetAddress_Private client_local_addr, client_remote_addr;
169 ASSERT_TRUE(client_socket.GetLocalAddress(&client_local_addr));
170 ASSERT_TRUE(client_socket.GetRemoteAddress(&client_remote_addr));
172 accept_callback.WaitForResult(accept_rv);
173 CHECK_CALLBACK_BEHAVIOR(accept_callback);
174 ASSERT_EQ(PP_OK, accept_callback.result());
176 ASSERT_TRUE(resource != 0);
177 TCPSocketPrivate accepted_socket(pp::PassRef(), resource);
178 PP_NetAddress_Private accepted_local_addr, accepted_remote_addr;
179 ASSERT_TRUE(accepted_socket.GetLocalAddress(&accepted_local_addr));
180 ASSERT_TRUE(accepted_socket.GetRemoteAddress(&accepted_remote_addr));
181 ASSERT_TRUE(NetAddressPrivate::AreEqual(client_local_addr,
182 accepted_remote_addr));
184 const char kSentByte = 'a';
185 ASSERT_SUBTEST_SUCCESS(SyncWrite(&client_socket,
186 &kSentByte,
187 sizeof(kSentByte)));
189 char received_byte;
190 ASSERT_SUBTEST_SUCCESS(SyncRead(&accepted_socket,
191 &received_byte,
192 sizeof(received_byte)));
193 ASSERT_EQ(kSentByte, received_byte);
195 accepted_socket.Disconnect();
196 client_socket.Disconnect();
197 server_socket.StopListening();
199 PASS();
202 std::string TestTCPServerSocketPrivate::TestBacklog() {
203 static const size_t kBacklog = 5;
205 TCPServerSocketPrivate server_socket(instance_);
206 PP_NetAddress_Private address;
207 ASSERT_SUBTEST_SUCCESS(SyncListen(&server_socket, &address, 2 * kBacklog));
209 std::vector<TCPSocketPrivate*> client_sockets(kBacklog);
210 std::vector<TestCompletionCallback*> connect_callbacks(kBacklog);
211 std::vector<int32_t> connect_rv(kBacklog);
212 for (size_t i = 0; i < kBacklog; ++i) {
213 client_sockets[i] = new TCPSocketPrivate(instance_);
214 connect_callbacks[i] = new TestCompletionCallback(instance_->pp_instance(),
215 callback_type());
216 connect_rv[i] = client_sockets[i]->ConnectWithNetAddress(
217 &address,
218 connect_callbacks[i]->GetCallback());
221 std::vector<PP_Resource> resources(kBacklog);
222 std::vector<TCPSocketPrivate*> accepted_sockets(kBacklog);
223 for (size_t i = 0; i < kBacklog; ++i) {
224 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
225 callback.WaitForResult(
226 server_socket.Accept(&resources[i], callback.GetCallback()));
227 CHECK_CALLBACK_BEHAVIOR(callback);
228 ASSERT_EQ(PP_OK, callback.result());
230 ASSERT_TRUE(resources[i] != 0);
231 accepted_sockets[i] = new TCPSocketPrivate(pp::PassRef(), resources[i]);
234 for (size_t i = 0; i < kBacklog; ++i) {
235 connect_callbacks[i]->WaitForResult(connect_rv[i]);
236 CHECK_CALLBACK_BEHAVIOR(*connect_callbacks[i]);
237 ASSERT_EQ(PP_OK, connect_callbacks[i]->result());
240 for (size_t i = 0; i < kBacklog; ++i) {
241 const char byte = static_cast<char>('a' + i);
242 ASSERT_SUBTEST_SUCCESS(SyncWrite(client_sockets[i], &byte, sizeof(byte)));
245 bool byte_received[kBacklog] = {};
246 for (size_t i = 0; i < kBacklog; ++i) {
247 char byte;
248 ASSERT_SUBTEST_SUCCESS(SyncRead(accepted_sockets[i], &byte, sizeof(byte)));
249 const size_t index = byte - 'a';
250 ASSERT_FALSE(byte_received[index]);
251 byte_received[index] = true;
254 for (size_t i = 0; i < kBacklog; ++i) {
255 client_sockets[i]->Disconnect();
256 delete client_sockets[i];
257 delete connect_callbacks[i];
258 accepted_sockets[i]->Disconnect();
259 delete accepted_sockets[i];
262 server_socket.StopListening();
263 PASS();