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