1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) International Business Machines  Corp., 2001
4 * 07/2001 Ported by Wayne Boyer
5 * Copyright (c) 2021 Xie Ziyao <xieziyao@huawei.com>
6 */
7
8/*\
9 * [Description]
10 *
11 * Verify that, chown(2) succeeds to change the owner and group of a file
12 * specified by path to any numeric owner(uid)/group(gid) values when invoked
13 * by super-user.
14 */
15
16#include "tst_test.h"
17#include "compat_tst_16.h"
18#include "tst_safe_macros.h"
19
20#define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
21#define TESTFILE "testfile"
22
23struct test_case_t {
24	char *desc;
25	uid_t uid;
26	gid_t gid;
27} tc[] = {
28	{"change owner/group ids", 700, 701},
29	{"change owner id only", 702, -1},
30	{"change owner id only", 703, 701},
31	{"change group id only", -1, 704},
32	{"change group id only", 703, 705},
33	{"no change", -1, -1}
34};
35
36static void run(unsigned int i)
37{
38	struct stat stat_buf;
39	uid_t expect_uid = tc[i].uid == (uid_t)-1 ? tc[i - 1].uid : tc[i].uid;
40	gid_t expect_gid = tc[i].gid == (uid_t)-1 ? tc[i - 1].gid : tc[i].gid;
41
42	TST_EXP_PASS(CHOWN(TESTFILE, tc[i].uid, tc[i].gid), "chown(%s, %d, %d), %s",
43		     TESTFILE, tc[i].uid, tc[i].gid, tc[i].desc);
44
45	SAFE_STAT(TESTFILE, &stat_buf);
46	if (stat_buf.st_uid != expect_uid || stat_buf.st_gid != expect_gid) {
47		tst_res(TFAIL, "%s: incorrect ownership set, expected %d %d",
48			TESTFILE, expect_uid, expect_gid);
49	}
50}
51
52static void setup(void)
53{
54	SAFE_TOUCH(TESTFILE, FILE_MODE, NULL);
55}
56
57static struct tst_test test = {
58	.tcnt = ARRAY_SIZE(tc),
59	.needs_root = 1,
60	.needs_tmpdir = 1,
61	.setup = setup,
62	.test = run,
63};
64