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