Zeng Linggang | 58241b1 | 2014-03-14 17:48:53 +0800 | [diff] [blame] | 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 |
| 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 Linggang | 58241b1 | 2014-03-14 17:48:53 +0800 | [diff] [blame] | 33 | #include "safe_macros.h" |
| 34 | |
| 35 | TCID_DEFINE(pselect02); |
| 36 | |
| 37 | static fd_set read_fds; |
| 38 | static struct timespec time_buf; |
| 39 | |
| 40 | static 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 | |
| 51 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
Zeng Linggang | 58241b1 | 2014-03-14 17:48:53 +0800 | [diff] [blame] | 52 | |
| 53 | static void setup(void); |
| 54 | static void cleanup(void); |
| 55 | static void pselect_verify(const struct test_case_t *); |
| 56 | |
| 57 | int main(int argc, char **argv) |
| 58 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 59 | const char *msg; |
Zeng Linggang | 58241b1 | 2014-03-14 17:48:53 +0800 | [diff] [blame] | 60 | 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 | |
| 78 | static void setup(void) |
| 79 | { |
| 80 | int fd; |
| 81 | |
| 82 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 83 | |
Zeng Linggang | 58241b1 | 2014-03-14 17:48:53 +0800 | [diff] [blame] | 84 | 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 | |
| 99 | static 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 | |
| 118 | static void cleanup(void) |
| 119 | { |
Zeng Linggang | 58241b1 | 2014-03-14 17:48:53 +0800 | [diff] [blame] | 120 | tst_rmdir(); |
| 121 | } |