diff mbox series

[v3,1/2] lib: TST_EXP_FAIL_PTR

Message ID 20240117125227.24700-2-wegao@suse.com
State Changes Requested
Headers show
Series lib: TST_EXP_FAIL_PTR | expand

Commit Message

Wei Gao Jan. 17, 2024, 12:52 p.m. UTC
Signed-off-by: Wei Gao <wegao@suse.com>
---
 include/tst_test_macros.h        | 41 +++++++++++++++++++++++++++++
 lib/newlib_tests/.gitignore      |  1 +
 lib/newlib_tests/test_macros07.c | 44 ++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+)
 create mode 100644 lib/newlib_tests/test_macros07.c

Comments

Petr Vorel Jan. 23, 2024, 8:45 a.m. UTC | #1
Hi Wei,

thank you for sending rebased version.

You could have added my RBT (the code you added didn't change).
Reviewed-by: Petr Vorel <pvorel@suse.cz>

nit (I have suggested in v2): subject should be: "lib: TST_EXP_{FAIL,PASS}_PTR"

Kind regards,
Petr
Cyril Hrubis Jan. 23, 2024, 10:41 a.m. UTC | #2
Hi!
> +#define TST_EXP_FAIL_SILENT_PTR_(SCALL, SSCALL, ERRNOS, ERRNOS_CNT, ...)       \
> +	do {                                                                   \
> +		TESTPTR(SCALL);                                                \
> +		                                                               \
> +		TST_PASS = 0;                                                  \
> +		                                                               \
> +		if (TST_RET_PTR) {                                             \

Unfortunatelly there are two types of calls, one returns NULL on a
failure and the second returns (void *)-1, from the top of my head these
are mmap(), shmat() and possibly other memory related syscalls.

So I suppose that this macro needs another parameter for the actual
value that is returned on a failure:

#define TST_EXP_FAIL_SILENT_PTR_(SCALL, SSCALL, FAIL_PTR_VAL, ERRNOS, ...)
	do {
		TESTPTR(SCALL);

		...

		if (TST_RET_PTR == FAILPTR_VAL) {
		...


The hard question is if we want to pass this parameter explicitly from the
macros used by the tests, i.e. if each test would need to specify a
FAIL_PTR_VAL or if we want to pass it in the macros, but that would mean
that the number of macros will explode again and we would have to figure
out a good names. So maybe it would be easier to pass them from each
test.
Petr Vorel Jan. 30, 2024, 12:20 p.m. UTC | #3
Hi Wei, Cyril,


> Hi!
> > +#define TST_EXP_FAIL_SILENT_PTR_(SCALL, SSCALL, ERRNOS, ERRNOS_CNT, ...)       \
> > +	do {                                                                   \
> > +		TESTPTR(SCALL);                                                \
> > +		                                                               \
> > +		TST_PASS = 0;                                                  \
> > +		                                                               \
> > +		if (TST_RET_PTR) {                                             \

> Unfortunatelly there are two types of calls, one returns NULL on a
> failure and the second returns (void *)-1, from the top of my head these
> are mmap(), shmat() and possibly other memory related syscalls.

> So I suppose that this macro needs another parameter for the actual
> value that is returned on a failure:

> #define TST_EXP_FAIL_SILENT_PTR_(SCALL, SSCALL, FAIL_PTR_VAL, ERRNOS, ...)
> 	do {
> 		TESTPTR(SCALL);

> 		...

> 		if (TST_RET_PTR == FAILPTR_VAL) {
> 		...

+1

> The hard question is if we want to pass this parameter explicitly from the
> macros used by the tests, i.e. if each test would need to specify a
> FAIL_PTR_VAL or if we want to pass it in the macros, but that would mean
> that the number of macros will explode again and we would have to figure
> out a good names. So maybe it would be easier to pass them from each
> test.

Names could be _NULL and _VOID, but I understand you want to limit number of
macros.

Wei, I guess testcases/kernel/syscalls/realpath/realpath01.c could use
TST_EXP_FAIL_PTR().

Kind regards,
Petr
diff mbox series

Patch

diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
index d2e50a219..5866d18b0 100644
--- a/include/tst_test_macros.h
+++ b/include/tst_test_macros.h
@@ -227,6 +227,30 @@  const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
 		}                                                              \
 	} while (0)
 
+#define TST_EXP_FAIL_SILENT_PTR_(SCALL, SSCALL, ERRNOS, ERRNOS_CNT, ...)       \
+	do {                                                                   \
+		TESTPTR(SCALL);                                                \
+		                                                               \
+		TST_PASS = 0;                                                  \
+		                                                               \
+		if (TST_RET_PTR) {                                             \
+			TST_MSG_(TFAIL, " succeeded", SSCALL, ##__VA_ARGS__);  \
+		        break;                                                 \
+		}                                                              \
+		                                                               \
+		if (!tst_errno_in_set(TST_ERR, ERRNOS, ERRNOS_CNT)) {          \
+			char tst_str_buf__[ERRNOS_CNT * 20];                   \
+			TST_MSGP_(TFAIL | TTERRNO, " expected %s",             \
+				  tst_errno_names(tst_str_buf__,               \
+						  ERRNOS, ERRNOS_CNT),         \
+				  SSCALL, ##__VA_ARGS__);                      \
+			break;                                                 \
+		}                                                              \
+                                                                               \
+		TST_PASS = 1;                                                  \
+                                                                               \
+	} while (0)
+
 #define TST_EXP_FAIL_ARR_(SCALL, SSCALL, EXP_ERRS, EXP_ERRS_CNT, ...)          \
 	do {                                                                   \
 		TST_EXP_FAIL_SILENT_(TST_RET == 0, SCALL, SSCALL,              \
@@ -258,6 +282,23 @@  const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
 		TST_EXP_FAIL2_ARR_(SCALL, #SCALL, EXP_ERRS,                    \
 		                  ARRAY_SIZE(EXP_ERRS), ##__VA_ARGS__);
 
+#define TST_EXP_FAIL_PTR(SCALL, EXP_ERR, ...)                                  \
+	do {                                                                   \
+		int tst_exp_err__ = EXP_ERR;                                   \
+		TST_EXP_FAIL_SILENT_PTR_(SCALL, #SCALL,                        \
+			&tst_exp_err__, 1, ##__VA_ARGS__);                     \
+		if (TST_PASS)                                                  \
+			TST_MSG_(TPASS | TTERRNO, " ", #SCALL, ##__VA_ARGS__); \
+	} while (0)
+
+#define TST_EXP_FAIL_PTR_ARR(SCALL, EXP_ERRS, ...)                             \
+	do {                                                                   \
+		TST_EXP_FAIL_SILENT_PTR_(SCALL, #SCALL,                        \
+			EXP_ERRS, ARRAY_SIZE(EXP_ERRS), ##__VA_ARGS__);        \
+		if (TST_PASS)                                                  \
+			TST_MSG_(TPASS | TTERRNO, " ", #SCALL, ##__VA_ARGS__); \
+	} while (0)
+
 #define TST_EXP_FAIL2(SCALL, EXP_ERR, ...)                                     \
 	do {                                                                   \
 		int tst_exp_err__ = EXP_ERR;                                   \
diff --git a/lib/newlib_tests/.gitignore b/lib/newlib_tests/.gitignore
index a69b29e24..4f43899e5 100644
--- a/lib/newlib_tests/.gitignore
+++ b/lib/newlib_tests/.gitignore
@@ -40,6 +40,7 @@  test_macros03
 test_macros04
 test_macros05
 test_macros06
+test_macros07
 tst_fuzzy_sync01
 tst_fuzzy_sync02
 tst_fuzzy_sync03
diff --git a/lib/newlib_tests/test_macros07.c b/lib/newlib_tests/test_macros07.c
new file mode 100644
index 000000000..45bba8409
--- /dev/null
+++ b/lib/newlib_tests/test_macros07.c
@@ -0,0 +1,44 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 Wei Gao <wegao@suse.com>
+ */
+
+/*
+ * Test TST_EXP_FAIL_PTR and TST_EXP_FAIL_PTR_ARR macro.
+ */
+
+#include "tst_test.h"
+
+static char *fail_fn(void)
+{
+	errno = EINVAL;
+	return NULL;
+}
+
+static char *pass_fn(void)
+{
+	return "pass";
+}
+
+static void do_test(void)
+{
+	const int exp_errs_pass[] = {ENOTTY, EINVAL};
+	const int exp_errs_fail[] = {ENOTTY, EISDIR};
+
+	tst_res(TINFO, "Testing TST_EXP_FAIL_PTR macro");
+	TST_EXP_FAIL_PTR(fail_fn(), EINVAL, "fail_fn()");
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TST_EXP_FAIL_PTR(fail_fn(), ENOTTY, "fail_fn()");
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TST_EXP_FAIL_PTR(pass_fn(), ENOTTY, "pass_fn()");
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TST_EXP_FAIL_PTR_ARR(fail_fn(), exp_errs_pass, "fail_fn()");
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+	TST_EXP_FAIL_PTR_ARR(fail_fn(), exp_errs_fail, "fail_fn()");
+	tst_res(TINFO, "TST_PASS = %i", TST_PASS);
+
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+};