1//
2// generic/datagram_protocol.cpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6//
7// Distributed under the Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9//
10
11// Disable autolinking for unit tests.
12#if !defined(BOOST_ALL_NO_LIB)
13#define BOOST_ALL_NO_LIB 1
14#endif // !defined(BOOST_ALL_NO_LIB)
15
16// Test that header file is self-contained.
17#include <boost/asio/generic/datagram_protocol.hpp>
18
19#include <cstring>
20#include <boost/asio/io_service.hpp>
21#include <boost/asio/ip/udp.hpp>
22#include "../unit_test.hpp"
23
24#if defined(__cplusplus_cli) || defined(__cplusplus_winrt)
25# define generic cpp_generic
26#endif
27
28//------------------------------------------------------------------------------
29
30// generic_datagram_protocol_socket_compile test
31// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32// The following test checks that all public member functions on the class
33// generic::datagram_socket::socket compile and link correctly. Runtime
34// failures are ignored.
35
36namespace generic_datagram_protocol_socket_compile {
37
38void connect_handler(const boost::system::error_code&)
39{
40}
41
42void send_handler(const boost::system::error_code&, std::size_t)
43{
44}
45
46void receive_handler(const boost::system::error_code&, std::size_t)
47{
48}
49
50void test()
51{
52 using namespace boost::asio;
53 namespace generic = boost::asio::generic;
54 typedef generic::datagram_protocol dp;
55
56 const int af_inet = BOOST_ASIO_OS_DEF(AF_INET);
57 const int ipproto_udp = BOOST_ASIO_OS_DEF(IPPROTO_UDP);
58 const int sock_dgram = BOOST_ASIO_OS_DEF(SOCK_DGRAM);
59
60 try
61 {
62 io_service ios;
63 char mutable_char_buffer[128] = "";
64 const char const_char_buffer[128] = "";
65 socket_base::message_flags in_flags = 0;
66 socket_base::send_buffer_size socket_option;
67 socket_base::bytes_readable io_control_command;
68 boost::system::error_code ec;
69
70 // basic_datagram_socket constructors.
71
72 dp::socket socket1(ios);
73 dp::socket socket2(ios, dp(af_inet, ipproto_udp));
74 dp::socket socket3(ios, dp::endpoint());
75#if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
76 dp::socket::native_handle_type native_socket1
77 = ::socket(af_inet, sock_dgram, 0);
78 dp::socket socket4(ios, dp(af_inet, ipproto_udp), native_socket1);
79#endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
80
81#if defined(BOOST_ASIO_HAS_MOVE)
82 dp::socket socket5(std::move(socket4));
83 boost::asio::ip::udp::socket udp_socket(ios);
84 dp::socket socket6(std::move(udp_socket));
85#endif // defined(BOOST_ASIO_HAS_MOVE)
86
87 // basic_datagram_socket operators.
88
89#if defined(BOOST_ASIO_HAS_MOVE)
90 socket1 = dp::socket(ios);
91 socket1 = std::move(socket2);
92 socket1 = boost::asio::ip::udp::socket(ios);
93#endif // defined(BOOST_ASIO_HAS_MOVE)
94
95 // basic_io_object functions.
96
97 io_service& ios_ref = socket1.get_io_service();
98 (void)ios_ref;
99
100 // basic_socket functions.
101
102 dp::socket::lowest_layer_type& lowest_layer = socket1.lowest_layer();
103 (void)lowest_layer;
104
105 socket1.open(dp(af_inet, ipproto_udp));
106 socket1.open(dp(af_inet, ipproto_udp), ec);
107
108#if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
109 dp::socket::native_handle_type native_socket2
110 = ::socket(af_inet, sock_dgram, 0);
111 socket1.assign(dp(af_inet, ipproto_udp), native_socket2);
112 dp::socket::native_handle_type native_socket3
113 = ::socket(af_inet, sock_dgram, 0);
114 socket1.assign(dp(af_inet, ipproto_udp), native_socket3, ec);
115#endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
116
117 bool is_open = socket1.is_open();
118 (void)is_open;
119
120 socket1.close();
121 socket1.close(ec);
122
123 dp::socket::native_type native_socket4 = socket1.native();
124 (void)native_socket4;
125
126 socket1.cancel();
127 socket1.cancel(ec);
128
129 bool at_mark1 = socket1.at_mark();
130 (void)at_mark1;
131 bool at_mark2 = socket1.at_mark(ec);
132 (void)at_mark2;
133
134 std::size_t available1 = socket1.available();
135 (void)available1;
136 std::size_t available2 = socket1.available(ec);
137 (void)available2;
138
139 socket1.bind(dp::endpoint());
140 socket1.bind(dp::endpoint(), ec);
141
142 socket1.connect(dp::endpoint());
143 socket1.connect(dp::endpoint(), ec);
144
145 socket1.async_connect(dp::endpoint(), connect_handler);
146
147 socket1.set_option(socket_option);
148 socket1.set_option(socket_option, ec);
149
150 socket1.get_option(socket_option);
151 socket1.get_option(socket_option, ec);
152
153 socket1.io_control(io_control_command);
154 socket1.io_control(io_control_command, ec);
155
156 dp::endpoint endpoint1 = socket1.local_endpoint();
157 dp::endpoint endpoint2 = socket1.local_endpoint(ec);
158
159 dp::endpoint endpoint3 = socket1.remote_endpoint();
160 dp::endpoint endpoint4 = socket1.remote_endpoint(ec);
161
162 socket1.shutdown(socket_base::shutdown_both);
163 socket1.shutdown(socket_base::shutdown_both, ec);
164
165 // basic_datagram_socket functions.
166
167 socket1.send(buffer(mutable_char_buffer));
168 socket1.send(buffer(const_char_buffer));
169 socket1.send(null_buffers());
170 socket1.send(buffer(mutable_char_buffer), in_flags);
171 socket1.send(buffer(const_char_buffer), in_flags);
172 socket1.send(null_buffers(), in_flags);
173 socket1.send(buffer(mutable_char_buffer), in_flags, ec);
174 socket1.send(buffer(const_char_buffer), in_flags, ec);
175 socket1.send(null_buffers(), in_flags, ec);
176
177 socket1.async_send(buffer(mutable_char_buffer), send_handler);
178 socket1.async_send(buffer(const_char_buffer), send_handler);
179 socket1.async_send(null_buffers(), send_handler);
180 socket1.async_send(buffer(mutable_char_buffer), in_flags, send_handler);
181 socket1.async_send(buffer(const_char_buffer), in_flags, send_handler);
182 socket1.async_send(null_buffers(), in_flags, send_handler);
183
184 socket1.send_to(buffer(mutable_char_buffer),
185 dp::endpoint());
186 socket1.send_to(buffer(const_char_buffer),
187 dp::endpoint());
188 socket1.send_to(null_buffers(),
189 dp::endpoint());
190 socket1.send_to(buffer(mutable_char_buffer),
191 dp::endpoint(), in_flags);
192 socket1.send_to(buffer(const_char_buffer),
193 dp::endpoint(), in_flags);
194 socket1.send_to(null_buffers(),
195 dp::endpoint(), in_flags);
196 socket1.send_to(buffer(mutable_char_buffer),
197 dp::endpoint(), in_flags, ec);
198 socket1.send_to(buffer(const_char_buffer),
199 dp::endpoint(), in_flags, ec);
200 socket1.send_to(null_buffers(),
201 dp::endpoint(), in_flags, ec);
202
203 socket1.async_send_to(buffer(mutable_char_buffer),
204 dp::endpoint(), send_handler);
205 socket1.async_send_to(buffer(const_char_buffer),
206 dp::endpoint(), send_handler);
207 socket1.async_send_to(null_buffers(),
208 dp::endpoint(), send_handler);
209 socket1.async_send_to(buffer(mutable_char_buffer),
210 dp::endpoint(), in_flags, send_handler);
211 socket1.async_send_to(buffer(const_char_buffer),
212 dp::endpoint(), in_flags, send_handler);
213 socket1.async_send_to(null_buffers(),
214 dp::endpoint(), in_flags, send_handler);
215
216 socket1.receive(buffer(mutable_char_buffer));
217 socket1.receive(null_buffers());
218 socket1.receive(buffer(mutable_char_buffer), in_flags);
219 socket1.receive(null_buffers(), in_flags);
220 socket1.receive(buffer(mutable_char_buffer), in_flags, ec);
221 socket1.receive(null_buffers(), in_flags, ec);
222
223 socket1.async_receive(buffer(mutable_char_buffer), receive_handler);
224 socket1.async_receive(null_buffers(), receive_handler);
225 socket1.async_receive(buffer(mutable_char_buffer), in_flags,
226 receive_handler);
227 socket1.async_receive(null_buffers(), in_flags, receive_handler);
228
229 dp::endpoint endpoint;
230 socket1.receive_from(buffer(mutable_char_buffer), endpoint);
231 socket1.receive_from(null_buffers(), endpoint);
232 socket1.receive_from(buffer(mutable_char_buffer), endpoint, in_flags);
233 socket1.receive_from(null_buffers(), endpoint, in_flags);
234 socket1.receive_from(buffer(mutable_char_buffer), endpoint, in_flags, ec);
235 socket1.receive_from(null_buffers(), endpoint, in_flags, ec);
236
237 socket1.async_receive_from(buffer(mutable_char_buffer),
238 endpoint, receive_handler);
239 socket1.async_receive_from(null_buffers(),
240 endpoint, receive_handler);
241 socket1.async_receive_from(buffer(mutable_char_buffer),
242 endpoint, in_flags, receive_handler);
243 socket1.async_receive_from(null_buffers(),
244 endpoint, in_flags, receive_handler);
245 }
246 catch (std::exception&)
247 {
248 }
249}
250
251} // namespace generic_datagram_protocol_socket_compile
252
253//------------------------------------------------------------------------------
254
255BOOST_ASIO_TEST_SUITE
256(
257 "generic/datagram_protocol",
258 BOOST_ASIO_TEST_CASE(generic_datagram_protocol_socket_compile::test)
259)
260