]> code.delx.au - pulseaudio/blob - src/tests/usergroup-test.c
sink-input, source-output: Add hooks for mute changes
[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 <check.h>
33
34 #include <pulsecore/usergroup.h>
35 #include <pulsecore/core-util.h>
36
37 static int load_reference_structs(struct group **gr, struct passwd **pw) {
38 setpwent();
39 *pw = getpwent();
40 endpwent();
41
42 setgrent();
43 *gr = getgrent();
44 endgrent();
45
46 return (*gr && *pw) ? 0 : 1;
47 }
48
49 static int compare_group(const struct group *a, const struct group *b) {
50 char **amem, **bmem;
51
52 if (!pa_streq(a->gr_name, b->gr_name)) {
53 fprintf(stderr, "Group name mismatch: [%s] [%s]\n", a->gr_name, b->gr_name);
54 return 1;
55 }
56
57 if (!pa_streq(a->gr_passwd, b->gr_passwd)) {
58 fprintf(stderr, "Group password mismatch: [%s] [%s]\n", a->gr_passwd, b->gr_passwd);
59 return 1;
60 }
61
62 if (a->gr_gid != b->gr_gid) {
63 fprintf(stderr, "Gid mismatch: [%lu] [%lu]\n", (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 (!pa_streq(*amem, *bmem)) {
70 fprintf(stderr, "Group member mismatch: [%s] [%s]\n", *amem, *bmem);
71 return 1;
72 }
73 }
74
75 if (*amem || *bmem) {
76 fprintf(stderr, "Mismatched group count\n");
77 return 1;
78 }
79
80 return 0;
81 }
82
83 static int compare_passwd(const struct passwd *a, const struct passwd *b) {
84 if (!pa_streq(a->pw_name, b->pw_name)) {
85 fprintf(stderr, "pw_name mismatch: [%s] [%s]\n", a->pw_name, b->pw_name);
86 return 1;
87 }
88
89 if (!pa_streq(a->pw_passwd, b->pw_passwd)) {
90 fprintf(stderr, "pw_passwd mismatch: [%s] [%s]\n", a->pw_passwd, b->pw_passwd);
91 return 1;
92 }
93
94 if (a->pw_uid != b->pw_uid) {
95 fprintf(stderr, "pw_uid mismatch: [%lu] [%lu]\n", (unsigned long) a->pw_uid, (unsigned long) b->pw_uid);
96 return 1;
97 }
98
99 if (a->pw_gid != b->pw_gid) {
100 fprintf(stderr, "pw_gid mismatch: [%lu] [%lu]\n", (unsigned long) a->pw_gid, (unsigned long) b->pw_gid);
101 return 1;
102 }
103
104 if (!pa_streq(a->pw_gecos, b->pw_gecos)) {
105 fprintf(stderr, "pw_gecos mismatch: [%s] [%s]\n", a->pw_gecos, b->pw_gecos);
106 return 1;
107 }
108
109 if (!pa_streq(a->pw_dir, b->pw_dir)) {
110 fprintf(stderr, "pw_dir mismatch: [%s] [%s]\n", a->pw_dir, b->pw_dir);
111 return 1;
112 }
113
114 if (!pa_streq(a->pw_shell, b->pw_shell)) {
115 fprintf(stderr, "pw_shell mismatch: [%s] [%s]\n", a->pw_shell, b->pw_shell);
116 return 1;
117 }
118
119 return 0;
120 }
121
122 START_TEST (usergroup_test) {
123 struct group *gr;
124 struct passwd *pw;
125 struct group *reference_group = NULL;
126 struct passwd *reference_passwd = NULL;
127
128 fail_if(load_reference_structs(&reference_group, &reference_passwd));
129
130 errno = 0;
131 gr = pa_getgrgid_malloc(reference_group->gr_gid);
132 fail_if(compare_group(reference_group, gr));
133 pa_getgrgid_free(gr);
134
135 errno = 0;
136 gr = pa_getgrnam_malloc(reference_group->gr_name);
137 fail_if(compare_group(reference_group, gr));
138 pa_getgrnam_free(gr);
139
140 errno = 0;
141 pw = pa_getpwuid_malloc(reference_passwd->pw_uid);
142 fail_if(compare_passwd(reference_passwd, pw));
143 pa_getpwuid_free(pw);
144
145 errno = 0;
146 pw = pa_getpwnam_malloc(reference_passwd->pw_name);
147 fail_if(compare_passwd(reference_passwd, pw));
148 pa_getpwnam_free(pw);
149 }
150 END_TEST
151
152 int main(int argc, char *argv[]) {
153 int failed = 0;
154 Suite *s;
155 TCase *tc;
156 SRunner *sr;
157
158 s = suite_create("Usergroup");
159 tc = tcase_create("usergroup");
160 tcase_add_test(tc, usergroup_test);
161 suite_add_tcase(s, tc);
162
163 sr = srunner_create(s);
164 srunner_run_all(sr, CK_NORMAL);
165 failed = srunner_ntests_failed(sr);
166 srunner_free(sr);
167
168 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
169 }