Linux Test Project
 help / color / mirror / Atom feed
From: Wei Gao via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v7 2/2] connect01: Add negative tests
Date: Tue, 16 Jun 2026 09:31:05 +0000	[thread overview]
Message-ID: <20260616093119.8654-3-wegao@suse.com> (raw)
In-Reply-To: <20260616093119.8654-1-wegao@suse.com>

Add negative cases for connect(), when errno is EPROTOTYPE or EACCES.
These error paths were not exercised by the existing test.

Signed-off-by: Wei Gao <wegao@suse.com>
---
 testcases/kernel/syscalls/connect/connect01.c | 54 +++++++++++++++++--
 1 file changed, 51 insertions(+), 3 deletions(-)

diff --git a/testcases/kernel/syscalls/connect/connect01.c b/testcases/kernel/syscalls/connect/connect01.c
index fd288559e..79aa899eb 100644
--- a/testcases/kernel/syscalls/connect/connect01.c
+++ b/testcases/kernel/syscalls/connect/connect01.c
@@ -5,26 +5,39 @@
  */
 
 /*\
- * Verify that :manpage:`connect(2)` returns the proper errno for various failure cases.
+ * Verify that :manpage:`connect(2)` returns the proper errno
+ * for various failure cases.
+ *
+ * Requires root to test EACCES by dropping privileges to an
+ * unprivileged user.
  */
 
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
+#include <sys/un.h>
 #include <netinet/in.h>
+#include <pwd.h>
 #include "tst_test.h"
 #include "lapi/syscalls.h"
 
+#define SOCK_FILE "sock_file"
+
 static int fd_invalid = -1;
 static int fd_socket = -1;
 static int fd_null = -1;
 static int fd_connected = -1;
 static int fd_server = -1;
+static int fd_unix_dgram = -1;
+static int fd_unix_stream = -1;
+static int fd_unix_server = -1;
 
 static struct sockaddr_in sock1;
 static struct sockaddr_in sock2;
 static struct sockaddr_in sock3;
+static struct sockaddr_un sock4;
 static void *bad_addr;
+static struct passwd *pw;
 
 static pid_t pid;
 
@@ -49,6 +62,10 @@ static struct test_case_t {
 		"connect on a socket found no one listening on remote address"},
 	{&fd_socket, &sock3, sizeof(sock3), EAFNOSUPPORT,
 		"address doesn't have the correct address family in sa_family"},
+	{&fd_unix_dgram, &sock4, sizeof(sock4), EPROTOTYPE,
+		"socket type does not support the protocol"},
+	{&fd_unix_stream, &sock4, sizeof(sock4), EACCES,
+		"write permission is denied on the socket file"},
 };
 
 static int sys_connect(int sockfd, const struct sockaddr *addr,
@@ -103,6 +120,19 @@ static void setup(void)
 	sock3.sin_family = 47;
 	sock3.sin_port = 0;
 	sock3.sin_addr.s_addr = htonl(0x0AFFFEFD);
+
+	sock4.sun_family = AF_UNIX;
+	strncpy(sock4.sun_path, SOCK_FILE, sizeof(sock4.sun_path));
+
+	fd_unix_server = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
+	SAFE_BIND(fd_unix_server, (struct sockaddr *)&sock4, sizeof(sock4));
+	SAFE_CHMOD(SOCK_FILE, 0700);
+	SAFE_LISTEN(fd_unix_server, 5);
+
+	fd_unix_dgram = SAFE_SOCKET(AF_UNIX, SOCK_DGRAM, 0);
+	fd_unix_stream = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
+
+	pw = SAFE_GETPWNAM("nobody");
 }
 
 static void cleanup(void)
@@ -115,6 +145,12 @@ static void cleanup(void)
 		SAFE_CLOSE(fd_connected);
 	if (fd_server != -1)
 		SAFE_CLOSE(fd_server);
