Home | History | Annotate | Download | only in rename
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * NAME
     22  *	rename10
     23  *
     24  * DESCRIPTION
     25  *	This test will verify that rename(2) syscall fails with ENAMETOOLONG
     26  *      and ENOENT
     27  *
     28  * ALGORITHM
     29  *	Setup:
     30  *		Setup signal handling.
     31  *		Create temporary directory.
     32  *		Pause for SIGUSR1 if option specified.
     33  *              create the "old" file
     34  *
     35  *	Test:
     36  *		Loop if the proper options are given.
     37  *              1.  rename the "old" to the "new" file
     38  *                  verify rename() failed with error ENAMETOOLONG
     39  *
     40  *              2.  "new" path contains a directory that does not exist
     41  *                  rename the "old" to the "new"
     42  *                  verify rename() failed with error ENOENT
     43  *	Cleanup:
     44  *		Print errno log and/or timing stats if options given
     45  *		Delete the temporary directory created.*
     46  *
     47  * USAGE
     48  *	rename10 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
     49  *	where,  -c n : Run n copies concurrently.
     50  *		-e   : Turn on errno logging.
     51  *		-i n : Execute test n times.
     52  *		-I x : Execute test for x seconds.
     53  *		-P x : Pause for x seconds between iterations.
     54  *		-t   : Turn on syscall timing.
     55  *
     56  * HISTORY
     57  *	07/2001 Ported by Wayne Boyer
     58  *
     59  * RESTRICTIONS
     60  *	None.
     61  */
     62 #include <sys/types.h>
     63 #include <fcntl.h>
     64 #include <unistd.h>
     65 #include <errno.h>
     66 
     67 #include "test.h"
     68 
     69 void setup();
     70 void cleanup();
     71 
     72 char *TCID = "rename10";
     73 int TST_TOTAL = 2;
     74 
     75 char badmname[] =
     76     "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
     77 
     78 int fd;
     79 char fname[255], mname[255];
     80 char mdir[255];
     81 
     82 struct test_case_t {
     83 	char *fd1;
     84 	char *fd2;
     85 	int error;
     86 } TC[] = {
     87 	/* badmname is too long for a file name - ENAMETOOLONG */
     88 	{
     89 	fname, badmname, ENAMETOOLONG},
     90 	    /* mname contains a directory component which does not exist - ENOENT */
     91 	{
     92 	fname, mname, ENOENT}
     93 };
     94 
     95 int main(int ac, char **av)
     96 {
     97 	int lc;
     98 	int i;
     99 
    100 	/*
    101 	 * parse standard options
    102 	 */
    103 	tst_parse_opts(ac, av, NULL, NULL);
    104 
    105 	/*
    106 	 * perform global setup for test
    107 	 */
    108 	setup();
    109 
    110 	/*
    111 	 * check looping state if -i option given
    112 	 */
    113 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    114 
    115 		tst_count = 0;
    116 
    117 		/* loop through the test cases */
    118 		for (i = 0; i < TST_TOTAL; i++) {
    119 
    120 			TEST(rename(TC[i].fd1, TC[i].fd2));
    121 
    122 			if (TEST_RETURN != -1) {
    123 				tst_resm(TFAIL, "call succeeded unexpectedly");
    124 				continue;
    125 			}
    126 
    127 			if (TEST_ERRNO == TC[i].error) {
    128 				tst_resm(TPASS, "expected failure - "
    129 					 "errno = %d : %s", TEST_ERRNO,
    130 					 strerror(TEST_ERRNO));
    131 			} else {
    132 				tst_resm(TFAIL, "unexpected error - %d : %s - "
    133 					 "expected %d", TEST_ERRNO,
    134 					 strerror(TEST_ERRNO), TC[i].error);
    135 			}
    136 		}
    137 	}
    138 
    139 	cleanup();
    140 	tst_exit();
    141 
    142 }
    143 
    144 /*
    145  * setup() - performs all ONE TIME setup for this test.
    146  */
    147 void setup(void)
    148 {
    149 
    150 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    151 
    152 	TEST_PAUSE;
    153 
    154 	/* Create a temporary directory and make it current. */
    155 	tst_tmpdir();
    156 
    157 	sprintf(fname, "./tfile_%d", getpid());
    158 	sprintf(mdir, "./rndir_%d", getpid());
    159 	sprintf(mname, "%s/rnfile_%d", mdir, getpid());
    160 
    161 	SAFE_TOUCH(cleanup, fname, 0700, NULL);
    162 }
    163 
    164 /*
    165  * cleanup() - performs all ONE TIME cleanup for this test at
    166  *             completion or premature exit.
    167  */
    168 void cleanup(void)
    169 {
    170 
    171 	/*
    172 	 * Remove the temporary directory.
    173 	 */
    174 	tst_rmdir();
    175 }
    176