JayBeams  0.1
Another project to have fun coding.
ut_make_socket_udp_send.cpp
Go to the documentation of this file.
2 
4 #include <boost/test/unit_test.hpp>
5 
6 /// @test Verify that jb::itch5::make_socket_udp_send compiles.
7 BOOST_AUTO_TEST_CASE(itch5_make_socket_udp_send_compile) {
8  boost::asio::io_service io;
10  auto socket =
11  jb::itch5::make_socket_udp_send(io, cfg.address("127.0.0.1").port(40000));
12  BOOST_CHECK(socket.is_open());
13 }
14 
16 
17 /// @test Create a simple unicast socket on the default interface
18 BOOST_AUTO_TEST_CASE(itch5_make_socket_udp_send_basic) {
19  using namespace ::testing;
20  NiceMock<mock_udp_socket> socket;
21  EXPECT_CALL(socket, open(_)).Times(1);
22  EXPECT_CALL(socket, bind(_)).Times(1);
23  EXPECT_CALL(
24  socket, set_option(An<boost::asio::ip::multicast::join_group const&>()))
25  .Times(0);
26  EXPECT_CALL(
27  socket,
28  set_option(An<boost::asio::ip::multicast::enable_loopback const&>()))
29  .Times(0);
31  socket, jb::itch5::udp_sender_config().address("::1").port(50000));
32 }
33 
34 /// @test Create a IPv4 multicast socket on the default interface.
35 BOOST_AUTO_TEST_CASE(itch5_make_socket_udp_send_multicast_ipv4) {
36  using namespace ::testing;
37  NiceMock<mock_udp_socket> socket;
38  EXPECT_CALL(socket, open(_)).Times(1);
39  EXPECT_CALL(
40  socket,
41  bind(boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4(), 0)))
42  .Times(1);
43  EXPECT_CALL(
44  socket,
45  set_option(An<boost::asio::ip::multicast::enable_loopback const&>()))
46  .Times(1);
47 
49  socket,
50  jb::itch5::udp_sender_config().address("239.128.1.1").port(50000));
51 }
52 
53 /// @test Create a IPv6 multicast socket on the default interface.
54 BOOST_AUTO_TEST_CASE(itch5_make_socket_udp_send_multicast_ipv6) {
55  using namespace ::testing;
56  NiceMock<mock_udp_socket> socket;
57  EXPECT_CALL(socket, open(_)).Times(1);
58  EXPECT_CALL(
59  socket,
60  bind(boost::asio::ip::udp::endpoint(boost::asio::ip::address_v6(), 0)))
61  .Times(1);
62  EXPECT_CALL(
63  socket,
64  set_option(An<boost::asio::ip::multicast::enable_loopback const&>()))
65  .Times(1);
66 
68  socket, jb::itch5::udp_sender_config().address("ff05::").port(50000));
69 }
70 
71 /// @test Create a multicast socket on an specific interface ...
72 BOOST_AUTO_TEST_CASE(itch5_make_socket_udp_send_multicast_options) {
73  using namespace ::testing;
74  NiceMock<mock_udp_socket> v6s;
75  EXPECT_CALL(v6s, open(_)).Times(1);
76  EXPECT_CALL(
77  v6s,
78  bind(boost::asio::ip::udp::endpoint(boost::asio::ip::address_v6(), 0)))
79  .Times(1);
80  EXPECT_CALL(
81  v6s, set_option(An<boost::asio::ip::multicast::enable_loopback const&>()))
82  .Times(1);
83  EXPECT_CALL(
84  v6s, set_option(Matcher<boost::asio::ip::multicast::hops const&>(
85  Truly([](boost::asio::ip::multicast::hops const& h) {
86  return h.value() == 10;
87  }))))
88  .Times(1);
89 
92  .address("ff05::")
93  .port(50000)
94  .enable_loopback(true)
95  .hops(10)
96  .outbound_interface("2"));
97 
98  NiceMock<mock_udp_socket> v4s;
99  EXPECT_CALL(v4s, open(_)).Times(1);
100  EXPECT_CALL(
101  v4s,
102  bind(boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4(), 0)))
103  .Times(1);
104  EXPECT_CALL(
105  v4s, set_option(boost::asio::ip::multicast::enable_loopback(true)))
106  .Times(1);
107  EXPECT_CALL(v4s, set_option(An<boost::asio::ip::multicast::hops const&>()))
108  .Times(0);
111  .address("239.128.1.1")
112  .port(50000)
113  .enable_loopback(true)
114  .hops(-1)
115  .outbound_interface("127.0.0.1"));
116 }
117 
118 /// @test Create a multicast socket on an specific interface.
119 BOOST_AUTO_TEST_CASE(itch5_make_socket_udp_send_multicast_options_errors) {
120  boost::asio::io_service io;
121  BOOST_CHECK_THROW(
124  .address("ff05::")
125  .port(50000)
126  .enable_loopback(true)
127  .hops(10)
128  .outbound_interface("abcd")),
129  std::runtime_error);
130 }
131 
132 /// @test Create a multicast socket on an specific interface.
133 BOOST_AUTO_TEST_CASE(itch5_make_socket_udp_send_unicast_options) {
134  using namespace ::testing;
135  NiceMock<mock_udp_socket> socket;
136  EXPECT_CALL(socket, open(_)).Times(1);
137  EXPECT_CALL(
138  socket,
139  bind(boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4(), 0)))
140  .Times(1);
141  EXPECT_CALL(
142  socket, set_option(Matcher<boost::asio::ip::unicast::hops const&>(
143  Truly([](boost::asio::ip::unicast::hops const& h) {
144  return h.value() == 10;
145  }))))
146  .Times(1);
147  EXPECT_CALL(socket, set_option(boost::asio::socket_base::broadcast(true)))
148  .Times(1);
149 
152  .address("192.168.1.7")
153  .port(50000)
154  .broadcast(true)
155  .hops(10));
156 }
BOOST_AUTO_TEST_CASE(itch5_make_socket_udp_send_compile)
A Mock Object for the socket class.
socket_t make_socket_udp_send(boost::asio::io_service &io, udp_sender_config const &cfg)
Create a socket to send UDP messages given the configuration parameters.
jb::config_attribute< udp_sender_config, std::string > address
A configuration object for UDP senders.
void setup_socket_udp_send(socket_t &s, udp_sender_config const &cfg)