blob: 423f053188df8c2ad23bb6d623924b5bbb089b0d [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
subrata_modak4bb656a2009-02-26 12:02:09 +000020/*
plars4f822e02002-02-20 23:53:19 +000021 * Test Name: chown05
plars865695b2001-08-27 22:15:12 +000022 *
23 * Test Description:
24 * Verify that, chown(2) succeeds to change the owner and group of a file
subrata_modak4bb656a2009-02-26 12:02:09 +000025 * specified by path to any numeric owner(uid)/group(gid) values when invoked
plars865695b2001-08-27 22:15:12 +000026 * by super-user.
27 *
28 * Expected Result:
subrata_modak4bb656a2009-02-26 12:02:09 +000029 * chown(2) should return 0 and the ownership set on the file should match
plars865695b2001-08-27 22:15:12 +000030 * the numeric values contained in owner and group respectively.
subrata_modakbdbaec52009-02-26 12:14:51 +000031 *
plars865695b2001-08-27 22:15:12 +000032 * 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_modakbdbaec52009-02-26 12:14:51 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * 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>
plars4f822e02002-02-20 23:53:19 +000054 * chown05 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000055 * 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 Pingtian75fb0572014-11-28 16:33:41 +080079#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000080
Garrett Cooperccfb5582010-12-17 16:06:54 -080081#define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
plars865695b2001-08-27 22:15:12 +000082#define TESTFILE "testfile"
83
Han Pingtian75fb0572014-11-28 16:33:41 +080084TCID_DEFINE(chown05);
plars865695b2001-08-27 22:15:12 +000085
86struct test_case_t {
plars865695b2001-08-27 22:15:12 +000087 uid_t user_id;
88 gid_t group_id;
Garrett Cooperccfb5582010-12-17 16:06:54 -080089} test_cases[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 {
91 700, 701}, {
92 702, -1}, {
93 703, 701}, {
94 -1, 704}, {
95703, 705},};
96
Cyril Hrubisb863a0b2014-09-24 13:15:29 +020097int TST_TOTAL = ARRAY_SIZE(test_cases);
plars865695b2001-08-27 22:15:12 +000098
99void setup(); /* setup function for the test */
100void cleanup(); /* cleanup function for the test */
101
subrata_modak56207ce2009-03-23 13:35:39 +0000102int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000103{
104 struct stat stat_buf; /* stat(2) struct contents */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200105 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200106 const char *msg;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200107 int i;
Garrett Cooper53740502010-12-16 00:04:01 -0800108 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_modak56207ce2009-03-23 13:35:39 +0000110
Garrett Cooperccfb5582010-12-17 16:06:54 -0800111 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000112 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800113
plars865695b2001-08-27 22:15:12 +0000114 setup();
115
plars865695b2001-08-27 22:15:12 +0000116 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800117
Caspar Zhangd59a6592013-03-07 14:59:12 +0800118 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000119
Garrett Cooperccfb5582010-12-17 16:06:54 -0800120 for (i = 0; i < TST_TOTAL; i++) {
121 user_id = test_cases[i].user_id;
122 group_id = test_cases[i].group_id;
plars865695b2001-08-27 22:15:12 +0000123
Han Pingtian75fb0572014-11-28 16:33:41 +0800124 TEST(CHOWN(cleanup, TESTFILE, user_id, group_id));
plars865695b2001-08-27 22:15:12 +0000125
plars865695b2001-08-27 22:15:12 +0000126 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 tst_resm(TFAIL | TTERRNO, "chown failed");
plars865695b2001-08-27 22:15:12 +0000128 continue;
129 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200130 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;
plars865695b2001-08-27 22:15:12 +0000136
Cyril Hrubise38b9612014-06-02 17:20:57 +0200137 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");
plars865695b2001-08-27 22:15:12 +0000145 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800146 }
plars865695b2001-08-27 22:15:12 +0000147
plars865695b2001-08-27 22:15:12 +0000148 cleanup();
Garrett Cooperccfb5582010-12-17 16:06:54 -0800149 tst_exit();
150}
plars865695b2001-08-27 22:15:12 +0000151
Mike Frysingerc57fba52014-04-09 18:56:30 -0400152void setup(void)
plars865695b2001-08-27 22:15:12 +0000153{
154 int fd;
155
plars865695b2001-08-27 22:15:12 +0000156 tst_sig(NOFORK, DEF_HANDLER, cleanup);
157
Garrett Cooperccfb5582010-12-17 16:06:54 -0800158 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000159
plars865695b2001-08-27 22:15:12 +0000160 TEST_PAUSE;
subrata_modak56207ce2009-03-23 13:35:39 +0000161
plars865695b2001-08-27 22:15:12 +0000162 tst_tmpdir();
163
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1)
165 tst_brkm(TBROK | TERRNO, cleanup, "opening %s failed",
166 TESTFILE);
vapier3fdf88a2009-08-28 11:59:17 +0000167 if (close(fd) == -1)
Garrett Cooperccfb5582010-12-17 16:06:54 -0800168 tst_brkm(TBROK, cleanup, "closing %s failed", TESTFILE);
plars865695b2001-08-27 22:15:12 +0000169
Garrett Cooper2c282152010-12-16 00:55:50 -0800170}
plars865695b2001-08-27 22:15:12 +0000171
Mike Frysingerc57fba52014-04-09 18:56:30 -0400172void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000173{
plars865695b2001-08-27 22:15:12 +0000174 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700175}