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