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_client_socket.h" | 5 #include "net/udp/udp_client_socket.h" |
6 #include "net/udp/udp_server_socket.h" | 6 #include "net/udp/udp_server_socket.h" |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 EXPECT_TRUE(LogContainsEndEvent( | 194 EXPECT_TRUE(LogContainsEndEvent( |
195 client_entries, 2, NetLog::TYPE_UDP_CONNECT)); | 195 client_entries, 2, NetLog::TYPE_UDP_CONNECT)); |
196 EXPECT_TRUE(LogContainsEvent( | 196 EXPECT_TRUE(LogContainsEvent( |
197 client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE)); | 197 client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE)); |
198 EXPECT_TRUE(LogContainsEvent( | 198 EXPECT_TRUE(LogContainsEvent( |
199 client_entries, 4, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); | 199 client_entries, 4, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); |
200 EXPECT_TRUE(LogContainsEndEvent( | 200 EXPECT_TRUE(LogContainsEndEvent( |
201 client_entries, 5, NetLog::TYPE_SOCKET_ALIVE)); | 201 client_entries, 5, NetLog::TYPE_SOCKET_ALIVE)); |
202 } | 202 } |
203 | 203 |
| 204 TEST_F(UDPSocketTest, Broadcast) { |
| 205 const int kPort = 9999; |
| 206 std::string first_message("first message"), second_message("second message"); |
| 207 |
| 208 IPEndPoint broadcast_address; |
| 209 CreateUDPAddress("255.255.255.255", kPort, &broadcast_address); |
| 210 IPEndPoint listen_address; |
| 211 CreateUDPAddress("0.0.0.0", kPort, &listen_address); |
| 212 |
| 213 CapturingNetLog server1_log, server2_log; |
| 214 scoped_ptr<UDPServerSocket> server1( |
| 215 new UDPServerSocket(&server1_log, NetLog::Source())); |
| 216 scoped_ptr<UDPServerSocket> server2( |
| 217 new UDPServerSocket(&server2_log, NetLog::Source())); |
| 218 server1->AllowAddressReuse(); |
| 219 server1->AllowBroadcast(); |
| 220 server2->AllowAddressReuse(); |
| 221 server2->AllowBroadcast(); |
| 222 |
| 223 int rv = server1->Listen(listen_address); |
| 224 EXPECT_EQ(OK, rv); |
| 225 rv = server2->Listen(listen_address); |
| 226 EXPECT_EQ(OK, rv); |
| 227 |
| 228 rv = SendToSocket(server1.get(), first_message, broadcast_address); |
| 229 EXPECT_EQ(static_cast<int>(first_message.size()), rv); |
| 230 std::string str = RecvFromSocket(server1.get()); |
| 231 ASSERT_EQ(first_message, str); |
| 232 str = RecvFromSocket(server2.get()); |
| 233 ASSERT_EQ(first_message, str); |
| 234 |
| 235 rv = SendToSocket(server2.get(), second_message, broadcast_address); |
| 236 EXPECT_EQ(static_cast<int>(second_message.size()), rv); |
| 237 str = RecvFromSocket(server1.get()); |
| 238 ASSERT_EQ(second_message, str); |
| 239 str = RecvFromSocket(server2.get()); |
| 240 ASSERT_EQ(second_message, str); |
| 241 } |
| 242 |
204 // In this test, we verify that random binding logic works, which attempts | 243 // In this test, we verify that random binding logic works, which attempts |
205 // to bind to a random port and returns if succeeds, otherwise retries for | 244 // to bind to a random port and returns if succeeds, otherwise retries for |
206 // |kBindRetries| number of times. | 245 // |kBindRetries| number of times. |
207 | 246 |
208 // To generate the scenario, we first create |kBindRetries| number of | 247 // To generate the scenario, we first create |kBindRetries| number of |
209 // UDPClientSockets with default binding policy and connect to the same | 248 // UDPClientSockets with default binding policy and connect to the same |
210 // peer and save the used port numbers. Then we get rid of the last | 249 // peer and save the used port numbers. Then we get rid of the last |
211 // socket, making sure that the local port it was bound to is available. | 250 // socket, making sure that the local port it was bound to is available. |
212 // Finally, we create a socket with random binding policy, passing it a | 251 // Finally, we create a socket with random binding policy, passing it a |
213 // test PRNG that would serve used port numbers in the array, one after | 252 // test PRNG that would serve used port numbers in the array, one after |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 EXPECT_EQ(rv, ERR_IO_PENDING); | 478 EXPECT_EQ(rv, ERR_IO_PENDING); |
440 | 479 |
441 server.Close(); | 480 server.Close(); |
442 | 481 |
443 EXPECT_FALSE(callback.have_result()); | 482 EXPECT_FALSE(callback.have_result()); |
444 } | 483 } |
445 | 484 |
446 } // namespace | 485 } // namespace |
447 | 486 |
448 } // namespace net | 487 } // namespace net |
OLD | NEW |