1 // Copyright 2019 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 "osp/impl/quic/testing/quic_test_support.h"
6
7 #include <memory>
8
9 #include "osp/impl/quic/quic_client.h"
10 #include "osp/impl/quic/quic_server.h"
11 #include "osp/public/network_service_manager.h"
12 #include "platform/test/fake_task_runner.h"
13
14 namespace openscreen {
15 namespace osp {
16
FakeQuicBridge(FakeTaskRunner * task_runner,ClockNowFunctionPtr now_function)17 FakeQuicBridge::FakeQuicBridge(FakeTaskRunner* task_runner,
18 ClockNowFunctionPtr now_function)
19 : task_runner_(task_runner) {
20 fake_bridge =
21 std::make_unique<FakeQuicConnectionFactoryBridge>(kControllerEndpoint);
22
23 controller_demuxer = std::make_unique<MessageDemuxer>(
24 now_function, MessageDemuxer::kDefaultBufferLimit);
25 receiver_demuxer = std::make_unique<MessageDemuxer>(
26 now_function, MessageDemuxer::kDefaultBufferLimit);
27
28 auto fake_client_factory =
29 std::make_unique<FakeClientQuicConnectionFactory>(fake_bridge.get());
30 client_socket_ =
31 std::make_unique<FakeUdpSocket>(task_runner_, fake_client_factory.get());
32
33 quic_client = std::make_unique<QuicClient>(
34 controller_demuxer.get(), std::move(fake_client_factory),
35 &mock_client_observer, now_function, task_runner);
36
37 auto fake_server_factory =
38 std::make_unique<FakeServerQuicConnectionFactory>(fake_bridge.get());
39 server_socket_ =
40 std::make_unique<FakeUdpSocket>(task_runner_, fake_server_factory.get());
41 ServerConfig config;
42 config.connection_endpoints.push_back(kReceiverEndpoint);
43 quic_server = std::make_unique<QuicServer>(
44 config, receiver_demuxer.get(), std::move(fake_server_factory),
45 &mock_server_observer, now_function, task_runner);
46
47 quic_client->Start();
48 quic_server->Start();
49 }
50
51 FakeQuicBridge::~FakeQuicBridge() = default;
52
PostClientPacket()53 void FakeQuicBridge::PostClientPacket() {
54 UdpPacket packet;
55 packet.set_socket(client_socket_.get());
56 client_socket_->MockReceivePacket(std::move(packet));
57 }
58
PostServerPacket()59 void FakeQuicBridge::PostServerPacket() {
60 UdpPacket packet;
61 packet.set_socket(server_socket_.get());
62 server_socket_->MockReceivePacket(std::move(packet));
63 }
64
PostPacketsUntilIdle()65 void FakeQuicBridge::PostPacketsUntilIdle() {
66 bool client_idle = fake_bridge->client_idle();
67 bool server_idle = fake_bridge->server_idle();
68 if (!client_idle || !server_idle) {
69 PostClientPacket();
70 PostServerPacket();
71 task_runner_->PostTask([this]() { this->PostPacketsUntilIdle(); });
72 }
73 }
74
RunTasksUntilIdle()75 void FakeQuicBridge::RunTasksUntilIdle() {
76 PostClientPacket();
77 PostServerPacket();
78 task_runner_->PostTask(
79 std::bind(&FakeQuicBridge::PostPacketsUntilIdle, this));
80 task_runner_->RunTasksUntilIdle();
81 }
82
83 } // namespace osp
84 } // namespace openscreen
85