tst-nss-files-hosts-erange.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Parse /etc/hosts in multi mode with a trailing long line (bug 21915).
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <dlfcn.h>
  16. #include <errno.h>
  17. #include <gnu/lib-names.h>
  18. #include <netdb.h>
  19. #include <nss.h>
  20. #include <support/check.h>
  21. #include <support/check_nss.h>
  22. #include <support/namespace.h>
  23. #include <support/test-driver.h>
  24. #include <support/xunistd.h>
  25. struct support_chroot *chroot_env;
  26. #define X10 "XXXXXXXXXX"
  27. #define X100 X10 X10 X10 X10 X10 X10 X10 X10 X10 X10
  28. #define X1000 X100 X100 X100 X100 X100 X100 X100 X100 X100 X100
  29. static void
  30. prepare (int argc, char **argv)
  31. {
  32. chroot_env = support_chroot_create
  33. ((struct support_chroot_configuration)
  34. {
  35. .resolv_conf = "",
  36. .hosts =
  37. "127.0.0.1 localhost localhost.localdomain\n"
  38. "::1 localhost localhost.localdomain\n"
  39. "192.0.2.1 example.com\n"
  40. "#" X1000 X100 "\n",
  41. .host_conf = "multi on\n",
  42. });
  43. }
  44. static int
  45. do_test (void)
  46. {
  47. support_become_root ();
  48. if (!support_can_chroot ())
  49. return EXIT_UNSUPPORTED;
  50. __nss_configure_lookup ("hosts", "files");
  51. if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
  52. FAIL_EXIT1 ("could not load " LIBNSS_DNS_SO ": %s", dlerror ());
  53. xchroot (chroot_env->path_chroot);
  54. errno = ERANGE;
  55. h_errno = NETDB_INTERNAL;
  56. check_hostent ("gethostbyname example.com",
  57. gethostbyname ("example.com"),
  58. "name: example.com\n"
  59. "address: 192.0.2.1\n");
  60. errno = ERANGE;
  61. h_errno = NETDB_INTERNAL;
  62. check_hostent ("gethostbyname2 AF_INET example.com",
  63. gethostbyname2 ("example.com", AF_INET),
  64. "name: example.com\n"
  65. "address: 192.0.2.1\n");
  66. {
  67. struct addrinfo hints =
  68. {
  69. .ai_family = AF_UNSPEC,
  70. .ai_socktype = SOCK_STREAM,
  71. .ai_protocol = IPPROTO_TCP,
  72. };
  73. errno = ERANGE;
  74. h_errno = NETDB_INTERNAL;
  75. struct addrinfo *ai;
  76. int ret = getaddrinfo ("example.com", "80", &hints, &ai);
  77. check_addrinfo ("example.com AF_UNSPEC", ai, ret,
  78. "address: STREAM/TCP 192.0.2.1 80\n");
  79. if (ret == 0)
  80. freeaddrinfo (ai);
  81. hints.ai_family = AF_INET;
  82. errno = ERANGE;
  83. h_errno = NETDB_INTERNAL;
  84. ret = getaddrinfo ("example.com", "80", &hints, &ai);
  85. check_addrinfo ("example.com AF_INET", ai, ret,
  86. "address: STREAM/TCP 192.0.2.1 80\n");
  87. if (ret == 0)
  88. freeaddrinfo (ai);
  89. }
  90. support_chroot_free (chroot_env);
  91. return 0;
  92. }
  93. #define PREPARE prepare
  94. #include <support/test-driver.c>