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 *  05/2019 Ported to new library: Christian Amann <camann@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Tests if fstat() returns correctly and reports correct file information
12f08c3bdfSopenharmony_ci * using the stat structure.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#define TESTFILE        "test_file"
18f08c3bdfSopenharmony_ci#define LINK_TESTFILE   "link_test_file"
19f08c3bdfSopenharmony_ci#define FILE_SIZE       1024
20f08c3bdfSopenharmony_ci#define FILE_MODE	0644
21f08c3bdfSopenharmony_ci#define NLINK	        2
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic struct stat stat_buf;
24f08c3bdfSopenharmony_cistatic uid_t user_id;
25f08c3bdfSopenharmony_cistatic gid_t group_id;
26f08c3bdfSopenharmony_cistatic int fildes;
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_cistatic void run(void)
29f08c3bdfSopenharmony_ci{
30f08c3bdfSopenharmony_ci	TST_EXP_PASS(fstat(fildes, &stat_buf));
31f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(stat_buf.st_uid, user_id);
32f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(stat_buf.st_gid, group_id);
33f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(stat_buf.st_size, FILE_SIZE);
34f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(stat_buf.st_mode & 0777, FILE_MODE);
35f08c3bdfSopenharmony_ci	TST_EXP_EQ_LU(stat_buf.st_nlink, NLINK);
36f08c3bdfSopenharmony_ci}
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic void setup(void)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	user_id  = getuid();
41f08c3bdfSopenharmony_ci	group_id = getgid();
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ci	umask(0);
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	fildes = SAFE_OPEN(TESTFILE, O_WRONLY | O_CREAT, FILE_MODE);
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	if (tst_fill_file(TESTFILE, 'a', FILE_SIZE, 1))
48f08c3bdfSopenharmony_ci		tst_brk(TBROK, "Could not fill test file");
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	SAFE_LINK(TESTFILE, LINK_TESTFILE);
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic void cleanup(void)
54f08c3bdfSopenharmony_ci{
55f08c3bdfSopenharmony_ci	if (fildes > 0)
56f08c3bdfSopenharmony_ci		SAFE_CLOSE(fildes);
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic struct tst_test test = {
60f08c3bdfSopenharmony_ci	.test_all = run,
61f08c3bdfSopenharmony_ci	.setup = setup,
62f08c3bdfSopenharmony_ci	.cleanup = cleanup,
63f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
64f08c3bdfSopenharmony_ci};
65