blob: d4f964fb6af2f7444766ae0011d41f8d7011a969 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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_modak4bb656a2009-02-26 12:02:09 +000026 *
plars865695b2001-08-27 22:15:12 +000027 * 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_modakbdbaec52009-02-26 12:14:51 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * 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>
robbiewf44201d2001-09-25 16:07:44 +000072#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000073
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_modak56207ce2009-03-23 13:35:39 +000082char *TCID = "readlink01"; /* Test program identifier. */
83int TST_TOTAL = 1; /* Total number of test cases. */
plars865695b2001-08-27 22:15:12 +000084extern int Tst_count; /* Test Case counter for tst_* routines */
85
86int exp_val; /* strlen of testfile */
87
88void setup(); /* Setup function for the test */
89void cleanup(); /* Cleanup function for the test */
90
robbiewf44201d2001-09-25 16:07:44 +000091char nobody_uid[] = "nobody";
92struct passwd *ltpuser;
93
subrata_modak56207ce2009-03-23 13:35:39 +000094int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000095{
subrata_modak56207ce2009-03-23 13:35:39 +000096 char buffer[MAX_SIZE]; /* temporary buffer to hold symlink contents */
plars865695b2001-08-27 22:15:12 +000097 int lc; /* loop counter */
98 char *msg; /* message returned from parse_opts */
subrata_modak56207ce2009-03-23 13:35:39 +000099
plars865695b2001-08-27 22:15:12 +0000100 /* Parse standard options given to run the test. */
subrata_modak56207ce2009-03-23 13:35:39 +0000101 msg = parse_opts(ac, av, (option_t *) NULL, NULL);
plars865695b2001-08-27 22:15:12 +0000102 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_modak56207ce2009-03-23 13:35:39 +0000113 Tst_count = 0;
subrata_modakbdbaec52009-02-26 12:14:51 +0000114
subrata_modak4bb656a2009-02-26 12:02:09 +0000115 /*
plars865695b2001-08-27 22:15:12 +0000116 * Call readlink(2) to read the contents of
117 * symlink into a buffer.
118 */
119 TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
subrata_modakbdbaec52009-02-26 12:14:51 +0000120
plars865695b2001-08-27 22:15:12 +0000121 /* Check return code of readlink(2) */
122 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000123 tst_resm(TFAIL,
124 "readlink() on %s failed, errno=%d : %s",
125 SYMFILE, TEST_ERRNO, strerror(TEST_ERRNO));
plars865695b2001-08-27 22:15:12 +0000126 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_modak56207ce2009-03-23 13:35:39 +0000141 if (memcmp(buffer, TESTFILE, exp_val) != 0) {
plars865695b2001-08-27 22:15:12 +0000142 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_modak56207ce2009-03-23 13:35:39 +0000158 } /* End for TEST_LOOPING */
plars865695b2001-08-27 22:15:12 +0000159
160 /* Call cleanup() to undo setup done for the test. */
161 cleanup();
162
subrata_modak43337a32009-02-26 11:43:51 +0000163 return 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000164} /* End main */
plars865695b2001-08-27 22:15:12 +0000165
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_modak56207ce2009-03-23 13:35:39 +0000173void setup()
plars865695b2001-08-27 22:15:12 +0000174{
175 int fd; /* file handle for testfile */
176
robbiewf44201d2001-09-25 16:07:44 +0000177 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000178 if (geteuid() != 0) {
179 tst_brkm(TBROK, tst_exit, "Test must be run as root");
180 }
181 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
mridge75cee4f2004-09-08 15:31:00 +0000182 tst_brkm(TBROK, cleanup, "getpwname(nobody_uid) failed ");
subrata_modak56207ce2009-03-23 13:35:39 +0000183 }
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 }
plars865695b2001-08-27 22:15:12 +0000189
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_modak56207ce2009-03-23 13:35:39 +0000199 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
plars865695b2001-08-27 22:15:12 +0000200 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_modak56207ce2009-03-23 13:35:39 +0000207 TESTFILE, errno, strerror(errno));
plars865695b2001-08-27 22:15:12 +0000208 }
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_modak56207ce2009-03-23 13:35:39 +0000227void cleanup()
plars865695b2001-08-27 22:15:12 +0000228{
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}