r23779: Change from v2 or later to v3 or later.
[jra/samba/.git] / testsuite / nsswitch / getent_pwent.c
1 /* Test out of order operations with {set,get,end}pwent */
2
3 /* 
4    Unix SMB/Netbios implementation.
5    Version 1.9.
6    Security context tests
7    Copyright (C) Tim Potter 2000
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include <stdio.h>
25 #include <pwd.h>
26
27 int main (int argc, char **argv)
28 {
29         struct passwd *pw;
30         int found = 0;
31         int num_users, i;
32
33         /* Test getpwent() without setpwent() */
34
35         for (i = 0; i < 100; i++) {
36                 pw = getpwent();
37
38                 /* This is supposed to work */
39
40 #if 0
41                 if (pw != NULL) {
42                         printf("FAIL: getpwent() with no setpwent()\n");
43                         return 1;
44                 }
45 #endif
46         }
47
48         /* Work out how many user till first domain user */
49
50         num_users = 0;
51         setpwent();
52
53         while (1) {
54                 pw = getpwent();
55                 num_users++;
56
57                 if (pw == NULL) break;
58
59                 if (strchr(pw->pw_name, '/')) {
60                         found = 1;
61                         break;
62                 }
63
64         }
65
66         if (!found) {
67                 printf("FAIL: could not find any domain users\n");
68                 return 1;
69         }
70
71         /* Test stopping getpwent in the middle of a set of users */
72
73         endpwent();
74
75         /* Test setpwent() without any getpwent() calls */
76
77         setpwent();
78
79         for (i = 0; i < (num_users - 1); i++) {
80                 getpwent();
81         }
82
83         endpwent();
84
85         /* Test lots of setpwent() calls */
86         
87         setpwent();
88
89         for (i = 0; i < (num_users - 1); i++) {
90                 getpwent();
91         }
92
93         for (i = 0; i < 100; i++) {
94                 setpwent();
95         }
96
97         /* Test lots of endpwent() calls */
98
99         setpwent();
100
101         for (i = 0; i < (num_users - 1); i++) {
102                 getpwent();
103         }
104
105         for (i = 0; i < 100; i++) {
106                 endpwent();
107         }
108
109         /* Everything's cool */
110
111         printf("PASS\n");
112         return 0;
113 }