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
|
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PLATFORM_TEST_MOCK_UDP_SOCKET_H_
#define PLATFORM_TEST_MOCK_UDP_SOCKET_H_
#include <algorithm>
#include <memory>
#include <queue>
#include "gmock/gmock.h"
#include "platform/api/logging.h"
#include "platform/api/time.h"
#include "platform/api/udp_socket.h"
#include "platform/test/fake_clock.h"
#include "platform/test/fake_task_runner.h"
namespace openscreen {
namespace platform {
// TODO(rwkeane): Rename this class to "FakeUdpSocket".
class MockUdpSocket : public UdpSocket {
public:
class MockClient : public UdpSocket::Client {
public:
MOCK_METHOD2(OnError, void(UdpSocket*, Error));
MOCK_METHOD2(OnSendError, void(UdpSocket*, Error));
MOCK_METHOD2(OnReadInternal, void(UdpSocket*, const ErrorOr<UdpPacket>&));
void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) override {
OnReadInternal(socket, packet);
}
};
static std::unique_ptr<MockUdpSocket> CreateDefault(
Version version = Version::kV4);
MockUdpSocket(TaskRunner* task_runner,
Client* client,
Version version = Version::kV4);
~MockUdpSocket() override;
bool IsIPv4() const override;
bool IsIPv6() const override;
IPEndpoint GetLocalEndpoint() const override;
// UdpSocket overrides
void Bind() override;
void SendMessage(const void* data,
size_t length,
const IPEndpoint& dest) override;
void SetMulticastOutboundInterface(NetworkInterfaceIndex interface) override;
void JoinMulticastGroup(const IPAddress& address,
NetworkInterfaceIndex interface) override;
void SetDscp(DscpMode mode) override;
// Operatons to queue errors to be returned by the above functions
void EnqueueBindResult(Error error) { bind_errors_.push(error); }
void EnqueueSendResult(Error error) { send_errors_.push(error); }
void EnqueueSetMulticastOutboundInterfaceResult(Error error) {
set_multicast_outbound_interface_errors_.push(error);
}
void EnqueueJoinMulticastGroupResult(Error error) {
join_multicast_group_errors_.push(error);
}
void EnqueueSetDscpResult(Error error) { set_dscp_errors_.push(error); }
// Accessors for the size of the internal error queues.
size_t bind_queue_size() { return bind_errors_.size(); }
size_t send_queue_size() { return send_errors_.size(); }
size_t set_multicast_outbound_interface_queue_size() {
return set_multicast_outbound_interface_errors_.size();
}
size_t join_multicast_group_queue_size() {
return join_multicast_group_errors_.size();
}
size_t set_dscp_queue_size() { return set_dscp_errors_.size(); }
// Posts a task to call client_'s OnRead method
void MockReceivePacket(UdpPacket packet) { OnRead(std::move(packet)); }
MockUdpSocket::MockClient* client_mock() { return fake_client_.get(); }
private:
void ProcessConfigurationMethod(std::queue<Error>* errors);
void Close() override {}
Client* client_;
Version version_;
// Queues for the response to calls above
std::queue<Error> bind_errors_;
std::queue<Error> send_errors_;
std::queue<Error> set_multicast_outbound_interface_errors_;
std::queue<Error> join_multicast_group_errors_;
std::queue<Error> set_dscp_errors_;
// Fake implementations to be set by CreateDefault().
std::unique_ptr<FakeTaskRunner> fake_task_runner_;
std::unique_ptr<MockUdpSocket::MockClient> fake_client_;
std::unique_ptr<FakeClock> fake_clock_;
};
} // namespace platform
} // namespace openscreen
#endif // PLATFORM_TEST_MOCK_UDP_SOCKET_H_
|