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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_NETWORK_P2P_SOCKET_TEST_UTILS_H_
#define SERVICES_NETWORK_P2P_SOCKET_TEST_UTILS_H_
#include <stdint.h>
#include <list>
#include <memory>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/net_errors.h"
#include "net/base/network_interfaces.h"
#include "net/log/net_log_with_source.h"
#include "net/socket/stream_socket.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/p2p/socket.h"
#include "services/network/public/cpp/p2p_param_traits.h"
#include "services/network/public/mojom/p2p.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc/rtc_base/time_utils.h"
namespace network {
const char kTestLocalIpAddress[] = "123.44.22.4";
const char kTestIpAddress1[] = "123.44.22.31";
const uint16_t kTestPort1 = 234;
const char kTestIpAddress2[] = "133.11.22.33";
const uint16_t kTestPort2 = 543;
class FakeP2PSocketDelegate : public P2PSocket::Delegate {
public:
FakeP2PSocketDelegate();
~FakeP2PSocketDelegate() override;
// P2PSocket::Delegate interface.
void DestroySocket(P2PSocket* socket) override;
void DumpPacket(base::span<const uint8_t> data, bool incoming) override;
void ExpectDestruction(std::unique_ptr<P2PSocket> socket);
private:
std::vector<std::unique_ptr<P2PSocket>> sockets_to_be_destroyed_;
};
class FakeSocket : public net::StreamSocket {
public:
explicit FakeSocket(std::string* written_data);
~FakeSocket() override;
void set_async_write(bool async_write) { async_write_ = async_write; }
void set_error_on_next_write(int code) { error_on_next_write_ = code; }
void AppendInputData(std::string_view data);
int input_pos() const { return input_pos_; }
bool read_pending() const { return read_pending_; }
void SetPeerAddress(const net::IPEndPoint& peer_address);
void SetLocalAddress(const net::IPEndPoint& local_address);
// net::Socket implementation.
int Read(net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback) override;
int Write(
net::IOBuffer* buf,
int buf_len,
net::CompletionOnceCallback callback,
const net::NetworkTrafficAnnotationTag& traffic_annotation) override;
int SetReceiveBufferSize(int32_t size) override;
int SetSendBufferSize(int32_t size) override;
int Connect(net::CompletionOnceCallback callback) override;
void Disconnect() override;
bool IsConnected() const override;
bool IsConnectedAndIdle() const override;
int GetPeerAddress(net::IPEndPoint* address) const override;
int GetLocalAddress(net::IPEndPoint* address) const override;
const net::NetLogWithSource& NetLog() const override;
bool WasEverUsed() const override;
net::NextProto GetNegotiatedProtocol() const override;
bool GetSSLInfo(net::SSLInfo* ssl_info) override;
int64_t GetTotalReceivedBytes() const override;
void ApplySocketTag(const net::SocketTag& tag) override {}
private:
void DoAsyncWrite(scoped_refptr<net::IOBuffer> buf,
int buf_len,
net::CompletionOnceCallback callback);
bool read_pending_;
scoped_refptr<net::IOBuffer> read_buffer_;
int read_buffer_size_;
net::CompletionOnceCallback read_callback_;
std::string input_data_;
int input_pos_;
raw_ptr<std::string> written_data_;
bool async_write_;
bool write_pending_;
int error_on_next_write_ = 0;
net::IPEndPoint peer_address_;
net::IPEndPoint local_address_;
net::NetLogWithSource net_log_;
};
class FakeSocketClient : public mojom::P2PSocketClient {
public:
FakeSocketClient(
mojo::PendingRemote<mojom::P2PSocket> socket,
mojo::PendingReceiver<mojom::P2PSocketClient> client_receiver);
~FakeSocketClient() override;
// mojom::P2PSocketClient interface.
MOCK_METHOD(void,
SocketCreated,
(const net::IPEndPoint&, const net::IPEndPoint&));
MOCK_METHOD(void, SendComplete, (const P2PSendPacketMetrics&));
MOCK_METHOD(void,
DataReceived,
(const std::vector<network::mojom::P2PReceivedPacketPtr>));
MOCK_METHOD(void,
SendBatchComplete,
(const std::vector<P2PSendPacketMetrics>&));
bool connection_error() { return disconnect_error_; }
private:
mojo::Remote<mojom::P2PSocket> socket_;
mojo::Receiver<mojom::P2PSocketClient> receiver_;
bool disconnect_error_ = false;
};
class FakeNetworkNotificationClient
: public mojom::P2PNetworkNotificationClient {
public:
FakeNetworkNotificationClient(
base::OnceClosure closure,
mojo::PendingReceiver<mojom::P2PNetworkNotificationClient>
notification_client);
~FakeNetworkNotificationClient() override;
void NetworkListChanged(
const std::vector<::net::NetworkInterface>& networks,
const ::net::IPAddress& default_ipv4_local_address,
const ::net::IPAddress& default_ipv6_local_address) override;
bool get_network_list_changed() { return network_list_changed_; }
private:
mojo::Receiver<mojom::P2PNetworkNotificationClient> notification_client_;
bool network_list_changed_ = false;
base::OnceClosure closure_;
};
void CreateRandomPacket(std::vector<uint8_t>* packet);
void CreateStunRequest(std::vector<uint8_t>* packet);
void CreateStunResponse(std::vector<uint8_t>* packet);
void CreateStunError(std::vector<uint8_t>* packet);
net::IPEndPoint ParseAddress(const std::string& ip_str, uint16_t port);
MATCHER_P(MatchMessage, type, "") {
return arg->type() == type;
}
MATCHER_P2(MatchSendPacketMetrics, rtc_packet_id, test_start_time, "") {
return arg.rtc_packet_id == rtc_packet_id &&
arg.send_time_ms >= test_start_time &&
arg.send_time_ms <= webrtc::TimeMillis();
}
// Creates a GMock matcher that matches `base::span` to `std::vector`.
MATCHER_P(SpanEq, expected, "") {
return arg == base::as_byte_span(expected);
}
} // namespace network
#endif // SERVICES_NETWORK_P2P_SOCKET_TEST_UTILS_H_
|