1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31  *
32  */
33 /* $Id: unlink08.c,v 1.5 2009/11/02 13:57:19 subrata_modak Exp $ */
34 /**********************************************************
35  *
36  *    OS Test - Silicon Graphics, Inc.
37  *
38  *    TEST IDENTIFIER	: unlink08
39  *
40  *    EXECUTED BY	: anyone
41  *
42  *    TEST TITLE	: unlink(2) negative testcases
43  *
44  *    PARENT DOCUMENT	: usctpl01
45  *
46  *    TEST CASE TOTAL	: 3
47  *
48  *    WALL CLOCK TIME	: 1
49  *
50  *    CPU TYPES		: ALL
51  *
52  *    AUTHOR		: Richard Logan
53  *
54  *    CO-PILOT		: William Roske
55  *
56  *    DATE STARTED	: 03/30/94
57  *
58  *    INITIAL RELEASE	: UNICOS 7.0
59  *
60  *    TEST CASES
61  *
62  * 	1-3) See Testcases structure below.
63  *
64  *    INPUT SPECIFICATIONS
65  * 	The standard options for system call tests are accepted.
66  *	(See the parse_opts(3) man page).
67  *
68  *    OUTPUT SPECIFICATIONS
69  *$
70  *    DURATION
71  * 	Terminates - with frequency and infinite modes.
72  *
73  *    SIGNALS
74  * 	Uses SIGUSR1 to pause before test if option set.
75  * 	(See the parse_opts(3) man page).
76  *
77  *    RESOURCES
78  * 	None
79  *
80  *    ENVIRONMENTAL NEEDS
81  *      No run-time environmental needs.
82  *
83  *    SPECIAL PROCEDURAL REQUIREMENTS
84  * 	None
85  *
86  *    INTERCASE DEPENDENCIES
87  * 	None
88  *
89  *    DETAILED DESCRIPTION
90  *	This is a Phase I test for the unlink(2) system call.  It is intended
91  *	to provide a limited exposure of the system call, for now.  It
92  *	should/will be extended when full functional tests are written for
93  *	unlink(2).
94  *
95  * 	Setup:
96  * 	  Setup signal handling.
97  *	  Pause for SIGUSR1 if option specified.
98  *
99  * 	Test:
100  *	 Loop if the proper options are given.
101  * 	  Execute system call
102  *	  Check return code, if system call failed (return=-1)
103  *		Log the errno and Issue a FAIL message.
104  *	  Otherwise, Issue a PASS message.
105  *
106  * 	Cleanup:
107  * 	  Print errno log and/or timing stats if options given
108  *
109  *
110  *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
111 
112 #include <sys/types.h>
113 #include <fcntl.h>
114 #include <sys/stat.h>
115 #include <errno.h>
116 #include <signal.h>
117 #include <unistd.h>
118 #include <string.h>
119 #include "test.h"
120 #include "safe_macros.h"
121 
122 void setup(void);
123 void cleanup(void);
124 
125 char *TCID = "unlink08";
126 int TST_TOTAL = 3;
127 
128 int unwrite_dir_setup(int flag);
129 int unsearch_dir_setup(int flag);
130 int dir_setup(int flag);
131 int no_setup(int flag);
132 
133 struct test_case_t {
134 	char *pathname;
135 	char *desc;
136 	int (*setupfunc) (int flag);
137 	int exp_ret;		/* -1 means error, 0 means != -1 */
138 	int exp_errno;
139 } Test_cases[] = {
140 	{
141 	"unwrite_dir/file", "unwritable directory", unwrite_dir_setup,
142 		    -1, EACCES}, {
143 	"unsearch_dir/file", "unsearchable directory",
144 		    unsearch_dir_setup, -1, EACCES},
145 #ifdef linux
146 	{
147 	"regdir", "directory", dir_setup, -1, EISDIR},
148 #else
149 	{
150 	"regdir", "directory", dir_setup, -1, EPERM},
151 #endif
152 	{
153 	NULL, NULL, no_setup, -1, 0}
154 };
155 
156 /***********************************************************************
157  * Main
158  ***********************************************************************/
main(int ac,char ** av)159 int main(int ac, char **av)
160 {
161 	int lc;
162 	char *fname;
163 	char *desc;
164 	int ind;
165 
166     /***************************************************************
167      * parse standard options
168      ***************************************************************/
169 	tst_parse_opts(ac, av, NULL, NULL);
170 
171     /***************************************************************
172      * perform global setup for test
173      ***************************************************************/
174 	setup();
175 
176     /***************************************************************
177      * check looping state if -c option given
178      ***************************************************************/
179 	for (lc = 0; TEST_LOOPING(lc); lc++) {
180 
181 		tst_count = 0;
182 
183 		for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
184 
185 			fname = Test_cases[ind].pathname;
186 			desc = Test_cases[ind].desc;
187 
188 			/*
189 			 *  Call unlink(2)
190 			 */
191 			TEST(unlink(fname));
192 
193 			/* check return code */
194 			if (TEST_RETURN == -1) {
195 				if (Test_cases[ind].exp_ret == -1) {	/* neg test */
196 					if (TEST_ERRNO ==
197 					    Test_cases[ind].exp_errno)
198 						tst_resm(TPASS,
199 							 "unlink(<%s>) Failed, errno=%d",
200 							 desc,
201 							 TEST_ERRNO);
202 					else
203 						tst_resm(TFAIL,
204 							 "unlink(<%s>) Failed, errno=%d, expected errno:%d",
205 							 desc,
206 							 TEST_ERRNO,
207 							 Test_cases
208 							 [ind].exp_errno);
209 				} else {
210 					tst_resm(TFAIL,
211 						 "unlink(<%s>) Failed, errno=%d",
212 						 desc, TEST_ERRNO);
213 				}
214 			} else {
215 				if (Test_cases[ind].exp_ret == -1) {
216 					tst_resm(TFAIL,
217 						 "unlink(<%s>) returned %ld, expected -1, errno:%d",
218 						 desc, TEST_RETURN,
219 						 Test_cases[ind].exp_errno);
220 				} else {
221 					tst_resm(TPASS,
222 						 "unlink(<%s>) returned %ld",
223 						 desc, TEST_RETURN);
224 				}
225 				Test_cases[ind].setupfunc(1);
226 			}
227 		}
228 
229 	}
230 
231 	cleanup();
232 	tst_exit();
233 }
234 
235 /***************************************************************
236  * setup() - performs all ONE TIME setup for this test.
237  ***************************************************************/
setup(void)238 void setup(void)
239 {
240 	int ind;
241 	int postest = 0;
242 
243 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
244 
245 	TEST_PAUSE;
246 
247 	tst_tmpdir();
248 
249 	if (geteuid() == 0) {
250 		postest++;
251 	}
252 
253 	for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
254 		if (Test_cases[ind].exp_errno == EACCES && postest)
255 			Test_cases[ind].exp_ret = 0;	/* set as a pos test */
256 		Test_cases[ind].setupfunc(0);
257 	}
258 
259 }
260 
261 /***************************************************************
262  * cleanup() - performs all ONE TIME cleanup for this test at
263  *		completion or premature exit.
264  ***************************************************************/
cleanup(void)265 void cleanup(void)
266 {
267 	chmod("unwrite_dir", 0777);
268 	chmod("unsearch_dir", 0777);
269 
270 	tst_rmdir();
271 
272 }
273 
274 /******************************************************************
275  *
276  ******************************************************************/
unwrite_dir_setup(int flag)277 int unwrite_dir_setup(int flag)
278 {
279 	int fd;
280 
281 	if (!flag) {		/* initial setup */
282 		SAFE_MKDIR(cleanup, "unwrite_dir", 0777);
283 
284 		if ((fd = creat("unwrite_dir/file", 0777)) == -1) {
285 			tst_brkm(TBROK, cleanup,
286 				 "creat(unwrite_dir/file, 0777) failed, errno:%d %s",
287 				 errno, strerror(errno));
288 		}
289 		close(fd);
290 
291 		SAFE_CHMOD(cleanup, "unwrite_dir", 0555);
292 	} else {		/* resetup */
293 		SAFE_CHMOD(cleanup, "unwrite_dir", 0777);
294 
295 		if ((fd = creat("unwrite_dir/file", 0777)) == -1) {
296 			tst_brkm(TBROK, cleanup,
297 				 "creat(unwrite_dir/file, 0777) failed, errno:%d %s",
298 				 errno, strerror(errno));
299 		}
300 		close(fd);
301 
302 		SAFE_CHMOD(cleanup, "unwrite_dir", 0555);
303 	}
304 	return 0;
305 }
306 
307 /******************************************************************
308  *
309  ******************************************************************/
unsearch_dir_setup(int flag)310 int unsearch_dir_setup(int flag)
311 {
312 	int fd;
313 
314 	if (!flag) {		/* initial setup */
315 		SAFE_MKDIR(cleanup, "unsearch_dir", 0777);
316 
317 		if ((fd = creat("unsearch_dir/file", 0777)) == -1) {
318 			tst_brkm(TBROK, cleanup,
319 				 "creat(unsearch_dir/file, 0777) failed, errno:%d %s",
320 				 errno, strerror(errno));
321 		}
322 		close(fd);
323 
324 		SAFE_CHMOD(cleanup, "unsearch_dir", 0666);
325 	} else {		/* resetup */
326 		SAFE_CHMOD(cleanup, "unsearch_dir", 0777);
327 
328 		if ((fd = creat("unsearch_dir/file", 0777)) == -1) {
329 			tst_brkm(TBROK, cleanup,
330 				 "creat(unsearch_dir/file, 0777) failed, errno:%d %s",
331 				 errno, strerror(errno));
332 		}
333 		close(fd);
334 
335 		SAFE_CHMOD(cleanup, "unsearch_dir", 0666);
336 	}
337 	return 0;
338 }
339 
340 /******************************************************************
341  *
342  ******************************************************************/
dir_setup(int flag)343 int dir_setup(int flag)
344 {
345 	SAFE_MKDIR(cleanup, "regdir", 0777);
346 	return 0;
347 }
348 
349 /******************************************************************
350  *
351  ******************************************************************/
no_setup(int flag)352 int no_setup(int flag)
353 {
354 	return 0;
355 }
356