1 /* Test manipulation of non-blocking flag.
2 Copyright (C) 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2011. */
21 #include "nonblocking.h"
25 #include <sys/socket.h>
33 const char *file = "test-nonblock.tmp";
37 bool sock_works = true;
39 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
40 /* For now, we can't get nonblocking status of windows sockets. */
44 fd_file = creat (file, 0600);
46 /* Assume std descriptors were provided by invoker. */
47 ASSERT (STDERR_FILENO < fd_file);
49 /* Test regular files; setting nonblocking on file is unspecified. */
50 ASSERT (get_nonblocking_flag (fd_file) == 0);
51 ASSERT (set_nonblocking_flag (fd_file, false) == 0);
52 ASSERT (get_nonblocking_flag (fd_file) == 0);
53 ASSERT (close (fd_file) == 0);
54 ASSERT (unlink (file) == 0);
56 /* Test directories; setting nonblocking is unspecified. */
57 fd_file = open (".", O_RDONLY);
58 ASSERT (STDERR_FILENO < fd_file);
59 ASSERT (get_nonblocking_flag (fd_file) == 0);
60 ASSERT (set_nonblocking_flag (fd_file, false) == 0);
61 ASSERT (get_nonblocking_flag (fd_file) == 0);
62 ASSERT (close (fd_file) == 0);
65 ASSERT (pipe (fd_pipe) == 0);
66 ASSERT (get_nonblocking_flag (fd_pipe[0]) == 0);
67 ASSERT (get_nonblocking_flag (fd_pipe[1]) == 0);
68 ASSERT (set_nonblocking_flag (fd_pipe[0], true) == 0);
69 ASSERT (get_nonblocking_flag (fd_pipe[0]) == 1);
70 ASSERT (get_nonblocking_flag (fd_pipe[1]) == 0);
71 ASSERT (set_nonblocking_flag (fd_pipe[1], true) == 0);
72 ASSERT (set_nonblocking_flag (fd_pipe[0], false) == 0);
73 ASSERT (get_nonblocking_flag (fd_pipe[0]) == 0);
74 ASSERT (get_nonblocking_flag (fd_pipe[1]) == 1);
75 ASSERT (close (fd_pipe[0]) == 0);
76 ASSERT (close (fd_pipe[1]) == 0);
79 /* mingw still lacks O_NONBLOCK replacement. */
80 ASSERT (pipe2 (fd_pipe, O_NONBLOCK) == 0);
81 ASSERT (get_nonblocking_flag (fd_pipe[0]) == !!O_NONBLOCK);
82 ASSERT (get_nonblocking_flag (fd_pipe[1]) == !!O_NONBLOCK);
83 ASSERT (close (fd_pipe[0]) == 0);
84 ASSERT (close (fd_pipe[1]) == 0);
85 #endif /* GNULIB_TEST_PIPE2 */
88 fd_sock = socket (AF_INET, SOCK_STREAM, 0);
89 ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 0 : -1));
90 ASSERT (set_nonblocking_flag (fd_sock, true) == 0);
91 ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 1 : -1));
92 ASSERT (set_nonblocking_flag (fd_sock, false) == 0);
93 ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 0 : -1));
94 ASSERT (close (fd_sock) == 0);
97 fd_sock = socket (AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
98 ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 1 : -1));
99 ASSERT (close (fd_sock) == 0);
100 #endif /* SOCK_NONBLOCK */
102 /* Test error handling. */
105 ASSERT (get_nonblocking_flag (-1) == -1);
106 ASSERT (errno == EBADF);
110 ASSERT (set_nonblocking_flag (-1, false) == -1);
111 ASSERT (errno == EBADF);
115 ASSERT (set_nonblocking_flag (-1, true) == -1);
116 ASSERT (errno == EBADF);
120 ASSERT (set_nonblocking_flag (10000000, false) == -1);
121 ASSERT (errno == EBADF);