• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // generic/raw_protocol.cpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 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/raw_protocol.hpp>
18 
19 #include <cstring>
20 #include <boost/asio/io_context.hpp>
21 #include <boost/asio/ip/icmp.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_raw_protocol_socket_compile test
31 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 // The following test checks that all public member functions on the class
33 // generic::raw_socket::socket compile and link correctly. Runtime failures
34 // are ignored.
35 
36 namespace generic_raw_protocol_socket_compile {
37 
connect_handler(const boost::system::error_code &)38 void connect_handler(const boost::system::error_code&)
39 {
40 }
41 
send_handler(const boost::system::error_code &,std::size_t)42 void send_handler(const boost::system::error_code&, std::size_t)
43 {
44 }
45 
receive_handler(const boost::system::error_code &,std::size_t)46 void receive_handler(const boost::system::error_code&, std::size_t)
47 {
48 }
49 
test()50 void test()
51 {
52   using namespace boost::asio;
53   namespace generic = boost::asio::generic;
54   typedef generic::raw_protocol rp;
55 
56   const int af_inet = BOOST_ASIO_OS_DEF(AF_INET);
57   const int ipproto_icmp = BOOST_ASIO_OS_DEF(IPPROTO_ICMP);
58   const int sock_raw = BOOST_ASIO_OS_DEF(SOCK_RAW);
59 
60   try
61   {
62     io_context ioc;
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_raw_socket constructors.
71 
72     rp::socket socket1(ioc);
73     rp::socket socket2(ioc, rp(af_inet, ipproto_icmp));
74     rp::socket socket3(ioc, rp::endpoint());
75 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
76     rp::socket::native_handle_type native_socket1
77       = ::socket(af_inet, sock_raw, 0);
78     rp::socket socket4(ioc, rp(af_inet, ipproto_icmp), native_socket1);
79 #endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
80 
81 #if defined(BOOST_ASIO_HAS_MOVE)
82     rp::socket socket5(std::move(socket4));
83     boost::asio::ip::icmp::socket icmp_socket(ioc);
84     rp::socket socket6(std::move(icmp_socket));
85 #endif // defined(BOOST_ASIO_HAS_MOVE)
86 
87     // basic_datagram_socket operators.
88 
89 #if defined(BOOST_ASIO_HAS_MOVE)
90     socket1 = rp::socket(ioc);
91     socket1 = std::move(socket2);
92     socket1 = boost::asio::ip::icmp::socket(ioc);
93 #endif // defined(BOOST_ASIO_HAS_MOVE)
94 
95     // basic_io_object functions.
96 
97     rp::socket::executor_type ex = socket1.get_executor();
98     (void)ex;
99 
100     // basic_socket functions.
101 
102     rp::socket::lowest_layer_type& lowest_layer = socket1.lowest_layer();
103     (void)lowest_layer;
104 
105     socket1.open(rp(af_inet, ipproto_icmp));
106     socket1.open(rp(af_inet, ipproto_icmp), ec);
107 
108 #if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
109     rp::socket::native_handle_type native_socket2
110       = ::socket(af_inet, sock_raw, 0);
111     socket1.assign(rp(af_inet, ipproto_icmp), native_socket2);
112     rp::socket::native_handle_type native_socket3
113       = ::socket(af_inet, sock_raw, 0);
114     socket1.assign(rp(af_inet, ipproto_icmp), 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     rp::socket::native_handle_type native_socket4 = socket1.native_handle();
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(rp::endpoint());
140     socket1.bind(rp::endpoint(), ec);
141 
142     socket1.connect(rp::endpoint());
143     socket1.connect(rp::endpoint(), ec);
144 
145     socket1.async_connect(rp::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     rp::endpoint endpoint1 = socket1.local_endpoint();
157     (void)endpoint1;
158     rp::endpoint endpoint2 = socket1.local_endpoint(ec);
159     (void)endpoint2;
160 
161     rp::endpoint endpoint3 = socket1.remote_endpoint();
162     (void)endpoint3;
163     rp::endpoint endpoint4 = socket1.remote_endpoint(ec);
164     (void)endpoint4;
165 
166     socket1.shutdown(socket_base::shutdown_both);
167     socket1.shutdown(socket_base::shutdown_both, ec);
168 
169     // basic_raw_socket functions.
170 
171     socket1.send(buffer(mutable_char_buffer));
172     socket1.send(buffer(const_char_buffer));
173     socket1.send(null_buffers());
174     socket1.send(buffer(mutable_char_buffer), in_flags);
175     socket1.send(buffer(const_char_buffer), in_flags);
176     socket1.send(null_buffers(), in_flags);
177     socket1.send(buffer(mutable_char_buffer), in_flags, ec);
178     socket1.send(buffer(const_char_buffer), in_flags, ec);
179     socket1.send(null_buffers(), in_flags, ec);
180 
181     socket1.async_send(buffer(mutable_char_buffer), send_handler);
182     socket1.async_send(buffer(const_char_buffer), send_handler);
183     socket1.async_send(null_buffers(), send_handler);
184     socket1.async_send(buffer(mutable_char_buffer), in_flags, send_handler);
185     socket1.async_send(buffer(const_char_buffer), in_flags, send_handler);
186     socket1.async_send(null_buffers(), in_flags, send_handler);
187 
188     socket1.send_to(buffer(mutable_char_buffer),
189         rp::endpoint());
190     socket1.send_to(buffer(const_char_buffer),
191         rp::endpoint());
192     socket1.send_to(null_buffers(),
193         rp::endpoint());
194     socket1.send_to(buffer(mutable_char_buffer),
195         rp::endpoint(), in_flags);
196     socket1.send_to(buffer(const_char_buffer),
197         rp::endpoint(), in_flags);
198     socket1.send_to(null_buffers(),
199         rp::endpoint(), in_flags);
200     socket1.send_to(buffer(mutable_char_buffer),
201         rp::endpoint(), in_flags, ec);
202     socket1.send_to(buffer(const_char_buffer),
203         rp::endpoint(), in_flags, ec);
204     socket1.send_to(null_buffers(),
205         rp::endpoint(), in_flags, ec);
206 
207     socket1.async_send_to(buffer(mutable_char_buffer),
208         rp::endpoint(), send_handler);
209     socket1.async_send_to(buffer(const_char_buffer),
210         rp::endpoint(), send_handler);
211     socket1.async_send_to(null_buffers(),
212         rp::endpoint(), send_handler);
213     socket1.async_send_to(buffer(mutable_char_buffer),
214         rp::endpoint(), in_flags, send_handler);
215     socket1.async_send_to(buffer(const_char_buffer),
216         rp::endpoint(), in_flags, send_handler);
217     socket1.async_send_to(null_buffers(),
218         rp::endpoint(), in_flags, send_handler);
219 
220     socket1.receive(buffer(mutable_char_buffer));
221     socket1.receive(null_buffers());
222     socket1.receive(buffer(mutable_char_buffer), in_flags);
223     socket1.receive(null_buffers(), in_flags);
224     socket1.receive(buffer(mutable_char_buffer), in_flags, ec);
225     socket1.receive(null_buffers(), in_flags, ec);
226 
227     socket1.async_receive(buffer(mutable_char_buffer), receive_handler);
228     socket1.async_receive(null_buffers(), receive_handler);
229     socket1.async_receive(buffer(mutable_char_buffer), in_flags,
230         receive_handler);
231     socket1.async_receive(null_buffers(), in_flags, receive_handler);
232 
233     rp::endpoint endpoint;
234     socket1.receive_from(buffer(mutable_char_buffer), endpoint);
235     socket1.receive_from(null_buffers(), endpoint);
236     socket1.receive_from(buffer(mutable_char_buffer), endpoint, in_flags);
237     socket1.receive_from(null_buffers(), endpoint, in_flags);
238     socket1.receive_from(buffer(mutable_char_buffer), endpoint, in_flags, ec);
239     socket1.receive_from(null_buffers(), endpoint, in_flags, ec);
240 
241     socket1.async_receive_from(buffer(mutable_char_buffer),
242         endpoint, receive_handler);
243     socket1.async_receive_from(null_buffers(),
244         endpoint, receive_handler);
245     socket1.async_receive_from(buffer(mutable_char_buffer),
246         endpoint, in_flags, receive_handler);
247     socket1.async_receive_from(null_buffers(),
248         endpoint, in_flags, receive_handler);
249   }
250   catch (std::exception&)
251   {
252   }
253 }
254 
255 } // namespace generic_raw_protocol_socket_compile
256 
257 //------------------------------------------------------------------------------
258 
259 BOOST_ASIO_TEST_SUITE
260 (
261   "generic/raw_protocol",
262   BOOST_ASIO_TEST_CASE(generic_raw_protocol_socket_compile::test)
263 )
264