[LTP] [PATCH 1/1] test_macros: Add TST_EXP_PASS_OR_FAIL()

Cyril Hrubis chrubis@suse.cz
Fri Jul 10 11:30:21 CEST 2026


Hi!
>  include/tst_test_macros.h | 25 ++++++++++++++++++++++++-
>  1 file changed, 24 insertions(+), 1 deletion(-)
> 
> diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
> index f06c8aeb77..e980cc6022 100644
> --- a/include/tst_test_macros.h
> +++ b/include/tst_test_macros.h
> @@ -168,7 +168,7 @@ extern int TST_PASS;
>   * @ERRNO: Expected errno or 0.
>   * @...: A printf-like parameters.
>   *
> - * Expect a file descriptor if errno is 0 otherwise expect a failure with
> + * Expect a file descriptor if ERRNO is 0 otherwise expect a failure with
>   * expected errno.
>   *
>   * Internally it uses TST_EXP_FAIL() and TST_EXP_FD().
> @@ -354,6 +354,29 @@ extern int TST_PASS;
>  			TST_MSG_(TPASS, " passed", #SCALL, ##__VA_ARGS__);     \
>  	} while (0)
>  
> +/**
> + * TST_EXP_PASS_OR_FAIL() - Test syscall to and expect to pass or fail with
> + * expected errno.
> + *
> + * @SCALL: Tested syscall.
> + * @ERRNO: Expected errno or 0.
> + * @...: A printf-like parameters.
> + *
> + * Expect to pass if ERRNO is 0 otherwise expect a failure with
> + * expected errno.
> + *
> + * Internally it uses TST_EXP_FAIL() and TST_EXP_PASS().
> + */
> +#define TST_EXP_PASS_OR_FAIL(SCALL, ERRNO, ...)                                \
> +	({                                                                     \
> +		if (ERRNO)                                                     \
> +			TST_EXP_FAIL(SCALL, ERRNO, ##__VA_ARGS__);             \
> +		else                                                           \
> +			TST_EXP_PASS(SCALL, ##__VA_ARGS__);                      \
> +		                                                               \
> +		TST_RET;                                                       \
> +	})

I do not think that this is working as expected. If I remmeber correctly
we have to stringify the SCALL in the first macro is passed into
otherwise it may get expanded and produce unexpected results.

E.g. if we pass something with macro constants such as open() with
O_RDONLY the O_RDONLY will be replaced with 0. This is the reason why
the rest of the macros pass #SCALL to any macros it uses.

We need to change the TST_EXP_FAIL() and TST_EXP_PASS() so that they
have underscore variant first, e.g.:

diff --git a/include/tst_test_macros.h b/include/tst_test_macros.h
index f06c8aeb7..d391bdb9e 100644
--- a/include/tst_test_macros.h
+++ b/include/tst_test_macros.h
@@ -488,10 +488,13 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt);
  * printed by the pass or fail tst_res() calls. If omitted the first parameter
  * is converted to a string and used instead.
  */
-#define TST_EXP_FAIL(SCALL, EXP_ERR, ...)                                      \
+#define TST_EXP_FAIL(SCAL, EXP_ERR, ...) \
+       TST_EXP_FAIL_(SCAL, #SCAL, ##__VA_ARGS__)
+
+#define TST_EXP_FAIL_(SCALL, SSCAL, EXP_ERR, ...)                                      \
        do {                                                                   \
                int tst_exp_err__ = EXP_ERR;                                   \
-               TST_EXP_FAIL_ARR_(SCALL, #SCALL, &tst_exp_err__, 1,            \
+               TST_EXP_FAIL_ARR_(SCALL, SSCALL, &tst_exp_err__, 1,            \
                                   ##__VA_ARGS__);                              \
        } while (0)


Then we can build the _OR_FAIL() macros on the top of that. And the
TST_EXP_FD_OR_FAIL() should be fixed too.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list