Home | History | Annotate | Download | only in readlink
      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 : readlink01
     22  *
     23  * Test Description :
     24  *  Verify that, readlink will succeed to read the contents of the symbolic
     25  *  link created the process.
     26  *
     27  * Expected Result:
     28  *  readlink() should return the contents of symbolic link path in the buffer
     29  *  on success.
     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  *   	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  *  readlink01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
     54  *     where,  -c n : Run n copies concurrently.
     55  *             -f   : Turn off functionality Testing.
     56  *	       -i n : Execute test n times.
     57  *	       -I x : Execute test for x seconds.
     58  *	       -P x : Pause for x seconds between iterations.
     59  *	       -t   : Turn on syscall timing.
     60  *
     61  * HISTORY
     62  *	07/2001 Ported by Wayne Boyer
     63  *
     64  * RESTRICTIONS:
     65  *  This test should be run by 'non-super-user' only.
     66  *
     67  */
     68 #include <unistd.h>
     69 #include <fcntl.h>
     70 #include <errno.h>
     71 #include <sys/stat.h>
     72 #include <pwd.h>
     73 
     74 #include "test.h"
     75 #include "safe_macros.h"
     76 
     77 #define TESTFILE	"testfile"
     78 #define SYMFILE		"slink_file"
     79 #define FILE_MODE       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
     80 #define MAX_SIZE	256
     81 
     82 char *TCID = "readlink01";
     83 int TST_TOTAL = 1;
     84 
     85 const int exp_val = sizeof(TESTFILE) - 1;	/* strlen of testfile */
     86 
     87 void setup();
     88 void cleanup();
     89 
     90 char nobody_uid[] = "nobody";
     91 struct passwd *ltpuser;
     92 
     93 int main(int ac, char **av)
     94 {
     95 	char buffer[MAX_SIZE];	/* temporary buffer to hold symlink contents */
     96 	int lc;
     97 
     98 	tst_parse_opts(ac, av, NULL, NULL);
     99 
    100 	setup();
    101 
    102 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    103 
    104 		tst_count = 0;
    105 
    106 		/*
    107 		 * Call readlink(2) to read the contents of
    108 		 * symlink into a buffer.
    109 		 */
    110 		TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
    111 
    112 		if (TEST_RETURN == -1) {
    113 			tst_resm(TFAIL,
    114 				 "readlink() on %s failed, errno=%d : %s",
    115 				 SYMFILE, TEST_ERRNO, strerror(TEST_ERRNO));
    116 			continue;
    117 		}
    118 
    119 		/*
    120 		 * Compare the return value of readlink()
    121 		 * with the expected value which is the
    122 		 * strlen() of testfile.
    123 		 */
    124 		if (TEST_RETURN == exp_val) {
    125 			/* Check for the contents of buffer */
    126 			if (memcmp(buffer, TESTFILE, exp_val) != 0) {
    127 				tst_resm(TFAIL, "Pathname %s and buffer"
    128 					 " contents %s differ",
    129 					 TESTFILE, buffer);
    130 			} else {
    131 				tst_resm(TPASS, "readlink() "
    132 					 "functionality on '%s' is "
    133 					 "correct", SYMFILE);
    134 			}
    135 		} else {
    136 			tst_resm(TFAIL, "readlink() return value %ld "
    137 				 "does't match, Expected %d",
    138 				 TEST_RETURN, exp_val);
    139 		}
    140 	}
    141 
    142 	cleanup();
    143 	tst_exit();
    144 }
    145 
    146 /*
    147  * setup() - performs all ONE TIME setup for this test.
    148  *
    149  *  Create a temporary directory and change directory to it.
    150  *  Create a test file under temporary directory and close it
    151  *  Create a symbolic link of testfile.
    152  */
    153 void setup(void)
    154 {
    155 	int fd;			/* file handle for testfile */
    156 
    157 	tst_require_root();
    158 
    159 	if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
    160 		tst_brkm(TBROK, cleanup, "getpwname(nobody_uid) failed ");
    161 	}
    162 	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
    163 
    164 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    165 
    166 	TEST_PAUSE;
    167 
    168 	tst_tmpdir();
    169 
    170 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
    171 		tst_brkm(TBROK | TERRNO, cleanup,
    172 			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
    173 			 TESTFILE, FILE_MODE);
    174 	}
    175 
    176 	if (close(fd) == -1) {
    177 		tst_resm(TWARN | TERRNO, "close(%s) failed", TESTFILE);
    178 	}
    179 
    180 	/* Create a symlink of testfile under temporary directory */
    181 	SAFE_SYMLINK(cleanup, TESTFILE, SYMFILE);
    182 }
    183 
    184 /*
    185  * cleanup() - performs all ONE TIME cleanup for this test at
    186  *             completion or premature exit.
    187  *
    188  *  Remove the test directory and testfile created in the setup.
    189  */
    190 void cleanup(void)
    191 {
    192 
    193 	tst_rmdir();
    194 
    195 }
    196