1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4f08c3bdfSopenharmony_ci *  Authors:	William Roske, Dave Fenner
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci *  06/2019 Ported to new library:
7f08c3bdfSopenharmony_ci *		Christian Amann <camann@suse.com>
8f08c3bdfSopenharmony_ci */
9f08c3bdfSopenharmony_ci/*
10f08c3bdfSopenharmony_ci * Basic test for lstat():
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Tests if lstat() writes correct information about a symlink
13f08c3bdfSopenharmony_ci * into the stat structure.
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#include <errno.h>
17f08c3bdfSopenharmony_ci#include <string.h>
18f08c3bdfSopenharmony_ci#include <unistd.h>
19f08c3bdfSopenharmony_ci#include <sys/stat.h>
20f08c3bdfSopenharmony_ci#include <sys/types.h>
21f08c3bdfSopenharmony_ci#include "tst_test.h"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#define TESTFILE        "tst_file"
24f08c3bdfSopenharmony_ci#define TESTSYML        "tst_syml"
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic uid_t user_id;
27f08c3bdfSopenharmony_cistatic gid_t group_id;
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic void run(void)
30f08c3bdfSopenharmony_ci{
31f08c3bdfSopenharmony_ci	struct stat stat_buf;
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	memset(&stat_buf, 0, sizeof(stat_buf));
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	TEST(lstat(TESTSYML, &stat_buf));
36f08c3bdfSopenharmony_ci	if (TST_RET == -1)
37f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "Calling lstat() failed");
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	if ((stat_buf.st_mode & S_IFMT) != S_IFLNK ||
40f08c3bdfSopenharmony_ci	    stat_buf.st_uid != user_id ||
41f08c3bdfSopenharmony_ci	    stat_buf.st_gid != group_id ||
42f08c3bdfSopenharmony_ci	    stat_buf.st_size != strlen(TESTFILE)) {
43f08c3bdfSopenharmony_ci		tst_res(TFAIL,
44f08c3bdfSopenharmony_ci			"lstat() reported incorrect values for the symlink!");
45f08c3bdfSopenharmony_ci	} else {
46f08c3bdfSopenharmony_ci		tst_res(TPASS,
47f08c3bdfSopenharmony_ci			"lstat() reported correct values for the symlink!");
48f08c3bdfSopenharmony_ci	}
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void setup(void)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	user_id  = getuid();
54f08c3bdfSopenharmony_ci	group_id = getgid();
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	SAFE_TOUCH(TESTFILE, 0644, NULL);
57f08c3bdfSopenharmony_ci	SAFE_SYMLINK(TESTFILE, TESTSYML);
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cistatic void cleanup(void)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	SAFE_UNLINK(TESTSYML);
63f08c3bdfSopenharmony_ci}
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_cistatic struct tst_test test = {
66f08c3bdfSopenharmony_ci	.test_all = run,
67f08c3bdfSopenharmony_ci	.setup = setup,
68f08c3bdfSopenharmony_ci	.cleanup = cleanup,
69f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
70f08c3bdfSopenharmony_ci};
71