usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-nonblocking-socket-main.c
blobf59cf445dac6dbb6bd31454468366be030f35f76
1 /* Test for nonblocking read and write on sockets.
3 Copyright (C) 2011-2024 Free Software Foundation, Inc.
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 3 of the License, or
8 (at your option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/time.h>
25 #include <sys/socket.h>
27 #if defined _WIN32 && ! defined __CYGWIN__
28 # include <process.h>
29 #else
30 # include <spawn.h>
31 #endif
33 #include "nonblocking.h"
34 #include "wait-process.h"
36 #include "macros.h"
37 #include "socket-server.h"
38 #include "test-nonblocking-socket.h"
39 #define PROG_ROLE "main"
40 #include "test-nonblocking-writer.h"
42 int
43 main (int argc, char *argv[])
45 const char *child_path;
46 int test;
47 int server;
48 int port;
49 pid_t child;
50 int server_socket;
51 int exitcode;
53 child_path = argv[1];
54 test = atoi (argv[2]);
56 /* Create a server socket. */
57 server = create_server (0, 1, &port);
59 /* Spawn the child process. */
61 char port_arg[10+1];
62 const char *child_argv[4];
64 sprintf (port_arg, "%u", port);
65 child_argv[0] = child_path;
66 child_argv[1] = argv[2];
67 child_argv[2] = port_arg;
68 child_argv[3] = NULL;
70 #if defined _WIN32 && ! defined __CYGWIN__
71 child = _spawnvpe (P_NOWAIT, child_path, child_argv,
72 (const char **) environ);
73 ASSERT (child >= 0);
74 #else
76 pid_t child_pid;
77 int err =
78 posix_spawnp (&child_pid, child_path, NULL, NULL, (char **) child_argv,
79 environ);
80 ASSERT (err == 0);
81 child = child_pid;
83 #endif
86 /* Accept a connection from the child process. */
87 server_socket = create_server_socket (server);
89 /* Prepare the file descriptor. */
90 if (test & 1)
91 ASSERT (set_nonblocking_flag (server_socket, true) >= 0);
93 #if ENABLE_DEBUGGING
94 # ifdef SO_SNDBUF
96 int value;
97 socklen_t value_len = sizeof (value);
98 if (getsockopt (server_socket, SOL_SOCKET, SO_SNDBUF, &value, &value_len) >= 0)
99 fprintf (stderr, "SO_SNDBUF = %d\n", value);
101 # endif
102 # ifdef SO_RCVBUF
104 int value;
105 socklen_t value_len = sizeof (value);
106 if (getsockopt (server_socket, SOL_SOCKET, SO_RCVBUF, &value, &value_len) >= 0)
107 fprintf (stderr, "SO_RCVBUF = %d\n", value);
109 # endif
110 #endif
112 exitcode =
113 main_writer_loop (test, SOCKET_DATA_BLOCK_SIZE, server_socket,
114 SOCKET_HAS_LARGE_BUFFER);
117 int err =
118 wait_subprocess (child, child_path, false, false, false, false, NULL);
119 ASSERT (err == 0);
122 return (exitcode ? exitcode : test_exit_status);