Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(760)

Side by Side Diff: ppapi/tests/test_udp_socket.cc

Issue 704133005: Pepper: Add support for multicast in PPB_UDPSocket API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unit tests, thunk code auto generated Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "ppapi/tests/test_udp_socket.h" 5 #include "ppapi/tests/test_udp_socket.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "ppapi/cpp/pass_ref.h" 9 #include "ppapi/cpp/pass_ref.h"
10 #include "ppapi/cpp/tcp_socket.h" 10 #include "ppapi/cpp/tcp_socket.h"
(...skipping 29 matching lines...) Expand all
40 } 40 }
41 default: { 41 default: {
42 PP_NOTREACHED(); 42 PP_NOTREACHED();
43 } 43 }
44 } 44 }
45 return pp::NetAddress(); 45 return pp::NetAddress();
46 } 46 }
47 47
48 } // namespace 48 } // namespace
49 49
50 TestUDPSocket::TestUDPSocket(TestingInstance* instance) : TestCase(instance) { 50 TestUDPSocket::TestUDPSocket(TestingInstance* instance)
51 : TestCase(instance),
52 socket_interface_1_0_(NULL),
53 socket_interface_1_1_(NULL),
54 has_interface_1_2_(false) {
51 } 55 }
52 56
53 bool TestUDPSocket::Init() { 57 bool TestUDPSocket::Init() {
54 bool tcp_socket_is_available = pp::TCPSocket::IsAvailable(); 58 bool tcp_socket_is_available = pp::TCPSocket::IsAvailable();
55 if (!tcp_socket_is_available) 59 if (!tcp_socket_is_available)
56 instance_->AppendError("PPB_TCPSocket interface not available"); 60 instance_->AppendError("PPB_TCPSocket interface not available");
57 61
58 bool udp_socket_is_available = pp::UDPSocket::IsAvailable(); 62 bool udp_socket_is_available = pp::UDPSocket::IsAvailable();
59 if (!udp_socket_is_available) 63 if (!udp_socket_is_available)
60 instance_->AppendError("PPB_UDPSocket interface not available"); 64 instance_->AppendError("PPB_UDPSocket interface not available");
61 65
62 bool net_address_is_available = pp::NetAddress::IsAvailable(); 66 bool net_address_is_available = pp::NetAddress::IsAvailable();
63 if (!net_address_is_available) 67 if (!net_address_is_available)
64 instance_->AppendError("PPB_NetAddress interface not available"); 68 instance_->AppendError("PPB_NetAddress interface not available");
65 69
66 std::string host; 70 std::string host;
67 uint16_t port = 0; 71 uint16_t port = 0;
68 bool init_address = 72 bool init_address =
69 GetLocalHostPort(instance_->pp_instance(), &host, &port) && 73 GetLocalHostPort(instance_->pp_instance(), &host, &port) &&
70 ResolveHost(instance_->pp_instance(), host, port, &address_); 74 ResolveHost(instance_->pp_instance(), host, port, &address_);
71 if (!init_address) 75 if (!init_address)
72 instance_->AppendError("Can't init address"); 76 instance_->AppendError("Can't init address");
73 77
78 socket_interface_1_0_ =
79 static_cast<const PPB_UDPSocket_1_0*>(
80 pp::Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_INTERFACE_1_0));
81 if (!socket_interface_1_0_)
82 instance_->AppendError("PPB_UDPSocket_1_0 interface not available");
83
84 socket_interface_1_1_ =
85 static_cast<const PPB_UDPSocket_1_1*>(
86 pp::Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_INTERFACE_1_1));
87 if (!socket_interface_1_1_)
88 instance_->AppendError("PPB_UDPSocket_1_1 interface not available");
89
90 const PPB_UDPSocket_1_2* iface_1_2 =
91 static_cast<const PPB_UDPSocket_1_2*>(
92 pp::Module::Get()->GetBrowserInterface(PPB_UDPSOCKET_INTERFACE_1_2));
93 has_interface_1_2_ = iface_1_2 != NULL;
94
etrunko 2015/02/23 22:22:15 I did not find an easier way to check for specific
bbudge 2015/03/02 16:03:08 This is fine. You don't need to test for 1.2 thoug
74 return tcp_socket_is_available && 95 return tcp_socket_is_available &&
75 udp_socket_is_available && 96 udp_socket_is_available &&
76 net_address_is_available && 97 net_address_is_available &&
77 init_address && 98 init_address &&
78 CheckTestingInterface() && 99 CheckTestingInterface() &&
79 EnsureRunningOverHTTP(); 100 EnsureRunningOverHTTP() &&
101 socket_interface_1_0_ != NULL &&
102 socket_interface_1_1_ != NULL;
80 } 103 }
81 104
82 void TestUDPSocket::RunTests(const std::string& filter) { 105 void TestUDPSocket::RunTests(const std::string& filter) {
83 RUN_CALLBACK_TEST(TestUDPSocket, ReadWrite, filter); 106 RUN_CALLBACK_TEST(TestUDPSocket, ReadWrite, filter);
84 RUN_CALLBACK_TEST(TestUDPSocket, Broadcast, filter); 107 RUN_CALLBACK_TEST(TestUDPSocket, Broadcast, filter);
108 RUN_CALLBACK_TEST(TestUDPSocket, SetOption_1_0, filter);
109 RUN_CALLBACK_TEST(TestUDPSocket, SetOption_1_1, filter);
85 RUN_CALLBACK_TEST(TestUDPSocket, SetOption, filter); 110 RUN_CALLBACK_TEST(TestUDPSocket, SetOption, filter);
86 RUN_CALLBACK_TEST(TestUDPSocket, ParallelSend, filter); 111 RUN_CALLBACK_TEST(TestUDPSocket, ParallelSend, filter);
112 RUN_CALLBACK_TEST(TestUDPSocket, Multicast, filter);
87 } 113 }
88 114
89 std::string TestUDPSocket::GetLocalAddress(pp::NetAddress* address) { 115 std::string TestUDPSocket::GetLocalAddress(pp::NetAddress* address) {
90 pp::TCPSocket socket(instance_); 116 pp::TCPSocket socket(instance_);
91 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); 117 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
92 callback.WaitForResult(socket.Connect(address_, callback.GetCallback())); 118 callback.WaitForResult(socket.Connect(address_, callback.GetCallback()));
93 CHECK_CALLBACK_BEHAVIOR(callback); 119 CHECK_CALLBACK_BEHAVIOR(callback);
94 ASSERT_EQ(PP_OK, callback.result()); 120 ASSERT_EQ(PP_OK, callback.result());
95 *address = socket.GetLocalAddress(); 121 *address = socket.GetLocalAddress();
96 ASSERT_NE(0, address->pp_resource()); 122 ASSERT_NE(0, address->pp_resource());
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 &str)); 207 &str));
182 208
183 callback.WaitForResult(rv); 209 callback.WaitForResult(rv);
184 CHECK_CALLBACK_BEHAVIOR(callback); 210 CHECK_CALLBACK_BEHAVIOR(callback);
185 ASSERT_FALSE(callback.result() < 0); 211 ASSERT_FALSE(callback.result() < 0);
186 ASSERT_EQ(message.size(), static_cast<size_t>(callback.result())); 212 ASSERT_EQ(message.size(), static_cast<size_t>(callback.result()));
187 ASSERT_EQ(message, str); 213 ASSERT_EQ(message, str);
188 PASS(); 214 PASS();
189 } 215 }
190 216
217 std::string TestUDPSocket::SetMulticastOptions(pp::UDPSocket* socket) {
218 int32_t expected = has_interface_1_2_ ? PP_OK : PP_ERROR_BADARGUMENT;
219
220 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
221 callback.WaitForResult(socket->SetOption(
222 PP_UDPSOCKET_OPTION_MULTICAST_LOOP, pp::Var(true),
223 callback.GetCallback()));
224 CHECK_CALLBACK_BEHAVIOR(callback);
225 ASSERT_EQ(expected, callback.result());
226
227 callback.WaitForResult(socket->SetOption(
228 PP_UDPSOCKET_OPTION_MULTICAST_TTL, pp::Var(1), callback.GetCallback()));
229 CHECK_CALLBACK_BEHAVIOR(callback);
230 ASSERT_EQ(expected, callback.result());
231
232 PASS();
233 }
234
191 std::string TestUDPSocket::TestReadWrite() { 235 std::string TestUDPSocket::TestReadWrite() {
192 pp::UDPSocket server_socket(instance_), client_socket(instance_); 236 pp::UDPSocket server_socket(instance_), client_socket(instance_);
193 pp::NetAddress server_address, client_address; 237 pp::NetAddress server_address, client_address;
194 238
195 ASSERT_SUBTEST_SUCCESS(LookupPortAndBindUDPSocket(&server_socket, 239 ASSERT_SUBTEST_SUCCESS(LookupPortAndBindUDPSocket(&server_socket,
196 &server_address)); 240 &server_address));
197 ASSERT_SUBTEST_SUCCESS(LookupPortAndBindUDPSocket(&client_socket, 241 ASSERT_SUBTEST_SUCCESS(LookupPortAndBindUDPSocket(&client_socket,
198 &client_address)); 242 &client_address));
199 const std::string message = "Simple message that will be sent via UDP"; 243 const std::string message = "Simple message that will be sent via UDP";
200 pp::NetAddress recvfrom_address; 244 pp::NetAddress recvfrom_address;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 // |second_message| was also received by |server1|. 295 // |second_message| was also received by |server1|.
252 ASSERT_SUBTEST_SUCCESS(ReadSocket(&server1, &recvfrom_address, 296 ASSERT_SUBTEST_SUCCESS(ReadSocket(&server1, &recvfrom_address,
253 second_message.size(), &message)); 297 second_message.size(), &message));
254 ASSERT_EQ(second_message, message); 298 ASSERT_EQ(second_message, message);
255 299
256 server1.Close(); 300 server1.Close();
257 server2.Close(); 301 server2.Close();
258 PASS(); 302 PASS();
259 } 303 }
260 304
305 int32_t TestUDPSocket::SetOptionValue(UDPSocketSetOption func,
306 PP_Resource socket,
307 PP_UDPSocket_Option option,
308 struct PP_Var value) {
309 PP_TimeTicks start_time(NowInTimeTicks());
310 TestCompletionCallback cb(instance_->pp_instance(), callback_type());
311 cb.WaitForResult(func(socket, option, value,
312 cb.GetCallback().pp_completion_callback()));
313
314 // Expanded from CHECK_CALLBACK_BEHAVIOR macro.
315 if (cb.failed()) {
316 std::string msg = MakeFailureMessage(__FILE__, __LINE__,
317 cb.errors().c_str());
318
319 instance_->LogTest("SetOptionValue", msg, start_time);
320 return PP_ERROR_FAILED;
321 }
322 return cb.result();
323 }
324
325 std::string TestUDPSocket::TestSetOption_1_0() {
326 PP_Resource socket = socket_interface_1_0_->Create(instance_->pp_instance());
327 ASSERT_NE(0, socket);
328
329 // Multicast options are not supported in interface 1.0.
330 ASSERT_EQ(PP_ERROR_BADARGUMENT,
331 SetOptionValue(socket_interface_1_0_->SetOption,
332 socket,
333 PP_UDPSOCKET_OPTION_MULTICAST_LOOP,
334 PP_MakeBool(PP_FromBool(true))));
335
336 ASSERT_EQ(PP_ERROR_BADARGUMENT,
337 SetOptionValue(socket_interface_1_0_->SetOption,
338 socket,
339 PP_UDPSOCKET_OPTION_MULTICAST_TTL,
340 PP_MakeInt32(1)));
341
342 socket_interface_1_0_->Close(socket);
343 pp::Module::Get()->core()->ReleaseResource(socket);
344
345 PASS();
346 }
347
348 std::string TestUDPSocket::TestSetOption_1_1() {
349 PP_Resource socket = socket_interface_1_1_->Create(instance_->pp_instance());
350 ASSERT_NE(0, socket);
351
352 // Multicast options are not supported in interface 1.1.
353 ASSERT_EQ(PP_ERROR_BADARGUMENT,
354 SetOptionValue(socket_interface_1_1_->SetOption,
355 socket,
356 PP_UDPSOCKET_OPTION_MULTICAST_LOOP,
357 PP_MakeBool(PP_FromBool(true))));
358
359 ASSERT_EQ(PP_ERROR_BADARGUMENT,
360 SetOptionValue(socket_interface_1_1_->SetOption,
361 socket,
362 PP_UDPSOCKET_OPTION_MULTICAST_TTL,
363 PP_MakeInt32(1)));
364
365 socket_interface_1_1_->Close(socket);
366 pp::Module::Get()->core()->ReleaseResource(socket);
367
368 PASS();
369 }
370
261 std::string TestUDPSocket::TestSetOption() { 371 std::string TestUDPSocket::TestSetOption() {
262 pp::UDPSocket socket(instance_); 372 pp::UDPSocket socket(instance_);
263 373
264 ASSERT_SUBTEST_SUCCESS(SetBroadcastOptions(&socket)); 374 ASSERT_SUBTEST_SUCCESS(SetBroadcastOptions(&socket));
375 ASSERT_SUBTEST_SUCCESS(SetMulticastOptions(&socket));
265 376
266 // Try to pass incorrect option value's type. 377 // Try to pass incorrect option value's type.
267 TestCompletionCallback callback(instance_->pp_instance(), callback_type()); 378 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
268 callback.WaitForResult(socket.SetOption( 379 callback.WaitForResult(socket.SetOption(
269 PP_UDPSOCKET_OPTION_ADDRESS_REUSE, pp::Var(1), callback.GetCallback())); 380 PP_UDPSOCKET_OPTION_ADDRESS_REUSE, pp::Var(1), callback.GetCallback()));
270 CHECK_CALLBACK_BEHAVIOR(callback); 381 CHECK_CALLBACK_BEHAVIOR(callback);
271 ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result()); 382 ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
272 383
384 // Invalid multicast TTL values (less than 0 and greater than 255).
385 callback.WaitForResult(socket.SetOption(
386 PP_UDPSOCKET_OPTION_MULTICAST_TTL, pp::Var(-1), callback.GetCallback()));
387 CHECK_CALLBACK_BEHAVIOR(callback);
388 ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
389
390 callback.WaitForResult(socket.SetOption(
391 PP_UDPSOCKET_OPTION_MULTICAST_TTL, pp::Var(256), callback.GetCallback()));
392 CHECK_CALLBACK_BEHAVIOR(callback);
393 ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
394
273 callback.WaitForResult(socket.SetOption( 395 callback.WaitForResult(socket.SetOption(
274 PP_UDPSOCKET_OPTION_BROADCAST, pp::Var(false), callback.GetCallback())); 396 PP_UDPSOCKET_OPTION_BROADCAST, pp::Var(false), callback.GetCallback()));
275 CHECK_CALLBACK_BEHAVIOR(callback); 397 CHECK_CALLBACK_BEHAVIOR(callback);
276 ASSERT_EQ(PP_OK, callback.result()); 398 ASSERT_EQ(PP_OK, callback.result());
277 399
278 callback.WaitForResult(socket.SetOption( 400 callback.WaitForResult(socket.SetOption(
279 PP_UDPSOCKET_OPTION_SEND_BUFFER_SIZE, pp::Var(4096), 401 PP_UDPSOCKET_OPTION_SEND_BUFFER_SIZE, pp::Var(4096),
280 callback.GetCallback())); 402 callback.GetCallback()));
281 CHECK_CALLBACK_BEHAVIOR(callback); 403 CHECK_CALLBACK_BEHAVIOR(callback);
282 ASSERT_EQ(PP_OK, callback.result()); 404 ASSERT_EQ(PP_OK, callback.result());
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 ASSERT_SUBTEST_SUCCESS( 503 ASSERT_SUBTEST_SUCCESS(
382 ReadSocket(&server_socket, &recvfrom_address, message.size(), &str)); 504 ReadSocket(&server_socket, &recvfrom_address, message.size(), &str));
383 ASSERT_EQ(message, str); 505 ASSERT_EQ(message, str);
384 } 506 }
385 507
386 server_socket.Close(); 508 server_socket.Close();
387 client_socket.Close(); 509 client_socket.Close();
388 510
389 PASS(); 511 PASS();
390 } 512 }
513
514 std::string TestUDPSocket::TestMulticast() {
515 pp::UDPSocket server1(instance_), server2(instance_);
516
517 ASSERT_SUBTEST_SUCCESS(SetMulticastOptions(&server1));
518 ASSERT_SUBTEST_SUCCESS(SetMulticastOptions(&server2));
519
520 server1.Close();
521 server2.Close();
522
523 if (!has_interface_1_2_)
524 PASS();
etrunko 2015/02/23 22:22:15 Again, if there is a better way to check for inter
bbudge 2015/03/02 16:03:08 You don't have to check for 1.2 (see comment above
525 PASS();
526 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698