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: unlink07.c,v 1.8 2009/11/02 13:57:19 subrata_modak Exp $ */
34 /**********************************************************
35  *
36  *    OS Test - Silicon Graphics, Inc.
37  *
38  *    TEST IDENTIFIER	: unlink07
39  *
40  *    EXECUTED BY	: anyone
41  *
42  *    TEST TITLE	: unlink(2) negative testcases
43  *
44  *    PARENT DOCUMENT	: usctpl01
45  *
46  *    TEST CASE TOTAL	: 1
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-8) 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 <sys/mman.h>
116 #include <errno.h>
117 #include <string.h>
118 #include <signal.h>
119 #include <unistd.h>
120 #include <sys/param.h>		/* for PATH_MAX */
121 #include "test.h"
122 
123 void setup();
124 void cleanup();
125 
126 extern char *get_high_address();
127 
128 char *TCID = "unlink07";
129 int TST_TOTAL = 6;
130 
131 char *bad_addr = 0;
132 
133 int longpath_setup();
134 int no_setup();
135 int filepath_setup();
136 char Longpathname[PATH_MAX + 2];
137 char High_address[64];
138 
139 struct test_case_t {
140 	char *pathname;
141 	char *desc;
142 	int exp_errno;
143 	int (*setupfunc) ();
144 } Test_cases[] = {
145 	{
146 	"nonexistfile", "non-existent file", ENOENT, no_setup}, {
147 	"", "path is empty string", ENOENT, no_setup}, {
148 	"nefile/file", "path contains a non-existent file",
149 		    ENOENT, no_setup},
150 #if !defined(UCLINUX)
151 	{
152 	High_address, "address beyond address space", EFAULT, no_setup},
153 #endif
154 	{
155 	"file/file", "path contains a regular file",
156 		    ENOTDIR, filepath_setup},
157 #if !defined(UCLINUX)
158 	{
159 	High_address, "address beyond address space", EFAULT, no_setup},
160 #endif
161 	{
162 	Longpathname, "pathname too long", ENAMETOOLONG, longpath_setup}, {
163 	(char *)-1, "negative address", EFAULT, no_setup}, {
164 	NULL, NULL, 0, no_setup}
165 };
166 
167 /***********************************************************************
168  * Main
169  ***********************************************************************/
main(int ac,char ** av)170 int main(int ac, char **av)
171 {
172 	int lc;
173 	char *fname;
174 	char *desc;
175 	int ind;
176 
177     /***************************************************************
178      * parse standard options
179      ***************************************************************/
180 	tst_parse_opts(ac, av, NULL, NULL);
181 
182     /***************************************************************
183      * perform global setup for test
184      ***************************************************************/
185 	setup();
186 
187     /***************************************************************
188      * check looping state if -c option given
189      ***************************************************************/
190 	for (lc = 0; TEST_LOOPING(lc); lc++) {
191 
192 		tst_count = 0;
193 
194 		for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
195 
196 			fname = Test_cases[ind].pathname;
197 			desc = Test_cases[ind].desc;
198 
199 #if !defined(UCLINUX)
200 			if (fname == High_address)
201 				fname = get_high_address();
202 #endif
203 			/*
204 			 *  Call unlink(2)
205 			 */
206 			TEST(unlink(fname));
207 
208 			/* check return code */
209 			if (TEST_RETURN == -1) {
210 				if (TEST_ERRNO ==
211 				    Test_cases[ind].exp_errno)
212 					tst_resm(TPASS,
213 						 "unlink(<%s>) Failed, errno=%d",
214 						 desc, TEST_ERRNO);
215 				else
216 					tst_resm(TFAIL,
217 						 "unlink(<%s>) Failed, errno=%d, expected errno:%d",
218 						 desc, TEST_ERRNO,
219 						 Test_cases
220 						 [ind].exp_errno);
221 			} else {
222 				tst_resm(TFAIL,
223 					 "unlink(<%s>) returned %ld, expected -1, errno:%d",
224 					 desc, TEST_RETURN,
225 					 Test_cases[ind].exp_errno);
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 
242 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
243 
244 	TEST_PAUSE;
245 
246 	tst_tmpdir();
247 
248 	bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
249 	if (bad_addr == MAP_FAILED) {
250 		tst_brkm(TBROK, cleanup, "mmap failed");
251 	}
252 	Test_cases[7].pathname = bad_addr;
253 
254 	for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
255 		Test_cases[ind].setupfunc();
256 	}
257 
258 }
259 
260 /***************************************************************
261  * cleanup() - performs all ONE TIME cleanup for this test at
262  *		completion or premature exit.
263  ***************************************************************/
cleanup(void)264 void cleanup(void)
265 {
266 	chmod("unwrite_dir", 0777);
267 	chmod("unsearch_dir", 0777);
268 
269 	tst_rmdir();
270 
271 }
272 
273 /******************************************************************
274  *
275  ******************************************************************/
no_setup(void)276 int no_setup(void)
277 {
278 	return 0;
279 }
280 
281 /******************************************************************
282  *
283  ******************************************************************/
longpath_setup(void)284 int longpath_setup(void)
285 {
286 	int ind;
287 
288 	for (ind = 0; ind <= PATH_MAX + 1; ind++) {
289 		Longpathname[ind] = 'a';
290 	}
291 	return 0;
292 
293 }
294 
295 /******************************************************************
296  *
297  ******************************************************************/
filepath_setup(void)298 int filepath_setup(void)
299 {
300 	int fd;
301 
302 	if ((fd = creat("file", 0777)) == -1) {
303 		tst_brkm(TBROK, cleanup, "creat(file) failed, errno:%d %s",
304 			 errno, strerror(errno));
305 	}
306 	close(fd);
307 	return 0;
308 }
309