summaryrefslogtreecommitdiff
path: root/gl/tests
diff options
context:
space:
mode:
authorSimon Josefsson <simon@josefsson.org>2008-11-10 12:08:50 +0100
committerSimon Josefsson <simon@josefsson.org>2008-11-10 12:08:50 +0100
commit622b00c1f9f2dae59e1f4b29ba7c5566e3cceb25 (patch)
treeacedc1db086a648ef27d7973de731677e8c11bf5 /gl/tests
parent8db007da11d55f0b3b05a7efbfba05481401db16 (diff)
downloadgnutls-622b00c1f9f2dae59e1f4b29ba7c5566e3cceb25.tar.gz
Update gnulib files.
Diffstat (limited to 'gl/tests')
-rw-r--r--gl/tests/gettimeofday.c142
-rw-r--r--gl/tests/test-gettimeofday.c45
-rw-r--r--gl/tests/test-select-fd.c66
-rwxr-xr-xgl/tests/test-select-in.sh29
-rwxr-xr-xgl/tests/test-select-out.sh29
-rw-r--r--gl/tests/test-select-stdin.c80
6 files changed, 391 insertions, 0 deletions
diff --git a/gl/tests/gettimeofday.c b/gl/tests/gettimeofday.c
new file mode 100644
index 0000000000..88295ce6a8
--- /dev/null
+++ b/gl/tests/gettimeofday.c
@@ -0,0 +1,142 @@
+/* Provide gettimeofday for systems that don't have it or for which it's broken.
+
+ Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007 Free Software
+ Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+
+/* written by Jim Meyering */
+
+#include <config.h>
+
+/* Specification. */
+#include <sys/time.h>
+
+#include <time.h>
+
+#if HAVE_SYS_TIMEB_H
+# include <sys/timeb.h>
+#endif
+
+#if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
+
+/* Work around the bug in some systems whereby gettimeofday clobbers
+ the static buffer that localtime uses for its return value. The
+ gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
+ this problem. The tzset replacement is necessary for at least
+ Solaris 2.5, 2.5.1, and 2.6. */
+
+static struct tm tm_zero_buffer;
+static struct tm *localtime_buffer_addr = &tm_zero_buffer;
+
+/* This is a wrapper for localtime. It is used only on systems for which
+ gettimeofday clobbers the static buffer used for localtime's result.
+
+ On the first call, record the address of the static buffer that
+ localtime uses for its result. */
+
+struct tm *
+rpl_localtime (time_t const *timep)
+{
+#undef localtime
+ extern struct tm *localtime (time_t const *);
+ struct tm *tm = localtime (timep);
+
+ if (localtime_buffer_addr == &tm_zero_buffer)
+ localtime_buffer_addr = tm;
+
+ return tm;
+}
+
+/* Same as above, since gmtime and localtime use the same buffer. */
+struct tm *
+rpl_gmtime (time_t const *timep)
+{
+#undef gmtime
+ extern struct tm *gmtime (time_t const *);
+ struct tm *tm = gmtime (timep);
+
+ if (localtime_buffer_addr == &tm_zero_buffer)
+ localtime_buffer_addr = tm;
+
+ return tm;
+}
+
+#endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
+
+#if TZSET_CLOBBERS_LOCALTIME
+/* This is a wrapper for tzset, for systems on which tzset may clobber
+ the static buffer used for localtime's result. */
+void
+rpl_tzset (void)
+{
+#undef tzset
+ extern void tzset (void);
+
+ /* Save and restore the contents of the buffer used for localtime's
+ result around the call to tzset. */
+ struct tm save = *localtime_buffer_addr;
+ tzset ();
+ *localtime_buffer_addr = save;
+}
+#endif
+
+/* This is a wrapper for gettimeofday. It is used only on systems
+ that lack this function, or whose implementation of this function
+ causes problems. */
+
+int
+rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
+{
+#undef gettimeofday
+#if HAVE_GETTIMEOFDAY
+# if GETTIMEOFDAY_CLOBBERS_LOCALTIME
+ /* Save and restore the contents of the buffer used for localtime's
+ result around the call to gettimeofday. */
+ struct tm save = *localtime_buffer_addr;
+# endif
+
+ int result = gettimeofday (tv, tz);
+
+# if GETTIMEOFDAY_CLOBBERS_LOCALTIME
+ *localtime_buffer_addr = save;
+# endif
+
+ return result;
+
+#else
+
+# if HAVE__FTIME
+
+ struct _timeb timebuf;
+ _ftime (&timebuf);
+ tv->tv_sec = timebuf.time;
+ tv->tv_usec = timebuf.millitm * 1000;
+
+# else
+
+# if !defined OK_TO_USE_1S_CLOCK
+# error "Only 1-second nominal clock resolution found. Is that intended?" \
+ "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
+# endif
+ tv->tv_sec = time (NULL);
+ tv->tv_usec = 0;
+
+# endif
+
+ return 0;
+
+#endif
+}
diff --git a/gl/tests/test-gettimeofday.c b/gl/tests/test-gettimeofday.c
new file mode 100644
index 0000000000..892b18829c
--- /dev/null
+++ b/gl/tests/test-gettimeofday.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2005, 2007 Free Software Foundation
+ * Written by Jim Meyering.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <sys/time.h>
+#include <time.h>
+
+#include <stdio.h>
+#include <string.h>
+
+int
+main (int argc, char *argv[])
+{
+ time_t t = 0;
+ struct tm *lt;
+ struct tm saved_lt;
+ struct timeval tv;
+ lt = localtime (&t);
+ saved_lt = *lt;
+ gettimeofday (&tv, NULL);
+ if (memcmp (lt, &saved_lt, sizeof (struct tm)) != 0)
+ {
+ fprintf (stderr, "gettimeofday still clobbers the localtime buffer!\n");
+ return 1;
+ }
+ else
+ {
+ return 0;
+ }
+}
diff --git a/gl/tests/test-select-fd.c b/gl/tests/test-select-fd.c
new file mode 100644
index 0000000000..d362170a87
--- /dev/null
+++ b/gl/tests/test-select-fd.c
@@ -0,0 +1,66 @@
+/* Test of select() substitute, reading or writing from a given file descriptor.
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2008. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/select.h>
+
+int
+main (int argc, char *argv[])
+{
+ if (argc == 3)
+ {
+ char mode = argv[1][0];
+
+ if (mode == 'r' || mode == 'w')
+ {
+ int fd = atoi (argv[2]);
+
+ if (fd >= 0)
+ {
+ fd_set fds;
+ struct timeval timeout;
+ int ret;
+
+ FD_ZERO (&fds);
+ FD_SET (fd, &fds);
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 10000;
+ ret = (mode == 'r'
+ ? select (fd + 1, &fds, NULL, NULL, &timeout)
+ : select (fd + 1, NULL, &fds, NULL, &timeout));
+ if (ret < 0)
+ {
+ perror ("select failed");
+ exit (1);
+ }
+ if ((ret == 0) != ! FD_ISSET (fd, &fds))
+ {
+ fprintf (stderr, "incorrect return value\n");
+ exit (1);
+ }
+ fprintf (stderr, "%d\n", ret);
+ exit (0);
+ }
+ }
+ }
+ fprintf (stderr, "Usage: test-select-fd mode fd\n");
+ exit (1);
+}
diff --git a/gl/tests/test-select-in.sh b/gl/tests/test-select-in.sh
new file mode 100755
index 0000000000..b93fee9a04
--- /dev/null
+++ b/gl/tests/test-select-in.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+# Test select() on file descriptors opened for reading.
+
+tmpfiles=""
+trap 'rm -fr $tmpfiles' 1 2 3 15
+
+tmpfiles="$tmpfiles t-select-in.tmp"
+
+# Regular files.
+
+./test-select-fd${EXEEXT} r 0 < ./test-select-fd${EXEEXT} 2> t-select-in.tmp
+test `cat t-select-in.tmp` = "1" || exit 1
+
+# Pipes.
+
+{ sleep 1; echo abc; } | ./test-select-fd${EXEEXT} r 0 2> t-select-in.tmp
+test `cat t-select-in.tmp` = "0" || exit 1
+
+echo abc | { sleep 1; ./test-select-fd${EXEEXT} r 0; } 2> t-select-in.tmp
+test `cat t-select-in.tmp` = "1" || exit 1
+
+# Special files.
+
+./test-select-fd${EXEEXT} r 0 < /dev/null 2> t-select-in.tmp
+test `cat t-select-in.tmp` = "1" || exit 1
+
+rm -fr $tmpfiles
+
+exit 0
diff --git a/gl/tests/test-select-out.sh b/gl/tests/test-select-out.sh
new file mode 100755
index 0000000000..f83beeb1f5
--- /dev/null
+++ b/gl/tests/test-select-out.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+# Test select() on file descriptors opened for writing.
+
+tmpfiles=""
+trap 'rm -fr $tmpfiles' 1 2 3 15
+
+tmpfiles="$tmpfiles t-select-out.out t-select-out.tmp"
+
+# Regular files.
+
+./test-select-fd${EXEEXT} w 1 > t-select-out.out 2> t-select-out.tmp
+test `cat t-select-out.tmp` = "1" || exit 1
+
+# Pipes.
+
+( { echo abc; ./test-select-fd${EXEEXT} w 1; } | { sleep 1; cat; } ) > /dev/null 2> t-select-out.tmp
+test `cat t-select-out.tmp` = "0" || exit 1
+
+( { sleep 1; echo abc; ./test-select-fd${EXEEXT} w 1; } | cat) > /dev/null 2> t-select-out.tmp
+test `cat t-select-out.tmp` = "1" || exit 1
+
+# Special files.
+
+./test-select-fd${EXEEXT} w 1 > /dev/null 2> t-select-out.tmp
+test `cat t-select-out.tmp` = "1" || exit 1
+
+rm -fr $tmpfiles
+
+exit 0
diff --git a/gl/tests/test-select-stdin.c b/gl/tests/test-select-stdin.c
new file mode 100644
index 0000000000..e855f92d20
--- /dev/null
+++ b/gl/tests/test-select-stdin.c
@@ -0,0 +1,80 @@
+/* Test of select() substitute, reading from stdin.
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2008. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+int
+main ()
+{
+ printf ("Applying select() from standard input. Press Ctrl-C to abort.\n");
+ for (;;)
+ {
+ struct timeval before;
+ struct timeval after;
+ unsigned long spent_usec;
+ fd_set readfds;
+ struct timeval timeout;
+ int ret;
+
+ gettimeofday (&before, NULL);
+
+ FD_ZERO (&readfds);
+ FD_SET (0, &readfds);
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 500000;
+ ret = select (1, &readfds, NULL, NULL, &timeout);
+
+ gettimeofday (&after, NULL);
+ spent_usec = (after.tv_sec - before.tv_sec) * 1000000
+ + after.tv_usec - before.tv_usec;
+
+ if (ret < 0)
+ {
+ perror ("select failed");
+ exit (1);
+ }
+ if ((ret == 0) != ! FD_ISSET (0, &readfds))
+ {
+ fprintf (stderr, "incorrect return value\n");
+ exit (1);
+ }
+ if (ret == 0)
+ {
+ if (spent_usec < 250000)
+ {
+ fprintf (stderr, "returned too early\n");
+ exit (1);
+ }
+ /* Timeout */
+ printf ("."); fflush (stdout);
+ }
+ else
+ {
+ char c;
+
+ printf ("Input available! Trying to read 1 byte...\n");
+ read (0, &c, 1);
+ }
+ }
+}