Introduce TST_EXP_MACROS
v2 cover Introduce TST_EXP_MACROS
This patchset adds TST_EXP_MACROS() implementation.
These macros are intended to simplify and unify syscall unit tests.
This patchset converts a few testcases to use TST_EXP_MACROS() it's intenteded to serve as an example since there is a lot of testcases that could use them as well.
Also the documentation for these macros is ommited at this point, I will add it in a case that we agree to include them in the test library.
Changes in v2:
- renamed the macros so that it's more clear that they do
- added documentation to test-writing-guidelines
- minor fixed in the converted testcases
Cyril Hrubis (10):
lib: Introduce TST_EXP_* macros
syscalls/uname: Make use of TST_EXP_MACROS
syscalls/accept: Make use of TST_EXP_MACROS
syscalls/access: Make use of TST_EXP_MACROS
syscalls/bind: Make use of TST_EXP_MACROS
syscalls/brk01: Make use of TST_EXP_MACROS
syscalls/cacheflush: Make use of TST_EXP_MACROS
syscalls/capget: Make use of TEST_MACROS
syscalls/capset: Make use of TST_EXP_MACROS
syscalls/open: Make use of TST_EXP_MACROS
doc/test-writing-guidelines.txt | 54 ++++++++ include/tst_test.h | 28 +--- include/tst_test_macros.h | 130 ++++++++++++++++++ lib/newlib_tests/.gitignore | 3 + lib/newlib_tests/test_macros01.c | 40 ++++++ lib/newlib_tests/test_macros02.c | 42 ++++++ lib/newlib_tests/test_macros03.c | 40 ++++++ lib/tst_test.c | 1 + testcases/kernel/syscalls/accept/accept01.c | 17 +-- testcases/kernel/syscalls/access/access01.c | 45 +----- testcases/kernel/syscalls/access/access02.c | 8 +- testcases/kernel/syscalls/access/access03.c | 28 +--- testcases/kernel/syscalls/access/access04.c | 17 +-- testcases/kernel/syscalls/bind/bind01.c | 12 +- testcases/kernel/syscalls/bind/bind02.c | 11 +- testcases/kernel/syscalls/bind/bind03.c | 26 +--- testcases/kernel/syscalls/bind/bind04.c | 5 +- testcases/kernel/syscalls/bind/bind05.c | 5 +- testcases/kernel/syscalls/brk/brk01.c | 9 +- .../kernel/syscalls/cacheflush/cacheflush01.c | 8 +- testcases/kernel/syscalls/capget/capget01.c | 17 +-- testcases/kernel/syscalls/capget/capget02.c | 26 ++-- testcases/kernel/syscalls/capset/capset01.c | 14 +- testcases/kernel/syscalls/capset/capset02.c | 28 ++-- testcases/kernel/syscalls/capset/capset03.c | 10 +- testcases/kernel/syscalls/capset/capset04.c | 13 +- testcases/kernel/syscalls/open/open01.c | 11 +- testcases/kernel/syscalls/open/open02.c | 25 +--- testcases/kernel/syscalls/open/open11.c | 22 ++- testcases/kernel/syscalls/uname/uname01.c | 13 +- testcases/kernel/syscalls/uname/uname02.c | 17 +-- 31 files changed, 403 insertions(+), 322 deletions(-) create mode 100644 include/tst_test_macros.h create mode 100644 lib/newlib_tests/test_macros01.c create mode 100644 lib/newlib_tests/test_macros02.c create mode 100644 lib/newlib_tests/test_macros03.c
Signature · 1 line
2.26.2
PATCH v2 1/10 lib: Introduce TST_EXP_* macros
In order to simplify common return value checks.
--- doc/test-writing-guidelines.txt | 54 +++++++++++++ include/tst_test.h | 28 +------ include/tst_test_macros.h | 130 +++++++++++++++++++++++++++++++ lib/newlib_tests/.gitignore | 3 + lib/newlib_tests/test_macros01.c | 40 ++++++++++ lib/newlib_tests/test_macros02.c | 42 ++++++++++ lib/newlib_tests/test_macros03.c | 40 ++++++++++ lib/tst_test.c | 1 + 8 files changed, 311 insertions(+), 27 deletions(-) create mode 100644 include/tst_test_macros.h create mode 100644 lib/newlib_tests/test_macros01.c create mode 100644 lib/newlib_tests/test_macros02.c create mode 100644 lib/newlib_tests/test_macros03.c
doc/test-writing-guidelines.txt +54 -0
@@ -451,6 +451,60 @@ Printf-like function to report error and exit the test, it can be used with ttyp The 'ttype' can be combined bitwise with 'TERRNO' or 'TTERRNO' to print 'errno', 'TST_ERR' respectively. +There are also 'TST_EXP_*()' macros that can simplify syscall unit tests to a +single line, use them whenever possible. These macros take a function call as +the first parameter and a printf-like format string and parameters as well. +These test macros then expand to a code that runs the call, checks the return +value and errno and reports the test result. + +[source,c] +------------------------------------------------------------------------------- +static void test(void) +{ + ... + TST_EXP_PASS(stat(fname, &statbuf), "stat(%s, ...)", fname); + + if (!TST_PASS) + return; + ... +} +------------------------------------------------------------------------------- + +The 'TST_EXP_PASS()' can be used for calls that return -1 on failure and 0 on +success. It will check for the return value and reports failure if the return +value is not equal to 0. The call also sets the 'TST_PASS' variable to 1 if +the call succeeeded. + +[source,c] +------------------------------------------------------------------------------- +static void test(void) +{ + ... + TST_EXP_FD(open(fname, O_RDONLY), "open(%s, O_RDONLY)", fname); + + SAFE_CLOSE(TST_RET); + ... +} +------------------------------------------------------------------------------- + +The 'TST_EXP_FD()' is the same as 'TST_EXP_PASS()' the only difference is that +the return value is expected to be a file descriptor so the call passes if +positive integer is returned. + +[source,c] +------------------------------------------------------------------------------- +static void test(void) +{ + ... + TST_EXP_FAIL(stat(fname, &statbuf), ENOENT, "stat(%s, ...)", fname); + ... +} +------------------------------------------------------------------------------- + +The 'TST_EXP_FAIL()' is similar to 'TST_EXP_PASS()' but it fails the test if +the call haven't failed with -1 and 'errno' wasn't set to the expected one +passed as the second argument. + [source,c] ------------------------------------------------------------------------------- const char *tst_strsig(int sig);
include/tst_test.h +1 -27
@@ -18,6 +18,7 @@#include "tst_common.h" #include "tst_res_flags.h" +#include "tst_test_macros.h" #include "tst_checkpoint.h" #include "tst_device.h" #include "tst_mkfs.h"
@@ -272,33 +273,6 @@ void tst_run_tcases(int argc, char *argv[], struct tst_test *self)*/ void tst_reinit (void ); -//TODO Clean? -#define TEST (SCALL ) \ - do { \ - errno = 0; \ - TST_RET = SCALL; \ - TST_ERR = errno; \ - } while (0) - -#define TEST_VOID (SCALL ) \ - do { \ - errno = 0; \ - SCALL; \ - TST_ERR = errno; \ - } while (0) - -extern long TST_RET ; -extern int TST_ERR ; - -extern void *TST_RET_PTR ; - -#define TESTPTR (SCALL ) \ - do { \ - errno = 0; \ - TST_RET_PTR = (void*)SCALL; \ - TST_ERR = errno; \ - } while (0) -/* * Functions to convert ERRNO to its name and SIGNAL to its name. */
include/tst_test_macros.h new file +130 -0
130 lines not on this page · raw message
lib/newlib_tests/.gitignore +3 -0
@@ -37,3 +37,6 @@ test_kconfig02 variant test_guarded_buf tst_bool_expr +test_macros01 +test_macros02 +test_macros03
lib/newlib_tests/test_macros01.c new file +40 -0
@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 Cyril Hrubis <[email protected]> +*/ + +/* +* Test macros. +*/ + +#include "tst_test.h" + +static int fail_fd (void ) +{ + errno =EINVAL ; +return -1 ; +} + +static int pass_fd (void ) +{ +return 42 ; +} + +static int inval_val (void ) +{ +return -42 ; +} + +static void do_test (void ) +{ +TST_EXP_FD (fail_fd (),"TEST DESCRIPTION" ); +tst_res (TINFO ,"TST_PASS = %i" ,TST_PASS ); +TST_EXP_FD (pass_fd (),"%s" ,"TEST DESCRIPTION PARAM" ); +tst_res (TINFO ,"TST_PASS = %i" ,TST_PASS ); +TST_EXP_FD (inval_val ()); +tst_res (TINFO ,"TST_PASS = %i" ,TST_PASS ); +} + +static struct tst_test test = { + .test_all = do_test, +};
lib/newlib_tests/test_macros02.c new file +42 -0
42 lines not on this page · raw message
lib/newlib_tests/test_macros03.c new file +40 -0
@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2020 Cyril Hrubis <[email protected]> +*/ + +/* +* Test macros. +*/ + +#include "tst_test.h" + +static int fail_fn (void ) +{ + errno =EINVAL ; +return -1 ; +} + +static int pass_fn (void ) +{ +return 0 ; +} + +static int inval_ret_fn (void ) +{ +return 42 ; +} + +static void do_test (void ) +{ +TST_EXP_PASS (fail_fn ()); +tst_res (TINFO ,"TST_PASS = %i" ,TST_PASS ); +TST_EXP_PASS (pass_fn (),"TEST DESCRIPTION" ); +tst_res (TINFO ,"TST_PASS = %i" ,TST_PASS ); +TST_EXP_PASS (inval_ret_fn ()); +tst_res (TINFO ,"TST_PASS = %i" ,TST_PASS ); +} + +static struct tst_test test = { + .test_all = do_test, +};
lib/tst_test.c +1 -0
@@ -75,6 +75,7 @@ const char *tst_ipc_path = ipc_path;static char shm_path[1024 ];int TST_ERR ; +int TST_PASS ;long TST_RET ;static void do_cleanup (void );
Signature · 1 line
2.26.2
PATCH v2 2/10 syscalls/uname: Make use of TST_EXP_MACROS
In reply to Cyril Hrubis--- testcases/kernel/syscalls/uname/uname01.c | 13 +++---------- testcases/kernel/syscalls/uname/uname02.c | 17 +---------------- 2 files changed, 4 insertions(+), 26 deletions(-)
testcases/kernel/syscalls/uname/uname01.c +3 -10
@@ -18,24 +18,17 @@ static void verify_uname(void)memset (&un,0 ,sizeof (un)); -TEST (uname (&un)); -if (TST_RET ==-1 ) { -tst_res (TFAIL |TTERRNO ,"uname() failed" ); -return ; - } +TST_EXP_PASS (uname (&un)); -if (TST_RET !=0 ) { -tst_res (TFAIL |TTERRNO , -"uname() returned invalid value %ld" ,TST_RET ); +if (!TST_PASS )return ; - }if (strcmp (un.sysname,"Linux" )) {tst_res (TFAIL ,"sysname is not Linux" );return ; } -tst_res (TPASS ,"uname() succeeded" ); +tst_res (TPASS ,"sysname set to Linux" ); }static struct tst_test test = {
testcases/kernel/syscalls/uname/uname02.c +1 -16
@@ -16,22 +16,7 @@ static void *bad_addr;static void verify_uname (void ) { -TEST (uname (bad_addr)); -if (TST_RET ==0 ) { -tst_res (TFAIL ,"uname() succeed when failure expected" ); -return ; - } - -if (TST_RET !=-1 ) { -tst_res (TFAIL ,"Invalid uname() return value %ld" ,TST_RET ); -return ; - } - -if (TST_ERR ==EFAULT ) -tst_res (TPASS ,"uname() got EFAULT as expected" ); -else -tst_res (TFAIL |TTERRNO ,"uname() failed unexpectedly" ); - +TST_EXP_FAIL (uname (bad_addr),EFAULT ); }static void setup (void )
Signature · 1 line
2.26.2
PATCH v2 3/10 syscalls/accept: Make use of TST_EXP_MACROS
In reply to Cyril Hrubis--- testcases/kernel/syscalls/accept/accept01.c | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-)
testcases/kernel/syscalls/accept/accept01.c +2 -15
@@ -97,21 +97,8 @@ void verify_accept(unsigned int nr) {struct test_case *tcase = &tcases[nr]; -TEST (accept (*tcase->fd, tcase->sockaddr, &tcase->salen)); - -if (TST_RET !=-1 ) { -tst_res (TFAIL ,"%s: returned %li, expected -1" , - tcase->desc,TST_RET ); -return ; - } - -if (TST_ERR != tcase->experrno) { -tst_res (TFAIL |TTERRNO ,"%s: expected errno %s, got " , - tcase->desc,tst_strerrno (tcase->experrno)); -return ; - } - -tst_res (TPASS |TTERRNO ,"%s successful" , tcase->desc); +TST_EXP_FAIL (accept (*tcase->fd, tcase->sockaddr, &tcase->salen), + tcase->experrno,"%s" , tcase->desc); }static struct tst_test test = {
Signature · 1 line
2.26.2
PATCH v2 4/10 syscalls/access: Make use of TST_EXP_MACROS
In reply to Cyril Hrubis--- testcases/kernel/syscalls/access/access01.c | 45 ++++----------------- testcases/kernel/syscalls/access/access02.c | 8 ++-- testcases/kernel/syscalls/access/access03.c | 28 ++----------- testcases/kernel/syscalls/access/access04.c | 17 +------- 4 files changed, 16 insertions(+), 82 deletions(-)
testcases/kernel/syscalls/access/access01.c +7 -38
@@ -231,46 +231,15 @@ static struct tcase { {DNAME_WX"/"FNAME_X, W_OK, "W_OK", EACCES, 1} }; -static void verify_success (struct tcase *tc,const char *user) -{ -if (TST_RET ==-1 ) { -tst_res (TFAIL |TTERRNO , -"access(%s, %s) as %s failed unexpectedly" , - tc->fname, tc->name, user); -return ; - } - -tst_res (TPASS ,"access(%s, %s) as %s" , tc->fname, tc->name, user); -} - -static void verify_failure (struct tcase *tc,const char *user) -{ -if (TST_RET !=-1 ) { -tst_res (TFAIL ,"access(%s, %s) as %s succeded unexpectedly" , - tc->fname, tc->name, user); -return ; - } - -if (TST_ERR != tc->exp_errno) { -tst_res (TFAIL |TTERRNO , -"access(%s, %s) as %s should fail with %s" , - tc->fname, tc->name, user, -tst_strerrno (tc->exp_errno)); -return ; - } - -tst_res (TPASS |TTERRNO ,"access(%s, %s) as %s" , - tc->fname, tc->name, user); -} -static void access_test (struct tcase *tc,const char *user) { -TEST (access (tc->fname, tc->mode)); - -if (tc->exp_errno) -verify_failure (tc, user); -else -verify_success (tc, user); +if (tc->exp_errno) { +TST_EXP_FAIL (access (tc->fname, tc->mode), tc->exp_errno, +"access(%s, %s) as %s" , tc->fname, tc->name, user); + }else { +TST_EXP_PASS (access (tc->fname, tc->mode), +"access(%s, %s) as %s" , tc->fname, tc->name, user); + } }static void verify_access (unsigned int n)
testcases/kernel/syscalls/access/access02.c +3 -5
@@ -59,13 +59,11 @@ static void access_test(struct tcase *tc, const char *user)struct stat stat_buf;char command[64 ]; -TEST (access (tc->pathname, tc->mode)); +TST_EXP_PASS (access (tc->pathname, tc->mode), +"access(%s, %s) as %s" , tc->pathname, tc->name, user); -if (TST_RET ==-1 ) { -tst_res (TFAIL |TTERRNO ,"access(%s, %s) as %s failed" , - tc->pathname, tc->name, user); +if (!TST_PASS )return ; - }switch (tc->mode) {case F_OK :
testcases/kernel/syscalls/access/access03.c +4 -24
@@ -26,34 +26,13 @@ static struct tcase { {(void *)-1, X_OK, "X_OK"}, }; -static void access_test (struct tcase *tc,const char *user) -{ -TEST (access (tc->addr, tc->mode)); - -if (TST_RET !=-1 ) { -tst_res (TFAIL ,"access(%p, %s) as %s succeeded unexpectedly" , - tc->addr, tc->name, user); -return ; - } - -if (TST_ERR !=EFAULT ) { -tst_res (TFAIL |TTERRNO , -"access(%p, %s) as %s should fail with EFAULT" , - tc->addr, tc->name, user); -return ; - } - -tst_res (TPASS |TTERRNO ,"access(%p, %s) as %s" , - tc->addr, tc->name, user); -} -static void verify_access (unsigned int n) {struct tcase *tc = &tcases[n];pid_t pid; -/* test as root */ -access_test (tc,"root" ); +TST_EXP_FAIL (access (tc->addr, tc->mode),EFAULT , +"invalid address as root" );/* test as nobody */ pid =SAFE_FORK ();
@@ -61,7 +40,8 @@ static void verify_access(unsigned int n)SAFE_WAITPID (pid,NULL ,0 ); }else {SAFE_SETUID (uid); -access_test (tc,"nobody" ); +TST_EXP_FAIL (access (tc->addr, tc->mode),EFAULT , +"invalid address as nobody" ); } }
testcases/kernel/syscalls/access/access04.c +2 -15
@@ -58,21 +58,8 @@ static struct tcase {static void access_test (struct tcase *tc,const char *user) { -TEST (access (tc->pathname, tc->mode)); - -if (TST_RET !=-1 ) { -tst_res (TFAIL ,"access as %s succeeded unexpectedly" , user); -return ; - } - -if (tc->exp_errno !=TST_ERR ) { -tst_res (TFAIL |TTERRNO , -"access as %s should fail with %s" , - user,tst_strerrno (tc->exp_errno)); -return ; - } - -tst_res (TPASS |TTERRNO ,"access as %s failed expectedly" , user); +TST_EXP_FAIL (access (tc->pathname, tc->mode), tc->exp_errno, +"access as %s" , user); }static void verify_access (unsigned int n)
Signature · 1 line
2.26.2
PATCH v2 5/10 syscalls/bind: Make use of TST_EXP_MACROS
In reply to Cyril Hrubis--- testcases/kernel/syscalls/bind/bind01.c | 12 +++++------- testcases/kernel/syscalls/bind/bind02.c | 11 ++--------- testcases/kernel/syscalls/bind/bind03.c | 26 ++++--------------------- testcases/kernel/syscalls/bind/bind04.c | 5 ++--- testcases/kernel/syscalls/bind/bind05.c | 5 ++--- 5 files changed, 15 insertions(+), 44 deletions(-)
testcases/kernel/syscalls/bind/bind01.c +5 -7
@@ -47,14 +47,12 @@ void verify_bind(unsigned int nr) {struct test_case *tcase = &tcases[nr]; -TEST (bind (*tcase->socket_fd, tcase->sockaddr, tcase->salen)); -if (TST_RET != tcase->retval &&TST_ERR != tcase->experrno) { -tst_res (TFAIL ,"%s ; returned" -" %ld (expected %d), errno %d (expected" -" %d)" , tcase->desc,TST_RET , tcase->retval, -TST_ERR , tcase->experrno); +if (tcase->experrno) { +TST_EXP_FAIL (bind (*tcase->socket_fd, tcase->sockaddr, tcase->salen), + tcase->experrno,"%s" , tcase->desc); }else { -tst_res (TPASS ,"%s successful" , tcase->desc); +TST_EXP_PASS (bind (*tcase->socket_fd, tcase->sockaddr, tcase->salen), +"%s" , tcase->desc); } }
testcases/kernel/syscalls/bind/bind02.c +2 -9
@@ -36,16 +36,9 @@ static void run(void) servaddr.sin_family =AF_INET ; servaddr.sin_port =htons (TCP_PRIVILEGED_PORT ); servaddr.sin_addr.s_addr =htonl (INADDR_ANY ); -TEST (bind (sockfd, (struct sockaddr *)&servaddr,sizeof (servaddr))); +TST_EXP_FAIL (bind (sockfd, (struct sockaddr *)&servaddr,sizeof (servaddr)), +EACCES ,"bind()" );SAFE_CLOSE (sockfd); - -if (TST_RET !=-1 ) { -tst_res (TFAIL ,"bind() returned %li, expected -1" ,TST_RET ); - }else if (TST_ERR ==EACCES ) { -tst_res (TPASS |TTERRNO ,"bind() failed as expected" ); - }else { -tst_res (TFAIL |TTERRNO ,"Unexpected error" ); - } }static void setup (void )
testcases/kernel/syscalls/bind/bind03.c +4 -22
@@ -51,17 +51,8 @@ void run(void)* Once a STREAM UNIX domain socket has been bound, it can't be * rebound. */ -if (bind (sock1, (struct sockaddr *)&sun2,sizeof (sun2)) ==0 ) { -tst_res (TFAIL ,"re-binding of socket succeeded" ); -return ; - } - -if (errno !=EINVAL ) { -tst_res (TFAIL |TERRNO ,"expected EINVAL" ); -return ; - } - -tst_res (TPASS ,"bind() failed with EINVAL as expected" ); +TST_EXP_FAIL (bind (sock1, (struct sockaddr *)&sun2,sizeof (sun2)), +EINVAL ,"re-bind() socket" ); sock2 =SAFE_SOCKET (PF_UNIX ,SOCK_STREAM ,0 );
@@ -69,17 +60,8 @@ void run(void)* Since a socket is already bound to the pathname, it can't be bound * to a second socket. Expected error is EADDRINUSE. */ -if (bind (sock2, (struct sockaddr *)&sun1,sizeof (sun1)) ==0 ) { -tst_res (TFAIL ,"bind() succeeded with already bound pathname!" ); -return ; - } - -if (errno !=EADDRINUSE ) { -tst_res (TFAIL |TERRNO ,"expected to fail with EADDRINUSE" ); -return ; - } - -tst_res (TPASS ,"bind() failed with EADDRINUSE as expected" ); +TST_EXP_FAIL (bind (sock2, (struct sockaddr *)&sun1,sizeof (sun1)), +EADDRINUSE ,"bind() with bound pathname" ); }static void cleanup (void )
testcases/kernel/syscalls/bind/bind04.c +2 -3
@@ -118,10 +118,9 @@ static void test_bind(unsigned int n) listen_sock =SAFE_SOCKET (tc->address->sa_family, tc->type, tc->protocol); -TEST (bind (listen_sock, tc->address, tc->addrlen)); +TST_EXP_PASS (bind (listen_sock, tc->address, tc->addrlen),"bind()" ); -if (TST_RET ) { -tst_res (TFAIL |TERRNO ,"bind() failed" ); +if (!TST_PASS ) {SAFE_CLOSE (listen_sock);return ; }
testcases/kernel/syscalls/bind/bind05.c +2 -3
@@ -131,10 +131,9 @@ static void test_bind(unsigned int n)tst_res (TINFO ,"Testing %s" , tc->description); sock =SAFE_SOCKET (tc->address->sa_family, tc->type, tc->protocol); -TEST (bind (sock, tc->address, tc->addrlen)); +TST_EXP_PASS (bind (sock, tc->address, tc->addrlen),"bind()" ); -if (TST_RET ) { -tst_res (TFAIL |TERRNO ,"bind() failed" ); +if (!TST_PASS ) {SAFE_CLOSE (sock);return ; }
Signature · 1 line
2.26.2
PATCH v2 6/10 syscalls/brk01: Make use of TST_EXP_MACROS
In reply to Cyril Hrubis--- testcases/kernel/syscalls/brk/brk01.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
testcases/kernel/syscalls/brk/brk01.c +1 -8
@@ -31,12 +31,7 @@ void verify_brk(void)break ; } -TEST (brk ((void *)new_brk)); - -if (TST_RET ==-1 ) { -tst_res (TFAIL |TERRNO ,"brk() failed" ); -return ; - } +TST_EXP_PASS (brk ((void *)new_brk),"brk()" ); cur_brk = (uintptr_t )sbrk (0 );
@@ -51,8 +46,6 @@ void verify_brk(void)if (i %3 ==0 ) *((char *)cur_brk) =0 ; } - -tst_res (TPASS ,"brk() works fine" ); }static struct tst_test test = {
Signature · 1 line
2.26.2
PATCH v2 7/10 syscalls/cacheflush: Make use of TST_EXP_MACROS
In reply to Cyril Hrubis--- testcases/kernel/syscalls/cacheflush/cacheflush01.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/cacheflush/cacheflush01.c +2 -6
@@ -45,12 +45,8 @@ static void test_cacheflush(unsigned int i) {struct test_case_t *tc = &test_cases[i]; -TEST (tst_syscall (__NR_cacheflush, addr,getpagesize (), tc->cache)); -if (TST_RET ==0 ) { -tst_res (TPASS ,"%s passed" , tc->desc); - }else { -tst_res (TFAIL |TTERRNO ,"%s failed" , tc->desc); - } +TST_EXP_PASS (tst_syscall (__NR_cacheflush, addr,getpagesize (), tc->cache), +"%s" , tc->desc); }static struct tst_test test = {
Signature · 1 line
2.26.2
PATCH v2 8/10 syscalls/capget: Make use of TEST_MACROS
In reply to Cyril Hrubis--- testcases/kernel/syscalls/capget/capget01.c | 17 +++++--------- testcases/kernel/syscalls/capget/capget02.c | 26 +++++++-------------- 2 files changed, 14 insertions(+), 29 deletions(-)
testcases/kernel/syscalls/capget/capget01.c +6 -11
@@ -20,25 +20,20 @@ static struct tcase {int version;char *message; } tcases[] = { - {0x19980330, "Test on LINUX_CAPABILITY_VERSION_1"}, - {0x20071026, "Test on LINUX_CAPABILITY_VERSION_2"}, - {0x20080522, "Test on LINUX_CAPABILITY_VERSION_3"}, + {0x19980330, "LINUX_CAPABILITY_VERSION_1"}, + {0x20071026, "LINUX_CAPABILITY_VERSION_2"}, + {0x20080522, "LINUX_CAPABILITY_VERSION_3"}, }; static void verify_capget(unsigned int n) { struct tcase *tc = &tcases[n]; - tst_res(TINFO, "%s", tc->message); - hdr->version = tc->version; hdr->pid = pid; - TEST(tst_syscall(__NR_capget, hdr, data)); - if (TST_RET == 0) - tst_res(TPASS, "capget() returned %ld", TST_RET); - else - tst_res(TFAIL | TTERRNO, "Test Failed, capget() returned %ld", - TST_RET); + + TST_EXP_PASS(tst_syscall(__NR_capget, hdr, data), + "capget() with %s", tc->message); if (data[0].effective & 1 << CAP_NET_RAW) tst_res(TFAIL, "capget() gets CAP_NET_RAW unexpectedly in pE");
testcases/kernel/syscalls/capget/capget02.c +8 -18
@@ -31,11 +31,11 @@ static struct tcase {int flag;char *message; } tcases[] = { - {0x20080522, 0, EFAULT, 1, "Test bad address header"}, - {0x20080522, 0, EFAULT, 2, "Test bad address data"}, - {0, 0, EINVAL, 0, "Test bad version"}, - {0x20080522, -1, EINVAL, 0, "Test bad pid"}, - {0x20080522, 1, ESRCH, 0, "Test unused pid"}, + {0x20080522, 0, EFAULT, 1, "bad address header"}, + {0x20080522, 0, EFAULT, 2, "bad address data"}, + {0, 0, EINVAL, 0, "bad version"}, + {0x20080522, -1, EINVAL, 0, "bad pid"}, + {0x20080522, 1, ESRCH, 0, "unused pid"}, }; static void verify_capget(unsigned int n)
@@ -48,25 +48,15 @@ static void verify_capget(unsigned int n)else header->pid = tc->pid; -tst_res (TINFO ,"%s" , tc->message); -/* * header must not be NULL. data may be NULL only when the user is * trying to determine the preferred capability version format * supported by the kernel. So use tst_get_bad_addr() to get * this error. */ -TEST (tst_syscall (__NR_capget, tc->flag -1 ? header :NULL , - tc->flag -2 ? data : bad_data)); -if (TST_RET ==0 ) { -tst_res (TFAIL ,"capget() succeed unexpectedly" ); -return ; - } -if (TST_ERR == tc->exp_err) -tst_res (TPASS |TTERRNO ,"capget() failed as expected" ); -else -tst_res (TFAIL |TTERRNO ,"capget() expected %s got " , -tst_strerrno (tc->exp_err)); +TST_EXP_FAIL (tst_syscall (__NR_capget, tc->flag -1 ? header :NULL , + tc->flag -2 ? data : bad_data), + tc->exp_err,"capget() with %s" , tc->message);/* * When an unsupported version value is specified, it will
Signature · 1 line
2.26.2
PATCH v2 9/10 syscalls/capset: Make use of TST_EXP_MACROS
In reply to Cyril Hrubis--- testcases/kernel/syscalls/capset/capset01.c | 14 ++++------- testcases/kernel/syscalls/capset/capset02.c | 28 +++++++-------------- testcases/kernel/syscalls/capset/capset03.c | 10 +------- testcases/kernel/syscalls/capset/capset04.c | 13 +++------- 4 files changed, 18 insertions(+), 47 deletions(-)
testcases/kernel/syscalls/capset/capset01.c +5 -9
@@ -20,16 +20,15 @@ static struct tcase {int version;char *message; } tcases[] = { - {0x19980330, "Test on LINUX_CAPABILITY_VERSION_1"}, - {0x20071026, "Test on LINUX_CAPABILITY_VERSION_2"}, - {0x20080522, "Test on LINUX_CAPABILITY_VERSION_3"}, + {0x19980330, "LINUX_CAPABILITY_VERSION_1"}, + {0x20071026, "LINUX_CAPABILITY_VERSION_2"}, + {0x20080522, "LINUX_CAPABILITY_VERSION_3"}, }; static void verify_capset(unsigned int n) { struct tcase *tc = &tcases[n]; - tst_res(TINFO, "%s", tc->message); header->version = tc->version; header->pid = pid;
@@ -38,11 +37,8 @@ static void verify_capset(unsigned int n)return ; } -TEST (tst_syscall (__NR_capset, header, data)); -if (TST_RET ==0 ) -tst_res (TPASS ,"capset() returned %ld" ,TST_RET ); -else -tst_res (TFAIL |TTERRNO ,"Test Failed, capset() returned %ld" ,TST_RET ); +TST_EXP_PASS (tst_syscall (__NR_capset, header, data), +"capset() with %s" , tc->message); }static void setup (void )
testcases/kernel/syscalls/capset/capset02.c +9 -19
@@ -43,12 +43,12 @@ static struct tcase {int flag;char *message; } tcases[] = { - {0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 1, "Test bad address header"}, - {0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 2, "Test bad address data"}, - {0, 0, CAP1, CAP1, CAP1, EINVAL, 0, "Test bad version"}, - {0x20080522, 0, CAP2, CAP1, CAP1, EPERM, 0, "Test bad value data(when pE is not in pP)"}, - {0x20080522, 0, CAP1, CAP2, CAP1, EPERM, 0, "Test bad value data(when pP is not in old pP)"}, - {0x20080522, 0, CAP1, CAP1, CAP2, EPERM, 0, "Test bad value data(when pI is not in bounding set or old pI)"}, + {0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 1, "bad address header"}, + {0x20080522, 0, CAP1, CAP1, CAP1, EFAULT, 2, "bad address data"}, + {0, 0, CAP1, CAP1, CAP1, EINVAL, 0, "bad version"}, + {0x20080522, 0, CAP2, CAP1, CAP1, EPERM, 0, "bad value data(when pE is not in pP)"}, + {0x20080522, 0, CAP1, CAP2, CAP1, EPERM, 0, "bad value data(when pP is not in old pP)"}, + {0x20080522, 0, CAP1, CAP1, CAP2, EPERM, 0, "bad value data(when pI is not in bounding set or old pI)"}, }; static void verify_capset(unsigned int n)
@@ -62,19 +62,9 @@ static void verify_capset(unsigned int n) data->permitted = tc->permitted; data->inheritable = tc->inheritable; -tst_res (TINFO ,"%s" , tc->message); - -TEST (tst_syscall (__NR_capset, tc->flag -1 ? header : bad_addr, - tc->flag -2 ? data : bad_addr)); -if (TST_RET ==0 ) { -tst_res (TFAIL ,"capset() succeed unexpectedly" ); -return ; - } -if (TST_ERR == tc->exp_err) -tst_res (TPASS |TTERRNO ,"capset() failed as expected" ); -else -tst_res (TFAIL |TTERRNO ,"capset() expected %s got " , -tst_strerrno (tc->exp_err)); +TST_EXP_FAIL (tst_syscall (__NR_capset, tc->flag -1 ? header : bad_addr, + tc->flag -2 ? data : bad_addr), + tc->exp_err,"capset() with %s" , tc->message);/* * When an unsupported version value is specified, it will * return the kernel preferred value of _LINUX_CAPABILITY_VERSION_?.
testcases/kernel/syscalls/capset/capset03.c +1 -9
@@ -23,15 +23,7 @@ static void verify_capset(void) {tst_res (TINFO ,"Test bad value data(when pI is not old pP or old pI without CAP_SETPCAP)" ); data[0 ].inheritable =CAP2 ; -TEST (tst_syscall (__NR_capset, header, data)); -if (TST_RET ==0 ) { -tst_res (TFAIL ,"capset succeed unexpectedly" ); -return ; - } -if (TST_ERR ==EPERM ) -tst_res (TPASS |TTERRNO ,"capset() failed as expected" ); -else -tst_res (TFAIL |TTERRNO ,"capset expected EPERM, bug got" ); +TST_EXP_FAIL (tst_syscall (__NR_capset, header, data),EPERM ,"capset()" ); }static void setup (void )
testcases/kernel/syscalls/capset/capset04.c +3 -10
@@ -24,18 +24,11 @@ static void verify_capset(void)if (!child_pid)pause (); - header->pid = child_pid; +tst_res (TINFO ,"Test capset() for a different process" ); -TEST (tst_syscall (__NR_capset, header, data)); -if (TST_RET ==0 ) { -tst_res (TFAIL ,"capset succeed unexpectedly" ); -return ; - } + header->pid = child_pid; -if (TST_ERR ==EPERM ) -tst_res (TPASS ,"capset can't modify other process capabilities" ); -else -tst_res (TFAIL |TTERRNO ,"capset expected EPERM, bug got" ); +TST_EXP_FAIL (tst_syscall (__NR_capset, header, data),EPERM ,"capset()" );SAFE_KILL (child_pid,SIGTERM );SAFE_WAIT (NULL );
Signature · 1 line
2.26.2
PATCH v2 10/10 syscalls/open: Make use of TST_EXP_MACROS
In reply to Cyril HrubisIn the newlib testcases at least.
--- testcases/kernel/syscalls/open/open01.c | 11 ++++------- testcases/kernel/syscalls/open/open02.c | 25 ++++++------------------- testcases/kernel/syscalls/open/open11.c | 22 ++++++++++------------ 3 files changed, 20 insertions(+), 38 deletions(-)
testcases/kernel/syscalls/open/open01.c +4 -7
@@ -36,8 +36,8 @@ static struct tcase {unsigned short tst_bit;char *desc; } tcases[] = { - {TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "Sticky bit"}, - {TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "Directory bit"} + {TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"}, + {TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "sirectory bit"} }; static void verify_open(unsigned int n)
@@ -45,12 +45,9 @@ static void verify_open(unsigned int n)struct tcase *tc = &tcases[n];struct stat buf; -TEST (open (tc->filename, tc->flag, tc->mode)); +TST_EXP_FD (open (tc->filename, tc->flag, tc->mode), +"open() with %s" , tc->desc); fd =TST_RET ; -if (fd ==-1 ) { -tst_res (TFAIL ,"Cannot open a file" ); -return ; - }SAFE_FSTAT (fd, &buf);if (!(buf.st_mode & tc->tst_bit))
testcases/kernel/syscalls/open/open02.c +6 -19
@@ -25,12 +25,13 @@#define TEST_FILE2 "test_file2"static struct tcase { -char *filename; +const char *filename;int flag;int exp_errno; +const char *desc; } tcases[] = { - {TEST_FILE ,O_RDWR ,ENOENT }, - {TEST_FILE2 ,O_RDONLY |O_NOATIME ,EPERM }, + {TEST_FILE ,O_RDWR ,ENOENT ,"new file without O_CREAT" }, + {TEST_FILE2 ,O_RDONLY |O_NOATIME ,EPERM ,"unpriviledget O_RDONLY | O_NOATIME" }, };void setup (void )
@@ -48,22 +49,8 @@ static void verify_open(unsigned int n) {struct tcase *tc = &tcases[n]; -TEST (open (tc->filename, tc->flag,0444 )); - -if (TST_RET !=-1 ) { -tst_res (TFAIL ,"open(%s) succeeded unexpectedly" , - tc->filename); -return ; - } - -if (tc->exp_errno !=TST_ERR ) { -tst_res (TFAIL |TTERRNO , -"open() should fail with %s" , -tst_strerrno (tc->exp_errno)); -return ; - } - -tst_res (TPASS |TTERRNO ,"open() failed as expected" ); +TST_EXP_FAIL (open (tc->filename, tc->flag,0444 ), + tc->exp_errno,"open() %s" , tc->desc); }void cleanup (void )
testcases/kernel/syscalls/open/open11.c +10 -12
@@ -277,21 +277,19 @@ static struct test_case {static void verify_open (unsigned int n) { -int fd; - -TEST (open (tc[n].path, tc[n].flags, tc[n].mode)); - fd =TST_RET ; - -if (fd >0 ) -SAFE_CLOSE (fd); - -if (tc[n].err ==-1 ||TST_ERR == tc[n].err) { +if (tc[n].err >0 ) { +TST_EXP_FAIL (open (tc[n].path, tc[n].flags, tc[n].mode), + tc[n].err,"%s" , tc[n].desc); + }else if (tc[n].err ==0 ) { +TST_EXP_FD (open (tc[n].path, tc[n].flags, tc[n].mode), +"%s" , tc[n].desc); + }else { +TEST (open (tc[n].path, tc[n].flags, tc[n].mode));tst_res (TPASS ,"%s" , tc[n].desc); -return ; } -tst_res (TFAIL |TTERRNO ,"%s - expected %s" , - tc[n].desc,tst_strerrno (tc[n].err)); +if (TST_RET >0 ) +SAFE_CLOSE (TST_RET ); }static void setup (void )
Signature · 1 line
2.26.2
v2 cover Introduce TST_EXP_MACROS
In reply to Cyril HrubisQuoted 12 lines · Cyril Hrubis
This patchset adds
TST_EXP_MACROS()implementation.These macros are intended to simplify and unify syscall unit tests.
This patchset converts a few testcases to use
TST_EXP_MACROS()it's intenteded to serve as an example since there is a lot of testcases that could use them as well.Also the documentation for these macros is ommited at this point, I will add it in a case that we agree to include them in the test library.
Quoted 6 lines
Changes in v2:
- renamed the macros so that it's more clear that they do
- added documentation to test-writing-guidelines
- minor fixed in the converted testcases
One more point, we might have to handle the unsupported syscall issue in TST_EXP_*. Otherwise, we can NOT make use of these macros in tests like madvise01.c. But, I agree to merge patchset as V2, and solve that in follow-up changes.
For the series:
Signature · 5 lines
Regards, Li Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.linux.it/pipermail/ltp/attachments/20201211/2c269794/attachment-0001.htm>
v2 cover Introduce TST_EXP_MACROS
Hi!
Quoted 4 lines
One more point, we might have to handle the unsupported syscall issue in TST_EXP_*. Otherwise, we can NOT make use of these macros in tests like
madvise01.c. But, I agree to merge patchset as V2, and solve that in follow-up changes.
We should add versions that will take zero terminated array of errnos that should end up in TCONF later on, but as said I want to convert the easy cases for now.
Quoted 2 lines
For the series:
Acked-by Li Wang <[email protected]>
Pushed, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]
v2 cover Introduce TST_EXP_MACROS
In reply to Cyril HrubisHi Cyril,
Very nice helpers!
Another tip for future: many tests would benefit for handling some errno as TCONF (e.g. TST_EXP_{FAIL,PASS}_TCONF(), but it's be nice if more than one could be passed).
Kind regards,
Petr
v2 cover Introduce TST_EXP_MACROS
Hi!
Quoted 3 lines
Another tip for future: many tests would benefit for handling some errno as TCONF (e.g. TST_EXP_{FAIL,PASS}_TCONF(), but it's be nice if more than one could be passed).
See the answer to Li.
Also thanks for the review.
Signature · 2 lines
Cyril Hrubis [email protected]