#include "net/socket/tcp_client_socket.h"
#include <stddef.h>
#include <set>
#include <string>
#include <vector>
#include "base/strings/string_number_conversions.h"
#include "base/test/bind.h"
#include "base/test/power_monitor_test.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "net/base/features.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/log/net_log_source.h"
#include "net/nqe/network_quality_estimator_test_util.h"
#include "net/socket/socket_performance_watcher.h"
#include "net/socket/socket_test_util.h"
#include "net/socket/tcp_server_socket.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/gtest_util.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#if !BUILDFLAG(IS_ANDROID)
#define TCP_CLIENT_SOCKET_OBSERVES_SUSPEND
#endif
IsError;
IsOk;
Not;
namespace base {
class TimeDelta;
}
namespace net {
namespace {
class TCPClientSocketTest : public testing::Test { … };
TEST_F(TCPClientSocketTest, BindLoopbackToLoopback) { … }
TEST_F(TCPClientSocketTest, BindLoopbackToExternal) { … }
TEST_F(TCPClientSocketTest, BindLoopbackToIPv6) { … }
TEST_F(TCPClientSocketTest, WasEverUsed) { … }
TEST_F(TCPClientSocketTest, DnsAliasesPersistForReuse) { … }
class TestSocketPerformanceWatcher : public SocketPerformanceWatcher { … };
#if defined(TCP_INFO) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_TestSocketPerformanceWatcher …
#else
#define MAYBE_TestSocketPerformanceWatcher …
#endif
TEST_F(TCPClientSocketTest, MAYBE_TestSocketPerformanceWatcher) { … }
#if BUILDFLAG(IS_ANDROID)
TEST_F(TCPClientSocketTest, Tag) {
if (!CanGetTaggedBytes()) {
DVLOG(0) << "Skipping test - GetTaggedBytes unsupported.";
return;
}
EmbeddedTestServer test_server;
test_server.AddDefaultHandlers(base::FilePath());
ASSERT_TRUE(test_server.Start());
AddressList addr_list;
ASSERT_TRUE(test_server.GetAddressList(&addr_list));
TCPClientSocket s(addr_list, nullptr, nullptr, nullptr, NetLogSource());
int32_t tag_val1 = 0x12345678;
uint64_t old_traffic = GetTaggedBytes(tag_val1);
SocketTag tag1(SocketTag::UNSET_UID, tag_val1);
s.ApplySocketTag(tag1);
TestCompletionCallback connect_callback;
int connect_result = s.Connect(connect_callback.callback());
EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk());
EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic);
int32_t tag_val2 = 0x87654321;
old_traffic = GetTaggedBytes(tag_val2);
SocketTag tag2(getuid(), tag_val2);
s.ApplySocketTag(tag2);
const char kRequest1[] = "GET / HTTP/1.0";
auto write_buffer1 = base::MakeRefCounted<StringIOBuffer>(kRequest1);
TestCompletionCallback write_callback1;
EXPECT_EQ(s.Write(write_buffer1.get(), strlen(kRequest1),
write_callback1.callback(), TRAFFIC_ANNOTATION_FOR_TESTS),
static_cast<int>(strlen(kRequest1)));
EXPECT_GT(GetTaggedBytes(tag_val2), old_traffic);
old_traffic = GetTaggedBytes(tag_val1);
s.ApplySocketTag(tag1);
const char kRequest2[] = "\n\n";
scoped_refptr<IOBufferWithSize> write_buffer2 =
base::MakeRefCounted<IOBufferWithSize>(strlen(kRequest2));
memmove(write_buffer2->data(), kRequest2, strlen(kRequest2));
TestCompletionCallback write_callback2;
EXPECT_EQ(s.Write(write_buffer2.get(), strlen(kRequest2),
write_callback2.callback(), TRAFFIC_ANNOTATION_FOR_TESTS),
static_cast<int>(strlen(kRequest2)));
EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic);
s.Disconnect();
}
TEST_F(TCPClientSocketTest, TagAfterConnect) {
if (!CanGetTaggedBytes()) {
DVLOG(0) << "Skipping test - GetTaggedBytes unsupported.";
return;
}
EmbeddedTestServer test_server;
test_server.AddDefaultHandlers(base::FilePath());
ASSERT_TRUE(test_server.Start());
AddressList addr_list;
ASSERT_TRUE(test_server.GetAddressList(&addr_list));
TCPClientSocket s(addr_list, nullptr, nullptr, nullptr, NetLogSource());
TestCompletionCallback connect_callback;
int connect_result = s.Connect(connect_callback.callback());
EXPECT_THAT(connect_callback.GetResult(connect_result), IsOk());
int32_t tag_val2 = 0x87654321;
uint64_t old_traffic = GetTaggedBytes(tag_val2);
SocketTag tag2(getuid(), tag_val2);
s.ApplySocketTag(tag2);
const char kRequest1[] = "GET / HTTP/1.0";
auto write_buffer1 = base::MakeRefCounted<StringIOBuffer>(kRequest1);
TestCompletionCallback write_callback1;
EXPECT_EQ(s.Write(write_buffer1.get(), strlen(kRequest1),
write_callback1.callback(), TRAFFIC_ANNOTATION_FOR_TESTS),
static_cast<int>(strlen(kRequest1)));
EXPECT_GT(GetTaggedBytes(tag_val2), old_traffic);
int32_t tag_val1 = 0x12345678;
old_traffic = GetTaggedBytes(tag_val1);
SocketTag tag1(SocketTag::UNSET_UID, tag_val1);
s.ApplySocketTag(tag1);
const char kRequest2[] = "\n\n";
auto write_buffer2 = base::MakeRefCounted<StringIOBuffer>(kRequest2);
TestCompletionCallback write_callback2;
EXPECT_EQ(s.Write(write_buffer2.get(), strlen(kRequest2),
write_callback2.callback(), TRAFFIC_ANNOTATION_FOR_TESTS),
static_cast<int>(strlen(kRequest2)));
EXPECT_GT(GetTaggedBytes(tag_val1), old_traffic);
s.Disconnect();
}
#endif
class NeverConnectingTCPClientSocket : public TCPClientSocket { … };
#if defined(TCP_CLIENT_SOCKET_OBSERVES_SUSPEND)
TEST_F(TCPClientSocketTest, SuspendBeforeConnect) { … }
TEST_F(TCPClientSocketTest, SuspendDuringConnect) { … }
TEST_F(TCPClientSocketTest, SuspendDuringConnectMultipleAddresses) { … }
TEST_F(TCPClientSocketTest, SuspendWhileIdle) { … }
TEST_F(TCPClientSocketTest, SuspendDuringRead) { … }
TEST_F(TCPClientSocketTest, SuspendDuringWrite) { … }
TEST_F(TCPClientSocketTest, SuspendDuringReadAndWrite) { … }
#endif
class OverrideTcpConnectAttemptTimeout { … };
class TCPClientSocketMockTimeTest : public testing::Test { … };
TEST_F(TCPClientSocketMockTimeTest, NoConnectAttemptTimeoutByDefault) { … }
TEST_F(TCPClientSocketMockTimeTest, ConnectAttemptTimeoutUsesMaxWhenNoRTT) { … }
TEST_F(TCPClientSocketMockTimeTest, ConnectAttemptTimeoutUsesMinWhenRTTLow) { … }
TEST_F(TCPClientSocketMockTimeTest, ConnectAttemptTimeoutUsesMinWhenRTTHigh) { … }
TEST_F(TCPClientSocketMockTimeTest, ConnectAttemptTimeoutUsesRTT) { … }
TEST_F(TCPClientSocketMockTimeTest, ConnectAttemptTimeoutIndependent) { … }
}
}