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