diff mbox series

[v1] getcwd01: Implement .test_variants

Message ID 20231206105318.11832-1-wegao@suse.com
State Changes Requested
Headers show
Series [v1] getcwd01: Implement .test_variants | expand

Commit Message

Wei Gao Dec. 6, 2023, 10:53 a.m. UTC
Signed-off-by: Wei Gao <wegao@suse.com>
---
 testcases/kernel/syscalls/getcwd/getcwd01.c | 81 ++++++++++++++-------
 1 file changed, 55 insertions(+), 26 deletions(-)

Comments

Petr Vorel Dec. 15, 2023, 7:47 p.m. UTC | #1
Hi Wei,

>  /*
>   * DESCRIPTION
> - * Testcase to test that getcwd(2) sets errno correctly.
> - * 1) getcwd(2) fails if buf points to a bad address.
> - * 2) getcwd(2) fails if the size is invalid.
> - * 3) getcwd(2) fails if the size is set to 0.
> - * 4) getcwd(2) fails if the size is set to 1.
> - * 5) getcwd(2) fails if buf points to NULL and the size is set to 1.
> - *
> - * Expected Result:
> - * 1) getcwd(2) should return NULL and set errno to EFAULT.
> - * 2) getcwd(2) should return NULL and set errno to EFAULT.
> - * 3) getcwd(2) should return NULL and set errno to ERANGE.
> - * 4) getcwd(2) should return NULL and set errno to ERANGE.
> - * 5) getcwd(2) should return NULL and set errno to ERANGE.
> + * Testcase to test that getcwd() sets errno correctly.

We prefer to keep info about tested errno (you can write errno or errnos if they
are different for syscall into it).

* 1) getcwd(2) fails if buf points to a bad address (EFAULT)
* 2) getcwd(2) fails if the size is invalid (ERANGE)

>  #include <errno.h>
> @@ -27,28 +16,68 @@
>  #include "lapi/syscalls.h"

>  static char buffer[5];
> -
> -static struct t_case {
> +struct getcwd_variants {
> +	void (*getcwd)(char *buf, size_t size, int exp_err);
>  	char *buf;
>  	size_t size;
>  	int exp_err;
> -} tcases[] = {
> -	{(void *)-1, PATH_MAX, EFAULT},
> -	{NULL, (size_t)-1, EFAULT},
> -	{buffer, 0, ERANGE},
> -	{buffer, 1, ERANGE},
> -	{NULL, 1, ERANGE}
>  };

> +static void verify_getcwd_raw_syscall(char *buf, size_t size, int exp_err);
> +static void verify_getcwd(char *buf, size_t size, int exp_err);
> +
> +static struct getcwd_variants variants[] = {
> +#ifdef __GLIBC__
> +	{ .getcwd = verify_getcwd, .buf = NULL, .size = (size_t)-1, .exp_err = ENOMEM},
> +	{ .getcwd = verify_getcwd, .buf = NULL, .size = 1, .exp_err = ERANGE},
> +#endif
> +	{ .getcwd = verify_getcwd, .buf = (void *)-1, .size = PATH_MAX, .exp_err = EFAULT},
> +	{ .getcwd = verify_getcwd, .buf = buffer, .size = 0, .exp_err = EINVAL},
> +	{ .getcwd = verify_getcwd, .buf = buffer, .size = 1, .exp_err = ERANGE},
> +	{ .getcwd = verify_getcwd_raw_syscall, .buf = buffer, .size = 0, .exp_err = ERANGE},
> +	{ .getcwd = verify_getcwd_raw_syscall, .buf = (void *)-1, .size = PATH_MAX, .exp_err = EFAULT},
> +	{ .getcwd = verify_getcwd_raw_syscall, .buf = NULL, .size = (size_t)-1, .exp_err = EFAULT},
> +	{ .getcwd = verify_getcwd_raw_syscall, .buf = buffer, .size = 0, .exp_err = ERANGE},
> +	{ .getcwd = verify_getcwd_raw_syscall, .buf = buffer, .size = 1, .exp_err = ERANGE},
> +	{ .getcwd = verify_getcwd_raw_syscall, .buf = NULL, .size = 1, .exp_err = ERANGE},

Well, this is works, but we don't use test variants this way. We define
.test_variants in struct tst_test. Please have look how getdents01.c for
example.

Why it's important (besides consistency of how we write tests)? We parse
test_variants in our documentation.
https://github.com/linux-test-project/ltp/releases/download/20230929/metadata.20230929.html
=> see metadata.html#test_variants in your locally downloaded file, or run:
cd metadata && make && chromium ../docparse/*.html

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/getcwd/getcwd01.c b/testcases/kernel/syscalls/getcwd/getcwd01.c
index 218bf4ef2..e62254deb 100644
--- a/testcases/kernel/syscalls/getcwd/getcwd01.c
+++ b/testcases/kernel/syscalls/getcwd/getcwd01.c
@@ -1,23 +1,12 @@ 
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines Corp., 2001
+ * Copyright (c) 2023 Wei Gao <wegao@suse.com>
  */
 
 /*
  * DESCRIPTION
- * Testcase to test that getcwd(2) sets errno correctly.
- * 1) getcwd(2) fails if buf points to a bad address.
- * 2) getcwd(2) fails if the size is invalid.
- * 3) getcwd(2) fails if the size is set to 0.
- * 4) getcwd(2) fails if the size is set to 1.
- * 5) getcwd(2) fails if buf points to NULL and the size is set to 1.
- *
- * Expected Result:
- * 1) getcwd(2) should return NULL and set errno to EFAULT.
- * 2) getcwd(2) should return NULL and set errno to EFAULT.
- * 3) getcwd(2) should return NULL and set errno to ERANGE.
- * 4) getcwd(2) should return NULL and set errno to ERANGE.
- * 5) getcwd(2) should return NULL and set errno to ERANGE.
+ * Testcase to test that getcwd() sets errno correctly.
  */
 
 #include <errno.h>
