plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | /* |
plars | 4f822e0 | 2002-02-20 23:53:19 +0000 | [diff] [blame] | 21 | * Test Name: chown05 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, chown(2) succeeds to change the owner and group of a file |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * specified by path to any numeric owner(uid)/group(gid) values when invoked |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * by super-user. |
| 27 | * |
| 28 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * chown(2) should return 0 and the ownership set on the file should match |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | * the numeric values contained in owner and group respectively. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 31 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
plars | 4f822e0 | 2002-02-20 23:53:19 +0000 | [diff] [blame] | 54 | * chown05 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 <sys/fcntl.h> |
| 74 | #include <errno.h> |
| 75 | #include <string.h> |
| 76 | #include <signal.h> |
| 77 | |
| 78 | #include "test.h" |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 79 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
Garrett Cooper | ccfb558 | 2010-12-17 16:06:54 -0800 | [diff] [blame] | 81 | #define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | #define TESTFILE "testfile" |
| 83 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 84 | TCID_DEFINE(chown05); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
| 86 | struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | uid_t user_id; |
| 88 | gid_t group_id; |
Garrett Cooper | ccfb558 | 2010-12-17 16:06:54 -0800 | [diff] [blame] | 89 | } test_cases[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | { |
| 91 | 700, 701}, { |
| 92 | 702, -1}, { |
| 93 | 703, 701}, { |
| 94 | -1, 704}, { |
| 95 | 703, 705},}; |
| 96 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 97 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
| 99 | void setup(); /* setup function for the test */ |
| 100 | void cleanup(); /* cleanup function for the test */ |
| 101 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | { |
| 104 | struct stat stat_buf; /* stat(2) struct contents */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 105 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 106 | const char *msg; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 107 | int i; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 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 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | |
Garrett Cooper | ccfb558 | 2010-12-17 16:06:54 -0800 | [diff] [blame] | 111 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 113 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | setup(); |
| 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 117 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 118 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | |
Garrett Cooper | ccfb558 | 2010-12-17 16:06:54 -0800 | [diff] [blame] | 120 | for (i = 0; i < TST_TOTAL; i++) { |
| 121 | user_id = test_cases[i].user_id; |
| 122 | group_id = test_cases[i].group_id; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 124 | TEST(CHOWN(cleanup, TESTFILE, user_id, group_id)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | tst_resm(TFAIL | TTERRNO, "chown failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | continue; |
| 129 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | if (stat(TESTFILE, &stat_buf) == -1) |
| 131 | tst_brkm(TFAIL, cleanup, "stat failed"); |
| 132 | if (user_id == -1) |
| 133 | user_id = test_cases[i - 1].user_id; |
| 134 | if (group_id == -1) |
| 135 | group_id = test_cases[i - 1].group_id; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 137 | if (stat_buf.st_uid != user_id || |
| 138 | stat_buf.st_gid != group_id) |
| 139 | tst_resm(TFAIL, "%s: incorrect " |
| 140 | "ownership set, Expected %d " |
| 141 | "%d", TESTFILE, user_id, |
| 142 | group_id); |
| 143 | else |
| 144 | tst_resm(TPASS, "chown succeeded"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | cleanup(); |
Garrett Cooper | ccfb558 | 2010-12-17 16:06:54 -0800 | [diff] [blame] | 149 | tst_exit(); |
| 150 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 152 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | { |
| 154 | int fd; |
| 155 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 157 | |
Garrett Cooper | ccfb558 | 2010-12-17 16:06:54 -0800 | [diff] [blame] | 158 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | TEST_PAUSE; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 161 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | tst_tmpdir(); |
| 163 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) |
| 165 | tst_brkm(TBROK | TERRNO, cleanup, "opening %s failed", |
| 166 | TESTFILE); |
vapier | 3fdf88a | 2009-08-28 11:59:17 +0000 | [diff] [blame] | 167 | if (close(fd) == -1) |
Garrett Cooper | ccfb558 | 2010-12-17 16:06:54 -0800 | [diff] [blame] | 168 | tst_brkm(TBROK, cleanup, "closing %s failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 170 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 172 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 175 | } |