Skip to content

Commit 64310e3

Browse files
committed
tests: port dispatch_io_net to Windows
This test needs to use WinSock on Windows.
1 parent 12e5dd5 commit 64310e3

File tree

2 files changed

+70
-36
lines changed

2 files changed

+70
-36
lines changed

tests/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ add_unit_test(dispatch_c99 NO_BSD_OVERLAY SOURCES dispatch_c99.c)
196196
add_unit_test(dispatch_plusplus SOURCES dispatch_plusplus.cpp)
197197

198198
# test-specific link options
199-
if(NOT WIN32)
199+
if(WIN32)
200+
target_link_libraries(dispatch_io_net PRIVATE WS2_32)
201+
else()
200202
target_link_libraries(dispatch_group PRIVATE m)
201203
target_link_libraries(dispatch_timer_short PRIVATE m)
202204
endif()

tests/dispatch_io_net.c

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@
2121
#include <stdio.h>
2222
#include <stdlib.h>
2323
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
24+
#include <netdb.h>
25+
#include <netinet/in.h>
26+
#include <spawn.h>
27+
#include <sys/socket.h>
28+
#include <sys/param.h>
2429
#include <unistd.h>
30+
#elif defined(_WIN32)
31+
#include <WinSock2.h>
32+
#include <WS2tcpip.h>
33+
#include <Windows.h>
2534
#endif
2635
#include <errno.h>
27-
#include <netdb.h>
2836
#include <sys/types.h>
29-
#include <sys/param.h>
30-
#include <netinet/in.h>
31-
#include <sys/socket.h>
3237
#include <fcntl.h>
3338
#include <sys/stat.h>
34-
#include <spawn.h>
3539
#ifdef __APPLE__
3640
#include <crt_externs.h>
3741
#include <mach-o/dyld.h>
@@ -41,30 +45,49 @@
4145
#include "dispatch_test.h"
4246
#include <dispatch/dispatch.h>
4347

48+
#if !defined(_WIN32)
4449
extern char **environ;
50+
#endif
4551

4652
#ifndef DISPATCHTEST_IO
4753
#if DISPATCH_API_VERSION >= 20100226 && DISPATCH_API_VERSION != 20101110
4854
#define DISPATCHTEST_IO 1
4955
#endif
5056
#endif
5157

52-
#if defined(__linux__) || defined(__FreeBSD__)
58+
#if defined(__linux__) || defined(__FreeBSD__) || defined(_WIN32)
5359
#define _NSGetExecutablePath(ef,bs) (*(bs)=(size_t)snprintf(ef,*(bs),"%s",argv[0]),0)
5460
#endif
5561

