1 /*
2  * Copyright (c) 2014 Fujitsu Ltd.
3  * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 /*
18  * Test Description:
19  *  Verify that,
20  *   1. path is NULL, EFAULT would return.
21  *   2. Too many symbolic links were encountered in translating path,
22  *	ELOOP would return.
23  *   3. path is too long, ENAMETOOLONG would return.
24  *   4. The file referred to by path does not exist, ENOENT would return.
25  *   5. A component of the path prefix of path is not a directory,
26  *	ENOENT would return.
27  */
28 
29 #include <errno.h>
30 #include <sys/statvfs.h>
31 #include <sys/mman.h>
32 
33 #include "test.h"
34 #include "safe_macros.h"
35 
36 #define TEST_SYMLINK	"statvfs_symlink"
37 #define TEST_FILE	"statvfs_file"
38 
39 char *TCID = "statvfs02";
40 
41 static struct statvfs buf;
42 static char nametoolong[PATH_MAX+2];
43 static void setup(void);
44 static void cleanup(void);
45 
46 static struct test_case_t {
47 	char *path;
48 	struct statvfs *buf;
49 	int exp_errno;
50 } test_cases[] = {
51 	{NULL, &buf, EFAULT},
52 	{TEST_SYMLINK, &buf, ELOOP},
53 	{nametoolong, &buf, ENAMETOOLONG},
54 	{"filenoexist", &buf, ENOENT},
55 	{"statvfs_file/test", &buf, ENOTDIR},
56 };
57 
58 int TST_TOTAL = ARRAY_SIZE(test_cases);
59 static void statvfs_verify(const struct test_case_t *);
60 
61 int main(int argc, char **argv)
62 {
63 	int i, lc;
64 
65 	tst_parse_opts(argc, argv, NULL, NULL);
66 
67 	setup();
68 
69 	for (lc = 0; TEST_LOOPING(lc); lc++) {
70 		tst_count = 0;
71 		for (i = 0; i < TST_TOTAL; i++)
72 			statvfs_verify(&test_cases[i]);
73 	}
74 
75 	cleanup();
76 	tst_exit();
77 }
78 
79 static void setup(void)
80 {
81 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
82 
83 	TEST_PAUSE;
84 
85 	tst_tmpdir();
86 
87 	SAFE_SYMLINK(cleanup, TEST_SYMLINK, "statfs_symlink_2");
88 	SAFE_SYMLINK(cleanup, "statfs_symlink_2", TEST_SYMLINK);
89 
90 	memset(nametoolong, 'a', PATH_MAX+1);
91 
92 	SAFE_TOUCH(cleanup, TEST_FILE, 0644, NULL);
93 
94 	test_cases[0].path = SAFE_MMAP(cleanup, NULL, 1, PROT_NONE,
95 	                               MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
96 }
97 
98 static void statvfs_verify(const struct test_case_t *test)
99 {
100 	TEST(statvfs(test->path, test->buf));
101 
102 	if (TEST_RETURN != -1) {
103 		tst_resm(TFAIL, "statvfs() succeeded unexpectedly");
104 		return;
105 	}
106 
107 	if (TEST_ERRNO == test->exp_errno) {
108 		tst_resm(TPASS | TTERRNO, "statvfs() failed as expected");
109 	} else {
110 		tst_resm(TFAIL | TTERRNO,
111 			 "statvfs() failed unexpectedly; expected: %d - %s",
112 			 test->exp_errno, strerror(test->exp_errno));
113 	}
114 }
115 
116 static void cleanup(void)
117 {
118 	tst_rmdir();
119 }
120