From: mvs Date: Tue, 14 Dec 2021 15:57:57 +0000 (+0000) Subject: Move 'unixsock' test from regress/sys/kern/unixsock/ to X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=5eaa5d75116473501123c7c49a53926e9903ef15;p=openbsd Move 'unixsock' test from regress/sys/kern/unixsock/ to regress/sys/kern/unixsockets/. ok claudio@ --- diff --git a/regress/sys/kern/Makefile b/regress/sys/kern/Makefile index d3e78b86a16..ef6534c56bf 100644 --- a/regress/sys/kern/Makefile +++ b/regress/sys/kern/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.101 2021/12/10 00:50:18 mvs Exp $ +# $OpenBSD: Makefile,v 1.102 2021/12/14 15:57:57 mvs Exp $ SUBDIR+= __syscall SUBDIR+= accept access @@ -23,7 +23,7 @@ SUBDIR+= setuid .endif SUBDIR+= signal sosplice stackjmp stackpivot syscall syscall_segment SUBDIR+= sysvmsg sysvsem sysvshm -SUBDIR+= unalign unfdpass unixsockets unixsock unveil unveil-unmount +SUBDIR+= unalign unfdpass unixsockets unveil unveil-unmount SUBDIR+= wait install: diff --git a/regress/sys/kern/unixsock/Makefile b/regress/sys/kern/unixsock/Makefile deleted file mode 100644 index 5fd1f468c84..00000000000 --- a/regress/sys/kern/unixsock/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -# $OpenBSD: Makefile,v 1.1 2011/07/05 23:09:16 claudio Exp $ - -PROG= unixsock_test - -.include diff --git a/regress/sys/kern/unixsock/unixsock_test.c b/regress/sys/kern/unixsock/unixsock_test.c deleted file mode 100644 index decbfa92086..00000000000 --- a/regress/sys/kern/unixsock/unixsock_test.c +++ /dev/null @@ -1,159 +0,0 @@ -/* $OpenBSD: unixsock_test.c,v 1.3 2017/03/08 19:28:47 deraadt Exp $ */ -/* Written by Claudio Jeker in 2011 */ -/* Public domain */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -char path[1024]; -char *dir; - -int -test_bind(struct sockaddr_un *sun, socklen_t slen) -{ - int s, e, r; - - s = socket(AF_UNIX, SOCK_STREAM, 0); - if (s == -1) - err(1, "socket"); - - r = bind(s, (struct sockaddr *)sun, slen); - e = errno; - close(s); - sun->sun_path[slen - 2] = '\0'; - unlink(sun->sun_path); - errno = e; - return r; -} - -int -test_connect(struct sockaddr_un *sun, socklen_t slen, struct sockaddr_un *b) -{ - int s, s2, e, r; - - s = socket(AF_UNIX, SOCK_STREAM, 0); - if (s == -1) - err(1, "socket"); - - s2 = socket(AF_UNIX, SOCK_STREAM, 0); - if (s2 == -1) - err(1, "socket"); - if (bind(s2, (struct sockaddr *)b, sizeof(*b)) == -1) - err(1, "bind"); - if (listen(s2, 5) == -1) - err(1, "listen"); - - r = connect(s, (struct sockaddr *)sun, slen); - e = errno; - close(s); - close(s2); - unlink(b->sun_path); - errno = e; - return r; -} - -struct test { - socklen_t len; - int r; -} t[] = { - {30, 0}, - {50, 0}, - {100, 0}, - {102, 0}, - {103, 0}, - {104, -1}, - {105, -1}, - {110, -1}, - {200, -1}, - {0, 0} -}; - -int -main() -{ - struct sockaddr_storage ss; - struct sockaddr_un *sun, sun2; - char dir_template[] = "/tmp/peer.XXXXXXXXXX"; - char aaa[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - int i, fail = 0; - - dir = mkdtemp(dir_template); - if (!dir) - err(1, "mkdtemp"); - if (chdir(dir) == -1) - err(1, "chdir"); - snprintf(path, sizeof path, "%s/%s", dir, "socket"); - - for (i = 0; t[i].len != 0; i++) { - socklen_t slen = t[i].len; - memset(&ss, 0xfe, sizeof(ss)); - sun = (struct sockaddr_un *)&ss; - sun->sun_family = AF_UNIX; - - memset(&sun2, 0, sizeof(sun2)); - sun2.sun_family = AF_UNIX; - - snprintf(sun->sun_path, sizeof(ss) - 2, "%s.%.*s", path, - (int)(slen - strlen(path) - 1), aaa); - snprintf(sun2.sun_path, sizeof(sun2) - 2, "%s.%.*s", "socket", - (int)(slen - strlen(path) - 1), aaa); - - if (test_bind(sun, slen + 2) != t[i].r) { - warn("FAIL: bind(\"%s\") len %d", sun->sun_path, - slen + 2); - fail = 1; - } - if (test_bind(sun, slen + 3) != t[i].r) { - warn("FAIL2: bind(\"%s\") len %d", sun->sun_path, - slen + 3); - fail = 1; - } - if (sizeof(*sun) >= slen + 2 && - test_bind(sun, sizeof(*sun)) != t[i].r) { - warn("FAIL3: bind(\"%s\") len %zd", sun->sun_path, - sizeof(*sun)); - fail = 1; - } - sun->sun_path[slen] = 'a'; - if (test_bind(sun, slen + 2) != t[i].r) { - warn("FAIL4: bind(\"%.*s\") len %d no-NUL", - (int)(slen + 2), sun->sun_path, slen + 2); - fail = 1; - } - sun->sun_path[slen] = '\0'; - - if (test_connect(sun, slen + 2, &sun2) != t[i].r) { - warn("FAIL: connect(\"%s\") len %d", sun->sun_path, - slen + 2); - fail = 1; - } - if (test_connect(sun, slen + 3, &sun2) != t[i].r) { - warn("FAIL2: connect(\"%s\") len %d", sun->sun_path, - slen + 3); - fail = 1; - } - if (sizeof(*sun) >= slen + 2 && - test_connect(sun, sizeof(*sun), &sun2) != t[i].r) { - warn("FAIL3: connect(\"%s\") len %zd", sun->sun_path, - sizeof(*sun)); - fail = 1; - } - sun->sun_path[slen] = 'a'; - if (test_connect(sun, slen + 2, &sun2) != t[i].r) { - warn("FAIL4: connect(\"%.*s\") len %d no-NUL", - slen + 2, sun->sun_path, slen + 2); - fail = 1; - } - } - return fail; -} diff --git a/regress/sys/kern/unixsockets/Makefile b/regress/sys/kern/unixsockets/Makefile index 3c308c0b3dc..c39225c2ad5 100644 --- a/regress/sys/kern/unixsockets/Makefile +++ b/regress/sys/kern/unixsockets/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.6 2021/12/10 00:50:18 mvs Exp $ +# $OpenBSD: Makefile,v 1.7 2021/12/14 15:57:57 mvs Exp $ # Copyright (c) 2021 Makkoveev Vitaliy # @@ -16,7 +16,7 @@ WARNINGS = yes -PROGS = undgram_selfconn ungc +PROGS = undgram_selfconn ungc unixsock_test PROGS += unconacc LDADD_unconacc = -lpthread diff --git a/regress/sys/kern/unixsockets/unixsock_test.c b/regress/sys/kern/unixsockets/unixsock_test.c new file mode 100644 index 00000000000..b8503a769f4 --- /dev/null +++ b/regress/sys/kern/unixsockets/unixsock_test.c @@ -0,0 +1,159 @@ +/* $OpenBSD: unixsock_test.c,v 1.1 2021/12/14 15:57:57 mvs Exp $ */ +/* Written by Claudio Jeker in 2011 */ +/* Public domain */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char path[1024]; +char *dir; + +int +test_bind(struct sockaddr_un *sun, socklen_t slen) +{ + int s, e, r; + + s = socket(AF_UNIX, SOCK_STREAM, 0); + if (s == -1) + err(1, "socket"); + + r = bind(s, (struct sockaddr *)sun, slen); + e = errno; + close(s); + sun->sun_path[slen - 2] = '\0'; + unlink(sun->sun_path); + errno = e; + return r; +} + +int +test_connect(struct sockaddr_un *sun, socklen_t slen, struct sockaddr_un *b) +{ + int s, s2, e, r; + + s = socket(AF_UNIX, SOCK_STREAM, 0); + if (s == -1) + err(1, "socket"); + + s2 = socket(AF_UNIX, SOCK_STREAM, 0); + if (s2 == -1) + err(1, "socket"); + if (bind(s2, (struct sockaddr *)b, sizeof(*b)) == -1) + err(1, "bind"); + if (listen(s2, 5) == -1) + err(1, "listen"); + + r = connect(s, (struct sockaddr *)sun, slen); + e = errno; + close(s); + close(s2); + unlink(b->sun_path); + errno = e; + return r; +} + +struct test { + socklen_t len; + int r; +} t[] = { + {30, 0}, + {50, 0}, + {100, 0}, + {102, 0}, + {103, 0}, + {104, -1}, + {105, -1}, + {110, -1}, + {200, -1}, + {0, 0} +}; + +int +main() +{ + struct sockaddr_storage ss; + struct sockaddr_un *sun, sun2; + char dir_template[] = "/tmp/peer.XXXXXXXXXX"; + char aaa[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + int i, fail = 0; + + dir = mkdtemp(dir_template); + if (!dir) + err(1, "mkdtemp"); + if (chdir(dir) == -1) + err(1, "chdir"); + snprintf(path, sizeof path, "%s/%s", dir, "socket"); + + for (i = 0; t[i].len != 0; i++) { + socklen_t slen = t[i].len; + memset(&ss, 0xfe, sizeof(ss)); + sun = (struct sockaddr_un *)&ss; + sun->sun_family = AF_UNIX; + + memset(&sun2, 0, sizeof(sun2)); + sun2.sun_family = AF_UNIX; + + snprintf(sun->sun_path, sizeof(ss) - 2, "%s.%.*s", path, + (int)(slen - strlen(path) - 1), aaa); + snprintf(sun2.sun_path, sizeof(sun2) - 2, "%s.%.*s", "socket", + (int)(slen - strlen(path) - 1), aaa); + + if (test_bind(sun, slen + 2) != t[i].r) { + warn("FAIL: bind(\"%s\") len %d", sun->sun_path, + slen + 2); + fail = 1; + } + if (test_bind(sun, slen + 3) != t[i].r) { + warn("FAIL2: bind(\"%s\") len %d", sun->sun_path, + slen + 3); + fail = 1; + } + if (sizeof(*sun) >= slen + 2 && + test_bind(sun, sizeof(*sun)) != t[i].r) { + warn("FAIL3: bind(\"%s\") len %zd", sun->sun_path, + sizeof(*sun)); + fail = 1; + } + sun->sun_path[slen] = 'a'; + if (test_bind(sun, slen + 2) != t[i].r) { + warn("FAIL4: bind(\"%.*s\") len %d no-NUL", + (int)(slen + 2), sun->sun_path, slen + 2); + fail = 1; + } + sun->sun_path[slen] = '\0'; + + if (test_connect(sun, slen + 2, &sun2) != t[i].r) { + warn("FAIL: connect(\"%s\") len %d", sun->sun_path, + slen + 2); + fail = 1; + } + if (test_connect(sun, slen + 3, &sun2) != t[i].r) { + warn("FAIL2: connect(\"%s\") len %d", sun->sun_path, + slen + 3); + fail = 1; + } + if (sizeof(*sun) >= slen + 2 && + test_connect(sun, sizeof(*sun), &sun2) != t[i].r) { + warn("FAIL3: connect(\"%s\") len %zd", sun->sun_path, + sizeof(*sun)); + fail = 1; + } + sun->sun_path[slen] = 'a'; + if (test_connect(sun, slen + 2, &sun2) != t[i].r) { + warn("FAIL4: connect(\"%.*s\") len %d no-NUL", + slen + 2, sun->sun_path, slen + 2); + fail = 1; + } + } + return fail; +}