| 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/socket/udp_socket.h" | 5 #include "net/socket/udp_socket.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 void CreateUDPAddress(const std::string& ip_str, | 139 void CreateUDPAddress(const std::string& ip_str, |
| 140 uint16_t port, | 140 uint16_t port, |
| 141 IPEndPoint* address) { | 141 IPEndPoint* address) { |
| 142 IPAddress ip_address; | 142 IPAddress ip_address; |
| 143 if (!ip_address.AssignFromIPLiteral(ip_str)) | 143 if (!ip_address.AssignFromIPLiteral(ip_str)) |
| 144 return; | 144 return; |
| 145 *address = IPEndPoint(ip_address, port); | 145 *address = IPEndPoint(ip_address, port); |
| 146 } | 146 } |
| 147 | 147 |
| 148 // Run unit test for a connection test. | 148 // Run unit test for a connection test. |
| 149 // |use_nonblocking_io| is used to switch between overlapped and non-blocking | 149 void ConnectTest(); |
| 150 // IO on Windows. It has no effect in other ports. | |
| 151 void ConnectTest(bool use_nonblocking_io); | |
| 152 | 150 |
| 153 protected: | 151 protected: |
| 154 static const int kMaxRead = 1024; | 152 static const int kMaxRead = 1024; |
| 155 scoped_refptr<IOBufferWithSize> buffer_; | 153 scoped_refptr<IOBufferWithSize> buffer_; |
| 156 IPEndPoint recv_from_address_; | 154 IPEndPoint recv_from_address_; |
| 157 }; | 155 }; |
| 158 | 156 |
| 159 void ReadCompleteCallback(int* result_out, base::Closure callback, int result) { | 157 void ReadCompleteCallback(int* result_out, base::Closure callback, int result) { |
| 160 *result_out = result; | 158 *result_out = result; |
| 161 callback.Run(); | 159 callback.Run(); |
| 162 } | 160 } |
| 163 | 161 |
| 164 void UDPSocketTest::ConnectTest(bool use_nonblocking_io) { | 162 TEST_F(UDPSocketTest, Connect) { |
| 165 const uint16_t kPort = 9999; | 163 const uint16_t kPort = 9999; |
| 166 std::string simple_message("hello world!"); | 164 std::string simple_message("hello world!"); |
| 167 | 165 |
| 168 // Setup the server to listen. | 166 // Setup the server to listen. |
| 169 IPEndPoint bind_address; | 167 IPEndPoint bind_address; |
| 170 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | 168 CreateUDPAddress("127.0.0.1", kPort, &bind_address); |
| 171 TestNetLog server_log; | 169 TestNetLog server_log; |
| 172 std::unique_ptr<UDPServerSocket> server( | 170 std::unique_ptr<UDPServerSocket> server( |
| 173 new UDPServerSocket(&server_log, NetLogSource())); | 171 new UDPServerSocket(&server_log, NetLogSource())); |
| 174 if (use_nonblocking_io) | |
| 175 server->UseNonBlockingIO(); | |
| 176 server->AllowAddressReuse(); | 172 server->AllowAddressReuse(); |
| 177 int rv = server->Listen(bind_address); | 173 int rv = server->Listen(bind_address); |
| 178 ASSERT_THAT(rv, IsOk()); | 174 ASSERT_THAT(rv, IsOk()); |
| 179 | 175 |
| 180 // Setup the client. | 176 // Setup the client. |
| 181 IPEndPoint server_address; | 177 IPEndPoint server_address; |
| 182 CreateUDPAddress("127.0.0.1", kPort, &server_address); | 178 CreateUDPAddress("127.0.0.1", kPort, &server_address); |
| 183 TestNetLog client_log; | 179 TestNetLog client_log; |
| 184 std::unique_ptr<UDPClientSocket> client( | 180 std::unique_ptr<UDPClientSocket> client( |
| 185 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | 181 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), |
| 186 &client_log, NetLogSource())); | 182 &client_log, NetLogSource())); |
| 187 if (use_nonblocking_io) | |
| 188 client->UseNonBlockingIO(); | |
| 189 | 183 |
| 190 rv = client->Connect(server_address); | 184 rv = client->Connect(server_address); |
| 191 EXPECT_THAT(rv, IsOk()); | 185 EXPECT_THAT(rv, IsOk()); |
| 192 | 186 |
| 193 // Client sends to the server. | 187 // Client sends to the server. |
| 194 rv = WriteSocket(client.get(), simple_message); | 188 rv = WriteSocket(client.get(), simple_message); |
| 195 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); | 189 EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); |
| 196 | 190 |
| 197 // Server waits for message. | 191 // Server waits for message. |
| 198 std::string str = RecvFromSocket(server.get()); | 192 std::string str = RecvFromSocket(server.get()); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 EXPECT_TRUE(LogContainsEvent(client_entries, 4, | 255 EXPECT_TRUE(LogContainsEvent(client_entries, 4, |
| 262 NetLogEventType::UDP_BYTES_RECEIVED, | 256 NetLogEventType::UDP_BYTES_RECEIVED, |
| 263 NetLogEventPhase::NONE)); | 257 NetLogEventPhase::NONE)); |
| 264 EXPECT_TRUE(LogContainsEvent(client_entries, 5, | 258 EXPECT_TRUE(LogContainsEvent(client_entries, 5, |
| 265 NetLogEventType::UDP_BYTES_SENT, | 259 NetLogEventType::UDP_BYTES_SENT, |
| 266 NetLogEventPhase::NONE)); | 260 NetLogEventPhase::NONE)); |
| 267 EXPECT_TRUE( | 261 EXPECT_TRUE( |
| 268 LogContainsEndEvent(client_entries, 6, NetLogEventType::SOCKET_ALIVE)); | 262 LogContainsEndEvent(client_entries, 6, NetLogEventType::SOCKET_ALIVE)); |
| 269 } | 263 } |
| 270 | 264 |
| 271 TEST_F(UDPSocketTest, Connect) { | |
| 272 // The variable |use_nonblocking_io| has no effect in non-Windows ports. | |
| 273 ConnectTest(false); | |
| 274 } | |
| 275 | |
| 276 #if defined(OS_WIN) | |
| 277 TEST_F(UDPSocketTest, ConnectNonBlocking) { | |
| 278 ConnectTest(true); | |
| 279 } | |
| 280 #endif | |
| 281 | |
| 282 #if defined(OS_MACOSX) | 265 #if defined(OS_MACOSX) |
| 283 // UDPSocketPrivate_Broadcast is disabled for OSX because it requires | 266 // UDPSocketPrivate_Broadcast is disabled for OSX because it requires |
| 284 // root permissions on OSX 10.7+. | 267 // root permissions on OSX 10.7+. |
| 285 TEST_F(UDPSocketTest, DISABLED_Broadcast) { | 268 TEST_F(UDPSocketTest, DISABLED_Broadcast) { |
| 286 #elif defined(OS_ANDROID) | 269 #elif defined(OS_ANDROID) |
| 287 // Disabled for Android because devices attached to testbots don't have default | 270 // Disabled for Android because devices attached to testbots don't have default |
| 288 // network, so broadcasting to 255.255.255.255 returns error -109 (Address not | 271 // network, so broadcasting to 255.255.255.255 returns error -109 (Address not |
| 289 // reachable). crbug.com/139144. | 272 // reachable). crbug.com/139144. |
| 290 TEST_F(UDPSocketTest, DISABLED_Broadcast) { | 273 TEST_F(UDPSocketTest, DISABLED_Broadcast) { |
| 291 #else | 274 #else |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 890 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; | 873 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
| 891 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_NO_CHANGE), IsOk()); | 874 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_NO_CHANGE), IsOk()); |
| 892 g_expected_dscp = DSCP_DEFAULT; | 875 g_expected_dscp = DSCP_DEFAULT; |
| 893 g_expected_traffic_type = QOSTrafficTypeBestEffort; | 876 g_expected_traffic_type = QOSTrafficTypeBestEffort; |
| 894 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_DEFAULT), IsOk()); | 877 EXPECT_THAT(client.SetDiffServCodePoint(DSCP_DEFAULT), IsOk()); |
| 895 client.Close(); | 878 client.Close(); |
| 896 } | 879 } |
| 897 #endif | 880 #endif |
| 898 | 881 |
| 899 } // namespace net | 882 } // namespace net |
| OLD | NEW |