#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 = …;
const net::BackoffEntry::Policy kTestBackoffPolicy = …;
std::vector<GURL> BuildEndpoints() { … }
void FillLoginRequest(mcs_proto::LoginRequest* login_request) { … }
double CalculateBackoff(int num_attempts) { … }
void ReadContinuation(std::unique_ptr<google::protobuf::MessageLite> message) { … }
void WriteContinuation() { … }
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() { … }
}
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() { … }
TEST_F(ConnectionFactoryImplTest, Initialize) { … }
TEST_F(ConnectionFactoryImplTest, ConnectSuccess) { … }
TEST_F(ConnectionFactoryImplTest, ConnectFail) { … }
TEST_F(ConnectionFactoryImplTest, FailThenSucceed) { … }
TEST_F(ConnectionFactoryImplTest, MultipleFailuresThenSucceed) { … }
TEST_F(ConnectionFactoryImplTest, FailThenNetworkChangeEvent) { … }
TEST_F(ConnectionFactoryImplTest, CanarySucceedsThenDisconnects) { … }
TEST_F(ConnectionFactoryImplTest, CanarySucceedsRetryDuringLogin) { … }
TEST_F(ConnectionFactoryImplTest, FailViaSignalReset) { … }
TEST_F(ConnectionFactoryImplTest, IgnoreResetWhileConnecting) { … }
TEST_F(ConnectionFactoryImplTest, SignalResetRestoresBackoff) { … }
TEST_F(ConnectionFactoryImplTest,
ShouldNotIncreaseBackoffDelayOnNetworkChange) { … }
TEST_F(ConnectionFactoryImplTest,
ShouldRetryWithSmallDelayAfterManyNetworkChanges) { … }
TEST_F(ConnectionFactoryImplTest, SuppressConnectWhenNoNetwork) { … }
TEST_F(ConnectionFactoryImplTest, NetworkChangeBeforeFirstConnection) { … }
TEST_F(ConnectionFactoryImplTest, ConnectionResetRace) { … }
TEST_F(ConnectionFactoryImplTest, MultipleFailuresWrapClientEvents) { … }
TEST_F(ConnectionFactoryImplTest,
ShouldConnectWhenNetworkChangedDuringHandshake) { … }
}