(rfc3484_sort): Make sure that even if qsort doesn't support
[glibc.git] / posix / tst-rfc3484.c
blob08d57a66eec379273dab760cc51b16dde74504e6
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;
21 int
22 __idna_to_ascii_lz (const char *input, char **output, int flags)
24 return 0;
26 int
27 __idna_to_unicode_lzlz (const char *input, char **output, int flags)
29 *output = NULL;
30 return 0;
33 #include "../sysdeps/posix/getaddrinfo.c"
35 service_user *__nss_hosts_database attribute_hidden;
38 /* This is the beginning of the real test code. The above defines
39 (among other things) the function rfc3484_sort. */
42 #if __BYTE_ORDER == __BIG_ENDIAN
43 # define h(n) n
44 #else
45 # define h(n) __bswap_constant_32 (n)
46 #endif
48 struct sockaddr_in addrs[] =
50 { .sin_family = AF_INET, .sin_addr = { h (0xc0a86d1d) } },
51 { .sin_family = AF_INET, .sin_addr = { h (0xc0a85d03) } },
52 { .sin_family = AF_INET, .sin_addr = { h (0xc0a82c3d) } },
53 { .sin_family = AF_INET, .sin_addr = { h (0xc0a86002) } },
54 { .sin_family = AF_INET, .sin_addr = { h (0xc0a802f3) } },
55 { .sin_family = AF_INET, .sin_addr = { h (0xc0a80810) } },
56 { .sin_family = AF_INET, .sin_addr = { h (0xc0a85e02) } }
58 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
59 static struct addrinfo ais[naddrs];
60 static struct sort_result results[naddrs];
62 static int expected[naddrs] =
64 6, 1, 0, 3, 2, 4, 5
68 ssize_t
69 __getline (char **lineptr, size_t *n, FILE *s)
71 *lineptr = NULL;
72 *n = 0;
73 return 0;
77 static int
78 do_test (void)
80 labels = default_labels;
81 precedence = default_precedence;
83 struct sockaddr_in so;
84 so.sin_family = AF_INET;
85 so.sin_addr.s_addr = h (0xc0a85f19);
87 for (int i = 0; i < naddrs; ++i)
89 ais[i].ai_family = AF_INET;
90 ais[i].ai_addr = (struct sockaddr *) &addrs[i];
91 results[i].dest_addr = &ais[i];
92 results[i].got_source_addr = true;
93 memcpy(&results[i].source_addr, &so, sizeof (so));
94 results[i].source_addr_len = sizeof (so);
95 results[i].source_addr_flags = 0;
96 results[i].service_order = i;
99 qsort (results, naddrs, sizeof (results[0]), rfc3484_sort);
101 int result = 0;
102 for (int i = 0; i < naddrs; ++i)
104 struct in_addr addr = ((struct sockaddr_in *) (results[i].dest_addr->ai_addr))->sin_addr;
106 int here = memcmp (&addr, &addrs[expected[i]].sin_addr,
107 sizeof (struct in_addr));
108 printf ("[%d] = %s: %s\n", i, inet_ntoa (addr), here ? "FAIL" : "OK");
109 result |= here;
112 return result;
115 #define TEST_FUNCTION do_test ()
116 #include "../test-skeleton.c"