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 "platform/test/fake_udp_socket.h"
6
7 #include <utility>
8
9 namespace openscreen {
10
FakeUdpSocket(TaskRunner * task_runner,Client * client,Version version)11 FakeUdpSocket::FakeUdpSocket(TaskRunner* task_runner,
12 Client* client,
13 Version version)
14 : client_(client), version_(version) {}
15
16 FakeUdpSocket::~FakeUdpSocket() = default;
17
IsIPv4() const18 bool FakeUdpSocket::IsIPv4() const {
19 return version_ == UdpSocket::Version::kV4;
20 }
21
IsIPv6() const22 bool FakeUdpSocket::IsIPv6() const {
23 return version_ == UdpSocket::Version::kV6;
24 }
25
GetLocalEndpoint() const26 IPEndpoint FakeUdpSocket::GetLocalEndpoint() const {
27 return IPEndpoint{};
28 }
29
Bind()30 void FakeUdpSocket::Bind() {
31 OSP_CHECK(bind_errors_.size()) << "No Bind responses queued.";
32 ProcessConfigurationMethod(&bind_errors_);
33 }
34
SetMulticastOutboundInterface(NetworkInterfaceIndex interface)35 void FakeUdpSocket::SetMulticastOutboundInterface(
36 NetworkInterfaceIndex interface) {
37 OSP_CHECK(set_multicast_outbound_interface_errors_.size())
38 << "No SetMulticastOutboundInterface responses queued.";
39 ProcessConfigurationMethod(&set_multicast_outbound_interface_errors_);
40 }
41
JoinMulticastGroup(const IPAddress & address,NetworkInterfaceIndex interface)42 void FakeUdpSocket::JoinMulticastGroup(const IPAddress& address,
43 NetworkInterfaceIndex interface) {
44 OSP_CHECK(join_multicast_group_errors_.size())
45 << "No JoinMulticastGroup responses queued.";
46 ProcessConfigurationMethod(&join_multicast_group_errors_);
47 }
48
SetDscp(DscpMode mode)49 void FakeUdpSocket::SetDscp(DscpMode mode) {
50 OSP_CHECK(set_dscp_errors_.size()) << "No SetDscp responses queued.";
51 ProcessConfigurationMethod(&set_dscp_errors_);
52 }
53
MockReceivePacket(UdpPacket packet)54 void FakeUdpSocket::MockReceivePacket(UdpPacket packet) {
55 if (client_) {
56 client_->OnRead(this, std::move(packet));
57 }
58 }
59
ProcessConfigurationMethod(std::queue<Error> * errors)60 void FakeUdpSocket::ProcessConfigurationMethod(std::queue<Error>* errors) {
61 Error error = errors->front();
62 errors->pop();
63
64 if (!error.ok() && client_) {
65 client_->OnError(this, std::move(error));
66 }
67 }
68
SendMessage(const void * data,size_t length,const IPEndpoint & dest)69 void FakeUdpSocket::SendMessage(const void* data,
70 size_t length,
71 const IPEndpoint& dest) {
72 OSP_CHECK(send_errors_.size()) << "No SendMessage responses queued.";
73 Error error = send_errors_.front();
74 send_errors_.pop();
75
76 if (!error.ok() && client_) {
77 client_->OnSendError(this, std::move(error));
78 }
79 }
80
81 } // namespace openscreen
82