• 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  * DESCRIPTION
21  *	check mkdir() with various error conditions that should produce
22  *	EFAULT, ENAMETOOLONG, EEXIST, ENOENT, ENOTDIR, ELOOP and EROFS
23  */
24 
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/mman.h>
29 #include <fcntl.h>
30 #include <sys/mount.h>
31 
32 #include "tst_test.h"
33 
34 #define TST_EEXIST	"tst_eexist"
35 #define TST_ENOENT	"tst_enoent/tst"
36 #define TST_ENOTDIR_FILE "tst_enotdir"
37 #define TST_ENOTDIR_DIR	"tst_enotdir/tst"
38 #define MODE		0777
39 
40 #define MNT_POINT	"mntpoint"
41 #define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
42 			 S_IXGRP|S_IROTH|S_IXOTH)
43 #define TST_EROFS      "mntpoint/tst_erofs"
44 
45 static char long_dir[PATH_MAX + 2] = {[0 ... PATH_MAX + 1] = 'a'};
46 static char loop_dir[PATH_MAX] = ".";
47 
48 struct tcase;
49 
50 static struct tcase {
51 	char *pathname;
52 	int exp_errno;
53 } TC[] = {
54 	{NULL, EFAULT},
55 	{long_dir, ENAMETOOLONG},
56 	{TST_EEXIST, EEXIST},
57 	{TST_ENOENT, ENOENT},
58 	{TST_ENOTDIR_DIR, ENOTDIR},
59 	{loop_dir, ELOOP},
60 	{TST_EROFS, EROFS},
61 };
62 
verify_mkdir(unsigned int n)63 static void verify_mkdir(unsigned int n)
64 {
65 	struct tcase *tc = TC + n;
66 
67 	TEST(mkdir(tc->pathname, MODE));
68 	if (TST_RET != -1) {
69 		tst_res(TFAIL, "mkdir() returned %ld, expected -1, errno=%d",
70 			TST_RET, tc->exp_errno);
71 		return;
72 	}
73 
74 	if (TST_ERR == tc->exp_errno) {
75 		tst_res(TPASS | TTERRNO, "mkdir() failed as expected");
76 	} else {
77 		tst_res(TFAIL | TTERRNO,
78 			"mkdir() failed unexpectedly; expected: %d - %s",
79 			 tc->exp_errno, strerror(tc->exp_errno));
80 	}
81 }
82 
setup(void)83 static void setup(void)
84 {
85 	unsigned int i;
86 
87 	SAFE_TOUCH(TST_EEXIST, MODE, NULL);
88 	SAFE_TOUCH(TST_ENOTDIR_FILE, MODE, NULL);
89 
90 	for (i = 0; i < ARRAY_SIZE(TC); i++) {
91 		if (TC[i].exp_errno == EFAULT)
92 			TC[i].pathname = tst_get_bad_addr(NULL);
93 	}
94 
95 	SAFE_MKDIR("test_eloop", DIR_MODE);
96 	SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
97 	for (i = 0; i < 43; i++)
98 		strcat(loop_dir, "/test_eloop");
99 }
100 
101 static struct tst_test test = {
102 	.tcnt = ARRAY_SIZE(TC),
103 	.needs_tmpdir = 1,
104 	.needs_root = 1,
105 	.needs_rofs = 1,
106 	.mntpoint = MNT_POINT,
107 	.setup = setup,
108 	.test = verify_mkdir,
109 };
110