]> git.ipfire.org Git - thirdparty/glibc.git/blob - posix/tst-rfc3484-3.c
tst-gnuglob64: New test for glob64 based on tst-gnuglob
[thirdparty/glibc.git] / posix / tst-rfc3484-3.c
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <ifaddrs.h>
4 #include <stdint.h>
5
6 /* Internal definitions used in the libc code. */
7 #define __getservbyname_r getservbyname_r
8 #define __socket socket
9 #define __getsockname getsockname
10 #define __inet_aton inet_aton
11 #define __gethostbyaddr_r gethostbyaddr_r
12 #define __gethostbyname2_r gethostbyname2_r
13 #define __qsort_r qsort_r
14
15 void
16 attribute_hidden
17 __check_pf (bool *p1, bool *p2, struct in6addrinfo **in6ai, size_t *in6ailen)
18 {
19 *p1 = *p2 = true;
20 *in6ai = NULL;
21 *in6ailen = 0;
22 }
23
24 void
25 attribute_hidden
26 __free_in6ai (struct in6addrinfo *ai)
27 {
28 }
29
30 void
31 attribute_hidden
32 __check_native (uint32_t a1_index, int *a1_native,
33 uint32_t a2_index, int *a2_native)
34 {
35 }
36
37 int
38 attribute_hidden
39 __idna_to_ascii_lz (const char *input, char **output, int flags)
40 {
41 return 0;
42 }
43
44 int
45 attribute_hidden
46 __idna_to_unicode_lzlz (const char *input, char **output, int flags)
47 {
48 *output = NULL;
49 return 0;
50 }
51
52 void
53 attribute_hidden
54 _res_hconf_init (void)
55 {
56 }
57
58 #undef USE_NSCD
59 #include "../sysdeps/posix/getaddrinfo.c"
60
61
62 /* This is the beginning of the real test code. The above defines
63 (among other things) the function rfc3484_sort. */
64
65
66 #if __BYTE_ORDER == __BIG_ENDIAN
67 # define h(n) n
68 #else
69 # define h(n) __bswap_constant_32 (n)
70 #endif
71
72 struct sockaddr_in addrs[] =
73 {
74 { .sin_family = AF_INET, .sin_addr = { h (0xa0a86d1d) } },
75 { .sin_family = AF_INET, .sin_addr = { h (0xa0a85d03) } },
76 { .sin_family = AF_INET, .sin_addr = { h (0xa0a82c3d) } },
77 { .sin_family = AF_INET, .sin_addr = { h (0xa0a86002) } },
78 { .sin_family = AF_INET, .sin_addr = { h (0xa0a802f3) } },
79 { .sin_family = AF_INET, .sin_addr = { h (0xa0a80810) } },
80 { .sin_family = AF_INET, .sin_addr = { h (0xa0a85e02) } },
81 { .sin_family = AF_INET, .sin_addr = { h (0xac162311) } },
82 { .sin_family = AF_INET, .sin_addr = { h (0x0a324572) } }
83 };
84 #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
85 static struct addrinfo ais[naddrs];
86 static struct sort_result results[naddrs];
87 static size_t order[naddrs];
88
89 static const int expected[naddrs] =
90 {
91 8, 0, 1, 2, 3, 4, 5, 6, 7
92 };
93
94 static const struct scopeentry new_scopes[] =
95 {
96 { { { 169, 254, 0, 0 } }, h (0xffff0000), 2 },
97 { { { 127, 0, 0, 0 } }, h (0xff000000), 2 },
98 { { { 10, 0, 0, 0 } }, h (0xff000000), 5 },
99 { { { 192, 168, 0, 0 } }, h(0xffff0000), 5 },
100 { { { 0, 0, 0, 0 } }, h (0x00000000), 14 }
101 };
102
103
104 ssize_t
105 __getline (char **lineptr, size_t *n, FILE *s)
106 {
107 *lineptr = NULL;
108 *n = 0;
109 return 0;
110 }
111
112
113 static int
114 do_test (void)
115 {
116 labels = default_labels;
117 precedence = default_precedence;
118 scopes= new_scopes;
119
120 struct sockaddr_in so;
121 so.sin_family = AF_INET;
122 so.sin_addr.s_addr = h (0x0aa85f19);
123 /* Clear the rest of the structure to avoid warnings. */
124 memset (so.sin_zero, '\0', sizeof (so.sin_zero));
125
126 for (int i = 0; i < naddrs; ++i)
127 {
128 ais[i].ai_family = AF_INET;
129 ais[i].ai_addr = (struct sockaddr *) &addrs[i];
130 results[i].dest_addr = &ais[i];
131 results[i].got_source_addr = true;
132 memcpy(&results[i].source_addr, &so, sizeof (so));
133 results[i].source_addr_len = sizeof (so);
134 results[i].source_addr_flags = 0;
135 results[i].prefixlen = 8;
136 results[i].index = 0;
137
138 order[i] = i;
139 }
140
141 struct sort_result_combo combo = { .results = results, .nresults = naddrs };
142 qsort_r (order, naddrs, sizeof (order[0]), rfc3484_sort, &combo);
143
144 int result = 0;
145 for (int i = 0; i < naddrs; ++i)
146 {
147 struct in_addr addr = ((struct sockaddr_in *) (results[order[i]].dest_addr->ai_addr))->sin_addr;
148
149 int here = memcmp (&addr, &addrs[expected[i]].sin_addr,
150 sizeof (struct in_addr));
151 printf ("[%d] = %s: %s\n", i, inet_ntoa (addr), here ? "FAIL" : "OK");
152 result |= here;
153 }
154
155 return result;
156 }
157
158 #define TEST_FUNCTION do_test ()
159 #include "../test-skeleton.c"