From: Yang Xu via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] bind: Add negative tests for bind
Date: Fri, 12 Apr 2024 04:05:57 -0400 [thread overview]
Message-ID: <20240412080557.18922-1-xuyang2018.jy@fujitsu.com> (raw)
Add negative cases for bind(), when errno is EBADF or ENOTDIR
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/bind/.gitignore | 1 +
testcases/kernel/syscalls/bind/bind07.c | 81 +++++++++++++++++++++++
3 files changed, 83 insertions(+)
create mode 100644 testcases/kernel/syscalls/bind/bind07.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 3521047f4..71579cde4 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -37,6 +37,7 @@ bind03 bind03
bind04 bind04
bind05 bind05
bind06 bind06
+bind07 bind07
bpf_map01 bpf_map01
bpf_prog01 bpf_prog01
diff --git a/testcases/kernel/syscalls/bind/.gitignore b/testcases/kernel/syscalls/bind/.gitignore
index c85774441..8aff2456f 100644
--- a/testcases/kernel/syscalls/bind/.gitignore
+++ b/testcases/kernel/syscalls/bind/.gitignore
@@ -4,3 +4,4 @@
/bind04
/bind05
/bind06
+/bind07
diff --git a/testcases/kernel/syscalls/bind/bind07.c b/testcases/kernel/syscalls/bind/bind07.c
new file mode 100644
index 000000000..dda6e8ad4
--- /dev/null
+++ b/testcases/kernel/syscalls/bind/bind07.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that bind(2) fails with
+ *
+ * - EBADF when sockfd is not a valid file descriptor
+ * - ENOTDIR when a component of addr prefix is not a directory
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include "tst_test.h"
+#include "libbind.h"
+
+#define TEST_EBADF "test_ebadf"
+#define DIR_ENOTDIR "dir_enotdir"
+#define TEST_ENOTDIR "test_enotdir"
+
+static struct sockaddr_in sock_ebadf;
+static struct sockaddr_un sock_enotdir;
+
+static struct test_case_t {
+ int sockfd;
+ struct sockaddr *addr;
+ socklen_t addrlen;
+ char *sockfile;
+ int type;
+ int protocol;
+ int expected_errno;
+ char *desc;
+} tcases[] = {
+ {-1, (struct sockaddr *)&sock_ebadf, sizeof(sock_ebadf), TEST_EBADF,
+ SOCK_STREAM, IPPROTO_TCP, EBADF,
+ "bind() sockfd is not a valid file descriptor"},
+ {0, (struct sockaddr *)&sock_enotdir, sizeof(sock_enotdir),
+ DIR_ENOTDIR "/" TEST_ENOTDIR, SOCK_STREAM, 0, ENOTDIR,
+ "bind() a component of addr prefix is not a directory"},
+};
+
+static void setup(void)
+{
+ tst_init_sockaddr_inet(&sock_ebadf, IPV4_ADDRESS, 0);
+
+ SAFE_TOUCH(DIR_ENOTDIR, 0777, NULL);
+ sock_enotdir.sun_family = AF_UNIX;
+ strncpy(sock_enotdir.sun_path, DIR_ENOTDIR "/" TEST_ENOTDIR,
+ sizeof(sock_enotdir.sun_path));
+}
+
+static void verify_bind(unsigned int i)
+{
+ struct test_case_t *tc = &tcases[i];
+
+ int sockfd = tc->sockfd;
+
+ if (!sockfd)
+ sockfd = SAFE_SOCKET(tc->addr->sa_family, tc->type,
+ tc->protocol);
+
+ TST_EXP_FAIL(bind(sockfd, tc->addr, tc->addrlen), tc->expected_errno,
+ "%s", tc->desc);
+
+ if (sockfd > 0)
+ SAFE_CLOSE(sockfd);
+ if (!TST_RET)
+ SAFE_UNLINK(tc->sockfile);
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .tcnt = ARRAY_SIZE(tcases),
+ .test = verify_bind,
+ .needs_tmpdir = 1,
+ .needs_root = 1,
+};
--
2.39.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next reply other threads:[~2024-04-12 8:40 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-12 8:05 Yang Xu via ltp [this message]
2024-05-06 15:54 ` [LTP] [PATCH] bind: Add negative tests for bind Martin Doucha
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240412080557.18922-1-xuyang2018.jy@fujitsu.com \
--to=ltp@lists.linux.it \
--cc=xuyang2018.jy@fujitsu.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).