Updated to fedora-glibc-20071212T1051
[glibc.git] / posix / tst-rfc3484.c
blobfe06255759e1da50a71a2bc5f481224a3db577c8
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 void
22 attribute_hidden
23 __check_native (uint32_t a1_index, int *a1_native,
24 uint32_t a2_index, int *a2_native)
27 int
28 __idna_to_ascii_lz (const char *input, char **output, int flags)
30 return 0;
32 int
33 __idna_to_unicode_lzlz (const char *input, char **output, int flags)
35 *output = NULL;
36 return 0;
39 #include "../sysdeps/posix/getaddrinfo.c"
41 service_user *__nss_hosts_database attribute_hidden;
44 /* This is the beginning of the real test code. The above defines
45 (among other things) the function rfc3484_sort. */
48 #if __BYTE_ORDER == __BIG_ENDIAN
49 # define h(n) n
50 #else
51 # define h(n) __bswap_constant_32 (n)
52 #endif
54 struct sockaddr_in addrs[] =
56 { .sin_family = AF_INET, .sin_addr = { h (0xc0a86d1d) } },
57 { .sin_family = AF_INET, .sin_addr = { h (0xc0a85d03) } },
58 { .sin_family = AF_INET, .sin_addr = { h (0xc0a82c3d) } },
59 { .sin_family = AF_INET, .sin_addr = { h (0xc0a86002) } },
60 { .sin_family = AF_INET, .sin_addr = { h (0xc0a802f3) } },
61 { .sin_family = AF_INET, .sin_addr = { h (0xc0a80810) } },
62 { .sin_family = AF_INET, .sin_addr = { h (0xc0a85e02) } }
64 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
65 static struct addrinfo ais[naddrs];
66 static struct sort_result results[naddrs];
68 static int expected[naddrs] =
70 6, 1, 0, 3, 2, 4, 5
74 ssize_t
75 __getline (char **lineptr, size_t *n, FILE *s)
77 *lineptr = NULL;
78 *n = 0;
79 return 0;
83 static int
84 do_test (void)
86 labels = default_labels;
87 precedence = default_precedence;
88 scopes= default_scopes;
90 struct sockaddr_in so;
91 so.sin_family = AF_INET;
92 so.sin_addr.s_addr = h (0xc0a85f19);
94 for (int i = 0; i < naddrs; ++i)
96 ais[i].ai_family = AF_INET;
97 ais[i].ai_addr = (struct sockaddr *) &addrs[i];
98 results[i].dest_addr = &ais[i];
99 results[i].got_source_addr = true;
100 memcpy(&results[i].source_addr, &so, sizeof (so));
101 results[i].source_addr_len = sizeof (so);
102 results[i].source_addr_flags = 0;
103 results[i].service_order = i;
104 results[i].prefixlen = 8;
105 results[i].index = 0;
108 struct sort_result_combo combo = { .results = results, .nresults = naddrs };
109 qsort_r (results, naddrs, sizeof (results[0]), rfc3484_sort, &combo);
111 int result = 0;
112 for (int i = 0; i < naddrs; ++i)
114 struct in_addr addr = ((struct sockaddr_in *) (results[i].dest_addr->ai_addr))->sin_addr;
116 int here = memcmp (&addr, &addrs[expected[i]].sin_addr,
117 sizeof (struct in_addr));
118 printf ("[%d] = %s: %s\n", i, inet_ntoa (addr), here ? "FAIL" : "OK");
119 result |= here;
122 return result;
125 #define TEST_FUNCTION do_test ()
126 #include "../test-skeleton.c"