(__bswap_32): Add cast to avoid invalid asm.
[glibc.git] / login / tst-utmp.c
blob9ff8b5ddaa139aa4293f148b56daedefa54acca8
1 /* Tests for UTMP functions.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <time.h>
27 #ifdef UTMPX
28 # include <utmpx.h>
29 # define utmp utmpx
30 # define utmpname utmpxname
31 # define setutent setutxent
32 # define getutent getutxent
33 # define endutent endutxent
34 # define getutline getutxline
35 # define getutid getutxid
36 # define pututline pututxline
37 #else
38 # include <utmp.h>
39 #endif
42 /* Prototype for our test function. */
43 static int do_test (int argc, char *argv[]);
45 /* We have a preparation function. */
46 static void do_prepare (int argc, char *argv[]);
47 #define PREPARE do_prepare
49 /* This defines the `main' function and some more. */
50 #include <test-skeleton.c>
53 /* These are for the temporary file we generate. */
54 char *name;
55 int fd;
57 static void
58 do_prepare (int argc, char *argv[])
60 size_t name_len;
62 name_len = strlen (test_dir);
63 name = malloc (name_len + sizeof ("/utmpXXXXXX"));
64 mempcpy (mempcpy (name, test_dir, name_len),
65 "/utmpXXXXXX", sizeof ("/utmpXXXXXX"));
66 add_temp_file (name);
68 /* Open our test file. */
69 fd = mkstemp (name);
70 if (fd == -1)
71 error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
75 struct utmp entry[] =
77 { ut_type: BOOT_TIME, ut_pid: 1, ut_tv: { tv_sec: 1000 } },
78 { ut_type: RUN_LVL, ut_pid: 1, ut_tv: { tv_sec: 2000 } },
79 { ut_type: INIT_PROCESS, ut_pid: 5, ut_id: "si", ut_tv: { tv_sec: 3000 } },
80 { ut_type: LOGIN_PROCESS, ut_pid: 23, ut_line: "tty1", ut_id: "1",
81 ut_user: "LOGIN", ut_session: 23, ut_tv: { tv_sec: 4000 } },
82 { ut_type: USER_PROCESS, ut_pid: 24, ut_line: "tty2", ut_id: "2",
83 ut_user: "albert", ut_session: 24, ut_tv: { tv_sec: 8000 } },
84 { ut_type: USER_PROCESS, ut_pid: 196, ut_line: "ttyp0", ut_id: "p0",
85 ut_user: "niels", ut_session: 196, ut_tv: { tv_sec: 10000 } },
86 { ut_type: DEAD_PROCESS, ut_line: "ttyp1", ut_id: "p1",
87 ut_tv: { tv_sec: 16000 } },
88 { ut_type: EMPTY },
89 { ut_type: EMPTY }
91 int num_entries = sizeof entry / sizeof (struct utmp);
93 time_t entry_time = 20000;
94 pid_t entry_pid = 234;
96 static int
97 do_init (void)
99 int n;
101 setutent ();
103 for (n = 0; n < num_entries; n++)
105 if (pututline (&entry[n]) == NULL)
107 error (0, errno, "cannot write UTMP entry");
108 return 1;
112 endutent ();
114 return 0;
118 static int
119 do_check (void)
121 struct utmp *ut;
122 int n;
124 setutent ();
126 n = 0;
127 while ((ut = getutent ()))
129 if (n < num_entries &&
130 memcmp (ut, &entry[n], sizeof (struct utmp)))
132 error (0, 0, "UTMP entry does not match");
133 return 1;
136 n++;
139 if (n != num_entries)
141 error (0, 0, "number of UTMP entries is incorrect");
142 return 1;
145 endutent ();
147 return 0;
150 static int
151 simulate_login (const char *line, const char *user)
153 int n;
155 for (n = 0; n < num_entries; n++)
157 if (strcmp (line, entry[n].ut_line) == 0 ||
158 entry[n].ut_type == DEAD_PROCESS)
160 if (entry[n].ut_pid == DEAD_PROCESS)
161 entry[n].ut_pid = (entry_pid += 27);
162 entry[n].ut_type = USER_PROCESS;
163 strcpy (entry[n].ut_user, user);
164 entry[n].ut_tv.tv_sec = (entry_time += 1000);
166 setutent ();
168 if (pututline (&entry[n]) == NULL)
170 error (0, errno, "cannot write UTMP entry");
171 return 1;
174 endutent ();
176 return 0;
180 error (0, 0, "no entries available");
181 return 1;
184 static int
185 simulate_logout (const char *line)
187 int n;
189 for (n = 0; n < num_entries; n++)
191 if (strcmp (line, entry[n].ut_line) == 0)
193 entry[n].ut_type = DEAD_PROCESS;
194 entry[n].ut_user[0] = '\0';
195 entry[n].ut_tv.tv_sec = (entry_time += 1000);
197 setutent ();
199 if (pututline (&entry[n]) == NULL)
201 error (0, errno, "cannot write UTMP entry");
202 return 1;
205 endutent ();
207 return 0;
211 error (0, 0, "no entry found for `%s'", line);
212 return 1;
215 static int
216 check_login (const char *line)
218 struct utmp *up;
219 struct utmp ut;
220 int n;
222 setutent ();
224 strcpy (ut.ut_line, line);
225 up = getutline (&ut);
226 if (up == NULL)
228 error (0, errno, "cannot get entry for line `%s'", line);
229 return 1;
232 endutent ();
234 for (n = 0; n < num_entries; n++)
236 if (strcmp (line, entry[n].ut_line) == 0)
238 if (memcmp (up, &entry[n], sizeof (struct utmp)))
240 error (0, 0, "UTMP entry does not match");
241 return 1;
244 return 0;
248 error (0, 0, "bogus entry for line `%s'", line);
249 return 1;
252 static int
253 check_logout (const char *line)
255 struct utmp ut;
257 setutent ();
259 strcpy (ut.ut_line, line);
260 if (getutline (&ut) != NULL)
262 error (0, 0, "bogus login entry for `%s'", line);
263 return 1;
266 endutent ();
268 return 0;
271 static int
272 check_id (const char *id)
274 struct utmp *up;
275 struct utmp ut;
276 int n;
278 setutent ();
280 ut.ut_type = USER_PROCESS;
281 strcpy (ut.ut_id, id);
282 up = getutid (&ut);
283 if (up == NULL)
285 error (0, errno, "cannot get entry for ID `%s'", id);
286 return 1;
289 endutent ();
291 for (n = 0; n < num_entries; n++)
293 if (strcmp (id, entry[n].ut_id) == 0)
295 if (memcmp (up, &entry[n], sizeof (struct utmp)))
297 error (0, 0, "UTMP entry does not match");
298 return 1;
301 return 0;
305 error (0, 0, "bogus entry for ID `%s'", id);
306 return 1;
309 static int
310 check_type (int type)
312 struct utmp *up;
313 struct utmp ut;
314 int n;
316 setutent ();
318 ut.ut_type = type;
319 up = getutid (&ut);
320 if (up == NULL)
322 error (0, errno, "cannot get entry for type `%d'", type);
323 return 1;
326 endutent ();
328 for (n = 0; n < num_entries; n++)
330 if (type == entry[n].ut_type)
332 if (memcmp (up, &entry[n], sizeof (struct utmp)))
334 error (0, 0, "UTMP entry does not match");
335 return 1;
338 return 0;
342 error (0, 0, "bogus entry for type `%d'", type);
343 return 1;
346 static int
347 do_test (int argc, char *argv[])
349 int result = 0;
351 utmpname (name);
353 result |= do_init ();
354 result |= do_check ();
356 result |= simulate_login ("tty1", "erwin");
357 result |= do_check ();
359 result |= simulate_login ("ttyp1", "paul");
360 result |= do_check ();
362 result |= simulate_logout ("tty2");
363 result |= do_check ();
365 result |= simulate_logout ("ttyp0");
366 result |= do_check ();
368 result |= simulate_login ("ttyp2", "richard");
369 result |= do_check ();
371 result |= check_login ("tty1");
372 result |= check_logout ("ttyp0");
373 result |= check_id ("p1");
374 result |= check_id ("2");
375 result |= check_id ("si");
376 result |= check_type (BOOT_TIME);
377 result |= check_type (RUN_LVL);
379 return result;