[LTP] [PATCH v2] getcwd01: Implement .test_variants

Wei Gao wegao@suse.com
Fri Dec 22 11:06:11 CET 2023


Signed-off-by: Wei Gao <wegao@suse.com>
---
 testcases/kernel/syscalls/getcwd/getcwd.h   | 80 +++++++++++++++++++++
 testcases/kernel/syscalls/getcwd/getcwd01.c | 35 ++++++---
 2 files changed, 105 insertions(+), 10 deletions(-)
 create mode 100644 testcases/kernel/syscalls/getcwd/getcwd.h

diff --git a/testcases/kernel/syscalls/getcwd/getcwd.h b/testcases/kernel/syscalls/getcwd/getcwd.h
new file mode 100644
index 000000000..91f229904
--- /dev/null
+++ b/testcases/kernel/syscalls/getcwd/getcwd.h
@@ -0,0 +1,80 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (c) International Business Machines  Corp., 2001
+ * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
+ *
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program;  if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef GETCWD_H
+#define GETCWD_H
+
+#include <stdint.h>
+#include "config.h"
+#include "lapi/syscalls.h"
+
+static inline void
+check_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 inline void
+tst_getcwd(char *buf, size_t size, int exp_err, int exp_err2)
+{
+	switch (tst_variant) {
+	case 0:
+		TST_EXP_FAIL2(tst_syscall(__NR_getcwd, buf, size), exp_err);
+		break;
+	case 1:
+#ifdef __GLIBC__
+		check_getcwd(buf, size, exp_err2);
+#endif
+		break;
+	}
+}
+
+static inline void
+getcwd_info(void)
+{
+	switch (tst_variant) {
+	case 0:
+		tst_res(TINFO, "Testing getcwd with raw syscall");
+		break;
+	case 1:
+		tst_res(TINFO, "Testing getcwd with wrap syscall");
+		break;
+	}
+}
+
+#define TEST_VARIANTS 2
+
+#endif /* GETCWD_H */
diff --git a/testcases/kernel/syscalls/getcwd/getcwd01.c b/testcases/kernel/syscalls/getcwd/getcwd01.c
index 218bf4ef2..6decb961f 100644
--- a/testcases/kernel/syscalls/getcwd/getcwd01.c
+++ b/testcases/kernel/syscalls/getcwd/getcwd01.c
@@ -13,18 +13,26 @@
  * 5) getcwd(2) fails if buf points to NULL and the size is set to 1.
  *
  * Expected Result:
+ * linux syscall
  * 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.
+ *
+ * glibc
+ * 1) getcwd(2) should return NULL and set errno to EFAULT.
+ * 2) getcwd(2) should return NULL and set errno to ENOMEM.
+ * 3) getcwd(2) should return NULL and set errno to EINVAL.
+ * 4) getcwd(2) should return NULL and set errno to ERANGE.
+ * 5) getcwd(2) should return NULL and set errno to ERANGE.
  */
 
 #include <errno.h>
 #include <unistd.h>
 #include <limits.h>
 #include "tst_test.h"
-#include "lapi/syscalls.h"
+#include "getcwd.h"
 
 static char buffer[5];
 
@@ -32,23 +40,30 @@ static struct t_case {
 	char *buf;
 	size_t size;
 	int exp_err;
+	int exp_err2;
 } tcases[] = {
-	{(void *)-1, PATH_MAX, EFAULT},
-	{NULL, (size_t)-1, EFAULT},
-	{buffer, 0, ERANGE},
-	{buffer, 1, ERANGE},
-	{NULL, 1, ERANGE}
+	{(void *)-1, PATH_MAX, EFAULT, EFAULT},
+	{NULL, (size_t)-1, EFAULT, ENOMEM},
+	{buffer, 0, ERANGE, EINVAL},
+	{buffer, 1, ERANGE, ERANGE},
+	{NULL, 1, ERANGE, ERANGE},
 };
 
-
-static void verify_getcwd(unsigned int n)
+static void run(unsigned int n)
 {
 	struct t_case *tc = &tcases[n];
 
-	TST_EXP_FAIL2(tst_syscall(__NR_getcwd, tc->buf, tc->size), tc->exp_err);
+	tst_getcwd(tc->buf, tc->size, tc->exp_err, tc->exp_err2);
+}
+
+static void setup(void)
+{
+	getcwd_info();
 }
 
 static struct tst_test test = {
+	.setup = setup,
 	.tcnt = ARRAY_SIZE(tcases),
-	.test = verify_getcwd
+	.test = run,
+	.test_variants = TEST_VARIANTS,
 };
-- 
2.35.3



More information about the ltp mailing list