syscall/renameat2: Add tests for the syscall renameat2
PATCH 1/1 syscall/renameat2: Add tests for the syscall renameat2
--- include/lapi/fcntl.h | 12 ++ runtest/syscalls | 3 + testcases/kernel/syscalls/.gitignore | 2 + testcases/kernel/syscalls/renameat2/Makefile | 24 ++++ testcases/kernel/syscalls/renameat2/renameat2.h | 37 ++++++ testcases/kernel/syscalls/renameat2/renameat201.c | 152 ++++++++++++++++++++++ testcases/kernel/syscalls/renameat2/renameat202.c | 129 ++++++++++++++++++ 7 files changed, 359 insertions(+) create mode 100644 testcases/kernel/syscalls/renameat2/Makefile create mode 100644 testcases/kernel/syscalls/renameat2/renameat2.h create mode 100644 testcases/kernel/syscalls/renameat2/renameat201.c create mode 100644 testcases/kernel/syscalls/renameat2/renameat202.c
include/lapi/fcntl.h +12 -0
@@ -67,6 +67,18 @@# define FALLOC_FL_KEEP_SIZE 1#endif +#ifndef RENAME_NOREPLACE +# define RENAME_NOREPLACE (1 << 0) +#endif + +#ifndef RENAME_EXCHANGE +# define RENAME_EXCHANGE (1 << 1) +#endif + +#ifndef RENAME_WHITEOUT +# define RENAME_WHITEOUT (1 << 2) +#endif +/* splice, vmsplice, tee */ #ifndef SPLICE_F_NONBLOCK
runtest/syscalls +3 -0
@@ -863,6 +863,9 @@ rename14 rename14 #renameat test cases renameat01 renameat01 +renameat201 renameat201 +renameat202 renameat202 + rmdir01 rmdir01 rmdir02 rmdir02 rmdir03 rmdir03
testcases/kernel/syscalls/.gitignore +2 -0
@@ -713,6 +713,8 @@ /rename/rename13 /rename/rename14 /renameat/renameat01 +/renameat2/renameat201 +/renameat2/renameat202 /rmdir/rmdir01 /rmdir/rmdir02 /rmdir/rmdir03
testcases/kernel/syscalls/renameat2/Makefile new file +24 -0
@@ -0,0 +1,24 @@ +# +# Copyright (C) 2015 Cedric Hnyda [email protected] +# +# 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 St, Fifth Floor, Boston, MA 02110-1301 USA +# + + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/renameat2/renameat2.h new file +37 -0
@@ -0,0 +1,37 @@ +/* +* Copyright (C) 2015 Cedric Hnyda [email protected] +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it would be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* +* Further, this software is distributed without any warranty that it is +* free of the rightful claim of any third person regarding infringement +* or the like. Any license provided herein, whether implied or +* otherwise, applies only to this software file. Patent licenses, if +* any, provided herein do not apply to combinations of this program with +* other software, or any other product whatsoever. +* +*/ + +#ifndef RENAMEAT2_H +#define RENAMEAT2_H + +#include <sys/types.h> +#include "config.h" +#include "linux_syscall_numbers.h" + +#if !defined(HAVE_RENAMEAT2 ) +int renameat2 (int olddirfd,const char *oldpath,int newdirfd, +const char *newpath,unsigned int flags) +{ +return ltp_syscall (__NR_renameat2, olddirfd, oldpath, newdirfd, + newpath, flags); +} +#endif + +#endif /* RENAMEAT2_H */
testcases/kernel/syscalls/renameat2/renameat201.c new file +152 -0
152 lines not on this page · raw message
testcases/kernel/syscalls/renameat2/renameat202.c new file +129 -0
129 lines not on this page · raw message
Signature · 1 line
2.1.4
PATCH 1/1 syscall/renameat2: Add tests for the syscall renameat2
Hi!
Quoted 18 lines
testcases/kernel/syscalls/renameat2/renameat2.h new file +18
-0+/* +* Copyright (C) 2015 Cedric Hnyda [email protected] +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it would be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* +* Further, this software is distributed without any warranty that it is +* free of the rightful claim of any third person regarding infringement +* or the like. Any license provided herein, whether implied or +* otherwise, applies only to this software file. Patent licenses, if +* any, provided herein do not apply to combinations of this program with +* other software, or any other product whatsoever. +*
testcases/kernel/syscalls/renameat2/renameat2.h line 28
Quoted 10 lines
testcases/kernel/syscalls/renameat2/renameat2.h new file +10
-0+*/ + +#ifndef RENAMEAT2_H +#define RENAMEAT2_H + +#include <sys/types.h> +#include "config.h" +#include "linux_syscall_numbers.h" + +#if !defined(HAVE_RENAMEAT2 )
We need to add a configure check for renameat2 otherwise this macro is never defined. Have a look at m4/ltp-renameat.m4 which defines LTP_CHECK_RENAMEAT which is then called from configure.ac.
testcases/kernel/syscalls/renameat2/renameat2.h line 33
Quoted 5 lines
testcases/kernel/syscalls/renameat2/renameat2.h new file +5
-0+int renameat2 (int olddirfd,const char *oldpath,int newdirfd, +const char *newpath,unsigned int flags) +{ +return ltp_syscall (__NR_renameat2, olddirfd, oldpath, newdirfd, + newpath, flags);
Similarily we should make sure that __NR_renameat2 is defined, hence we should add the syscall numbers into testcases/kernel/include/*.in (Hint just do git grep __NR_renameat2 in the Linux kernel sources to get the syscall numbers)
Quoted 150 lines
testcases/kernel/syscalls/renameat2/renameat2.h new file +4
-0+} +#endif + +#endif /* RENAMEAT2_H */ testcases/kernel/syscalls/renameat2/renameat201.c new file +140
-0@@ -0,0 +1,152 @@ +/* +* Copyright (c) 2015 Cedric Hnyda <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it would be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* +* Further, this software is distributed without any warranty that it is +* free of the rightful claim of any third person regarding infringement +* or the like. Any license provided herein, whether implied or +* otherwise, applies only to this software file. Patent licenses, if +* any, provided herein do not apply to combinations of this program with +* other software, or any other product whatsoever. +*/ + +/* Description: +* Verify that: +* 1) renameat2(2) returns -1 and sets errno to EEXIST because newpath +* already exists and the flag RENAME_NOREPLACE is used. +* 2) renameat2(2) returns 0. +* 3) renameat2(2) returns -1 and sets errno to ENOENT because the flag +* RENAME_EXCHANGE is used and newpath does not exist. +* 4) renameat2(2) returns 0 because the flag RENAME_NOREPLACE is used, +* both olddirfd and newdirfd are valid and oldpath exists and +* newpath does not exist. +* 5) renameat2(2) returns -1 and sets errno to EINVAL because +* RENAME_NOREPLACE and RENAME_EXCHANGE are used together +* 6) renameat2(2) returns -1 and sets errno to EINVAL because +* RENAME_WHITEOUT and RENAME_EXCHANGE are used together +*/ + +#define _GNU_SOURCE + +#include "test.h" +#include "safe_macros.h" +#include "lapi/fcntl.h" +#include "renameat2.h" + +#define TEST_DIR "test_dir/" +#define TEST_DIR2 "test_dir2/" + +#define TEST_FILE "test_file" +#define TEST_FILE2 "test_file2" +#define TEST_FILE3 "test_file3" +#define NON_EXIST "non_exist" + +char *TCID ="renameat201" ; + +static int olddirfd; +static int newdirfd; + + +static struct test_case { +int *olddirfd; +const char *oldpath; +int *newdirfd; +const char *newpath; +int flags; +int exp_errno; +} test_cases[] = { + {&olddirfd,TEST_FILE , &newdirfd,TEST_FILE2 ,RENAME_NOREPLACE ,EEXIST }, + {&olddirfd,TEST_FILE , &newdirfd,TEST_FILE2 ,RENAME_EXCHANGE ,0 }, + {&olddirfd,TEST_FILE , &newdirfd,NON_EXIST ,RENAME_EXCHANGE ,ENOENT }, + {&olddirfd,TEST_FILE , &newdirfd,TEST_FILE3 ,RENAME_NOREPLACE ,0 }, + {&olddirfd,TEST_FILE , &newdirfd,TEST_FILE2 ,RENAME_NOREPLACE + |RENAME_EXCHANGE ,EINVAL }, + {&olddirfd,TEST_FILE , &newdirfd,TEST_FILE2 ,RENAME_WHITEOUT + |RENAME_EXCHANGE ,EINVAL } +}; + +int TST_TOTAL =ARRAY_SIZE (test_cases); + +static void setup (void ); +static void cleanup (void ); +static void renameat2_verify (const struct test_case *test); + + +int main (int ac,char **av) +{ +int i; +int lc; + +tst_parse_opts (ac, av,NULL ,NULL ); + +setup (); + +for (lc =0 ; lc <TEST_LOOPING (lc); lc++) { + tst_count =0 ; + +for (i =0 ; i <TST_TOTAL ; i++) +renameat2_verify (&test_cases[i]); + } +cleanup (); +tst_exit (); +} + +static void setup (void ) +{ +if ((tst_kvercmp (3 ,15 ,0 )) <0 ) { +tst_brkm (TCONF ,NULL , +"This test can only run on kernels that are 3.15. and higher" ); + } + +tst_tmpdir (); + +SAFE_MKDIR (cleanup,TEST_DIR ,0700 ); +SAFE_MKDIR (cleanup,TEST_DIR2 ,0700 ); + + SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE, 0600, NULL); + SAFE_TOUCH(cleanup, TEST_DIR2 TEST_FILE2, 0600, NULL); + SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE3, 0600, NULL); + + olddirfd =SAFE_OPEN (cleanup,TEST_DIR ,O_DIRECTORY ); + newdirfd =SAFE_OPEN (cleanup,TEST_DIR2 ,O_DIRECTORY ); +} + +static void cleanup (void ) +{ +if (olddirfd >0 &&close (olddirfd) <0 ) +tst_resm (TWARN |TERRNO ,"close olddirfd failed" ); + +if (newdirfd >0 &&close (newdirfd) <0 ) +tst_resm (TWARN |TERRNO ,"close newdirfd failed" ); + +tst_rmdir (); + +} + +static void renameat2_verify (const struct test_case *test) +{ +TEST (renameat2 (*(test->olddirfd), test->oldpath, + *(test->newdirfd), test->newpath, test->flags)); + +if (test->exp_errno &&TEST_RETURN !=-1 ) { +tst_resm (TFAIL ,"renameat2() succeeded unexpectedly" ); + }else if (test->exp_errno ==0 &&TEST_RETURN !=0 ) {
testcases/kernel/syscalls/renameat2/renameat201.c line 150
Quoted 10 lines
testcases/kernel/syscalls/renameat2/renameat201.c new file +10
-0+tst_resm (TFAIL |TTERRNO ,"renameat2() failed unexpectedly" ); + }else if (test->exp_errno ==TEST_ERRNO ) { +tst_resm (TPASS |TTERRNO , +"renameat2() returned the expected value" ); + }else { +tst_resm (TFAIL |TTERRNO , +"renameat2() got unexpected return value: expected: %d - %s" , + test->exp_errno, +strerror (test->exp_errno)); + }
I would write this part using return instead of the else if as:
if (foo) {
tst_resm(...);
return;
}if (bar) {
tst_resm(...);
return;
}As I find it easier to read.
Quoted 54 lines
testcases/kernel/syscalls/renameat2/renameat201.c new file +1
-0+}testcases/kernel/syscalls/renameat2/renameat202.c new file +47
-0@@ -0,0 +1,129 @@ +/* +* Copyright (c) 2015 Cedric Hnyda <[email protected]> +* +* This program is free software; you can redistribute it and/or modify it +* under the terms of version 2 of the GNU General Public License as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it would be useful, but +* WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* +* Further, this software is distributed without any warranty that it is +* free of the rightful claim of any third person regarding infringement +* or the like. Any license provided herein, whether implied or +* otherwise, applies only to this software file. Patent licenses, if +* any, provided herein do not apply to combinations of this program with +* other software, or any other product whatsoever. +*/ + +/* Description: +* Calls renameat2(2) with the flag RENAME_EXCHANGE and check that +* the content was swapped +*/ + +#define _GNU_SOURCE + +#include "test.h" +#include "safe_macros.h" +#include "lapi/fcntl.h" +#include "renameat2.h" + +#define TEST_DIR "test_dir/" +#define TEST_DIR2 "test_dir2/" + +#define TEST_FILE "test_file" +#define TEST_FILE2 "test_file2" + +char *TCID ="renameat202" ; + +static int olddirfd; +static int newdirfd; +static int fd =-1 ; + +static const char *content ="content" ; + + +int TST_TOTAL =1 ;
testcases/kernel/syscalls/renameat2/renameat202.c line 57
Quoted 10 lines
testcases/kernel/syscalls/renameat2/renameat202.c new file +10
-0+ +static void setup (void ); +static void cleanup (void ); +static void renameat2_verify (void ); + + +int main (int ac,char **av) +{ + +
Minor style nit, why do we add two empty lines here?
testcases/kernel/syscalls/renameat2/renameat202.c line 65
Quoted 8 lines
testcases/kernel/syscalls/renameat2/renameat202.c new file +8
-0+tst_parse_opts (ac, av,NULL ,NULL ); + +setup (); + +TEST (renameat2 (olddirfd,TEST_FILE , + newdirfd,TEST_FILE2 ,RENAME_EXCHANGE )); + +renameat2_verify ();
We should do the test in the TEST_LOOPING() loop here as well, otherwise some of the standard test options will not work.
Quoted 43 lines
testcases/kernel/syscalls/renameat2/renameat202.c new file +43
-0+cleanup (); +tst_exit (); +} + +static void setup (void ) +{ +if ((tst_kvercmp (3 ,15 ,0 )) <0 ) { +tst_brkm (TCONF ,NULL , +"This test can only run on kernels that are 3.15. and higher" ); + } + +tst_tmpdir (); + +SAFE_MKDIR (cleanup,TEST_DIR ,0700 ); +SAFE_MKDIR (cleanup,TEST_DIR2 ,0700 ); + + SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE, 0600, NULL); + SAFE_TOUCH(cleanup, TEST_DIR2 TEST_FILE2, 0600, NULL); + + olddirfd =SAFE_OPEN (cleanup,TEST_DIR ,O_DIRECTORY ); + newdirfd =SAFE_OPEN (cleanup,TEST_DIR2 ,O_DIRECTORY ); + + SAFE_FILE_PRINTF(cleanup, TEST_DIR TEST_FILE, "%s", content); + +} + +static void cleanup (void ) +{ +if (olddirfd >0 &&close (olddirfd) <0 ) +tst_resm (TWARN |TERRNO ,"close olddirfd failed" ); + +if (newdirfd >0 &&close (newdirfd) <0 ) +tst_resm (TWARN |TERRNO ,"close newdirfd failed" ); + +if (fd >0 &&close (fd) <0 ) +tst_resm (TWARN |TERRNO ,"close fd failed" ); + +tst_rmdir (); + +} + +static void renameat2_verify (void ) +{
testcases/kernel/syscalls/renameat2/renameat202.c line 119
Quoted 10 lines
testcases/kernel/syscalls/renameat2/renameat202.c new file +10
-0+char *str; + +if (TEST_RETURN !=0 ) { +tst_resm (TFAIL ,"renameat2() failed unexpectedly" ); +return ; + } + + fd = SAFE_OPEN(cleanup, TEST_DIR2 TEST_FILE2, O_RDONLY); + + str =malloc (sizeof (char ) *strlen (content));
You are not checking the return value from malloc here, also there is no need to allocate the buffer if the size is known at the compile time.
If you have defined the content as an array, you can use sizeof() to get it's size:
static const char content[] = "content";
Then you can do here:
char str[sizeof(content)];
Moreover we may want to try to read more than the size of the string here to make sure it wasn't corrupted.
testcases/kernel/syscalls/renameat2/renameat202.c line 122
Quoted 3 lines
testcases/kernel/syscalls/renameat2/renameat202.c new file +3
-0+SAFE_READ (cleanup,1 , fd, str,strlen (content)); + +if (!strcmp (content, str))
Here we rely on the fact that the data read from the file are null terminated. It would be more robust to check that they really are, because if they are not the test will crash here.
testcases/kernel/syscalls/renameat2/renameat202.c line 129
Quoted 7 lines
testcases/kernel/syscalls/renameat2/renameat202.c new file +7
-0+tst_resm (TPASS , +"renameat2() swapped the content of the two files" ); + else + tst_resm(TFAIL, + "renameat2() didn't swap the content of the two files"); +free (str); +}
Apart from the minor nits, the patches looks very good.
Signature · 2 lines
Cyril Hrubis [email protected]