[LTP] [PATCH] bind: Add negative tests for bind

Martin Doucha mdoucha@suse.cz
Mon May 6 17:54:12 CEST 2024


Hi,
wouldn't it be better to add these testcases to bind01?

Also, why .needs_root = 1?

On 12. 04. 24 10:05, Yang Xu via ltp wrote:
> 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,
> +};

-- 
Martin Doucha   mdoucha@suse.cz
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic



More information about the ltp mailing list