tst-nss-test1.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Basic test of passwd database handling.
  2. Copyright (C) 2017-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 <nss.h>
  16. #include <pwd.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <support/support.h>
  21. #include "nss_test.h"
  22. static int hook_called = 0;
  23. /* Note: the values chosen here are arbitrary; they need only be
  24. unique within the table. However, they do need to match the
  25. "pwdids" array further down. */
  26. static struct passwd pwd_table[] = {
  27. PWD (100),
  28. PWD (30),
  29. PWD (200),
  30. PWD (60),
  31. PWD (20000),
  32. PWD_LAST ()
  33. };
  34. void
  35. _nss_test1_init_hook(test_tables *t)
  36. {
  37. hook_called = 1;
  38. t->pwd_table = pwd_table;
  39. }
  40. static int
  41. do_test (void)
  42. {
  43. int retval = 0;
  44. __nss_configure_lookup ("passwd", "test1");
  45. /* This must match the pwd_table above. */
  46. static const unsigned int pwdids[] = { 100, 30, 200, 60, 20000 };
  47. #define npwdids (sizeof (pwdids) / sizeof (pwdids[0]))
  48. setpwent ();
  49. const unsigned int *np = pwdids;
  50. for (struct passwd *p = getpwent (); p != NULL; ++np, p = getpwent ())
  51. {
  52. retval += compare_passwds (np-pwdids, p, & pwd_table[np-pwdids]);
  53. if (p->pw_uid != *np || strncmp (p->pw_name, "name", 4) != 0
  54. || atol (p->pw_name + 4) != *np)
  55. {
  56. printf ("FAIL: passwd entry %td wrong (%s, %u)\n",
  57. np - pwdids, p->pw_name, p->pw_uid);
  58. retval = 1;
  59. break;
  60. }
  61. }
  62. endpwent ();
  63. for (int i = npwdids - 1; i >= 0; --i)
  64. {
  65. char buf[30];
  66. snprintf (buf, sizeof (buf), "name%u", pwdids[i]);
  67. struct passwd *p = getpwnam (buf);
  68. if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
  69. {
  70. printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
  71. retval = 1;
  72. }
  73. p = getpwuid (pwdids[i]);
  74. if (p == NULL || p->pw_uid != pwdids[i] || strcmp (buf, p->pw_name) != 0)
  75. {
  76. printf ("FAIL: passwd entry %u wrong\n", pwdids[i]);
  77. retval = 1;
  78. }
  79. snprintf (buf, sizeof (buf), "name%u", pwdids[i] + 1);
  80. p = getpwnam (buf);
  81. if (p != NULL)
  82. {
  83. printf ("FAIL: passwd entry \"%s\" wrong\n", buf);
  84. retval = 1;
  85. }
  86. p = getpwuid (pwdids[i] + 1);
  87. if (p != NULL)
  88. {
  89. printf ("FAIL: passwd entry %u wrong\n", pwdids[i] + 1);
  90. retval = 1;
  91. }
  92. }
  93. if (!hook_called)
  94. {
  95. retval = 1;
  96. printf("FAIL: init hook never called\n");
  97. }
  98. return retval;
  99. }
  100. #include <support/test-driver.c>