* [LTP] [PATCH] gethostname: Add negative test for gethostname
@ 2024-04-16 8:04 Yang Xu via ltp
2024-04-18 16:18 ` Avinesh Kumar
0 siblings, 1 reply; 2+ messages in thread
From: Yang Xu via ltp @ 2024-04-16 8:04 UTC (permalink / raw
To: ltp
Add negative cases for gethostname(), when errno is ENAMETOOLONG
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
runtest/syscalls | 1 +
.../kernel/syscalls/gethostname/.gitignore | 1 +
.../syscalls/gethostname/gethostname02.c | 41 +++++++++++++++++++
3 files changed, 43 insertions(+)
create mode 100644 testcases/kernel/syscalls/gethostname/gethostname02.c
diff --git a/runtest/syscalls b/runtest/syscalls
index de4f5a633..cc1e39c05 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -477,6 +477,7 @@ gethostbyname_r01 gethostbyname_r01
gethostid01 gethostid01
gethostname01 gethostname01
+gethostname02 gethostname02
getitimer01 getitimer01
getitimer02 getitimer02
diff --git a/testcases/kernel/syscalls/gethostname/.gitignore b/testcases/kernel/syscalls/gethostname/.gitignore
index d09d5d288..d6e4cffcb 100644
--- a/testcases/kernel/syscalls/gethostname/.gitignore
+++ b/testcases/kernel/syscalls/gethostname/.gitignore
@@ -1 +1,2 @@
/gethostname01
+/gethostname02
diff --git a/testcases/kernel/syscalls/gethostname/gethostname02.c b/testcases/kernel/syscalls/gethostname/gethostname02.c
new file mode 100644
index 000000000..e9d97d05a
--- /dev/null
+++ b/testcases/kernel/syscalls/gethostname/gethostname02.c
@@ -0,0 +1,41 @@
+// 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 gethostname(2) fails with
+ *
+ * - ENAMETOOLONG when len is smaller than the actual size
+ */
+
+#include "tst_test.h"
+
+static char hostname_enametoolong[100];
+
+static struct test_case_t {
+ char *hostname;
+ int size;
+ int expected_errno;
+ char *desc;
+} tcases[] = {
+ {hostname_enametoolong, 1, ENAMETOOLONG,
+ "len is smaller than the actual size"},
+};
+
+static void verify_gethostname(unsigned int i)
+{
+ struct test_case_t *tc = &tcases[i];
+
+ TST_EXP_FAIL(gethostname(tc->hostname, sizeof(tc->size)),
+ tc->expected_errno, "%s", tc->desc);
+}
+
+static struct tst_test test = {
+ .tcnt = ARRAY_SIZE(tcases),
+ .test = verify_gethostname,
+ .needs_root = 1,
+};
--
2.39.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [LTP] [PATCH] gethostname: Add negative test for gethostname
2024-04-16 8:04 [LTP] [PATCH] gethostname: Add negative test for gethostname Yang Xu via ltp
@ 2024-04-18 16:18 ` Avinesh Kumar
0 siblings, 0 replies; 2+ messages in thread
From: Avinesh Kumar @ 2024-04-18 16:18 UTC (permalink / raw
To: ltp
Hi Yang Xu,
Please see comments below
On Tuesday, April 16, 2024 10:04:14 AM GMT+2 Yang Xu via ltp wrote:
> Add negative cases for gethostname(), when errno is ENAMETOOLONG
>
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
> runtest/syscalls | 1 +
> .../kernel/syscalls/gethostname/.gitignore | 1 +
> .../syscalls/gethostname/gethostname02.c | 41 +++++++++++++++++++
> 3 files changed, 43 insertions(+)
> create mode 100644 testcases/kernel/syscalls/gethostname/gethostname02.c
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index de4f5a633..cc1e39c05 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -477,6 +477,7 @@ gethostbyname_r01 gethostbyname_r01
> gethostid01 gethostid01
>
> gethostname01 gethostname01
> +gethostname02 gethostname02
>
> getitimer01 getitimer01
> getitimer02 getitimer02
> diff --git a/testcases/kernel/syscalls/gethostname/.gitignore
> b/testcases/kernel/syscalls/gethostname/.gitignore index
> d09d5d288..d6e4cffcb 100644
> --- a/testcases/kernel/syscalls/gethostname/.gitignore
> +++ b/testcases/kernel/syscalls/gethostname/.gitignore
> @@ -1 +1,2 @@
> /gethostname01
> +/gethostname02
> diff --git a/testcases/kernel/syscalls/gethostname/gethostname02.c
> b/testcases/kernel/syscalls/gethostname/gethostname02.c new file mode
> 100644
> index 000000000..e9d97d05a
> --- /dev/null
> +++ b/testcases/kernel/syscalls/gethostname/gethostname02.c
> @@ -0,0 +1,41 @@
> +// 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 gethostname(2) fails with
> + *
> + * - ENAMETOOLONG when len is smaller than the actual size
> + */
> +
> +#include "tst_test.h"
> +
> +static char hostname_enametoolong[100];
> +
> +static struct test_case_t {
> + char *hostname;
> + int size;
> + int expected_errno;
> + char *desc;
> +} tcases[] = {
> + {hostname_enametoolong, 1, ENAMETOOLONG,
If I found correctly, minimum possible size for hostname can be just 1 char.
So if we want to test ENAMETOOLONG errno by passing a len value which is not
enough to store the hostname value, maybe one approach can be, first fetch the
hostname value by a successful gethostname() call, and then pass the smaller
len value than the received hostname length to verify this errno scenario.
Also, if we are testing just a single case, imho lets not use the
test_case_t struct.
> + "len is smaller than the actual size"},
> +};
> +
> +static void verify_gethostname(unsigned int i)
> +{
> + struct test_case_t *tc = &tcases[i];
> +
> + TST_EXP_FAIL(gethostname(tc->hostname, sizeof(tc->size)),
I am confused why we have sizeof() here.
> + tc->expected_errno, "%s", tc->desc);
> +}
> +
> +static struct tst_test test = {
> + .tcnt = ARRAY_SIZE(tcases),
> + .test = verify_gethostname,
> + .needs_root = 1,
I guess root is not required.
> +};
Regards,
Avinesh
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-04-18 16:19 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-16 8:04 [LTP] [PATCH] gethostname: Add negative test for gethostname Yang Xu via ltp
2024-04-18 16:18 ` Avinesh Kumar
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.