2 //=============================================================================
4 * @file Multihomed_INET_Addr_Test.cpp
6 * Performs several tests on the Multihomed_ACE_INET_Addr class.
7 * It creates several IPv4 addresses and checks that the
8 * address formed by the class is valid.
10 * @author Edward Mulholland (emulholl@atl.lmco.com)
12 //=============================================================================
15 #include "test_config.h"
16 #include "ace/OS_NS_string.h"
17 #include "ace/Multihomed_INET_Addr.h"
18 #include "ace/Log_Msg.h"
19 #include "ace/OS_NS_arpa_inet.h"
21 int run_main (int, ACE_TCHAR
*[])
23 ACE_START_TEST (ACE_TEXT ("Multihomed_INET_Addr_Test"));
25 int status
= 0; // Innocent until proven guilty
31 // The port will always be this
34 // The primary address will always be this
35 const char *primary_dotted_decimal
= "138.38.180.251";
37 // The secondary addresses will always be these...
38 const char *secondary_dotted_decimals
[] = {
46 // ... and as you can see, there are 5 of them
47 const size_t num_secondaries
= 5;
49 // We also need the primary address and the secondary addresses
50 // in ACE_UINT32 format in host byte order
51 ACE_UINT32 primary_addr32
;
52 ACE_UINT32 secondary_addr32
[5];
55 struct in_addr addrv4
;
56 ACE_OS::memset ((void *) &addrv4
, 0, sizeof addrv4
);
57 ACE_OS::inet_pton (AF_INET
, primary_dotted_decimal
, &addrv4
);
58 ACE_OS::memcpy (&primary_addr32
, &addrv4
, sizeof (primary_addr32
));
59 primary_addr32
= ACE_NTOHL(primary_addr32
);
62 for (i
= 0; i
< num_secondaries
; ++i
) {
63 struct in_addr addrv4
;
64 ACE_OS::memset ((void *) &addrv4
, 0, sizeof addrv4
);
65 ACE_OS::inet_pton (AF_INET
, secondary_dotted_decimals
[i
], &addrv4
);
66 ACE_OS::memcpy (&secondary_addr32
[i
], &addrv4
, sizeof (primary_addr32
));
67 secondary_addr32
[i
] = ACE_NTOHL(secondary_addr32
[i
]);
71 ACE_Multihomed_INET_Addr addr
;
73 // Array of ones (used to clear the secondary addresses of the test
75 ACE_UINT32 array_of_threes
[5] = { ACE_UINT32 (3),
81 // Array of INET_Addrs that will repeatedly be passed into the
82 // get_secondary_addresses accessor of Multihomed_INET_Addr
83 ACE_INET_Addr in_out
[5];
85 // Array of INET_Addrs against which the above array will be tested.
86 ACE_INET_Addr stay_out
[5];
88 // Array of sockaddrs that will repeatedly be passed into the
89 // get_addresses accessor of Multihomed_INET_Addr
90 const size_t num_sockaddrs
= 6;
91 sockaddr_in in_out_sockaddr
[num_sockaddrs
];
93 // Run the test with a varying number of secondary addresses
94 for (i
= 0; i
<= num_secondaries
; ++i
) {
95 /****** Clear the in_out array and test subject ******/
98 // Clear the in_out array by setting every port to 0 and every
100 for (j
= 0; j
< num_secondaries
; ++j
) {
101 in_out
[j
].set(0, ACE_UINT32 (1), 1);
104 // Clear the in_out_sockaddr array by setting every port to 0 and
105 // every address to 1
106 ACE_OS::memset(in_out_sockaddr
, 0, num_sockaddrs
* sizeof(sockaddr
));
108 // Clear the test subject by setting the port to 2 and every
109 // address (both the primary and the secondaries) to 3
110 addr
.set (2, ACE_UINT32 (3), 1, array_of_threes
, num_secondaries
);
112 // Check that the port is 2
113 if (addr
.get_port_number() != 2) {
114 ACE_ERROR ((LM_ERROR
,
115 ACE_TEXT ("Failed get_port_number check\n")
116 ACE_TEXT ("%d != %d\n"),
117 addr
.get_port_number(),
122 // Check that the primary address is 3
123 if (addr
.get_ip_address() != ACE_UINT32 (3)) {
124 ACE_ERROR ((LM_ERROR
,
125 ACE_TEXT ("Failed get_ip_address check\n")
126 ACE_TEXT ("0x%x != 0x%x\n"),
127 addr
.get_ip_address(),
132 // Check that the test subject reports the correct number of
133 // secondary addresses.
134 size_t returned_num_secondaries
= addr
.get_num_secondary_addresses();
135 if (returned_num_secondaries
== num_secondaries
) {
136 // Set a stay_out element to the state that we expect to see
137 // from every in_out element after the in_out array is passed to
138 // the accessor of the test subject.
139 stay_out
[0].set(2, ACE_UINT32 (3), 1);
141 // Pass the in_out array to the accessor
142 addr
.get_secondary_addresses(in_out
, num_secondaries
);
144 // Check that the in_out array matches stay_out element
145 for (j
= 0; j
< num_secondaries
; ++j
) {
146 if (in_out
[j
] != stay_out
[0]) {
147 ACE_TCHAR in_out_string
[100];
148 ACE_TCHAR stay_out_string
[100];
150 in_out
[j
].addr_to_string(in_out_string
, 100);
151 stay_out
[0].addr_to_string(stay_out_string
, 100);
153 ACE_ERROR ((LM_ERROR
,
154 ACE_TEXT ("Failed get_secondary_addresses check\n")
155 ACE_TEXT ("%s != %s\n"),
163 // Pass the in_out_sockaddr array to the accessor
164 addr
.get_addresses(in_out_sockaddr
, num_secondaries
+ 1);
166 // Check that the in_out_sockaddr array matches stay_out element
167 for (j
= 0, pointer
= in_out_sockaddr
;
168 j
< num_secondaries
+ 1;
170 if (ACE_OS::memcmp(pointer
, stay_out
[0].get_addr(), sizeof(sockaddr
))) {
171 ACE_ERROR ((LM_ERROR
,
172 ACE_TEXT ("Failed get_addresses check\n")));
179 ACE_ERROR ((LM_ERROR
,
180 ACE_TEXT ("Failed get_num_secondary_addresses check\n")
181 ACE_TEXT ("%d != %d\n"),
182 returned_num_secondaries
,
188 /**** Test set (u_short, const char[], int, int, const char *([]), size_t) ****/
192 primary_dotted_decimal
,
195 secondary_dotted_decimals
,
198 // Check the port number
199 if (addr
.get_port_number() != port
) {
200 ACE_ERROR ((LM_ERROR
,
201 ACE_TEXT ("Failed second get_port_number check\n")
202 ACE_TEXT ("%d != %d\n"),
203 addr
.get_port_number(),
208 // Check the primary address
209 if (0 != ACE_OS::strcmp (addr
.get_host_addr(), primary_dotted_decimal
))
211 ACE_ERROR ((LM_ERROR
,
212 ACE_TEXT ("%C failed get_host_addr() check\n")
213 ACE_TEXT ("%C != %C\n"),
214 primary_dotted_decimal
,
215 addr
.get_host_addr (),
216 primary_dotted_decimal
));
220 // Check that the test subject reports the correct number of
221 // secondary addresses.
222 returned_num_secondaries
= addr
.get_num_secondary_addresses();
223 if (returned_num_secondaries
== i
) {
224 // Initialize the stay_out array with the secondary addresses
225 for (j
= 0; j
< i
; ++j
) {
226 stay_out
[j
].set(port
, secondary_dotted_decimals
[j
]);
229 // Pass the in_out array to the accessor
230 addr
.get_secondary_addresses(in_out
, i
);
232 // Check that the in_out array matches stay_out array
233 for (j
= 0; j
< i
; ++j
) {
234 if (in_out
[j
] != stay_out
[j
]) {
235 ACE_TCHAR in_out_string
[100];
236 ACE_TCHAR stay_out_string
[100];
238 in_out
[j
].addr_to_string(in_out_string
, 100);
239 stay_out
[j
].addr_to_string(stay_out_string
, 100);
241 ACE_ERROR ((LM_ERROR
,
242 ACE_TEXT ("Failed second get_secondary_addresses check\n")
243 ACE_TEXT ("%s != %s\n"),
251 // Pass the in_out_sockaddr array to the accessor
252 addr
.get_addresses(in_out_sockaddr
, i
+ 1);
254 // Check that the primary address in the in_out_sockaddr array
255 // matches the primary address reported by the superclass
256 if (ACE_OS::memcmp(in_out_sockaddr
, addr
.get_addr(), sizeof(sockaddr
))) {
257 ACE_ERROR ((LM_ERROR
,
258 ACE_TEXT ("Failed second get_addresses check ")
259 ACE_TEXT ("(for primary address)\n")));
264 // Check that the secondary addresses in the in_out_sockaddr
265 // array match the stay_out array
266 for (j
= 1, pointer
= &in_out_sockaddr
[1];
269 if (ACE_OS::memcmp(pointer
, stay_out
[j
-1].get_addr(), sizeof(sockaddr
))) {
270 ACE_ERROR ((LM_ERROR
,
271 ACE_TEXT ("Failed second get_addresses check ")
272 ACE_TEXT ("(for secondary addresses)\n")));
279 ACE_ERROR ((LM_ERROR
,
280 ACE_TEXT ("Failed second get_num_secondary_addresses check\n")
281 ACE_TEXT ("%d != %d\n"),
282 returned_num_secondaries
,
288 /****** Clear the in_out array and test subject AGAIN ******/
291 // Clear the in_out array by setting every port to 0 and every
293 for (j
= 0; j
< num_secondaries
; ++j
) {
294 in_out
[j
].set(0, ACE_UINT32 (1), 1);
297 // Clear the test subject by setting the port to 2 and every
298 // address (both the primary and the secondaries) to 3
299 addr
.set (2, ACE_UINT32 (3), 1, array_of_threes
, num_secondaries
);
301 // Check that the port is 2
302 if (addr
.get_port_number() != 2) {
303 ACE_ERROR ((LM_ERROR
,
304 ACE_TEXT ("Failed third get_port_number check\n")
305 ACE_TEXT ("%d != %d\n"),
306 addr
.get_port_number(),
311 // Check that the primary address is 3
312 if (addr
.get_ip_address() != ACE_UINT32 (3)) {
313 ACE_ERROR ((LM_ERROR
,
314 ACE_TEXT ("Failed third get_ip_address check\n")
315 ACE_TEXT ("0x%x != 0x%x\n"),
316 addr
.get_ip_address(),
321 // Check that the test subject reports the correct number of
322 // secondary addresses.
323 returned_num_secondaries
= addr
.get_num_secondary_addresses();
324 if (returned_num_secondaries
== num_secondaries
) {
325 // Set a stay_out element to the state that we expect to see
326 // from every in_out element after the in_out array is passed to
327 // the accessor of the test subject.
328 stay_out
[0].set(2, ACE_UINT32 (3), 1);
330 // Pass the in_out array to the accessor
331 addr
.get_secondary_addresses(in_out
, num_secondaries
);
333 // Check that the in_out array matches stay_out array
334 for (j
= 0; j
< num_secondaries
; ++j
) {
335 if (in_out
[j
] != stay_out
[0]) {
336 ACE_TCHAR in_out_string
[100];
337 ACE_TCHAR stay_out_string
[100];
339 in_out
[j
].addr_to_string(in_out_string
, 100);
340 stay_out
[0].addr_to_string(stay_out_string
, 100);
342 ACE_ERROR ((LM_ERROR
,
343 ACE_TEXT ("Failed third get_secondary_addresses check\n")
344 ACE_TEXT ("%s != %s\n"),
353 ACE_ERROR ((LM_ERROR
,
354 ACE_TEXT ("Failed third get_num_secondary_addresses check\n")
355 ACE_TEXT ("%d != %d\n"),
356 returned_num_secondaries
,
362 /**** Test set (u_short, ACE_UINT32, int, const ACE_UINT32 *, size_t) ****/
370 // Check the port number
371 if (addr
.get_port_number() != port
) {
372 ACE_ERROR ((LM_ERROR
,
373 ACE_TEXT ("Failed forth get_port_number check\n")
374 ACE_TEXT ("%d != %d\n"),
375 addr
.get_port_number(),
380 // Check the primary address
381 if (0 != ACE_OS::strcmp (addr
.get_host_addr(), primary_dotted_decimal
))
383 ACE_ERROR ((LM_ERROR
,
384 ACE_TEXT ("%C failed second get_ip_address() check\n")
385 ACE_TEXT ("%C != %C\n"),
386 primary_dotted_decimal
,
387 addr
.get_host_addr (),
388 primary_dotted_decimal
));
392 // Check that the test subject reports the correct number of
393 // secondary addresses.
394 returned_num_secondaries
= addr
.get_num_secondary_addresses();
395 if (returned_num_secondaries
== i
) {
396 // Initialize the stay_out array with the secondary addresses
397 for (j
= 0; j
< i
; ++j
) {
398 stay_out
[j
].set(port
, secondary_addr32
[j
]);
401 // Pass the in_out array to the accessor
402 addr
.get_secondary_addresses(in_out
, j
);
404 // Check that the in_out array matches stay_out array
405 for (j
= 0; j
< i
; ++j
) {
406 if (in_out
[j
] != stay_out
[j
]) {
407 ACE_TCHAR in_out_string
[100];
408 ACE_TCHAR stay_out_string
[100];
410 in_out
[j
].addr_to_string(in_out_string
, 100);
411 stay_out
[j
].addr_to_string(stay_out_string
, 100);
413 ACE_ERROR ((LM_ERROR
,
414 ACE_TEXT ("Failed forth get_secondary_addresses check\n")
415 ACE_TEXT ("%s != %s\n"),
424 ACE_ERROR ((LM_ERROR
,
425 ACE_TEXT ("Failed forth get_num_secondary_addresses check\n")
426 ACE_TEXT ("%d != %d\n"),
427 returned_num_secondaries
,