tst-nss-test2.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Basic test for two passwd databases.
  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 data in these tables is arbitrary, but the merged data based on
  23. the first two tables will be compared against the expected data in
  24. the pwd_expected table, and the tests[] array. */
  25. static struct passwd pwd_table_1[] = {
  26. PWD (100),
  27. PWD (30),
  28. PWD (200),
  29. PWD (60),
  30. PWD (20000),
  31. PWD_LAST ()
  32. };
  33. static struct passwd pwd_table_2[] = {
  34. PWD (5),
  35. PWD_N(200, "name30"),
  36. PWD (16),
  37. PWD_LAST ()
  38. };
  39. void
  40. _nss_test1_init_hook(test_tables *t)
  41. {
  42. t->pwd_table = pwd_table_1;
  43. }
  44. void
  45. _nss_test2_init_hook(test_tables *t)
  46. {
  47. t->pwd_table = pwd_table_2;
  48. }
  49. static struct passwd pwd_expected[] = {
  50. PWD(100),
  51. PWD(30),
  52. PWD(200),
  53. PWD(60),
  54. PWD(20000),
  55. PWD(5),
  56. PWD_N(200, "name30"),
  57. PWD(16),
  58. PWD_LAST ()
  59. };
  60. static struct {
  61. uid_t uid;
  62. const char *name;
  63. } tests[] = {
  64. { 100, "name100" }, /* control, first db */
  65. { 16, "name16" }, /* second db */
  66. { 30, "name30" }, /* test overlaps in name */
  67. { 200, "name200" }, /* test overlaps uid */
  68. { 0, NULL }
  69. };
  70. static int
  71. do_test (void)
  72. {
  73. int retval = 0;
  74. int i;
  75. __nss_configure_lookup ("passwd", "test1 test2");
  76. setpwent ();
  77. i = 0;
  78. for (struct passwd *p = getpwent (); p != NULL; ++i, p = getpwent ())
  79. {
  80. retval += compare_passwds (i, & pwd_expected[i], p);
  81. if (p->pw_uid != pwd_expected[i].pw_uid || strcmp (p->pw_name, pwd_expected[i].pw_name) != 0)
  82. {
  83. printf ("FAIL: getpwent for %u.%s returned %u.%s\n",
  84. pwd_expected[i].pw_uid, pwd_expected[i].pw_name,
  85. p->pw_uid, p->pw_name);
  86. retval = 1;
  87. break;
  88. }
  89. }
  90. endpwent ();
  91. for (i=0; tests[i].name; i++)
  92. {
  93. struct passwd *p = getpwnam (tests[i].name);
  94. if (strcmp (p->pw_name, tests[i].name) != 0
  95. || p->pw_uid != tests[i].uid)
  96. {
  97. printf("FAIL: getpwnam for %u.%s returned %u.%s\n",
  98. tests[i].uid, tests[i].name,
  99. p->pw_uid, p->pw_name);
  100. retval = 1;
  101. }
  102. p = getpwuid (tests[i].uid);
  103. if (strcmp (p->pw_name, tests[i].name) != 0
  104. || p->pw_uid != tests[i].uid)
  105. {
  106. printf("FAIL: getpwuid for %u.%s returned %u.%s\n",
  107. tests[i].uid, tests[i].name,
  108. p->pw_uid, p->pw_name);
  109. retval = 1;
  110. }
  111. }
  112. return retval;
  113. }
  114. #include <support/test-driver.c>