[BZ #2380]
[glibc.git] / posix / tst-rfc3484.c
blob378e43a01197154c263cf0d8b0e2e9006fc2fbaa
1 #include <stdbool.h>
2 #include <stdio.h>
4 /* Internal definitions used in the libc code. */
5 #define __getservbyname_r getservbyname_r
6 #define __socket socket
7 #define __getsockname getsockname
8 #define __inet_aton inet_aton
9 #define __gethostbyaddr_r gethostbyaddr_r
10 #define __gethostbyname2_r gethostbyname2_r
12 void
13 attribute_hidden
14 __check_pf (bool *p1, bool *p2)
16 *p1 = *p2 = true;
18 int
19 __idna_to_ascii_lz (const char *input, char **output, int flags)
21 return 0;
23 int
24 __idna_to_unicode_lzlz (const char *input, char **output, int flags)
26 return 0;
29 #include "../sysdeps/posix/getaddrinfo.c"
31 service_user *__nss_hosts_database attribute_hidden;
34 /* This is the beginning of the real test code. The above defines
35 (among other things) the function rfc3484_sort. */
38 #if __BYTE_ORDER == __BIG_ENDIAN
39 # define h(n) n
40 #else
41 # define h(n) __bswap_constant_32 (n)
42 #endif
44 struct sockaddr_in addrs[] =
46 { .sin_family = AF_INET, .sin_addr = { h (0xc0a86d1d) } },
47 { .sin_family = AF_INET, .sin_addr = { h (0xc0a85d03) } },
48 { .sin_family = AF_INET, .sin_addr = { h (0xc0a82c3d) } },
49 { .sin_family = AF_INET, .sin_addr = { h (0xc0a86002) } },
50 { .sin_family = AF_INET, .sin_addr = { h (0xc0a802f3) } },
51 { .sin_family = AF_INET, .sin_addr = { h (0xc0a80810) } },
52 { .sin_family = AF_INET, .sin_addr = { h (0xc0a85e02) } }
54 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
55 static struct addrinfo ais[naddrs];
56 static struct sort_result results[naddrs];
58 static int expected[naddrs] =
60 6, 1, 0, 3, 2, 4, 5
64 static int
65 do_test (void)
67 struct sockaddr_in so;
68 so.sin_family = AF_INET;
69 so.sin_addr.s_addr = h (0xc0a85f19);
71 for (int i = 0; i < naddrs; ++i)
73 ais[i].ai_family = AF_INET;
74 ais[i].ai_addr = (struct sockaddr *) &addrs[i];
75 results[i].dest_addr = &ais[i];
76 results[i].got_source_addr = true;
77 memcpy(&results[i].source_addr, &so, sizeof (so));
78 results[i].source_addr_len = sizeof (so);
81 qsort (results, naddrs, sizeof (results[0]), rfc3484_sort);
83 int result = 0;
84 for (int i = 0; i < naddrs; ++i)
86 struct in_addr addr = ((struct sockaddr_in *) (results[i].dest_addr->ai_addr))->sin_addr;
88 int here = memcmp (&addr, &addrs[expected[i]].sin_addr,
89 sizeof (struct in_addr));
90 printf ("[%d] = %s: %s\n", i, inet_ntoa (addr), here ? "FAIL" : "OK");
91 result |= here;
94 return result;
97 #define TEST_FUNCTION do_test ()
98 #include "../test-skeleton.c"