aboutsummaryrefslogtreecommitdiffstats
path: root/gshadow/tst-gshadow.c
blob: 8b469b723d1aecf80b0317996533c229463804d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <gshadow.h>
#include <stdio.h>
#include <string.h>


static const struct sgrp data[] =
  {
    { (char *) "one", (char *) "pwdone",
      (char *[]) { (char *) "admoneone", (char *) "admonetwo",
		  (char *) "admonethree", NULL },
      (char *[]) { (char *) "memoneone", (char *) "memonetwo",
		  (char *) "memonethree", NULL } },
    { (char *) "two", (char *) "pwdtwo",
      (char *[]) { (char *) "admtwoone", (char *) "admtwotwo", NULL },
      (char *[]) { (char *) "memtwoone", (char *) "memtwotwo",
		  (char *) "memtwothree", NULL } },
    { (char *) "three", (char *) "pwdthree",
      (char *[]) { (char *) "admthreeone", (char *) "admthreetwo", NULL },
      (char *[]) { (char *) "memthreeone", (char *) "memthreetwo", NULL } },
    { (char *) "four", (char *) "pwdfour",
      (char *[]) { (char *) "admfourone", (char *) "admfourtwo", NULL },
      (char *[]) { NULL } },
    { (char *) "five", (char *) "pwdfive",
      (char *[]) { NULL },
      (char *[]) { (char *) "memfiveone", (char *) "memfivetwo", NULL } },
  };
#define ndata (sizeof (data) / sizeof (data[0]))


static int
do_test (void)
{
  FILE *fp = tmpfile ();
  if (fp == NULL)
    {
      puts ("cannot open temporary file");
      return 1;
    }

  for (size_t i = 0; i < ndata; ++i)
    if (putsgent (&data[i], fp) != 0)
      {
	printf ("putsgent call %zu failed\n", i + 1);
	return 1;
      }

  rewind (fp);

  int result = 0;
  int seen = -1;
  struct sgrp *g;
  while ((g = fgetsgent (fp)) != NULL)
    {
      ++seen;
      if (strcmp (g->sg_namp, data[seen].sg_namp) != 0)
	{
	  printf ("sg_namp of entry %d does not match: %s vs %s\n",
		  seen + 1, g->sg_namp, data[seen].sg_namp);
	  result = 1;
	}
      if (strcmp (g->sg_passwd, data[seen].sg_passwd) != 0)
	{
	  printf ("sg_passwd of entry %d does not match: %s vs %s\n",
		  seen + 1, g->sg_passwd, data[seen].sg_passwd);
	  result = 1;
	}
      if (g->sg_adm == NULL)
	{
	  printf ("sg_adm of entry %d is NULL\n", seen + 1);
	  result = 1;
	}
      else
	{
	  int i = 1;
	  char **sp1 = g->sg_adm;
	  char **sp2 = data[seen].sg_adm;
	  while (*sp1 != NULL && *sp2 != NULL)
	    {
	      if (strcmp (*sp1, *sp2) != 0)
		{
		  printf ("sg_adm[%d] of entry %d does not match: %s vs %s\n",
			  i, seen + 1, *sp1, *sp2);
		  result = 1;
		}
	      ++sp1;
	      ++sp2;
	      ++i;
	    }
	  if (*sp1 == NULL && *sp2 != NULL)
	    {
	      printf ("sg_adm of entry %d has too few entries\n", seen + 1);
	      result = 1;
	    }
	  else if (*sp1 != NULL && *sp2 == NULL)
	    {
	      printf ("sg_adm of entry %d has too many entries\n", seen + 1);
	      result = 1;
	    }
	}
      if (g->sg_mem == NULL)
	{
	  printf ("sg_mem of entry %d is NULL\n", seen + 1);
	  result = 1;
	}
      else
	{
	  int i = 1;
	  char **sp1 = g->sg_mem;
	  char **sp2 = data[seen].sg_mem;
	  while (*sp1 != NULL && *sp2 != NULL)
	    {
	      if (strcmp (*sp1, *sp2) != 0)
		{
		  printf ("sg_mem[%d] of entry %d does not match: %s vs %s\n",
			  i, seen + 1, *sp1, *sp2);
		  result = 1;
		}
	      ++sp1;
	      ++sp2;
	      ++i;
	    }
	  if (*sp1 == NULL && *sp2 != NULL)
	    {
	      printf ("sg_mem of entry %d has too few entries\n", seen + 1);
	      result = 1;
	    }
	  else if (*sp1 != NULL && *sp2 == NULL)
	    {
	      printf ("sg_mem of entry %d has too many entries\n", seen + 1);
	      result = 1;
	    }
	}
    }

  fclose (fp);

  return result;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"