• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
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 <camann@suse.com>
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 	    stat_buf.st_size != strlen(TESTFILE)) {
43 		tst_res(TFAIL,
44 			"lstat() reported incorrect values for the symlink!");
45 	} else {
46 		tst_res(TPASS,
47 			"lstat() reported correct values for the symlink!");
48 	}
49 }
50 
setup(void)51 static void setup(void)
52 {
53 	user_id  = getuid();
54 	group_id = getgid();
55 
56 	SAFE_TOUCH(TESTFILE, 0644, NULL);
57 	SAFE_SYMLINK(TESTFILE, TESTSYML);
58 }
59 
cleanup(void)60 static void cleanup(void)
61 {
62 	SAFE_UNLINK(TESTSYML);
63 }
64 
65 static struct tst_test test = {
66 	.test_all = run,
67 	.setup = setup,
68 	.cleanup = cleanup,
69 	.needs_tmpdir = 1,
70 };
71