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. */
24 #include <sys/types.h>
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
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. */
58 do_prepare (int argc
, char *argv
[])
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"));
68 /* Open our test file. */
71 error (EXIT_FAILURE
, errno
, "cannot open test file `%s'", name
);
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 } },
91 int num_entries
= sizeof entry
/ sizeof (struct utmp
);
93 time_t entry_time
= 20000;
94 pid_t entry_pid
= 234;
103 for (n
= 0; n
< num_entries
; n
++)
105 if (pututline (&entry
[n
]) == NULL
)
107 error (0, errno
, "cannot write UTMP entry");
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");
139 if (n
!= num_entries
)
141 error (0, 0, "number of UTMP entries is incorrect");
151 simulate_login (const char *line
, const char *user
)
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);
168 if (pututline (&entry
[n
]) == NULL
)
170 error (0, errno
, "cannot write UTMP entry");
180 error (0, 0, "no entries available");
185 simulate_logout (const char *line
)
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);
199 if (pututline (&entry
[n
]) == NULL
)
201 error (0, errno
, "cannot write UTMP entry");
211 error (0, 0, "no entry found for `%s'", line
);
216 check_login (const char *line
)
224 strcpy (ut
.ut_line
, line
);
225 up
= getutline (&ut
);
228 error (0, errno
, "cannot get entry for line `%s'", line
);
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");
248 error (0, 0, "bogus entry for line `%s'", line
);
253 check_logout (const char *line
)
259 strcpy (ut
.ut_line
, line
);
260 if (getutline (&ut
) != NULL
)
262 error (0, 0, "bogus login entry for `%s'", line
);
272 check_id (const char *id
)
280 ut
.ut_type
= USER_PROCESS
;
281 strcpy (ut
.ut_id
, id
);
285 error (0, errno
, "cannot get entry for ID `%s'", id
);
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");
305 error (0, 0, "bogus entry for ID `%s'", id
);
310 check_type (int type
)
322 error (0, errno
, "cannot get entry for type `%d'", type
);
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");
342 error (0, 0, "bogus entry for type `%d'", type
);
347 do_test (int argc
, char *argv
[])
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
);