21
21
#include <stdio.h>
22
22
#include <stdlib.h>
23
23
#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>
24
29
#include <unistd.h>
30
+ #elif defined(_WIN32 )
31
+ #include <WinSock2.h>
32
+ #include <WS2tcpip.h>
33
+ #include <Windows.h>
25
34
#endif
26
35
#include <errno.h>
27
- #include <netdb.h>
28
36
#include <sys/types.h>
29
- #include <sys/param.h>
30
- #include <netinet/in.h>
31
- #include <sys/socket.h>
32
37
#include <fcntl.h>
33
38
#include <sys/stat.h>
34
- #include <spawn.h>
35
39
#ifdef __APPLE__
36
40
#include <crt_externs.h>
37
41
#include <mach-o/dyld.h>
41
45
#include "dispatch_test.h"
42
46
#include <dispatch/dispatch.h>
43
47
48
+ #if !defined(_WIN32 )
44
49
extern char * * environ ;
50
+ #endif
45
51
46
52
#ifndef DISPATCHTEST_IO
47
53
#if DISPATCH_API_VERSION >= 20100226 && DISPATCH_API_VERSION != 20101110
48
54
#define DISPATCHTEST_IO 1
49
55
#endif
50
56
#endif
51
57
52
- #if defined(__linux__ ) || defined(__FreeBSD__ )
58
+ #if defined(__linux__ ) || defined(__FreeBSD__ ) || defined( _WIN32 )
53
59
#define _NSGetExecutablePath (ef ,bs ) (*(bs)=(size_t)snprintf(ef,*(bs),"%s",argv[0]),0)
54
60
#endif
55
61
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
+
56
70
#if DISPATCHTEST_IO
57
71
int
58
72
main (int argc , char * * argv )
59
73
{
60
74
struct hostent * he ;
61
75
int sockfd = -1 , clientfd = -1 ;
62
- int read_fd = -1 , fd = -1 ;
76
+ dispatch_fd_t read_fd = -1 , fd = -1 ;
63
77
struct sockaddr_in addr1 , addr2 , server ;
64
78
socklen_t addr2len ;
65
79
socklen_t addr1len ;
66
80
pid_t clientid ;
67
81
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
+
68
91
if (argc == 3 ) {
69
92
// Client
70
93
dispatch_test_start (NULL );
@@ -93,7 +116,7 @@ main(int argc, char** argv)
93
116
// Read from the socket and compare the contents are what we expect
94
117
95
118
const char * path = argv [2 ];
96
- fd = open (path , O_RDONLY );
119
+ fd = dispatch_test_fd_open (path , O_RDONLY );
97
120
if (fd == -1 ) {
98
121
test_errno ("client-open" , errno , 0 );
99
122
test_stop ();
@@ -114,12 +137,8 @@ main(int argc, char** argv)
114
137
// investigate what the impact of lack of file cache disabling has
115
138
// for this test
116
139
#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 );
123
142
124
143
__block dispatch_data_t g_d1 = dispatch_data_empty ;
125
144
__block dispatch_data_t g_d2 = dispatch_data_empty ;
@@ -167,8 +186,8 @@ main(int argc, char** argv)
167
186
memcmp (dict_contig_buf , socket_contig_buf ,
168
187
MIN (dict_contig_size , socket_contig_size )), 0 );
169
188
170
- close (fd );
171
- close (sockfd );
189
+ dispatch_test_fd_close (fd );
190
+ closesocket (sockfd );
172
191
dispatch_release (g_d1 );
173
192
dispatch_release (g_d2 );
174
193
dispatch_release (dict_data );
@@ -223,7 +242,7 @@ main(int argc, char** argv)
223
242
// unlink the file as soon as it can, so the server must open it before
224
243
// starting the client process.
225
244
char * path = dispatch_test_get_large_file ();
226
- read_fd = open (path , O_RDONLY );
245
+ read_fd = dispatch_test_fd_open (path , O_RDONLY );
227
246
if (read_fd == -1 ) {
228
247
test_errno ("open" , errno , 0 );
229
248
goto stop_test ;
@@ -242,6 +261,23 @@ main(int argc, char** argv)
242
261
test_errno ("Server-posix_spawnp()" , error , 0 );
243
262
goto stop_test ;
244
263
}
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 ;
245
281
#elif defined(__unix__ )
246
282
clientid = fork ();
247
283
if (clientid == -1 ) {
@@ -275,12 +311,8 @@ main(int argc, char** argv)
275
311
// investigate what the impact of lack of file cache disabling has
276
312
// for this test
277
313
#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 );
284
316
285
317
dispatch_group_t g = dispatch_group_create ();
286
318
dispatch_group_enter (g );
@@ -294,9 +326,9 @@ main(int argc, char** argv)
294
326
// convenience method handlers should only be called once
295
327
if (dispatch_data_get_size (d )!= size ) {
296
328
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 );
300
332
test_stop ();
301
333
}
302
334
dispatch_group_enter (g );
@@ -308,21 +340,21 @@ main(int argc, char** argv)
308
340
if (remaining ) {
309
341
fprintf (stderr , "Server-dispatch_write() incomplete .. "
310
342
"%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 );
314
346
test_stop ();
315
347
}
316
- close (clientfd ); // Sending the client EOF
348
+ closesocket (clientfd ); // Sending the client EOF
317
349
dispatch_group_leave (g );
318
350
});
319
- close (read_fd );
351
+ dispatch_test_fd_close (read_fd );
320
352
dispatch_group_leave (g );
321
353
});
322
354
test_group_wait (g );
323
355
dispatch_release (g );
324
356
fprintf (stderr , "Shutting down server\n" );
325
- close (sockfd );
357
+ closesocket (sockfd );
326
358
free (path );
327
359
test_stop ();
328
360
@@ -331,9 +363,9 @@ main(int argc, char** argv)
331
363
dispatch_test_release_large_file (path );
332
364
free (path );
333
365
}
334
- close (read_fd );
335
- close (clientfd );
336
- close (sockfd );
366
+ dispatch_test_fd_close (read_fd );
367
+ closesocket (clientfd );
368
+ closesocket (sockfd );
337
369
test_stop ();
338
370
}
339
371
}
0 commit comments