This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37 fd_file = creat (file, 0600);
38
39
40 ASSERT (STDERR_FILENO < fd_file);
41
42
43 ASSERT (get_nonblocking_flag (fd_file) == 0);
44 ASSERT (set_nonblocking_flag (fd_file, false) == 0);
45 ASSERT (get_nonblocking_flag (fd_file) == 0);
46 ASSERT (close (fd_file) == 0);
47 ASSERT (unlink (file) == 0);
48
49
50 fd_file = open (".", O_RDONLY);
51 if (STDERR_FILENO < fd_file)
52 {
53
54 ASSERT (get_nonblocking_flag (fd_file) == 0);
55 ASSERT (set_nonblocking_flag (fd_file, false) == 0);
56 ASSERT (get_nonblocking_flag (fd_file) == 0);
57 ASSERT (close (fd_file) == 0);
58 }
59
60
61 ASSERT (pipe (fd_pipe) == 0);
62 ASSERT (get_nonblocking_flag (fd_pipe[0]) == 0);
63 ASSERT (get_nonblocking_flag (fd_pipe[1]) == 0);
64 ASSERT (set_nonblocking_flag (fd_pipe[0], true) == 0);
65 ASSERT (get_nonblocking_flag (fd_pipe[0]) == 1);
66 ASSERT (get_nonblocking_flag (fd_pipe[1]) == 0);
67 ASSERT (set_nonblocking_flag (fd_pipe[1], true) == 0);
68 ASSERT (set_nonblocking_flag (fd_pipe[0], false) == 0);
69 ASSERT (get_nonblocking_flag (fd_pipe[0]) == 0);
70 ASSERT (get_nonblocking_flag (fd_pipe[1]) == 1);
71 ASSERT (close (fd_pipe[0]) == 0);
72 ASSERT (close (fd_pipe[1]) == 0);
73
74 #if GNULIB_TEST_PIPE2
75 ASSERT (pipe2 (fd_pipe, O_NONBLOCK) == 0);
76 ASSERT (get_nonblocking_flag (fd_pipe[0]) == 1);
77 ASSERT (get_nonblocking_flag (fd_pipe[1]) == 1);
78 ASSERT (close (fd_pipe[0]) == 0);
79 ASSERT (close (fd_pipe[1]) == 0);
80 #endif
81
82 #if GNULIB_TEST_SOCKET
83 {
84
85 bool sock_works = true;
86 int fd_sock;
87
88 # if defined _WIN32 && ! defined __CYGWIN__
89
90 sock_works = false;
91 # endif
92
93 fd_sock = socket (AF_INET, SOCK_STREAM, 0);
94 ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 0 : -1));
95 ASSERT (set_nonblocking_flag (fd_sock, true) == 0);
96 ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 1 : -1));
97 ASSERT (set_nonblocking_flag (fd_sock, false) == 0);
98 ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 0 : -1));
99 ASSERT (close (fd_sock) == 0);
100
101 # if SOCK_NONBLOCK
102 fd_sock = socket (AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
103 ASSERT (get_nonblocking_flag (fd_sock) == (sock_works ? 1 : -1));
104 ASSERT (close (fd_sock) == 0);
105 # endif
106 }
107 #endif
108
109
110 {
111 errno = 0;
112 ASSERT (get_nonblocking_flag (-1) == -1);
113 ASSERT (errno == EBADF);
114 }
115 {
116 errno = 0;
117 ASSERT (set_nonblocking_flag (-1, false) == -1);
118 ASSERT (errno == EBADF);
119 }
120 {
121 errno = 0;
122 ASSERT (set_nonblocking_flag (-1, true) == -1);
123 ASSERT (errno == EBADF);
124 }
125 {
126 errno = 0;
127 ASSERT (set_nonblocking_flag (getdtablesize (), false) == -1);
128 ASSERT (errno == EBADF);
129 }
130
131 return 0;
132 }