Update.
[glibc.git] / login / tst-utmp.c
blob8497067eb30ac06f066ebc4fd8a39b06e636f39d
1 /* Tests for UTMP functions.
2 Copyright (C) 1998, 2001 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);
74 struct utmp entry[] =
76 #if _HAVE_UT_TV || defined UTMPX
77 #define UT(a) ut_tv:{tv_sec:(a)}
78 #else
79 #define UT(a) ut_time:(a)
80 #endif
82 { ut_type: BOOT_TIME, ut_pid: 1, UT(1000) },
83 { ut_type: RUN_LVL, ut_pid: 1, UT(2000) },
84 { ut_type: INIT_PROCESS, ut_pid: 5, ut_id: "si", UT(3000) },
85 { ut_type: LOGIN_PROCESS, ut_pid: 23, ut_line: "tty1", ut_id: "1",
86 ut_user: "LOGIN", UT(4000) },
87 { ut_type: USER_PROCESS, ut_pid: 24, ut_line: "tty2", ut_id: "2",
88 ut_user: "albert", UT(8000) },
89 { ut_type: USER_PROCESS, ut_pid: 196, ut_line: "ttyp0", ut_id: "p0",
90 ut_user: "niels", UT(10000) },
91 { ut_type: DEAD_PROCESS, ut_line: "ttyp1", ut_id: "p1", UT(16000) },
92 { ut_type: EMPTY },
93 { ut_type: EMPTY }
95 int num_entries = sizeof entry / sizeof (struct utmp);
97 time_t entry_time = 20000;
98 pid_t entry_pid = 234;
100 static int
101 do_init (void)
103 int n;
105 setutent ();
107 for (n = 0; n < num_entries; n++)
109 if (pututline (&entry[n]) == NULL)
111 error (0, errno, "cannot write UTMP entry");
112 return 1;
116 endutent ();
118 return 0;
122 static int
123 do_check (void)
125 struct utmp *ut;
126 int n;
128 setutent ();
130 n = 0;
131 while ((ut = getutent ()))
133 if (n < num_entries &&
134 memcmp (ut, &entry[n], sizeof (struct utmp)))
136 error (0, 0, "UTMP entry does not match");
137 return 1;
140 n++;
143 if (n != num_entries)
145 error (0, 0, "number of UTMP entries is incorrect");
146 return 1;
149 endutent ();
151 return 0;
154 static int
155 simulate_login (const char *line, const char *user)
157 int n;
159 for (n = 0; n < num_entries; n++)
161 if (strcmp (line, entry[n].ut_line) == 0 ||
162 entry[n].ut_type == DEAD_PROCESS)
164 if (entry[n].ut_pid == DEAD_PROCESS)
165 entry[n].ut_pid = (entry_pid += 27);
166 entry[n].ut_type = USER_PROCESS;
167 strcpy (entry[n].ut_user, user);
168 #if _HAVE_UT_TV - 0 || defined UTMPX
169 entry[n].ut_tv.tv_sec = (entry_time += 1000);
170 #else
171 entry[n].ut_time = (entry_time += 1000);
172 #endif
173 setutent ();
175 if (pututline (&entry[n]) == NULL)
177 error (0, errno, "cannot write UTMP entry");
178 return 1;
181 endutent ();
183 return 0;
187 error (0, 0, "no entries available");
188 return 1;
191 static int
192 simulate_logout (const char *line)
194 int n;
196 for (n = 0; n < num_entries; n++)
198 if (strcmp (line, entry[n].ut_line) == 0)
200 entry[n].ut_type = DEAD_PROCESS;
201 entry[n].ut_user[0] = '\0';
202 #if _HAVE_UT_TV - 0 || defined UTMPX
203 entry[n].ut_tv.tv_sec = (entry_time += 1000);
204 #else
205 entry[n].ut_time = (entry_time += 1000);
206 #endif
207 setutent ();
209 if (pututline (&entry[n]) == NULL)
211 error (0, errno, "cannot write UTMP entry");
212 return 1;
215 endutent ();
217 return 0;
221 error (0, 0, "no entry found for `%s'", line);
222 return 1;
225 static int
226 check_login (const char *line)
228 struct utmp *up;
229 struct utmp ut;
230 int n;
232 setutent ();
234 strcpy (ut.ut_line, line);
235 up = getutline (&ut);
236 if (up == NULL)
238 error (0, errno, "cannot get entry for line `%s'", line);
239 return 1;
242 endutent ();
244 for (n = 0; n < num_entries; n++)
246 if (strcmp (line, entry[n].ut_line) == 0)
248 if (memcmp (up, &entry[n], sizeof (struct utmp)))
250 error (0, 0, "UTMP entry does not match");
251 return 1;
254 return 0;
258 error (0, 0, "bogus entry for line `%s'", line);
259 return 1;
262 static int
263 check_logout (const char *line)
265 struct utmp ut;
267 setutent ();
269 strcpy (ut.ut_line, line);
270 if (getutline (&ut) != NULL)
272 error (0, 0, "bogus login entry for `%s'", line);
273 return 1;
276 endutent ();
278 return 0;
281 static int
282 check_id (const char *id)
284 struct utmp *up;
285 struct utmp ut;
286 int n;
288 setutent ();
290 ut.ut_type = USER_PROCESS;
291 strcpy (ut.ut_id, id);
292 up = getutid (&ut);
293 if (up == NULL)
295 error (0, errno, "cannot get entry for ID `%s'", id);
296 return 1;
299 endutent ();
301 for (n = 0; n < num_entries; n++)
303 if (strcmp (id, entry[n].ut_id) == 0)
305 if (memcmp (up, &entry[n], sizeof (struct utmp)))
307 error (0, 0, "UTMP entry does not match");
308 return 1;
311 return 0;
315 error (0, 0, "bogus entry for ID `%s'", id);
316 return 1;
319 static int
320 check_type (int type)
322 struct utmp *up;
323 struct utmp ut;
324 int n;
326 setutent ();
328 ut.ut_type = type;
329 up = getutid (&ut);
330 if (up == NULL)
332 error (0, errno, "cannot get entry for type `%d'", type);
333 return 1;
336 endutent ();
338 for (n = 0; n < num_entries; n++)
340 if (type == entry[n].ut_type)
342 if (memcmp (up, &entry[n], sizeof (struct utmp)))
344 error (0, 0, "UTMP entry does not match");
345 return 1;
348 return 0;
352 error (0, 0, "bogus entry for type `%d'", type);
353 return 1;
356 static int
357 do_test (int argc, char *argv[])
359 int result = 0;
361 utmpname (name);
363 result |= do_init ();
364 result |= do_check ();
366 result |= simulate_login ("tty1", "erwin");
367 result |= do_check ();
369 result |= simulate_login ("ttyp1", "paul");
370 result |= do_check ();
372 result |= simulate_logout ("tty2");
373 result |= do_check ();
375 result |= simulate_logout ("ttyp0");
376 result |= do_check ();
378 result |= simulate_login ("ttyp2", "richard");
379 result |= do_check ();
381 result |= check_login ("tty1");
382 result |= check_logout ("ttyp0");
383 result |= check_id ("p1");
384 result |= check_id ("2");
385 result |= check_id ("si");
386 result |= check_type (BOOT_TIME);
387 result |= check_type (RUN_LVL);
389 return result;