tst-gshadow.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <gshadow.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. static const struct sgrp data[] =
  5. {
  6. { (char *) "one", (char *) "pwdone",
  7. (char *[]) { (char *) "admoneone", (char *) "admonetwo",
  8. (char *) "admonethree", NULL },
  9. (char *[]) { (char *) "memoneone", (char *) "memonetwo",
  10. (char *) "memonethree", NULL } },
  11. { (char *) "two", (char *) "pwdtwo",
  12. (char *[]) { (char *) "admtwoone", (char *) "admtwotwo", NULL },
  13. (char *[]) { (char *) "memtwoone", (char *) "memtwotwo",
  14. (char *) "memtwothree", NULL } },
  15. { (char *) "three", (char *) "pwdthree",
  16. (char *[]) { (char *) "admthreeone", (char *) "admthreetwo", NULL },
  17. (char *[]) { (char *) "memthreeone", (char *) "memthreetwo", NULL } },
  18. { (char *) "four", (char *) "pwdfour",
  19. (char *[]) { (char *) "admfourone", (char *) "admfourtwo", NULL },
  20. (char *[]) { NULL } },
  21. { (char *) "five", (char *) "pwdfive",
  22. (char *[]) { NULL },
  23. (char *[]) { (char *) "memfiveone", (char *) "memfivetwo", NULL } },
  24. };
  25. #define ndata (sizeof (data) / sizeof (data[0]))
  26. static int
  27. do_test (void)
  28. {
  29. FILE *fp = tmpfile ();
  30. if (fp == NULL)
  31. {
  32. puts ("cannot open temporary file");
  33. return 1;
  34. }
  35. for (size_t i = 0; i < ndata; ++i)
  36. if (putsgent (&data[i], fp) != 0)
  37. {
  38. printf ("putsgent call %zu failed\n", i + 1);
  39. return 1;
  40. }
  41. rewind (fp);
  42. int result = 0;
  43. int seen = -1;
  44. struct sgrp *g;
  45. while ((g = fgetsgent (fp)) != NULL)
  46. {
  47. ++seen;
  48. if (strcmp (g->sg_namp, data[seen].sg_namp) != 0)
  49. {
  50. printf ("sg_namp of entry %d does not match: %s vs %s\n",
  51. seen + 1, g->sg_namp, data[seen].sg_namp);
  52. result = 1;
  53. }
  54. if (strcmp (g->sg_passwd, data[seen].sg_passwd) != 0)
  55. {
  56. printf ("sg_passwd of entry %d does not match: %s vs %s\n",
  57. seen + 1, g->sg_passwd, data[seen].sg_passwd);
  58. result = 1;
  59. }
  60. if (g->sg_adm == NULL)
  61. {
  62. printf ("sg_adm of entry %d is NULL\n", seen + 1);
  63. result = 1;
  64. }
  65. else
  66. {
  67. int i = 1;
  68. char **sp1 = g->sg_adm;
  69. char **sp2 = data[seen].sg_adm;
  70. while (*sp1 != NULL && *sp2 != NULL)
  71. {
  72. if (strcmp (*sp1, *sp2) != 0)
  73. {
  74. printf ("sg_adm[%d] of entry %d does not match: %s vs %s\n",
  75. i, seen + 1, *sp1, *sp2);
  76. result = 1;
  77. }
  78. ++sp1;
  79. ++sp2;
  80. ++i;
  81. }
  82. if (*sp1 == NULL && *sp2 != NULL)
  83. {
  84. printf ("sg_adm of entry %d has too few entries\n", seen + 1);
  85. result = 1;
  86. }
  87. else if (*sp1 != NULL && *sp2 == NULL)
  88. {
  89. printf ("sg_adm of entry %d has too many entries\n", seen + 1);
  90. result = 1;
  91. }
  92. }
  93. if (g->sg_mem == NULL)
  94. {
  95. printf ("sg_mem of entry %d is NULL\n", seen + 1);
  96. result = 1;
  97. }
  98. else
  99. {
  100. int i = 1;
  101. char **sp1 = g->sg_mem;
  102. char **sp2 = data[seen].sg_mem;
  103. while (*sp1 != NULL && *sp2 != NULL)
  104. {
  105. if (strcmp (*sp1, *sp2) != 0)
  106. {
  107. printf ("sg_mem[%d] of entry %d does not match: %s vs %s\n",
  108. i, seen + 1, *sp1, *sp2);
  109. result = 1;
  110. }
  111. ++sp1;
  112. ++sp2;
  113. ++i;
  114. }
  115. if (*sp1 == NULL && *sp2 != NULL)
  116. {
  117. printf ("sg_mem of entry %d has too few entries\n", seen + 1);
  118. result = 1;
  119. }
  120. else if (*sp1 != NULL && *sp2 == NULL)
  121. {
  122. printf ("sg_mem of entry %d has too many entries\n", seen + 1);
  123. result = 1;
  124. }
  125. }
  126. }
  127. fclose (fp);
  128. return result;
  129. }
  130. #define TEST_FUNCTION do_test ()
  131. #include "../test-skeleton.c"