xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/lstat/lstat01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
4  *  Authors:	William Roske, Dave Fenner
5  *
6  *  06/2019 Ported to new library:
7  *		Christian Amann <[email protected]>
8  */
9 /*
10  * Basic test for lstat():
11  *
12  * Tests if lstat() writes correct information about a symlink
13  * into the stat structure.
14  */
15 
16 #include <errno.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include "tst_test.h"
22 
23 #define TESTFILE        "tst_file"
24 #define TESTSYML        "tst_syml"
25 
26 static uid_t user_id;
27 static gid_t group_id;
28 
run(void)29 static void run(void)
30 {
31 	struct stat stat_buf;
32 
33 	memset(&stat_buf, 0, sizeof(stat_buf));
34 
35 	TEST(lstat(TESTSYML, &stat_buf));
36 	if (TST_RET == -1)
37 		tst_res(TFAIL | TTERRNO, "Calling lstat() failed");
38 
39 	if ((stat_buf.st_mode & S_IFMT) != S_IFLNK ||
40 	    stat_buf.st_uid != user_id ||
41 	    stat_buf.st_gid != group_id) {
42 /*
43  * b/148414662 b/150467619
44  * Symlinks normally have a size equal to the path length but this is not true
45  * for symlinks encrypted by per-file encryption.
46  * 	    stat_buf.st_size != strlen(TESTFILE)) {
47  */
48 		tst_res(TFAIL,
49 			"lstat() reported incorrect values for the symlink!");
50 	} else {
51 		tst_res(TPASS,
52 			"lstat() reported correct values for the symlink!");
53 	}
54 }
55 
setup(void)56 static void setup(void)
57 {
58 	user_id  = getuid();
59 	group_id = getgid();
60 
61 	SAFE_TOUCH(TESTFILE, 0644, NULL);
62 	SAFE_SYMLINK(TESTFILE, TESTSYML);
63 }
64 
cleanup(void)65 static void cleanup(void)
66 {
67 	SAFE_UNLINK(TESTSYML);
68 }
69 
70 static struct tst_test test = {
71 	.test_all = run,
72 	.setup = setup,
73 	.cleanup = cleanup,
74 	.needs_tmpdir = 1,
75 };
76