| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/test/simple_test_tick_clock.h" | |
| 16 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 17 #include "net/base/address_list.h" | 16 #include "net/base/address_list.h" |
| 18 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 19 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
| 20 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 21 #include "net/base/sockaddr_storage.h" | 20 #include "net/base/sockaddr_storage.h" |
| 22 #include "net/base/test_completion_callback.h" | 21 #include "net/base/test_completion_callback.h" |
| 23 #include "net/log/net_log_source.h" | 22 #include "net/log/net_log_source.h" |
| 24 #include "net/socket/socket_performance_watcher.h" | 23 #include "net/socket/socket_performance_watcher.h" |
| 25 #include "net/socket/tcp_client_socket.h" | 24 #include "net/socket/tcp_client_socket.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); | 108 EXPECT_THAT(accept_callback.WaitForResult(), IsOk()); |
| 110 | 109 |
| 111 EXPECT_TRUE(accepted_socket.get()); | 110 EXPECT_TRUE(accepted_socket.get()); |
| 112 | 111 |
| 113 // Both sockets should be on the loopback network interface. | 112 // Both sockets should be on the loopback network interface. |
| 114 EXPECT_EQ(accepted_address.address(), local_address_.address()); | 113 EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 115 } | 114 } |
| 116 | 115 |
| 117 #if defined(TCP_INFO) || defined(OS_LINUX) | 116 #if defined(TCP_INFO) || defined(OS_LINUX) |
| 118 // Tests that notifications to Socket Performance Watcher (SPW) are delivered | 117 // Tests that notifications to Socket Performance Watcher (SPW) are delivered |
| 119 // correctly. |advance_ticks| is the duration by which the clock is advanced | 118 // correctly. |should_notify_updated_rtt| is true if the SPW is interested in |
| 120 // before a message is read. |should_notify_updated_rtt| is true if the SPW | 119 // receiving RTT notifications. |num_messages| is the number of messages that |
| 121 // is interested in receiving RTT notifications. |num_messages| is the number | 120 // are written/read by the sockets. |expect_connection_changed_count| is the |
| 122 // of messages that are written/read by the sockets. | 121 // expected number of connection change notifications received by the SPW. |
| 123 // |expect_connection_changed_count| is the expected number of connection | 122 // |expect_rtt_notification_count| is the expected number of RTT |
| 124 // change notifications received by the SPW. |expect_rtt_notification_count| | 123 // notifications received by the SPW. This test works by writing |
| 125 // is the expected number of RTT notifications received by the SPW. | 124 // |num_messages| to the socket. A different socket (with a SPW attached to |
| 126 // This test works by writing |num_messages| to the socket. A different | 125 // it) reads the messages. |
| 127 // socket (with a SPW attached to it) reads the messages. | 126 void TestSPWNotifications(bool should_notify_updated_rtt, |
| 128 void TestSPWNotifications(const base::TimeDelta& advance_ticks, | |
| 129 bool should_notify_updated_rtt, | |
| 130 size_t num_messages, | 127 size_t num_messages, |
| 131 size_t expect_connection_changed_count, | 128 size_t expect_connection_changed_count, |
| 132 size_t expect_rtt_notification_count) { | 129 size_t expect_rtt_notification_count) { |
| 133 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); | 130 ASSERT_NO_FATAL_FAILURE(SetUpListenIPv4()); |
| 134 | 131 |
| 135 std::unique_ptr<base::SimpleTestTickClock> tick_clock( | |
| 136 new base::SimpleTestTickClock()); | |
| 137 base::SimpleTestTickClock* tick_clock_ptr = tick_clock.get(); | |
| 138 tick_clock_ptr->SetNowTicks(base::TimeTicks::Now()); | |
| 139 | |
| 140 TestCompletionCallback connect_callback; | 132 TestCompletionCallback connect_callback; |
| 141 | 133 |
| 142 std::unique_ptr<TestSocketPerformanceWatcher> watcher( | 134 std::unique_ptr<TestSocketPerformanceWatcher> watcher( |
| 143 new TestSocketPerformanceWatcher(should_notify_updated_rtt)); | 135 new TestSocketPerformanceWatcher(should_notify_updated_rtt)); |
| 144 TestSocketPerformanceWatcher* watcher_ptr = watcher.get(); | 136 TestSocketPerformanceWatcher* watcher_ptr = watcher.get(); |
| 145 | 137 |
| 146 TCPSocket connecting_socket(std::move(watcher), NULL, NetLogSource()); | 138 TCPSocket connecting_socket(std::move(watcher), NULL, NetLogSource()); |
| 147 connecting_socket.SetTickClockForTesting(std::move(tick_clock)); | |
| 148 | 139 |
| 149 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4); | 140 int result = connecting_socket.Open(ADDRESS_FAMILY_IPV4); |
| 150 ASSERT_THAT(result, IsOk()); | 141 ASSERT_THAT(result, IsOk()); |
| 151 connecting_socket.Connect(local_address_, connect_callback.callback()); | 142 connecting_socket.Connect(local_address_, connect_callback.callback()); |
| 152 | 143 |
| 153 TestCompletionCallback accept_callback; | 144 TestCompletionCallback accept_callback; |
| 154 std::unique_ptr<TCPSocket> accepted_socket; | 145 std::unique_ptr<TCPSocket> accepted_socket; |
| 155 IPEndPoint accepted_address; | 146 IPEndPoint accepted_address; |
| 156 result = socket_.Accept(&accepted_socket, &accepted_address, | 147 result = socket_.Accept(&accepted_socket, &accepted_address, |
| 157 accept_callback.callback()); | 148 accept_callback.callback()); |
| 158 ASSERT_THAT(accept_callback.GetResult(result), IsOk()); | 149 ASSERT_THAT(accept_callback.GetResult(result), IsOk()); |
| 159 | 150 |
| 160 ASSERT_TRUE(accepted_socket.get()); | 151 ASSERT_TRUE(accepted_socket.get()); |
| 161 | 152 |
| 162 // Both sockets should be on the loopback network interface. | 153 // Both sockets should be on the loopback network interface. |
| 163 EXPECT_EQ(accepted_address.address(), local_address_.address()); | 154 EXPECT_EQ(accepted_address.address(), local_address_.address()); |
| 164 | 155 |
| 165 ASSERT_THAT(connect_callback.WaitForResult(), IsOk()); | 156 ASSERT_THAT(connect_callback.WaitForResult(), IsOk()); |
| 166 | 157 |
| 167 for (size_t i = 0; i < num_messages; ++i) { | 158 for (size_t i = 0; i < num_messages; ++i) { |
| 168 tick_clock_ptr->Advance(advance_ticks); | |
| 169 | |
| 170 // Use a 1 byte message so that the watcher is notified at most once per | 159 // Use a 1 byte message so that the watcher is notified at most once per |
| 171 // message. | 160 // message. |
| 172 const std::string message("t"); | 161 const std::string message("t"); |
| 173 | 162 |
| 174 scoped_refptr<IOBufferWithSize> write_buffer( | 163 scoped_refptr<IOBufferWithSize> write_buffer( |
| 175 new IOBufferWithSize(message.size())); | 164 new IOBufferWithSize(message.size())); |
| 176 memmove(write_buffer->data(), message.data(), message.size()); | 165 memmove(write_buffer->data(), message.data(), message.size()); |
| 177 | 166 |
| 178 TestCompletionCallback write_callback; | 167 TestCompletionCallback write_callback; |
| 179 int write_result = accepted_socket->Write( | 168 int write_result = accepted_socket->Write( |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 std::string received_message(buffer.begin(), buffer.end()); | 378 std::string received_message(buffer.begin(), buffer.end()); |
| 390 ASSERT_EQ(message, received_message); | 379 ASSERT_EQ(message, received_message); |
| 391 } | 380 } |
| 392 | 381 |
| 393 // These tests require kernel support for tcp_info struct, and so they are | 382 // These tests require kernel support for tcp_info struct, and so they are |
| 394 // enabled only on certain platforms. | 383 // enabled only on certain platforms. |
| 395 #if defined(TCP_INFO) || defined(OS_LINUX) | 384 #if defined(TCP_INFO) || defined(OS_LINUX) |
| 396 // If SocketPerformanceWatcher::ShouldNotifyUpdatedRTT always returns false, | 385 // If SocketPerformanceWatcher::ShouldNotifyUpdatedRTT always returns false, |
| 397 // then the wtatcher should not receive any notifications. | 386 // then the wtatcher should not receive any notifications. |
| 398 TEST_F(TCPSocketTest, SPWNotInterested) { | 387 TEST_F(TCPSocketTest, SPWNotInterested) { |
| 399 TestSPWNotifications(base::TimeDelta::FromSeconds(0), false, 2u, 0u, 0u); | 388 TestSPWNotifications(false, 2u, 0u, 0u); |
| 400 } | |
| 401 | |
| 402 // One notification should be received when the socket connects. No additional | |
| 403 // notifications should be received when the message is read because the clock | |
| 404 // is not advanced. | |
| 405 TEST_F(TCPSocketTest, SPWNoAdvance) { | |
| 406 TestSPWNotifications(base::TimeDelta::FromSeconds(0), true, 2u, 0u, 1u); | |
| 407 } | 389 } |
| 408 | 390 |
| 409 // One notification should be received when the socket connects. One | 391 // One notification should be received when the socket connects. One |
| 410 // additional notification should be received for each message read since this | 392 // additional notification should be received for each message read. |
| 411 // test advances clock by 2 seconds (which is longer than the minimum interval | 393 TEST_F(TCPSocketTest, SPWNoAdvance) { |
| 412 // between consecutive notifications) before every read. | 394 TestSPWNotifications(true, 2u, 0u, 3u); |
| 413 TEST_F(TCPSocketTest, SPWAdvance) { | |
| 414 TestSPWNotifications(base::TimeDelta::FromSeconds(2), true, 2u, 0u, 3u); | |
| 415 } | 395 } |
| 416 #endif // defined(TCP_INFO) || defined(OS_LINUX) | 396 #endif // defined(TCP_INFO) || defined(OS_LINUX) |
| 417 | 397 |
| 418 } // namespace | 398 } // namespace |
| 419 } // namespace net | 399 } // namespace net |
| OLD | NEW |