1 //
2 // address_v4.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/ip/address_v4.hpp>
18 
19 #include "../unit_test.hpp"
20 #include <sstream>
21 
22 //------------------------------------------------------------------------------
23 
24 // ip_address_v4_compile test
25 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
26 // The following test checks that all public member functions on the class
27 // ip::address_v4 compile and link correctly. Runtime failures are ignored.
28 
29 namespace ip_address_v4_compile {
30 
test()31 void test()
32 {
33   using namespace boost::asio;
34   namespace ip = boost::asio::ip;
35 
36   try
37   {
38     boost::system::error_code ec;
39 
40     // address_v4 constructors.
41 
42     ip::address_v4 addr1;
43     const ip::address_v4::bytes_type const_bytes_value = { { 127, 0, 0, 1 } };
44     ip::address_v4 addr2(const_bytes_value);
45     const unsigned long const_ulong_value = 0x7F000001;
46     ip::address_v4 addr3(const_ulong_value);
47 
48     // address_v4 functions.
49 
50     bool b = addr1.is_loopback();
51     (void)b;
52 
53     b = addr1.is_unspecified();
54     (void)b;
55 
56 #if !defined(BOOST_ASIO_NO_DEPRECATED)
57     b = addr1.is_class_a();
58     (void)b;
59 
60     b = addr1.is_class_b();
61     (void)b;
62 
63     b = addr1.is_class_c();
64     (void)b;
65 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
66 
67     b = addr1.is_multicast();
68     (void)b;
69 
70     ip::address_v4::bytes_type bytes_value = addr1.to_bytes();
71     (void)bytes_value;
72 
73     ip::address_v4::uint_type uint_value = addr1.to_uint();
74     (void)uint_value;
75 
76 #if !defined(BOOST_ASIO_NO_DEPRECATED)
77     unsigned long ulong_value = addr1.to_ulong();
78     (void)ulong_value;
79 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
80 
81     std::string string_value = addr1.to_string();
82 #if !defined(BOOST_ASIO_NO_DEPRECATED)
83     string_value = addr1.to_string(ec);
84 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
85 
86     // address_v4 static functions.
87 
88 #if !defined(BOOST_ASIO_NO_DEPRECATED)
89     addr1 = ip::address_v4::from_string("127.0.0.1");
90     addr1 = ip::address_v4::from_string("127.0.0.1", ec);
91     addr1 = ip::address_v4::from_string(string_value);
92     addr1 = ip::address_v4::from_string(string_value, ec);
93 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
94 
95     addr1 = ip::address_v4::any();
96 
97     addr1 = ip::address_v4::loopback();
98 
99     addr1 = ip::address_v4::broadcast();
100 
101 #if !defined(BOOST_ASIO_NO_DEPRECATED)
102     addr1 = ip::address_v4::broadcast(addr2, addr3);
103 
104     addr1 = ip::address_v4::netmask(addr2);
105 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
106 
107     // address_v4 comparisons.
108 
109     b = (addr1 == addr2);
110     (void)b;
111 
112     b = (addr1 != addr2);
113     (void)b;
114 
115     b = (addr1 < addr2);
116     (void)b;
117 
118     b = (addr1 > addr2);
119     (void)b;
120 
121     b = (addr1 <= addr2);
122     (void)b;
123 
124     b = (addr1 >= addr2);
125     (void)b;
126 
127     // address_v4 creation functions.
128 
129     addr1 = ip::make_address_v4(const_bytes_value);
130     addr1 = ip::make_address_v4(const_ulong_value);
131     addr1 = ip::make_address_v4("127.0.0.1");
132     addr1 = ip::make_address_v4("127.0.0.1", ec);
133     addr1 = ip::make_address_v4(string_value);
134     addr1 = ip::make_address_v4(string_value, ec);
135 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
136 # if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
137     std::string_view string_view_value("127.0.0.1");
138 # elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
139     std::experimental::string_view string_view_value("127.0.0.1");
140 # endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
141     addr1 = ip::make_address_v4(string_view_value);
142     addr1 = ip::make_address_v4(string_view_value, ec);
143 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
144 
145     // address_v4 I/O.
146 
147     std::ostringstream os;
148     os << addr1;
149 
150 #if !defined(BOOST_NO_STD_WSTREAMBUF)
151     std::wostringstream wos;
152     wos << addr1;
153 #endif // !defined(BOOST_NO_STD_WSTREAMBUF)
154 
155 #if defined(BOOST_ASIO_HAS_STD_HASH)
156     std::size_t hash1 = std::hash<ip::address_v4>()(addr1);
157     (void)hash1;
158 #endif // defined(BOOST_ASIO_HAS_STD_HASH)
159   }
160   catch (std::exception&)
161   {
162   }
163 }
164 
165 } // namespace ip_address_v4_compile
166 
167 //------------------------------------------------------------------------------
168 
169 // ip_address_v4_runtime test
170 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
171 // The following test checks that the various public member functions meet the
172 // necessary postconditions.
173 
174 namespace ip_address_v4_runtime {
175 
test()176 void test()
177 {
178   using boost::asio::ip::address_v4;
179 
180   address_v4 a1;
181   BOOST_ASIO_CHECK(a1.to_bytes()[0] == 0);
182   BOOST_ASIO_CHECK(a1.to_bytes()[1] == 0);
183   BOOST_ASIO_CHECK(a1.to_bytes()[2] == 0);
184   BOOST_ASIO_CHECK(a1.to_bytes()[3] == 0);
185   BOOST_ASIO_CHECK(a1.to_uint() == 0);
186 #if !defined(BOOST_ASIO_NO_DEPRECATED)
187   BOOST_ASIO_CHECK(a1.to_ulong() == 0);
188 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
189 
190   address_v4::bytes_type b1 = {{ 1, 2, 3, 4 }};
191   address_v4 a2(b1);
192   BOOST_ASIO_CHECK(a2.to_bytes()[0] == 1);
193   BOOST_ASIO_CHECK(a2.to_bytes()[1] == 2);
194   BOOST_ASIO_CHECK(a2.to_bytes()[2] == 3);
195   BOOST_ASIO_CHECK(a2.to_bytes()[3] == 4);
196   BOOST_ASIO_CHECK(((a2.to_uint() >> 24) & 0xFF) == b1[0]);
197   BOOST_ASIO_CHECK(((a2.to_uint() >> 16) & 0xFF) == b1[1]);
198   BOOST_ASIO_CHECK(((a2.to_uint() >> 8) & 0xFF) == b1[2]);
199   BOOST_ASIO_CHECK((a2.to_uint() & 0xFF) == b1[3]);
200 #if !defined(BOOST_ASIO_NO_DEPRECATED)
201   BOOST_ASIO_CHECK(((a2.to_ulong() >> 24) & 0xFF) == b1[0]);
202   BOOST_ASIO_CHECK(((a2.to_ulong() >> 16) & 0xFF) == b1[1]);
203   BOOST_ASIO_CHECK(((a2.to_ulong() >> 8) & 0xFF) == b1[2]);
204   BOOST_ASIO_CHECK((a2.to_ulong() & 0xFF) == b1[3]);
205 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
206 
207   address_v4 a3(0x01020304);
208   BOOST_ASIO_CHECK(a3.to_bytes()[0] == 1);
209   BOOST_ASIO_CHECK(a3.to_bytes()[1] == 2);
210   BOOST_ASIO_CHECK(a3.to_bytes()[2] == 3);
211   BOOST_ASIO_CHECK(a3.to_bytes()[3] == 4);
212   BOOST_ASIO_CHECK(a3.to_uint() == 0x01020304);
213 #if !defined(BOOST_ASIO_NO_DEPRECATED)
214   BOOST_ASIO_CHECK(a3.to_ulong() == 0x01020304);
215 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
216 
217   BOOST_ASIO_CHECK(address_v4(0x7F000001).is_loopback());
218   BOOST_ASIO_CHECK(address_v4(0x7F000002).is_loopback());
219   BOOST_ASIO_CHECK(!address_v4(0x00000000).is_loopback());
220   BOOST_ASIO_CHECK(!address_v4(0x01020304).is_loopback());
221 
222   BOOST_ASIO_CHECK(address_v4(0x00000000).is_unspecified());
223   BOOST_ASIO_CHECK(!address_v4(0x7F000001).is_unspecified());
224   BOOST_ASIO_CHECK(!address_v4(0x01020304).is_unspecified());
225 
226 #if !defined(BOOST_ASIO_NO_DEPRECATED)
227   BOOST_ASIO_CHECK(address_v4(0x01000000).is_class_a());
228   BOOST_ASIO_CHECK(address_v4(0x7F000000).is_class_a());
229   BOOST_ASIO_CHECK(!address_v4(0x80000000).is_class_a());
230   BOOST_ASIO_CHECK(!address_v4(0xBFFF0000).is_class_a());
231   BOOST_ASIO_CHECK(!address_v4(0xC0000000).is_class_a());
232   BOOST_ASIO_CHECK(!address_v4(0xDFFFFF00).is_class_a());
233   BOOST_ASIO_CHECK(!address_v4(0xE0000000).is_class_a());
234   BOOST_ASIO_CHECK(!address_v4(0xEFFFFFFF).is_class_a());
235   BOOST_ASIO_CHECK(!address_v4(0xF0000000).is_class_a());
236   BOOST_ASIO_CHECK(!address_v4(0xFFFFFFFF).is_class_a());
237 
238   BOOST_ASIO_CHECK(!address_v4(0x01000000).is_class_b());
239   BOOST_ASIO_CHECK(!address_v4(0x7F000000).is_class_b());
240   BOOST_ASIO_CHECK(address_v4(0x80000000).is_class_b());
241   BOOST_ASIO_CHECK(address_v4(0xBFFF0000).is_class_b());
242   BOOST_ASIO_CHECK(!address_v4(0xC0000000).is_class_b());
243   BOOST_ASIO_CHECK(!address_v4(0xDFFFFF00).is_class_b());
244   BOOST_ASIO_CHECK(!address_v4(0xE0000000).is_class_b());
245   BOOST_ASIO_CHECK(!address_v4(0xEFFFFFFF).is_class_b());
246   BOOST_ASIO_CHECK(!address_v4(0xF0000000).is_class_b());
247   BOOST_ASIO_CHECK(!address_v4(0xFFFFFFFF).is_class_b());
248 
249   BOOST_ASIO_CHECK(!address_v4(0x01000000).is_class_c());
250   BOOST_ASIO_CHECK(!address_v4(0x7F000000).is_class_c());
251   BOOST_ASIO_CHECK(!address_v4(0x80000000).is_class_c());
252   BOOST_ASIO_CHECK(!address_v4(0xBFFF0000).is_class_c());
253   BOOST_ASIO_CHECK(address_v4(0xC0000000).is_class_c());
254   BOOST_ASIO_CHECK(address_v4(0xDFFFFF00).is_class_c());
255   BOOST_ASIO_CHECK(!address_v4(0xE0000000).is_class_c());
256   BOOST_ASIO_CHECK(!address_v4(0xEFFFFFFF).is_class_c());
257   BOOST_ASIO_CHECK(!address_v4(0xF0000000).is_class_c());
258   BOOST_ASIO_CHECK(!address_v4(0xFFFFFFFF).is_class_c());
259 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
260 
261   BOOST_ASIO_CHECK(!address_v4(0x01000000).is_multicast());
262   BOOST_ASIO_CHECK(!address_v4(0x7F000000).is_multicast());
263   BOOST_ASIO_CHECK(!address_v4(0x80000000).is_multicast());
264   BOOST_ASIO_CHECK(!address_v4(0xBFFF0000).is_multicast());
265   BOOST_ASIO_CHECK(!address_v4(0xC0000000).is_multicast());
266   BOOST_ASIO_CHECK(!address_v4(0xDFFFFF00).is_multicast());
267   BOOST_ASIO_CHECK(address_v4(0xE0000000).is_multicast());
268   BOOST_ASIO_CHECK(address_v4(0xEFFFFFFF).is_multicast());
269   BOOST_ASIO_CHECK(!address_v4(0xF0000000).is_multicast());
270   BOOST_ASIO_CHECK(!address_v4(0xFFFFFFFF).is_multicast());
271 
272   address_v4 a4 = address_v4::any();
273   BOOST_ASIO_CHECK(a4.to_bytes()[0] == 0);
274   BOOST_ASIO_CHECK(a4.to_bytes()[1] == 0);
275   BOOST_ASIO_CHECK(a4.to_bytes()[2] == 0);
276   BOOST_ASIO_CHECK(a4.to_bytes()[3] == 0);
277   BOOST_ASIO_CHECK(a4.to_uint() == 0);
278 #if !defined(BOOST_ASIO_NO_DEPRECATED)
279   BOOST_ASIO_CHECK(a4.to_ulong() == 0);
280 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
281 
282   address_v4 a5 = address_v4::loopback();
283   BOOST_ASIO_CHECK(a5.to_bytes()[0] == 0x7F);
284   BOOST_ASIO_CHECK(a5.to_bytes()[1] == 0);
285   BOOST_ASIO_CHECK(a5.to_bytes()[2] == 0);
286   BOOST_ASIO_CHECK(a5.to_bytes()[3] == 0x01);
287   BOOST_ASIO_CHECK(a5.to_uint() == 0x7F000001);
288 #if !defined(BOOST_ASIO_NO_DEPRECATED)
289   BOOST_ASIO_CHECK(a5.to_ulong() == 0x7F000001);
290 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
291 
292   address_v4 a6 = address_v4::broadcast();
293   BOOST_ASIO_CHECK(a6.to_bytes()[0] == 0xFF);
294   BOOST_ASIO_CHECK(a6.to_bytes()[1] == 0xFF);
295   BOOST_ASIO_CHECK(a6.to_bytes()[2] == 0xFF);
296   BOOST_ASIO_CHECK(a6.to_bytes()[3] == 0xFF);
297   BOOST_ASIO_CHECK(a6.to_uint() == 0xFFFFFFFF);
298 #if !defined(BOOST_ASIO_NO_DEPRECATED)
299   BOOST_ASIO_CHECK(a6.to_ulong() == 0xFFFFFFFF);
300 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
301 
302 #if !defined(BOOST_ASIO_NO_DEPRECATED)
303   address_v4 class_a_net(0xFF000000);
304   address_v4 class_b_net(0xFFFF0000);
305   address_v4 class_c_net(0xFFFFFF00);
306   address_v4 other_net(0xFFFFFFFF);
307   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0x01000000)) == class_a_net);
308   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0x7F000000)) == class_a_net);
309   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0x80000000)) == class_b_net);
310   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xBFFF0000)) == class_b_net);
311   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xC0000000)) == class_c_net);
312   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xDFFFFF00)) == class_c_net);
313   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xE0000000)) == other_net);
314   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xEFFFFFFF)) == other_net);
315   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xF0000000)) == other_net);
316   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xFFFFFFFF)) == other_net);
317 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
318 }
319 
320 } // namespace ip_address_v4_runtime
321 
322 //------------------------------------------------------------------------------
323 
324 BOOST_ASIO_TEST_SUITE
325 (
326   "ip/address_v4",
327   BOOST_ASIO_TEST_CASE(ip_address_v4_compile::test)
328   BOOST_ASIO_TEST_CASE(ip_address_v4_runtime::test)
329 )
330