1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Ported to LTP: Wayne Boyer
5 */
6 /*
7 * DESCRIPTION
8 * check mkdir() with various error conditions that should produce
9 * EFAULT, ENAMETOOLONG, EEXIST, ENOENT, ENOTDIR, ELOOP and EROFS
10 */
11
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/mman.h>
16 #include <fcntl.h>
17 #include <sys/mount.h>
18
19 #include "tst_test.h"
20
21 #define TST_EEXIST "tst_eexist"
22 #define TST_ENOENT "tst_enoent/tst"
23 #define TST_ENOTDIR_FILE "tst_enotdir"
24 #define TST_ENOTDIR_DIR "tst_enotdir/tst"
25 #define MODE 0777
26
27 #define MNT_POINT "mntpoint"
28 #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
29 S_IXGRP|S_IROTH|S_IXOTH)
30 #define TST_EROFS "mntpoint/tst_erofs"
31
32 static char long_dir[PATH_MAX + 2] = {[0 ... PATH_MAX + 1] = 'a'};
33 static char loop_dir[PATH_MAX] = ".";
34
35 struct tcase;
36
37 static struct tcase {
38 char *pathname;
39 int exp_errno;
40 } TC[] = {
41 {NULL, EFAULT},
42 {long_dir, ENAMETOOLONG},
43 {TST_EEXIST, EEXIST},
44 {TST_ENOENT, ENOENT},
45 {TST_ENOTDIR_DIR, ENOTDIR},
46 {loop_dir, ELOOP},
47 {TST_EROFS, EROFS},
48 };
49
verify_mkdir(unsigned int n)50 static void verify_mkdir(unsigned int n)
51 {
52 struct tcase *tc = TC + n;
53
54 TEST(mkdir(tc->pathname, MODE));
55 if (TST_RET != -1) {
56 tst_res(TFAIL, "mkdir() returned %ld, expected -1, errno=%d",
57 TST_RET, tc->exp_errno);
58 return;
59 }
60
61 if (TST_ERR == tc->exp_errno) {
62 tst_res(TPASS | TTERRNO, "mkdir() failed as expected");
63 } else {
64 tst_res(TFAIL | TTERRNO,
65 "mkdir() failed unexpectedly; expected: %d - %s",
66 tc->exp_errno, strerror(tc->exp_errno));
67 }
68 }
69
setup(void)70 static void setup(void)
71 {
72 unsigned int i;
73
74 SAFE_TOUCH(TST_EEXIST, MODE, NULL);
75 SAFE_TOUCH(TST_ENOTDIR_FILE, MODE, NULL);
76
77 for (i = 0; i < ARRAY_SIZE(TC); i++) {
78 if (TC[i].exp_errno == EFAULT)
79 TC[i].pathname = tst_get_bad_addr(NULL);
80 }
81
82 SAFE_MKDIR("test_eloop", DIR_MODE);
83 SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
84 for (i = 0; i < 43; i++)
85 strcat(loop_dir, "/test_eloop");
86 }
87
88 static struct tst_test test = {
89 .tcnt = ARRAY_SIZE(TC),
90 .needs_root = 1,
91 .needs_rofs = 1,
92 .mntpoint = MNT_POINT,
93 .setup = setup,
94 .test = verify_mkdir,
95 };
96