From cbce5abb876a050cfdd01dc44d8474ec34fa2519 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Wed, 15 Feb 2006 05:26:34 +0000 Subject: grab some tests from glibc --- test/misc/bug-readdir1.c | 38 +++++ test/misc/opendir-tst1.c | 95 +++++++++++ test/misc/tst-regex2.c | 244 ++++++++++++++++++++++++++++ test/misc/tst-regexloc.c | 48 ++++++ test/misc/tst-seekdir.c | 80 ++++++++++ test/misc/tst-utmp.c | 404 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 909 insertions(+) create mode 100644 test/misc/bug-readdir1.c create mode 100644 test/misc/opendir-tst1.c create mode 100644 test/misc/tst-regex2.c create mode 100644 test/misc/tst-regexloc.c create mode 100644 test/misc/tst-seekdir.c create mode 100644 test/misc/tst-utmp.c (limited to 'test/misc') diff --git a/test/misc/bug-readdir1.c b/test/misc/bug-readdir1.c new file mode 100644 index 000000000..4c3521dbb --- /dev/null +++ b/test/misc/bug-readdir1.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include +#include + + +int +main (void) +{ + DIR *dirp; + struct dirent* ent; + + /* open a dir stream */ + dirp = opendir ("/tmp"); + if (dirp == NULL) + { + if (errno == ENOENT) + exit (0); + + perror ("opendir"); + exit (1); + } + + /* close the directory file descriptor, making it invalid */ + if (close (dirfd (dirp)) != 0) + { + puts ("could not close directory file descriptor"); + /* This is not an error. It is not guaranteed this is possible. */ + return 0; + } + + ent = readdir (dirp); + + return ent != NULL || errno != EBADF; +} diff --git a/test/misc/opendir-tst1.c b/test/misc/opendir-tst1.c new file mode 100644 index 000000000..983d4b482 --- /dev/null +++ b/test/misc/opendir-tst1.c @@ -0,0 +1,95 @@ +/* Copyright (C) 1998, 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1998. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include +#include +#include +#include +#include +#include +#include + +/* Name of the FIFO. */ +char tmpname[] = "fifoXXXXXX"; + + +/* Do the real work. */ +static int +real_test (void) +{ + DIR *dirp; + + /* This should not block for an FIFO. */ + dirp = opendir (tmpname); + + /* Successful. */ + if (dirp != NULL) + { + /* Oh, oh, how can this work? */ + fputs ("`opendir' succeeded on a FIFO???\n", stdout); + closedir (dirp); + return 1; + } + + if (errno != ENOTDIR) + { + fprintf (stdout, "`opendir' return error `%s' instead of `%s'\n", + strerror (errno), strerror (ENOTDIR)); + return 1; + } + + return 0; +} + + +static int +do_test (int argc, char *argv[]) +{ + int retval; + + retval = mkstemp(tmpname); + close(retval); + unlink(tmpname); + + /* Try to generate a FIFO. */ + if (mknod (tmpname, 0600 | S_IFIFO, 0) < 0) + { + perror ("mknod"); + /* We cannot make this an error. */ + return 0; + } + + retval = real_test (); + + remove (tmpname); + + return retval; +} + + +static void +do_cleanup (void) +{ + remove (tmpname); +} +#define CLEANUP_HANDLER do_cleanup () + + +/* Include the test skeleton. */ +#include diff --git a/test/misc/tst-regex2.c b/test/misc/tst-regex2.c new file mode 100644 index 000000000..94be59d85 --- /dev/null +++ b/test/misc/tst-regex2.c @@ -0,0 +1,244 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#undef _POSIX_CPUTIME +#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0 +static clockid_t cl; +static int use_clock; +#endif + +static int +do_test (void) +{ +#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0 +# if _POSIX_CPUTIME == 0 + if (sysconf (_SC_CPUTIME) < 0) + use_clock = 0; + else +# endif + /* See whether we can use the CPU clock. */ + use_clock = clock_getcpuclockid (0, &cl) == 0; +#endif + + static const char *pat[] = { + ".?.?.?.?.?.?.?Log\\.13", + "(.?)(.?)(.?)(.?)(.?)(.?)(.?)Log\\.13", + "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))" + "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))" + "((((((((((.?))))))))))Log\\.13" }; + + int fd = open (".regex.ChangeLog.14", O_RDONLY); + if (fd < 0) + { + printf ("Couldn't open .regex.ChangeLog.14: %s\n", strerror(errno)); + return 1; + } + + struct stat64 st; + if (fstat64 (fd, &st) < 0) + { + printf ("Couldn't fstat ChangeLog.14: %s\n", strerror(errno)); + return 1; + } + + char *buf = malloc (st.st_size + 1); + if (buf == NULL) + { + printf ("Couldn't allocate buffer: %s\n", strerror(errno)); + return 1; + } + + if (read (fd, buf, st.st_size) != (ssize_t) st.st_size) + { + puts ("Couldn't read ChangeLog.14"); + return 1; + } + + close (fd); + buf[st.st_size] = '\0'; + +#ifdef __UCLIBC_HAS_XLOCALE__ + setlocale (LC_ALL, "de_DE.UTF-8"); +#endif + + char *string = buf; + size_t len = st.st_size; + + for (int testno = 0; testno < 4; ++testno) + for (int i = 0; i < sizeof (pat) / sizeof (pat[0]); ++i) + { + printf ("test %d pattern %d", testno, i); + + regex_t rbuf; + struct re_pattern_buffer rpbuf; + int err; + if (testno < 2) + { + err = regcomp (&rbuf, pat[i], + REG_EXTENDED | (testno ? REG_NOSUB : 0)); + if (err != 0) + { + putchar ('\n'); + char errstr[300]; + regerror (err, &rbuf, errstr, sizeof (errstr)); + puts (errstr); + return err; + } + } + else + { + re_set_syntax (RE_SYNTAX_POSIX_EGREP + | (testno == 3 ? RE_NO_SUB : 0)); + + memset (&rpbuf, 0, sizeof (rpbuf)); + const char *s = re_compile_pattern (pat[i], strlen (pat[i]), + &rpbuf); + if (s != NULL) + { + printf ("\n%s\n", s); + return 1; + } + + /* Just so that this can be tested with earlier glibc as well. */ + if (testno == 3) + rpbuf.no_sub = 1; + } + +#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0 + struct timespec start, stop; + if (use_clock) + use_clock = clock_gettime (cl, &start) == 0; +#endif + + if (testno < 2) + { + regmatch_t pmatch[71]; + err = regexec (&rbuf, string, 71, pmatch, 0); + if (err == REG_NOMATCH) + { + puts ("\nregexec failed"); + return 1; + } + + if (testno == 0) + { + if (pmatch[0].rm_eo != pmatch[0].rm_so + 13 + || pmatch[0].rm_eo > len + || pmatch[0].rm_so < len - 100 + || strncmp (string + pmatch[0].rm_so, + " ChangeLog.13 for earlier changes", + sizeof " ChangeLog.13 for earlier changes" - 1) + != 0) + { + puts ("\nregexec without REG_NOSUB did not find the correct match"); + return 1; + } + + if (i > 0) + for (int j = 0, l = 1; j < 7; ++j) + for (int k = 0; k < (i == 1 ? 1 : 10); ++k, ++l) + if (pmatch[l].rm_so != pmatch[0].rm_so + j + || pmatch[l].rm_eo != pmatch[l].rm_so + 1) + { + printf ("\npmatch[%d] incorrect\n", l); + return 1; + } + } + } + else + { + struct re_registers regs; + + memset (®s, 0, sizeof (regs)); + int match = re_search (&rpbuf, string, len, 0, len, + ®s); + if (match < 0) + { + puts ("\nre_search failed"); + return 1; + } + + if (match + 13 > len + || match < len - 100 + || strncmp (string + match, + " ChangeLog.13 for earlier changes", + sizeof " ChangeLog.13 for earlier changes" - 1) + != 0) + { + puts ("\nre_search did not find the correct match"); + return 1; + } + + if (testno == 2) + { + if (regs.num_regs != 2 + (i == 0 ? 0 : i == 1 ? 7 : 70)) + { + printf ("\nincorrect num_regs %d\n", regs.num_regs); + return 1; + } + + if (regs.start[0] != match || regs.end[0] != match + 13) + { + printf ("\nincorrect regs.{start,end}[0] = { %d, %d}\n", + regs.start[0], regs.end[0]); + return 1; + } + + if (regs.start[regs.num_regs - 1] != -1 + || regs.end[regs.num_regs - 1] != -1) + { + puts ("\nincorrect regs.{start,end}[num_regs - 1]"); + return 1; + } + + if (i > 0) + for (int j = 0, l = 1; j < 7; ++j) + for (int k = 0; k < (i == 1 ? 1 : 10); ++k, ++l) + if (regs.start[l] != match + j + || regs.end[l] != regs.start[l] + 1) + { + printf ("\nregs.{start,end}[%d] incorrect\n", l); + return 1; + } + } + } + +#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0 + if (use_clock) + use_clock = clock_gettime (cl, &stop) == 0; + if (use_clock) + { + stop.tv_sec -= start.tv_sec; + if (stop.tv_nsec < start.tv_nsec) + { + stop.tv_sec--; + stop.tv_nsec += 1000000000 - start.tv_nsec; + } + else + stop.tv_nsec -= start.tv_nsec; + printf (": %ld.%09lds\n", (long) stop.tv_sec, (long) stop.tv_nsec); + } + else +#endif + putchar ('\n'); + + if (testno < 2) + regfree (&rbuf); + else + regfree (&rpbuf); + } + + return 0; +} + +#define TIMEOUT 20 +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/test/misc/tst-regexloc.c b/test/misc/tst-regexloc.c new file mode 100644 index 000000000..034fee706 --- /dev/null +++ b/test/misc/tst-regexloc.c @@ -0,0 +1,48 @@ +/* Copyright (C) 2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include +#include +#include +#include + +int +main (int argc, char *argv[]) +{ +#ifdef __UCLIBC_HAS_XLOCALE__ + regex_t re; + regmatch_t mat[1]; + int res = 1; + + if (setlocale (LC_ALL, "de_DE.ISO-8859-1") == NULL) + puts ("cannot set locale"); + else if (regcomp (&re, "[a-f]*", 0) != REG_NOERROR) + puts ("cannot compile expression \"[a-f]*\""); + else if (regexec (&re, "abcdefCDEF", 1, mat, 0) == REG_NOMATCH) + puts ("no match"); + else + { + printf ("match from %d to %d\n", mat[0].rm_so, mat[0].rm_eo); + res = mat[0].rm_so != 0 || mat[0].rm_eo != 6; + } + + return res; +#else + return 0; +#endif +} diff --git a/test/misc/tst-seekdir.c b/test/misc/tst-seekdir.c new file mode 100644 index 000000000..5a2810abd --- /dev/null +++ b/test/misc/tst-seekdir.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +int +main (int argc, char *argv[]) +{ + DIR * dirp; + long int save3 = 0; + long int cur; + int i = 0; + int result = 0; + struct dirent *dp; + long int save0; + long int rewind; + + dirp = opendir ("."); + if (dirp == NULL) + { + printf ("opendir failed: %s\n", strerror(errno)); + return 1; + } + + save0 = telldir (dirp); + if (save0 == -1) + { + printf ("telldir failed: %s\n", strerror(errno)); + result = 1; + } + + for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp)) + { + /* save position 3 (after fourth entry) */ + if (i++ == 3) + save3 = telldir (dirp); + + printf ("%s\n", dp->d_name); + + /* stop at 400 (just to make sure dirp->__offset and dirp->__size are + scrambled */ + if (i == 400) + break; + } + + printf ("going back past 4-th entry...\n"); + + /* go back to saved entry */ + seekdir (dirp, save3); + + /* Check whether telldir equals to save3 now. */ + cur = telldir (dirp); + if (cur != save3) + { + printf ("seekdir (d, %ld); telldir (d) == %ld\n", save3, cur); + result = 1; + } + + /* print remaining files (3-last) */ + for (dp = readdir (dirp); dp != NULL; dp = readdir (dirp)) + printf ("%s\n", dp->d_name); + + /* Check rewinddir */ + rewinddir (dirp); + rewind = telldir (dirp); + if (rewind == -1) + { + printf ("telldir failed: %s\n", strerror(errno)); + result = 1; + } + else if (save0 != rewind) + { + printf ("rewinddir didn't reset directory stream\n"); + result = 1; + } + + closedir (dirp); + return result; +} diff --git a/test/misc/tst-utmp.c b/test/misc/tst-utmp.c new file mode 100644 index 000000000..12a208855 --- /dev/null +++ b/test/misc/tst-utmp.c @@ -0,0 +1,404 @@ +/* Tests for UTMP functions. + Copyright (C) 1998, 2001-2003 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Mark Kettenis , 1998. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include +#include +#include +#include +#include +#include + +#ifdef UTMPX +# include +# define utmp utmpx +# define utmpname utmpxname +# define setutent setutxent +# define getutent getutxent +# define endutent endutxent +# define getutline getutxline +# define getutid getutxid +# define pututline pututxline +#else +# include +#endif + + +#if _HAVE_UT_TYPE || defined UTMPX + +/* Prototype for our test function. */ +static int do_test (int argc, char *argv[]); + +/* We have a preparation function. */ +static void do_prepare (int argc, char *argv[]); +#define PREPARE do_prepare + +/* This defines the `main' function and some more. */ +#include + + +/* These are for the temporary file we generate. */ +char *name; +int fd; + +static void +do_prepare (int argc, char *argv[]) +{ + size_t name_len; + + name_len = strlen (test_dir); + name = malloc (name_len + sizeof ("/utmpXXXXXX")); + mempcpy (mempcpy (name, test_dir, name_len), + "/utmpXXXXXX", sizeof ("/utmpXXXXXX")); + add_temp_file (name); + + /* Open our test file. */ + fd = mkstemp (name); + if (fd == -1) + error (EXIT_FAILURE, errno, "cannot open test file `%s'", name); +} + +struct utmp entry[] = +{ +#if _HAVE_UT_TV || defined UTMPX +#define UT(a) .ut_tv = { .tv_sec = (a)} +#else +#define UT(a) .ut_time = (a) +#endif + + { .ut_type = BOOT_TIME, .ut_pid = 1, UT(1000) }, + { .ut_type = RUN_LVL, .ut_pid = 1, UT(2000) }, + { .ut_type = INIT_PROCESS, .ut_pid = 5, .ut_id = "si", UT(3000) }, + { .ut_type = LOGIN_PROCESS, .ut_pid = 23, .ut_line = "tty1", .ut_id = "1", + .ut_user = "LOGIN", UT(4000) }, + { .ut_type = USER_PROCESS, .ut_pid = 24, .ut_line = "tty2", .ut_id = "2", + .ut_user = "albert", UT(8000) }, + { .ut_type = USER_PROCESS, .ut_pid = 196, .ut_line = "ttyp0", .ut_id = "p0", + .ut_user = "niels", UT(10000) }, + { .ut_type = DEAD_PROCESS, .ut_line = "ttyp1", .ut_id = "p1", UT(16000) }, + { .ut_type = EMPTY }, + { .ut_type = EMPTY } +}; +int num_entries = sizeof entry / sizeof (struct utmp); + +time_t entry_time = 20000; +pid_t entry_pid = 234; + +static int +do_init (void) +{ + int n; + + setutent (); + + for (n = 0; n < num_entries; n++) + { + if (pututline (&entry[n]) == NULL) + { + error (0, errno, "cannot write UTMP entry"); + return 1; + } + } + + endutent (); + + return 0; +} + + +static int +do_check (void) +{ + struct utmp *ut; + int n; + + setutent (); + + n = 0; + while ((ut = getutent ())) + { + if (n < num_entries && + memcmp (ut, &entry[n], sizeof (struct utmp))) + { + error (0, 0, "UTMP entry does not match"); + return 1; + } + + n++; + } + + if (n != num_entries) + { + error (0, 0, "number of UTMP entries is incorrect"); + return 1; + } + + endutent (); + + return 0; +} + +static int +simulate_login (const char *line, const char *user) +{ + int n; + + for (n = 0; n < num_entries; n++) + { + if (strcmp (line, entry[n].ut_line) == 0 || + entry[n].ut_type == DEAD_PROCESS) + { + if (entry[n].ut_pid == DEAD_PROCESS) + entry[n].ut_pid = (entry_pid += 27); + entry[n].ut_type = USER_PROCESS; + strncpy (entry[n].ut_user, user, sizeof (entry[n].ut_user)); +#if _HAVE_UT_TV - 0 || defined UTMPX + entry[n].ut_tv.tv_sec = (entry_time += 1000); +#else + entry[n].ut_time = (entry_time += 1000); +#endif + setutent (); + + if (pututline (&entry[n]) == NULL) + { + error (0, errno, "cannot write UTMP entry"); + return 1; + } + + endutent (); + + return 0; + } + } + + error (0, 0, "no entries available"); + return 1; +} + +static int +simulate_logout (const char *line) +{ + int n; + + for (n = 0; n < num_entries; n++) + { + if (strcmp (line, entry[n].ut_line) == 0) + { + entry[n].ut_type = DEAD_PROCESS; + strncpy (entry[n].ut_user, "", sizeof (entry[n].ut_user)); +#if _HAVE_UT_TV - 0 || defined UTMPX + entry[n].ut_tv.tv_sec = (entry_time += 1000); +#else + entry[n].ut_time = (entry_time += 1000); +#endif + setutent (); + + if (pututline (&entry[n]) == NULL) + { + error (0, errno, "cannot write UTMP entry"); + return 1; + } + + endutent (); + + return 0; + } + } + + error (0, 0, "no entry found for `%s'", line); + return 1; +} + +static int +check_login (const char *line) +{ + struct utmp *up; + struct utmp ut; + int n; + + setutent (); + + strcpy (ut.ut_line, line); + up = getutline (&ut); + if (up == NULL) + { + error (0, errno, "cannot get entry for line `%s'", line); + return 1; + } + + endutent (); + + for (n = 0; n < num_entries; n++) + { + if (strcmp (line, entry[n].ut_line) == 0) + { + if (memcmp (up, &entry[n], sizeof (struct utmp))) + { + error (0, 0, "UTMP entry does not match"); + return 1; + } + + return 0; + } + } + + error (0, 0, "bogus entry for line `%s'", line); + return 1; +} + +static int +check_logout (const char *line) +{ + struct utmp ut; + + setutent (); + + strcpy (ut.ut_line, line); + if (getutline (&ut) != NULL) + { + error (0, 0, "bogus login entry for `%s'", line); + return 1; + } + + endutent (); + + return 0; +} + +static int +check_id (const char *id) +{ + struct utmp *up; + struct utmp ut; + int n; + + setutent (); + + ut.ut_type = USER_PROCESS; + strcpy (ut.ut_id, id); + up = getutid (&ut); + if (up == NULL) + { + error (0, errno, "cannot get entry for ID `%s'", id); + return 1; + } + + endutent (); + + for (n = 0; n < num_entries; n++) + { + if (strcmp (id, entry[n].ut_id) == 0) + { + if (memcmp (up, &entry[n], sizeof (struct utmp))) + { + error (0, 0, "UTMP entry does not match"); + return 1; + } + + return 0; + } + } + + error (0, 0, "bogus entry for ID `%s'", id); + return 1; +} + +static int +check_type (int type) +{ + struct utmp *up; + struct utmp ut; + int n; + + setutent (); + + ut.ut_type = type; + up = getutid (&ut); + if (up == NULL) + { + error (0, errno, "cannot get entry for type `%d'", type); + return 1; + } + + endutent (); + + for (n = 0; n < num_entries; n++) + { + if (type == entry[n].ut_type) + { + if (memcmp (up, &entry[n], sizeof (struct utmp))) + { + error (0, 0, "UTMP entry does not match"); + return 1; + } + + return 0; + } + } + + error (0, 0, "bogus entry for type `%d'", type); + return 1; +} + +static int +do_test (int argc, char *argv[]) +{ + int result = 0; + + utmpname (name); + + result |= do_init (); + result |= do_check (); + + result |= simulate_login ("tty1", "erwin"); + result |= do_check (); + + result |= simulate_login ("ttyp1", "paul"); + result |= do_check (); + + result |= simulate_logout ("tty2"); + result |= do_check (); + + result |= simulate_logout ("ttyp0"); + result |= do_check (); + + result |= simulate_login ("ttyp2", "richard"); + result |= do_check (); + + result |= check_login ("tty1"); + result |= check_logout ("ttyp0"); + result |= check_id ("p1"); + result |= check_id ("2"); + result |= check_id ("si"); + result |= check_type (BOOT_TIME); + result |= check_type (RUN_LVL); + + return result; +} + +#else + +/* No field 'ut_type' in struct utmp. */ +int +main () +{ + return 0; +} + +#endif -- cgit v1.2.3