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/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/memory/weak_ptr.h" |
12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/run_loop.h" |
13 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
14 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
15 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
16 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
17 #include "net/base/net_log_unittest.h" | 19 #include "net/base/net_log_unittest.h" |
18 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
19 #include "net/base/test_completion_callback.h" | 21 #include "net/base/test_completion_callback.h" |
20 #include "net/test/net_test_suite.h" | 22 #include "net/test/net_test_suite.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
23 | 25 |
24 namespace net { | 26 namespace net { |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 class UDPSocketTest : public PlatformTest { | 30 class UDPSocketTest : public PlatformTest { |
29 public: | 31 public: |
30 UDPSocketTest() | 32 UDPSocketTest() |
31 : buffer_(new IOBufferWithSize(kMaxRead)) { | 33 : buffer_(new IOBufferWithSize(kMaxRead)), weak_factory_(this) {} |
32 } | |
33 | 34 |
34 // Blocks until data is read from the socket. | 35 // Blocks until data is read from the socket. |
35 std::string RecvFromSocket(UDPServerSocket* socket) { | 36 std::string RecvFromSocket(UDPServerSocket* socket) { |
36 TestCompletionCallback callback; | 37 TestCompletionCallback callback; |
37 | 38 |
38 int rv = socket->RecvFrom( | 39 int rv = socket->RecvFrom( |
39 buffer_.get(), kMaxRead, &recv_from_address_, callback.callback()); | 40 buffer_.get(), kMaxRead, &recv_from_address_, callback.callback()); |
40 if (rv == ERR_IO_PENDING) | 41 if (rv == ERR_IO_PENDING) |
41 rv = callback.WaitForResult(); | 42 rv = callback.WaitForResult(); |
42 if (rv < 0) | 43 if (rv < 0) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 if (rv == ERR_IO_PENDING) | 106 if (rv == ERR_IO_PENDING) |
106 rv = callback.WaitForResult(); | 107 rv = callback.WaitForResult(); |
107 if (rv <= 0) | 108 if (rv <= 0) |
108 return bytes_sent > 0 ? bytes_sent : rv; | 109 return bytes_sent > 0 ? bytes_sent : rv; |
109 bytes_sent += rv; | 110 bytes_sent += rv; |
110 buffer->DidConsume(rv); | 111 buffer->DidConsume(rv); |
111 } | 112 } |
112 return bytes_sent; | 113 return bytes_sent; |
113 } | 114 } |
114 | 115 |
| 116 // Creates and address from an ip/port and returns it in |address|. |
| 117 void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) { |
| 118 IPAddressNumber ip_number; |
| 119 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); |
| 120 if (!rv) |
| 121 return; |
| 122 *address = IPEndPoint(ip_number, port); |
| 123 } |
| 124 |
| 125 // Run unit test for a connection test. |
| 126 // |use_nonblocking_io| is used to switch between overlapped and non-blocking |
| 127 // IO on Windows. It has no effect in other ports. |
| 128 void ConnectTest(bool use_nonblocking_io) { |
| 129 const uint16 kPort = 9999; |
| 130 std::string simple_message("hello world!"); |
| 131 |
| 132 // Setup the server to listen. |
| 133 IPEndPoint bind_address; |
| 134 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
| 135 CapturingNetLog server_log; |
| 136 scoped_ptr<UDPServerSocket> server( |
| 137 new UDPServerSocket(&server_log, NetLog::Source())); |
| 138 #if defined(OS_WIN) |
| 139 if (use_nonblocking_io) |
| 140 server->UseNonBlockingIO(); |
| 141 #endif |
| 142 server->AllowAddressReuse(); |
| 143 int rv = server->Listen(bind_address); |
| 144 ASSERT_EQ(OK, rv); |
| 145 |
| 146 // Setup the client. |
| 147 IPEndPoint server_address; |
| 148 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
| 149 CapturingNetLog client_log; |
| 150 scoped_ptr<UDPClientSocket> client( |
| 151 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
| 152 &client_log, NetLog::Source())); |
| 153 #if defined(OS_WIN) |
| 154 if (use_nonblocking_io) |
| 155 client->UseNonBlockingIO(); |
| 156 #endif |
| 157 |
| 158 rv = client->Connect(server_address); |
| 159 EXPECT_EQ(OK, rv); |
| 160 |
| 161 // Client sends to the server. |
| 162 rv = WriteSocket(client.get(), simple_message); |
| 163 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
| 164 |
| 165 // Server waits for message. |
| 166 std::string str = RecvFromSocket(server.get()); |
| 167 DCHECK(simple_message == str); |
| 168 |
| 169 // Server echoes reply. |
| 170 rv = SendToSocket(server.get(), simple_message); |
| 171 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
| 172 |
| 173 // Client waits for response. |
| 174 str = ReadSocket(client.get()); |
| 175 DCHECK(simple_message == str); |
| 176 |
| 177 // Delete sockets so they log their final events. |
| 178 server.reset(); |
| 179 client.reset(); |
| 180 |
| 181 // Check the server's log. |
| 182 CapturingNetLog::CapturedEntryList server_entries; |
| 183 server_log.GetEntries(&server_entries); |
| 184 EXPECT_EQ(4u, server_entries.size()); |
| 185 EXPECT_TRUE( |
| 186 LogContainsBeginEvent(server_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); |
| 187 EXPECT_TRUE(LogContainsEvent(server_entries, 1, |
| 188 NetLog::TYPE_UDP_BYTES_RECEIVED, |
| 189 NetLog::PHASE_NONE)); |
| 190 EXPECT_TRUE(LogContainsEvent(server_entries, 2, NetLog::TYPE_UDP_BYTES_SENT, |
| 191 NetLog::PHASE_NONE)); |
| 192 EXPECT_TRUE( |
| 193 LogContainsEndEvent(server_entries, 3, NetLog::TYPE_SOCKET_ALIVE)); |
| 194 |
| 195 // Check the client's log. |
| 196 CapturingNetLog::CapturedEntryList client_entries; |
| 197 client_log.GetEntries(&client_entries); |
| 198 EXPECT_EQ(6u, client_entries.size()); |
| 199 EXPECT_TRUE( |
| 200 LogContainsBeginEvent(client_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); |
| 201 EXPECT_TRUE( |
| 202 LogContainsBeginEvent(client_entries, 1, NetLog::TYPE_UDP_CONNECT)); |
| 203 EXPECT_TRUE( |
| 204 LogContainsEndEvent(client_entries, 2, NetLog::TYPE_UDP_CONNECT)); |
| 205 EXPECT_TRUE(LogContainsEvent(client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, |
| 206 NetLog::PHASE_NONE)); |
| 207 EXPECT_TRUE(LogContainsEvent(client_entries, 4, |
| 208 NetLog::TYPE_UDP_BYTES_RECEIVED, |
| 209 NetLog::PHASE_NONE)); |
| 210 EXPECT_TRUE( |
| 211 LogContainsEndEvent(client_entries, 5, NetLog::TYPE_SOCKET_ALIVE)); |
| 212 } |
| 213 |
115 protected: | 214 protected: |
116 static const int kMaxRead = 1024; | 215 static const int kMaxRead = 1024; |
117 scoped_refptr<IOBufferWithSize> buffer_; | 216 scoped_refptr<IOBufferWithSize> buffer_; |
118 IPEndPoint recv_from_address_; | 217 IPEndPoint recv_from_address_; |
| 218 base::WeakPtrFactory<UDPSocketTest> weak_factory_; |
119 }; | 219 }; |
120 | 220 |
121 // Creates and address from an ip/port and returns it in |address|. | 221 TEST_F(UDPSocketTest, Connect) { |
122 void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) { | 222 // The variable |use_nonblocking_io| has no effect in non-Windows ports. |
123 IPAddressNumber ip_number; | 223 ConnectTest(false); |
124 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); | |
125 if (!rv) | |
126 return; | |
127 *address = IPEndPoint(ip_number, port); | |
128 } | 224 } |
129 | 225 |
130 TEST_F(UDPSocketTest, Connect) { | 226 #if defined(OS_WIN) |
131 const uint16 kPort = 9999; | 227 TEST_F(UDPSocketTest, ConnectNonBlocking) { |
132 std::string simple_message("hello world!"); | 228 ConnectTest(true); |
133 | |
134 // Setup the server to listen. | |
135 IPEndPoint bind_address; | |
136 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | |
137 CapturingNetLog server_log; | |
138 scoped_ptr<UDPServerSocket> server( | |
139 new UDPServerSocket(&server_log, NetLog::Source())); | |
140 server->AllowAddressReuse(); | |
141 int rv = server->Listen(bind_address); | |
142 ASSERT_EQ(OK, rv); | |
143 | |
144 // Setup the client. | |
145 IPEndPoint server_address; | |
146 CreateUDPAddress("127.0.0.1", kPort, &server_address); | |
147 CapturingNetLog client_log; | |
148 scoped_ptr<UDPClientSocket> client( | |
149 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, | |
150 RandIntCallback(), | |
151 &client_log, | |
152 NetLog::Source())); | |
153 rv = client->Connect(server_address); | |
154 EXPECT_EQ(OK, rv); | |
155 | |
156 // Client sends to the server. | |
157 rv = WriteSocket(client.get(), simple_message); | |
158 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | |
159 | |
160 // Server waits for message. | |
161 std::string str = RecvFromSocket(server.get()); | |
162 DCHECK(simple_message == str); | |
163 | |
164 // Server echoes reply. | |
165 rv = SendToSocket(server.get(), simple_message); | |
166 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | |
167 | |
168 // Client waits for response. | |
169 str = ReadSocket(client.get()); | |
170 DCHECK(simple_message == str); | |
171 | |
172 // Delete sockets so they log their final events. | |
173 server.reset(); | |
174 client.reset(); | |
175 | |
176 // Check the server's log. | |
177 CapturingNetLog::CapturedEntryList server_entries; | |
178 server_log.GetEntries(&server_entries); | |
179 EXPECT_EQ(4u, server_entries.size()); | |
180 EXPECT_TRUE(LogContainsBeginEvent( | |
181 server_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); | |
182 EXPECT_TRUE(LogContainsEvent( | |
183 server_entries, 1, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); | |
184 EXPECT_TRUE(LogContainsEvent( | |
185 server_entries, 2, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE)); | |
186 EXPECT_TRUE(LogContainsEndEvent( | |
187 server_entries, 3, NetLog::TYPE_SOCKET_ALIVE)); | |
188 | |
189 // Check the client's log. | |
190 CapturingNetLog::CapturedEntryList client_entries; | |
191 client_log.GetEntries(&client_entries); | |
192 EXPECT_EQ(6u, client_entries.size()); | |
193 EXPECT_TRUE(LogContainsBeginEvent( | |
194 client_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); | |
195 EXPECT_TRUE(LogContainsBeginEvent( | |
196 client_entries, 1, NetLog::TYPE_UDP_CONNECT)); | |
197 EXPECT_TRUE(LogContainsEndEvent( | |
198 client_entries, 2, NetLog::TYPE_UDP_CONNECT)); | |
199 EXPECT_TRUE(LogContainsEvent( | |
200 client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE)); | |
201 EXPECT_TRUE(LogContainsEvent( | |
202 client_entries, 4, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); | |
203 EXPECT_TRUE(LogContainsEndEvent( | |
204 client_entries, 5, NetLog::TYPE_SOCKET_ALIVE)); | |
205 } | 229 } |
| 230 #endif |
206 | 231 |
207 #if defined(OS_MACOSX) | 232 #if defined(OS_MACOSX) |
208 // UDPSocketPrivate_Broadcast is disabled for OSX because it requires | 233 // UDPSocketPrivate_Broadcast is disabled for OSX because it requires |
209 // root permissions on OSX 10.7+. | 234 // root permissions on OSX 10.7+. |
210 TEST_F(UDPSocketTest, DISABLED_Broadcast) { | 235 TEST_F(UDPSocketTest, DISABLED_Broadcast) { |
211 #elif defined(OS_ANDROID) | 236 #elif defined(OS_ANDROID) |
212 // It is also disabled for Android because it is extremely flaky. | 237 // It is also disabled for Android because it is extremely flaky. |
213 // The first call to SendToSocket returns -109 (Address not reachable) | 238 // The first call to SendToSocket returns -109 (Address not reachable) |
214 // in some unpredictable cases. crbug.com/139144. | 239 // in some unpredictable cases. crbug.com/139144. |
215 TEST_F(UDPSocketTest, DISABLED_Broadcast) { | 240 TEST_F(UDPSocketTest, DISABLED_Broadcast) { |
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; | 768 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
744 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); | 769 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); |
745 g_expected_dscp = DSCP_DEFAULT; | 770 g_expected_dscp = DSCP_DEFAULT; |
746 g_expected_traffic_type = QOSTrafficTypeBestEffort; | 771 g_expected_traffic_type = QOSTrafficTypeBestEffort; |
747 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); | 772 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); |
748 client.Close(); | 773 client.Close(); |
749 } | 774 } |
750 #endif | 775 #endif |
751 | 776 |
752 } // namespace net | 777 } // namespace net |
OLD | NEW |