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
) {
97 /****** Clear the in_out array and test subject ******/
100 // Clear the in_out array by setting every port to 0 and every
102 for (j
= 0; j
< num_secondaries
; ++j
) {
103 in_out
[j
].set(0, ACE_UINT32 (1), 1);
106 // Clear the in_out_sockaddr array by setting every port to 0 and
107 // every address to 1
108 ACE_OS::memset(in_out_sockaddr
, 0, num_sockaddrs
* sizeof(sockaddr
));
110 // Clear the test subject by setting the port to 2 and every
111 // address (both the primary and the secondaries) to 3
112 addr
.set (2, ACE_UINT32 (3), 1, array_of_threes
, num_secondaries
);
114 // Check that the port is 2
115 if (addr
.get_port_number() != 2) {
116 ACE_ERROR ((LM_ERROR
,
117 ACE_TEXT ("Failed get_port_number check\n")
118 ACE_TEXT ("%d != %d\n"),
119 addr
.get_port_number(),
124 // Check that the primary address is 3
125 if (addr
.get_ip_address() != ACE_UINT32 (3)) {
126 ACE_ERROR ((LM_ERROR
,
127 ACE_TEXT ("Failed get_ip_address check\n")
128 ACE_TEXT ("0x%x != 0x%x\n"),
129 addr
.get_ip_address(),
134 // Check that the test subject reports the correct number of
135 // secondary addresses.
136 size_t returned_num_secondaries
= addr
.get_num_secondary_addresses();
137 if (returned_num_secondaries
== num_secondaries
) {
139 // Set a stay_out element to the state that we expect to see
140 // from every in_out element after the in_out array is passed to
141 // the accessor of the test subject.
142 stay_out
[0].set(2, ACE_UINT32 (3), 1);
144 // Pass the in_out array to the accessor
145 addr
.get_secondary_addresses(in_out
, num_secondaries
);
147 // Check that the in_out array matches stay_out element
148 for (j
= 0; j
< num_secondaries
; ++j
) {
150 if (in_out
[j
] != stay_out
[0]) {
152 ACE_TCHAR in_out_string
[100];
153 ACE_TCHAR stay_out_string
[100];
155 in_out
[j
].addr_to_string(in_out_string
, 100);
156 stay_out
[0].addr_to_string(stay_out_string
, 100);
158 ACE_ERROR ((LM_ERROR
,
159 ACE_TEXT ("Failed get_secondary_addresses check\n")
160 ACE_TEXT ("%s != %s\n"),
168 // Pass the in_out_sockaddr array to the accessor
169 addr
.get_addresses(in_out_sockaddr
, num_secondaries
+ 1);
171 // Check that the in_out_sockaddr array matches stay_out element
172 for (j
= 0, pointer
= in_out_sockaddr
;
173 j
< num_secondaries
+ 1;
176 if (ACE_OS::memcmp(pointer
, stay_out
[0].get_addr(), sizeof(sockaddr
))) {
178 ACE_ERROR ((LM_ERROR
,
179 ACE_TEXT ("Failed get_addresses check\n")));
187 ACE_ERROR ((LM_ERROR
,
188 ACE_TEXT ("Failed get_num_secondary_addresses check\n")
189 ACE_TEXT ("%d != %d\n"),
190 returned_num_secondaries
,
197 /**** Test set (u_short, const char[], int, int, const char *([]), size_t) ****/
201 primary_dotted_decimal
,
204 secondary_dotted_decimals
,
207 // Check the port number
208 if (addr
.get_port_number() != port
) {
209 ACE_ERROR ((LM_ERROR
,
210 ACE_TEXT ("Failed second get_port_number check\n")
211 ACE_TEXT ("%d != %d\n"),
212 addr
.get_port_number(),
217 // Check the primary address
218 if (0 != ACE_OS::strcmp (addr
.get_host_addr(), primary_dotted_decimal
))
220 ACE_ERROR ((LM_ERROR
,
221 ACE_TEXT ("%C failed get_host_addr() check\n")
222 ACE_TEXT ("%C != %C\n"),
223 primary_dotted_decimal
,
224 addr
.get_host_addr (),
225 primary_dotted_decimal
));
229 // Check that the test subject reports the correct number of
230 // secondary addresses.
231 returned_num_secondaries
= addr
.get_num_secondary_addresses();
232 if (returned_num_secondaries
== i
) {
234 // Initialize the stay_out array with the secondary addresses
235 for (j
= 0; j
< i
; ++j
) {
236 stay_out
[j
].set(port
, secondary_dotted_decimals
[j
]);
239 // Pass the in_out array to the accessor
240 addr
.get_secondary_addresses(in_out
, i
);
242 // Check that the in_out array matches stay_out array
243 for (j
= 0; j
< i
; ++j
) {
245 if (in_out
[j
] != stay_out
[j
]) {
247 ACE_TCHAR in_out_string
[100];
248 ACE_TCHAR stay_out_string
[100];
250 in_out
[j
].addr_to_string(in_out_string
, 100);
251 stay_out
[j
].addr_to_string(stay_out_string
, 100);
253 ACE_ERROR ((LM_ERROR
,
254 ACE_TEXT ("Failed second get_secondary_addresses check\n")
255 ACE_TEXT ("%s != %s\n"),
263 // Pass the in_out_sockaddr array to the accessor
264 addr
.get_addresses(in_out_sockaddr
, i
+ 1);
266 // Check that the primary address in the in_out_sockaddr array
267 // matches the primary address reported by the superclass
268 if (ACE_OS::memcmp(in_out_sockaddr
, addr
.get_addr(), sizeof(sockaddr
))) {
270 ACE_ERROR ((LM_ERROR
,
271 ACE_TEXT ("Failed second get_addresses check ")
272 ACE_TEXT ("(for primary address)\n")));
278 // Check that the secondary addresses in the in_out_sockaddr
279 // array match the stay_out array
280 for (j
= 1, pointer
= &in_out_sockaddr
[1];
284 if (ACE_OS::memcmp(pointer
, stay_out
[j
-1].get_addr(), sizeof(sockaddr
))) {
286 ACE_ERROR ((LM_ERROR
,
287 ACE_TEXT ("Failed second get_addresses check ")
288 ACE_TEXT ("(for secondary addresses)\n")));
296 ACE_ERROR ((LM_ERROR
,
297 ACE_TEXT ("Failed second get_num_secondary_addresses check\n")
298 ACE_TEXT ("%d != %d\n"),
299 returned_num_secondaries
,
305 /****** Clear the in_out array and test subject AGAIN ******/
308 // Clear the in_out array by setting every port to 0 and every
310 for (j
= 0; j
< num_secondaries
; ++j
) {
311 in_out
[j
].set(0, ACE_UINT32 (1), 1);
314 // Clear the test subject by setting the port to 2 and every
315 // address (both the primary and the secondaries) to 3
316 addr
.set (2, ACE_UINT32 (3), 1, array_of_threes
, num_secondaries
);
318 // Check that the port is 2
319 if (addr
.get_port_number() != 2) {
320 ACE_ERROR ((LM_ERROR
,
321 ACE_TEXT ("Failed third get_port_number check\n")
322 ACE_TEXT ("%d != %d\n"),
323 addr
.get_port_number(),
328 // Check that the primary address is 3
329 if (addr
.get_ip_address() != ACE_UINT32 (3)) {
330 ACE_ERROR ((LM_ERROR
,
331 ACE_TEXT ("Failed third get_ip_address check\n")
332 ACE_TEXT ("0x%x != 0x%x\n"),
333 addr
.get_ip_address(),
338 // Check that the test subject reports the correct number of
339 // secondary addresses.
340 returned_num_secondaries
= addr
.get_num_secondary_addresses();
341 if (returned_num_secondaries
== num_secondaries
) {
343 // Set a stay_out element to the state that we expect to see
344 // from every in_out element after the in_out array is passed to
345 // the accessor of the test subject.
346 stay_out
[0].set(2, ACE_UINT32 (3), 1);
348 // Pass the in_out array to the accessor
349 addr
.get_secondary_addresses(in_out
, num_secondaries
);
351 // Check that the in_out array matches stay_out array
352 for (j
= 0; j
< num_secondaries
; ++j
) {
354 if (in_out
[j
] != stay_out
[0]) {
356 ACE_TCHAR in_out_string
[100];
357 ACE_TCHAR stay_out_string
[100];
359 in_out
[j
].addr_to_string(in_out_string
, 100);
360 stay_out
[0].addr_to_string(stay_out_string
, 100);
362 ACE_ERROR ((LM_ERROR
,
363 ACE_TEXT ("Failed third get_secondary_addresses check\n")
364 ACE_TEXT ("%s != %s\n"),
374 ACE_ERROR ((LM_ERROR
,
375 ACE_TEXT ("Failed third get_num_secondary_addresses check\n")
376 ACE_TEXT ("%d != %d\n"),
377 returned_num_secondaries
,
384 /**** Test set (u_short, ACE_UINT32, int, const ACE_UINT32 *, size_t) ****/
392 // Check the port number
393 if (addr
.get_port_number() != port
) {
394 ACE_ERROR ((LM_ERROR
,
395 ACE_TEXT ("Failed forth get_port_number check\n")
396 ACE_TEXT ("%d != %d\n"),
397 addr
.get_port_number(),
402 // Check the primary address
403 if (0 != ACE_OS::strcmp (addr
.get_host_addr(), primary_dotted_decimal
))
405 ACE_ERROR ((LM_ERROR
,
406 ACE_TEXT ("%C failed second get_ip_address() check\n")
407 ACE_TEXT ("%C != %C\n"),
408 primary_dotted_decimal
,
409 addr
.get_host_addr (),
410 primary_dotted_decimal
));
414 // Check that the test subject reports the correct number of
415 // secondary addresses.
416 returned_num_secondaries
= addr
.get_num_secondary_addresses();
417 if (returned_num_secondaries
== i
) {
419 // Initialize the stay_out array with the secondary addresses
420 for (j
= 0; j
< i
; ++j
) {
421 stay_out
[j
].set(port
, secondary_addr32
[j
]);
424 // Pass the in_out array to the accessor
425 addr
.get_secondary_addresses(in_out
, j
);
427 // Check that the in_out array matches stay_out array
428 for (j
= 0; j
< i
; ++j
) {
430 if (in_out
[j
] != stay_out
[j
]) {
432 ACE_TCHAR in_out_string
[100];
433 ACE_TCHAR stay_out_string
[100];
435 in_out
[j
].addr_to_string(in_out_string
, 100);
436 stay_out
[j
].addr_to_string(stay_out_string
, 100);
438 ACE_ERROR ((LM_ERROR
,
439 ACE_TEXT ("Failed forth get_secondary_addresses check\n")
440 ACE_TEXT ("%s != %s\n"),
450 ACE_ERROR ((LM_ERROR
,
451 ACE_TEXT ("Failed forth get_num_secondary_addresses check\n")
452 ACE_TEXT ("%d != %d\n"),
453 returned_num_secondaries
,