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 <sys/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 121 void setup(void); 122 void cleanup(void); 123 124 char *TCID = "unlink08"; 125 int TST_TOTAL = 3; 126 127 int unwrite_dir_setup(int flag); 128 int unsearch_dir_setup(int flag); 129 int dir_setup(int flag); 130 int no_setup(int flag); 131 132 struct test_case_t { 133 char *pathname; 134 char *desc; 135 int (*setupfunc) (int flag); 136 int exp_ret; /* -1 means error, 0 means != -1 */ 137 int exp_errno; 138 } Test_cases[] = { 139 { 140 "unwrite_dir/file", "unwritable directory", unwrite_dir_setup, 141 -1, EACCES}, { 142 "unsearch_dir/file", "unsearchable directory", 143 unsearch_dir_setup, -1, EACCES}, 144 #ifdef linux 145 { 146 "regdir", "directory", dir_setup, -1, EISDIR}, 147 #else 148 { 149 "regdir", "directory", dir_setup, -1, EPERM}, 150 #endif 151 { 152 NULL, NULL, no_setup, -1, 0} 153 }; 154 155 /*********************************************************************** 156 * Main 157 ***********************************************************************/ 158 int main(int ac, char **av) 159 { 160 int lc; 161 char *fname; 162 char *desc; 163 int ind; 164 165 /*************************************************************** 166 * parse standard options 167 ***************************************************************/ 168 tst_parse_opts(ac, av, NULL, NULL); 169 170 /*************************************************************** 171 * perform global setup for test 172 ***************************************************************/ 173 setup(); 174 175 /*************************************************************** 176 * check looping state if -c option given 177 ***************************************************************/ 178 for (lc = 0; TEST_LOOPING(lc); lc++) { 179 180 tst_count = 0; 181 182 for (ind = 0; Test_cases[ind].desc != NULL; ind++) { 183 184 fname = Test_cases[ind].pathname; 185 desc = Test_cases[ind].desc; 186 187 /* 188 * Call unlink(2) 189 */ 190 TEST(unlink(fname)); 191 192 /* check return code */ 193 if (TEST_RETURN == -1) { 194 if (Test_cases[ind].exp_ret == -1) { /* neg test */ 195 if (TEST_ERRNO == 196 Test_cases[ind].exp_errno) 197 tst_resm(TPASS, 198 "unlink(<%s>) Failed, errno=%d", 199 desc, 200 TEST_ERRNO); 201 else 202 tst_resm(TFAIL, 203 "unlink(<%s>) Failed, errno=%d, expected errno:%d", 204 desc, 205 TEST_ERRNO, 206 Test_cases 207 [ind].exp_errno); 208 } else { 209 tst_resm(TFAIL, 210 "unlink(<%s>) Failed, errno=%d", 211 desc, TEST_ERRNO); 212 } 213 } else { 214 if (Test_cases[ind].exp_ret == -1) { 215 tst_resm(TFAIL, 216 "unlink(<%s>) returned %ld, expected -1, errno:%d", 217 desc, TEST_RETURN, 218 Test_cases[ind].exp_errno); 219 } else { 220 tst_resm(TPASS, 221 "unlink(<%s>) returned %ld", 222 desc, TEST_RETURN); 223 } 224 Test_cases[ind].setupfunc(1); 225 } 226 } 227 228 } 229 230 cleanup(); 231 tst_exit(); 232 } 233 234 /*************************************************************** 235 * setup() - performs all ONE TIME setup for this test. 236 ***************************************************************/ 237 void setup(void) 238 { 239 int ind; 240 int postest = 0; 241 242 tst_sig(NOFORK, DEF_HANDLER, cleanup); 243 244 TEST_PAUSE; 245 246 tst_tmpdir(); 247 248 if (geteuid() == 0) { 249 postest++; 250 } 251 252 for (ind = 0; Test_cases[ind].desc != NULL; ind++) { 253 if (Test_cases[ind].exp_errno == EACCES && postest) 254 Test_cases[ind].exp_ret = 0; /* set as a pos test */ 255 Test_cases[ind].setupfunc(0); 256 } 257 258 } 259 260 /*************************************************************** 261 * cleanup() - performs all ONE TIME cleanup for this test at 262 * completion or premature exit. 263 ***************************************************************/ 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 ******************************************************************/ 276 int unwrite_dir_setup(int flag) 277 { 278 int fd; 279 280 if (!flag) { /* initial setup */ 281 if (mkdir("unwrite_dir", 0777) == -1) { 282 tst_brkm(TBROK, cleanup, 283 "mkdir(unwrite_dir, 0777) failed, errno:%d %s", 284 errno, strerror(errno)); 285 } 286 287 if ((fd = creat("unwrite_dir/file", 0777)) == -1) { 288 tst_brkm(TBROK, cleanup, 289 "creat(unwrite_dir/file, 0777) failed, errno:%d %s", 290 errno, strerror(errno)); 291 } 292 close(fd); 293 294 if (chmod("unwrite_dir", 0555) == -1) { 295 tst_brkm(TBROK, cleanup, 296 "chmod(unwrite_dir, 0555) failed, errno:%d %s", 297 errno, strerror(errno)); 298 } 299 } else { /* resetup */ 300 if (chmod("unwrite_dir", 0777) == -1) { 301 tst_brkm(TBROK, cleanup, 302 "chmod(unwrite_dir, 0777) failed, errno:%d %s", 303 errno, strerror(errno)); 304 } 305 306 if ((fd = creat("unwrite_dir/file", 0777)) == -1) { 307 tst_brkm(TBROK, cleanup, 308 "creat(unwrite_dir/file, 0777) failed, errno:%d %s", 309 errno, strerror(errno)); 310 } 311 close(fd); 312 313 if (chmod("unwrite_dir", 0555) == -1) { 314 tst_brkm(TBROK, cleanup, 315 "chmod(unwrite_dir, 0555) failed, errno:%d %s", 316 errno, strerror(errno)); 317 } 318 } 319 return 0; 320 } 321 322 /****************************************************************** 323 * 324 ******************************************************************/ 325 int unsearch_dir_setup(int flag) 326 { 327 int fd; 328 329 if (!flag) { /* initial setup */ 330 if (mkdir("unsearch_dir", 0777) == -1) { 331 tst_brkm(TBROK, cleanup, 332 "mkdir(unsearch_dir, 0777) failed, errno:%d %s", 333 errno, strerror(errno)); 334 } 335 336 if ((fd = creat("unsearch_dir/file", 0777)) == -1) { 337 tst_brkm(TBROK, cleanup, 338 "creat(unsearch_dir/file, 0777) failed, errno:%d %s", 339 errno, strerror(errno)); 340 } 341 close(fd); 342 343 if (chmod("unsearch_dir", 0666) == -1) { 344 tst_brkm(TBROK, cleanup, 345 "chmod(unsearch_dir, 0666) failed, errno:%d %s", 346 errno, strerror(errno)); 347 } 348 } else { /* resetup */ 349 if (chmod("unsearch_dir", 0777) == -1) { 350 tst_brkm(TBROK, cleanup, 351 "chmod(unsearch_dir, 0777) failed, errno:%d %s", 352 errno, strerror(errno)); 353 } 354 355 if ((fd = creat("unsearch_dir/file", 0777)) == -1) { 356 tst_brkm(TBROK, cleanup, 357 "creat(unsearch_dir/file, 0777) failed, errno:%d %s", 358 errno, strerror(errno)); 359 } 360 close(fd); 361 362 if (chmod("unsearch_dir", 0666) == -1) { 363 tst_brkm(TBROK, cleanup, 364 "chmod(unsearch_dir, 0666) failed, errno:%d %s", 365 errno, strerror(errno)); 366 } 367 } 368 return 0; 369 } 370 371 /****************************************************************** 372 * 373 ******************************************************************/ 374 int dir_setup(int flag) 375 { 376 if (mkdir("regdir", 0777) == -1) { 377 tst_brkm(TBROK, cleanup, 378 "mkdir(unwrite_dir, 0777) failed, errno:%d %s", 379 errno, strerror(errno)); 380 } 381 return 0; 382 } 383 384 /****************************************************************** 385 * 386 ******************************************************************/ 387 int no_setup(int flag) 388 { 389 return 0; 390 } 391