Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MockUDPSocket.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * MockUDPSocket.h
17  * Header file for the Mock UDP Socket class
18  * Copyright (C) 2010 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLA_TESTING_MOCKUDPSOCKET_H_
22 #define INCLUDE_OLA_TESTING_MOCKUDPSOCKET_H_
23 
24 #include <cppunit/extensions/HelperMacros.h>
25 
26 #include <ola/base/Macro.h>
28 #include <ola/network/Socket.h>
30 
31 #include <string>
32 #include <queue>
33 
34 namespace ola {
35 namespace testing {
36 
37 /*
38  * The MockUDPSocket allows one to stub out a UDP Socket for testing. The
39  * code-under-test can use this object as it would a UDP socket, and the code
40  * performing the test can verify that the data written matches what it
41  * expects. It does this by calling AddExpectedData(), e.g.
42  *
43  * // add the data we expect, you can call this more than once
44  * udp_socket.AddExpectedData(....);
45  * udp_socket.AddExpectedData(....);
46  * // this calls one of the SendTo methods
47  * MethodToTest(udp_socket);
48  * // verify all the expected data has been consumed
49  * udp_socket.Verify()
50  *
51  * You can also inject packets into the socket by calling InjectData(), this
52  * will trigger the on_read callback that was attached to the socket.
53  */
55  public:
56  MockUDPSocket();
57  ~MockUDPSocket() { Close(); }
58 
59  // These are the socket methods
60  bool Init();
61  bool Bind(const ola::network::IPV4SocketAddress &endpoint);
63  bool Close();
64  ola::io::DescriptorHandle ReadDescriptor() const { return m_dummy_handle; }
65  ola::io::DescriptorHandle WriteDescriptor() const { return m_dummy_handle; }
66  ssize_t SendTo(const uint8_t *buffer,
67  unsigned int size,
68  const ola::network::IPV4Address &ip,
69  unsigned short port) const;
70  ssize_t SendTo(const uint8_t *buffer,
71  unsigned int size,
72  const ola::network::IPV4SocketAddress &dest) const {
73  return SendTo(buffer, size, dest.Host(), dest.Port());
74  }
75  ssize_t SendTo(ola::io::IOVecInterface *data,
76  const ola::network::IPV4Address &ip,
77  unsigned short port) const;
79  const ola::network::IPV4SocketAddress &dest) const {
80  return SendTo(data, dest.Host(), dest.Port());
81  }
82 
83  bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const;
84  bool RecvFrom(
85  uint8_t *buffer,
86  ssize_t *data_read,
87  ola::network::IPV4Address &source) const; // NOLINT(runtime/references)
88  bool RecvFrom(
89  uint8_t *buffer,
90  ssize_t *data_read,
91  ola::network::IPV4Address &source, // NOLINT(runtime/references)
92  uint16_t &port) const; // NOLINT(runtime/references)
93  bool RecvFrom(uint8_t *buffer,
94  ssize_t *data_read,
96  bool EnableBroadcast();
98  bool JoinMulticast(const ola::network::IPV4Address &iface,
99  const ola::network::IPV4Address &group,
100  bool multicast_loop = false);
101  bool LeaveMulticast(const ola::network::IPV4Address &iface,
102  const ola::network::IPV4Address &group);
103 
104  bool SetTos(uint8_t tos);
105 
106  void SetDiscardMode(bool discard_mode) { m_discard_mode = discard_mode; }
107 
108  // these are methods used for verification
109  void AddExpectedData(const uint8_t *data,
110  unsigned int size,
111  const ola::network::IPV4Address &ip,
112  uint16_t port);
113  void AddExpectedData(ola::io::IOQueue *queue,
114  const ola::network::IPV4SocketAddress &dest);
115 
116  // this can be fetched by calling PerformRead() on the socket
117  void InjectData(const uint8_t *data,
118  unsigned int size,
119  const ola::network::IPV4Address &ip,
120  uint16_t port);
121  void InjectData(const uint8_t *data,
122  unsigned int size,
123  const ola::network::IPV4SocketAddress &source);
124  void InjectData(ola::io::IOQueue *ioqueue,
125  const ola::network::IPV4SocketAddress &source);
126 
127  void Verify();
128 
129  bool CheckNetworkParamsMatch(bool init_called,
130  bool bound_to_port,
131  uint16_t port,
132  bool broadcast_set);
133 
134  void SetInterface(const ola::network::IPV4Address &iface);
135 
136  private:
137  typedef struct {
138  const uint8_t *data;
139  unsigned int size;
141  uint16_t port;
142  bool free_data;
143  } expected_call;
144 
145  typedef expected_call received_data;
146 
147  bool m_init_called;
148  // We need a sd so that calls to select continue to work. This isn't used
149  // for anything else.
150  ola::io::DescriptorHandle m_dummy_handle;
151  bool m_bound_to_port;
152  bool m_broadcast_set;
153  uint16_t m_port;
154  uint8_t m_tos;
155  mutable std::queue<expected_call> m_expected_calls;
156  mutable std::queue<received_data> m_received_data;
157  ola::network::IPV4Address m_interface;
158  bool m_discard_mode;
159 
160  uint8_t* IOQueueToBuffer(ola::io::IOQueue *ioqueue,
161  unsigned int *size) const;
162 
163  DISALLOW_COPY_AND_ASSIGN(MockUDPSocket);
164 };
165 
166 
171  public:
172  explicit SocketVerifier(MockUDPSocket *socket)
173  : m_socket(socket) {
174  }
175  ~SocketVerifier() {
176  m_socket->Verify();
177  }
178 
179  private:
180  MockUDPSocket *m_socket;
181 
182  DISALLOW_COPY_AND_ASSIGN(SocketVerifier);
183 };
184 } // namespace testing
185 } // namespace ola
186 #endif // INCLUDE_OLA_TESTING_MOCKUDPSOCKET_H_
bool EnableBroadcast()
Enable broadcasting for this socket.
Definition: MockUDPSocket.cpp:209
Represents Socket Addresses.
bool SetTos(uint8_t tos)
Set the tos field for a socket.
Definition: MockUDPSocket.cpp:236
bool GetSocketAddress(ola::network::IPV4SocketAddress *address) const
Return the local address this socket is bound to.
Definition: MockUDPSocket.cpp:84
Definition: MockUDPSocket.h:170
The interface for UDPSockets.
Definition: Socket.h:45
bool Init()
Initialize the socket.
Definition: MockUDPSocket.cpp:59
bool JoinMulticast(const ola::network::IPV4Address &iface, const ola::network::IPV4Address &group, bool multicast_loop=false)
Join a multicast group.
Definition: MockUDPSocket.cpp:221
Definition: MockUDPSocket.h:54
bool Bind(const ola::network::IPV4SocketAddress &endpoint)
Bind this socket to an external address:port.
Definition: MockUDPSocket.cpp:77
Represents a IPv4 Address.
Definition: IPV4Address.h:55
ssize_t SendTo(const uint8_t *buffer, unsigned int size, const ola::network::IPV4Address &ip, unsigned short port) const
Send data on this UDPSocket.
Definition: MockUDPSocket.cpp:102
ola::io::DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition: MockUDPSocket.h:64
ssize_t SendTo(const uint8_t *buffer, unsigned int size, const ola::network::IPV4SocketAddress &dest) const
Send data on this UDPSocket.
Definition: MockUDPSocket.h:70
Definition: IOQueue.h:39
bool Close()
Close the socket.
Definition: MockUDPSocket.cpp:90
Definition: IOVecInterface.h:53
ola::io::DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition: MockUDPSocket.h:65
Represents an IPv4 Address.
ssize_t SendTo(ola::io::IOVecInterface *data, const ola::network::IPV4SocketAddress &dest) const
Send data from an IOVecInterface.
Definition: MockUDPSocket.h:78
Helper macros.
bool SetMulticastInterface(const ola::network::IPV4Address &iface)
Set the outgoing interface to be used for multicast transmission.
Definition: MockUDPSocket.cpp:215
bool LeaveMulticast(const ola::network::IPV4Address &iface, const ola::network::IPV4Address &group)
Leave a multicast group.
Definition: MockUDPSocket.cpp:229
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
bool RecvFrom(uint8_t *buffer, ssize_t *data_read) const
Receive data.
Definition: MockUDPSocket.cpp:155
An IPv4 SocketAddress.
Definition: SocketAddress.h:77
void InjectData(const uint8_t *data, unsigned int size, const ola::network::IPV4Address &ip, uint16_t port)
Definition: MockUDPSocket.cpp:263