Home | History | Annotate | Download | only in symlink
      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  * Test Name : symlink04
     22  *
     23  * Test Description :
     24  *  Verify that, symlink will succeed to creat a symbolic link of an existing
     25  *  object name path.
     26  *
     27  * Expected Result:
     28  *  symlink() should return value 0 on success and symbolic link of an
     29  *  existing object should be created.
     30  *
     31  * Algorithm:
     32  *  Setup:
     33  *   Setup signal handling.
     34  *   Create temporary directory.
     35  *   Pause for SIGUSR1 if option specified.
     36  *
     37  *  Test:
     38  *   Loop if the proper options are given.
     39  *   Execute system call
     40  *   Check return code, if system call failed (return=-1)
     41  *   	Log the errno and Issue a FAIL message.
     42  *   Otherwise,
     43  *   	Verify the Functionality of system call
     44  *      if successful,
     45  *      	Issue Functionality-Pass message.
     46  *      Otherwise,
     47  *		Issue Functionality-Fail message.
     48  *  Cleanup:
     49  *   Print errno log and/or timing stats if options given
     50  *   Delete the temporary directory created.
     51  *
     52  * Usage:  <for command-line>
     53  *  symlink04 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
     54  *	where,  -c n : Run n copies concurrently.
     55  *		-e   : Turn on errno logging.
     56  *		-f   : Turn off functionality Testing.
     57  *		-i n : Execute test n times.
     58  *		-I x : Execute test for x seconds.
     59  *		-P x : Pause for x seconds between iterations.
     60  *		-t   : Turn on syscall timing.
     61  *
     62  * History
     63  *	07/2001 John George
     64  *		-Ported
     65  *
     66  * Restrictions:
     67  *  None.
     68  *
     69  */
     70 
     71 #include <stdio.h>
     72 #include <sys/types.h>
     73 #include <sys/fcntl.h>
     74 #include <errno.h>
     75 #include <string.h>
     76 #include <signal.h>
     77 #include <sys/stat.h>
     78 #include "test.h"
     79 
     80 #define  TESTFILE	"testfile"
     81 #define  SYMFILE	"slink_file"
     82 #define FILE_MODE       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
     83 
     84 char *TCID = "symlink04";
     85 int TST_TOTAL = 1;
     86 
     87 void setup();
     88 void cleanup();
     89 
     90 int main(int ac, char **av)
     91 {
     92 	struct stat stat_buf;	/* stat structure buffer */
     93 	int lc;
     94 
     95 	tst_parse_opts(ac, av, NULL, NULL);
     96 
     97 	setup();
     98 
     99 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    100 
    101 		tst_count = 0;
    102 
    103 		/*
    104 		 * Call symlink(2) to create a symlink of
    105 		 * testfile.
    106 		 */
    107 		TEST(symlink(TESTFILE, SYMFILE));
    108 
    109 		if (TEST_RETURN == -1) {
    110 			tst_resm(TFAIL, "symlink(%s, %s) Failed, errno=%d : %s",
    111 				 TESTFILE, SYMFILE, TEST_ERRNO,
    112 				 strerror(TEST_ERRNO));
    113 		} else {
    114 			/*
    115 			 * Get the symlink file status information
    116 			 * using lstat(2).
    117 			 */
    118 			if (lstat(SYMFILE, &stat_buf) < 0) {
    119 				tst_brkm(TFAIL, cleanup, "lstat(2) of "
    120 					 "%s failed, error:%d", SYMFILE,
    121 					 errno);
    122 			}
    123 
    124 			/* Check if the st_mode contains a link  */
    125 			if (!S_ISLNK(stat_buf.st_mode)) {
    126 				tst_resm(TFAIL,
    127 					 "symlink of %s doesn't exist",
    128 					 TESTFILE);
    129 			} else {
    130 				tst_resm(TPASS, "symlink(%s, %s) "
    131 					 "functionality successful",
    132 					 TESTFILE, SYMFILE);
    133 			}
    134 		}
    135 
    136 		/* Unlink the symlink file for next loop */
    137 		if (unlink(SYMFILE) == -1) {
    138 			tst_brkm(TBROK, cleanup,
    139 				 "unlink(%s) Failed, errno=%d : %s",
    140 				 SYMFILE, errno, strerror(errno));
    141 		}
    142 		tst_count++;	/* incr TEST_LOOP counter */
    143 	}
    144 
    145 	cleanup();
    146 	tst_exit();
    147 
    148 }
    149 
    150 /*
    151  * void
    152  * setup() - performs all ONE TIME setup for this test.
    153  *  Create a temporary directory and change directory to it.
    154  *  Create a test file under temporary directory and close it
    155  */
    156 void setup(void)
    157 {
    158 	int fd;			/* file handle for testfile */
    159 
    160 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    161 
    162 	/* Pause if that option was specified
    163 	 * TEST_PAUSE contains the code to fork the test with the -i option.
    164 	 * You want to make sure you do this before you create your temporary
    165 	 * directory.
    166 	 */
    167 	TEST_PAUSE;
    168 
    169 	tst_tmpdir();
    170 
    171 	/* creat/open a testfile */
    172 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
    173 		tst_brkm(TBROK, cleanup,
    174 			 "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s",
    175 			 TESTFILE, FILE_MODE, errno, strerror(errno));
    176 	}
    177 
    178 	/* Close the temporary file created above */
    179 	if (close(fd) == -1) {
    180 		tst_resm(TBROK, "close(%s) Failed, errno=%d : %s",
    181 			 TESTFILE, errno, strerror(errno));
    182 	}
    183 }
    184 
    185 /*
    186  * void
    187  * cleanup() - performs all ONE TIME cleanup for this test at
    188  *             completion or premature exit.
    189  *  Remove the test directory and testfile created in the setup.
    190  */
    191 void cleanup(void)
    192 {
    193 
    194 	tst_rmdir();
    195 
    196 }
    197