chromium/services/network/p2p/socket_udp_unittest.cc

// 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.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "services/network/p2p/socket_udp.h"

#include <stdint.h>

#include <optional>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/containers/circular_deque.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/base/completion_once_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/base/network_anonymization_key.h"
#include "net/log/net_log_with_source.h"
#include "net/socket/datagram_server_socket.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/p2p/socket_test_utils.h"
#include "services/network/p2p/socket_throttler.h"
#include "services/network/public/cpp/p2p_socket_type.h"
#include "services/network/throttling/network_conditions.h"
#include "services/network/throttling/throttling_controller.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/webrtc/rtc_base/time_utils.h"

_;
DeleteArg;
DoAll;
ElementsAre;
Field;
InSequence;
Return;

namespace {

// TODO(nisse): We can't currently use rtc::ScopedFakeClock, because
// we don't link with webrtc rtc_base_tests_utils. So roll our own.

// Creating an object of this class makes rtc::TimeMicros() and
// related functions return zero unless the clock is advanced.
class ScopedFakeClock : public rtc::ClockInterface {};

class FakeDatagramServerSocket : public net::DatagramServerSocket {};

std::unique_ptr<net::DatagramServerSocket> CreateFakeDatagramServerSocket(
    base::circular_deque<FakeDatagramServerSocket::UDPPacket>* sent_packets,
    std::vector<uint16_t>* used_ports,
    ScopedFakeClock* fake_clock,
    net::NetLog* net_log) {}

}  // namespace

namespace network {

class P2PSocketUdpTest : public testing::Test {};

// Verify that we can send STUN messages before we receive anything
// from the other side.
TEST_F(P2PSocketUdpTest, SendStunNoAuth) {}

// Verify that no data packets can be sent before STUN binding has
// finished.
TEST_F(P2PSocketUdpTest, SendDataNoAuth) {}

// Verify that we can send data after we've received STUN request
// from the other side.
TEST_F(P2PSocketUdpTest, SendAfterStunRequest) {}

// Verify that we can send data after we've received STUN response
// from the other side.
TEST_F(P2PSocketUdpTest, SendAfterStunResponse) {}

// Verify messages still cannot be sent to an unathorized host after
// successful binding with different host.
TEST_F(P2PSocketUdpTest, SendAfterStunResponseDifferentHost) {}

TEST_F(P2PSocketUdpTest, BatchesSendAfterSendingAllowed) {}

// Verify throttler not allowing unlimited sending of ICE messages to
// any destination.
TEST_F(P2PSocketUdpTest, ThrottleAfterLimit) {}

// Verify we can send packets to a known destination when ICE throttling is
// active.
TEST_F(P2PSocketUdpTest, ThrottleAfterLimitAfterReceive) {}

// Test that once the limit is hit, the throttling stops at the expected time,
// allowing packets to be sent again.
TEST_F(P2PSocketUdpTest, ThrottlingStopsAtExpectedTimes) {}

// Verify that we can open UDP sockets listening in a given port range,
// and fail if all ports in the range are already in use.
TEST_F(P2PSocketUdpTest, PortRangeImplicitPort) {}

// Verify that we can open a UDP socket listening in a given port included in
// a given valid range.
TEST_F(P2PSocketUdpTest, PortRangeExplictValidPort) {}

// Verify that we cannot open a UDP socket listening in a given port not
// included in a given valid range.
TEST_F(P2PSocketUdpTest, PortRangeExplictInvalidPort) {}

// Verify that we can receive packets from the sockets, and that the
// discontinuous packets are not batched.
TEST_F(P2PSocketUdpTest, ReceiveDiscontinuousPackets) {}

// Verify that we can receive burst packets from the sockets, and that all the
// packets are batched together.
TEST_F(P2PSocketUdpTest, ReceiveBurstPacketsBasic) {}

// Verify that we can receive burst packets, and that the batching size does not
// exceed limit.
TEST_F(P2PSocketUdpTest, ReceiveBurstPacketsExceedingMaxBatchingSize) {}

// Verify that we can receive burst packets, and that the batching cancels if
// buffering time exceeds limit.
TEST_F(P2PSocketUdpTest, ReceiveBurstPacketsExceedingMaxBatchingBuffering) {}

class P2PSocketUdpWithInterceptorTest : public P2PSocketUdpTest {};

TEST_F(P2PSocketUdpWithInterceptorTest, SendPacket) {}

TEST_F(P2PSocketUdpWithInterceptorTest, SendPacketOffline) {}

TEST_F(P2PSocketUdpWithInterceptorTest, SendPacketDelayed) {}

TEST_F(P2PSocketUdpWithInterceptorTest, SendPacketAndRemoveThrottling) {}

TEST_F(P2PSocketUdpWithInterceptorTest, SendPacketDropsLongQueue) {}

TEST_F(P2PSocketUdpWithInterceptorTest, SendPacketWithPacketDrop) {}

TEST_F(P2PSocketUdpWithInterceptorTest, ReceivePackets) {}

// Verify that we can receive Explicit Congestion Notification (ECN) bits
// from the socket after enabling the socket option, while assuming that
// the sender is sending the ECN bits.
TEST_F(P2PSocketUdpWithInterceptorTest, ReceivePacketsWithEcn) {}

TEST_F(P2PSocketUdpWithInterceptorTest, ReceivePacketDelayed) {}

}  // namespace network