tst-rfc3484.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <ifaddrs.h>
  4. #include <stdint.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
  12. #define __qsort_r qsort_r
  13. #define __stat64 stat64
  14. void
  15. attribute_hidden
  16. __check_pf (bool *p1, bool *p2, struct in6addrinfo **in6ai, size_t *in6ailen)
  17. {
  18. *p1 = *p2 = true;
  19. *in6ai = NULL;
  20. *in6ailen = 0;
  21. }
  22. void
  23. attribute_hidden
  24. __free_in6ai (struct in6addrinfo *ai)
  25. {
  26. }
  27. void
  28. attribute_hidden
  29. __check_native (uint32_t a1_index, int *a1_native,
  30. uint32_t a2_index, int *a2_native)
  31. {
  32. }
  33. int
  34. attribute_hidden
  35. __idna_to_ascii_lz (const char *input, char **output, int flags)
  36. {
  37. return 0;
  38. }
  39. int
  40. attribute_hidden
  41. __idna_to_unicode_lzlz (const char *input, char **output, int flags)
  42. {
  43. *output = NULL;
  44. return 0;
  45. }
  46. void
  47. attribute_hidden
  48. _res_hconf_init (void)
  49. {
  50. }
  51. #undef USE_NSCD
  52. #include "getaddrinfo.c"
  53. /* This is the beginning of the real test code. The above defines
  54. (among other things) the function rfc3484_sort. */
  55. #if __BYTE_ORDER == __BIG_ENDIAN
  56. # define h(n) n
  57. #else
  58. # define h(n) __bswap_constant_32 (n)
  59. #endif
  60. struct sockaddr_in addrs[] =
  61. {
  62. { .sin_family = AF_INET, .sin_addr = { h (0x0aa85f19) } },
  63. { .sin_family = AF_INET, .sin_addr = { h (0xac105f19) } },
  64. { .sin_family = AF_INET, .sin_addr = { h (0xc0000219) } },
  65. { .sin_family = AF_INET, .sin_addr = { h (0xc0a86d1d) } },
  66. { .sin_family = AF_INET, .sin_addr = { h (0xc0a85d03) } },
  67. { .sin_family = AF_INET, .sin_addr = { h (0xc0a82c3d) } },
  68. { .sin_family = AF_INET, .sin_addr = { h (0xc0a86002) } },
  69. { .sin_family = AF_INET, .sin_addr = { h (0xc0a802f3) } },
  70. { .sin_family = AF_INET, .sin_addr = { h (0xc0a80810) } },
  71. { .sin_family = AF_INET, .sin_addr = { h (0xc0a85e02) } }
  72. };
  73. #define naddrs (sizeof (addrs) / sizeof (addrs[0]))
  74. static struct addrinfo ais[naddrs];
  75. static struct sort_result results[naddrs];
  76. static size_t order[naddrs];
  77. static int expected[naddrs] =
  78. {
  79. 9, 4, 3, 6, 5, 7, 8, 2, 0, 1
  80. };
  81. ssize_t
  82. __getline (char **lineptr, size_t *n, FILE *s)
  83. {
  84. *lineptr = NULL;
  85. *n = 0;
  86. return 0;
  87. }
  88. static int
  89. do_test (void)
  90. {
  91. labels = default_labels;
  92. precedence = default_precedence;
  93. scopes= default_scopes;
  94. struct sockaddr_in so;
  95. so.sin_family = AF_INET;
  96. so.sin_addr.s_addr = h (0xc0a85f19);
  97. /* Clear the rest of the structure to avoid warnings. */
  98. memset (so.sin_zero, '\0', sizeof (so.sin_zero));
  99. for (int i = 0; i < naddrs; ++i)
  100. {
  101. ais[i].ai_family = AF_INET;
  102. ais[i].ai_addr = (struct sockaddr *) &addrs[i];
  103. results[i].dest_addr = &ais[i];
  104. results[i].got_source_addr = true;
  105. memcpy(&results[i].source_addr, &so, sizeof (so));
  106. results[i].source_addr_len = sizeof (so);
  107. results[i].source_addr_flags = 0;
  108. results[i].prefixlen = 8;
  109. results[i].index = 0;
  110. order[i] = i;
  111. }
  112. struct sort_result_combo combo = { .results = results, .nresults = naddrs };
  113. qsort_r (order, naddrs, sizeof (order[0]), rfc3484_sort, &combo);
  114. int result = 0;
  115. for (int i = 0; i < naddrs; ++i)
  116. {
  117. struct in_addr addr = ((struct sockaddr_in *) (results[order[i]].dest_addr->ai_addr))->sin_addr;
  118. int here = memcmp (&addr, &addrs[expected[i]].sin_addr,
  119. sizeof (struct in_addr));
  120. printf ("[%d] = %s: %s\n", i, inet_ntoa (addr), here ? "FAIL" : "OK");
  121. result |= here;
  122. }
  123. return result;
  124. }
  125. #define TEST_FUNCTION do_test ()
  126. #include "../test-skeleton.c"