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
));
360 MacAddress( const char *inaddr);
361 MacAddress( const MacAddress &macaddr);
362 MacAddress( const GenAddress &genaddr);
364 * SmiUINT32 get_syntax();
365 * SnmpSyntax& operator=( SnmpSyntax &val);
366 MacAddress& operator=( const MacAddress &macaddress);
367 * SnmpSyntax *clone() const;
368 virtual char *to_string();
369 virtual operator const char *() const;
370 virtual addr_type get_type() const;
371 unsigned int hashFunction() const;
373 // MAC address format
378 // Valid input format
381 // Total length must be 17
382 // Each char must take on value 0-F
387 static void TestMacAddress()
390 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress: Tests\n"));
393 ACE_ASSERT(ma1
.valid() == 0);
394 ACE_ASSERT(ma1
.get_type() == type_mac
);
395 MacAddress
ma2("01:23:45:67:89:AB");
396 ACE_ASSERT(ma2
.valid() == 1);
397 ACE_ASSERT(ma2
.get_type() == type_mac
);
398 MacAddress
ma3("0123456789ABCEFGHI"); // invalid string
399 ACE_ASSERT(ma3
.valid() == 0);
400 ACE_ASSERT(ma3
.get_type() == type_mac
);
401 GenAddress
ga("01:23:45:67:89:AB"); // mac address
403 ACE_ASSERT(ma4
.valid() == 1);
404 ACE_ASSERT(ma4
.get_type() == type_mac
);
406 ACE_ASSERT(ma5
.valid() == 0);
407 ACE_ASSERT(ma5
.get_type() == type_mac
);
411 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma1(\"01:23:45:67:89:AB\") [%s]\n",
413 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma2(\"01:23:45:67:89:AB\") [%s]\n",
415 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma3(\"\") [%s]\n",
417 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma4(\"01:23:45:67:89:AB\") [%s]\n",
419 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma5(\"\") [%s]\n",
421 const char * ptr
= (const char *)ma5
;
422 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma5(\"\") [%s]\n",
425 // hashFunction crashes if not usedwith valid fn
426 int x
= ma2
.hashFunction();
428 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) MacAddress:ma2.hashFunction(\"483201\") [%d]\n",
434 UdpAddress( const char *inaddr);
435 UdpAddress( const UdpAddress &udpaddr);
436 UdpAddress( const GenAddress &genaddr);
437 UdpAddress( const IpAddress &ipaddr);
439 SmiUINT32 get_syntax();
440 SnmpSyntax& operator=( SnmpSyntax &val);
441 UdpAddress& operator=( const UdpAddress &udpaddr);
442 SnmpSyntax *clone() const;
443 virtual char *to_string() ;
444 virtual operator const char *() const;
445 void set_port( const unsigned short p);
446 unsigned short get_port() const;
447 virtual addr_type get_type() const;
449 // look for port info @ the end of the string
450 // port can be delineated by a ':' or a '/'
451 // if neither are present then just treat it
452 // like a normal IpAddress
456 static void TestUdpAddress()
458 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress: Tests\n"));
461 ACE_ASSERT(ua1
.valid() == 0);
462 ACE_ASSERT(ua1
.get_type() == type_udp
);
464 // semantics of not setting the port here are bizzare?
465 UdpAddress
ua2("127.0.0.1:161");
466 ACE_ASSERT(ua2
.valid() == 1);
467 ACE_ASSERT(ua2
.get_type() == type_udp
);
470 ACE_ASSERT(ua3
.valid() == 1);
471 ACE_ASSERT(ua3
.get_type() == type_udp
);
473 GenAddress
ga("localhost");
475 ACE_ASSERT(ua4
.valid() == 1);
476 ACE_ASSERT(ua4
.get_type() == type_udp
);
478 IpAddress
ia("localhost");
480 ACE_ASSERT(ua5
.valid() == 1);
481 ACE_ASSERT(ua5
.get_type() == type_udp
);
483 UdpAddress
*ua6
= new UdpAddress("localhost:161");
484 ACE_ASSERT(ua6
->valid() == 1);
485 ACE_ASSERT(ua6
->get_type() == type_udp
);
487 UdpAddress ua7
= UdpAddress("localhost/162");
488 ACE_ASSERT(ua7
.valid() == 1);
489 ACE_ASSERT(ua7
.get_type() == type_udp
);
492 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua1(\"\") [%s]\n",
494 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua2(\"127.0.0.1:161\") [%s]\n",
496 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua3(ua2)(\"127.0.0.1:161\") [%s]\n",
498 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua4(GenAddress)(\"127.0.0.1\") [%s]\n",
500 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua5(IpAddress)(\"127.0.0.1\") [%s]\n",
502 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua6()(\"localhost:161\") [%s]\n",
504 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua7()(\"localhost/162\") [%s]\n",
507 delete ua6
; // destructor
509 // assignement tests.
511 ACE_ASSERT(ua1
== ua2
);
512 ACE_ASSERT(ua1
.valid() == 1);
514 ACE_ASSERT(ua1
== ua1
);
515 ACE_ASSERT(ua1
.valid() == 1);
519 ACE_ASSERT(ua1
.get_port() == 333);
520 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) UdpAddress:ua1.set_port()(\"333\") [%s]\n",
526 ACE_TMAIN (int, ACE_TCHAR
*[])
528 ACE_START_TEST (ACE_TEXT ("Address_Test"));
534 TestNetbiosAddress();