plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
robbiew | f44201d | 2001-09-25 16:07:44 +0000 | [diff] [blame] | 72 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | #include "test.h" |
| 75 | #include "usctest.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 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 82 | char *TCID = "readlink01"; /* Test program identifier. */ |
| 83 | int TST_TOTAL = 1; /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
| 85 | |
| 86 | int exp_val; /* strlen of testfile */ |
| 87 | |
| 88 | void setup(); /* Setup function for the test */ |
| 89 | void cleanup(); /* Cleanup function for the test */ |
| 90 | |
robbiew | f44201d | 2001-09-25 16:07:44 +0000 | [diff] [blame] | 91 | char nobody_uid[] = "nobody"; |
| 92 | struct passwd *ltpuser; |
| 93 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 94 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 96 | char buffer[MAX_SIZE]; /* temporary buffer to hold symlink contents */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | int lc; /* loop counter */ |
| 98 | char *msg; /* message returned from parse_opts */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 99 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | /* Parse standard options given to run the test. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 101 | msg = parse_opts(ac, av, (option_t *) NULL, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | if (msg != (char *)NULL) { |
| 103 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 104 | } |
| 105 | |
| 106 | /* Perform global setup for test */ |
| 107 | setup(); |
| 108 | |
| 109 | /* Check looping state if -i option given */ |
| 110 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 111 | |
| 112 | /* Reset Tst_count in case we are looping. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 113 | Tst_count = 0; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 114 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 115 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | * Call readlink(2) to read the contents of |
| 117 | * symlink into a buffer. |
| 118 | */ |
| 119 | TEST(readlink(SYMFILE, buffer, sizeof(buffer))); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | /* Check return code of readlink(2) */ |
| 122 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 123 | tst_resm(TFAIL, |
| 124 | "readlink() on %s failed, errno=%d : %s", |
| 125 | SYMFILE, TEST_ERRNO, strerror(TEST_ERRNO)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | continue; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Perform functional verification if test |
| 131 | * executed without (-f) option. |
| 132 | */ |
| 133 | if (STD_FUNCTIONAL_TEST) { |
| 134 | /* |
| 135 | * Compare the return value of readlink() |
| 136 | * with the expected value which is the |
| 137 | * strlen() of testfile. |
| 138 | */ |
| 139 | if (TEST_RETURN == exp_val) { |
| 140 | /* Check for the contents of buffer */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 141 | if (memcmp(buffer, TESTFILE, exp_val) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | tst_resm(TFAIL, "Pathname %s and buffer" |
| 143 | " contents %s differ", |
| 144 | TESTFILE, buffer); |
| 145 | } else { |
| 146 | tst_resm(TPASS, "readlink() " |
| 147 | "functionality on '%s' is " |
| 148 | "correct", SYMFILE); |
| 149 | } |
| 150 | } else { |
| 151 | tst_resm(TFAIL, "readlink() return value %d " |
| 152 | "does't match, Expected %d", |
| 153 | TEST_RETURN, exp_val); |
| 154 | } |
| 155 | } else { |
| 156 | tst_resm(TPASS, "call succeeded"); |
| 157 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 158 | } /* End for TEST_LOOPING */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
| 160 | /* Call cleanup() to undo setup done for the test. */ |
| 161 | cleanup(); |
| 162 | |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 163 | return 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 164 | } /* End main */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * setup() - performs all ONE TIME setup for this test. |
| 168 | * |
| 169 | * Create a temporary directory and change directory to it. |
| 170 | * Create a test file under temporary directory and close it |
| 171 | * Create a symbolic link of testfile. |
| 172 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 173 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | { |
| 175 | int fd; /* file handle for testfile */ |
| 176 | |
robbiew | f44201d | 2001-09-25 16:07:44 +0000 | [diff] [blame] | 177 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 178 | if (geteuid() != 0) { |
| 179 | tst_brkm(TBROK, tst_exit, "Test must be run as root"); |
| 180 | } |
| 181 | if ((ltpuser = getpwnam(nobody_uid)) == NULL) { |
mridge | 75cee4f | 2004-09-08 15:31:00 +0000 | [diff] [blame] | 182 | tst_brkm(TBROK, cleanup, "getpwname(nobody_uid) failed "); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 183 | } |
| 184 | if (seteuid(ltpuser->pw_uid) == -1) { |
| 185 | tst_resm(TINFO, "seteuid failed to " |
| 186 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 187 | perror("seteuid"); |
| 188 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
| 190 | /* capture signals */ |
| 191 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 192 | |
| 193 | /* Pause if that option was specified */ |
| 194 | TEST_PAUSE; |
| 195 | |
| 196 | /* make a temp directory and cd to it */ |
| 197 | tst_tmpdir(); |
| 198 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 199 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | tst_brkm(TBROK, cleanup, |
| 201 | "open(%s, O_RDWR|O_CREAT, %#o) failed, errno=%d : %s", |
| 202 | TESTFILE, FILE_MODE, errno, strerror(errno)); |
| 203 | } |
| 204 | |
| 205 | if (close(fd) == -1) { |
| 206 | tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 207 | TESTFILE, errno, strerror(errno)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* Create a symlink of testfile under temporary directory */ |
| 211 | if (symlink(TESTFILE, SYMFILE) < 0) { |
| 212 | tst_brkm(TBROK, cleanup, |
| 213 | "symlink(%s, %s) failed, errno=%d : %s", |
| 214 | TESTFILE, SYMFILE, errno, strerror(errno)); |
| 215 | } |
| 216 | |
| 217 | /* Get the strlen of testfile */ |
| 218 | exp_val = strlen(TESTFILE); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 223 | * completion or premature exit. |
| 224 | * |
| 225 | * Remove the test directory and testfile created in the setup. |
| 226 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 227 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | { |
| 229 | /* |
| 230 | * print timing stats if that option was specified. |
| 231 | * print errno log if that option was specified. |
| 232 | */ |
| 233 | TEST_CLEANUP; |
| 234 | |
| 235 | /* Remove tmp dir and all files in it */ |
| 236 | tst_rmdir(); |
| 237 | |
| 238 | /* exit with return code appropriate for results */ |
| 239 | tst_exit(); |
| 240 | } |