• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *  07/2001 Ported by Wayne Boyer
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program;  if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Test Name: lstat03
22  *
23  * Test Description:
24  *  Verify that, lstat(2) succeeds to get the status of a file pointed to by
25  *  symlink and fills the stat structure elements.
26  *
27  * Expected Result:
28  *  lstat() should return value 0 on success and the stat structure elements
29  *  should be filled with the symlink file information.
30  */
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <fcntl.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <pwd.h>
39 
40 #include "test.h"
41 #include "safe_macros.h"
42 
43 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
44 #define TESTFILE	"testfile"
45 #define SFILE		"sfile"
46 #define FILE_SIZE       1024
47 #define BUF_SIZE	256
48 #define PERMS		0644
49 
50 char *TCID = "lstat03";
51 int TST_TOTAL = 1;
52 static uid_t user_id;
53 static gid_t group_id;
54 
55 static char nobody_uid[] = "nobody";
56 static struct passwd *ltpuser;
57 
58 static void setup(void);
59 static void cleanup(void);
60 
main(int ac,char ** av)61 int main(int ac, char **av)
62 {
63 	struct stat stat_buf;
64 	int lc;
65 
66 	tst_parse_opts(ac, av, NULL, NULL);
67 
68 	setup();
69 
70 	for (lc = 0; TEST_LOOPING(lc); lc++) {
71 		tst_count = 0;
72 
73 		TEST(lstat(SFILE, &stat_buf));
74 
75 		if (TEST_RETURN == -1) {
76 			tst_resm(TFAIL | TTERRNO, "lstat(%s) failed", SFILE);
77 			continue;
78 		}
79 
80 		if ((stat_buf.st_uid != user_id) ||
81 		    (stat_buf.st_gid != group_id) ||
82 		    ((stat_buf.st_mode & S_IFMT) != S_IFLNK) ||
83 		    (stat_buf.st_size != strlen(TESTFILE))) {
84 			tst_resm(TFAIL, "Functionality of lstat(2) on "
85 				 "'%s' Failed", SFILE);
86 		} else {
87 			tst_resm(TPASS, "Functionality of lstat(2) on "
88 				 "'%s' Succcessful", SFILE);
89 		}
90 	}
91 
92 	cleanup();
93 	tst_exit();
94 }
95 
setup(void)96 static void setup(void)
97 {
98 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
99 
100 	tst_require_root();
101 
102 	ltpuser = SAFE_GETPWNAM(NULL, nobody_uid);
103 	SAFE_SETUID(NULL, ltpuser->pw_uid);
104 
105 	TEST_PAUSE;
106 
107 	tst_tmpdir();
108 
109 	if (tst_fill_file(TESTFILE, 'a', 1024, 1))
110 		tst_brkm(TBROK, cleanup, "Failed to create " TESTFILE);
111 
112 	SAFE_SYMLINK(cleanup, TESTFILE, SFILE);
113 
114 	user_id = getuid();
115 	group_id = getgid();
116 }
117 
cleanup(void)118 static void cleanup(void)
119 {
120 	tst_rmdir();
121 }
122