File: fake_udp_socket.h

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (93 lines) | stat: -rw-r--r-- 3,186 bytes parent folder | download | duplicates (4)
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
// 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_FAKE_UDP_SOCKET_H_
#define PLATFORM_TEST_FAKE_UDP_SOCKET_H_

#include <algorithm>
#include <queue>

#include "gmock/gmock.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"
#include "util/osp_logging.h"

namespace openscreen {

class FakeUdpSocket : public UdpSocket {
 public:
  class MockClient : public UdpSocket::Client {
   public:
    MOCK_METHOD1(OnBound, void(UdpSocket*));
    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);
    }
  };

  explicit FakeUdpSocket(TaskRunner* task_runner = nullptr,
                         Client* client = nullptr,
                         Version version = Version::kV4);
  ~FakeUdpSocket() 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(); }

  void MockReceivePacket(UdpPacket packet);

 private:
  void ProcessConfigurationMethod(std::queue<Error>* errors);

  Client* const 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_;
};

}  // namespace openscreen

#endif  // PLATFORM_TEST_FAKE_UDP_SOCKET_H_