62+
#if defined(_WIN32)
63+
typedef USHORT in_port_t;
64+
#endif
65+
66+
#if !defined(_WIN32)
67+
#define closesocket(x) close(x)
68+
#endif
69+
5670
#if DISPATCHTEST_IO
5771
int
5872
main(int argc, char** argv)
5973
{
6074
struct hostent *he;
6175
int sockfd = -1, clientfd = -1;
62-
int read_fd = -1, fd = -1;
76+
dispatch_fd_t read_fd = -1, fd = -1;
6377
struct sockaddr_in addr1, addr2, server;
6478
socklen_t addr2len;
6579
socklen_t addr1len;
6680
pid_t clientid;
6781

82+
#if defined(_WIN32)
83+
WSADATA wsa;
84+
int err = WSAStartup(MAKEWORD(2, 2), &wsa);
85+
if (err != 0) {
86+
fprintf(stderr, "WSAStartup failed with %d\n", err);
87+
test_stop();
88+
}
89+
#endif
90+
6891
if (argc == 3) {
6992
// Client
7093
dispatch_test_start(NULL);
@@ -93,7 +116,7 @@ main(int argc, char** argv)
93116
// Read from the socket and compare the contents are what we expect
94117

95118
const char *path = argv[2];
96-
fd = open(path, O_RDONLY);
119+
fd = dispatch_test_fd_open(path, O_RDONLY);
97120
if (fd == -1) {
98121
test_errno("client-open", errno, 0);
99122
test_stop();
@@ -114,12 +137,8 @@ main(int argc, char** argv)
114137
// investigate what the impact of lack of file cache disabling has
115138
// for this test
116139
#endif
117-
struct stat sb;
118-
if (fstat(fd, &sb)) {
119-
test_errno("client-fstat", errno, 0);
120-
test_stop();
121-
}
122-
size_t size = (size_t)sb.st_size;
140+
size_t size = (size_t)dispatch_test_fd_lseek(fd, 0, SEEK_END);
141+
dispatch_test_fd_lseek(fd, 0, SEEK_SET);
123142

124143
__block dispatch_data_t g_d1 = dispatch_data_empty;
125144
__block dispatch_data_t g_d2 = dispatch_data_empty;
@@ -167,8 +186,8 @@ main(int argc, char** argv)
167186
memcmp(dict_contig_buf, socket_contig_buf,
168187
MIN(dict_contig_size, socket_contig_size)), 0);
169188

170-
close(fd);
171-
close(sockfd);
189+
dispatch_test_fd_close(fd);
190+
closesocket(sockfd);
172191
dispatch_release(g_d1);
173192
dispatch_release(g_d2);
174193
dispatch_release(dict_data);
@@ -223,7 +242,7 @@ main(int argc, char** argv)
223242
// unlink the file as soon as it can, so the server must open it before
224243
// starting the client process.
225244
char *path = dispatch_test_get_large_file();
226-
read_fd = open(path, O_RDONLY);
245+
read_fd = dispatch_test_fd_open(path, O_RDONLY);
227246
if (read_fd == -1) {
228247
test_errno("open", errno, 0);
229248
goto stop_test;
@@ -242,6 +261,23 @@ main(int argc, char** argv)
242261
test_errno("Server-posix_spawnp()", error, 0);
243262
goto stop_test;
244263
}
264+
#elif defined(_WIN32)
265+
WCHAR *cmdline = argv_to_command_line(arguments);
266+
if (!cmdline) {
267+
fprintf(stderr, "argv_to_command_line() failed\n");
268+
test_stop();
269+
}
270+
STARTUPINFOW si = {.cb = sizeof(si)};
271+
PROCESS_INFORMATION pi;
272+
BOOL created = CreateProcessW(NULL, cmdline, NULL, NULL, FALSE, 0, NULL,
273+
NULL, &si, &pi);
274+
DWORD error = GetLastError();
275+
free(cmdline);
276+
if (!created) {
277+
print_winapi_error("CreateProcessW", error);
278+
test_stop();
279+
}
280+
clientid = (pid_t)pi.dwProcessId;
245281
#elif defined(__unix__)
246282
clientid = fork();
247283
if (clientid == -1) {
@@ -275,12 +311,8 @@ main(int argc, char** argv)
275311
// investigate what the impact of lack of file cache disabling has
276312
// for this test
277313
#endif
278-
struct stat sb;
279-
if (fstat(read_fd, &sb)) {
280-
test_errno("fstat", errno, 0);
281-
goto stop_test;
282-
}
283-
size_t size = (size_t)sb.st_size;
314+
size_t size = (size_t)dispatch_test_fd_lseek(read_fd, 0, SEEK_END);
315+
dispatch_test_fd_lseek(read_fd, 0, SEEK_SET);
284316

285317
dispatch_group_t g = dispatch_group_create();
286318
dispatch_group_enter(g);
@@ -294,9 +326,9 @@ main(int argc, char** argv)
294326
// convenience method handlers should only be called once
295327
if (dispatch_data_get_size(d)!= size) {
296328
fprintf(stderr, "Reading of data didn't complete\n");
297-
close(read_fd);
298-
close(clientfd);
299-
close(sockfd);
329+
dispatch_test_fd_close(read_fd);
330+
closesocket(clientfd);
331+
closesocket(sockfd);
300332
test_stop();
301333
}
302334
dispatch_group_enter(g);
@@ -308,21 +340,21 @@ main(int argc, char** argv)
308340
if (remaining) {
309341
fprintf(stderr, "Server-dispatch_write() incomplete .. "
310342
"%zu bytes\n", dispatch_data_get_size(remaining));
311-
close(read_fd);
312-
close(clientfd);
313-
close(sockfd);
343+
dispatch_test_fd_close(read_fd);
344+
closesocket(clientfd);
345+
closesocket(sockfd);
314346
test_stop();
315347
}
316-
close(clientfd); // Sending the client EOF
348+
closesocket(clientfd); // Sending the client EOF
317349
dispatch_group_leave(g);
318350
});
319-
close(read_fd);
351+
dispatch_test_fd_close(read_fd);
320352
dispatch_group_leave(g);
321353
});
322354
test_group_wait(g);
323355
dispatch_release(g);
324356
fprintf(stderr, "Shutting down server\n");
325-
close(sockfd);
357+
closesocket(sockfd);
326358
free(path);
327359
test_stop();
328360

@@ -331,9 +363,9 @@ main(int argc, char** argv)
331363
dispatch_test_release_large_file(path);
332364
free(path);
333365
}
334-
close(read_fd);
335-
close(clientfd);
336-
close(sockfd);
366+
dispatch_test_fd_close(read_fd);
367+
closesocket(clientfd);
368+
closesocket(sockfd);
337369
test_stop();
338370
}
339371
}

0 commit comments

Comments
 (0)