Fix tst-rfc3484* build failures from USE_NSCD move to config.h.
[glibc.git] / posix / tst-rfc3484.c
blob29e1461e1181f2d49c29bcf8934cef8244d7230b
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <ifaddrs.h>
5 /* Internal definitions used in the libc code. */
6 #define __getservbyname_r getservbyname_r
7 #define __socket socket
8 #define __getsockname getsockname
9 #define __inet_aton inet_aton
10 #define __gethostbyaddr_r gethostbyaddr_r
11 #define __gethostbyname2_r gethostbyname2_r
13 void
14 attribute_hidden
15 __check_pf (bool *p1, bool *p2, struct in6addrinfo **in6ai, size_t *in6ailen)
17 *p1 = *p2 = true;
18 *in6ai = NULL;
19 *in6ailen = 0;
22 void
23 attribute_hidden
24 __free_in6ai (struct in6addrinfo *ai)
28 void
29 attribute_hidden
30 __check_native (uint32_t a1_index, int *a1_native,
31 uint32_t a2_index, int *a2_native)
35 int
36 attribute_hidden
37 __idna_to_ascii_lz (const char *input, char **output, int flags)
39 return 0;
42 int
43 attribute_hidden
44 __idna_to_unicode_lzlz (const char *input, char **output, int flags)
46 *output = NULL;
47 return 0;
50 void
51 attribute_hidden
52 _res_hconf_init (void)
56 #undef USE_NSCD
57 #include "../sysdeps/posix/getaddrinfo.c"
59 service_user *__nss_hosts_database attribute_hidden;
62 /* This is the beginning of the real test code. The above defines
63 (among other things) the function rfc3484_sort. */
66 #if __BYTE_ORDER == __BIG_ENDIAN
67 # define h(n) n
68 #else
69 # define h(n) __bswap_constant_32 (n)
70 #endif
72 struct sockaddr_in addrs[] =
74 { .sin_family = AF_INET, .sin_addr = { h (0xc0a86d1d) } },
75 { .sin_family = AF_INET, .sin_addr = { h (0xc0a85d03) } },
76 { .sin_family = AF_INET, .sin_addr = { h (0xc0a82c3d) } },
77 { .sin_family = AF_INET, .sin_addr = { h (0xc0a86002) } },
78 { .sin_family = AF_INET, .sin_addr = { h (0xc0a802f3) } },
79 { .sin_family = AF_INET, .sin_addr = { h (0xc0a80810) } },
80 { .sin_family = AF_INET, .sin_addr = { h (0xc0a85e02) } }
82 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
83 static struct addrinfo ais[naddrs];
84 static struct sort_result results[naddrs];
85 static size_t order[naddrs];
87 static int expected[naddrs] =
89 6, 1, 0, 3, 2, 4, 5
93 ssize_t
94 __getline (char **lineptr, size_t *n, FILE *s)
96 *lineptr = NULL;
97 *n = 0;
98 return 0;
102 static int
103 do_test (void)
105 labels = default_labels;
106 precedence = default_precedence;
107 scopes= default_scopes;
109 struct sockaddr_in so;
110 so.sin_family = AF_INET;
111 so.sin_addr.s_addr = h (0xc0a85f19);
112 /* Clear the rest of the structure to avoid warnings. */
113 memset (so.sin_zero, '\0', sizeof (so.sin_zero));
115 for (int i = 0; i < naddrs; ++i)
117 ais[i].ai_family = AF_INET;
118 ais[i].ai_addr = (struct sockaddr *) &addrs[i];
119 results[i].dest_addr = &ais[i];
120 results[i].got_source_addr = true;
121 memcpy(&results[i].source_addr, &so, sizeof (so));
122 results[i].source_addr_len = sizeof (so);
123 results[i].source_addr_flags = 0;
124 results[i].prefixlen = 8;
125 results[i].index = 0;
127 order[i] = i;
130 struct sort_result_combo combo = { .results = results, .nresults = naddrs };
131 qsort_r (order, naddrs, sizeof (order[0]), rfc3484_sort, &combo);
133 int result = 0;
134 for (int i = 0; i < naddrs; ++i)
136 struct in_addr addr = ((struct sockaddr_in *) (results[order[i]].dest_addr->ai_addr))->sin_addr;
138 int here = memcmp (&addr, &addrs[expected[i]].sin_addr,
139 sizeof (struct in_addr));
140 printf ("[%d] = %s: %s\n", i, inet_ntoa (addr), here ? "FAIL" : "OK");
141 result |= here;
144 return result;
147 #define TEST_FUNCTION do_test ()
148 #include "../test-skeleton.c"