@@ -27,28 +16,68 @@ 
 #include "lapi/syscalls.h"
 
 static char buffer[5];
-
-static struct t_case {
+struct getcwd_variants {
+	void (*getcwd)(char *buf, size_t size, int exp_err);
 	char *buf;
 	size_t size;
 	int exp_err;
-} tcases[] = {
-	{(void *)-1, PATH_MAX, EFAULT},
-	{NULL, (size_t)-1, EFAULT},
-	{buffer, 0, ERANGE},
-	{buffer, 1, ERANGE},
-	{NULL, 1, ERANGE}
 };
 
+static void verify_getcwd_raw_syscall(char *buf, size_t size, int exp_err);
+static void verify_getcwd(char *buf, size_t size, int exp_err);
+
+static struct getcwd_variants variants[] = {
+#ifdef __GLIBC__
+	{ .getcwd = verify_getcwd, .buf = NULL, .size = (size_t)-1, .exp_err = ENOMEM},
+	{ .getcwd = verify_getcwd, .buf = NULL, .size = 1, .exp_err = ERANGE},
+#endif
+	{ .getcwd = verify_getcwd, .buf = (void *)-1, .size = PATH_MAX, .exp_err = EFAULT},
+	{ .getcwd = verify_getcwd, .buf = buffer, .size = 0, .exp_err = EINVAL},
+	{ .getcwd = verify_getcwd, .buf = buffer, .size = 1, .exp_err = ERANGE},
+	{ .getcwd = verify_getcwd_raw_syscall, .buf = buffer, .size = 0, .exp_err = ERANGE},
+	{ .getcwd = verify_getcwd_raw_syscall, .buf = (void *)-1, .size = PATH_MAX, .exp_err = EFAULT},
+	{ .getcwd = verify_getcwd_raw_syscall, .buf = NULL, .size = (size_t)-1, .exp_err = EFAULT},
+	{ .getcwd = verify_getcwd_raw_syscall, .buf = buffer, .size = 0, .exp_err = ERANGE},
+	{ .getcwd = verify_getcwd_raw_syscall, .buf = buffer, .size = 1, .exp_err = ERANGE},
+	{ .getcwd = verify_getcwd_raw_syscall, .buf = NULL, .size = 1, .exp_err = ERANGE},
+};
+
+static void verify_getcwd(char *buf, size_t size, int exp_err)
+{
+	char *res;
+
+	errno = 0;
+	res = getcwd(buf, size);
+	TST_ERR = errno;
+	if (res) {
+		tst_res(TFAIL, "getcwd() succeeded unexpectedly");
+		return;
+	}
+
+	if (TST_ERR != exp_err) {
+		tst_res(TFAIL | TTERRNO, "getcwd() failed unexpectedly, expected %s",
+				tst_strerrno(exp_err));
+		return;
+	}
+
+	tst_res(TPASS | TTERRNO, "getcwd() failed as expected");
+
+}
+
+static void verify_getcwd_raw_syscall(char *buf, size_t size, int exp_err)
+{
+
+	TST_EXP_FAIL2(tst_syscall(__NR_getcwd, buf, size), exp_err);
+}
 
-static void verify_getcwd(unsigned int n)
+static void verify(void)
 {
-	struct t_case *tc = &tcases[n];
+	struct getcwd_variants *tv = &variants[tst_variant];
 
-	TST_EXP_FAIL2(tst_syscall(__NR_getcwd, tc->buf, tc->size), tc->exp_err);
+	tv->getcwd(tv->buf, tv->size, tv->exp_err);
 }
 
 static struct tst_test test = {
-	.tcnt = ARRAY_SIZE(tcases),
-	.test = verify_getcwd
+	.test_variants = ARRAY_SIZE(variants),
+	.test_all = verify,
 };