blob: f6c444d7802dc5f71555a79a667582eb7a359239 [file] [log] [blame]
Zeng Linggang58241b12014-03-14 17:48:53 +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
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 the
13 * GNU Library 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
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20/*
21 * Test Description:
22 * Verify that,
23 * 1. pselect() fails with -1 return value and sets errno to EBADF
24 * if a file descriptor that was already closed.
25 * 2. pselect() fails with -1 return value and sets errno to EINVAL
26 * if nfds was negative.
27 * 3. pselect() fails with -1 return value and sets errno to EINVAL
28 * if the value contained within timeout was invalid.
29 */
30
31#include <errno.h>
32#include "test.h"
Zeng Linggang58241b12014-03-14 17:48:53 +080033#include "safe_macros.h"
34
35TCID_DEFINE(pselect02);
36
37static fd_set read_fds;
38static struct timespec time_buf;
39
40static struct test_case_t {
41 int nfds;
42 fd_set *readfds;
43 struct timespec *timeout;
44 int exp_errno;
45} test_cases[] = {
46 {128, &read_fds, NULL, EBADF},
47 {-1, NULL, NULL, EINVAL},
48 {128, NULL, &time_buf, EINVAL},
49};
50
51int TST_TOTAL = ARRAY_SIZE(test_cases);
Zeng Linggang58241b12014-03-14 17:48:53 +080052
53static void setup(void);
54static void cleanup(void);
55static void pselect_verify(const struct test_case_t *);
56
57int main(int argc, char **argv)
58{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020059 const char *msg;
Zeng Linggang58241b12014-03-14 17:48:53 +080060 int lc, i;
61
62 msg = parse_opts(argc, argv, NULL, NULL);
63 if (msg != NULL)
64 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
65
66 setup();
67
68 for (lc = 0; TEST_LOOPING(lc); lc++) {
69 tst_count = 0;
70 for (i = 0; i < TST_TOTAL; i++)
71 pselect_verify(&test_cases[i]);
72 }
73
74 cleanup();
75 tst_exit();
76}
77
78static void setup(void)
79{
80 int fd;
81
82 tst_sig(NOFORK, DEF_HANDLER, cleanup);
83
Zeng Linggang58241b12014-03-14 17:48:53 +080084 TEST_PAUSE;
85
86 tst_tmpdir();
87
88 fd = SAFE_OPEN(cleanup, "test_file", O_RDWR | O_CREAT, 0777);
89
90 FD_ZERO(&read_fds);
91 FD_SET(fd, &read_fds);
92
93 SAFE_CLOSE(cleanup, fd);
94
95 time_buf.tv_sec = -1;
96 time_buf.tv_nsec = 0;
97}
98
99static void pselect_verify(const struct test_case_t *test)
100{
101 TEST(pselect(test->nfds, test->readfds, NULL, NULL, test->timeout,
102 NULL));
103
104 if (TEST_RETURN != -1) {
105 tst_resm(TFAIL, "pselect() succeeded unexpectedly");
106 return;
107 }
108
109 if (TEST_ERRNO == test->exp_errno) {
110 tst_resm(TPASS | TTERRNO, "pselect() failed as expected");
111 } else {
112 tst_resm(TFAIL | TTERRNO,
113 "pselect() failed unexpectedly; expected: %d - %s",
114 test->exp_errno, strerror(test->exp_errno));
115 }
116}
117
118static void cleanup(void)
119{
Zeng Linggang58241b12014-03-14 17:48:53 +0800120 tst_rmdir();
121}