tst-nss-test3.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Test error checking for group entries.
  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 <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <support/support.h>
  20. #include "nss_test.h"
  21. /* The names here are arbitrary, but the *lengths* of the arrays is
  22. not, and groups 6 and 7 test for partial matches. */
  23. static const char *group_2[] = {
  24. "foo", "bar", NULL
  25. };
  26. static const char *group_3[] = {
  27. "tom", "dick", "harry", NULL
  28. };
  29. static const char *group_4[] = {
  30. "alpha", "beta", "gamma", "fred", NULL
  31. };
  32. static const char *group_6[] = {
  33. "larry", "curly", "moe", NULL
  34. };
  35. static const char *group_7[] = {
  36. "larry", "curly", "darryl", NULL
  37. };
  38. static const char *group_14[] = {
  39. "huey", "dewey", "louis", NULL
  40. };
  41. /* Note that we're intentionally causing mis-matches here; the purpose
  42. of this test case is to test each error check and make sure they
  43. detect the errors they check for, and to ensure that the harness
  44. can process all the error cases properly (i.e. a NULL gr_name
  45. field). We check for the correct number of mismatches at the
  46. end. */
  47. /* This is the data we're giving the service. */
  48. static struct group group_table_data[] = {
  49. GRP(4), /* match */
  50. GRP_N(8, "name6", group_6), /* wrong gid */
  51. GRP_N(14, NULL, group_14), /* missing name */
  52. GRP(14), /* unexpected name */
  53. GRP_N(7, "name7_wrong", group_7), /* wrong name */
  54. { .gr_name = (char *)"name5", .gr_passwd = (char *)"wilma", .gr_gid = 5, .gr_mem = NULL }, /* unexpected passwd */
  55. { .gr_name = (char *)"name5", .gr_passwd = NULL, .gr_gid = 5, .gr_mem = NULL }, /* missing passwd */
  56. { .gr_name = (char *)"name5", .gr_passwd = (char *)"wilma", .gr_gid = 5, .gr_mem = NULL }, /* wrong passwd */
  57. GRP_N(3, "name3a", NULL), /* missing member list */
  58. GRP_N(3, "name3b", group_3), /* unexpected member list */
  59. GRP_N(3, "name3c", group_3), /* wrong/short member list */
  60. GRP_N(3, "name3d", group_4), /* wrong/long member list */
  61. GRP_LAST ()
  62. };
  63. /* This is the data we compare against. */
  64. static struct group group_table[] = {
  65. GRP(4),
  66. GRP(6),
  67. GRP(14),
  68. GRP_N(14, NULL, group_14),
  69. GRP(7),
  70. { .gr_name = (char *)"name5", .gr_passwd = NULL, .gr_gid = 5, .gr_mem = NULL },
  71. { .gr_name = (char *)"name5", .gr_passwd = (char *)"fred", .gr_gid = 5, .gr_mem = NULL },
  72. { .gr_name = (char *)"name5", .gr_passwd = (char *)"fred", .gr_gid = 5, .gr_mem = NULL },
  73. GRP_N(3, "name3a", group_3),
  74. GRP_N(3, "name3b", NULL),
  75. GRP_N(3, "name3c", group_4),
  76. GRP_N(3, "name3d", group_3),
  77. GRP(2),
  78. GRP_LAST ()
  79. };
  80. void
  81. _nss_test1_init_hook(test_tables *t)
  82. {
  83. t->grp_table = group_table_data;
  84. }
  85. static int
  86. do_test (void)
  87. {
  88. int retval = 0;
  89. int i;
  90. struct group *g = NULL;
  91. /* Previously we used __nss_configure_lookup to isolate the test
  92. from the host environment and to get it to lookup from our new
  93. test1 NSS service module, but now this test is run in a different
  94. root filesystem via the test-container support and we directly
  95. configure the use of the test1 NSS service. */
  96. setgrent ();
  97. i = 0;
  98. for (g = getgrent () ;
  99. g != NULL && ! GRP_ISLAST(&group_table[i]);
  100. ++i, g = getgrent ())
  101. {
  102. retval += compare_groups (i, g, & group_table[i]);
  103. }
  104. endgrent ();
  105. if (g)
  106. {
  107. printf ("FAIL: [?] group entry %u.%s unexpected\n", g->gr_gid, g->gr_name);
  108. ++retval;
  109. }
  110. if (group_table[i].gr_name || group_table[i].gr_gid)
  111. {
  112. printf ("FAIL: [%d] group entry %u.%s missing\n", i,
  113. group_table[i].gr_gid, group_table[i].gr_name);
  114. ++retval;
  115. }
  116. #define EXPECTED 18
  117. if (retval == EXPECTED)
  118. {
  119. if (retval > 0)
  120. printf ("PASS: Found %d expected errors\n", retval);
  121. return 0;
  122. }
  123. else
  124. {
  125. printf ("FAIL: Found %d errors, expected %d\n", retval, EXPECTED);
  126. return 1;
  127. }
  128. }
  129. #include <support/test-driver.c>