• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PLATFORM_TEST_FAKE_UDP_SOCKET_H_
6 #define PLATFORM_TEST_FAKE_UDP_SOCKET_H_
7 
8 #include <algorithm>
9 #include <queue>
10 
11 #include "gmock/gmock.h"
12 #include "platform/api/time.h"
13 #include "platform/api/udp_socket.h"
14 #include "platform/test/fake_clock.h"
15 #include "platform/test/fake_task_runner.h"
16 #include "util/osp_logging.h"
17 
18 namespace openscreen {
19 
20 class FakeUdpSocket : public UdpSocket {
21  public:
22   class MockClient : public UdpSocket::Client {
23    public:
24     MOCK_METHOD1(OnBound, void(UdpSocket*));
25     MOCK_METHOD2(OnError, void(UdpSocket*, Error));
26     MOCK_METHOD2(OnSendError, void(UdpSocket*, Error));
27     MOCK_METHOD2(OnReadInternal, void(UdpSocket*, const ErrorOr<UdpPacket>&));
28 
OnRead(UdpSocket * socket,ErrorOr<UdpPacket> packet)29     void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) override {
30       OnReadInternal(socket, packet);
31     }
32   };
33 
34   explicit FakeUdpSocket(TaskRunner* task_runner = nullptr,
35                          Client* client = nullptr,
36                          Version version = Version::kV4);
37   ~FakeUdpSocket() override;
38 
39   bool IsIPv4() const override;
40   bool IsIPv6() const override;
41   IPEndpoint GetLocalEndpoint() const override;
42 
43   // UdpSocket overrides
44   void Bind() override;
45   void SendMessage(const void* data,
46                    size_t length,
47                    const IPEndpoint& dest) override;
48   void SetMulticastOutboundInterface(NetworkInterfaceIndex interface) override;
49   void JoinMulticastGroup(const IPAddress& address,
50                           NetworkInterfaceIndex interface) override;
51   void SetDscp(DscpMode mode) override;
52 
53   // Operatons to queue errors to be returned by the above functions
EnqueueBindResult(Error error)54   void EnqueueBindResult(Error error) { bind_errors_.push(error); }
EnqueueSendResult(Error error)55   void EnqueueSendResult(Error error) { send_errors_.push(error); }
EnqueueSetMulticastOutboundInterfaceResult(Error error)56   void EnqueueSetMulticastOutboundInterfaceResult(Error error) {
57     set_multicast_outbound_interface_errors_.push(error);
58   }
EnqueueJoinMulticastGroupResult(Error error)59   void EnqueueJoinMulticastGroupResult(Error error) {
60     join_multicast_group_errors_.push(error);
61   }
EnqueueSetDscpResult(Error error)62   void EnqueueSetDscpResult(Error error) { set_dscp_errors_.push(error); }
63 
64   // Accessors for the size of the internal error queues.
bind_queue_size()65   size_t bind_queue_size() { return bind_errors_.size(); }
send_queue_size()66   size_t send_queue_size() { return send_errors_.size(); }
set_multicast_outbound_interface_queue_size()67   size_t set_multicast_outbound_interface_queue_size() {
68     return set_multicast_outbound_interface_errors_.size();
69   }
join_multicast_group_queue_size()70   size_t join_multicast_group_queue_size() {
71     return join_multicast_group_errors_.size();
72   }
set_dscp_queue_size()73   size_t set_dscp_queue_size() { return set_dscp_errors_.size(); }
74 
75   void MockReceivePacket(UdpPacket packet);
76 
77  private:
78   void ProcessConfigurationMethod(std::queue<Error>* errors);
79 
80   Client* const client_;
81   Version version_;
82 
83   // Queues for the response to calls above
84   std::queue<Error> bind_errors_;
85   std::queue<Error> send_errors_;
86   std::queue<Error> set_multicast_outbound_interface_errors_;
87   std::queue<Error> join_multicast_group_errors_;
88   std::queue<Error> set_dscp_errors_;
89 };
90 
91 }  // namespace openscreen
92 
93 #endif  // PLATFORM_TEST_FAKE_UDP_SOCKET_H_
94