chromium/services/network/tcp_socket_unittest.cc

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include <utility>
#include <vector>

#include "base/check_op.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/unique_receiver_set.h"
#include "mojo/public/cpp/system/data_pipe_utils.h"
#include "mojo/public/cpp/system/simple_watcher.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/socket/server_socket.h"
#include "net/socket/socket_test_util.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
#include "net/url_request/url_request_test_util.h"
#include "services/network/mojo_socket_test_util.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/public/mojom/tcp_socket.mojom-forward.h"
#include "services/network/public/mojom/tcp_socket.mojom.h"
#include "services/network/public/mojom/tls_socket.mojom.h"
#include "services/network/public/mojom/udp_socket.mojom.h"
#include "services/network/socket_factory.h"
#include "services/network/tcp_connected_socket.h"
#include "services/network/tcp_server_socket.h"
#include "services/network/test/test_socket_broker_impl.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#endif

namespace network {

namespace {

// A mock ServerSocket that completes Accept() using a specified result.
class MockServerSocket : public net::ServerSocket {};

// A MockServerSocket that fails at GetLocalAddress().
class FailingServerSocket : public MockServerSocket {};

// A server implemented using mojom::TCPServerSocket. It owns the server socket
// pointer and as well as client connections. SendData() and StartReading()
// operate on the newest client connection.
class TestServer {};

}  // namespace

class TCPSocketTest : public testing::Test {};

#if BUILDFLAG(IS_WIN)
TEST_F(TCPSocketTest, BrokerCreateTCPServerSocketSuccess) {
  TestServer server;
  server.StartWithBroker(1 /*backlog*/, false /*fail_server_socket_creation*/);
}

TEST_F(TCPSocketTest, BrokerCreateTCPServerSocketFailure) {
  TestServer server;
  server.StartWithBroker(1 /*backlog*/, true /*fail_server_socket_creation*/);
}
#endif  // BUILDFLAG(IS_WIN)

TEST_F(TCPSocketTest, ReadAndWrite) {}

TEST_F(TCPSocketTest, CannotConnectToWrongInterface) {}

TEST_F(TCPSocketTest, ServerReceivesMultipleAccept) {}

// Check that accepted sockets can't be upgraded to TLS, since UpgradeToTLS only
// supports the client side of a TLS handshake.
TEST_F(TCPSocketTest, AcceptedSocketCantUpgradeToTLS) {}

// Tests that if a socket is closed, the other side can observe that the pipes
// are broken.
TEST_F(TCPSocketTest, SocketClosed) {}

TEST_F(TCPSocketTest, ReadPipeClosed) {}

TEST_F(TCPSocketTest, WritePipeClosed) {}

// Tests that if the server socket is destroyed, any connected sockets that it
// handed out remain alive.
TEST_F(TCPSocketTest, ServerSocketClosedAcceptedSocketAlive) {}

// Tests both async and sync cases.
class TCPSocketWithMockSocketTest
    : public TCPSocketTest,
      public ::testing::WithParamInterface<net::IoMode> {};

INSTANTIATE_TEST_SUITE_P();

// Tests that a server socket handles Accept() correctly when the underlying
// implementation completes Accept() in sync and async mode.
TEST_P(TCPSocketWithMockSocketTest,
       ServerAcceptClientConnectionWithMockSocket) {}

// Tests that TCPServerSocket::Accept() is used with a non-null
// SocketObserver and that the observer is invoked when a read error
// occurs.
TEST_P(TCPSocketWithMockSocketTest, ServerAcceptWithObserverReadError) {}

// Tests that TCPServerSocket::Accept() is used with a non-null SocketObserver
// and that the observer is invoked when a write error occurs.
TEST_P(TCPSocketWithMockSocketTest, ServerAcceptWithObserverWriteError) {}

TEST_P(TCPSocketWithMockSocketTest, ReadAndWriteMultiple) {}

TEST_P(TCPSocketWithMockSocketTest, PartialStreamSocketWrite) {}

TEST_P(TCPSocketWithMockSocketTest, ReadError) {}

TEST_P(TCPSocketWithMockSocketTest, WriteError) {}

TEST_P(TCPSocketWithMockSocketTest, InitialTCPConnectedSocketOptions) {}

TEST_P(TCPSocketWithMockSocketTest, InitialTCPConnectedSocketOptionsFails) {}

// Simulates the initial connection attempt failing, followed by another
// attempt. Used to simulate cases where the BeforeConnectionCallback is
// invoked multiple times.
TEST_P(TCPSocketWithMockSocketTest,
       InitialTCPConnectedSocketSucceedsOnSecondAttempt) {}

TEST_P(TCPSocketWithMockSocketTest, SetBufferSizes) {}

TEST_P(TCPSocketWithMockSocketTest, SetBufferSizesFails) {}

TEST_F(TCPSocketWithMockSocketTest, SetNoDelayAndKeepAlive) {}

TEST_F(TCPSocketWithMockSocketTest, SetNoDelayFails) {}

TEST_F(TCPSocketWithMockSocketTest, SetOptionsAfterTLSUpgrade) {}

TEST_F(TCPSocketWithMockSocketTest, SocketDestroyedBeforeConnectCompletes) {}

// Tests the case where net::ServerSocket::Listen() succeeds but
// net::ServerSocket::GetLocalAddress() fails. This should still be considered
// as a failure.
TEST(TCPServerSocketTest, GetLocalAddressFailedInListen) {}

}  // namespace network