chromium/google_apis/gcm/engine/connection_factory_impl_unittest.cc

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

#include "google_apis/gcm/engine/connection_factory_impl.h"

#include <cmath>
#include <memory>
#include <optional>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "google_apis/gcm/base/gcm_features.h"
#include "google_apis/gcm/base/mcs_util.h"
#include "google_apis/gcm/engine/connection_factory.h"
#include "google_apis/gcm/engine/fake_connection_handler.h"
#include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "net/base/backoff_entry.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_test_util.h"
#include "services/network/network_context.h"
#include "services/network/network_service.h"
#include "services/network/public/mojom/proxy_resolving_socket.mojom.h"
#include "services/network/test/fake_test_cert_verifier_params_factory.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"

class Policy;

namespace gcm {
namespace {

const char kMCSEndpoint[] =;
const char kMCSEndpoint2[] =;

const int kBackoffDelayMs =;
const int kBackoffMultiplier =;

// A backoff policy with small enough delays that tests aren't burdened.
const net::BackoffEntry::Policy kTestBackoffPolicy =;

std::vector<GURL> BuildEndpoints() {}

// Used as a builder for test login requests.
void FillLoginRequest(mcs_proto::LoginRequest* login_request) {}

// Helper for calculating total expected exponential backoff delay given an
// arbitrary number of failed attempts. See BackoffEntry::CalculateReleaseTime.
double CalculateBackoff(int num_attempts) {}

void ReadContinuation(std::unique_ptr<google::protobuf::MessageLite> message) {}

void WriteContinuation() {}

// A connection factory that stubs out network requests and overrides the
// backoff policy.
class TestConnectionFactoryImpl : public ConnectionFactoryImpl {};

TestConnectionFactoryImpl::TestConnectionFactoryImpl(
    GetProxyResolvingFactoryCallback get_socket_factory_callback,
    base::RepeatingClosure finished_callback)
    :{}

TestConnectionFactoryImpl::~TestConnectionFactoryImpl() {}

void TestConnectionFactoryImpl::StartConnection(
    bool ignore_connection_failure) {}

std::unique_ptr<net::BackoffEntry>
TestConnectionFactoryImpl::CreateBackoffEntry(
    const net::BackoffEntry::Policy* const policy) {}

std::unique_ptr<ConnectionHandler>
TestConnectionFactoryImpl::CreateConnectionHandler(
    base::TimeDelta read_timeout,
    const ConnectionHandler::ProtoReceivedCallback& read_callback,
    const ConnectionHandler::ProtoSentCallback& write_callback,
    const ConnectionHandler::ConnectionChangedCallback& connection_callback) {}

base::TimeTicks TestConnectionFactoryImpl::NowTicks() {}

void TestConnectionFactoryImpl::SetConnectResult(int connect_result) {}

void TestConnectionFactoryImpl::SetMultipleConnectResults(
    int connect_result,
    int num_expected_attempts) {}

void TestConnectionFactoryImpl::SetDelayLogin(bool delay_login) {}

void TestConnectionFactoryImpl::SetSocketError() {}

}  // namespace

class ConnectionFactoryImplTest
    : public testing::Test,
      public ConnectionFactory::ConnectionListener {};

ConnectionFactoryImplTest::ConnectionFactoryImplTest()
    :{}

ConnectionFactoryImplTest::~ConnectionFactoryImplTest() = default;

void ConnectionFactoryImplTest::WaitForConnections() {}

void ConnectionFactoryImplTest::ConnectionsComplete() {}

void ConnectionFactoryImplTest::OnConnected(
    const GURL& current_server,
    const net::IPEndPoint& ip_endpoint) {}

void ConnectionFactoryImplTest::OnDisconnected() {}

// Verify building a connection handler works.
TEST_F(ConnectionFactoryImplTest, Initialize) {}

// An initial successful connection should not result in backoff.
TEST_F(ConnectionFactoryImplTest, ConnectSuccess) {}

// A connection failure should result in backoff, and attempting the fallback
// endpoint next.
TEST_F(ConnectionFactoryImplTest, ConnectFail) {}

// A connection success after a failure should reset backoff.
TEST_F(ConnectionFactoryImplTest, FailThenSucceed) {}

// Multiple connection failures should retry with an exponentially increasing
// backoff, then reset on success.
TEST_F(ConnectionFactoryImplTest, MultipleFailuresThenSucceed) {}

// Network change events should trigger canary connections.
TEST_F(ConnectionFactoryImplTest, FailThenNetworkChangeEvent) {}

// Verify that we reconnect even if a canary succeeded then disconnected while
// a backoff was pending.
TEST_F(ConnectionFactoryImplTest, CanarySucceedsThenDisconnects) {}

// Verify that if a canary connects, but hasn't finished the handshake, a
// pending backoff attempt doesn't interrupt the connection.
TEST_F(ConnectionFactoryImplTest, CanarySucceedsRetryDuringLogin) {}

// Fail after successful connection via signal reset.
TEST_F(ConnectionFactoryImplTest, FailViaSignalReset) {}

TEST_F(ConnectionFactoryImplTest, IgnoreResetWhileConnecting) {}

// Go into backoff due to connection failure. On successful connection, receive
// a signal reset. The original backoff should be restored and extended, rather
// than a new backoff starting from scratch.
TEST_F(ConnectionFactoryImplTest, SignalResetRestoresBackoff) {}

TEST_F(ConnectionFactoryImplTest,
       ShouldNotIncreaseBackoffDelayOnNetworkChange) {}

// Tests that if there are a lot of network changes resulting in net error
// (could happen in some cases with VPN connection), backoff delay does not
// increase. This is needed to avoid huge delays while there is no network
// connection.
TEST_F(ConnectionFactoryImplTest,
       ShouldRetryWithSmallDelayAfterManyNetworkChanges) {}

// When the network is disconnected, close the socket and suppress further
// connection attempts until the network returns.
TEST_F(ConnectionFactoryImplTest, SuppressConnectWhenNoNetwork) {}

// Receiving a network change event before the initial connection should have
// no effect.
TEST_F(ConnectionFactoryImplTest, NetworkChangeBeforeFirstConnection) {}

// Test that if the client attempts to reconnect while a connection is already
// open, we don't crash.
TEST_F(ConnectionFactoryImplTest, ConnectionResetRace) {}

TEST_F(ConnectionFactoryImplTest, MultipleFailuresWrapClientEvents) {}

TEST_F(ConnectionFactoryImplTest,
       ShouldConnectWhenNetworkChangedDuringHandshake) {}

}  // namespace gcm