Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/tcp_socket.h" | 5 #include "net/socket/tcp_socket.h" |
| 6 | 6 |
| 7 #include <cstring> | |
|
wtc
2013/09/06 21:55:42
Nit: can you <string.h>?
yzshen1
2013/09/09 23:14:48
Done.
| |
| 8 | |
| 7 #include <string> | 9 #include <string> |
| 10 #include <vector> | |
| 8 | 11 |
| 12 #include "base/memory/ref_counted.h" | |
| 9 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "build/build_config.h" | |
| 10 #include "net/base/address_list.h" | 15 #include "net/base/address_list.h" |
| 16 #include "net/base/io_buffer.h" | |
| 11 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
| 12 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 13 #include "net/base/test_completion_callback.h" | 19 #include "net/base/test_completion_callback.h" |
| 14 #include "net/socket/tcp_client_socket.h" | 20 #include "net/socket/tcp_client_socket.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
| 17 | 23 |
| 18 namespace net { | 24 namespace net { |
| 19 | 25 |
| 20 namespace { | 26 namespace { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 ASSERT_EQ(OK, result); | 193 ASSERT_EQ(OK, result); |
| 188 | 194 |
| 189 EXPECT_TRUE(accepted_socket.get()); | 195 EXPECT_TRUE(accepted_socket.get()); |
| 190 | 196 |
| 191 // Both sockets should be on the loopback network interface. | 197 // Both sockets should be on the loopback network interface. |
| 192 EXPECT_EQ(accepted_address.address(), local_address_.address()); | 198 EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 193 | 199 |
| 194 EXPECT_EQ(OK, connect_callback.WaitForResult()); | 200 EXPECT_EQ(OK, connect_callback.WaitForResult()); |
| 195 } | 201 } |
| 196 | 202 |
| 203 // TODO(yzshen): Enable it for other platforms once TCPSocketLibevent supports | |
| 204 // client socket operations. | |
| 205 #if defined(OS_WIN) | |
| 206 | |
| 207 TEST_F(TCPSocketTest, ReadWrite) { | |
| 208 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); | |
| 209 | |
| 210 TestCompletionCallback connect_callback; | |
| 211 TCPSocket connecting_socket(NULL, NetLog::Source()); | |
| 212 int result = connecting_socket.Create(ADDRESS_FAMILY_IPV4); | |
| 213 ASSERT_EQ(OK, result); | |
| 214 connecting_socket.Connect(local_address_, connect_callback.callback()); | |
| 215 | |
| 216 TestCompletionCallback accept_callback; | |
| 217 scoped_ptr<TCPSocket> accepted_socket; | |
| 218 IPEndPoint accepted_address; | |
| 219 result = socket_.Accept(&accepted_socket, &accepted_address, | |
| 220 accept_callback.callback()); | |
| 221 ASSERT_EQ(OK, accept_callback.GetResult(result)); | |
| 222 | |
| 223 ASSERT_TRUE(accepted_socket.get()); | |
| 224 | |
| 225 // Both sockets should be on the loopback network interface. | |
| 226 EXPECT_EQ(accepted_address.address(), local_address_.address()); | |
| 227 | |
| 228 EXPECT_EQ(OK, connect_callback.WaitForResult()); | |
| 229 | |
| 230 const std::string message("test message"); | |
| 231 std::vector<char> buffer(message.size()); | |
| 232 | |
| 233 size_t bytes_written = 0; | |
| 234 while (bytes_written < message.size()) { | |
| 235 scoped_refptr<net::IOBufferWithSize> write_buffer( | |
|
wtc
2013/09/06 21:55:42
Remove all net:: because this file is in the net n
yzshen1
2013/09/09 23:14:48
Done.
| |
| 236 new net::IOBufferWithSize(message.size() - bytes_written)); | |
| 237 memmove(write_buffer->data(), message.data() + bytes_written, | |
| 238 message.size() - bytes_written); | |
| 239 | |
| 240 TestCompletionCallback write_callback; | |
| 241 int write_result = accepted_socket->Write( | |
| 242 write_buffer.get(), write_buffer->size(), write_callback.callback()); | |
| 243 write_result = write_callback.GetResult(write_result); | |
| 244 ASSERT_TRUE(write_result >= 0); | |
| 245 bytes_written += write_result; | |
| 246 ASSERT_TRUE(bytes_written <= message.size()); | |
| 247 } | |
| 248 | |
| 249 size_t bytes_read = 0; | |
| 250 while (bytes_read < message.size()) { | |
| 251 scoped_refptr<net::IOBufferWithSize> read_buffer( | |
| 252 new net::IOBufferWithSize(message.size() - bytes_read)); | |
| 253 TestCompletionCallback read_callback; | |
| 254 int read_result = connecting_socket.Read( | |
| 255 read_buffer.get(), read_buffer->size(), read_callback.callback()); | |
| 256 read_result = read_callback.GetResult(read_result); | |
| 257 ASSERT_TRUE(read_result >= 0); | |
| 258 ASSERT_TRUE(bytes_read + read_result <= message.size()); | |
| 259 memmove(&buffer[bytes_read], read_buffer->data(), read_result); | |
| 260 bytes_read += read_result; | |
| 261 } | |
| 262 | |
| 263 std::string received_message(buffer.begin(), buffer.end()); | |
| 264 ASSERT_EQ(message, received_message); | |
| 265 } | |
| 266 | |
| 267 #endif | |
| 268 | |
| 197 } // namespace | 269 } // namespace |
| 198 } // namespace net | 270 } // namespace net |
| OLD | NEW |