blob: 484a6ae67847ae6b371d0b774183da6f209d6b17 [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: lstat02
22 *
23 * Test Description:
24 * Verify that,
25 * 1) lstat(2) returns -1 and sets errno to EACCES if search permission is
26 * denied on a component of the path prefix.
27 * 2) lstat(2) returns -1 and sets errno to ENOENT if the specified file
28 * does not exists or empty string.
29 * 3) lstat(2) returns -1 and sets errno to EFAULT if pathname points
30 * outside user's accessible address space.
31 * 4) lstat(2) returns -1 and sets errno to ENAMETOOLONG if the pathname
32 * component is too long.
33 * 5) lstat(2) returns -1 and sets errno to ENOTDIR if the directory
34 * component in pathname is not a directory.
35 *
36 * Expected Result:
37 * lstat() should fail with return value -1 and set expected errno.
38 *
39 * Algorithm:
40 * Setup:
41 * Setup signal handling.
42 * Create temporary directory.
43 * Pause for SIGUSR1 if option specified.
44 *
45 * Test:
46 * Loop if the proper options are given.
47 * Execute system call
48 * Check return code, if system call failed (return=-1)
49 * if errno set == expected errno
50 * Issue sys call fails with expected return value and errno.
51 * Otherwise,
52 * Issue sys call fails with unexpected errno.
53 * Otherwise,
54 * Issue sys call returns unexpected value.
55 *
56 * Cleanup:
57 * Print errno log and/or timing stats if options given
58 * Delete the temporary directory(s)/file(s) created.
59 *
60 * Usage: <for command-line>
61 * lstat02 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
62 * where, -c n : Run n copies concurrently.
63 * -e : Turn on errno logging.
64 * -f : Turn off functionality Testing.
65 * -i n : Execute test n times.
66 * -I x : Execute test for x seconds.
67 * -P x : Pause for x seconds between iterations.
68 * -t : Turn on syscall timing.
69 *
70 * HISTORY
71 * 07/2001 Ported by Wayne Boyer
72 *
73 * RESTRICTIONS:
74 * This test should be executed by 'non-super-user' only.
75 */
76
77#include <stdio.h>
78#include <stdlib.h>
79#include <unistd.h>
80#include <fcntl.h>
81#include <errno.h>
82#include <string.h>
83#include <signal.h>
84#include <sys/types.h>
85#include <sys/stat.h>
86
87#include "test.h"
88#include "usctest.h"
89
90#define MODE_RWX S_IRWXU | S_IRWXG | S_IRWXO
91#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
92#define DIR_TEMP "testdir_1"
93#define TEST_FILE1 "testdir_1/tfile_1"
94#define SFILE1 "testdir_1/sfile_1"
95#define TEST_FILE2 "t_file/tfile_2"
96#define SFILE2 "t_file/sfile_2"
97
98int no_setup();
99int setup1(); /* setup function to test chmod for EACCES */
100int setup2(); /* setup function to test chmod for ENOTDIR */
101int longpath_setup(); /* setup function to test chmod for ENAMETOOLONG */
102
103char Longpathname[PATH_MAX+2];
104char High_address_node[64];
105
106struct test_case_t { /* test case struct. to hold ref. test cond's*/
107 char *pathname;
108 char *desc;
109 int exp_errno;
110 int (*setupfunc)();
111} Test_cases[] = {
112 { SFILE1, "No Search permissions to process", EACCES, setup1 },
113 { High_address_node, "Address beyond address space", EFAULT, no_setup },
114 { (char *)-1, "Negative address", EFAULT, no_setup },
115 { Longpathname, "Pathname too long", ENAMETOOLONG, longpath_setup },
116 { "", "Pathname is empty", ENOENT, no_setup },
117 { SFILE2, "Path contains regular file", ENOTDIR, setup2 },
118 { NULL, NULL, 0, no_setup }
119};
120
121char *TCID="lstat02"; /* Test program identifier. */
122int TST_TOTAL = 6; /* Total number of test cases. */
123extern int Tst_count; /* Test Case counter for tst_* routines */
124int exp_enos[]={EACCES, EFAULT, ENAMETOOLONG, ENOENT, ENOTDIR, 0};
125
126void setup(); /* Main setup function for the tests */
127void cleanup(); /* cleanup function for the test */
128
129int
130main(int ac, char **av)
131{
132 struct stat stat_buf; /* stat structure buffer */
133 int lc; /* loop counter */
134 char *msg; /* message returned from parse_opts */
135 char *file_name; /* ptr. for file name whose mode is modified*/
136 char *test_desc; /* test specific error message */
137 int ind; /* counter to test different test conditions */
138
139 /* Parse standard options given to run the test. */
140 msg = parse_opts(ac, av, (option_t *) NULL, NULL);
141 if (msg != (char *) NULL) {
142 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
143 tst_exit();
144 }
145
146 /*
147 * Invoke setup function to call individual test setup functions
148 * to simulate test conditions.
149 */
150 setup();
151
152 /* set the expected errnos... */
153 TEST_EXP_ENOS(exp_enos);
154
155 /* Check looping state if -i option given */
156 for (lc = 0; TEST_LOOPING(lc); lc++) {
157 /* Reset Tst_count in case we are looping. */
158 Tst_count=0;
159
160 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
161 file_name = Test_cases[ind].pathname;
162 test_desc = Test_cases[ind].desc;
163
164 if (file_name == High_address_node) {
165 file_name = (char *)get_high_address();
166 }
167
168 /*
169 * Call lstat(2) to test different test conditions.
170 * verify that it fails with -1 return value and
171 * sets appropriate errno.
172 */
173 TEST(lstat(file_name, &stat_buf));
174
175 /* Check return code from lstat(2) */
176 if (TEST_RETURN != -1) {
177 tst_resm(TFAIL, "lstat(2) returned %d, expected"
178 " -1, errno:%d", TEST_RETURN,
179 Test_cases[ind].exp_errno);
180 continue;
181 }
182 TEST_ERROR_LOG(TEST_ERRNO);
183 if (TEST_ERRNO == Test_cases[ind].exp_errno) {
184 tst_resm(TPASS, "lstat() fails, %s, errno:%d",
185 test_desc, TEST_ERRNO);
186 } else {
187 tst_resm(TFAIL, "lstat() fails, %s, errno:%d, "
188 "expected errno:%d", test_desc,
189 TEST_ERRNO, Test_cases[ind].exp_errno);
190 }
191 } /* End of TEST CASE LOOPING. */
192 } /* End for TEST_LOOPING */
193
194 /*
195 * Invoke cleanup() to delete the test directory/file(s) created
196 * in the setup().
197 */
198 cleanup();
199
200 /*NOTREACHED*/
201} /* End main */
202
203/*
204 * setup(void) - performs all ONE TIME setup for this test.
205 * Exit the test program on receipt of unexpected signals.
206 * Create a temporary directory and change directory to it.
207 * Invoke individual test setup functions according to the order
208 * set in struct. definition.
209 */
210void
211setup()
212{
213 int ind; /* counter for setup functions */
214
215 /* Capture unexpected signals */
216 tst_sig(FORK, DEF_HANDLER, cleanup);
217
218 /* Check that the test process id is not root/super-user */
219 if (geteuid() == 0) {
220 tst_brkm(TBROK, NULL, "Must be non-root/super for this test!");
221 tst_exit();
222 }
223
224 /* Pause if that option was specified */
225 TEST_PAUSE;
226
227 /* Make a temp dir and cd to it */
228 tst_tmpdir();
229
230 /* call individual setup functions */
231 for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
232 Test_cases[ind].setupfunc();
233 }
234}
235
236/*
237 * no_setup() - Some test conditions for lstat(2) do not any setup.
238 * Hence, this function just returns 0.
239 * This function simply returns 0.
240 */
241int
242no_setup()
243{
244 return 0;
245}
246
247
248/*
249 * setup1() - setup function for a test condition for which lstat(2)
250 * returns -1 and sets errno to EACCES.
251 * Create a test directory under temporary directory and create a test file
252 * under this directory with mode "0666" permissions.
253 * Modify the mode permissions on test directory such that process will not
254 * have search permissions on test directory.
255 *
256 * The function returns 0.
257 */
258int
259setup1()
260{
261 int fd; /* file handle for testfile */
262
263 if (mkdir(DIR_TEMP, MODE_RWX) < 0) {
264 tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP);
265 }
266
267 if ((fd = open(TEST_FILE1, O_RDWR|O_CREAT, 0666)) == -1) {
268 tst_brkm(TBROK, cleanup,
269 "open(%s, O_RDWR|O_CREAT, 0666) failed, errno=%d : %s",
270 TEST_FILE1, errno, strerror(errno));
271 }
272 if (close(fd) == -1) {
273 tst_brkm(TBROK, cleanup,
274 "close(%s) Failed, errno=%d : %s",
275 TEST_FILE1, errno, strerror(errno));
276 }
277
278 /* Create a symlink of testfile */
279 if (symlink(TEST_FILE1, SFILE1) < 0) {
280 tst_brkm(TBROK, cleanup,
281 "symlink() of %s Failed, errno=%d : %s",
282 TEST_FILE1, errno, strerror(errno));
283 }
284
285 /* Modify mode permissions on test directory */
286 if (chmod(DIR_TEMP, FILE_MODE) < 0) {
287 tst_brkm(TBROK, cleanup, "chmod(2) of %s failed", DIR_TEMP);
288 }
289 return 0;
290}
291
292/*
293 * setup2() - setup function for a test condition for which lstat(2)
294 * returns -1 and sets errno to ENOTDIR.
295 *
296 * Create a test file under temporary directory so that test tries to
297 * change mode of a testfile "tfile_2" under "t_file" which happens to be
298 * another regular file.
299 */
300int
301setup2()
302{
303 int fd; /* file handle */
304
305 if ((fd = open("t_file", O_RDWR|O_CREAT, MODE_RWX)) == -1) {
306 tst_brkm(TBROK, cleanup,
307 "open(2) on t_file failed, errno=%d : %s",
308 errno, strerror(errno));
309 }
310 if (close(fd) == -1) {
311 tst_brkm(TBROK, cleanup,
312 "close(t_file) Failed, errno=%d : %s",
313 errno, strerror(errno));
314 }
315 return 0;
316}
317
318/*
319 * longpath_setup() - setup to create a node with a name length exceeding
320 * the MAX. length of PATH_MAX.
321 * This function retruns 0.
322 */
323int
324longpath_setup()
325{
326 int ind; /* counter variable */
327
328 for (ind = 0; ind <= (PATH_MAX + 1); ind++) {
329 Longpathname[ind] = 'a';
330 }
331 return 0;
332}
333
334/*
335 * cleanup() - Performs all ONE TIME cleanup for this test at
336 * completion or premature exit.
337 */
338void
339cleanup()
340{
341 /*
342 * print timing stats if that option was specified.
343 * print errno log if that option was specified.
344 */
345 TEST_CLEANUP;
346
347 /* Restore mode permissions on test directory created in setup2() */
348 if (chmod(DIR_TEMP, MODE_RWX) < 0) {
349 tst_brkm(TBROK, NULL, "chmod(2) of %s failed", DIR_TEMP);
350 }
351
352 /* Remove files and temporary directory created */
353 tst_rmdir();
354
355 /* exit with return code appropriate for results */
356 tst_exit();
357}