summaryrefslogtreecommitdiff
path: root/regress
diff options
context:
space:
mode:
authorFederico G. Schwindt <fgsch@cvs.openbsd.org>2009-12-26 01:34:19 +0000
committerFederico G. Schwindt <fgsch@cvs.openbsd.org>2009-12-26 01:34:19 +0000
commitf911a54d4cdbeb7dabc209d044d85387474b9054 (patch)
treeb484f2f710c8684f0ccb6c79d8ed61c59fada70d /regress
parent417aa4484ec04cf3c9d4ff79ea97016f662dddc7 (diff)
Add tests to check timeout on sockets under different situations. kurt@ ok.
Diffstat (limited to 'regress')
-rw-r--r--regress/lib/libpthread/setsockopt/1/Makefile6
-rw-r--r--regress/lib/libpthread/setsockopt/1/setsockopt1.c101
-rw-r--r--regress/lib/libpthread/setsockopt/2/Makefile6
-rw-r--r--regress/lib/libpthread/setsockopt/2/setsockopt2.c119
-rw-r--r--regress/lib/libpthread/setsockopt/3/Makefile7
-rw-r--r--regress/lib/libpthread/setsockopt/3/setsockopt3.c75
-rw-r--r--regress/lib/libpthread/setsockopt/3a/Makefile18
-rw-r--r--regress/lib/libpthread/setsockopt/3a/setsockopt3a.c75
-rw-r--r--regress/lib/libpthread/setsockopt/Makefile5
-rw-r--r--regress/lib/libpthread/setsockopt/Makefile.inc3
10 files changed, 415 insertions, 0 deletions
diff --git a/regress/lib/libpthread/setsockopt/1/Makefile b/regress/lib/libpthread/setsockopt/1/Makefile
new file mode 100644
index 00000000000..265a8e015fa
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/1/Makefile
@@ -0,0 +1,6 @@
+# $OpenBSD: Makefile,v 1.1 2009/12/26 01:34:18 fgsch Exp $
+
+PROG= setsockopt1
+CFLAGS+= -I${.CURDIR}/../../include
+
+.include <bsd.regress.mk>
diff --git a/regress/lib/libpthread/setsockopt/1/setsockopt1.c b/regress/lib/libpthread/setsockopt/1/setsockopt1.c
new file mode 100644
index 00000000000..758b4f71138
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/1/setsockopt1.c
@@ -0,0 +1,101 @@
+/* $OpenBSD: setsockopt1.c,v 1.1 2009/12/26 01:34:18 fgsch Exp $ */
+/*
+ * Federico G. Schwindt <fgsch@openbsd.org>, 2009. Public Domain.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "test.h"
+
+static void
+alarm_handler(int sig)
+{
+ _exit(NOTOK);
+}
+
+int
+check_timeout(int s, int sec, struct timeval *to)
+{
+ struct timeval t1, t2;
+ struct timeval e, d;
+ char buf[BUFSIZ];
+
+ ASSERT(signal(SIGALRM, alarm_handler) != SIG_ERR);
+ CHECKe(alarm(sec));
+ CHECKe(gettimeofday(&t1, NULL));
+ ASSERT(read(s, &buf, sizeof(buf)) == -1);
+ CHECKe(gettimeofday(&t2, NULL));
+ ASSERT(errno == EAGAIN);
+ timersub(&t2, &t1, &e);
+ timersub(&e, to, &d);
+ return ((d.tv_sec > 1 || (d.tv_usec / 1000) > 100) ? 1 : 0);
+}
+
+static void *
+sock_connect(void *arg)
+{
+ struct sockaddr_in sin;
+ struct timeval to;
+ int s, s2, s3;
+
+ CHECKe(s = socket(AF_INET, SOCK_STREAM, 0));
+ CHECKe(s2 = dup(s));
+ CHECKe(s3 = fcntl(s, F_DUPFD, s));
+ bzero(&sin, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_len = sizeof(sin);
+ sin.sin_port = htons(6543);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ CHECKe(connect(s, (struct sockaddr *)&sin, sizeof(sin)));
+ to.tv_sec = 2;
+ to.tv_usec = 0.5 * 1e6;
+ CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to)));
+ CHECKr(check_timeout(s, 3, &to));
+ CHECKr(check_timeout(s2, 3, &to));
+ CHECKr(check_timeout(s3, 3, &to));
+ to.tv_sec = 1;
+ to.tv_usec = 0.5 * 1e6;
+ CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to)));
+ CHECKr(check_timeout(s, 2, &to));
+ CHECKr(check_timeout(s2, 2, &to));
+ CHECKr(check_timeout(s3, 2, &to));
+ return (NULL);
+}
+
+static void *
+sock_accept(void *arg)
+{
+ pthread_t connect_thread;
+ struct sockaddr_in sin;
+ int s;
+
+ CHECKe(s = socket(AF_INET, SOCK_STREAM, 0));
+ bzero(&sin, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_len = sizeof(sin);
+ sin.sin_port = htons(6543);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ CHECKe(bind(s, (struct sockaddr *)&sin, sizeof(sin)));
+ CHECKe(listen(s, 2));
+
+ CHECKr(pthread_create(&connect_thread, NULL, sock_connect, NULL));
+ CHECKr(pthread_join(connect_thread, NULL));
+ return (NULL);
+}
+
+int
+main(int argc, char **argv)
+{
+ pthread_t accept_thread;
+
+ CHECKr(pthread_create(&accept_thread, NULL, sock_accept, NULL));
+ CHECKr(pthread_join(accept_thread, NULL));
+ SUCCEED;
+}
diff --git a/regress/lib/libpthread/setsockopt/2/Makefile b/regress/lib/libpthread/setsockopt/2/Makefile
new file mode 100644
index 00000000000..3926a148f09
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/2/Makefile
@@ -0,0 +1,6 @@
+# $OpenBSD: Makefile,v 1.1 2009/12/26 01:34:18 fgsch Exp $
+
+PROG= setsockopt2
+CFLAGS+= -I${.CURDIR}/../../include
+
+.include <bsd.regress.mk>
diff --git a/regress/lib/libpthread/setsockopt/2/setsockopt2.c b/regress/lib/libpthread/setsockopt/2/setsockopt2.c
new file mode 100644
index 00000000000..21a296b4e1a
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/2/setsockopt2.c
@@ -0,0 +1,119 @@
+/* $OpenBSD: setsockopt2.c,v 1.1 2009/12/26 01:34:18 fgsch Exp $ */
+/*
+ * Federico G. Schwindt <fgsch@openbsd.org>, 2009. Public Domain.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <netinet/in.h>
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "test.h"
+
+static void
+alarm_handler(int sig)
+{
+ _exit(NOTOK);
+}
+
+int
+check_timeout(int s, int sec, struct timeval *to)
+{
+ struct timeval t1, t2;
+ struct timeval e, d;
+ char buf[BUFSIZ];
+
+ ASSERT(signal(SIGALRM, alarm_handler) != SIG_ERR);
+ CHECKe(alarm(sec));
+ CHECKe(gettimeofday(&t1, NULL));
+ ASSERT(read(s, &buf, sizeof(buf)) == -1);
+ CHECKe(gettimeofday(&t2, NULL));
+ ASSERT(errno == EAGAIN);
+ timersub(&t2, &t1, &e);
+ timersub(&e, to, &d);
+ return ((d.tv_sec > 1 || (d.tv_usec / 1000) > 100) ? 1 : 0);
+}
+
+static void *
+sock_connect(void *arg)
+{
+ struct sockaddr_in sin;
+ struct timeval to;
+ pid_t child_pid;
+ int status;
+ int s, s2, s3;
+
+ CHECKe(s = socket(AF_INET, SOCK_STREAM, 0));
+ CHECKe(s2 = dup(s));
+ CHECKe(s3 = fcntl(s, F_DUPFD, s));
+ bzero(&sin, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_len = sizeof(sin);
+ sin.sin_port = htons(6543);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ CHECKe(connect(s, (struct sockaddr *)&sin, sizeof(sin)));
+ to.tv_sec = 2;
+ to.tv_usec = 0.5 * 1e6;
+ CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to)));
+ CHECKe(child_pid = fork());
+ if (child_pid == 0) {
+ to.tv_sec = 1;
+ to.tv_usec = 0.5 * 1e6;
+ CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to)));
+ CHECKr(check_timeout(s, 2, &to));
+ CHECKr(check_timeout(s2, 2, &to));
+ CHECKr(check_timeout(s3, 2, &to));
+ return (NULL);
+ }
+ sleep(2);
+ CHECKr(check_timeout(s, 2, &to));
+ CHECKr(check_timeout(s2, 2, &to));
+ CHECKr(check_timeout(s3, 2, &to));
+ CHECKe(s2 = dup(s));
+ CHECKe(s3 = fcntl(s, F_DUPFD, s));
+ CHECKr(check_timeout(s2, 2, &to));
+ CHECKr(check_timeout(s3, 2, &to));
+ CHECKe(close(s));
+ CHECKe(close(s2));
+ CHECKe(close(s3));
+ ASSERTe(wait(&status), == child_pid);
+ ASSERT(WIFEXITED(status));
+ CHECKr(WEXITSTATUS(status));
+ return (NULL);
+}
+
+static void *
+sock_accept(void *arg)
+{
+ pthread_t connect_thread;
+ struct sockaddr_in sin;
+ int s;
+
+ CHECKe(s = socket(AF_INET, SOCK_STREAM, 0));
+ bzero(&sin, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_len = sizeof(sin);
+ sin.sin_port = htons(6543);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ CHECKe(bind(s, (struct sockaddr *)&sin, sizeof(sin)));
+ CHECKe(listen(s, 2));
+
+ CHECKr(pthread_create(&connect_thread, NULL, sock_connect, NULL));
+ CHECKr(pthread_join(connect_thread, NULL));
+ return (NULL);
+}
+
+int
+main(int argc, char **argv)
+{
+ pthread_t accept_thread;
+
+ CHECKr(pthread_create(&accept_thread, NULL, sock_accept, NULL));
+ CHECKr(pthread_join(accept_thread, NULL));
+ SUCCEED;
+}
diff --git a/regress/lib/libpthread/setsockopt/3/Makefile b/regress/lib/libpthread/setsockopt/3/Makefile
new file mode 100644
index 00000000000..ec19f6fe1f2
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/3/Makefile
@@ -0,0 +1,7 @@
+# $OpenBSD: Makefile,v 1.1 2009/12/26 01:34:18 fgsch Exp $
+
+PROG= setsockopt3
+CFLAGS+= -I${.CURDIR}/../../include
+CLEANFILES+= setsockopt3a
+
+.include <bsd.regress.mk>
diff --git a/regress/lib/libpthread/setsockopt/3/setsockopt3.c b/regress/lib/libpthread/setsockopt/3/setsockopt3.c
new file mode 100644
index 00000000000..5c249f9bff5
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/3/setsockopt3.c
@@ -0,0 +1,75 @@
+/* $OpenBSD: setsockopt3.c,v 1.1 2009/12/26 01:34:18 fgsch Exp $ */
+/*
+ * Federico G. Schwindt <fgsch@openbsd.org>, 2009. Public Domain.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <netinet/in.h>
+#include <err.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "test.h"
+
+static void *
+sock_connect(void *arg)
+{
+ struct timeval to;
+ pid_t child_pid;
+ int status;
+ int s;
+
+ CHECKe(s = socket(AF_INET, SOCK_STREAM, 0));
+ to.tv_sec = 2;
+ to.tv_usec = 0.5 * 1e6;
+ CHECKe(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to)));
+ CHECKe(child_pid = fork());
+ if (child_pid == 0) {
+ char *argv[3];
+ char fdstr[3];
+ snprintf(fdstr, sizeof(fdstr), "%d", s);
+ argv[0] = "setsockopt3a";
+ argv[1] = fdstr;
+ argv[2] = NULL;
+ execv(argv[0], argv);
+ _exit(NOTOK);
+ }
+ ASSERTe(wait(&status), == child_pid);
+ ASSERT(WIFEXITED(status));
+ ASSERT(WEXITSTATUS(status) == 0);
+ return (NULL);
+}
+
+static void *
+sock_accept(void *arg)
+{
+ pthread_t connect_thread;
+ struct sockaddr_in sin;
+ int s;
+
+ CHECKe(s = socket(AF_INET, SOCK_STREAM, 0));
+ bzero(&sin, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_len = sizeof(sin);
+ sin.sin_port = htons(6543);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ CHECKe(bind(s, (struct sockaddr *)&sin, sizeof(sin)));
+ CHECKe(listen(s, 2));
+
+ CHECKr(pthread_create(&connect_thread, NULL, sock_connect, NULL));
+ CHECKr(pthread_join(connect_thread, NULL));
+ return (NULL);
+}
+
+int
+main(int argc, char **argv)
+{
+ pthread_t accept_thread;
+
+ CHECKr(pthread_create(&accept_thread, NULL, sock_accept, NULL));
+ CHECKr(pthread_join(accept_thread, NULL));
+ SUCCEED;
+}
diff --git a/regress/lib/libpthread/setsockopt/3a/Makefile b/regress/lib/libpthread/setsockopt/3a/Makefile
new file mode 100644
index 00000000000..0e02ab3194f
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/3a/Makefile
@@ -0,0 +1,18 @@
+# $OpenBSD: Makefile,v 1.1 2009/12/26 01:34:18 fgsch Exp $
+
+PROG= setsockopt3a
+CFLAGS+= -I${.CURDIR}/../../include
+
+REGRESS_TARGETS=dummy
+
+# build prog and link into test 3 directory. Code is used by test 3
+#
+dummy: ${PROG}
+ @cd ${.CURDIR}/../3; \
+ if test -d ${__objdir} ; then \
+ cd ${__objdir} ; \
+ fi; \
+ ln -sf ${.OBJDIR}/${PROG}
+
+.include <bsd.regress.mk>
+
diff --git a/regress/lib/libpthread/setsockopt/3a/setsockopt3a.c b/regress/lib/libpthread/setsockopt/3a/setsockopt3a.c
new file mode 100644
index 00000000000..0ec37e01f57
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/3a/setsockopt3a.c
@@ -0,0 +1,75 @@
+/* $OpenBSD: setsockopt3a.c,v 1.1 2009/12/26 01:34:18 fgsch Exp $ */
+/*
+ * Federico G. Schwindt <fgsch@openbsd.org>, 2009. Public Domain.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <err.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "test.h"
+
+static void
+alarm_handler(int sig)
+{
+ _exit(NOTOK);
+}
+
+int
+check_timeout(int s, int sec, struct timeval *to)
+{
+ struct timeval t1, t2;
+ struct timeval e, d;
+ char buf[BUFSIZ];
+
+ ASSERT(signal(SIGALRM, alarm_handler) != SIG_ERR);
+ CHECKe(alarm(sec));
+ CHECKe(gettimeofday(&t1, NULL));
+ ASSERT(read(s, &buf, sizeof(buf)) == -1);
+ CHECKe(gettimeofday(&t2, NULL));
+ ASSERT(errno == EAGAIN);
+ timersub(&t2, &t1, &e);
+ timersub(&e, to, &d);
+ return ((d.tv_sec > 1 || (d.tv_usec / 1000) > 100) ? 1 : 0);
+}
+
+static void *
+sock_accept(void *arg)
+{
+ struct sockaddr_in sin;
+ struct timeval to;
+ int s, s2, s3;
+
+ CHECKe(s = strtol(arg, NULL, 10));
+ bzero(&sin, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_len = sizeof(sin);
+ sin.sin_port = htons(6543);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ CHECKe(connect(s, (struct sockaddr *)&sin, sizeof(sin)));
+ to.tv_sec = 2;
+ to.tv_usec = 0.5 * 1e6;
+ ASSERT(check_timeout(s, 3, &to) == 0);
+ CHECKe(s2 = dup(s));
+ CHECKe(s3 = fcntl(s, F_DUPFD, s));
+ ASSERT(check_timeout(s2, 3, &to) == 0);
+ ASSERT(check_timeout(s3, 3, &to) == 0);
+ return (NULL);
+}
+
+int
+main(int argc, char **argv)
+{
+ pthread_t accept_thread;
+
+ if (argc != 2)
+ exit(NOTOK);
+ CHECKr(pthread_create(&accept_thread, NULL, sock_accept, argv[1]));
+ CHECKr(pthread_join(accept_thread, NULL));
+ SUCCEED;
+}
diff --git a/regress/lib/libpthread/setsockopt/Makefile b/regress/lib/libpthread/setsockopt/Makefile
new file mode 100644
index 00000000000..67895dc85e2
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/Makefile
@@ -0,0 +1,5 @@
+# $OpenBSD: Makefile,v 1.1 2009/12/26 01:34:18 fgsch Exp $
+
+SUBDIR= 1 2 3a 3
+
+.include <bsd.subdir.mk>
diff --git a/regress/lib/libpthread/setsockopt/Makefile.inc b/regress/lib/libpthread/setsockopt/Makefile.inc
new file mode 100644
index 00000000000..9da23de6b13
--- /dev/null
+++ b/regress/lib/libpthread/setsockopt/Makefile.inc
@@ -0,0 +1,3 @@
+# $OpenBSD: Makefile.inc,v 1.1 2009/12/26 01:34:18 fgsch Exp $
+
+.include "${.CURDIR}/../../Makefile.inc"