OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "net/udp/udp_socket.h" | 5 #include "net/udp/udp_socket.h" |
6 | 6 |
7 #include "net/udp/udp_client_socket.h" | 7 #include "net/udp/udp_client_socket.h" |
8 #include "net/udp/udp_server_socket.h" | 8 #include "net/udp/udp_server_socket.h" |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 } | 152 } |
153 | 153 |
154 void UDPSocketTest::ConnectTest(bool use_nonblocking_io) { | 154 void UDPSocketTest::ConnectTest(bool use_nonblocking_io) { |
155 const uint16_t kPort = 9999; | 155 const uint16_t kPort = 9999; |
156 std::string simple_message("hello world!"); | 156 std::string simple_message("hello world!"); |
157 | 157 |
158 // Setup the server to listen. | 158 // Setup the server to listen. |
159 IPEndPoint bind_address; | 159 IPEndPoint bind_address; |
160 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | 160 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
161 TestNetLog server_log; | 161 TestNetLog server_log; |
162 scoped_ptr<UDPServerSocket> server( | 162 std::unique_ptr<UDPServerSocket> server( |
163 new UDPServerSocket(&server_log, NetLog::Source())); | 163 new UDPServerSocket(&server_log, NetLog::Source())); |
164 #if defined(OS_WIN) | 164 #if defined(OS_WIN) |
165 if (use_nonblocking_io) | 165 if (use_nonblocking_io) |
166 server->UseNonBlockingIO(); | 166 server->UseNonBlockingIO(); |
167 #endif | 167 #endif |
168 server->AllowAddressReuse(); | 168 server->AllowAddressReuse(); |
169 int rv = server->Listen(bind_address); | 169 int rv = server->Listen(bind_address); |
170 ASSERT_EQ(OK, rv); | 170 ASSERT_EQ(OK, rv); |
171 | 171 |
172 // Setup the client. | 172 // Setup the client. |
173 IPEndPoint server_address; | 173 IPEndPoint server_address; |
174 CreateUDPAddress("127.0.0.1", kPort, &server_address); | 174 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
175 TestNetLog client_log; | 175 TestNetLog client_log; |
176 scoped_ptr<UDPClientSocket> client( | 176 std::unique_ptr<UDPClientSocket> client( |
177 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 177 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
178 &client_log, NetLog::Source())); | 178 &client_log, NetLog::Source())); |
179 #if defined(OS_WIN) | 179 #if defined(OS_WIN) |
180 if (use_nonblocking_io) | 180 if (use_nonblocking_io) |
181 client->UseNonBlockingIO(); | 181 client->UseNonBlockingIO(); |
182 #endif | 182 #endif |
183 | 183 |
184 rv = client->Connect(server_address); | 184 rv = client->Connect(server_address); |
185 EXPECT_EQ(OK, rv); | 185 EXPECT_EQ(OK, rv); |
186 | 186 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 #endif | 280 #endif |
281 const uint16_t kPort = 9999; | 281 const uint16_t kPort = 9999; |
282 std::string first_message("first message"), second_message("second message"); | 282 std::string first_message("first message"), second_message("second message"); |
283 | 283 |
284 IPEndPoint broadcast_address; | 284 IPEndPoint broadcast_address; |
285 CreateUDPAddress("255.255.255.255", kPort, &broadcast_address); | 285 CreateUDPAddress("255.255.255.255", kPort, &broadcast_address); |
286 IPEndPoint listen_address; | 286 IPEndPoint listen_address; |
287 CreateUDPAddress("0.0.0.0", kPort, &listen_address); | 287 CreateUDPAddress("0.0.0.0", kPort, &listen_address); |
288 | 288 |
289 TestNetLog server1_log, server2_log; | 289 TestNetLog server1_log, server2_log; |
290 scoped_ptr<UDPServerSocket> server1( | 290 std::unique_ptr<UDPServerSocket> server1( |
291 new UDPServerSocket(&server1_log, NetLog::Source())); | 291 new UDPServerSocket(&server1_log, NetLog::Source())); |
292 scoped_ptr<UDPServerSocket> server2( | 292 std::unique_ptr<UDPServerSocket> server2( |
293 new UDPServerSocket(&server2_log, NetLog::Source())); | 293 new UDPServerSocket(&server2_log, NetLog::Source())); |
294 server1->AllowAddressReuse(); | 294 server1->AllowAddressReuse(); |
295 server1->AllowBroadcast(); | 295 server1->AllowBroadcast(); |
296 server2->AllowAddressReuse(); | 296 server2->AllowAddressReuse(); |
297 server2->AllowBroadcast(); | 297 server2->AllowBroadcast(); |
298 | 298 |
299 int rv = server1->Listen(listen_address); | 299 int rv = server1->Listen(listen_address); |
300 EXPECT_EQ(OK, rv); | 300 EXPECT_EQ(OK, rv); |
301 rv = server2->Listen(listen_address); | 301 rv = server2->Listen(listen_address); |
302 EXPECT_EQ(OK, rv); | 302 EXPECT_EQ(OK, rv); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 | 373 |
374 // Free the last socket, its local port is still in |used_ports|. | 374 // Free the last socket, its local port is still in |used_ports|. |
375 delete sockets.back(); | 375 delete sockets.back(); |
376 sockets.pop_back(); | 376 sockets.pop_back(); |
377 | 377 |
378 TestPrng test_prng(used_ports); | 378 TestPrng test_prng(used_ports); |
379 RandIntCallback rand_int_cb = | 379 RandIntCallback rand_int_cb = |
380 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); | 380 base::Bind(&TestPrng::GetNext, base::Unretained(&test_prng)); |
381 | 381 |
382 // Create a socket with random binding policy and connect. | 382 // Create a socket with random binding policy and connect. |
383 scoped_ptr<UDPClientSocket> test_socket( | 383 std::unique_ptr<UDPClientSocket> test_socket(new UDPClientSocket( |
384 new UDPClientSocket(DatagramSocket::RANDOM_BIND, | 384 DatagramSocket::RANDOM_BIND, rand_int_cb, NULL, NetLog::Source())); |
385 rand_int_cb, | |
386 NULL, | |
387 NetLog::Source())); | |
388 EXPECT_EQ(OK, test_socket->Connect(peer_address)); | 385 EXPECT_EQ(OK, test_socket->Connect(peer_address)); |
389 | 386 |
390 // Make sure that the last port number in the |used_ports| was used. | 387 // Make sure that the last port number in the |used_ports| was used. |
391 IPEndPoint client_address; | 388 IPEndPoint client_address; |
392 EXPECT_EQ(OK, test_socket->GetLocalAddress(&client_address)); | 389 EXPECT_EQ(OK, test_socket->GetLocalAddress(&client_address)); |
393 EXPECT_EQ(used_ports.back(), client_address.port()); | 390 EXPECT_EQ(used_ports.back(), client_address.port()); |
394 | 391 |
395 STLDeleteElements(&sockets); | 392 STLDeleteElements(&sockets); |
396 } | 393 } |
397 | 394 |
398 // Return a privileged port (under 1024) so binding will fail. | 395 // Return a privileged port (under 1024) so binding will fail. |
399 int PrivilegedRand(int min, int max) { | 396 int PrivilegedRand(int min, int max) { |
400 // Chosen by fair dice roll. Guaranteed to be random. | 397 // Chosen by fair dice roll. Guaranteed to be random. |
401 return 4; | 398 return 4; |
402 } | 399 } |
403 | 400 |
404 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR | 401 #if defined(OS_IOS) && !TARGET_IPHONE_SIMULATOR |
405 // TODO(droger): On iOS this test fails on device (but passes on simulator). | 402 // TODO(droger): On iOS this test fails on device (but passes on simulator). |
406 // See http://crbug.com/227760. | 403 // See http://crbug.com/227760. |
407 #define MAYBE_ConnectFail DISABLED_ConnectFail | 404 #define MAYBE_ConnectFail DISABLED_ConnectFail |
408 #else | 405 #else |
409 #define MAYBE_ConnectFail ConnectFail | 406 #define MAYBE_ConnectFail ConnectFail |
410 #endif | 407 #endif |
411 TEST_F(UDPSocketTest, MAYBE_ConnectFail) { | 408 TEST_F(UDPSocketTest, MAYBE_ConnectFail) { |
412 IPEndPoint peer_address; | 409 IPEndPoint peer_address; |
413 CreateUDPAddress("0.0.0.0", 53, &peer_address); | 410 CreateUDPAddress("0.0.0.0", 53, &peer_address); |
414 | 411 |
415 scoped_ptr<UDPSocket> socket( | 412 std::unique_ptr<UDPSocket> socket(new UDPSocket(DatagramSocket::RANDOM_BIND, |
416 new UDPSocket(DatagramSocket::RANDOM_BIND, | 413 base::Bind(&PrivilegedRand), |
417 base::Bind(&PrivilegedRand), | 414 NULL, NetLog::Source())); |
418 NULL, | |
419 NetLog::Source())); | |
420 int rv = socket->Open(peer_address.GetFamily()); | 415 int rv = socket->Open(peer_address.GetFamily()); |
421 EXPECT_EQ(OK, rv); | 416 EXPECT_EQ(OK, rv); |
422 rv = socket->Connect(peer_address); | 417 rv = socket->Connect(peer_address); |
423 // Connect should have failed since we couldn't bind to that port, | 418 // Connect should have failed since we couldn't bind to that port, |
424 EXPECT_NE(OK, rv); | 419 EXPECT_NE(OK, rv); |
425 // Make sure that UDPSocket actually closed the socket. | 420 // Make sure that UDPSocket actually closed the socket. |
426 EXPECT_FALSE(socket->is_connected()); | 421 EXPECT_FALSE(socket->is_connected()); |
427 } | 422 } |
428 | 423 |
429 // In this test, we verify that connect() on a socket will have the effect | 424 // In this test, we verify that connect() on a socket will have the effect |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; | 803 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
809 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); | 804 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); |
810 g_expected_dscp = DSCP_DEFAULT; | 805 g_expected_dscp = DSCP_DEFAULT; |
811 g_expected_traffic_type = QOSTrafficTypeBestEffort; | 806 g_expected_traffic_type = QOSTrafficTypeBestEffort; |
812 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); | 807 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); |
813 client.Close(); | 808 client.Close(); |
814 } | 809 } |
815 #endif | 810 #endif |
816 | 811 |
817 } // namespace net | 812 } // namespace net |
OLD | NEW |