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