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, chown(2) succeeds to change the owner and group of a file 12f08c3bdfSopenharmony_ci * specified by path to any numeric owner(uid)/group(gid) values when invoked 13f08c3bdfSopenharmony_ci * 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_cistruct test_case_t { 24f08c3bdfSopenharmony_ci char *desc; 25f08c3bdfSopenharmony_ci uid_t uid; 26f08c3bdfSopenharmony_ci gid_t gid; 27f08c3bdfSopenharmony_ci} tc[] = { 28f08c3bdfSopenharmony_ci {"change owner/group ids", 700, 701}, 29f08c3bdfSopenharmony_ci {"change owner id only", 702, -1}, 30f08c3bdfSopenharmony_ci {"change owner id only", 703, 701}, 31f08c3bdfSopenharmony_ci {"change group id only", -1, 704}, 32f08c3bdfSopenharmony_ci {"change group id only", 703, 705}, 33f08c3bdfSopenharmony_ci {"no change", -1, -1} 34f08c3bdfSopenharmony_ci}; 35f08c3bdfSopenharmony_ci 36f08c3bdfSopenharmony_cistatic void run(unsigned int i) 37f08c3bdfSopenharmony_ci{ 38f08c3bdfSopenharmony_ci struct stat stat_buf; 39f08c3bdfSopenharmony_ci uid_t expect_uid = tc[i].uid == (uid_t)-1 ? tc[i - 1].uid : tc[i].uid; 40f08c3bdfSopenharmony_ci gid_t expect_gid = tc[i].gid == (uid_t)-1 ? tc[i - 1].gid : tc[i].gid; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci TST_EXP_PASS(CHOWN(TESTFILE, tc[i].uid, tc[i].gid), "chown(%s, %d, %d), %s", 43f08c3bdfSopenharmony_ci TESTFILE, tc[i].uid, tc[i].gid, tc[i].desc); 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci SAFE_STAT(TESTFILE, &stat_buf); 46f08c3bdfSopenharmony_ci if (stat_buf.st_uid != expect_uid || stat_buf.st_gid != expect_gid) { 47f08c3bdfSopenharmony_ci tst_res(TFAIL, "%s: incorrect ownership set, expected %d %d", 48f08c3bdfSopenharmony_ci TESTFILE, expect_uid, expect_gid); 49f08c3bdfSopenharmony_ci } 50f08c3bdfSopenharmony_ci} 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_cistatic void setup(void) 53f08c3bdfSopenharmony_ci{ 54f08c3bdfSopenharmony_ci SAFE_TOUCH(TESTFILE, FILE_MODE, NULL); 55f08c3bdfSopenharmony_ci} 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_cistatic struct tst_test test = { 58f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(tc), 59f08c3bdfSopenharmony_ci .needs_root = 1, 60f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 61f08c3bdfSopenharmony_ci .setup = setup, 62f08c3bdfSopenharmony_ci .test = run, 63f08c3bdfSopenharmony_ci}; 64