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