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->AllowBroadcast(); |
| 219 server2->AllowBroadcast(); |
| 220 |
| 221 int rv = server1->Listen(listen_address); |
| 222 EXPECT_EQ(OK, rv); |
| 223 rv = server2->Listen(listen_address); |
| 224 EXPECT_EQ(OK, rv); |
| 225 |
| 226 rv = SendToSocket(server1.get(), first_message, broadcast_address); |
| 227 EXPECT_EQ(static_cast<int>(first_message.size()), rv); |
| 228 std::string str = RecvFromSocket(server1.get()); |
| 229 ASSERT_EQ(first_message, str); |
| 230 str = RecvFromSocket(server2.get()); |
| 231 ASSERT_EQ(first_message, str); |
| 232 |
| 233 rv = SendToSocket(server2.get(), second_message, broadcast_address); |
| 234 EXPECT_EQ(static_cast<int>(second_message.size()), rv); |
| 235 str = RecvFromSocket(server1.get()); |
| 236 ASSERT_EQ(second_message, str); |
| 237 str = RecvFromSocket(server2.get()); |
| 238 ASSERT_EQ(second_message, str); |
| 239 } |
| 240 |
204 // In this test, we verify that random binding logic works, which attempts | 241 // 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 | 242 // to bind to a random port and returns if succeeds, otherwise retries for |
206 // |kBindRetries| number of times. | 243 // |kBindRetries| number of times. |
207 | 244 |
208 // To generate the scenario, we first create |kBindRetries| number of | 245 // To generate the scenario, we first create |kBindRetries| number of |
209 // UDPClientSockets with default binding policy and connect to the same | 246 // 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 | 247 // 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. | 248 // 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 | 249 // 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 | 250 // 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); | 476 EXPECT_EQ(rv, ERR_IO_PENDING); |
440 | 477 |
441 server.Close(); | 478 server.Close(); |
442 | 479 |
443 EXPECT_FALSE(callback.have_result()); | 480 EXPECT_FALSE(callback.have_result()); |
444 } | 481 } |
445 | 482 |
446 } // namespace | 483 } // namespace |
447 | 484 |
448 } // namespace net | 485 } // namespace net |
OLD | NEW |