]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: (lsfd) add a case testing UDP sockets
authorMasatake YAMATO <yamato@redhat.com>
Wed, 19 Oct 2022 20:17:38 +0000 (05:17 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Wed, 19 Oct 2022 20:17:47 +0000 (05:17 +0900)
This tests UDP.LADDR, UDP.LPORT, UDP.RADDR, and UDP.RPORT.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
tests/expected/lsfd/mkfds-udp [new file with mode: 0644]
tests/helpers/test_mkfds.c
tests/ts/lsfd/mkfds-udp [new file with mode: 0755]

diff --git a/tests/expected/lsfd/mkfds-udp b/tests/expected/lsfd/mkfds-udp
new file mode 100644 (file)
index 0000000..9eb7a1e
--- /dev/null
@@ -0,0 +1,12 @@
+    3  UDP   SOCK state=close laddr=127.0.0.1:34567                                   close     dgram              0  127.0.0.1    0.0.0.0 127.0.0.1:34567     34567       0.0.0.0:0         0
+    4  UDP   SOCK state=established laddr=127.0.0.1:23456 raddr=127.0.0.1:34567 established     dgram              0  127.0.0.1  127.0.0.1 127.0.0.1:23456     23456 127.0.0.1:34567     34567
+ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT: 0
+    3  UDP   SOCK                                   0                                                     
+ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT: 0
+NAME pattern match: OK
+    4  UDP   SOCK established     dgram              0  127.0.0.1  127.0.0.1 127.0.0.1:34567     34567
+ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.RADDR,UDP.RPORT: 0
+LADDR/LPORT pattern match: OK
+    3  UDP   SOCK state=close laddr=127.0.0.1:34567      close     dgram              0  127.0.0.1    0.0.0.0 127.0.0.1:34567     34567 0.0.0.0:0         0
+    4  UDP   SOCK state=close laddr=127.0.0.1:23456      close     dgram              0  127.0.0.1    0.0.0.0 127.0.0.1:23456     23456 0.0.0.0:0         0
+ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT: 0
index c43c3de2f286c60c534ad8d7b9df0676a779e15e..9c072ea97ed8b4522c5e7b74619bef02e0c3eff8 100644 (file)
@@ -1372,6 +1372,137 @@ static void *make_tcp(const struct factory *factory, struct fdesc fdescs[],
        return NULL;
 }
 
+static void *make_udp(const struct factory *factory, struct fdesc fdescs[],
+                                   int argc, char ** argv)
+{
+       struct arg server_port = decode_arg("server-port", factory->params, argc, argv);
+       unsigned short iserver_port = (unsigned short)ARG_INTEGER(server_port);
+       struct arg client_port = decode_arg("client-port", factory->params, argc, argv);
+       unsigned short iclient_port = (unsigned short)ARG_INTEGER(client_port);
+
+       struct arg server_do_bind = decode_arg("server-do-bind", factory->params, argc, argv);
+       bool bserver_do_bind = ARG_BOOLEAN(server_do_bind);
+       struct arg client_do_bind = decode_arg("client-do-bind", factory->params, argc, argv);
+       bool bclient_do_bind = ARG_BOOLEAN(client_do_bind);
+       struct arg client_do_connect = decode_arg("client-do-connect", factory->params, argc, argv);
+       bool bclient_do_connect = ARG_BOOLEAN(client_do_connect);
+
+       struct sockaddr_in sin, cin;
+       int ssd, csd;
+
+       const int y = 1;
+
+       free_arg(&client_do_connect);
+       free_arg(&client_do_bind);
+       free_arg(&server_do_bind);
+       free_arg(&server_port);
+       free_arg(&client_port);
+
+       ssd = socket(AF_INET, SOCK_DGRAM, 0);
+       if (ssd < 0)
+               err(EXIT_FAILURE,
+                   _("failed to make a udp socket for server"));
+
+       if (setsockopt(ssd, SOL_SOCKET,
+                      SO_REUSEADDR, (const char *)&y, sizeof(y)) < 0) {
+               int e = errno;
+               close(ssd);
+               errno = e;
+               err(EXIT_FAILURE, "failed to setsockopt(SO_REUSEADDR)");
+       }
+
+       if (ssd != fdescs[0].fd) {
+               if (dup2(ssd, fdescs[0].fd) < 0) {
+                       int e = errno;
+                       close(ssd);
+                       errno = e;
+                       err(EXIT_FAILURE, "failed to dup %d -> %d", ssd, fdescs[0].fd);
+               }
+               close(ssd);
+               ssd = fdescs[0].fd;
+       }
+
+       memset(&sin, 0, sizeof(sin));
+       sin.sin_family = AF_INET;
+       sin.sin_port = htons(iserver_port);
+       sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+       if (bserver_do_bind) {
+               if (bind(ssd, &sin, sizeof(sin)) < 0) {
+                       int e = errno;
+                       close(ssd);
+                       errno = e;
+                       err(EXIT_FAILURE, "failed to bind a srever socket");
+               }
+       }
+
+       csd = socket(AF_INET, SOCK_DGRAM, 0);
+       if (csd < 0) {
+               int e = errno;
+               close(ssd);
+               errno = e;
+               err(EXIT_FAILURE,
+                   _("failed to make a udp client socket"));
+       }
+
+       if (setsockopt(csd, SOL_SOCKET,
+                      SO_REUSEADDR, (const char *)&y, sizeof(y)) < 0) {
+               int e = errno;
+               close(ssd);
+               close(csd);
+               errno = e;
+               err(EXIT_FAILURE, "failed to setsockopt(SO_REUSEADDR)");
+       }
+
+       if (csd != fdescs[1].fd) {
+               if (dup2(csd, fdescs[1].fd) < 0) {
+                       int e = errno;
+                       close(ssd);
+                       close(csd);
+                       errno = e;
+                       err(EXIT_FAILURE, "failed to dup %d -> %d", csd, fdescs[1].fd);
+               }
+               close(csd);
+               csd = fdescs[1].fd;
+       }
+
+       if (bclient_do_bind) {
+               memset(&cin, 0, sizeof(cin));
+               cin.sin_family = AF_INET;
+               cin.sin_port = htons(iclient_port);
+               cin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+               if (bind(csd, &cin, sizeof(cin)) < 0) {
+                       int e = errno;
+                       close(ssd);
+                       close(csd);
+                       errno = e;
+                       err(EXIT_FAILURE, "failed to bind a client socket");
+               }
+       }
+
+       if (bclient_do_connect) {
+               if (connect(csd, &sin, sizeof(sin)) < 0) {
+                       int e = errno;
+                       close(ssd);
+                       close(csd);
+                       errno = e;
+                       err(EXIT_FAILURE, "failed to connect a client socket to the serer socket");
+               }
+       }
+
+       fdescs[0] = (struct fdesc) {
+               .fd    = fdescs[0].fd,
+               .close = close_fdesc,
+               .data  = NULL,
+       };
+       fdescs[1] = (struct fdesc) {
+               .fd    = fdescs[1].fd,
+               .close = close_fdesc,
+               .data  = NULL,
+       };
+
+       return NULL;
+}
+
 #define PARAM_END { .name = NULL, }
 static const struct factory factories[] = {
        {
@@ -1690,6 +1821,47 @@ static const struct factory factories[] = {
                        PARAM_END
                }
        },
+       {
+               .name = "udp",
+               .desc = "AF_INET+SOCK_DGRAM sockets",
+               .priv = false,
+               .N    = 2,
+               .EX_N = 0,
+               .make = make_udp,
+               .params = (struct parameter []) {
+                       {
+                               .name = "server-port",
+                               .type = PTYPE_INTEGER,
+                               .desc = "TCP port the server may listen",
+                               .defv.integer = 12345,
+                       },
+                       {
+                               .name = "client-port",
+                               .type = PTYPE_INTEGER,
+                               .desc = "TCP port the client may bind",
+                               .defv.integer = 23456,
+                       },
+                       {
+                               .name = "server-do-bind",
+                               .type = PTYPE_BOOLEAN,
+                               .desc = "call bind with the server socket",
+                               .defv.boolean = true,
+                       },
+                       {
+                               .name = "client-do-bind",
+                               .type = PTYPE_BOOLEAN,
+                               .desc = "call bind with the client socket",
+                               .defv.boolean = true,
+                       },
+                       {
+                               .name = "client-do-connect",
+                               .type = PTYPE_BOOLEAN,
+                               .desc = "call connect with the client socket",
+                               .defv.boolean = true,
+                       },
+                       PARAM_END
+               }
+       }
 };
 
 static int count_parameters(const struct factory *factory)
diff --git a/tests/ts/lsfd/mkfds-udp b/tests/ts/lsfd/mkfds-udp
new file mode 100755 (executable)
index 0000000..01be789
--- /dev/null
@@ -0,0 +1,121 @@
+#!/bin/bash
+#
+# Copyright (C) 2022 Masatake YAMATO <yamato@redhat.com>
+#
+# This file is part of util-linux.
+#
+# This file 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 2 of the License, or
+# (at your option) any later version.
+#
+# This file 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.
+#
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="UDP sockets"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_LSFD"
+ts_check_test_command "$TS_HELPER_MKFDS"
+
+ts_cd "$TS_OUTDIR"
+
+PID=
+FDS=3
+FDC=4
+EXTRA_PARAMS=
+EXPR='(TYPE == "UDP") and (FD >= 3) and (FD <= 4)'
+EXPR_server='(TYPE == "UDP") and (FD == 3)'
+EXPR_client='(TYPE == "UDP") and (FD == 4)'
+NAME=
+LADDR=
+LPORT=
+
+{
+    coproc MKFDS { "$TS_HELPER_MKFDS" udp $FDS $FDC \
+                                     server-port=34567 \
+                                     client-port=23456 ; }
+    if read -r -u "${MKFDS[0]}" PID; then
+       ${TS_CMD_LSFD} -n \
+                      -o ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT \
+                      -p "${PID}" -Q "${EXPR}"
+       echo 'ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT': $?
+
+       kill -CONT "${PID}"
+       wait "${MKFDS_PID}"
+    fi
+
+    coproc MKFDS { "$TS_HELPER_MKFDS" udp $FDS $FDC \
+                                     server-port=34567 \
+                                     client-port=23456 \
+                                     server-do-bind=no; }
+    if read -r -u "${MKFDS[0]}" PID; then
+       ${TS_CMD_LSFD} -n \
+                      -o ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT \
+                      -p "${PID}" -Q "${EXPR_server}"
+       echo 'ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT': $?
+
+       NAME=$(${TS_CMD_LSFD} -n \
+                              --raw \
+                             -o NAME \
+                             -p "${PID}" -Q "${EXPR_server}")
+       if [[ "$NAME" =~ ^socket:\[[[:digit:]]+\]$ ]]; then
+           echo "NAME pattern match: OK"
+       else
+           echo "NAME pattern match: FAILED (NAME=$NAME)"
+       fi
+
+       kill -CONT "${PID}"
+       wait "${MKFDS_PID}"
+    fi
+
+    coproc MKFDS { "$TS_HELPER_MKFDS" udp $FDS $FDC \
+                                     server-port=34567 \
+                                     client-port=23456 \
+                                     client-do-bind=no; }
+    if read -r -u "${MKFDS[0]}" PID; then
+       ${TS_CMD_LSFD} -n \
+                      -o ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.RADDR,UDP.RPORT \
+                      -p "${PID}" -Q "${EXPR_client}"
+       echo 'ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.RADDR,UDP.RPORT': $?
+
+       LADDR=$(${TS_CMD_LSFD} -n \
+                              --raw \
+                              -o UDP.LADDR \
+                              -p "${PID}" -Q "${EXPR_client}")
+       LPORT=$(${TS_CMD_LSFD} -n \
+                              --raw \
+                              -o UDP.LPORT \
+                              -p "${PID}" -Q "${EXPR_client}")
+
+       if [[ 127.0.0.1:$LPORT == ${LADDR} ]]; then
+           echo "LADDR/LPORT pattern match: OK"
+       else
+           echo "LADDR/LPORT pattern match: FAILED (LADDR=$LADDR, LPORT=$LPORT)"
+       fi
+       
+       kill -CONT "${PID}"
+       wait "${MKFDS_PID}"
+    fi
+
+    coproc MKFDS { "$TS_HELPER_MKFDS" udp $FDS $FDC \
+                                     server-port=34567 \
+                                     client-port=23456 \
+                                     client-do-connect=no; }
+    if read -r -u "${MKFDS[0]}" PID; then
+       ${TS_CMD_LSFD} -n \
+                      -o ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT \
+                      -p "${PID}" -Q "${EXPR}"
+       echo 'ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT': $?
+
+       kill -CONT "${PID}"
+       wait "${MKFDS_PID}"
+    fi
+} > "$TS_OUTPUT" 2>&1
+
+ts_finalize