]> code.delx.au - pulseaudio/blob - src/tests/usergroup-test.c
a48b016d28b171d2a1e86e6caef2725a3c4d1916
[pulseaudio] / src / tests / usergroup-test.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Ted Percival
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <pwd.h>
29 #include <grp.h>
30 #include <errno.h>
31
32 #include <pulsecore/usergroup.h>
33
34 static int load_reference_structs(struct group **gr, struct passwd **pw) {
35 setpwent();
36 *pw = getpwent();
37 endpwent();
38
39 setgrent();
40 *gr = getgrent();
41 endgrent();
42
43 return (*gr && *pw) ? 0 : 1;
44 }
45
46 static int compare_group(const struct group *a, const struct group *b) {
47 char **amem, **bmem;
48
49 if (strcmp(a->gr_name, b->gr_name)) {
50 fprintf(stderr, "Group name mismatch: [%s] [%s]\n",
51 a->gr_name, b->gr_name);
52 return 1;
53 }
54
55 if (strcmp(a->gr_passwd, b->gr_passwd)) {
56 fprintf(stderr, "Group password mismatch: [%s] [%s]\n",
57 a->gr_passwd, b->gr_passwd);
58 return 1;
59 }
60
61 if (a->gr_gid != b->gr_gid) {
62 fprintf(stderr, "Gid mismatch: [%lu] [%lu]\n",
63 (unsigned long) a->gr_gid, (unsigned long) b->gr_gid);
64 return 1;
65 }
66
67 /* XXX: Assuming the group ordering is identical. */
68 for (amem = a->gr_mem, bmem = b->gr_mem; *amem && *bmem; ++amem, ++bmem) {
69 if (strcmp(*amem, *bmem)) {
70 fprintf(stderr, "Group member mismatch: [%s] [%s]\n",
71 *amem, *bmem);
72 return 1;
73 }
74 }
75
76 if (*amem || *bmem) {
77 fprintf(stderr, "Mismatched group count\n");
78 return 1;
79 }
80
81 return 0;
82 }
83
84 static int compare_passwd(const struct passwd *a, const struct passwd *b) {
85 if (strcmp(a->pw_name, b->pw_name)) {
86 fprintf(stderr, "pw_name mismatch: [%s] [%s]\n", a->pw_name, b->pw_name);
87 return 1;
88 }
89
90 if (strcmp(a->pw_passwd, b->pw_passwd)) {
91 fprintf(stderr, "pw_passwd mismatch: [%s] [%s]\n", a->pw_passwd, b->pw_passwd);
92 return 1;
93 }
94
95 if (a->pw_uid != b->pw_uid) {
96 fprintf(stderr, "pw_uid mismatch: [%lu] [%lu]\n",
97 (unsigned long) a->pw_uid, (unsigned long) b->pw_uid);
98 return 1;
99 }
100
101 if (a->pw_gid != b->pw_gid) {
102 fprintf(stderr, "pw_gid mismatch: [%lu] [%lu]\n",
103 (unsigned long) a->pw_gid, (unsigned long) b->pw_gid);
104 return 1;
105 }
106
107 if (strcmp(a->pw_gecos, b->pw_gecos)) {
108 fprintf(stderr, "pw_gecos mismatch: [%s] [%s]\n", a->pw_gecos, b->pw_gecos);
109 return 1;
110 }
111
112 if (strcmp(a->pw_dir, b->pw_dir)) {
113 fprintf(stderr, "pw_dir mismatch: [%s] [%s]\n", a->pw_dir, b->pw_dir);
114 return 1;
115 }
116
117 if (strcmp(a->pw_shell, b->pw_shell)) {
118 fprintf(stderr, "pw_shell mismatch: [%s] [%s]\n", a->pw_shell, b->pw_shell);
119 return 1;
120 }
121
122 return 0;
123 }
124
125 int main(int argc, char *argv[]) {
126 struct group *gr;
127 struct passwd *pw;
128 int err;
129 struct group *reference_group = NULL;
130 struct passwd *reference_passwd = NULL;
131
132 err = load_reference_structs(&reference_group, &reference_passwd);
133 if (err)
134 return 77;
135
136 errno = 0;
137 gr = pa_getgrgid_malloc(reference_group->gr_gid);
138 if (compare_group(reference_group, gr))
139 return 1;
140 pa_getgrgid_free(gr);
141
142 errno = 0;
143 gr = pa_getgrnam_malloc(reference_group->gr_name);
144 if (compare_group(reference_group, gr))
145 return 1;
146 pa_getgrnam_free(gr);
147
148 errno = 0;
149 pw = pa_getpwuid_malloc(reference_passwd->pw_uid);
150 if (compare_passwd(reference_passwd, pw))
151 return 1;
152 pa_getpwuid_free(pw);
153
154 errno = 0;
155 pw = pa_getpwnam_malloc(reference_passwd->pw_name);
156 if (compare_passwd(reference_passwd, pw))
157 return 1;
158 pa_getpwnam_free(pw);
159
160 return 0;
161 }