[LTP] [PATCH v2 2/2] connect01: Add negative tests

Yang Xu xuyang2018.jy@fujitsu.com
Tue May 14 06:51:10 CEST 2024


Add negative cases for connect(), when errno is EPROTOTYPE or EACCES

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 testcases/kernel/syscalls/connect/connect01.c | 54 ++++++++++++++++++-
 1 file changed, 52 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/connect/connect01.c b/testcases/kernel/syscalls/connect/connect01.c
index 52a6f154b..97d360482 100644
--- a/testcases/kernel/syscalls/connect/connect01.c
+++ b/testcases/kernel/syscalls/connect/connect01.c
@@ -18,19 +18,26 @@
 #include "tst_test.h"
 #include "lapi/syscalls.h"
 
+#define SOCK_FILE "sock_file"
+
+static struct passwd *pw;
+
 static int fd_share;
 static int fd_server;
 
 static struct sockaddr_in sock1;
 static struct sockaddr_in sock2;
 static struct sockaddr_in sock3;
+static struct sockaddr_un sock4;
 
 static pid_t pid;
 
 static void setup1(unsigned int);
 static void setup2(unsigned int);
 static void setup3(unsigned int);
+static void setup4(unsigned int);
 static void cleanup1(void);
+static void cleanup2(void);
 
 static struct test_case_t {
 	int domain;
@@ -64,6 +71,12 @@ static struct test_case_t {
 	{PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sock3,
 		sizeof(sock3), EAFNOSUPPORT, setup2, cleanup1,
 		"address doesn't have the correct address family in sa_family"},
+	{AF_UNIX, SOCK_DGRAM, 0, (struct sockaddr *)&sock4,
+		sizeof(sock4), EPROTOTYPE, setup4, cleanup2,
+		"socket type does not support the protocol"},
+	{AF_UNIX, SOCK_STREAM, 0, (struct sockaddr *)&sock4,
+		sizeof(sock4), EACCES, setup4, cleanup2,
+		"write permission is denied on the socket file"},
 };
 
 static int sys_connect(int sockfd, const struct sockaddr *addr,
@@ -145,6 +158,11 @@ 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));
+
+	pw = SAFE_GETPWNAM("nobody");
 }
 
 static void setup1(unsigned int i)
@@ -170,21 +188,53 @@ static void setup3(unsigned int i)
 	SAFE_CONNECT(fd_server, (const struct sockaddr *)&sock1, sizeof(sock1));
 }
 
+static void setup4(unsigned int i)
+{
+	struct test_case_t *tc = &tcases[i];
+
+	fd_share = SAFE_SOCKET(tc->domain, tc->type, tc->proto);
+	fd_server = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
+	SAFE_BIND(fd_server, tc->sockaddr, tc->salen);
+	SAFE_LISTEN(fd_server, 5);
+}
+
 static void cleanup1(void)
 {
 	if (fd_share > 0)
 		SAFE_CLOSE(fd_share);
 }
 
+static void cleanup2(void)
+{
+	if (fd_share > 0)
+		SAFE_CLOSE(fd_share);
+	if (fd_server > 0)
+		SAFE_CLOSE(fd_server);
+	SAFE_UNLINK(SOCK_FILE);
+}
+
 static void verify_connect(unsigned int i)
 {
 	struct test_case_t *tc = &tcases[i];
+	pid_t pid_child;
 
 	start_server(&sock1);
 	if (tc->setup)
 		tc->setup(i);
-	TST_EXP_FAIL(sys_connect(fd_share, tc->sockaddr, tc->salen),
-		tc->exp_errno, "%s", tc->desc);
+	if (tc->exp_errno == EACCES) {
+		pid_child = SAFE_FORK();
+		if (!pid_child) {
+			SAFE_SETUID(pw->pw_uid);
+			TST_EXP_FAIL(
+				sys_connect(fd_share, tc->sockaddr, tc->salen),
+				tc->exp_errno, "%s", tc->desc);
+			exit(0);
+		}
+		SAFE_WAITPID(pid_child, NULL, 0);
+	} else {
+		TST_EXP_FAIL(sys_connect(fd_share, tc->sockaddr, tc->salen),
+			tc->exp_errno, "%s", tc->desc);
+	}
 	SAFE_KILL(pid, SIGKILL);
 	SAFE_WAITPID(pid, NULL, 0);
 	if (tc->cleanup)
-- 
2.39.3



More information about the ltp mailing list