1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 John George
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 Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 /*
20 * Test Description:
21 * Verify that,
22 * 1) truncate(2) returns -1 and sets errno to EACCES if search/write
23 * permission denied for the process on the component of the path prefix
24 * or named file.
25 * 2) truncate(2) returns -1 and sets errno to ENOTDIR if the component of
26 * the path prefix is not a directory.
27 * 3) truncate(2) returns -1 and sets errno to EFAULT if pathname points
28 * outside user's accessible address space.
29 * 4) truncate(2) returns -1 and sets errno to ENAMETOOLONG if the component
30 * of a pathname exceeded 255 characters or entire pathname exceeds 1023
31 * characters.
32 * 5) truncate(2) returns -1 and sets errno to ENOENT if the named file
33 * does not exist.
34 * 6) truncate(2) returns -1 and sets errno to EISDIR if the named file
35 * is a directory.
36 * 7) truncate(2) returns -1 and sets errno to EFBIG if the argument length
37 * is larger than the maximum file size.
38 * 8) truncate(2) returns -1 and sets errno to ELOOP if too many symbolic
39 * links were encountered in translating the pathname.
40 */
41
42 #define _GNU_SOURCE
43
44 #include <stdio.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include <sys/mman.h>
49 #include <errno.h>
50 #include <string.h>
51 #include <signal.h>
52 #include <pwd.h>
53 #include <sys/resource.h>
54
55 #include "test.h"
56 #include "safe_macros.h"
57
58 #define TEST_FILE1 "testfile"
59 #define TEST_FILE2 "t_file/testfile"
60 #define TEST_FILE3 "testfile3"
61 #define TEST_SYM1 "testsymlink1"
62 #define TEST_SYM2 "testsymlink2"
63 #define TEST_DIR1 "testdir"
64 #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
65 #define NEW_MODE S_IRUSR | S_IRGRP | S_IROTH
66 #define DIR_MODE S_IRWXU
67 #define TRUNC_LEN 256
68 #define MAX_FSIZE (16*1024*1024)
69
70 static char long_pathname[PATH_MAX + 2];
71
72 static struct test_case_t {
73 char *pathname;
74 off_t length;
75 int exp_errno;
76 } test_cases[] = {
77 { TEST_FILE1, TRUNC_LEN, EACCES },
78 { TEST_FILE2, TRUNC_LEN, ENOTDIR },
79 { NULL, TRUNC_LEN, EFAULT },
80 { long_pathname, TRUNC_LEN, ENAMETOOLONG },
81 { "", TRUNC_LEN, ENOENT },
82 { TEST_DIR1, TRUNC_LEN, EISDIR },
83 { TEST_FILE3, MAX_FSIZE*2, EFBIG },
84 { TEST_SYM1, TRUNC_LEN, ELOOP }
85 };
86
87 static void setup(void);
88 static void cleanup(void);
89 static void truncate_verify(struct test_case_t *);
90
91 char *TCID = "truncate03";
92 int TST_TOTAL = ARRAY_SIZE(test_cases);
93
main(int ac,char ** av)94 int main(int ac, char **av)
95 {
96 int i, lc;
97
98 tst_parse_opts(ac, av, NULL, NULL);
99
100 setup();
101
102 for (lc = 0; TEST_LOOPING(lc); lc++) {
103 tst_count = 0;
104
105 for (i = 0; i < TST_TOTAL; i++)
106 truncate_verify(&test_cases[i]);
107
108 }
109
110 cleanup();
111 tst_exit();
112 }
113
setup(void)114 void setup(void)
115 {
116 struct passwd *ltpuser;
117 struct rlimit rlim;
118 sigset_t signalset;
119 int n;
120
121 tst_sig(NOFORK, DEF_HANDLER, cleanup);
122
123 tst_require_root();
124
125 ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
126 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
127
128 TEST_PAUSE;
129
130 tst_tmpdir();
131
132 SAFE_TOUCH(cleanup, TEST_FILE1, NEW_MODE, NULL);
133
134 SAFE_TOUCH(cleanup, "t_file", FILE_MODE, NULL);
135
136 memset(long_pathname, 'a', PATH_MAX + 1);
137
138 SAFE_MKDIR(cleanup, TEST_DIR1, DIR_MODE);
139
140 SAFE_TOUCH(cleanup, TEST_FILE3, FILE_MODE, NULL);
141
142 SAFE_SYMLINK(cleanup, TEST_SYM1, TEST_SYM2);
143 SAFE_SYMLINK(cleanup, TEST_SYM2, TEST_SYM1);
144
145 rlim.rlim_cur = MAX_FSIZE;
146 rlim.rlim_max = MAX_FSIZE;
147 SAFE_SETRLIMIT(cleanup, RLIMIT_FSIZE, &rlim);
148
149 sigemptyset(&signalset);
150 sigaddset(&signalset, SIGXFSZ);
151 TEST(sigprocmask(SIG_BLOCK, &signalset, NULL));
152 if (TEST_RETURN != 0)
153 tst_brkm(TBROK | TTERRNO, cleanup, "sigprocmask");
154
155 for (n = 0; n < TST_TOTAL; n++) {
156 if (!test_cases[n].pathname)
157 test_cases[n].pathname = tst_get_bad_addr(cleanup);
158 }
159
160 }
161
truncate_verify(struct test_case_t * tc)162 void truncate_verify(struct test_case_t *tc)
163 {
164 TEST(truncate(tc->pathname, tc->length));
165
166 if (TEST_RETURN != -1) {
167 tst_resm(TFAIL, "truncate() returned %ld, "
168 "expected -1, errno:%d", TEST_RETURN,
169 tc->exp_errno);
170 return;
171 }
172
173 if (TEST_ERRNO == tc->exp_errno) {
174 tst_resm(TPASS | TTERRNO, "truncate() failed as expected");
175 } else {
176 tst_resm(TFAIL | TTERRNO,
177 "truncate() failed unexpectedly; expected: %d - %s",
178 tc->exp_errno, strerror(tc->exp_errno));
179 }
180 }
181
cleanup(void)182 void cleanup(void)
183 {
184 tst_rmdir();
185 }
186