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 John George
5f08c3bdfSopenharmony_ci *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Verify that system call utime() fails with
12f08c3bdfSopenharmony_ci *
13f08c3bdfSopenharmony_ci * - EACCES when times argument is NULL and user does not have rights to modify
14f08c3bdfSopenharmony_ci *   the file.
15f08c3bdfSopenharmony_ci * - ENOENT when specified file does not exist.
16f08c3bdfSopenharmony_ci * - EPERM when times argument is not NULL and user does not have rights to
17f08c3bdfSopenharmony_ci *   modify the file.
18f08c3bdfSopenharmony_ci * - EROFS when the path resides on a read-only filesystem.
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#include <pwd.h>
22f08c3bdfSopenharmony_ci#include <utime.h>
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include "tst_test.h"
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#define TEMP_FILE	"tmp_file"
27f08c3bdfSopenharmony_ci#define MNT_POINT	"mntpoint"
28f08c3bdfSopenharmony_ci#define FILE_MODE	0644
29f08c3bdfSopenharmony_ci#define TEST_USERNAME "nobody"
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cistatic const struct utimbuf times;
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic struct tcase {
34f08c3bdfSopenharmony_ci	char *pathname;
35f08c3bdfSopenharmony_ci	int exp_errno;
36f08c3bdfSopenharmony_ci	const struct utimbuf *utimbuf;
37f08c3bdfSopenharmony_ci	char *err_desc;
38f08c3bdfSopenharmony_ci} tcases[] = {
39f08c3bdfSopenharmony_ci	{TEMP_FILE, EACCES, NULL, "No write access"},
40f08c3bdfSopenharmony_ci	{"", ENOENT, NULL, "File not exist"},
41f08c3bdfSopenharmony_ci	{TEMP_FILE, EPERM, &times, "Not file owner"},
42f08c3bdfSopenharmony_ci	{MNT_POINT, EROFS, NULL, "Read-only filesystem"}
43f08c3bdfSopenharmony_ci};
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void setup(void)
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	struct passwd *pw;
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ci	pw = SAFE_GETPWNAM(TEST_USERNAME);
53f08c3bdfSopenharmony_ci	tst_res(TINFO, "Switching effective user ID to user: %s", pw->pw_name);
54f08c3bdfSopenharmony_ci	SAFE_SETEUID(pw->pw_uid);
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic void run(unsigned int i)
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[i];
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	TST_EXP_FAIL(utime(tc->pathname, tc->utimbuf),
62f08c3bdfSopenharmony_ci				tc->exp_errno, "%s", tc->err_desc);
63f08c3bdfSopenharmony_ci}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic struct tst_test test = {
66f08c3bdfSopenharmony_ci	.setup = setup,
67f08c3bdfSopenharmony_ci	.test = run,
68f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
69f08c3bdfSopenharmony_ci	.needs_root = 1,
70f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
71f08c3bdfSopenharmony_ci	.mntpoint = MNT_POINT,
72f08c3bdfSopenharmony_ci	.needs_rofs = 1
73f08c3bdfSopenharmony_ci};
74