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 <fcntl.h>
     74 #include <errno.h>
     75 #include <string.h>
     76 #include <signal.h>
     77 
     78 #include "test.h"
     79 #include "safe_macros.h"
     80 
     81 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
     82 #define TESTFILE	"testfile"
     83 
     84 char *TCID = "chmod01";
     85 int TST_TOTAL = 8;
     86 
     87 int modes[] = { 0, 07, 070, 0700, 0777, 02777, 04777, 06777 };
     88 
     89 void setup();
     90 void cleanup();
     91 
     92 int main(int ac, char **av)
     93 {
     94 	struct stat stat_buf;
     95 	int lc;
     96 	int i;
     97 	int mode;
     98 
     99 	TST_TOTAL = sizeof(modes) / sizeof(int);
    100 
    101 	tst_parse_opts(ac, av, NULL, NULL);
    102 
    103 	setup();
    104 
    105 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    106 
    107 		tst_count = 0;
    108 
    109 		for (i = 0; i < TST_TOTAL; i++) {
    110 			mode = modes[i];
    111 
    112 			TEST(chmod(TESTFILE, mode));
    113 
    114 			if (TEST_RETURN == -1) {
    115 				tst_resm(TFAIL | TTERRNO,
    116 					 "chmod(%s, %#o) failed", TESTFILE,
    117 					 mode);
    118 				continue;
    119 			}
    120 			if (stat(TESTFILE, &stat_buf) < 0)
    121 				tst_brkm(TFAIL | TERRNO, cleanup,
    122 					 "stat(%s) failed", TESTFILE);
    123 			stat_buf.st_mode &= ~S_IFREG;
    124 
    125 			if (stat_buf.st_mode == mode)
    126 				tst_resm(TPASS, "Functionality of "
    127 					 "chmod(%s, %#o) successful",
    128 					 TESTFILE, mode);
    129 			else
    130 				tst_resm(TFAIL, "%s: Incorrect "
    131 					 "modes 0%03o, Expected 0%03o",
    132 					 TESTFILE, stat_buf.st_mode,
    133 					 mode);
    134 		}
    135 	}
    136 
    137 	cleanup();
    138 	tst_exit();
    139 }
    140 
    141 void setup(void)
    142 {
    143 	int fd;
    144 
    145 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    146 
    147 	TEST_PAUSE;
    148 
    149 	tst_tmpdir();
    150 
    151 	fd = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
    152 	SAFE_CLOSE(cleanup, fd);
    153 
    154 }
    155 
    156 void cleanup(void)
    157 {
    158 	tst_rmdir();
    159 }
    160