Re: [PATCH v7 2/2] connect01: Add negative tests

Petr Vorel <[email protected]>
Newsgroups gmane.linux.ltp
Message-ID <20260701083825.GB477554@pevik>
Hi Wei,

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

...
> @@ -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");
>  }

...

> -	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);
> +	}

This looks to me way complicated when put into an existing test. Wouldn't be
better to create to add them to a new file (connect03.c)? Please use the style I
introduced in connect01.c (designated initializers, errnos in doc, void =>
struct sockaddr_in, ...).

Kind regards,
Petr

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.