2 //=============================================================================
4 * @file Address_Test.cpp
6 * Test all the member functions of the Address family:
7 * GenAddress, MacAddress, IpxAddress, IpAddress, UdpAddress
8 * all which derive from abstract base class Address.
10 * @author Michael R. MacFaden <mrm@cisco.com>
12 //=============================================================================
14 /*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
15 Copyright 1997 Cisco Systems, Inc.
17 Permission to use, copy, modify, and distribute this software for any
18 purpose and without fee is hereby granted, provided that this
19 copyright and permission notice appear on all copies of the software and
20 supporting documentation, the name of Cisco Systems, Inc. not be used
21 in advertising or publicity pertaining to distribution of the
22 program without specific prior permission, and notice be given
23 in supporting documentation that modification, copying and distribution is by
24 permission of Cisco Systems, Inc.
26 Cisco Systems, Inc. makes no representations about the suitability of this
27 software for any purpose. THIS SOFTWARE IS PROVIDED ``AS IS''
28 AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
29 LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGMENT AND
30 FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL CISCO SYSTEMS, INC. BE
31 LIABLE FOR ANY DAMAGES ARISING OUT OF THIS LICENSE OR YOUR USE OF THE
32 SOFTWARE INCLUDING WITHOUT LIMITATION, DIRECT, INDIRECT OR CONSEQUENTIAL
34 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
36 #include "ace/OS_main.h"
37 #include "ace/OS_NS_string.h"
38 #include "asnmp/address.h"
39 #include "test_config.h"
41 // test the GenAddress interface
42 static void TestGenAddr()
44 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress: Tests: var(expected) [actual]\n"));
46 // the constructors and destructors
47 GenAddress
*ga1
= new GenAddress("1.2.3.4"); // use ipv4 address
48 ACE_ASSERT(ga1
->valid() == 1);
49 GenAddress
ga2(*ga1
); // copy constructor;
50 ACE_ASSERT(ga2
.valid() == 1);
51 GenAddress
ga3("localhost");
52 ACE_ASSERT(ga3
.valid() == 1);
53 GenAddress ga4
; // default constructor
54 ACE_ASSERT(ga4
.valid() == 0);
55 GenAddress ga5
; // default constructor
56 ACE_ASSERT(ga5
.valid() == 0);
57 GenAddress
ga6("127.0.0.1:7"); // udp address
58 ACE_ASSERT(ga6
.valid() == 1);
59 GenAddress
ga7("01234567.89ABcDeF0123"); // ipx address
60 ACE_ASSERT(ga7
.valid() == 1);
61 IpAddress
ip("1.2.3.4");
62 ACE_ASSERT(ip
.valid() == 1);
63 GenAddress
ga8(ip
); // Address conversion
64 ACE_ASSERT(ga8
.valid() == 1);
66 const char *ptr
= (const char *)ga8
;
69 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga1(\"1.2.3.4\") [%s]\n",
71 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga2(ga1) [%s]\n",
73 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga3(\"localhost\") [%s]\n",
75 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga4(\"\") [%s]\n",
77 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga6(\"127.0.0.1:7\") [%s]\n",
80 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga7(\"01234567.89ABcDeF0123\") [%s]\n",
82 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga8(\"1.2.3.4\") [%s]\n",
84 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga8(\"1.2.3.4\") [%s]\n",
87 // Test Assignment x = y, y = x
88 ga5
= ga3
; // regular assignment
89 ga3
= ga3
; // self assignment
90 ACE_ASSERT(ga5
== ga3
);
92 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga5=ga3(\"localhost\") [%s]\n",
95 addr_type a
= ga2
.get_type();
96 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga2.get_type(\"0\") [%d]\n",
100 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga2.get_type(\"2\") [%d]\n",
103 // udp address string
105 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) GenAddress:ga2.get_type(\"1\") [%d]\n",
112 IpAddress( const char *inaddr = "");
113 IpAddress( const IpAddress &ipaddr);
114 IpAddress( const GenAddress &genaddr);
116 SnmpSyntax& operator=( SnmpSyntax &val);
117 IpAddress& operator=( const IpAddress &ipaddress);
118 SnmpSyntax *clone() const;
119 char *resolve_hostname(int &status);
120 virtual char *to_string() ;
121 virtual operator const char *() const;
122 void mask( const IpAddress& ipaddr);
123 virtual addr_type get_type() const;
124 virtual SmiUINT32 get_syntax();
125 int is_loopback() const;
126 int is_multicast() const;
127 int is_broadcast() const;
128 int is_arpanet() const;
130 static void TestIpAddress()
132 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpAddress: Tests\n"));
135 GenAddress
ga("255.255.255.255");
137 IpAddress
ia2("224.2.3.4");
138 ACE_ASSERT(ia2
.is_multicast());
139 ACE_ASSERT(!ia2
.is_loopback());
140 IpAddress
ia3("localhost");
141 ACE_ASSERT(ia3
.is_loopback());
142 ACE_ASSERT(!ia3
.is_multicast());
144 ACE_ASSERT(ia4
.is_loopback());
145 ACE_ASSERT(!ia4
.is_multicast());
147 ACE_ASSERT(ia5
.is_broadcast());
148 IpAddress ia6
= IpAddress("10.0.0.2");
149 ACE_ASSERT(ia6
.is_private());
150 ACE_ASSERT(!ia6
.is_multicast());
151 ACE_ASSERT(!ia6
.is_loopback());
153 IpAddress
ia7("172.16.0.1");
154 ACE_ASSERT(ia7
.is_private());
156 IpAddress
ia8("192.168.0.1");
157 ACE_ASSERT(ia8
.is_private());
159 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpAddress:ia1(\"\") [%s]\n",
161 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpAddress:ia2(\"1.2.3.4\") [%s]\n",
163 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpAddress:ia3(\"127.0.0.1\") [%s]\n",
165 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpAddress:ia4(\"ia3\") [%s]\n",
167 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpAddress:ia5(\"255.255.255.255\") [%s]\n",
172 const char *ptr
= ia5
.resolve_hostname(status
);
173 ACE_ASSERT(status
== 0);
174 ACE_ASSERT(ptr
!= 0);
175 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpAddress:ia5.resolve_hostname():(\"\") [%s]\n",
178 // now lets try one we setup with a hostname
179 ptr
= ia3
.resolve_hostname(status
);
180 ACE_ASSERT(status
== 0);
181 ACE_ASSERT(ptr
!= 0);
182 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpAddress:ia3.resolve_hostname()(\"localhost\") [%s]\n",
185 ptr
= (const char *)ia5
;
186 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpAddress:(const char *)(\"255.255.255.255\") [%s]\n",
190 ACE_ASSERT(ia2
== ia3
);
192 ia4
.mask(ia3
); // mask with equal value should return same
193 ACE_ASSERT(ia2
== ia3
);
195 ACE_ASSERT(ia1
.get_type() == type_ip
);
196 ACE_ASSERT(ia1
.valid() == 0);
197 ACE_ASSERT(ia2
.get_type() == type_ip
);
198 ACE_ASSERT(ia2
.valid() == 1);
199 ACE_ASSERT(ia3
.get_type() == type_ip
);
200 ACE_ASSERT(ia3
.valid() == 1);
201 ACE_ASSERT(ia4
.get_type() == type_ip
);
202 ACE_ASSERT(ia4
.valid() == 1);
203 ACE_ASSERT(ia5
.get_type() == type_ip
);
204 ACE_ASSERT(ia5
.valid() == 1);
208 // --------------- Netbios ---------------
210 NetbiosAddress( const char *inaddr = "");
211 NetbiosAddress( const NetbiosAddress& nbaddr);
212 NetbiosAddress( const GenAddress& genaddr);
214 virtual char *to_string();
215 NetbiosAddress& operator=( const NetbiosAddress &nbaddr);
216 nb_service get_service_type() const;
217 void set_service_type(nb_service nbservice);
218 virtual operator const char *() const;
219 virtual SmiUINT32 get_syntax();
220 SnmpSyntax& operator=( SnmpSyntax &val);
221 SnmpSyntax *clone() const;
223 static void TestNetbiosAddress()
226 ACE_ASSERT(n1
.valid() == 0);
227 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) NetbiosAddress:n1(\"\") [%s]\n",
230 NetbiosAddress
n2(n1
);
231 ACE_ASSERT(n2
.valid() == 0);
232 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) NetbiosAddress:n2(n1) [%s]\n",
235 NetbiosAddress
n3("pcname");
236 ACE_ASSERT(n3
.valid() == 1);
237 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) NetbiosAddress:n3(\"pcname\") [%s]\n",
240 NetbiosAddress
n4("abcdefghigjklmn");
241 n4
.set_service_type(nb_workstation
);
242 ACE_ASSERT(n4
.valid() == 1);
243 ACE_ASSERT(n4
.get_service_type() == nb_workstation
);
244 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) NetbiosAddress:n4(\"abcdefghigjklmn\") [%s]\n",
247 NetbiosAddress
n5("abcdefghigjklmno0xx");
248 ACE_ASSERT(n5
.valid() == 0);
249 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) NetbiosAddress:n4(\"abcdefghigjklmno0xx\") [%s]\n",
253 ACE_ASSERT(n1
== n4
);
254 ACE_ASSERT(ACE_OS::strcmp((const char *)n1
, (const char *)n4
) == 0);
256 n1
.set_service_type(nb_server
);
257 ACE_ASSERT(nb_server
== n1
.get_service_type());
260 // --------------- IPX ---------------
263 IpxAddress( const char *inaddr);
264 IpxAddress( const IpxAddress &ipxaddr);
265 IpxAddress( const GenAddress &genaddr);
267 virtual SmiUINT32 get_syntax();
268 SnmpSyntax& operator=( SnmpSyntax &val);
269 IpxAddress& operator=( const IpxAddress &ipxaddress);
270 int get_hostid( MacAddress& mac);
271 SnmpSyntax *clone() const;
272 virtual operator const char *() const;
273 virtual addr_type get_type() const;
275 Ipx Address semantics: Total length must be 21
276 // Must have a separator in it
277 // First string length must be 8
278 // Second string length must be 12
279 // Each char must take on value 0-F
281 // Input formats recognized
283 // XXXXXXXX.XXXXXXXXXXXX
284 // XXXXXXXX:XXXXXXXXXXXX
285 // XXXXXXXX-XXXXXXXXXXXX
286 // XXXXXXXX.XXXXXX-XXXXXX
287 // XXXXXXXX:XXXXXX-XXXXXX
288 // XXXXXXXX-XXXXXX-XXXXXX
292 static void TestIpxAddress()
294 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress: Tests\n"));
296 ACE_ASSERT(xa1
.valid() == 0);
297 ACE_ASSERT(xa1
.get_type() == type_ipx
);
298 GenAddress
gen("01234567.0123456789AB");
299 ACE_ASSERT(gen
.valid() == 1);
300 IpxAddress
xa2("01234567.0123456789AB");
301 ACE_ASSERT(xa2
.get_type() == type_ipx
);
302 ACE_ASSERT(xa2
.valid() == 1);
303 IpxAddress
xa3("01234567:0123456789AB");
304 ACE_ASSERT(xa3
.get_type() == type_ipx
);
305 ACE_ASSERT(xa3
.valid() == 1);
306 IpxAddress
xa4("01234567-0123456789AB");
307 ACE_ASSERT(xa4
.get_type() == type_ipx
);
308 ACE_ASSERT(xa4
.valid() == 1);
309 IpxAddress
xa5("01234567.012345-6789AB");
310 ACE_ASSERT(xa5
.get_type() == type_ipx
);
311 ACE_ASSERT(xa5
.valid() == 1);
312 IpxAddress
xa6("01234567:012345-6789AB");
313 ACE_ASSERT(xa6
.get_type() == type_ipx
);
314 ACE_ASSERT(xa6
.valid() == 1);
315 IpxAddress
xa7("01234567-012345-6789AB");
316 ACE_ASSERT(xa7
.get_type() == type_ipx
);
317 ACE_ASSERT(xa7
.valid() == 1);
318 IpxAddress
xa8("01234567.");
319 ACE_ASSERT(xa8
.get_type() == type_ipx
);
320 ACE_ASSERT(xa8
.valid() == 0);
322 ACE_ASSERT(xa9
.valid() == 1);
323 IpxAddress
*xa10
= new IpxAddress(xa9
);
324 ACE_ASSERT(xa10
->get_type() == type_ipx
);
325 ACE_ASSERT(xa10
->valid() == 1);
328 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress:xa1(\"\") [%s]\n",
330 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress:xa2(\"01234567.0123456789AB\") [%s]\n",
332 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress:xa3(\"01234567:0123456789A\") [%s]\n",
334 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress:xa4(\"01234567-0123456789AB\") [%s]\n",
337 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress:xa5(\"01234567.012345-6789AB\") [%s]\n",
339 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress:xa6(\"01234567:012345-6789AB\") [%s]\n",
341 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress:xa7(\"01234567-012345-6789AB\") [%s]\n",
346 ACE_ASSERT(xa1
== xa3
);
348 ACE_ASSERT(xa4
.get_hostid(mac
) == 1);
349 ACE_ASSERT(mac
.valid() == 1);
350 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress:xa4:get_hostid(\"01:23:45:67:89:ab\") [%s]\n", mac
.to_string()));
352 const char *ptr
= (const char *)xa7
;
353 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) IpxAddress:xa7-ptr(\"01234567-012345-6789AB\") [%s]\n", ptr
));
359 MacAddress( const char *inaddr);
360 MacAddress( const MacAddress &macaddr);
361 MacAddress( const GenAddress &genaddr);
363 * SmiUINT32 get_syntax();
364 * SnmpSyntax& operator=( SnmpSyntax &val);
365 MacAddress& operator=( const MacAddress &macaddress);
366 * SnmpSyntax *clone() const;
367 virtual char *to_string();
368 virtual operator const char *() const;
369 virtual addr_type get_type() const;
370 unsigned int hashFunction() const;
372 // MAC address format
377 // Valid input format
380 // Total length must be 17
381 // Each char must take on value 0-F
386 static void TestMacAddress()
388 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress: Tests\n"));
391 ACE_ASSERT(ma1
.valid() == 0);
392 ACE_ASSERT(ma1
.get_type() == type_mac
);
393 MacAddress
ma2("01:23:45:67:89:AB");
394 ACE_ASSERT(ma2
.valid() == 1);
395 ACE_ASSERT(ma2
.get_type() == type_mac
);
396 MacAddress
ma3("0123456789ABCEFGHI"); // invalid string
397 ACE_ASSERT(ma3
.valid() == 0);
398 ACE_ASSERT(ma3
.get_type() == type_mac
);
399 GenAddress
ga("01:23:45:67:89:AB"); // mac address
401 ACE_ASSERT(ma4
.valid() == 1);
402 ACE_ASSERT(ma4
.get_type() == type_mac
);
404 ACE_ASSERT(ma5
.valid() == 0);
405 ACE_ASSERT(ma5
.get_type() == type_mac
);
409 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma1(\"01:23:45:67:89:AB\") [%s]\n",
411 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma2(\"01:23:45:67:89:AB\") [%s]\n",
413 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma3(\"\") [%s]\n",
415 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma4(\"01:23:45:67:89:AB\") [%s]\n",
417 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma5(\"\") [%s]\n",
419 const char * ptr
= (const char *)ma5
;
420 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma5(\"\") [%s]\n",
423 // hashFunction crashes if not usedwith valid fn
424 int x
= ma2
.hashFunction();
426 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma2.hashFunction(\"483201\") [%d]\n",
432 UdpAddress( const char *inaddr);
433 UdpAddress( const UdpAddress &udpaddr);
434 UdpAddress( const GenAddress &genaddr);
435 UdpAddress( const IpAddress &ipaddr);
437 SmiUINT32 get_syntax();
438 SnmpSyntax& operator=( SnmpSyntax &val);
439 UdpAddress& operator=( const UdpAddress &udpaddr);
440 SnmpSyntax *clone() const;
441 virtual char *to_string() ;
442 virtual operator const char *() const;
443 void set_port( const unsigned short p);
444 unsigned short get_port() const;
445 virtual addr_type get_type() const;
447 // look for port info @ the end of the string
448 // port can be delineated by a ':' or a '/'
449 // if neither are present then just treat it
450 // like a normal IpAddress
454 static void TestUdpAddress()
456 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress: Tests\n"));
459 ACE_ASSERT(ua1
.valid() == 0);
460 ACE_ASSERT(ua1
.get_type() == type_udp
);
462 // semantics of not setting the port here are bizzare?
463 UdpAddress
ua2("127.0.0.1:161");
464 ACE_ASSERT(ua2
.valid() == 1);
465 ACE_ASSERT(ua2
.get_type() == type_udp
);
468 ACE_ASSERT(ua3
.valid() == 1);
469 ACE_ASSERT(ua3
.get_type() == type_udp
);
471 GenAddress
ga("localhost");
473 ACE_ASSERT(ua4
.valid() == 1);
474 ACE_ASSERT(ua4
.get_type() == type_udp
);
476 IpAddress
ia("localhost");
478 ACE_ASSERT(ua5
.valid() == 1);
479 ACE_ASSERT(ua5
.get_type() == type_udp
);
481 UdpAddress
*ua6
= new UdpAddress("localhost:161");
482 ACE_ASSERT(ua6
->valid() == 1);
483 ACE_ASSERT(ua6
->get_type() == type_udp
);
485 UdpAddress ua7
= UdpAddress("localhost/162");
486 ACE_ASSERT(ua7
.valid() == 1);
487 ACE_ASSERT(ua7
.get_type() == type_udp
);
490 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua1(\"\") [%s]\n",
492 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua2(\"127.0.0.1:161\") [%s]\n",
494 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua3(ua2)(\"127.0.0.1:161\") [%s]\n",
496 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua4(GenAddress)(\"127.0.0.1\") [%s]\n",
498 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua5(IpAddress)(\"127.0.0.1\") [%s]\n",
500 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua6()(\"localhost:161\") [%s]\n",
502 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua7()(\"localhost/162\") [%s]\n",
505 delete ua6
; // destructor
507 // assignement tests.
509 ACE_ASSERT(ua1
== ua2
);
510 ACE_ASSERT(ua1
.valid() == 1);
512 ACE_ASSERT(ua1
== ua1
);
513 ACE_ASSERT(ua1
.valid() == 1);
517 ACE_ASSERT(ua1
.get_port() == 333);
518 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua1.set_port()(\"333\") [%s]\n",
523 ACE_TMAIN (int, ACE_TCHAR
*[])
525 ACE_START_TEST (ACE_TEXT ("Address_Test"));
531 TestNetbiosAddress();