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