blob: 0d42fa85d80edde733abdcdf2ee83e58a6cbd24f [file] [log] [blame]
Zeng Linggangc7fb4b72014-05-07 18:51:41 +08001/*
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. Creat a directory and open it, then delete the directory, ENOENT would
21 * return.
22 * 2. File descriptor does not refer to a directory, ENOTDIR would return.
Zeng Linggang0d99d6f2014-05-14 16:54:54 +080023 * 3. Invalid file descriptor fd, EBADF would return.
24 * 4. Argument points outside the calling process's address space, EFAULT
25 * would return.
Zeng Linggangc7fb4b72014-05-07 18:51:41 +080026 *
27 * PS:
28 * This file is for readdir(2) and the other files(readdir01.c and
29 * readdir02.c) are for readdir(3).
30 */
31
32#define _GNU_SOURCE
33
34#include <stdio.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <fcntl.h>
38#include <errno.h>
39#include <unistd.h>
Zeng Linggang0d99d6f2014-05-14 16:54:54 +080040#include <sys/mman.h>
Zeng Linggangc7fb4b72014-05-07 18:51:41 +080041#include "test.h"
Zeng Linggangc7fb4b72014-05-07 18:51:41 +080042#include "safe_macros.h"
43#include "linux_syscall_numbers.h"
44#include "lapi/readdir.h"
45
46char *TCID = "readdir21";
47
48#define TEST_DIR "test_dir"
Zeng Linggang0d99d6f2014-05-14 16:54:54 +080049#define TEST_DIR4 "test_dir4"
Zeng Linggangc7fb4b72014-05-07 18:51:41 +080050#define TEST_FILE "test_file"
51#define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
52 S_IXGRP|S_IROTH|S_IXOTH)
53
54static unsigned int del_dir_fd, file_fd;
Zeng Linggang0d99d6f2014-05-14 16:54:54 +080055static unsigned int invalid_fd = 999;
56static unsigned int dir_fd;
Zeng Linggangc7fb4b72014-05-07 18:51:41 +080057static struct old_linux_dirent dirp;
58static void setup(void);
59static void cleanup(void);
60
61static struct test_case_t {
62 unsigned int *fd;
63 struct old_linux_dirent *dirp;
64 unsigned int count;
65 int exp_errno;
66} test_cases[] = {
67 {&del_dir_fd, &dirp, sizeof(struct old_linux_dirent), ENOENT},
68 {&file_fd, &dirp, sizeof(struct old_linux_dirent), ENOTDIR},
Zeng Linggang0d99d6f2014-05-14 16:54:54 +080069 {&invalid_fd, &dirp, sizeof(struct old_linux_dirent), EBADF},
70#if !defined(UCLINUX)
71 {&dir_fd, (struct old_linux_dirent *)-1,
72 sizeof(struct old_linux_dirent), EFAULT},
73#endif
Zeng Linggangc7fb4b72014-05-07 18:51:41 +080074};
75
76int TST_TOTAL = ARRAY_SIZE(test_cases);
Zeng Linggangc7fb4b72014-05-07 18:51:41 +080077static void readdir_verify(const struct test_case_t *);
78
79int main(int argc, char **argv)
80{
81 int i, lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020082 const char *msg;
Zeng Linggangc7fb4b72014-05-07 18:51:41 +080083
84 msg = parse_opts(argc, argv, NULL, NULL);
85 if (msg != NULL)
86 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
87
88 setup();
89
90 for (lc = 0; TEST_LOOPING(lc); lc++) {
91 tst_count = 0;
92 for (i = 0; i < TST_TOTAL; i++)
93 readdir_verify(&test_cases[i]);
94 }
95
96 cleanup();
97 tst_exit();
98}
99
100static void setup(void)
101{
102 tst_sig(NOFORK, DEF_HANDLER, cleanup);
103
Zeng Linggangc7fb4b72014-05-07 18:51:41 +0800104 TEST_PAUSE;
105
106 tst_tmpdir();
107
108 SAFE_MKDIR(cleanup, TEST_DIR, DIR_MODE);
109 del_dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_RDONLY | O_DIRECTORY);
Cyril Hrubis97499c22014-05-07 14:54:22 +0200110 SAFE_RMDIR(cleanup, TEST_DIR);
Zeng Linggangc7fb4b72014-05-07 18:51:41 +0800111
112 file_fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0777);
Zeng Linggang0d99d6f2014-05-14 16:54:54 +0800113
114 SAFE_MKDIR(cleanup, TEST_DIR4, DIR_MODE);
115 dir_fd = SAFE_OPEN(cleanup, TEST_DIR4, O_RDONLY | O_DIRECTORY);
116
117#if !defined(UCLINUX)
118 test_cases[3].dirp = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
119 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
120#endif
Zeng Linggangc7fb4b72014-05-07 18:51:41 +0800121}
122
123static void readdir_verify(const struct test_case_t *test)
124{
125 TEST(ltp_syscall(__NR_readdir, *test->fd, test->dirp, test->count));
126
127 if (TEST_RETURN != -1) {
128 tst_resm(TFAIL, "readdir() succeeded unexpectedly");
129 return;
130 }
131
132 if (TEST_ERRNO == test->exp_errno) {
133 tst_resm(TPASS | TTERRNO, "readdir() failed as expected");
134 } else {
135 tst_resm(TFAIL | TTERRNO,
136 "readdir() failed unexpectedly; expected: %d - %s",
137 test->exp_errno, strerror(test->exp_errno));
138 }
139}
140
141static void cleanup(void)
142{
Zeng Linggang0d99d6f2014-05-14 16:54:54 +0800143 if (dir_fd > 0)
144 close(dir_fd);
145
Zeng Linggangc7fb4b72014-05-07 18:51:41 +0800146 if (del_dir_fd > 0)
147 close(del_dir_fd);
148
149 if (file_fd > 0)
150 close(file_fd);
151
152 tst_rmdir();
153}