• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // address_v4.cpp
3 // ~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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   catch (std::exception&)
156   {
157   }
158 }
159 
160 } // namespace ip_address_v4_compile
161 
162 //------------------------------------------------------------------------------
163 
164 // ip_address_v4_runtime test
165 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
166 // The following test checks that the various public member functions meet the
167 // necessary postconditions.
168 
169 namespace ip_address_v4_runtime {
170 
test()171 void test()
172 {
173   using boost::asio::ip::address_v4;
174 
175   address_v4 a1;
176   BOOST_ASIO_CHECK(a1.to_bytes()[0] == 0);
177   BOOST_ASIO_CHECK(a1.to_bytes()[1] == 0);
178   BOOST_ASIO_CHECK(a1.to_bytes()[2] == 0);
179   BOOST_ASIO_CHECK(a1.to_bytes()[3] == 0);
180   BOOST_ASIO_CHECK(a1.to_uint() == 0);
181 #if !defined(BOOST_ASIO_NO_DEPRECATED)
182   BOOST_ASIO_CHECK(a1.to_ulong() == 0);
183 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
184 
185   address_v4::bytes_type b1 = {{ 1, 2, 3, 4 }};
186   address_v4 a2(b1);
187   BOOST_ASIO_CHECK(a2.to_bytes()[0] == 1);
188   BOOST_ASIO_CHECK(a2.to_bytes()[1] == 2);
189   BOOST_ASIO_CHECK(a2.to_bytes()[2] == 3);
190   BOOST_ASIO_CHECK(a2.to_bytes()[3] == 4);
191   BOOST_ASIO_CHECK(((a2.to_uint() >> 24) & 0xFF) == b1[0]);
192   BOOST_ASIO_CHECK(((a2.to_uint() >> 16) & 0xFF) == b1[1]);
193   BOOST_ASIO_CHECK(((a2.to_uint() >> 8) & 0xFF) == b1[2]);
194   BOOST_ASIO_CHECK((a2.to_uint() & 0xFF) == b1[3]);
195 #if !defined(BOOST_ASIO_NO_DEPRECATED)
196   BOOST_ASIO_CHECK(((a2.to_ulong() >> 24) & 0xFF) == b1[0]);
197   BOOST_ASIO_CHECK(((a2.to_ulong() >> 16) & 0xFF) == b1[1]);
198   BOOST_ASIO_CHECK(((a2.to_ulong() >> 8) & 0xFF) == b1[2]);
199   BOOST_ASIO_CHECK((a2.to_ulong() & 0xFF) == b1[3]);
200 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
201 
202   address_v4 a3(0x01020304);
203   BOOST_ASIO_CHECK(a3.to_bytes()[0] == 1);
204   BOOST_ASIO_CHECK(a3.to_bytes()[1] == 2);
205   BOOST_ASIO_CHECK(a3.to_bytes()[2] == 3);
206   BOOST_ASIO_CHECK(a3.to_bytes()[3] == 4);
207   BOOST_ASIO_CHECK(a3.to_uint() == 0x01020304);
208 #if !defined(BOOST_ASIO_NO_DEPRECATED)
209   BOOST_ASIO_CHECK(a3.to_ulong() == 0x01020304);
210 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
211 
212   BOOST_ASIO_CHECK(address_v4(0x7F000001).is_loopback());
213   BOOST_ASIO_CHECK(address_v4(0x7F000002).is_loopback());
214   BOOST_ASIO_CHECK(!address_v4(0x00000000).is_loopback());
215   BOOST_ASIO_CHECK(!address_v4(0x01020304).is_loopback());
216 
217   BOOST_ASIO_CHECK(address_v4(0x00000000).is_unspecified());
218   BOOST_ASIO_CHECK(!address_v4(0x7F000001).is_unspecified());
219   BOOST_ASIO_CHECK(!address_v4(0x01020304).is_unspecified());
220 
221 #if !defined(BOOST_ASIO_NO_DEPRECATED)
222   BOOST_ASIO_CHECK(address_v4(0x01000000).is_class_a());
223   BOOST_ASIO_CHECK(address_v4(0x7F000000).is_class_a());
224   BOOST_ASIO_CHECK(!address_v4(0x80000000).is_class_a());
225   BOOST_ASIO_CHECK(!address_v4(0xBFFF0000).is_class_a());
226   BOOST_ASIO_CHECK(!address_v4(0xC0000000).is_class_a());
227   BOOST_ASIO_CHECK(!address_v4(0xDFFFFF00).is_class_a());
228   BOOST_ASIO_CHECK(!address_v4(0xE0000000).is_class_a());
229   BOOST_ASIO_CHECK(!address_v4(0xEFFFFFFF).is_class_a());
230   BOOST_ASIO_CHECK(!address_v4(0xF0000000).is_class_a());
231   BOOST_ASIO_CHECK(!address_v4(0xFFFFFFFF).is_class_a());
232 
233   BOOST_ASIO_CHECK(!address_v4(0x01000000).is_class_b());
234   BOOST_ASIO_CHECK(!address_v4(0x7F000000).is_class_b());
235   BOOST_ASIO_CHECK(address_v4(0x80000000).is_class_b());
236   BOOST_ASIO_CHECK(address_v4(0xBFFF0000).is_class_b());
237   BOOST_ASIO_CHECK(!address_v4(0xC0000000).is_class_b());
238   BOOST_ASIO_CHECK(!address_v4(0xDFFFFF00).is_class_b());
239   BOOST_ASIO_CHECK(!address_v4(0xE0000000).is_class_b());
240   BOOST_ASIO_CHECK(!address_v4(0xEFFFFFFF).is_class_b());
241   BOOST_ASIO_CHECK(!address_v4(0xF0000000).is_class_b());
242   BOOST_ASIO_CHECK(!address_v4(0xFFFFFFFF).is_class_b());
243 
244   BOOST_ASIO_CHECK(!address_v4(0x01000000).is_class_c());
245   BOOST_ASIO_CHECK(!address_v4(0x7F000000).is_class_c());
246   BOOST_ASIO_CHECK(!address_v4(0x80000000).is_class_c());
247   BOOST_ASIO_CHECK(!address_v4(0xBFFF0000).is_class_c());
248   BOOST_ASIO_CHECK(address_v4(0xC0000000).is_class_c());
249   BOOST_ASIO_CHECK(address_v4(0xDFFFFF00).is_class_c());
250   BOOST_ASIO_CHECK(!address_v4(0xE0000000).is_class_c());
251   BOOST_ASIO_CHECK(!address_v4(0xEFFFFFFF).is_class_c());
252   BOOST_ASIO_CHECK(!address_v4(0xF0000000).is_class_c());
253   BOOST_ASIO_CHECK(!address_v4(0xFFFFFFFF).is_class_c());
254 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
255 
256   BOOST_ASIO_CHECK(!address_v4(0x01000000).is_multicast());
257   BOOST_ASIO_CHECK(!address_v4(0x7F000000).is_multicast());
258   BOOST_ASIO_CHECK(!address_v4(0x80000000).is_multicast());
259   BOOST_ASIO_CHECK(!address_v4(0xBFFF0000).is_multicast());
260   BOOST_ASIO_CHECK(!address_v4(0xC0000000).is_multicast());
261   BOOST_ASIO_CHECK(!address_v4(0xDFFFFF00).is_multicast());
262   BOOST_ASIO_CHECK(address_v4(0xE0000000).is_multicast());
263   BOOST_ASIO_CHECK(address_v4(0xEFFFFFFF).is_multicast());
264   BOOST_ASIO_CHECK(!address_v4(0xF0000000).is_multicast());
265   BOOST_ASIO_CHECK(!address_v4(0xFFFFFFFF).is_multicast());
266 
267   address_v4 a4 = address_v4::any();
268   BOOST_ASIO_CHECK(a4.to_bytes()[0] == 0);
269   BOOST_ASIO_CHECK(a4.to_bytes()[1] == 0);
270   BOOST_ASIO_CHECK(a4.to_bytes()[2] == 0);
271   BOOST_ASIO_CHECK(a4.to_bytes()[3] == 0);
272   BOOST_ASIO_CHECK(a4.to_uint() == 0);
273 #if !defined(BOOST_ASIO_NO_DEPRECATED)
274   BOOST_ASIO_CHECK(a4.to_ulong() == 0);
275 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
276 
277   address_v4 a5 = address_v4::loopback();
278   BOOST_ASIO_CHECK(a5.to_bytes()[0] == 0x7F);
279   BOOST_ASIO_CHECK(a5.to_bytes()[1] == 0);
280   BOOST_ASIO_CHECK(a5.to_bytes()[2] == 0);
281   BOOST_ASIO_CHECK(a5.to_bytes()[3] == 0x01);
282   BOOST_ASIO_CHECK(a5.to_uint() == 0x7F000001);
283 #if !defined(BOOST_ASIO_NO_DEPRECATED)
284   BOOST_ASIO_CHECK(a5.to_ulong() == 0x7F000001);
285 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
286 
287   address_v4 a6 = address_v4::broadcast();
288   BOOST_ASIO_CHECK(a6.to_bytes()[0] == 0xFF);
289   BOOST_ASIO_CHECK(a6.to_bytes()[1] == 0xFF);
290   BOOST_ASIO_CHECK(a6.to_bytes()[2] == 0xFF);
291   BOOST_ASIO_CHECK(a6.to_bytes()[3] == 0xFF);
292   BOOST_ASIO_CHECK(a6.to_uint() == 0xFFFFFFFF);
293 #if !defined(BOOST_ASIO_NO_DEPRECATED)
294   BOOST_ASIO_CHECK(a6.to_ulong() == 0xFFFFFFFF);
295 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
296 
297 #if !defined(BOOST_ASIO_NO_DEPRECATED)
298   address_v4 class_a_net(0xFF000000);
299   address_v4 class_b_net(0xFFFF0000);
300   address_v4 class_c_net(0xFFFFFF00);
301   address_v4 other_net(0xFFFFFFFF);
302   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0x01000000)) == class_a_net);
303   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0x7F000000)) == class_a_net);
304   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0x80000000)) == class_b_net);
305   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xBFFF0000)) == class_b_net);
306   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xC0000000)) == class_c_net);
307   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xDFFFFF00)) == class_c_net);
308   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xE0000000)) == other_net);
309   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xEFFFFFFF)) == other_net);
310   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xF0000000)) == other_net);
311   BOOST_ASIO_CHECK(address_v4::netmask(address_v4(0xFFFFFFFF)) == other_net);
312 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
313 }
314 
315 } // namespace ip_address_v4_runtime
316 
317 //------------------------------------------------------------------------------
318 
319 BOOST_ASIO_TEST_SUITE
320 (
321   "ip/address_v4",
322   BOOST_ASIO_TEST_CASE(ip_address_v4_compile::test)
323   BOOST_ASIO_TEST_CASE(ip_address_v4_runtime::test)
324 )
325