Home | History | Annotate | Download | only in chown
      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: chown05
     22  *
     23  * Test Description:
     24  *  Verify that, chown(2) succeeds to change the owner and group of a file
     25  *  specified by path to any numeric owner(uid)/group(gid) values when invoked
     26  *  by super-user.
     27  *
     28  * Expected Result:
     29  *  chown(2) should return 0 and the ownership set on the file should match
     30  *  the numeric values contained in owner and group respectively.
     31  *
     32  * Algorithm:
     33  *  Setup:
     34  *   Setup signal handling.
     35  *   Create temporary directory.
     36  *   Pause for SIGUSR1 if option specified.
     37  *
     38  *  Test:
     39  *   Loop if the proper options are given.
     40  *   Execute system call
     41  *   Check return code, if system call failed (return=-1)
     42  *   	Log the errno and Issue a FAIL message.
     43  *   Otherwise,
     44  *   	Verify the Functionality of system call
     45  *      if successful,
     46  *      	Issue Functionality-Pass message.
     47  *      Otherwise,
     48  *		Issue Functionality-Fail message.
     49  *  Cleanup:
     50  *   Print errno log and/or timing stats if options given
     51  *   Delete the temporary directory created.
     52  *
     53  * Usage:  <for command-line>
     54  *  chown05 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
     55  *     where,  -c n : Run n copies concurrently.
     56  *             -e   : Turn on errno logging.
     57  *             -f   : Turn off functionality Testing.
     58  *	       -i n : Execute test n times.
     59  *	       -I x : Execute test for x seconds.
     60  *	       -P x : Pause for x seconds between iterations.
     61  *	       -t   : Turn on syscall timing.
     62  *
     63  * HISTORY
     64  *	07/2001 Ported by Wayne Boyer
     65  *
     66  * RESTRICTIONS:
     67  *  This test should be run by 'super-user' (root) only.
     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 #include "compat_16.h"
     81 
     82 #define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
     83 #define TESTFILE	"testfile"
     84 
     85 TCID_DEFINE(chown05);
     86 
     87 struct test_case_t {
     88 	uid_t user_id;
     89 	gid_t group_id;
     90 } test_cases[] = {
     91 	{
     92 	700, 701}, {
     93 	702, -1}, {
     94 	703, 701}, {
     95 	-1, 704}, {
     96 703, 705},};
     97 
     98 int TST_TOTAL = ARRAY_SIZE(test_cases);
     99 
    100 void setup();			/* setup function for the test */
    101 void cleanup();			/* cleanup function for the test */
    102 
    103 int main(int ac, char **av)
    104 {
    105 	struct stat stat_buf;	/* stat(2) struct contents */
    106 	int lc;
    107 	int i;
    108 	uid_t user_id;		/* user id of the user set for testfile */
    109 	gid_t group_id;		/* group id of the user set for testfile */
    110 
    111 	tst_parse_opts(ac, av, NULL, NULL);
    112 
    113 	setup();
    114 
    115 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    116 
    117 		tst_count = 0;
    118 
    119 		for (i = 0; i < TST_TOTAL; i++) {
    120 			user_id = test_cases[i].user_id;
    121 			group_id = test_cases[i].group_id;
    122 
    123 			TEST(CHOWN(cleanup, TESTFILE, user_id, group_id));
    124 
    125 			if (TEST_RETURN == -1) {
    126 				tst_resm(TFAIL | TTERRNO, "chown failed");
    127 				continue;
    128 			}
    129 			if (stat(TESTFILE, &stat_buf) == -1)
    130 				tst_brkm(TFAIL, cleanup, "stat failed");
    131 			if (user_id == -1)
    132 				user_id = test_cases[i - 1].user_id;
    133 			if (group_id == -1)
    134 				group_id = test_cases[i - 1].group_id;
    135 
    136 			if (stat_buf.st_uid != user_id ||
    137 			    stat_buf.st_gid != group_id)
    138 				tst_resm(TFAIL, "%s: incorrect "
    139 					 "ownership set, Expected %d "
    140 					 "%d", TESTFILE, user_id,
    141 					 group_id);
    142 			else
    143 				tst_resm(TPASS, "chown succeeded");
    144 		}
    145 	}
    146 
    147 	cleanup();
    148 	tst_exit();
    149 }
    150 
    151 void setup(void)
    152 {
    153 	int fd;
    154 
    155 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    156 
    157 	tst_require_root();
    158 
    159 	TEST_PAUSE;
    160 
    161 	tst_tmpdir();
    162 
    163 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1)
    164 		tst_brkm(TBROK | TERRNO, cleanup, "opening %s failed",
    165 			 TESTFILE);
    166 	SAFE_CLOSE(cleanup, fd);
    167 
    168 }
    169 
    170 void cleanup(void)
    171 {
    172 	tst_rmdir();
    173 }
    174