blob: 49efb3dfd7e9c33ade48692f817c8bac72afa925 [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
nstraz64d7fe62001-09-26 21:34:54 +000022 * chdir04.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Testcase to test whether chdir(2) sets errno correctly.
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
plars865695b2001-08-27 22:15:12 +000027 * ALGORITHM
28 * 1. Test for ENAMETOOLONG:
29 * Create a bad directory name with length more than
30 *
31 * VFS_MAXNAMELEN (Linux kernel variable), and attempt to
32 * chdir(2) to it.
33 *
34 * 2. Test for ENOENT:
35 * Attempt to chdir(2) on a non-existent directory
36 *
37 * 3. Test for EFAULT:
38 * Pass an address which lies outside the address space of the
39 * process, and expect an EFAULT.
40 *
41 * USAGE: <for command-line>
nstraz64d7fe62001-09-26 21:34:54 +000042 * chdir04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000043 * where, -c n : Run n copies concurrently.
44 * -e : Turn on errno logging.
45 * -i n : Execute test n times.
46 * -I x : Execute test for x seconds.
47 * -P x : Pause for x seconds between iterations.
48 * -t : Turn on syscall timing.
49 *
50 * HISTORY
51 * 07/2001 Ported by Wayne Boyer
52 *
53 * RESTRICTIONS
54 * NONE
55 */
56
57#include <stdio.h>
58#include <errno.h>
59#include <sys/stat.h>
plars1ad84512002-07-23 13:11:18 +000060#include <sys/mman.h>
Garrett Cooper71ff32c2010-12-17 11:30:53 -080061#include "test.h"
plars865695b2001-08-27 22:15:12 +000062
nstraz64d7fe62001-09-26 21:34:54 +000063char *TCID = "chdir04";
plars865695b2001-08-27 22:15:12 +000064
subrata_modak56207ce2009-03-23 13:35:39 +000065char bad_dir[] =
66 "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz";
plars865695b2001-08-27 22:15:12 +000067
68char noexist_dir[] = "/tmp/noexistdir";
69
70struct test_case_t {
71 char *dname;
72 int error;
73} TC[] = {
74 /*
subrata_modak56207ce2009-03-23 13:35:39 +000075 * to test whether chdir() is setting ENAMETOOLONG if the
plars865695b2001-08-27 22:15:12 +000076 * directory is more than VFS_MAXNAMELEN
plars865695b2001-08-27 22:15:12 +000077 */
Wanlong Gao354ebb42012-12-07 10:10:04 +080078 {
79 bad_dir, ENAMETOOLONG},
subrata_modak56207ce2009-03-23 13:35:39 +000080 /*
81 * to test whether chdir() is setting ENOENT if the
82 * directory is not existing.
83 */
Wanlong Gao354ebb42012-12-07 10:10:04 +080084 {
85 noexist_dir, ENOENT},
Garrett Cooper71ff32c2010-12-17 11:30:53 -080086#ifndef UCLINUX
subrata_modak56207ce2009-03-23 13:35:39 +000087 /*
88 * to test whether chdir() is setting EFAULT if the
89 * directory is an invalid address.
90 */
91 {
92 (void *)-1, EFAULT}
vapierb5ed1f62006-08-24 04:16:32 +000093#endif
plars865695b2001-08-27 22:15:12 +000094};
95
Cyril Hrubisb863a0b2014-09-24 13:15:29 +020096int TST_TOTAL = ARRAY_SIZE(TC);
vapierb5ed1f62006-08-24 04:16:32 +000097
plars865695b2001-08-27 22:15:12 +000098int flag;
99#define FAILED 1
100
101void setup(void);
102void cleanup(void);
103
subrata_modak56207ce2009-03-23 13:35:39 +0000104char *bad_addr = 0;
plars1ad84512002-07-23 13:11:18 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000107{
Garrett Cooper71ff32c2010-12-17 11:30:53 -0800108 int lc;
plars865695b2001-08-27 22:15:12 +0000109 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200110 const char *msg;
plars865695b2001-08-27 22:15:12 +0000111
Garrett Cooper71ff32c2010-12-17 11:30:53 -0800112 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800113 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000114
115 setup();
116
plars865695b2001-08-27 22:15:12 +0000117 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800118 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000119
subrata_modak56207ce2009-03-23 13:35:39 +0000120 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000121
122 TEST(chdir(TC[i].dname));
123
124 if (TEST_RETURN != -1) {
125 tst_resm(TFAIL, "call succeeded unexpectedly");
126 continue;
127 }
128
Garrett Cooper71ff32c2010-12-17 11:30:53 -0800129 if (TEST_ERRNO == TC[i].error)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800130 tst_resm(TPASS | TTERRNO, "failed as expected");
Garrett Cooper71ff32c2010-12-17 11:30:53 -0800131 else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_resm(TFAIL | TTERRNO,
133 "didn't fail as expected (expected %d)",
134 TC[i].error);
plars865695b2001-08-27 22:15:12 +0000135 }
136 }
137 }
138 cleanup();
139
Garrett Cooper71ff32c2010-12-17 11:30:53 -0800140 tst_exit();
plars865695b2001-08-27 22:15:12 +0000141
Garrett Cooper71ff32c2010-12-17 11:30:53 -0800142}
143
Mike Frysingerc57fba52014-04-09 18:56:30 -0400144void setup(void)
plars865695b2001-08-27 22:15:12 +0000145{
Garrett Cooper2c282152010-12-16 00:55:50 -0800146
plars865695b2001-08-27 22:15:12 +0000147 tst_sig(NOFORK, DEF_HANDLER, cleanup);
148
plars865695b2001-08-27 22:15:12 +0000149 TEST_PAUSE;
150
plars865695b2001-08-27 22:15:12 +0000151 tst_tmpdir();
plars1ad84512002-07-23 13:11:18 +0000152
Garrett Cooper71ff32c2010-12-17 11:30:53 -0800153#ifdef UCLINUX
robbiewd34d5812005-07-11 22:28:09 +0000154 bad_addr = mmap(0, 1, PROT_NONE,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
Garrett Cooper71ff32c2010-12-17 11:30:53 -0800156 if (bad_addr == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 tst_brkm(TBROK | TERRNO, cleanup, "mmap() failed");
plars1ad84512002-07-23 13:11:18 +0000158 TC[2].dname = bad_addr;
vapierb5ed1f62006-08-24 04:16:32 +0000159#endif
plars865695b2001-08-27 22:15:12 +0000160}
161
Mike Frysingerc57fba52014-04-09 18:56:30 -0400162void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000163{
plars865695b2001-08-27 22:15:12 +0000164 tst_rmdir();
165
Chris Dearmanec6edca2012-10-17 19:54:01 -0700166}