diff options
-rw-r--r-- | lib9p/CMakeLists.txt | 3 | ||||
-rw-r--r-- | lib9p/tests/testclient-hangup.c | 100 | ||||
-rw-r--r-- | lib9p/tests/testclient-hangup.explog | 14 | ||||
-rw-r--r-- | lib9p/tests/testclient-sess.c | 2 |
4 files changed, 118 insertions, 1 deletions
diff --git a/lib9p/CMakeLists.txt b/lib9p/CMakeLists.txt index 2a2f858..3433417 100644 --- a/lib9p/CMakeLists.txt +++ b/lib9p/CMakeLists.txt @@ -48,6 +48,9 @@ if (ENABLE_TESTS) add_lib9p_executable("testclient-sess") add_lib9p_test("./testclient-sess") + add_lib9p_executable("testclient-hangup") + add_lib9p_test("./testclient-hangup") + set(cfg_matrix "CONFIG_9P_SRV_DEBUG;[0;1]" "CONFIG_9P_ENABLE_9P2000;[0;1]" diff --git a/lib9p/tests/testclient-hangup.c b/lib9p/tests/testclient-hangup.c new file mode 100644 index 0000000..a819e80 --- /dev/null +++ b/lib9p/tests/testclient-hangup.c @@ -0,0 +1,100 @@ +/* lib9p/tests/testclient-hangup.c - Test the 9P `test_server`'s handling of TCP hangups + * + * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <arpa/inet.h> /* for htons(), inet_addr() */ +#include <errno.h> +#include <netinet/in.h> /* for struct sockaddr{,_in} */ +#include <stdlib.h> /* for atoi() */ +#include <sys/socket.h> /* for socket(), connect() */ +#include <sys/uio.h> /* for writev() */ +#include <unistd.h> /* for read() */ + +#define error __error +#include <error.h> +#undef error + +#include <lib9p/core.h> +#include <libmisc/assert.h> +#include <libmisc/endian.h> + +#define MAX_MSG_SIZE (8*1024) + +static void _send9p(int fd, struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body) { + struct lib9p_Tmsg_send_buf buf; + bool err = lib9p_Tmsg_marshal(ctx, typ, body, &buf); + assert(!err); + size_t exp = 0; + for (size_t i = 0; i < buf.iov_cnt; i++) + exp += buf.iov[i].iov_len; + ssize_t act = writev(fd, buf.iov, buf.iov_cnt); + if (act < 0) + __error(1, errno, "writev"); + assert((size_t)act == exp); +} + +#define send9p(typ, ...) _send9p(fd, &ctx, LIB9P_TYP_##typ, &((struct lib9p_msg_##typ){ __VA_ARGS__ })) + +static void _recv9p(int fd) { + uint8_t buf[MAX_MSG_SIZE]; + size_t goal = 4; + size_t done = 0; + while (done < goal) { + ssize_t n = read(fd, &buf[done], goal-done); + if (n < 0) + __error(1, errno, "read"); + done += n; + } + goal = uint32le_decode(buf); + assert(goal <= MAX_MSG_SIZE); + while (done < goal) { + ssize_t n = read(fd, &buf[done], goal-done); + if (n < 0) + __error(1, errno, "read"); + done += n; + } +} + +#define recv9p() _recv9p(fd) + +int main(int argc, char *argv[]) { + if (argc != 2) + __error(2, 0, "Usage: %s SERVER_PORT", argv[0]); + uint16_t server_port = atoi(argv[1]); + + union { + struct sockaddr gen; + struct sockaddr_in in; + } server_addr = {}; + server_addr.in.sin_family = AF_INET; + server_addr.in.sin_addr.s_addr = inet_addr("127.0.0.1"); + server_addr.in.sin_port = htons(server_port); + + int fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) + __error(1, errno, "socket"); + if (connect(fd, &server_addr.gen, sizeof(server_addr)) < 0) + __error(1, errno, "connect"); + + struct lib9p_ctx ctx = { + .max_msg_size = 16*1024, + }; + + struct lib9p_s wname[1]; + + /**********************************************************************/ + + send9p(Tversion, .tag=0, .max_msg_size=(8*1024), .version=lib9p_str("9P2000")); + recv9p(); /* Rversion */ + ctx.version = LIB9P_VER_9P2000; + send9p(Tattach, .tag=0, .fid=0, .afid=LIB9P_FID_NOFID, .uname=lib9p_str("nobody"), .aname=lib9p_str("")); + recv9p(); /* Rattach */ + wname[0] = lib9p_str("shutdown"); send9p(Twalk, .tag=0, .fid=0, .newfid=0, .nwname=1, .wname=wname); + recv9p(); /* Rwalk */ + send9p(Topen, .tag=0, .fid=0, .mode=LIB9P_O_MODE_WRITE); + recv9p(); /* Ropen */ + send9p(Twrite, .tag=0, .fid=0, .offset=0, .count=2, .data="1\n"); + return 0; /* Hang up without waiting for Rwrite. */ +} diff --git a/lib9p/tests/testclient-hangup.explog b/lib9p/tests/testclient-hangup.explog new file mode 100644 index 0000000..568b0fc --- /dev/null +++ b/lib9p/tests/testclient-hangup.explog @@ -0,0 +1,14 @@ +# lib9p/tests/testclient-hangup.explog - Expected 9P logfile of testclient-hangup +# +# Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com> +# SPDX-License-Identifier: AGPL-3.0-or-later +> Tversion { tag=0 max_msg_size=8192 version="9P2000" } +< Rversion { tag=0 max_msg_size=4120 version="9P2000" } +> Tattach { tag=0 fid=0 afid=NOFID uname="nobody" aname="" unum=0 } +< Rattach { tag=0 qid={ type=(DIR) vers=1 path=1 } } +> Twalk { tag=0 fid=0 newfid=0 nwname=1 wname=[ "shutdown" ] } +< Rwalk { tag=0 nwqid=1 wqid=[ { type=(APPEND) vers=1 path=5 } ] } +> Topen { tag=0 fid=0 mode=(MODE_WRITE) } +< Ropen { tag=0 qid={ type=(APPEND) vers=1 path=5 } iounit=0 } +> Twrite { tag=0 fid=0 offset=0 count=2 data="1\n" } +< Rwrite { tag=0 count=2 } diff --git a/lib9p/tests/testclient-sess.c b/lib9p/tests/testclient-sess.c index eef9d2c..276f932 100644 --- a/lib9p/tests/testclient-sess.c +++ b/lib9p/tests/testclient-sess.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include <arpa/inet.h> /* for htons(), inet_addr() */ +#include <arpa/inet.h> /* for htons(), inet_addr() */ #include <errno.h> #include <error.h> #include <netinet/in.h> /* for struct sockaddr{,_in} */ |