Fix unnecessary overallocation due to incomplete character
[platform/upstream/glibc.git] / posix / tst-rfc3484-2.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 __check_native (uint32_t a1_index, int *a1_native,
25                 uint32_t a2_index, int *a2_native)
26 {
27 }
28
29 int
30 attribute_hidden
31 __idna_to_ascii_lz (const char *input, char **output, int flags)
32 {
33   return 0;
34 }
35
36 int
37 attribute_hidden
38 __idna_to_unicode_lzlz (const char *input, char **output, int flags)
39 {
40   *output = NULL;
41   return 0;
42 }
43
44 void
45 attribute_hidden
46 _res_hconf_init (void)
47 {
48 }
49
50 #include "../sysdeps/posix/getaddrinfo.c"
51
52 service_user *__nss_hosts_database attribute_hidden;
53
54
55 /* This is the beginning of the real test code.  The above defines
56    (among other things) the function rfc3484_sort.  */
57
58
59 #if __BYTE_ORDER == __BIG_ENDIAN
60 # define h(n) n
61 #else
62 # define h(n) __bswap_constant_32 (n)
63 #endif
64
65
66 ssize_t
67 __getline (char **lineptr, size_t *n, FILE *s)
68 {
69   *lineptr = NULL;
70   *n = 0;
71   return 0;
72 }
73
74
75 static int
76 do_test (void)
77 {
78   labels = default_labels;
79   precedence = default_precedence;
80   scopes = default_scopes;
81
82   struct sockaddr_in so1;
83   so1.sin_family = AF_INET;
84   so1.sin_addr.s_addr = h (0xc0a85f19);
85   /* Clear the rest of the structure to avoid warnings.  */
86   memset (so1.sin_zero, '\0', sizeof (so1.sin_zero));
87
88   struct sockaddr_in sa1;
89   sa1.sin_family = AF_INET;
90   sa1.sin_addr.s_addr = h (0xe0a85f19);
91
92   struct addrinfo ai1;
93   ai1.ai_family = AF_INET;
94   ai1.ai_addr = (struct sockaddr *) &sa1;
95
96   struct sockaddr_in6 so2;
97   so2.sin6_family = AF_INET6;
98   so2.sin6_addr.s6_addr32[0] = h (0xfec01234);
99   so2.sin6_addr.s6_addr32[1] = 1;
100   so2.sin6_addr.s6_addr32[2] = 1;
101   so2.sin6_addr.s6_addr32[3] = 1;
102
103   struct sockaddr_in6 sa2;
104   sa2.sin6_family = AF_INET6;
105   sa2.sin6_addr.s6_addr32[0] = h (0x07d10001);
106   sa2.sin6_addr.s6_addr32[1] = 1;
107   sa2.sin6_addr.s6_addr32[2] = 1;
108   sa2.sin6_addr.s6_addr32[3] = 1;
109
110   struct addrinfo ai2;
111   ai2.ai_family = AF_INET6;
112   ai2.ai_addr = (struct sockaddr *) &sa2;
113
114
115   struct sort_result results[2];
116   size_t order[2];
117
118   results[0].dest_addr = &ai1;
119   results[0].got_source_addr = true;
120   results[0].source_addr_len = sizeof (so1);
121   results[0].source_addr_flags = 0;
122   results[0].prefixlen = 16;
123   results[0].index = 0;
124   memcpy (&results[0].source_addr, &so1, sizeof (so1));
125   order[0] = 0;
126
127   results[1].dest_addr = &ai2;
128   results[1].got_source_addr = true;
129   results[1].source_addr_len = sizeof (so2);
130   results[1].source_addr_flags = 0;
131   results[1].prefixlen = 16;
132   results[1].index = 0;
133   memcpy (&results[1].source_addr, &so2, sizeof (so2));
134   order[1] = 1;
135
136
137   struct sort_result_combo combo = { .results = results, .nresults = 2 };
138   qsort_r (order, 2, sizeof (order[0]), rfc3484_sort, &combo);
139
140   int result = 0;
141   if (results[order[0]].dest_addr->ai_family == AF_INET6)
142     {
143       puts ("wrong order in first test");
144       result |= 1;
145     }
146
147
148   /* And again, this time with the reverse starting order.  */
149   results[1].dest_addr = &ai1;
150   results[1].got_source_addr = true;
151   results[1].source_addr_len = sizeof (so1);
152   results[1].source_addr_flags = 0;
153   results[1].prefixlen = 16;
154   results[1].index = 0;
155   memcpy (&results[1].source_addr, &so1, sizeof (so1));
156   order[1] = 1;
157
158   results[0].dest_addr = &ai2;
159   results[0].got_source_addr = true;
160   results[0].source_addr_len = sizeof (so2);
161   results[0].source_addr_flags = 0;
162   results[0].prefixlen = 16;
163   results[0].index = 0;
164   memcpy (&results[0].source_addr, &so2, sizeof (so2));
165   order[0] = 0;
166
167
168   qsort_r (order, 2, sizeof (order[0]), rfc3484_sort, &combo);
169
170   if (results[order[0]].dest_addr->ai_family == AF_INET6)
171     {
172       puts ("wrong order in second test");
173       result |= 1;
174     }
175
176   return result;
177 }
178
179 #define TEST_FUNCTION do_test ()
180 #include "../test-skeleton.c"