testIpAddress.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "ip/Address.h"
11 #include "ip/tools.h"
12 #include "tests/testIpAddress.h"
13 #include "unitTestMain.h"
14 
15 #include <cstring>
16 #include <stdexcept>
17 #include <string>
18 #if HAVE_NETINET_IN_H
19 #include <netinet/in.h>
20 #endif
21 #if HAVE_ARPA_INET_H
22 #include <arpa/inet.h>
23 #endif
24 #if HAVE_NETDB_H
25 #include <netdb.h>
26 #endif
27 
29 
30 /* so that we don't break POD dependency just for the test */
31 struct timeval current_time;
33 time_t squid_curtime = 0;
34 int shutting_down = 0;
35 
36 void
38 {
39  Ip::Address anIPA;
40 
41  /* test stored values */
42  CPPUNIT_ASSERT( anIPA.isAnyAddr() );
43  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
44  CPPUNIT_ASSERT( !anIPA.isIPv4() );
45  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
46  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
47  CPPUNIT_ASSERT( anIPA.isIPv6() );
48 }
49 
50 void
52 {
53  struct in_addr inval;
54  struct in_addr outval;
55 
56  inval.s_addr = htonl(0xC0A8640C);
57  outval.s_addr = htonl(0x00000000);
58 
59  Ip::Address anIPA(inval);
60 
61  /* test stored values */
62  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
63  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
64  CPPUNIT_ASSERT( anIPA.isIPv4() );
65  CPPUNIT_ASSERT( !anIPA.isIPv6() );
66  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
67  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
68  anIPA.getInAddr(outval);
69  CPPUNIT_ASSERT( memcmp(&inval, &outval, sizeof(struct in_addr)) == 0 );
70 }
71 
72 void
74 {
75  struct in6_addr inval;
76  struct in6_addr outval = IN6ADDR_ANY_INIT;
77 
78  inval.s6_addr32[0] = htonl(0xC0A8640C);
79  inval.s6_addr32[1] = htonl(0xFFFFFFFF);
80  inval.s6_addr32[2] = htonl(0xFFFFFFFF);
81  inval.s6_addr32[3] = htonl(0xFFFFFFFF);
82 
83  Ip::Address anIPA(inval);
84 
85  /* test stored values */
86  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
87  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
88  CPPUNIT_ASSERT( !anIPA.isIPv4() );
89  CPPUNIT_ASSERT( anIPA.isIPv6() );
90  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
91  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
92  anIPA.getInAddr(outval);
93  CPPUNIT_ASSERT( memcmp( &inval, &outval, sizeof(struct in6_addr)) == 0 );
94 }
95 
96 void
98 {
99  struct sockaddr_in insock;
100  struct sockaddr_in outsock;
101 
102  memset(&insock, 0, sizeof(struct sockaddr_in));
103  memset(&outsock, 0, sizeof(struct sockaddr_in));
104 
105  insock.sin_family = AF_INET;
106  insock.sin_port = htons(80);
107  insock.sin_addr.s_addr = htonl(0xC0A8640C);
108 #if HAVE_SIN_LEN_IN_SAI
109  insock.sin_len = sizeof(struct sockaddr_in);
110 #endif
111 
112  Ip::Address anIPA(insock);
113 
114  /* test stored values */
115  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
116  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
117  CPPUNIT_ASSERT( anIPA.isIPv4() );
118  CPPUNIT_ASSERT( !anIPA.isIPv6() );
119  CPPUNIT_ASSERT( anIPA.isSockAddr() );
120  CPPUNIT_ASSERT_EQUAL( (unsigned short) 80, anIPA.port() );
121  anIPA.getSockAddr(outsock);
122  CPPUNIT_ASSERT( memcmp( &insock, &outsock, sizeof(struct sockaddr_in)) == 0 );
123 }
124 
125 void
127 {
128  struct sockaddr_in6 insock;
129  struct sockaddr_in6 outsock;
130 
131  memset(&insock, 0, sizeof(struct sockaddr_in6));
132  memset(&outsock, 0, sizeof(struct sockaddr_in6));
133 
134  insock.sin6_family = AF_INET6;
135  insock.sin6_port = htons(80);
136  insock.sin6_addr.s6_addr32[0] = htonl(0xFFFFFFFF);
137  insock.sin6_addr.s6_addr32[1] = htonl(0x00000000);
138  insock.sin6_addr.s6_addr32[2] = htonl(0x0000FFFF);
139  insock.sin6_addr.s6_addr32[3] = htonl(0xC0A8640C);
140 #if HAVE_SIN6_LEN_IN_SAI
141  insock.sin6_len = sizeof(struct sockaddr_in6);
142 #endif
143 
144  Ip::Address anIPA((const struct sockaddr_in6)insock);
145 
146  /* test stored values */
147  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
148  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
149  CPPUNIT_ASSERT( !anIPA.isIPv4() );
150  CPPUNIT_ASSERT( anIPA.isIPv6() );
151  CPPUNIT_ASSERT( anIPA.isSockAddr() );
152  CPPUNIT_ASSERT_EQUAL( (unsigned short) 80, anIPA.port() );
153  anIPA.getSockAddr(outsock);
154  CPPUNIT_ASSERT( memcmp( &insock, &outsock, sizeof(struct sockaddr_in6)) == 0 );
155 }
156 
157 void
159 {
160  struct sockaddr_in insock;
161  struct sockaddr_in outsock;
162 
163  memset(&insock, 0, sizeof(struct sockaddr_in));
164  memset(&outsock, 0, sizeof(struct sockaddr_in));
165 
166  insock.sin_family = AF_INET;
167  insock.sin_port = htons(80);
168  insock.sin_addr.s_addr = htonl(0xC0A8640C);
169 #if HAVE_SIN_LEN_IN_SAI
170  insock.sin_len = sizeof(struct sockaddr_in);
171 #endif
172 
173  Ip::Address inIPA(insock);
174  Ip::Address outIPA(inIPA);
175 
176  /* test stored values */
177  CPPUNIT_ASSERT( !outIPA.isAnyAddr() );
178  CPPUNIT_ASSERT( !outIPA.isNoAddr() );
179  CPPUNIT_ASSERT( outIPA.isIPv4() );
180  CPPUNIT_ASSERT( !outIPA.isIPv6() );
181  CPPUNIT_ASSERT( outIPA.isSockAddr() );
182  CPPUNIT_ASSERT_EQUAL( (unsigned short) 80, outIPA.port() );
183  outIPA.getSockAddr(outsock);
184  CPPUNIT_ASSERT( memcmp( &insock, &outsock, sizeof(struct sockaddr_in)) == 0 );
185 }
186 
187 void
189 {
190  struct hostent *hp = NULL;
191  struct in_addr outval;
192  struct in_addr expectval;
193 
194  expectval.s_addr = htonl(0xC0A8640C);
195 
196  hp = gethostbyname("192.168.100.12");
197  CPPUNIT_ASSERT( hp != NULL /* gethostbyname failure.*/ );
198 
199  Ip::Address anIPA(*hp);
200 
201  /* test stored values */
202  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
203  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
204  CPPUNIT_ASSERT( anIPA.isIPv4() );
205  CPPUNIT_ASSERT( !anIPA.isIPv6() );
206  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
207  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
208  anIPA.getInAddr(outval);
209  CPPUNIT_ASSERT( memcmp( &expectval, &outval, sizeof(struct in_addr)) == 0 );
210 }
211 
212 void
214 {
215  struct in_addr outval;
216  struct in_addr expectval;
217 
218  expectval.s_addr = htonl(0xC0A8640C);
219 
220  Ip::Address anIPA = "192.168.100.12";
221 
222  /* test stored values */
223  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
224  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
225  CPPUNIT_ASSERT( anIPA.isIPv4() );
226  CPPUNIT_ASSERT( !anIPA.isIPv6() );
227  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
228  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
229  anIPA.getInAddr(outval);
230  CPPUNIT_ASSERT( memcmp( &expectval, &outval, sizeof(struct in_addr)) == 0 );
231 
232  struct in6_addr expectv6;
233  struct in6_addr outval6;
234 
235  expectv6.s6_addr32[0] = htonl(0x20000800);
236  expectv6.s6_addr32[1] = htonl(0x00000000);
237  expectv6.s6_addr32[2] = htonl(0x00000000);
238  expectv6.s6_addr32[3] = htonl(0x00000045);
239 
240  Ip::Address bnIPA = "2000:800::45";
241 
242 //char test[256];
243 //bnIPA.toStr(test, 256);
244 //printf("bnIPA: %s\n", test);
245 
246  /* test stored values */
247  CPPUNIT_ASSERT( !bnIPA.isAnyAddr() );
248  CPPUNIT_ASSERT( !bnIPA.isNoAddr() );
249  CPPUNIT_ASSERT( !bnIPA.isIPv4() );
250  CPPUNIT_ASSERT( bnIPA.isIPv6() );
251  CPPUNIT_ASSERT( !bnIPA.isSockAddr() );
252  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, bnIPA.port() );
253  bnIPA.getInAddr(outval6);
254  CPPUNIT_ASSERT( memcmp( &expectv6, &outval6, sizeof(struct in6_addr)) == 0 );
255 
256  /* test IPv6 as an old netmask format. This is invalid but sometimes use. */
257  Ip::Address cnIPA = "ffff:ffff:fff0::";
258 
259  expectv6.s6_addr32[0] = htonl(0xFFFFFFFF);
260  expectv6.s6_addr32[1] = htonl(0xFFF00000);
261  expectv6.s6_addr32[2] = htonl(0x00000000);
262  expectv6.s6_addr32[3] = htonl(0x00000000);
263 
264  /* test stored values */
265  CPPUNIT_ASSERT( !cnIPA.isAnyAddr() );
266  CPPUNIT_ASSERT( !cnIPA.isNoAddr() );
267  CPPUNIT_ASSERT( !cnIPA.isIPv4() );
268  CPPUNIT_ASSERT( cnIPA.isIPv6() );
269  CPPUNIT_ASSERT( !cnIPA.isSockAddr() );
270  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, cnIPA.port() );
271  cnIPA.getInAddr(outval6);
272  CPPUNIT_ASSERT( memcmp( &expectv6, &outval6, sizeof(struct in6_addr)) == 0 );
273 }
274 
275 void
277 {
278  Ip::Address anIPA;
279  struct in_addr inval;
280 
281  inval.s_addr = htonl(0xC0A8640C);
282 
283  anIPA = inval;
284 
285  /* test stored values before empty */
286  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
287  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
288  CPPUNIT_ASSERT( anIPA.isIPv4() );
289  CPPUNIT_ASSERT( !anIPA.isIPv6() );
290  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
291  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
292 
293  anIPA.setEmpty();
294 
295  /* test stored values after empty */
296  CPPUNIT_ASSERT( anIPA.isAnyAddr() );
297  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
298  CPPUNIT_ASSERT( !anIPA.isIPv4() );
299  CPPUNIT_ASSERT( anIPA.isIPv6() );
300  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
301  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
302 }
303 
304 void
306 {
307  Ip::Address lhsIPA;
308  Ip::Address rhsIPA;
309  struct in_addr valLow;
310  struct in_addr valHigh;
311 
312  valLow.s_addr = htonl(0xC0A8640C);
313  valHigh.s_addr = htonl(0xC0A8640F);
314 
315  /* test equality */
316  lhsIPA = valLow;
317  rhsIPA = valLow;
318  CPPUNIT_ASSERT( lhsIPA.matchIPAddr(rhsIPA) == 0 );
319  CPPUNIT_ASSERT( ( lhsIPA == rhsIPA ) );
320  CPPUNIT_ASSERT( !( lhsIPA != rhsIPA ) );
321  CPPUNIT_ASSERT( ( lhsIPA >= rhsIPA ) );
322  CPPUNIT_ASSERT( !( lhsIPA > rhsIPA ) );
323  CPPUNIT_ASSERT( ( lhsIPA <= rhsIPA ) );
324  CPPUNIT_ASSERT( !( lhsIPA < rhsIPA ) );
325 
326  /* test equality versus ANYADDR */
327  lhsIPA.setAnyAddr();
328  rhsIPA.setAnyAddr();
329  CPPUNIT_ASSERT( lhsIPA.matchIPAddr(rhsIPA) == 0 );
330  CPPUNIT_ASSERT( ( lhsIPA == rhsIPA ) );
331  CPPUNIT_ASSERT( !( lhsIPA != rhsIPA ) );
332  CPPUNIT_ASSERT( ( lhsIPA >= rhsIPA ) );
333  CPPUNIT_ASSERT( !( lhsIPA > rhsIPA ) );
334  CPPUNIT_ASSERT( ( lhsIPA <= rhsIPA ) );
335  CPPUNIT_ASSERT( !( lhsIPA < rhsIPA ) );
336 
337  /* test equality versus NOADDR */
338  lhsIPA.setNoAddr();
339  rhsIPA.setNoAddr();
340  CPPUNIT_ASSERT( lhsIPA.matchIPAddr(rhsIPA) == 0 );
341  CPPUNIT_ASSERT( ( lhsIPA == rhsIPA ) );
342  CPPUNIT_ASSERT( !( lhsIPA != rhsIPA ) );
343  CPPUNIT_ASSERT( ( lhsIPA >= rhsIPA ) );
344  CPPUNIT_ASSERT( !( lhsIPA > rhsIPA ) );
345  CPPUNIT_ASSERT( ( lhsIPA <= rhsIPA ) );
346  CPPUNIT_ASSERT( !( lhsIPA < rhsIPA ) );
347 
348  /* test inequality (less than) */
349  lhsIPA = valLow;
350  rhsIPA = valHigh;
351  CPPUNIT_ASSERT( lhsIPA.matchIPAddr(rhsIPA) < 0 );
352  CPPUNIT_ASSERT( !( lhsIPA == rhsIPA ) );
353  CPPUNIT_ASSERT( ( lhsIPA != rhsIPA ) );
354  CPPUNIT_ASSERT( !( lhsIPA >= rhsIPA ) );
355  CPPUNIT_ASSERT( !( lhsIPA > rhsIPA ) );
356  CPPUNIT_ASSERT( ( lhsIPA <= rhsIPA ) );
357  CPPUNIT_ASSERT( ( lhsIPA < rhsIPA ) );
358 
359  /* test inequality versus ANYADDR (less than) */
360  lhsIPA.setAnyAddr();
361  rhsIPA = valHigh;
362  CPPUNIT_ASSERT( lhsIPA.matchIPAddr(rhsIPA) < 0 );
363  CPPUNIT_ASSERT( !( lhsIPA == rhsIPA ) );
364  CPPUNIT_ASSERT( ( lhsIPA != rhsIPA ) );
365  CPPUNIT_ASSERT( !( lhsIPA >= rhsIPA ) );
366  CPPUNIT_ASSERT( !( lhsIPA > rhsIPA ) );
367  CPPUNIT_ASSERT( ( lhsIPA <= rhsIPA ) );
368  CPPUNIT_ASSERT( ( lhsIPA < rhsIPA ) );
369 
370  /* test inequality versus NOADDR (less than) */
371  lhsIPA = valLow;
372  rhsIPA.setNoAddr();
373  CPPUNIT_ASSERT( lhsIPA.matchIPAddr(rhsIPA) < 0 );
374  CPPUNIT_ASSERT( !( lhsIPA == rhsIPA ) );
375  CPPUNIT_ASSERT( ( lhsIPA != rhsIPA ) );
376  CPPUNIT_ASSERT( !( lhsIPA >= rhsIPA ) );
377  CPPUNIT_ASSERT( !( lhsIPA > rhsIPA ) );
378  CPPUNIT_ASSERT( ( lhsIPA <= rhsIPA ) );
379  CPPUNIT_ASSERT( ( lhsIPA < rhsIPA ) );
380 
381  /* test inequality (greater than) */
382  lhsIPA = valHigh;
383  rhsIPA = valLow;
384  CPPUNIT_ASSERT( lhsIPA.matchIPAddr(rhsIPA) > 0 );
385  CPPUNIT_ASSERT( !( lhsIPA == rhsIPA ) );
386  CPPUNIT_ASSERT( ( lhsIPA != rhsIPA ) );
387  CPPUNIT_ASSERT( ( lhsIPA >= rhsIPA ) );
388  CPPUNIT_ASSERT( ( lhsIPA > rhsIPA ) );
389  CPPUNIT_ASSERT( !( lhsIPA <= rhsIPA ) );
390  CPPUNIT_ASSERT( !( lhsIPA < rhsIPA ) );
391 
392  /* test inequality (greater than) */
393  lhsIPA = valHigh;
394  rhsIPA.setAnyAddr();
395  CPPUNIT_ASSERT( lhsIPA.matchIPAddr(rhsIPA) > 0 );
396  CPPUNIT_ASSERT( !( lhsIPA == rhsIPA ) );
397  CPPUNIT_ASSERT( ( lhsIPA != rhsIPA ) );
398  CPPUNIT_ASSERT( ( lhsIPA >= rhsIPA ) );
399  CPPUNIT_ASSERT( ( lhsIPA > rhsIPA ) );
400  CPPUNIT_ASSERT( !( lhsIPA <= rhsIPA ) );
401  CPPUNIT_ASSERT( !( lhsIPA < rhsIPA ) );
402 
403  /* test inequality versus NOADDR (greater than) */
404  lhsIPA.setNoAddr();
405  rhsIPA = valLow;
406  CPPUNIT_ASSERT( lhsIPA.matchIPAddr(rhsIPA) > 0 );
407  CPPUNIT_ASSERT( !( lhsIPA == rhsIPA ) );
408  CPPUNIT_ASSERT( ( lhsIPA != rhsIPA ) );
409  CPPUNIT_ASSERT( ( lhsIPA >= rhsIPA ) );
410  CPPUNIT_ASSERT( ( lhsIPA > rhsIPA ) );
411  CPPUNIT_ASSERT( !( lhsIPA <= rhsIPA ) );
412  CPPUNIT_ASSERT( !( lhsIPA < rhsIPA ) );
413 
414 }
415 
416 void
418 {
419  struct in_addr inval;
420  char buf[MAX_IPSTRLEN];
421  Ip::Address anIPA;
422 
423  anIPA.setAnyAddr();
424 
425  /* test AnyAddr display values */
426  CPPUNIT_ASSERT( memcmp("::", anIPA.toStr(buf,MAX_IPSTRLEN), 2) == 0 );
427 
428  inval.s_addr = htonl(0xC0A8640C);
429  anIPA = inval;
430 
431  /* test IP display */
432  CPPUNIT_ASSERT( memcmp("192.168.100.12",anIPA.toStr(buf,MAX_IPSTRLEN), 14) == 0 );
433 
434  anIPA.setNoAddr();
435 
436  /* test NoAddr display values */
437  CPPUNIT_ASSERT( memcmp("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",anIPA.toStr(buf,MAX_IPSTRLEN), 39) == 0 );
438 }
439 
440 void
442 {
443  char buf[MAX_IPSTRLEN];
444  buf[0] = '\0';
445  struct in_addr inval;
446 
447  inval.s_addr = htonl(0xC0A8640C);
448 
449  Ip::Address anIPA(inval);
450 
451  /* test values */
452  anIPA.toUrl(buf,MAX_IPSTRLEN);
453  CPPUNIT_ASSERT( memcmp("192.168.100.12", buf, 14) == 0 );
454 
455  /* test output when constructed from in6_addr with IPv6 */
456  struct in6_addr ip6val;
457 
458  ip6val.s6_addr32[0] = htonl(0xC0A8640C);
459  ip6val.s6_addr32[1] = htonl(0xFFFFFFFF);
460  ip6val.s6_addr32[2] = htonl(0xFFFFFFFF);
461  ip6val.s6_addr32[3] = htonl(0xFFFFFFFF);
462 
463  Ip::Address bnIPA(ip6val);
464 
465  bnIPA.toUrl(buf,MAX_IPSTRLEN);
466  CPPUNIT_ASSERT( memcmp("[c0a8:640c:ffff:ffff:ffff:ffff:ffff:ffff]", buf, 41) == 0 );
467 }
468 
469 void
471 {
472  struct sockaddr_in sock;
473  sock.sin_addr.s_addr = htonl(0xC0A8640C);
474  sock.sin_port = htons(80);
475  sock.sin_family = AF_INET;
476 #if HAVE_SIN_LEN_IN_SAI
477  sock.sin_len = sizeof(struct sockaddr_in);
478 #endif
479 
480  Ip::Address anIPA(sock);
481  char buf[MAX_IPSTRLEN];
482 
483  /* test values */
484  anIPA.toUrl(buf,MAX_IPSTRLEN);
485  CPPUNIT_ASSERT( memcmp("192.168.100.12:80", buf, 17) == 0 );
486 
487  /* test output when constructed from in6_addr with IPv6 */
488  struct sockaddr_in6 ip6val;
489 
490  ip6val.sin6_addr.s6_addr32[0] = htonl(0xC0A8640C);
491  ip6val.sin6_addr.s6_addr32[1] = htonl(0xFFFFFFFF);
492  ip6val.sin6_addr.s6_addr32[2] = htonl(0xFFFFFFFF);
493  ip6val.sin6_addr.s6_addr32[3] = htonl(0xFFFFFFFF);
494  ip6val.sin6_port = htons(80);
495  ip6val.sin6_family = AF_INET6;
496 #if HAVE_SIN6_LEN_IN_SAI
497  ip6val.sin6_len = sizeof(struct sockaddr_in6);
498 #endif
499 
500  Ip::Address bnIPA(ip6val);
501 
502  bnIPA.toUrl(buf,MAX_IPSTRLEN);
503  CPPUNIT_ASSERT( memcmp("[c0a8:640c:ffff:ffff:ffff:ffff:ffff:ffff]:80", buf, 44) == 0 );
504 }
505 
506 void
508 {
509  char buf[MAX_IPSTRLEN];
510 
511  struct in_addr ipv4val;
512  ipv4val.s_addr = htonl(0xC0A8640C);
513 
514  Ip::Address v4IPA(ipv4val);
515 
516  /* test IPv4 output */
517  v4IPA.getReverseString(buf);
518  CPPUNIT_ASSERT( memcmp("12.100.168.192.in-addr.arpa.",buf, 28) == 0 );
519 
520  v4IPA.getReverseString(buf,AF_INET);
521  CPPUNIT_ASSERT( memcmp("12.100.168.192.in-addr.arpa.",buf, 28) == 0 );
522 
523  v4IPA.getReverseString(buf,AF_INET6);
524  CPPUNIT_ASSERT( memcmp("",buf, 1) == 0 );
525 
526  struct in6_addr ip6val;
527 
528  ip6val.s6_addr32[0] = htonl(0xC0A8640C);
529  ip6val.s6_addr32[1] = htonl(0xFFFFFFFF);
530  ip6val.s6_addr32[2] = htonl(0xFFFFFFFF);
531  ip6val.s6_addr32[3] = htonl(0xFFFFFFFF);
532 
533  Ip::Address v6IPA(ip6val);
534 
535  /* test IPv6 output */
536  v6IPA.getReverseString(buf);
537  CPPUNIT_ASSERT( memcmp("f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.c.0.4.6.8.a.0.c.ip6.arpa.",buf,73) == 0 );
538 }
539 
540 void
542 {
543  char buf[MAX_IPSTRLEN];
544  Ip::Address anIPA;
545  Ip::Address maskIPA;
546 
547  /* Test Basic CIDR Routine */
548  anIPA.setAnyAddr();
549  CPPUNIT_ASSERT_EQUAL( 0,anIPA.cidr() );
550 
551  anIPA.setNoAddr();
552  CPPUNIT_ASSERT_EQUAL( 128, anIPA.cidr() );
553 
554  /* Test Numeric ApplyCIDR */
555  anIPA.setNoAddr();
556  CPPUNIT_ASSERT( !anIPA.applyMask(129,AF_INET6) );
557  CPPUNIT_ASSERT( !anIPA.applyMask(33,AF_INET) );
558 
559  anIPA.setNoAddr();
560  CPPUNIT_ASSERT( anIPA.applyMask(31,AF_INET) );
561  CPPUNIT_ASSERT_EQUAL( 127, anIPA.cidr() );
562 
563  anIPA.setNoAddr();
564  CPPUNIT_ASSERT( anIPA.applyMask(127,AF_INET6) );
565  CPPUNIT_ASSERT_EQUAL( 127, anIPA.cidr() );
566 
567  anIPA.setNoAddr();
568  anIPA.applyMask(80,AF_INET6);
569  CPPUNIT_ASSERT_EQUAL( 80, anIPA.cidr() );
570 
571  /* BUG Check: test values by display. */
572  CPPUNIT_ASSERT( anIPA.toStr(buf,MAX_IPSTRLEN) != NULL );
573  CPPUNIT_ASSERT( memcmp("ffff:ffff:ffff:ffff:ffff::", buf, 26) == 0 );
574 
575  /* Test Network Bitmask from Ip::Address */
576  anIPA.setNoAddr();
577  maskIPA = "255.255.240.0";
578  CPPUNIT_ASSERT_EQUAL( 20, maskIPA.cidr() );
579  anIPA.applyMask(maskIPA);
580  CPPUNIT_ASSERT_EQUAL( 20, anIPA.cidr() );
581 
582  /* BUG Check: test values memory after masking. */
583  struct in_addr btest;
584  CPPUNIT_ASSERT( anIPA.isIPv4() );
585  CPPUNIT_ASSERT( !anIPA.isIPv6() );
586  anIPA.getInAddr(btest);
587  CPPUNIT_ASSERT_EQUAL( (uint32_t)htonl(0xFFFFF000), btest.s_addr );
588 
589  /* BUG Check failing test. Masked values for display. */
590  CPPUNIT_ASSERT( memcmp("255.255.240.0",anIPA.toStr(buf,MAX_IPSTRLEN), 13) == 0 );
591 
592  anIPA.setNoAddr();
593  maskIPA.setNoAddr();
594 
595  /* IPv6 masks MUST be CIDR representations. */
596  /* however as with IPv4 they can technically be represented as a bitmask */
597  maskIPA = "ffff:ffff:fff0::";
598  CPPUNIT_ASSERT( !maskIPA.isAnyAddr() );
599  CPPUNIT_ASSERT( !maskIPA.isNoAddr() );
600  anIPA.applyMask(maskIPA);
601  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
602  CPPUNIT_ASSERT_EQUAL( 44, anIPA.cidr() );
603 
604  anIPA.setNoAddr();
605  maskIPA.setNoAddr();
606 
607  /* IPv4 masks represented in IPv6 as IPv4 bitmasks. */
608  maskIPA = "::ffff:ffff:f000";
609  CPPUNIT_ASSERT( !maskIPA.isAnyAddr() );
610  CPPUNIT_ASSERT( !maskIPA.isNoAddr() );
611  CPPUNIT_ASSERT( maskIPA.isIPv4() );
612  CPPUNIT_ASSERT( !maskIPA.isIPv6() );
613  anIPA.applyMask(maskIPA);
614  CPPUNIT_ASSERT( !maskIPA.isAnyAddr() );
615  CPPUNIT_ASSERT( !maskIPA.isNoAddr() );
616  CPPUNIT_ASSERT( maskIPA.isIPv4() );
617  CPPUNIT_ASSERT( !maskIPA.isIPv6() );
618  CPPUNIT_ASSERT_EQUAL( 20, anIPA.cidr() );
619 }
620 
621 void
623 {
624  struct addrinfo *expect;
625  struct addrinfo *ipval = NULL;
626  struct addrinfo hints;
627 
628  memset(&hints, 0, sizeof(struct addrinfo));
629 
630  hints.ai_flags = AI_NUMERICHOST;
631 
632  Ip::Address anIP = "127.0.0.1";
633 
634  /* assert this just to check that getaddrinfo is working properly */
635  CPPUNIT_ASSERT( getaddrinfo("127.0.0.1", NULL, &hints, &expect ) == 0 );
636 
637  anIP.getAddrInfo(ipval);
638 
639 #if 0
640  /* display a byte-by-byte hex comparison of the addr cores */
641  unsigned int *p;
642  p = (unsigned int*)expect;
643  printf("\nSYS-ADDRINFO: %2x %2x %2x %2x %2x %2x",
644  p[0],p[1],p[2],p[3],p[4],p[5]);
645 
646  p = (unsigned int*)ipval;
647  printf("\nSQD-ADDRINFO: %2x %2x %2x %2x %2x %2x",
648  p[0],p[1],p[2],p[3],p[4],p[5] );
649  printf("\n");
650 #endif /*0*/
651 
652  // check the addrinfo object core. (BUT not the two ptrs at the tail)
653  // details
654  CPPUNIT_ASSERT_EQUAL( expect->ai_flags, ipval->ai_flags );
655  CPPUNIT_ASSERT_EQUAL( expect->ai_family, ipval->ai_family );
656  // check the sockaddr it points to.
657  CPPUNIT_ASSERT_EQUAL( expect->ai_addrlen, ipval->ai_addrlen );
658 
659 #if 0
660  printf("sizeof IN(%d), IN6(%d), STORAGE(%d), \n",
661  sizeof(struct sockaddr_in), sizeof(struct sockaddr_in6), sizeof(struct sockaddr_storage));
662 
663  p = (unsigned int*)(expect->ai_addr);
664  printf("\nSYS-ADDR: (%d) {%d} %x %x %x %x %x %x %x %x ...",
665  expect->ai_addrlen, sizeof(*p),
666  p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7] );
667 
668  p = (unsigned int*)(ipval->ai_addr);
669  printf("\nSQD-ADDR: (%d) {%d} %x %x %x %x %x %x %x %x ...",
670  ipval->ai_addrlen, sizeof(*p),
671  p[0],p[1],p[2],p[3],p[4],p[5],p[6],p[7] );
672  printf("\n");
673 #if HAVE_SS_LEN_IN_SS
674  printf("\nSYS SS_LEN=%d\nSQD SS_LEN=%d\n",((struct sockaddr_storage*)expect->ai_addr)->ss_len,
675  ((struct sockaddr_storage*)ipval->ai_addr)->ss_len );
676 #endif
677 #endif /*0*/
678 
679 #if HAVE_SS_LEN_IN_SS
680  CPPUNIT_ASSERT_EQUAL( ((struct sockaddr_storage*)expect->ai_addr)->ss_len,
681  ((struct sockaddr_storage*)ipval->ai_addr)->ss_len );
682  CPPUNIT_ASSERT_EQUAL( (socklen_t)((struct sockaddr_storage*)ipval->ai_addr)->ss_len, ipval->ai_addrlen );
683 #endif
684 #if HAVE_SIN6_LEN_IN_SAI
685  CPPUNIT_ASSERT_EQUAL( ((struct sockaddr_in6*)expect->ai_addr)->sin6_len,
686  ((struct sockaddr_in6*)ipval->ai_addr)->sin6_len );
687  CPPUNIT_ASSERT_EQUAL( (socklen_t)((struct sockaddr_in6*)ipval->ai_addr)->sin6_len, ipval->ai_addrlen );
688 #endif
689 #if HAVE_SIN_LEN_IN_SAI
690  CPPUNIT_ASSERT_EQUAL( ((struct sockaddr_in*)expect->ai_addr)->sin_len,
691  ((struct sockaddr_in*)ipval->ai_addr)->sin_len );
692  CPPUNIT_ASSERT_EQUAL( (socklen_t)((struct sockaddr_in*)ipval->ai_addr)->sin_len, ipval->ai_addrlen );
693 #endif
694 
695  if (expect->ai_addrlen == sizeof(struct sockaddr_in)) {
696 //printf("FAMILY %d %d\n", ((struct sockaddr_in*)expect->ai_addr)->sin_family, ((struct sockaddr_in*)ipval->ai_addr)->sin_family);
697  CPPUNIT_ASSERT_EQUAL( ((struct sockaddr_in*)expect->ai_addr)->sin_family,
698  ((struct sockaddr_in*)ipval->ai_addr)->sin_family );
699 //printf("PORT %d %d\n", ((struct sockaddr_in*)expect->ai_addr)->sin_port, ((struct sockaddr_in*)ipval->ai_addr)->sin_port);
700  CPPUNIT_ASSERT_EQUAL( ((struct sockaddr_in*)expect->ai_addr)->sin_port,
701  ((struct sockaddr_in*)ipval->ai_addr)->sin_port );
702  }
703  if (expect->ai_addrlen == sizeof(struct sockaddr_in6)) {
704 //printf("FAMILY %d %d\n", ((struct sockaddr_in6*)expect->ai_addr)->sin6_family, ((struct sockaddr_in6*)ipval->ai_addr)->sin6_family);
705  CPPUNIT_ASSERT_EQUAL( ((struct sockaddr_in6*)expect->ai_addr)->sin6_family,
706  ((struct sockaddr_in6*)ipval->ai_addr)->sin6_family );
707 //printf("PORT %d %d\n", ((struct sockaddr_in6*)expect->ai_addr)->sin6_port, ((struct sockaddr_in6*)ipval->ai_addr)->sin6_port);
708  CPPUNIT_ASSERT_EQUAL( ((struct sockaddr_in6*)expect->ai_addr)->sin6_port,
709  ((struct sockaddr_in6*)ipval->ai_addr)->sin6_port );
710  }
711 
712  CPPUNIT_ASSERT( memcmp( expect->ai_addr, ipval->ai_addr, expect->ai_addrlen ) == 0 );
713 
714  freeaddrinfo(expect);
715  Ip::Address::FreeAddr(ipval);
716 }
717 
718 void
720 {
721  // Weird Bug: address set to empty during string conversion somewhere.
722  // initial string gets created and returned OK.
723  // but at the end of the process m_SocketAddr is left NULL'ed
724 
725  char ntoabuf[MAX_IPSTRLEN];
726  char hostbuf[MAX_IPSTRLEN];
727  char urlbuf[MAX_IPSTRLEN];
728 
729  struct in_addr outval;
730  struct in_addr expectval;
731 
732  expectval.s_addr = htonl(0xC0A8640C);
733 
734  Ip::Address anIPA = "192.168.100.12";
735 
736  /* test stored values */
737  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
738  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
739  CPPUNIT_ASSERT( anIPA.isIPv4() );
740  CPPUNIT_ASSERT( !anIPA.isIPv6() );
741  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
742  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
743  anIPA.getInAddr(outval);
744  CPPUNIT_ASSERT( memcmp( &expectval, &outval, sizeof(struct in_addr)) == 0 );
745 
746  /* POKE toStr display function to see what it is doing */
747  anIPA.toStr(ntoabuf,MAX_IPSTRLEN);
748  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
749  /* test stored values */
750  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
751  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
752  CPPUNIT_ASSERT( anIPA.isIPv4() );
753  CPPUNIT_ASSERT( !anIPA.isIPv6() );
754  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
755  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
756  anIPA.getInAddr(outval);
757  CPPUNIT_ASSERT( memcmp( &expectval, &outval, sizeof(struct in_addr)) == 0 );
758 
759  /* POKE toHostStr display function to see what it is doing */
760  anIPA.toHostStr(hostbuf,MAX_IPSTRLEN);
761  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
762  /* test stored values */
763  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
764  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
765  CPPUNIT_ASSERT( anIPA.isIPv4() );
766  CPPUNIT_ASSERT( !anIPA.isIPv6() );
767  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
768  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
769  anIPA.getInAddr(outval);
770  CPPUNIT_ASSERT( memcmp( &expectval, &outval, sizeof(struct in_addr)) == 0 );
771 
772  /* POKE toUrl display function to see what it is doing */
773  anIPA.toUrl(urlbuf,MAX_IPSTRLEN);
774  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
775  /* test stored values */
776  CPPUNIT_ASSERT( !anIPA.isAnyAddr() );
777  CPPUNIT_ASSERT( !anIPA.isNoAddr() );
778  CPPUNIT_ASSERT( anIPA.isIPv4() );
779  CPPUNIT_ASSERT( !anIPA.isIPv6() );
780  CPPUNIT_ASSERT_EQUAL( (unsigned short) 0, anIPA.port() );
781  CPPUNIT_ASSERT( !anIPA.isSockAddr() );
782  anIPA.getInAddr(outval);
783  CPPUNIT_ASSERT( memcmp( &expectval, &outval, sizeof(struct in_addr)) == 0 );
784 
785 }
786 
int cidr() const
Definition: Address.cc:44
int socklen_t
Definition: types.h:158
unsigned short port() const
Definition: Address.cc:778
int shutting_down
bool getInAddr(struct in_addr &) const
Definition: Address.cc:1030
void testCopyConstructor()
static void FreeAddr(struct addrinfo *&ai)
Definition: Address.cc:686
void testDefaults()
double current_dtime
bool isNoAddr() const
Definition: Address.cc:284
bool getReverseString(char buf[MAX_IPSTRLEN], int show_type=AF_UNSPEC) const
Definition: Address.cc:338
int applyMask(const Address &mask)
Definition: Address.cc:87
char * p
Definition: membanger.c:43
void testInAddr6Constructor()
char * toUrl(char *buf, unsigned int len) const
Definition: Address.cc:884
bool isIPv4() const
Definition: Address.cc:158
void freeaddrinfo()
void setNoAddr()
Definition: Address.cc:292
char * toStr(char *buf, const unsigned int blen, int force=AF_UNSPEC) const
Definition: Address.cc:802
void testSockAddrConstructor()
int getaddrinfo()
void testgetReverseString()
void testStringConstructor()
void testHostentConstructor()
bool isIPv6() const
Definition: Address.cc:164
void setEmpty()
Fast reset of the stored content to what would be after default constructor.
Definition: Address.cc:184
bool isAnyAddr() const
Definition: Address.cc:170
void getAddrInfo(struct addrinfo *&ai, int force=AF_UNSPEC) const
Definition: Address.cc:599
void testtoUrl_fromSockAddr()
void testSockAddr6Constructor()
CPPUNIT_TEST_SUITE_REGISTRATION(testIpAddress)
void const char * buf
Definition: stub_helper.cc:16
void testBugNullingDisplay()
#define AI_NUMERICHOST
bool isSockAddr() const
Definition: Address.cc:152
time_t squid_curtime
void testtoUrl_fromInAddr()
socklen_t ai_addrlen
void setAnyAddr()
NOTE: Does NOT clear the Port stored. Ony the Address and Type.
Definition: Address.cc:177
struct sockaddr * ai_addr
struct hostent * gethostbyname()
void getSockAddr(struct sockaddr_storage &addr, const int family) const
Definition: Address.cc:934
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:23
int matchIPAddr(const Address &rhs) const
Definition: Address.cc:703
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:852
struct timeval current_time
#define NULL
Definition: types.h:166
void testInAddrConstructor()

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors