1 // Copyright 2022 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "quiche/quic/test_tools/simulator/test_harness.h"
6
7 #include <memory>
8 #include <string>
9
10 #include "absl/strings/str_cat.h"
11 #include "quiche/quic/core/quic_connection.h"
12 #include "quiche/quic/core/quic_types.h"
13 #include "quiche/quic/core/quic_versions.h"
14 #include "quiche/quic/test_tools/quic_test_utils.h"
15 #include "quiche/quic/test_tools/simulator/packet_filter.h"
16 #include "quiche/quic/test_tools/simulator/port.h"
17 #include "quiche/quic/test_tools/simulator/quic_endpoint_base.h"
18 #include "quiche/quic/test_tools/simulator/simulator.h"
19
20 namespace quic::simulator {
21
22 class LoseEveryNFilter : public PacketFilter {
23 public:
LoseEveryNFilter(Endpoint * input,int n)24 LoseEveryNFilter(Endpoint* input, int n)
25 : PacketFilter(input->simulator(),
26 absl::StrCat(input->name(), " (loss filter)"), input),
27 n_(n) {}
28
29 protected:
FilterPacket(const Packet &)30 bool FilterPacket(const Packet& /*packet*/) {
31 ++counter_;
32 return (counter_ % n_) != 0;
33 }
34
35 private:
36 int n_;
37 int counter_ = 0;
38 };
39
QuicEndpointWithConnection(Simulator * simulator,const std::string & name,const std::string & peer_name,Perspective perspective,const ParsedQuicVersionVector & supported_versions)40 QuicEndpointWithConnection::QuicEndpointWithConnection(
41 Simulator* simulator, const std::string& name, const std::string& peer_name,
42 Perspective perspective, const ParsedQuicVersionVector& supported_versions)
43 : QuicEndpointBase(simulator, name, peer_name) {
44 connection_ = std::make_unique<QuicConnection>(
45 quic::test::TestConnectionId(0x10), GetAddressFromName(name),
46 GetAddressFromName(peer_name), simulator, simulator->GetAlarmFactory(),
47 &writer_, /*owns_writer=*/false, perspective, supported_versions,
48 connection_id_generator_);
49 connection_->SetSelfAddress(GetAddressFromName(name));
50 }
51
TestHarness()52 TestHarness::TestHarness() : switch_(&simulator_, "Switch", 8, 2 * kBdp) {}
53
WireUpEndpoints()54 void TestHarness::WireUpEndpoints() {
55 client_link_.emplace(client_, switch_.port(1), kClientBandwidth,
56 kClientPropagationDelay);
57 server_link_.emplace(server_, switch_.port(2), kServerBandwidth,
58 kServerPropagationDelay);
59 }
60
WireUpEndpointsWithLoss(int lose_every_n)61 void TestHarness::WireUpEndpointsWithLoss(int lose_every_n) {
62 client_filter_ = std::make_unique<LoseEveryNFilter>(client_, lose_every_n);
63 server_filter_ = std::make_unique<LoseEveryNFilter>(server_, lose_every_n);
64 client_link_.emplace(client_filter_.get(), switch_.port(1), kClientBandwidth,
65 kClientPropagationDelay);
66 server_link_.emplace(server_filter_.get(), switch_.port(2), kServerBandwidth,
67 kServerPropagationDelay);
68 }
69
70 } // namespace quic::simulator
71