getaddrinfo: Fix test for sa_len member.
[gnulib.git] / tests / test-nonblocking.c
1 /* Test manipulation of non-blocking flag.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2011.  */
18
19 #include <config.h>
20
21 #include "nonblocking.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <sys/socket.h>
26 #include <unistd.h>
27
28 #include "macros.h"
29
30 int
31 main (void)
32 {
33   const char *file = "test-nonblock.tmp";
34   int fd_file;
35   int fd_pipe[2];
36   int fd_sock;
37   bool sock_works = true;
38
39 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
40   /* For now, we can't get nonblocking status of windows sockets.  */
41   sock_works = false;
42 #endif
43
44   fd_file = creat (file, 0600);
45
46   /* Assume std descriptors were provided by invoker.  */
47   ASSERT (STDERR_FILENO < fd_file);
48
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);
55
56   /* Test directories; setting nonblocking is unspecified.  */
57   fd_file = open (".", O_RDONLY);
58   if (STDERR_FILENO < fd_file)
59     {
60       /* mingw can't open directories unless fchdir module is active.  */
61       ASSERT (get_nonblocking_flag (fd_file) == 0);
62       ASSERT (set_nonblocking_flag (fd_file, false) == 0);
63       ASSERT (get_nonblocking_flag (fd_file) == 0);
64       ASSERT (close (fd_file) == 0);
65     }
66
67   /* Test pipes.  */
68   ASSERT (pipe (fd_pipe) == 0);
69   ASSERT (get_nonblocking_flag (fd_pipe[0]) == 0);
70   ASSERT (get_nonblocking_flag (fd_pipe[1]) == 0);
71   ASSERT (set_nonblocking_flag (fd_pipe[0], true) == 0);
72   ASSERT (get_nonblocking_flag (fd_pipe[0]) == 1);
73   ASSERT (get_nonblocking_flag (fd_pipe[1]) == 0);
74   ASSERT (set_nonblocking_flag (fd_pipe[1], true) == 0);
75   ASSERT (set_nonblocking_flag (fd_pipe[0], false) == 0);
76   ASSERT (get_nonblocking_flag (fd_pipe[0]) == 0);
77   ASSERT (get_nonblocking_flag (fd_pipe[1]) == 1);
78   ASSERT (close (fd_pipe[0]) == 0);
79   ASSERT (close (fd_pipe[1]) == 0);
80
81 #if GNULIB_TEST_PIPE2
82   /* mingw still lacks O_NONBLOCK replacement.  */
83   ASSERT (pipe2 (fd_pipe, O_NONBLOCK) == 0);
84   ASSERT (get_nonblocking_flag (fd_pipe[0]) == !!O_NONBLOCK);
85   ASSERT (get_nonblocking_flag (fd_pipe[1]) == !!O_NONBLOCK);
86   ASSERT (close (fd_pipe[0]) == 0);
87   ASSERT (close (fd_pipe[1]) == 0);
88 #endif /* GNULIB_TEST_PIPE2 */
89
90   /* Test sockets.  */
91   fd_sock = socket (AF_INET, SOCK_STREAM, 0);
92   ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 0 : -1));
93   ASSERT (set_nonblocking_flag (fd_sock, true) == 0);
94   ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 1 : -1));
95   ASSERT (set_nonblocking_flag (fd_sock, false) == 0);
96   ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 0 : -1));
97   ASSERT (close (fd_sock) == 0);
98
99 #if SOCK_NONBLOCK
100   fd_sock = socket (AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
101   ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 1 : -1));
102   ASSERT (close (fd_sock) == 0);
103 #endif /* SOCK_NONBLOCK */
104
105   /* Test error handling.  */
106   {
107     errno = 0;
108     ASSERT (get_nonblocking_flag (-1) == -1);
109     ASSERT (errno == EBADF);
110   }
111   {
112     errno = 0;
113     ASSERT (set_nonblocking_flag (-1, false) == -1);
114     ASSERT (errno == EBADF);
115   }
116   {
117     errno = 0;
118     ASSERT (set_nonblocking_flag (-1, true) == -1);
119     ASSERT (errno == EBADF);
120   }
121   {
122     errno = 0;
123     ASSERT (set_nonblocking_flag (10000000, false) == -1);
124     ASSERT (errno == EBADF);
125   }
126
127   return 0;
128 }