Home | History | Annotate | Download | only in chmod
      1 /*
      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
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * Test Name: chmod01
     22  *
     23  * Test Description:
     24  *  Verify that, chmod(2) succeeds when used to change the mode permissions
     25  *  of a file.
     26  *
     27  * Expected Result:
     28  *  chmod(2) should return 0 and the mode permissions set on file should match
     29  *  the specified mode.
     30  *
     31  * Algorithm:
     32  *  Setup:
     33  *   Setup signal handling.
     34  *   Create temporary directory.
     35  *   Pause for SIGUSR1 if option specified.
     36  *
     37  *  Test:
     38  *   Loop if the proper options are given.
     39  *   Execute system call
     40  *   Check return code, if system call failed (return=-1)
     41  *   	Log the errno and Issue a FAIL message.
     42  *   Otherwise,
     43  *   	Verify the Functionality of system call
     44  *      if successful,
     45  *      	Issue Functionality-Pass message.
     46  *      Otherwise,
     47  *		Issue Functionality-Fail message.
     48  *  Cleanup:
     49  *   Print errno log and/or timing stats if options given
     50  *   Delete the temporary directory created.
     51  *
     52  * Usage:  <for command-line>
     53  *  chmod01 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
     54  *     where,  -c n : Run n copies concurrently.
     55  *             -e   : Turn on errno logging.
     56  *             -f   : Turn off functionality Testing.
     57  *	       -i n : Execute test n times.
     58  *	       -I x : Execute test for x seconds.
     59  *	       -P x : Pause for x seconds between iterations.
     60  *	       -t   : Turn on syscall timing.
     61  *
     62  * HISTORY
     63  *	07/2001 Ported by Wayne Boyer
     64  *
     65  * RESTRICTIONS:
     66  *  None.
     67  *
     68  */
     69 
     70 #include <stdio.h>
     71 #include <sys/types.h>
     72 #include <sys/stat.h>
     73 #include <sys/fcntl.h>
     74 #include <errno.h>
     75 #include <string.h>
     76 #include <signal.h>
     77 
     78 #include "test.h"
     79 
     80 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
     81 #define TESTFILE	"testfile"
     82 
     83 char *TCID = "chmod01";
     84 int TST_TOTAL = 8;
     85 
     86 int modes[] = { 0, 07, 070, 0700, 0777, 02777, 04777, 06777 };
     87 
     88 void setup();
     89 void cleanup();
     90 
     91 int main(int ac, char **av)
     92 {
     93 	struct stat stat_buf;
     94 	int lc;
     95 	int i;
     96 	int mode;
     97 
     98 	TST_TOTAL = sizeof(modes) / sizeof(int);
     99 
    100 	tst_parse_opts(ac, av, NULL, NULL);
    101 
    102 	setup();
    103 
    104 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    105 
    106 		tst_count = 0;
    107 
    108 		for (i = 0; i < TST_TOTAL; i++) {
    109 			mode = modes[i];
    110 
    111 			TEST(chmod(TESTFILE, mode));
    112 
    113 			if (TEST_RETURN == -1) {
    114 				tst_resm(TFAIL | TTERRNO,
    115 					 "chmod(%s, %#o) failed", TESTFILE,
    116 					 mode);
    117 				continue;
    118 			}
    119 			if (stat(TESTFILE, &stat_buf) < 0)
    120 				tst_brkm(TFAIL | TERRNO, cleanup,
    121 					 "stat(%s) failed", TESTFILE);
    122 			stat_buf.st_mode &= ~S_IFREG;
    123 
    124 			if (stat_buf.st_mode == mode)
    125 				tst_resm(TPASS, "Functionality of "
    126 					 "chmod(%s, %#o) successful",
    127 					 TESTFILE, mode);
    128 			else
    129 				tst_resm(TFAIL, "%s: Incorrect "
    130 					 "modes 0%03o, Expected 0%03o",
    131 					 TESTFILE, stat_buf.st_mode,
    132 					 mode);
    133 		}
    134 	}
    135 
    136 	cleanup();
    137 	tst_exit();
    138 }
    139 
    140 void setup(void)
    141 {
    142 	int fd;
    143 
    144 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    145 
    146 	TEST_PAUSE;
    147 
    148 	tst_tmpdir();
    149 
    150 	fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
    151 	if (fd == -1)
    152 		tst_brkm(TBROK | TERRNO, cleanup,
    153 			 "open(%s, O_RDWR|O_CREAT, %o) failed",
    154 			 TESTFILE, FILE_MODE);
    155 	if (close(fd) == -1)
    156 		tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", TESTFILE);
    157 
    158 }
    159 
    160 void cleanup(void)
    161 {
    162 	tst_rmdir();
    163 }
    164