+	if (fd_unix_dgram != -1)
+		SAFE_CLOSE(fd_unix_dgram);
+	if (fd_unix_stream != -1)
+		SAFE_CLOSE(fd_unix_stream);
+	if (fd_unix_server != -1)
+		SAFE_CLOSE(fd_unix_server);
 
 	if (pid > 0) {
 		SAFE_KILL(pid, SIGKILL);
@@ -127,8 +163,18 @@ static void verify_connect(unsigned int i)
 	struct test_case_t *tc = &tcases[i];
 	void *addr = tc->addr ? tc->addr : bad_addr;
 
-	TST_EXP_FAIL(sys_connect(*tc->fd, addr, tc->salen),
-		     tc->exp_errno, "%s", tc->desc);
+	if (tc->exp_errno == EACCES) {
+		if (!SAFE_FORK()) {
+			SAFE_SETUID(pw->pw_uid);
+			TST_EXP_FAIL(sys_connect(*tc->fd, addr, tc->salen),
+				     tc->exp_errno, "%s", tc->desc);
+			exit(0);
+		}
+		tst_reap_children();
+	} else {
+		TST_EXP_FAIL(sys_connect(*tc->fd, addr, tc->salen),
+			     tc->exp_errno, "%s", tc->desc);
+	}
 }
 
 static struct tst_test test = {
@@ -137,4 +183,6 @@ static struct tst_test test = {
 	.tcnt = ARRAY_SIZE(tcases),
 	.test = verify_connect,
 	.forks_child = 1,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
 };
-- 
2.54.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2026-06-16  9:32 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-14  4:51 [LTP] [PATCH v2 1/2] connect01: Convert to new API Yang Xu via ltp
2024-05-14  4:51 ` [LTP] [PATCH v2 2/2] connect01: Add negative tests Yang Xu via ltp
2024-07-17 12:30 ` [LTP] [PATCH v2 1/2] connect01: Convert to new API Cyril Hrubis
2026-05-25  9:02 ` [LTP] [PATCH v3 0/2] " Wei Gao via ltp
2026-05-25  9:02   ` [LTP] [PATCH v3 1/2] " Wei Gao via ltp
2026-05-25 10:12     ` [LTP] " linuxtestproject.agent
2026-05-28  4:43       ` Wei Gao via ltp
2026-06-04  4:49     ` [LTP] [PATCH v4 0/2] " Wei Gao via ltp
2026-06-04  4:49       ` [LTP] [PATCH v4 1/2] " Wei Gao via ltp
2026-06-04  7:15         ` [LTP] " linuxtestproject.agent
2026-06-16  1:47         ` [LTP] [PATCH v5 0/2] " Wei Gao via ltp
2026-06-16  1:47           ` [LTP] [PATCH v5 1/2] " Wei Gao via ltp
2026-06-16  4:07             ` [LTP] " linuxtestproject.agent
2026-06-16  5:24             ` [LTP] [PATCH v6 0/2] " Wei Gao via ltp
2026-06-16  5:24               ` [LTP] [PATCH v6 1/2] " Wei Gao via ltp
2026-06-16  8:35                 ` [LTP] " linuxtestproject.agent
2026-06-16  9:31                 ` [LTP] [PATCH v7 0/2] " Wei Gao via ltp
2026-06-16  9:31                   ` [LTP] [PATCH v7 1/2] " Wei Gao via ltp
2026-06-16 10:12                     ` [LTP] " linuxtestproject.agent
2026-06-16  9:31                   ` Wei Gao via ltp [this message]
2026-06-16  5:24               ` [LTP] [PATCH v6 2/2] connect01: Add negative tests Wei Gao via ltp
2026-06-16  1:47           ` [LTP] [PATCH v5 " Wei Gao via ltp
2026-06-04  4:49       ` [LTP] [PATCH v4 " Wei Gao via ltp
2026-05-25  9:02   ` [LTP] [PATCH v3 " Wei Gao via ltp

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=20260616093119.8654-3-wegao@suse.com \
    --to=ltp@lists.linux.it \
    --cc=wegao@suse.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