tst-nss-test5.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Test error checking for passwd entries.
  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 <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. /* The specific values and names used here are arbitrary, other than
  23. correspondence (with suitable differences according to the tests as
  24. commented) between the given and expected entries. */
  25. static struct passwd pwd_table[] = {
  26. PWD (100), /* baseline, matches */
  27. PWD (300), /* wrong name and uid */
  28. PWD_N (200, NULL), /* missing name */
  29. PWD (60), /* unexpected name */
  30. { .pw_name = (char *)"name20000", .pw_passwd = (char *) "*", .pw_uid = 20000, \
  31. .pw_gid = 200, .pw_gecos = (char *) "*", .pw_dir = (char *) "*", \
  32. .pw_shell = (char *) "*" }, /* wrong gid */
  33. { .pw_name = (char *)"name2", .pw_passwd = (char *) "x", .pw_uid = 2, \
  34. .pw_gid = 2, .pw_gecos = (char *) "y", .pw_dir = (char *) "z", \
  35. .pw_shell = (char *) "*" }, /* spot check other text fields */
  36. PWD_LAST ()
  37. };
  38. static struct passwd exp_table[] = {
  39. PWD (100),
  40. PWD (30),
  41. PWD (200),
  42. PWD_N (60, NULL),
  43. PWD (20000),
  44. PWD (2),
  45. PWD_LAST ()
  46. };
  47. void
  48. _nss_test1_init_hook(test_tables *t)
  49. {
  50. t->pwd_table = pwd_table;
  51. }
  52. static int
  53. do_test (void)
  54. {
  55. int retval = 0;
  56. int i;
  57. struct passwd *p;
  58. __nss_configure_lookup ("passwd", "test1");
  59. setpwent ();
  60. i = 0;
  61. for (p = getpwent ();
  62. p != NULL && ! PWD_ISLAST (& exp_table[i]);
  63. ++i, p = getpwent ())
  64. retval += compare_passwds (i, p, & exp_table[i]);
  65. endpwent ();
  66. if (p)
  67. {
  68. printf ("FAIL: [?] passwd entry %u.%s unexpected\n", p->pw_uid, p->pw_name);
  69. ++retval;
  70. }
  71. if (! PWD_ISLAST (& exp_table[i]))
  72. {
  73. printf ("FAIL: [%d] passwd entry %u.%s missing\n", i,
  74. exp_table[i].pw_uid, exp_table[i].pw_name);
  75. ++retval;
  76. }
  77. #define EXPECTED 9
  78. if (retval == EXPECTED)
  79. {
  80. if (retval > 0)
  81. printf ("PASS: Found %d expected errors\n", retval);
  82. return 0;
  83. }
  84. else
  85. {
  86. printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
  87. return 1;
  88. }
  89. }
  90. #include <support/test-driver.c>