1//
2// datagram_protocol.cpp
3// ~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 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/local/datagram_protocol.hpp>
18
19#include <cstring>
20#include <boost/asio/io_context.hpp>
21#include "../unit_test.hpp"
22
23//------------------------------------------------------------------------------
24
25// local_datagram_protocol_socket_compile test
26// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27// The following test checks that all public member functions on the class
28// local::datagram_socket::socket compile and link correctly. Runtime failures
29// are ignored.
30
31namespace local_datagram_protocol_socket_compile {
32
33void connect_handler(const boost::system::error_code&)
34{
35}
36
37void send_handler(const boost::system::error_code&, std::size_t)
38{
39}
40
41void receive_handler(const boost::system::error_code&, std::size_t)
42{
43}
44
45void test()
46{
47#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
48 using namespace boost::asio;
49 namespace local = boost::asio::local;
50 typedef local::datagram_protocol dp;
51
52 try
53 {
54 io_context ioc;
55 const io_context::executor_type ioc_ex = ioc.get_executor();
56 char mutable_char_buffer[128] = "";
57 const char const_char_buffer[128] = "";
58 socket_base::message_flags in_flags = 0;
59 socket_base::send_buffer_size socket_option;
60 socket_base::bytes_readable io_control_command;
61 boost::system::error_code ec;
62
63 // basic_datagram_socket constructors.
64
65 dp::socket socket1(ioc);
66 dp::socket socket2(ioc, dp());
67 dp::socket socket3(ioc, dp::endpoint(""));
68 int native_socket1 = ::socket(AF_UNIX, SOCK_DGRAM, protocol: 0);
69 dp::socket socket4(ioc, dp(), native_socket1);
70
71 dp::socket socket5(ioc_ex);
72 dp::socket socket6(ioc_ex, dp());
73 dp::socket socket7(ioc_ex, dp::endpoint(""));
74 int native_socket2 = ::socket(AF_UNIX, SOCK_DGRAM, protocol: 0);
75 dp::socket socket8(ioc_ex, dp(), native_socket2);
76
77 // basic_io_object functions.
78
79 dp::socket::executor_type ex = socket1.get_executor();
80 (void)ex;
81
82 // basic_socket functions.
83
84 dp::socket::lowest_layer_type& lowest_layer = socket1.lowest_layer();
85 (void)lowest_layer;
86
87 socket1.open(protocol: dp());
88 socket1.open(protocol: dp(), ec);
89
90 int native_socket3 = ::socket(AF_UNIX, SOCK_DGRAM, protocol: 0);
91 socket1.assign(protocol: dp(), native_socket: native_socket3);
92 int native_socket4 = ::socket(AF_UNIX, SOCK_DGRAM, protocol: 0);
93 socket1.assign(protocol: dp(), native_socket: native_socket4, ec);
94
95 bool is_open = socket1.is_open();
96 (void)is_open;
97
98 socket1.close();
99 socket1.close(ec);
100
101 dp::socket::native_handle_type native_socket5 = socket1.native_handle();
102 (void)native_socket5;
103
104 socket1.cancel();
105 socket1.cancel(ec);
106
107 bool at_mark1 = socket1.at_mark();
108 (void)at_mark1;
109 bool at_mark2 = socket1.at_mark(ec);
110 (void)at_mark2;
111
112 std::size_t available1 = socket1.available();
113 (void)available1;
114 std::size_t available2 = socket1.available(ec);
115 (void)available2;
116
117 socket1.bind(endpoint: dp::endpoint(""));
118 socket1.bind(endpoint: dp::endpoint(""), ec);
119
120 socket1.connect(peer_endpoint: dp::endpoint(""));
121 socket1.connect(peer_endpoint: dp::endpoint(""), ec);
122
123 socket1.async_connect(peer_endpoint: dp::endpoint(""), token&: connect_handler);
124
125 socket1.set_option(socket_option);
126 socket1.set_option(option: socket_option, ec);
127
128 socket1.get_option(option&: socket_option);
129 socket1.get_option(option&: socket_option, ec);
130
131 socket1.io_control(command&: io_control_command);
132 socket1.io_control(command&: io_control_command, ec);
133
134 dp::endpoint endpoint1 = socket1.local_endpoint();
135 (void)endpoint1;
136 dp::endpoint endpoint2 = socket1.local_endpoint(ec);
137 (void)endpoint2;
138
139 dp::endpoint endpoint3 = socket1.remote_endpoint();
140 (void)endpoint3;
141 dp::endpoint endpoint4 = socket1.remote_endpoint(ec);
142 (void)endpoint4;
143
144 socket1.shutdown(what: socket_base::shutdown_both);
145 socket1.shutdown(what: socket_base::shutdown_both, ec);
146
147 // basic_datagram_socket functions.
148
149 socket1.send(buffers: buffer(data&: mutable_char_buffer));
150 socket1.send(buffers: buffer(data: const_char_buffer));
151 socket1.send(buffers: null_buffers());
152 socket1.send(buffers: buffer(data&: mutable_char_buffer), flags: in_flags);
153 socket1.send(buffers: buffer(data: const_char_buffer), flags: in_flags);
154 socket1.send(buffers: null_buffers(), flags: in_flags);
155 socket1.send(buffers: buffer(data&: mutable_char_buffer), flags: in_flags, ec);
156 socket1.send(buffers: buffer(data: const_char_buffer), flags: in_flags, ec);
157 socket1.send(buffers: null_buffers(), flags: in_flags, ec);
158
159 socket1.async_send(buffers: buffer(data&: mutable_char_buffer), token&: send_handler);
160 socket1.async_send(buffers: buffer(data: const_char_buffer), token&: send_handler);
161 socket1.async_send(buffers: null_buffers(), token&: send_handler);
162 socket1.async_send(buffers: buffer(data&: mutable_char_buffer), flags: in_flags, token&: send_handler);
163 socket1.async_send(buffers: buffer(data: const_char_buffer), flags: in_flags, token&: send_handler);
164 socket1.async_send(buffers: null_buffers(), flags: in_flags, token&: send_handler);
165
166 socket1.send_to(buffers: buffer(data&: mutable_char_buffer),
167 destination: dp::endpoint(""));
168 socket1.send_to(buffers: buffer(data: const_char_buffer),
169 destination: dp::endpoint(""));
170 socket1.send_to(buffers: null_buffers(),
171 destination: dp::endpoint(""));
172 socket1.send_to(buffers: buffer(data&: mutable_char_buffer),
173 destination: dp::endpoint(""), flags: in_flags);
174 socket1.send_to(buffers: buffer(data: const_char_buffer),
175 destination: dp::endpoint(""), flags: in_flags);
176 socket1.send_to(buffers: null_buffers(),
177 destination: dp::endpoint(""), flags: in_flags);
178 socket1.send_to(buffers: buffer(data&: mutable_char_buffer),
179 destination: dp::endpoint(""), flags: in_flags, ec);
180 socket1.send_to(buffers: buffer(data: const_char_buffer),
181 destination: dp::endpoint(""), flags: in_flags, ec);
182 socket1.send_to(buffers: null_buffers(),
183 destination: dp::endpoint(""), flags: in_flags, ec);
184
185 socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
186 destination: dp::endpoint(""), token&: send_handler);
187 socket1.async_send_to(buffers: buffer(data: const_char_buffer),
188 destination: dp::endpoint(""), token&: send_handler);
189 socket1.async_send_to(buffers: null_buffers(),
190 destination: dp::endpoint(""), token&: send_handler);
191 socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
192 destination: dp::endpoint(""), flags: in_flags, token&: send_handler);
193 socket1.async_send_to(buffers: buffer(data: const_char_buffer),
194 destination: dp::endpoint(""), flags: in_flags, token&: send_handler);
195 socket1.async_send_to(buffers: null_buffers(),
196 destination: dp::endpoint(""), flags: in_flags, token&: send_handler);
197
198 socket1.receive(buffers: buffer(data&: mutable_char_buffer));
199 socket1.receive(buffers: null_buffers());
200 socket1.receive(buffers: buffer(data&: mutable_char_buffer), flags: in_flags);
201 socket1.receive(buffers: null_buffers(), flags: in_flags);
202 socket1.receive(buffers: buffer(data&: mutable_char_buffer), flags: in_flags, ec);
203 socket1.receive(buffers: null_buffers(), flags: in_flags, ec);
204
205 socket1.async_receive(buffers: buffer(data&: mutable_char_buffer), token&: receive_handler);
206 socket1.async_receive(buffers: null_buffers(), token&: receive_handler);
207 socket1.async_receive(buffers: buffer(data&: mutable_char_buffer), flags: in_flags,
208 token&: receive_handler);
209 socket1.async_receive(buffers: null_buffers(), flags: in_flags, token&: receive_handler);
210
211 dp::endpoint endpoint;
212 socket1.receive_from(buffers: buffer(data&: mutable_char_buffer), sender_endpoint&: endpoint);
213 socket1.receive_from(buffers: null_buffers(), sender_endpoint&: endpoint);
214 socket1.receive_from(buffers: buffer(data&: mutable_char_buffer), sender_endpoint&: endpoint, flags: in_flags);
215 socket1.receive_from(buffers: null_buffers(), sender_endpoint&: endpoint, flags: in_flags);
216 socket1.receive_from(buffers: buffer(data&: mutable_char_buffer), sender_endpoint&: endpoint, flags: in_flags, ec);
217 socket1.receive_from(buffers: null_buffers(), sender_endpoint&: endpoint, flags: in_flags, ec);
218
219 socket1.async_receive_from(buffers: buffer(data&: mutable_char_buffer),
220 sender_endpoint&: endpoint, token&: receive_handler);
221 socket1.async_receive_from(buffers: null_buffers(),
222 sender_endpoint&: endpoint, token&: receive_handler);
223 socket1.async_receive_from(buffers: buffer(data&: mutable_char_buffer),
224 sender_endpoint&: endpoint, flags: in_flags, token&: receive_handler);
225 socket1.async_receive_from(buffers: null_buffers(),
226 sender_endpoint&: endpoint, flags: in_flags, token&: receive_handler);
227 }
228 catch (std::exception&)
229 {
230 }
231#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
232}
233
234} // namespace local_datagram_protocol_socket_compile
235
236//------------------------------------------------------------------------------
237
238BOOST_ASIO_TEST_SUITE
239(
240 "local/datagram_protocol",
241 BOOST_ASIO_COMPILE_TEST_CASE(local_datagram_protocol_socket_compile::test)
242)
243

source code of boost/libs/asio/test/local/datagram_protocol.cpp