1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/openscreen_platform/tls_connection_factory.h"
#include <iostream>
#include <memory>
#include <utility>
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "components/openscreen_platform/network_context.h"
#include "components/openscreen_platform/tls_client_connection.h"
#include "net/base/net_errors.h"
#include "services/network/public/cpp/network_context_getter.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/test/test_network_context.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::NiceMock;
using ::testing::StrictMock;
using openscreen::Error;
using openscreen::TlsConnection;
using openscreen::TlsConnectOptions;
namespace openscreen_platform {
namespace {
const openscreen::IPEndpoint kValidOpenscreenEndpoint{
openscreen::IPAddress{192, 168, 0, 1}, 80};
class MockTlsConnectionFactoryClient
: public openscreen::TlsConnectionFactory::Client {
public:
MOCK_METHOD(void,
OnAccepted,
(openscreen::TlsConnectionFactory*,
std::vector<uint8_t>,
std::unique_ptr<TlsConnection>),
(override));
MOCK_METHOD(void,
OnConnected,
(openscreen::TlsConnectionFactory*,
std::vector<uint8_t>,
std::unique_ptr<TlsConnection>),
(override));
MOCK_METHOD(void,
OnConnectionFailed,
(openscreen::TlsConnectionFactory*,
const openscreen::IPEndpoint&),
(override));
MOCK_METHOD(void,
OnError,
(openscreen::TlsConnectionFactory*, const Error&),
(override));
};
class FakeNetworkContext : public network::TestNetworkContext {
public:
void CreateTCPConnectedSocket(
const std::optional<net::IPEndPoint>& local_addr,
const net::AddressList& remote_addr_list,
network::mojom::TCPConnectedSocketOptionsPtr tcp_connected_socket_options,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
mojo::PendingReceiver<network::mojom::TCPConnectedSocket> socket,
mojo::PendingRemote<network::mojom::SocketObserver> observer,
CreateTCPConnectedSocketCallback callback) override {
++times_called_;
callback_ = std::move(callback);
}
int times_called() { return times_called_; }
void ExecuteCreateCallback(int32_t net_result) {
std::move(callback_).Run(net_result, std::nullopt, std::nullopt,
mojo::ScopedDataPipeConsumerHandle{},
mojo::ScopedDataPipeProducerHandle{});
}
private:
CreateTCPConnectedSocketCallback callback_;
int times_called_ = 0;
};
} // namespace
class TlsConnectionFactoryTest : public ::testing::Test {
public:
void SetUp() override {
mock_network_context = std::make_unique<FakeNetworkContext>();
SetNetworkContextGetter(base::BindRepeating(
&TlsConnectionFactoryTest::GetNetworkContext, base::Unretained(this)));
}
void TearDown() override {
SetNetworkContextGetter(network::NetworkContextGetter());
}
protected:
network::mojom::NetworkContext* GetNetworkContext() {
return mock_network_context.get();
}
base::test::TaskEnvironment task_environment_;
std::unique_ptr<FakeNetworkContext> mock_network_context;
};
TEST_F(TlsConnectionFactoryTest, CallsNetworkContextCreateMethod) {
StrictMock<MockTlsConnectionFactoryClient> mock_client;
TlsConnectionFactory factory(mock_client);
factory.Connect(kValidOpenscreenEndpoint, TlsConnectOptions{});
mock_network_context->ExecuteCreateCallback(net::OK);
EXPECT_EQ(1, mock_network_context->times_called());
}
TEST_F(TlsConnectionFactoryTest,
CallsOnConnectionFailedWhenNetworkContextReportsError) {
StrictMock<MockTlsConnectionFactoryClient> mock_client;
TlsConnectionFactory factory(mock_client);
EXPECT_CALL(mock_client,
OnConnectionFailed(&factory, kValidOpenscreenEndpoint));
factory.Connect(kValidOpenscreenEndpoint, TlsConnectOptions{});
mock_network_context->ExecuteCreateCallback(net::ERR_FAILED);
EXPECT_EQ(1, mock_network_context->times_called());
base::RunLoop().RunUntilIdle();
}
} // namespace openscreen_platform
|