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" |
| 13 #include "base/message_loop/message_loop.h" |
12 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/run_loop.h" |
13 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
14 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
15 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
16 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
17 #include "net/base/net_log_unittest.h" | 20 #include "net/base/net_log_unittest.h" |
18 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
19 #include "net/base/test_completion_callback.h" | 22 #include "net/base/test_completion_callback.h" |
20 #include "net/test/net_test_suite.h" | 23 #include "net/test/net_test_suite.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "testing/platform_test.h" | 25 #include "testing/platform_test.h" |
23 | 26 |
24 namespace net { | 27 namespace net { |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
28 class UDPSocketTest : public PlatformTest { | 31 class UDPSocketTest : public PlatformTest { |
29 public: | 32 public: |
30 UDPSocketTest() | 33 UDPSocketTest() : buffer_(new IOBufferWithSize(kMaxRead)) {} |
31 : buffer_(new IOBufferWithSize(kMaxRead)) { | |
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 void WriteSocketIgnoreResult(UDPClientSocket* socket, std::string msg) { |
| 117 WriteSocket(socket, msg); |
| 118 } |
| 119 |
| 120 // Creates an address from ip address and port and writes it to |*address|. |
| 121 void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) { |
| 122 IPAddressNumber ip_number; |
| 123 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); |
| 124 if (!rv) |
| 125 return; |
| 126 *address = IPEndPoint(ip_number, port); |
| 127 } |
| 128 |
| 129 // Run unit test for a connection test. |
| 130 // |use_nonblocking_io| is used to switch between overlapped and non-blocking |
| 131 // IO on Windows. It has no effect in other ports. |
| 132 void ConnectTest(bool use_nonblocking_io); |
| 133 |
115 protected: | 134 protected: |
116 static const int kMaxRead = 1024; | 135 static const int kMaxRead = 1024; |
117 scoped_refptr<IOBufferWithSize> buffer_; | 136 scoped_refptr<IOBufferWithSize> buffer_; |
118 IPEndPoint recv_from_address_; | 137 IPEndPoint recv_from_address_; |
119 }; | 138 }; |
120 | 139 |
121 // Creates and address from an ip/port and returns it in |address|. | 140 void ReadCompleteCallback(int* result_out, base::Closure callback, int result) { |
122 void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) { | 141 *result_out = result; |
123 IPAddressNumber ip_number; | 142 callback.Run(); |
124 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); | |
125 if (!rv) | |
126 return; | |
127 *address = IPEndPoint(ip_number, port); | |
128 } | 143 } |
129 | 144 |
130 TEST_F(UDPSocketTest, Connect) { | 145 void UDPSocketTest::ConnectTest(bool use_nonblocking_io) { |
131 const uint16 kPort = 9999; | 146 const uint16 kPort = 9999; |
132 std::string simple_message("hello world!"); | 147 std::string simple_message("hello world!"); |
133 | 148 |
134 // Setup the server to listen. | 149 // Setup the server to listen. |
135 IPEndPoint bind_address; | 150 IPEndPoint bind_address; |
136 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | 151 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
137 CapturingNetLog server_log; | 152 CapturingNetLog server_log; |
138 scoped_ptr<UDPServerSocket> server( | 153 scoped_ptr<UDPServerSocket> server( |
139 new UDPServerSocket(&server_log, NetLog::Source())); | 154 new UDPServerSocket(&server_log, NetLog::Source())); |
| 155 #if defined(OS_WIN) |
| 156 if (use_nonblocking_io) |
| 157 server->UseNonBlockingIO(); |
| 158 #endif |
140 server->AllowAddressReuse(); | 159 server->AllowAddressReuse(); |
141 int rv = server->Listen(bind_address); | 160 int rv = server->Listen(bind_address); |
142 ASSERT_EQ(OK, rv); | 161 ASSERT_EQ(OK, rv); |
143 | 162 |
144 // Setup the client. | 163 // Setup the client. |
145 IPEndPoint server_address; | 164 IPEndPoint server_address; |
146 CreateUDPAddress("127.0.0.1", kPort, &server_address); | 165 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
147 CapturingNetLog client_log; | 166 CapturingNetLog client_log; |
148 scoped_ptr<UDPClientSocket> client( | 167 scoped_ptr<UDPClientSocket> client( |
149 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, | 168 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
150 RandIntCallback(), | 169 &client_log, NetLog::Source())); |
151 &client_log, | 170 #if defined(OS_WIN) |
152 NetLog::Source())); | 171 if (use_nonblocking_io) |
| 172 client->UseNonBlockingIO(); |
| 173 #endif |
| 174 |
153 rv = client->Connect(server_address); | 175 rv = client->Connect(server_address); |
154 EXPECT_EQ(OK, rv); | 176 EXPECT_EQ(OK, rv); |
155 | 177 |
156 // Client sends to the server. | 178 // Client sends to the server. |
157 rv = WriteSocket(client.get(), simple_message); | 179 rv = WriteSocket(client.get(), simple_message); |
158 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | 180 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
159 | 181 |
160 // Server waits for message. | 182 // Server waits for message. |
161 std::string str = RecvFromSocket(server.get()); | 183 std::string str = RecvFromSocket(server.get()); |
162 DCHECK(simple_message == str); | 184 DCHECK(simple_message == str); |
163 | 185 |
164 // Server echoes reply. | 186 // Server echoes reply. |
165 rv = SendToSocket(server.get(), simple_message); | 187 rv = SendToSocket(server.get(), simple_message); |
166 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | 188 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
167 | 189 |
168 // Client waits for response. | 190 // Client waits for response. |
169 str = ReadSocket(client.get()); | 191 str = ReadSocket(client.get()); |
170 DCHECK(simple_message == str); | 192 DCHECK(simple_message == str); |
171 | 193 |
| 194 // Test asynchronous read. Server waits for message. |
| 195 base::RunLoop run_loop; |
| 196 int read_result = 0; |
| 197 rv = server->RecvFrom( |
| 198 buffer_.get(), kMaxRead, &recv_from_address_, |
| 199 base::Bind(&ReadCompleteCallback, &read_result, run_loop.QuitClosure())); |
| 200 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 201 |
| 202 // Client sends to the server. |
| 203 base::MessageLoop::current()->PostTask( |
| 204 FROM_HERE, |
| 205 base::Bind(&UDPSocketTest::WriteSocketIgnoreResult, |
| 206 base::Unretained(this), client.get(), simple_message)); |
| 207 run_loop.Run(); |
| 208 EXPECT_EQ(simple_message.length(), static_cast<size_t>(read_result)); |
| 209 EXPECT_EQ(simple_message, std::string(buffer_->data(), read_result)); |
| 210 |
172 // Delete sockets so they log their final events. | 211 // Delete sockets so they log their final events. |
173 server.reset(); | 212 server.reset(); |
174 client.reset(); | 213 client.reset(); |
175 | 214 |
176 // Check the server's log. | 215 // Check the server's log. |
177 CapturingNetLog::CapturedEntryList server_entries; | 216 CapturingNetLog::CapturedEntryList server_entries; |
178 server_log.GetEntries(&server_entries); | 217 server_log.GetEntries(&server_entries); |
179 EXPECT_EQ(4u, server_entries.size()); | 218 EXPECT_EQ(5u, server_entries.size()); |
180 EXPECT_TRUE(LogContainsBeginEvent( | 219 EXPECT_TRUE( |
181 server_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); | 220 LogContainsBeginEvent(server_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); |
182 EXPECT_TRUE(LogContainsEvent( | 221 EXPECT_TRUE(LogContainsEvent( |
183 server_entries, 1, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); | 222 server_entries, 1, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); |
| 223 EXPECT_TRUE(LogContainsEvent(server_entries, 2, NetLog::TYPE_UDP_BYTES_SENT, |
| 224 NetLog::PHASE_NONE)); |
184 EXPECT_TRUE(LogContainsEvent( | 225 EXPECT_TRUE(LogContainsEvent( |
185 server_entries, 2, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE)); | 226 server_entries, 3, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); |
186 EXPECT_TRUE(LogContainsEndEvent( | 227 EXPECT_TRUE( |
187 server_entries, 3, NetLog::TYPE_SOCKET_ALIVE)); | 228 LogContainsEndEvent(server_entries, 4, NetLog::TYPE_SOCKET_ALIVE)); |
188 | 229 |
189 // Check the client's log. | 230 // Check the client's log. |
190 CapturingNetLog::CapturedEntryList client_entries; | 231 CapturingNetLog::CapturedEntryList client_entries; |
191 client_log.GetEntries(&client_entries); | 232 client_log.GetEntries(&client_entries); |
192 EXPECT_EQ(6u, client_entries.size()); | 233 EXPECT_EQ(7u, client_entries.size()); |
193 EXPECT_TRUE(LogContainsBeginEvent( | 234 EXPECT_TRUE( |
194 client_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); | 235 LogContainsBeginEvent(client_entries, 0, NetLog::TYPE_SOCKET_ALIVE)); |
195 EXPECT_TRUE(LogContainsBeginEvent( | 236 EXPECT_TRUE( |
196 client_entries, 1, NetLog::TYPE_UDP_CONNECT)); | 237 LogContainsBeginEvent(client_entries, 1, NetLog::TYPE_UDP_CONNECT)); |
197 EXPECT_TRUE(LogContainsEndEvent( | 238 EXPECT_TRUE(LogContainsEndEvent(client_entries, 2, NetLog::TYPE_UDP_CONNECT)); |
198 client_entries, 2, NetLog::TYPE_UDP_CONNECT)); | 239 EXPECT_TRUE(LogContainsEvent(client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, |
199 EXPECT_TRUE(LogContainsEvent( | 240 NetLog::PHASE_NONE)); |
200 client_entries, 3, NetLog::TYPE_UDP_BYTES_SENT, NetLog::PHASE_NONE)); | |
201 EXPECT_TRUE(LogContainsEvent( | 241 EXPECT_TRUE(LogContainsEvent( |
202 client_entries, 4, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); | 242 client_entries, 4, NetLog::TYPE_UDP_BYTES_RECEIVED, NetLog::PHASE_NONE)); |
203 EXPECT_TRUE(LogContainsEndEvent( | 243 EXPECT_TRUE(LogContainsEvent(client_entries, 5, NetLog::TYPE_UDP_BYTES_SENT, |
204 client_entries, 5, NetLog::TYPE_SOCKET_ALIVE)); | 244 NetLog::PHASE_NONE)); |
| 245 EXPECT_TRUE( |
| 246 LogContainsEndEvent(client_entries, 6, NetLog::TYPE_SOCKET_ALIVE)); |
205 } | 247 } |
206 | 248 |
| 249 TEST_F(UDPSocketTest, Connect) { |
| 250 // The variable |use_nonblocking_io| has no effect in non-Windows ports. |
| 251 ConnectTest(false); |
| 252 } |
| 253 |
| 254 #if defined(OS_WIN) |
| 255 TEST_F(UDPSocketTest, ConnectNonBlocking) { |
| 256 ConnectTest(true); |
| 257 } |
| 258 #endif |
| 259 |
207 #if defined(OS_MACOSX) | 260 #if defined(OS_MACOSX) |
208 // UDPSocketPrivate_Broadcast is disabled for OSX because it requires | 261 // UDPSocketPrivate_Broadcast is disabled for OSX because it requires |
209 // root permissions on OSX 10.7+. | 262 // root permissions on OSX 10.7+. |
210 TEST_F(UDPSocketTest, DISABLED_Broadcast) { | 263 TEST_F(UDPSocketTest, DISABLED_Broadcast) { |
211 #elif defined(OS_ANDROID) | 264 #elif defined(OS_ANDROID) |
212 // It is also disabled for Android because it is extremely flaky. | 265 // It is also disabled for Android because it is extremely flaky. |
213 // The first call to SendToSocket returns -109 (Address not reachable) | 266 // The first call to SendToSocket returns -109 (Address not reachable) |
214 // in some unpredictable cases. crbug.com/139144. | 267 // in some unpredictable cases. crbug.com/139144. |
215 TEST_F(UDPSocketTest, DISABLED_Broadcast) { | 268 TEST_F(UDPSocketTest, DISABLED_Broadcast) { |
216 #else | 269 #else |
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; | 796 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
744 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); | 797 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); |
745 g_expected_dscp = DSCP_DEFAULT; | 798 g_expected_dscp = DSCP_DEFAULT; |
746 g_expected_traffic_type = QOSTrafficTypeBestEffort; | 799 g_expected_traffic_type = QOSTrafficTypeBestEffort; |
747 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); | 800 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); |
748 client.Close(); | 801 client.Close(); |
749 } | 802 } |
750 #endif | 803 #endif |
751 | 804 |
752 } // namespace net | 805 } // namespace net |
OLD | NEW |