1//
2// icmp.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/ip/icmp.hpp>
18
19#include <cstring>
20#include <boost/asio/io_service.hpp>
21#include <boost/asio/placeholders.hpp>
22#include "../unit_test.hpp"
23#include "../archetypes/gettable_socket_option.hpp"
24#include "../archetypes/async_result.hpp"
25#include "../archetypes/io_control_command.hpp"
26#include "../archetypes/settable_socket_option.hpp"
27
28//------------------------------------------------------------------------------
29
30// ip_icmp_socket_compile test
31// ~~~~~~~~~~~~~~~~~~~~~~~~~~
32// The following test checks that all public member functions on the class
33// ip::icmp::socket compile and link correctly. Runtime failures are ignored.
34
35namespace ip_icmp_socket_compile {
36
37void connect_handler(const boost::system::error_code&)
38{
39}
40
41void send_handler(const boost::system::error_code&, std::size_t)
42{
43}
44
45void receive_handler(const boost::system::error_code&, std::size_t)
46{
47}
48
49void test()
50{
51 using namespace boost::asio;
52 namespace ip = boost::asio::ip;
53
54 try
55 {
56 io_service ios;
57 char mutable_char_buffer[128] = "";
58 const char const_char_buffer[128] = "";
59 socket_base::message_flags in_flags = 0;
60 archetypes::settable_socket_option<void> settable_socket_option1;
61 archetypes::settable_socket_option<int> settable_socket_option2;
62 archetypes::settable_socket_option<double> settable_socket_option3;
63 archetypes::gettable_socket_option<void> gettable_socket_option1;
64 archetypes::gettable_socket_option<int> gettable_socket_option2;
65 archetypes::gettable_socket_option<double> gettable_socket_option3;
66 archetypes::io_control_command io_control_command;
67 archetypes::lazy_handler lazy;
68 boost::system::error_code ec;
69
70 // basic_datagram_socket constructors.
71
72 ip::icmp::socket socket1(ios);
73 ip::icmp::socket socket2(ios, ip::icmp::v4());
74 ip::icmp::socket socket3(ios, ip::icmp::v6());
75 ip::icmp::socket socket4(ios, ip::icmp::endpoint(ip::icmp::v4(), 0));
76 ip::icmp::socket socket5(ios, ip::icmp::endpoint(ip::icmp::v6(), 0));
77#if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
78 ip::icmp::socket::native_handle_type native_socket1
79 = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
80 ip::icmp::socket socket6(ios, ip::icmp::v4(), native_socket1);
81#endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
82
83#if defined(BOOST_ASIO_HAS_MOVE)
84 ip::icmp::socket socket7(std::move(socket6));
85#endif // defined(BOOST_ASIO_HAS_MOVE)
86
87 // basic_datagram_socket operators.
88
89#if defined(BOOST_ASIO_HAS_MOVE)
90 socket1 = ip::icmp::socket(ios);
91 socket1 = std::move(socket2);
92#endif // defined(BOOST_ASIO_HAS_MOVE)
93
94 // basic_io_object functions.
95
96 io_service& ios_ref = socket1.get_io_service();
97 (void)ios_ref;
98
99 // basic_socket functions.
100
101 ip::icmp::socket::lowest_layer_type& lowest_layer = socket1.lowest_layer();
102 (void)lowest_layer;
103
104 const ip::icmp::socket& socket8 = socket1;
105 const ip::icmp::socket::lowest_layer_type& lowest_layer2
106 = socket8.lowest_layer();
107 (void)lowest_layer2;
108
109 socket1.open(protocol: ip::icmp::v4());
110 socket1.open(protocol: ip::icmp::v6());
111 socket1.open(protocol: ip::icmp::v4(), ec);
112 socket1.open(protocol: ip::icmp::v6(), ec);
113
114#if !defined(BOOST_ASIO_WINDOWS_RUNTIME)
115 ip::icmp::socket::native_handle_type native_socket2
116 = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
117 socket1.assign(protocol: ip::icmp::v4(), native_socket: native_socket2);
118 ip::icmp::socket::native_handle_type native_socket3
119 = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
120 socket1.assign(protocol: ip::icmp::v4(), native_socket: native_socket3, ec);
121#endif // !defined(BOOST_ASIO_WINDOWS_RUNTIME)
122
123 bool is_open = socket1.is_open();
124 (void)is_open;
125
126 socket1.close();
127 socket1.close(ec);
128
129 ip::icmp::socket::native_type native_socket4 = socket1.native();
130 (void)native_socket4;
131
132 ip::icmp::socket::native_handle_type native_socket5
133 = socket1.native_handle();
134 (void)native_socket5;
135
136 socket1.cancel();
137 socket1.cancel(ec);
138
139 bool at_mark1 = socket1.at_mark();
140 (void)at_mark1;
141 bool at_mark2 = socket1.at_mark(ec);
142 (void)at_mark2;
143
144 std::size_t available1 = socket1.available();
145 (void)available1;
146 std::size_t available2 = socket1.available(ec);
147 (void)available2;
148
149 socket1.bind(endpoint: ip::icmp::endpoint(ip::icmp::v4(), 0));
150 socket1.bind(endpoint: ip::icmp::endpoint(ip::icmp::v6(), 0));
151 socket1.bind(endpoint: ip::icmp::endpoint(ip::icmp::v4(), 0), ec);
152 socket1.bind(endpoint: ip::icmp::endpoint(ip::icmp::v6(), 0), ec);
153
154 socket1.connect(peer_endpoint: ip::icmp::endpoint(ip::icmp::v4(), 0));
155 socket1.connect(peer_endpoint: ip::icmp::endpoint(ip::icmp::v6(), 0));
156 socket1.connect(peer_endpoint: ip::icmp::endpoint(ip::icmp::v4(), 0), ec);
157 socket1.connect(peer_endpoint: ip::icmp::endpoint(ip::icmp::v6(), 0), ec);
158
159 socket1.async_connect(peer_endpoint: ip::icmp::endpoint(ip::icmp::v4(), 0),
160 handler: &connect_handler);
161 socket1.async_connect(peer_endpoint: ip::icmp::endpoint(ip::icmp::v6(), 0),
162 handler: &connect_handler);
163 int i1 = socket1.async_connect(peer_endpoint: ip::icmp::endpoint(ip::icmp::v4(), 0), handler&: lazy);
164 (void)i1;
165 int i2 = socket1.async_connect(peer_endpoint: ip::icmp::endpoint(ip::icmp::v6(), 0), handler&: lazy);
166 (void)i2;
167
168 socket1.set_option(settable_socket_option1);
169 socket1.set_option(option: settable_socket_option1, ec);
170 socket1.set_option(settable_socket_option2);
171 socket1.set_option(option: settable_socket_option2, ec);
172 socket1.set_option(settable_socket_option3);
173 socket1.set_option(option: settable_socket_option3, ec);
174
175 socket1.get_option(option&: gettable_socket_option1);
176 socket1.get_option(option&: gettable_socket_option1, ec);
177 socket1.get_option(option&: gettable_socket_option2);
178 socket1.get_option(option&: gettable_socket_option2, ec);
179 socket1.get_option(option&: gettable_socket_option3);
180 socket1.get_option(option&: gettable_socket_option3, ec);
181
182 socket1.io_control(command&: io_control_command);
183 socket1.io_control(command&: io_control_command, ec);
184
185 bool non_blocking1 = socket1.non_blocking();
186 (void)non_blocking1;
187 socket1.non_blocking(mode: true);
188 socket1.non_blocking(mode: false, ec);
189
190 bool non_blocking2 = socket1.native_non_blocking();
191 (void)non_blocking2;
192 socket1.native_non_blocking(mode: true);
193 socket1.native_non_blocking(mode: false, ec);
194
195 ip::icmp::endpoint endpoint1 = socket1.local_endpoint();
196 ip::icmp::endpoint endpoint2 = socket1.local_endpoint(ec);
197
198 ip::icmp::endpoint endpoint3 = socket1.remote_endpoint();
199 ip::icmp::endpoint endpoint4 = socket1.remote_endpoint(ec);
200
201 socket1.shutdown(what: socket_base::shutdown_both);
202 socket1.shutdown(what: socket_base::shutdown_both, ec);
203
204 // basic_datagram_socket functions.
205
206 socket1.send(buffers: buffer(data&: mutable_char_buffer));
207 socket1.send(buffers: buffer(data: const_char_buffer));
208 socket1.send(buffers: null_buffers());
209 socket1.send(buffers: buffer(data&: mutable_char_buffer), flags: in_flags);
210 socket1.send(buffers: buffer(data: const_char_buffer), flags: in_flags);
211 socket1.send(buffers: null_buffers(), flags: in_flags);
212 socket1.send(buffers: buffer(data&: mutable_char_buffer), flags: in_flags, ec);
213 socket1.send(buffers: buffer(data: const_char_buffer), flags: in_flags, ec);
214 socket1.send(buffers: null_buffers(), flags: in_flags, ec);
215
216 socket1.async_send(buffers: buffer(data&: mutable_char_buffer), handler: &send_handler);
217 socket1.async_send(buffers: buffer(data: const_char_buffer), handler: &send_handler);
218 socket1.async_send(buffers: null_buffers(), handler: &send_handler);
219 socket1.async_send(buffers: buffer(data&: mutable_char_buffer), flags: in_flags, handler: &send_handler);
220 socket1.async_send(buffers: buffer(data: const_char_buffer), flags: in_flags, handler: &send_handler);
221 socket1.async_send(buffers: null_buffers(), flags: in_flags, handler: &send_handler);
222 int i3 = socket1.async_send(buffers: buffer(data&: mutable_char_buffer), handler&: lazy);
223 (void)i3;
224 int i4 = socket1.async_send(buffers: buffer(data: const_char_buffer), handler&: lazy);
225 (void)i4;
226 int i5 = socket1.async_send(buffers: null_buffers(), handler&: lazy);
227 (void)i5;
228 int i6 = socket1.async_send(buffers: buffer(data&: mutable_char_buffer), flags: in_flags, handler&: lazy);
229 (void)i6;
230 int i7 = socket1.async_send(buffers: buffer(data: const_char_buffer), flags: in_flags, handler&: lazy);
231 (void)i7;
232 int i8 = socket1.async_send(buffers: null_buffers(), flags: in_flags, handler&: lazy);
233 (void)i8;
234
235 socket1.send_to(buffers: buffer(data&: mutable_char_buffer),
236 destination: ip::icmp::endpoint(ip::icmp::v4(), 0));
237 socket1.send_to(buffers: buffer(data&: mutable_char_buffer),
238 destination: ip::icmp::endpoint(ip::icmp::v6(), 0));
239 socket1.send_to(buffers: buffer(data: const_char_buffer),
240 destination: ip::icmp::endpoint(ip::icmp::v4(), 0));
241 socket1.send_to(buffers: buffer(data: const_char_buffer),
242 destination: ip::icmp::endpoint(ip::icmp::v6(), 0));
243 socket1.send_to(buffers: null_buffers(),
244 destination: ip::icmp::endpoint(ip::icmp::v4(), 0));
245 socket1.send_to(buffers: null_buffers(),
246 destination: ip::icmp::endpoint(ip::icmp::v6(), 0));
247 socket1.send_to(buffers: buffer(data&: mutable_char_buffer),
248 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags);
249 socket1.send_to(buffers: buffer(data&: mutable_char_buffer),
250 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags);
251 socket1.send_to(buffers: buffer(data: const_char_buffer),
252 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags);
253 socket1.send_to(buffers: buffer(data: const_char_buffer),
254 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags);
255 socket1.send_to(buffers: null_buffers(),
256 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags);
257 socket1.send_to(buffers: null_buffers(),
258 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags);
259 socket1.send_to(buffers: buffer(data&: mutable_char_buffer),
260 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags, ec);
261 socket1.send_to(buffers: buffer(data&: mutable_char_buffer),
262 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags, ec);
263 socket1.send_to(buffers: buffer(data: const_char_buffer),
264 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags, ec);
265 socket1.send_to(buffers: buffer(data: const_char_buffer),
266 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags, ec);
267 socket1.send_to(buffers: null_buffers(),
268 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags, ec);
269 socket1.send_to(buffers: null_buffers(),
270 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags, ec);
271
272 socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
273 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), handler: &send_handler);
274 socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
275 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), handler: &send_handler);
276 socket1.async_send_to(buffers: buffer(data: const_char_buffer),
277 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), handler: &send_handler);
278 socket1.async_send_to(buffers: buffer(data: const_char_buffer),
279 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), handler: &send_handler);
280 socket1.async_send_to(buffers: null_buffers(),
281 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), handler: &send_handler);
282 socket1.async_send_to(buffers: null_buffers(),
283 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), handler: &send_handler);
284 socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
285 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags, handler: &send_handler);
286 socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
287 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags, handler: &send_handler);
288 socket1.async_send_to(buffers: buffer(data: const_char_buffer),
289 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags, handler: &send_handler);
290 socket1.async_send_to(buffers: buffer(data: const_char_buffer),
291 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags, handler: &send_handler);
292 socket1.async_send_to(buffers: null_buffers(),
293 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags, handler: &send_handler);
294 socket1.async_send_to(buffers: null_buffers(),
295 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags, handler: &send_handler);
296 int i9 = socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
297 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), handler&: lazy);
298 (void)i9;
299 int i10 = socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
300 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), handler&: lazy);
301 (void)i10;
302 int i11 = socket1.async_send_to(buffers: buffer(data: const_char_buffer),
303 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), handler&: lazy);
304 (void)i11;
305 int i12 = socket1.async_send_to(buffers: buffer(data: const_char_buffer),
306 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), handler&: lazy);
307 (void)i12;
308 int i13 = socket1.async_send_to(buffers: null_buffers(),
309 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), handler&: lazy);
310 (void)i13;
311 int i14 = socket1.async_send_to(buffers: null_buffers(),
312 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), handler&: lazy);
313 (void)i14;
314 int i15 = socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
315 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags, handler&: lazy);
316 (void)i15;
317 int i16 = socket1.async_send_to(buffers: buffer(data&: mutable_char_buffer),
318 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags, handler&: lazy);
319 (void)i16;
320 int i17 = socket1.async_send_to(buffers: buffer(data: const_char_buffer),
321 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags, handler&: lazy);
322 (void)i17;
323 int i18 = socket1.async_send_to(buffers: buffer(data: const_char_buffer),
324 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags, handler&: lazy);
325 (void)i18;
326 int i19 = socket1.async_send_to(buffers: null_buffers(),
327 destination: ip::icmp::endpoint(ip::icmp::v4(), 0), flags: in_flags, handler&: lazy);
328 (void)i19;
329 int i20 = socket1.async_send_to(buffers: null_buffers(),
330 destination: ip::icmp::endpoint(ip::icmp::v6(), 0), flags: in_flags, handler&: lazy);
331 (void)i20;
332
333 socket1.receive(buffers: buffer(data&: mutable_char_buffer));
334 socket1.receive(buffers: null_buffers());
335 socket1.receive(buffers: buffer(data&: mutable_char_buffer), flags: in_flags);
336 socket1.receive(buffers: null_buffers(), flags: in_flags);
337 socket1.receive(buffers: buffer(data&: mutable_char_buffer), flags: in_flags, ec);
338 socket1.receive(buffers: null_buffers(), flags: in_flags, ec);
339
340 socket1.async_receive(buffers: buffer(data&: mutable_char_buffer), handler: &receive_handler);
341 socket1.async_receive(buffers: null_buffers(), handler: &receive_handler);
342 socket1.async_receive(buffers: buffer(data&: mutable_char_buffer), flags: in_flags,
343 handler: &receive_handler);
344 socket1.async_receive(buffers: null_buffers(), flags: in_flags, handler: &receive_handler);
345 int i21 = socket1.async_receive(buffers: buffer(data&: mutable_char_buffer), handler&: lazy);
346 (void)i21;
347 int i22 = socket1.async_receive(buffers: null_buffers(), handler&: lazy);
348 (void)i22;
349 int i23 = socket1.async_receive(buffers: buffer(data&: mutable_char_buffer),
350 flags: in_flags, handler&: lazy);
351 (void)i23;
352 int i24 = socket1.async_receive(buffers: null_buffers(), flags: in_flags, handler&: lazy);
353 (void)i24;
354
355 ip::icmp::endpoint endpoint;
356 socket1.receive_from(buffers: buffer(data&: mutable_char_buffer), sender_endpoint&: endpoint);
357 socket1.receive_from(buffers: null_buffers(), sender_endpoint&: endpoint);
358 socket1.receive_from(buffers: buffer(data&: mutable_char_buffer), sender_endpoint&: endpoint, flags: in_flags);
359 socket1.receive_from(buffers: null_buffers(), sender_endpoint&: endpoint, flags: in_flags);
360 socket1.receive_from(buffers: buffer(data&: mutable_char_buffer), sender_endpoint&: endpoint, flags: in_flags, ec);
361 socket1.receive_from(buffers: null_buffers(), sender_endpoint&: endpoint, flags: in_flags, ec);
362
363 socket1.async_receive_from(buffers: buffer(data&: mutable_char_buffer),
364 sender_endpoint&: endpoint, handler: &receive_handler);
365 socket1.async_receive_from(buffers: null_buffers(),
366 sender_endpoint&: endpoint, handler: &receive_handler);
367 socket1.async_receive_from(buffers: buffer(data&: mutable_char_buffer),
368 sender_endpoint&: endpoint, flags: in_flags, handler: &receive_handler);
369 socket1.async_receive_from(buffers: null_buffers(),
370 sender_endpoint&: endpoint, flags: in_flags, handler: &receive_handler);
371 int i25 = socket1.async_receive_from(buffers: buffer(data&: mutable_char_buffer),
372 sender_endpoint&: endpoint, handler&: lazy);
373 (void)i25;
374 int i26 = socket1.async_receive_from(buffers: null_buffers(),
375 sender_endpoint&: endpoint, handler&: lazy);
376 (void)i26;
377 int i27 = socket1.async_receive_from(buffers: buffer(data&: mutable_char_buffer),
378 sender_endpoint&: endpoint, flags: in_flags, handler&: lazy);
379 (void)i27;
380 int i28 = socket1.async_receive_from(buffers: null_buffers(),
381 sender_endpoint&: endpoint, flags: in_flags, handler&: lazy);
382 (void)i28;
383 }
384 catch (std::exception&)
385 {
386 }
387}
388
389} // namespace ip_icmp_socket_compile
390
391//------------------------------------------------------------------------------
392
393// ip_icmp_resolver_compile test
394// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
395// The following test checks that all public member functions on the class
396// ip::icmp::resolver compile and link correctly. Runtime failures are ignored.
397
398namespace ip_icmp_resolver_compile {
399
400void resolve_handler(const boost::system::error_code&,
401 boost::asio::ip::icmp::resolver::iterator)
402{
403}
404
405void test()
406{
407 using namespace boost::asio;
408 namespace ip = boost::asio::ip;
409
410 try
411 {
412 io_service ios;
413 archetypes::lazy_handler lazy;
414 boost::system::error_code ec;
415 ip::icmp::resolver::query q(ip::icmp::v4(), "localhost", "0");
416 ip::icmp::endpoint e(ip::address_v4::loopback(), 0);
417
418 // basic_resolver constructors.
419
420 ip::icmp::resolver resolver(ios);
421
422 // basic_io_object functions.
423
424 io_service& ios_ref = resolver.get_io_service();
425 (void)ios_ref;
426
427 // basic_resolver functions.
428
429 resolver.cancel();
430
431 ip::icmp::resolver::iterator iter1 = resolver.resolve(q);
432 (void)iter1;
433
434 ip::icmp::resolver::iterator iter2 = resolver.resolve(q, ec);
435 (void)iter2;
436
437 ip::icmp::resolver::iterator iter3 = resolver.resolve(e);
438 (void)iter3;
439
440 ip::icmp::resolver::iterator iter4 = resolver.resolve(e, ec);
441 (void)iter4;
442
443 resolver.async_resolve(q, handler: &resolve_handler);
444 int i1 = resolver.async_resolve(q, handler&: lazy);
445 (void)i1;
446
447 resolver.async_resolve(e, handler: &resolve_handler);
448 int i2 = resolver.async_resolve(e, handler&: lazy);
449 (void)i2;
450 }
451 catch (std::exception&)
452 {
453 }
454}
455
456} // namespace ip_icmp_resolver_compile
457
458//------------------------------------------------------------------------------
459
460BOOST_ASIO_TEST_SUITE
461(
462 "ip/icmp",
463 BOOST_ASIO_TEST_CASE(ip_icmp_socket_compile::test)
464 BOOST_ASIO_TEST_CASE(ip_icmp_resolver_compile::test)
465)
466

source code of boost/libs/asio/test/ip/icmp.cpp