tst-nss-files-network.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Test long entries and truncated numbers in /etc/networks (bug 32573/32575).
  2. Copyright (C) 2025-2026 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. <https://www.gnu.org/licenses/>. */
  15. #include <netdb.h>
  16. #include <gnu/lib-names.h>
  17. #include <nss.h>
  18. #include <stddef.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <support/check.h>
  23. #include <support/check_nss.h>
  24. #include <support/namespace.h>
  25. #include <support/test-driver.h>
  26. #include <support/xdlfcn.h>
  27. #include <support/xunistd.h>
  28. #include <sys/resource.h>
  29. #define STACK_LIM 1048576
  30. #define STRING_SIZE (2 * STACK_LIM)
  31. struct support_chroot *chroot_env;
  32. static void
  33. prepare (int argc, char **argv)
  34. {
  35. int ret;
  36. char *content;
  37. char *entry = malloc (STRING_SIZE);
  38. struct rlimit lim;
  39. getrlimit (RLIMIT_STACK, &lim);
  40. lim.rlim_cur = STACK_LIM;
  41. setrlimit (RLIMIT_STACK, &lim);
  42. if (entry == NULL)
  43. {
  44. puts ("malloc failed, cannot test");
  45. exit (1);
  46. }
  47. memset (entry, 'A', STRING_SIZE);
  48. entry[STRING_SIZE - 1] = 0;
  49. ret = asprintf (&content, "%s\n%s\nnet3 %s\n",
  50. "net1 x0000000000Ff.077", /* legal 255.63.0.0 */
  51. "net2 xFF00000000.0.0.0", /* illegal */
  52. entry /* illegal */);
  53. if (ret == -1)
  54. {
  55. puts ("asprintf failed, cannot test");
  56. exit (1);
  57. }
  58. free (entry);
  59. chroot_env = support_chroot_create
  60. ((struct support_chroot_configuration)
  61. {
  62. .networks = content
  63. });
  64. }
  65. static int
  66. do_test (void)
  67. {
  68. support_become_root ();
  69. if (!support_can_chroot ())
  70. return EXIT_UNSUPPORTED;
  71. __nss_configure_lookup ("networks", "files");
  72. xdlopen (LIBNSS_FILES_SO, RTLD_NOW);
  73. xchroot (chroot_env->path_chroot);
  74. check_netent ("net1", getnetbyname ("net1"),
  75. "name: net1\n"
  76. "net: 0xff3f0000\n");
  77. check_netent ("net2", getnetbyname ("net2"), "error: HOST_NOT_FOUND\n");
  78. support_chroot_free (chroot_env);
  79. return 0;
  80. }
  81. #define PREPARE prepare
  82. #include <support/test-driver.c>