Remove extra line from unit_tests.isolate
[chromium-blink-merge.git] / ppapi / tests / test_udp_socket_private.cc
blob4680ff2a1390ff4cb091d6271262217cf6da383d
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 <cstring>
6 #include <vector>
8 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/private/net_address_private.h"
10 #include "ppapi/cpp/private/tcp_socket_private.h"
11 #include "ppapi/cpp/var.h"
12 #include "ppapi/tests/test_udp_socket_private.h"
13 #include "ppapi/tests/test_utils.h"
14 #include "ppapi/tests/testing_instance.h"
16 REGISTER_TEST_CASE(UDPSocketPrivate);
18 namespace {
20 const uint16_t kPortScanFrom = 1024;
21 const uint16_t kPortScanTo = 4096;
23 } // namespace
25 TestUDPSocketPrivate::TestUDPSocketPrivate(
26 TestingInstance* instance)
27 : TestCase(instance) {
30 bool TestUDPSocketPrivate::Init() {
31 bool tcp_socket_private_is_available = pp::TCPSocketPrivate::IsAvailable();
32 if (!tcp_socket_private_is_available)
33 instance_->AppendError("PPB_TCPSocket_Private interface not available");
35 bool udp_socket_private_is_available = pp::UDPSocketPrivate::IsAvailable();
36 if (!udp_socket_private_is_available)
37 instance_->AppendError("PPB_UDPSocket_Private interface not available");
39 bool net_address_private_is_available = pp::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_socket_private_is_available &&
49 udp_socket_private_is_available &&
50 net_address_private_is_available &&
51 init_host_port &&
52 CheckTestingInterface() &&
53 EnsureRunningOverHTTP();
56 void TestUDPSocketPrivate::RunTests(const std::string& filter) {
57 RUN_TEST_FORCEASYNC_AND_NOT(Connect, filter);
58 RUN_TEST_FORCEASYNC_AND_NOT(ConnectFailure, filter);
59 RUN_TEST_FORCEASYNC_AND_NOT(Broadcast, filter);
60 RUN_TEST_FORCEASYNC_AND_NOT(SetSocketFeatureErrors, filter);
63 std::string TestUDPSocketPrivate::GetLocalAddress(
64 PP_NetAddress_Private* address) {
65 pp::TCPSocketPrivate socket(instance_);
66 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
67 int32_t rv = socket.Connect(host_.c_str(), port_, callback);
68 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
69 return ReportError("PPB_TCPSocket_Private::Connect force_async", rv);
70 if (rv == PP_OK_COMPLETIONPENDING)
71 rv = callback.WaitForResult();
72 if (rv != PP_OK)
73 return ReportError("PPB_TCPSocket_Private::Connect", rv);
74 if (!socket.GetLocalAddress(address))
75 return "PPB_TCPSocket_Private::GetLocalAddress: Failed";
76 socket.Disconnect();
77 PASS();
80 std::string TestUDPSocketPrivate::SetBroadcastOptions(
81 pp::UDPSocketPrivate* socket) {
82 int32_t rv = socket->SetSocketFeature(PP_UDPSOCKETFEATURE_ADDRESS_REUSE,
83 pp::Var(true));
84 if (rv != PP_OK)
85 return ReportError("PPB_UDPSocket_Private::SetSocketFeature", rv);
87 rv = socket->SetSocketFeature(PP_UDPSOCKETFEATURE_BROADCAST, pp::Var(true));
88 if (rv != PP_OK)
89 return ReportError("PPB_UDPSocket_Private::SetSocketFeature", rv);
91 PASS();
94 std::string TestUDPSocketPrivate::BindUDPSocket(
95 pp::UDPSocketPrivate* socket,
96 PP_NetAddress_Private* address) {
97 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
98 int32_t rv = socket->Bind(address, callback);
99 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
100 return ReportError("PPB_UDPSocket_Private::Bind force_async", rv);
101 if (rv == PP_OK_COMPLETIONPENDING)
102 rv = callback.WaitForResult();
103 if (rv != PP_OK)
104 return ReportError("PPB_UDPSocket_Private::Bind", rv);
105 PASS();
108 std::string TestUDPSocketPrivate::LookupPortAndBindUDPSocket(
109 pp::UDPSocketPrivate* socket,
110 PP_NetAddress_Private *address) {
111 PP_NetAddress_Private base_address;
112 ASSERT_SUBTEST_SUCCESS(GetLocalAddress(&base_address));
114 bool is_free_port_found = false;
115 for (uint16_t port = kPortScanFrom; port < kPortScanTo; ++port) {
116 if (!pp::NetAddressPrivate::ReplacePort(base_address, port, address))
117 return "PPB_NetAddress_Private::ReplacePort: Failed";
118 if (BindUDPSocket(socket, address).empty()) {
119 is_free_port_found = true;
120 break;
123 if (!is_free_port_found)
124 return "Can't find available port";
125 if (!socket->GetBoundAddress(address))
126 return "PPB_UDPSocket_Private::GetBoundAddress: Failed";
127 PASS();
130 std::string TestUDPSocketPrivate::BindUDPSocketFailure(
131 pp::UDPSocketPrivate* socket,
132 PP_NetAddress_Private *address) {
133 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
134 int32_t rv = socket->Bind(address, callback);
135 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
136 return ReportError("PPB_UDPSocket_Private::Bind force_async", rv);
137 if (rv == PP_OK_COMPLETIONPENDING)
138 rv = callback.WaitForResult();
139 if (rv == PP_OK)
140 return ReportError("PPB_UDPSocket_Private::Bind", rv);
141 if (socket->GetBoundAddress(address))
142 return "PPB_UDPSocket_Private::GetBoundAddress: Failed";
143 PASS();
146 std::string TestUDPSocketPrivate::ReadSocket(pp::UDPSocketPrivate* socket,
147 PP_NetAddress_Private* address,
148 size_t size,
149 std::string* message) {
150 std::vector<char> buffer(size);
151 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
152 int32_t rv = socket->RecvFrom(&buffer[0], size, callback);
153 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
154 return ReportError("PPB_UDPSocket_Private::RecvFrom force_async", rv);
155 if (rv == PP_OK_COMPLETIONPENDING)
156 rv = callback.WaitForResult();
157 if (rv < 0 || size != static_cast<size_t>(rv))
158 return ReportError("PPB_UDPSocket_Private::RecvFrom", rv);
159 message->assign(buffer.begin(), buffer.end());
160 PASS();
163 std::string TestUDPSocketPrivate::PassMessage(pp::UDPSocketPrivate* target,
164 pp::UDPSocketPrivate* source,
165 PP_NetAddress_Private* address,
166 const std::string& message) {
167 TestCompletionCallback callback(instance_->pp_instance(), force_async_);
168 int32_t rv = source->SendTo(message.c_str(), message.size(), address,
169 callback);
170 if (force_async_ && rv != PP_OK_COMPLETIONPENDING)
171 return ReportError("PPB_UDPSocket_Private::SendTo force_async", rv);
173 std::string str;
174 ASSERT_SUBTEST_SUCCESS(ReadSocket(target, address, message.size(), &str));
176 if (rv == PP_OK_COMPLETIONPENDING)
177 rv = callback.WaitForResult();
178 if (rv < 0 || message.size() != static_cast<size_t>(rv))
179 return ReportError("PPB_UDPSocket_Private::SendTo", rv);
181 ASSERT_EQ(message, str);
182 PASS();
185 std::string TestUDPSocketPrivate::TestConnect() {
186 pp::UDPSocketPrivate server_socket(instance_), client_socket(instance_);
187 PP_NetAddress_Private server_address, client_address;
189 ASSERT_SUBTEST_SUCCESS(LookupPortAndBindUDPSocket(&server_socket,
190 &server_address));
191 ASSERT_SUBTEST_SUCCESS(LookupPortAndBindUDPSocket(&client_socket,
192 &client_address));
193 const std::string message = "Simple message that will be sent via UDP";
194 ASSERT_SUBTEST_SUCCESS(PassMessage(&server_socket, &client_socket,
195 &server_address,
196 message));
197 PP_NetAddress_Private recv_from_address;
198 ASSERT_TRUE(server_socket.GetRecvFromAddress(&recv_from_address));
199 ASSERT_TRUE(pp::NetAddressPrivate::AreEqual(recv_from_address,
200 client_address));
202 server_socket.Close();
203 client_socket.Close();
205 if (server_socket.GetBoundAddress(&server_address))
206 return "PPB_UDPSocket_Private::GetBoundAddress: expected Failure";
207 PASS();
210 std::string TestUDPSocketPrivate::TestConnectFailure() {
211 pp::UDPSocketPrivate socket(instance_);
212 PP_NetAddress_Private invalid_address = {};
214 std::string error_message = BindUDPSocketFailure(&socket, &invalid_address);
215 if (!error_message.empty())
216 return error_message;
218 PASS();
221 std::string TestUDPSocketPrivate::TestBroadcast() {
222 const uint8_t broadcast_ip[4] = { 0xff, 0xff, 0xff, 0xff };
224 pp::UDPSocketPrivate server1(instance_), server2(instance_);
226 ASSERT_SUBTEST_SUCCESS(SetBroadcastOptions(&server1));
227 ASSERT_SUBTEST_SUCCESS(SetBroadcastOptions(&server2));
228 PP_NetAddress_Private server_address;
229 ASSERT_TRUE(pp::NetAddressPrivate::GetAnyAddress(false, &server_address));
230 ASSERT_SUBTEST_SUCCESS(BindUDPSocket(&server1, &server_address));
231 // Fill port field of |server_address|.
232 ASSERT_TRUE(server1.GetBoundAddress(&server_address));
233 ASSERT_SUBTEST_SUCCESS(BindUDPSocket(&server2, &server_address));
235 const uint16_t port = pp::NetAddressPrivate::GetPort(server_address);
236 PP_NetAddress_Private broadcast_address;
237 ASSERT_TRUE(pp::NetAddressPrivate::CreateFromIPv4Address(
238 broadcast_ip, port, &broadcast_address));
240 std::string message;
241 const std::string first_message = "first message";
242 const std::string second_message = "second_message";
244 ASSERT_SUBTEST_SUCCESS(PassMessage(&server1, &server2,
245 &broadcast_address,
246 first_message));
247 // |first_message| also arrived to |server2|.
248 ASSERT_SUBTEST_SUCCESS(ReadSocket(&server2, &broadcast_address,
249 first_message.size(), &message));
250 ASSERT_EQ(first_message, message);
252 ASSERT_SUBTEST_SUCCESS(PassMessage(&server2, &server1,
253 &broadcast_address,
254 second_message));
255 // |second_message| also arrived to |server1|.
256 ASSERT_SUBTEST_SUCCESS(ReadSocket(&server1, &broadcast_address,
257 second_message.size(), &message));
258 ASSERT_EQ(second_message, message);
260 server1.Close();
261 server2.Close();
262 PASS();
265 std::string TestUDPSocketPrivate::TestSetSocketFeatureErrors() {
266 pp::UDPSocketPrivate socket(instance_);
267 // Try to pass incorrect feature name.
268 int32_t rv = socket.SetSocketFeature(PP_UDPSOCKETFEATURE_COUNT,
269 pp::Var(true));
270 ASSERT_EQ(PP_ERROR_BADARGUMENT, rv);
272 // Try to pass incorrect feature value's type.
273 rv = socket.SetSocketFeature(PP_UDPSOCKETFEATURE_ADDRESS_REUSE, pp::Var(1));
274 ASSERT_EQ(PP_ERROR_BADARGUMENT, rv);
275 PASS();