33 cs130_tom 1.1 #define FIRST_CHAR 'A' /* First character in transferred pattern */
34 #define BIND_SLEEP 10 /* seconds to wait between attempts to bind() */
35 #define BIND_TRIES 6 /* Number of bind() attempts */
36 #define TEST_TIMEOUT 30 /* seconds to wait before killing child threads
37 after server initialization, if something hangs */
38
39 #define wsa_ok(op, cond, msg) \
40 do { \
41 int tmp, err = 0; \
42 tmp = op; \
43 if ( !(cond tmp) ) err = WSAGetLastError(); \
44 ok ( cond tmp, msg, GetCurrentThreadId(), err); \
45 } while (0);
46
47
48 /**************** Structs and typedefs ***************/
49
50 typedef struct thread_info
51 {
52 HANDLE thread;
53 DWORD id;
54 cs130_tom 1.1 } thread_info;
55
56 /* Information in the server about open client connections */
57 typedef struct sock_info
58 {
59 SOCKET s;
60 struct sockaddr_in addr;
61 struct sockaddr_in peer;
62 char *buf;
63 int nread;
64 } sock_info;
65
66 /* Test parameters for both server & client */
67 typedef struct test_params
68 {
69 int sock_type;
70 int sock_prot;
71 const char *inet_addr;
72 short inet_port;
73 int chunk_size;
74 int n_chunks;
75 cs130_tom 1.1 int n_clients;
76 } test_params;
77
78 /* server-specific test parameters */
79 typedef struct server_params
80 {
81 test_params *general;
82 DWORD sock_flags;
83 int buflen;
84 } server_params;
85
86 /* client-specific test parameters */
87 typedef struct client_params
88 {
89 test_params *general;
90 DWORD sock_flags;
91 int buflen;
92 } client_params;
93
94 /* This type combines all information for setting up a test scenario */
95 typedef struct test_setup
96 cs130_tom 1.1 {
97 test_params general;
98 LPVOID srv;
99 server_params srv_params;
100 LPVOID clt;
101 client_params clt_params;
102 } test_setup;
103
104 /* Thread local storage for server */
105 typedef struct server_memory
106 {
107 SOCKET s;
108 struct sockaddr_in addr;
109 sock_info sock[MAX_CLIENTS];
110 } server_memory;
111
112 /* Thread local storage for client */
113 typedef struct client_memory
114 {
115 SOCKET s;
116 struct sockaddr_in addr;
117 cs130_tom 1.1 char *send_buf;
118 char *recv_buf;
119 } client_memory;
120
121 /**************** Static variables ***************/
122
123 static DWORD tls; /* Thread local storage index */
124 static HANDLE thread[1+MAX_CLIENTS];
125 static DWORD thread_id[1+MAX_CLIENTS];
126 static HANDLE server_ready;
127 static HANDLE client_ready[MAX_CLIENTS];
128 static int client_id;
129
130 /**************** General utility functions ***************/
131
132 static void set_so_opentype ( BOOL overlapped )
133 {
134 int optval = !overlapped, newval, len = sizeof (int);
135
136 ok ( setsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
137 (LPVOID) &optval, sizeof (optval) ) == 0,
138 cs130_tom 1.1 "setting SO_OPENTYPE failed\n" );
139 ok ( getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
140 (LPVOID) &newval, &len ) == 0,
141 "getting SO_OPENTYPE failed\n" );
142 ok ( optval == newval, "failed to set SO_OPENTYPE\n" );
143 }
144
145 static int set_blocking ( SOCKET s, BOOL blocking )
146 {
147 u_long val = !blocking;
148 return ioctlsocket ( s, FIONBIO, &val );
149 }
150
151 static void fill_buffer ( char *buf, int chunk_size, int n_chunks )
152 {
153 char c, *p;
154 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
155 memset ( p, c, chunk_size );
156 }
157
158 static char* test_buffer ( char *buf, int chunk_size, int n_chunks )
159 cs130_tom 1.1 {
160 char c, *p;
161 int i;
162 for ( c = FIRST_CHAR, p = buf; c < FIRST_CHAR + n_chunks; c++, p += chunk_size )
163 {
164 for ( i = 0; i < chunk_size; i++ )
165 if ( p[i] != c ) return p + i;
166 }
167 return NULL;
168 }
169
170 /*
171 * This routine is called when a client / server does not expect any more data,
172 * but needs to acknowedge the closing of the connection (by reasing 0 bytes).
173 */
174 static void read_zero_bytes ( SOCKET s )
175 {
176 char buf[256];
177 int tmp, n = 0;
178 while ( ( tmp = recv ( s, buf, 256, 0 ) ) > 0 )
179 n += tmp;
180 cs130_tom 1.1 ok ( n <= 0, "garbage data received: %d bytes\n", n );
181 }
182
183 static int do_synchronous_send ( SOCKET s, char *buf, int buflen, int sendlen )
184 {
185 char* last = buf + buflen, *p;
186 int n = 1;
187 for ( p = buf; n > 0 && p < last; p += n )
188 n = send ( s, p, min ( sendlen, last - p ), 0 );
189 wsa_ok ( n, 0 <=, "do_synchronous_send (%lx): error %d\n" );
190 return p - buf;
191 }
192
193 static int do_synchronous_recv ( SOCKET s, char *buf, int buflen, int recvlen )
194 {
195 char* last = buf + buflen, *p;
196 int n = 1;
197 for ( p = buf; n > 0 && p < last; p += n )
198 n = recv ( s, p, min ( recvlen, last - p ), 0 );
199 wsa_ok ( n, 0 <=, "do_synchronous_recv (%lx): error %d:\n" );
200 return p - buf;
201 cs130_tom 1.1 }
202
|
235 cs130_tom 1.1 /*
236 * Call this routine right after thread startup.
237 * SO_OPENTYPE must by 0, regardless what the server did.
238 */
239 static void check_so_opentype (void)
240 {
241 int tmp = 1, len;
242 len = sizeof (tmp);
243 getsockopt ( INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (LPVOID) &tmp, &len );
244 ok ( tmp == 0, "check_so_opentype: wrong startup value of SO_OPENTYPE: %d\n", tmp );
245 }
246
247 /**************** Server utility functions ***************/
248
249 /*
250 * Even if we have closed our server socket cleanly,
251 * the OS may mark the address "in use" for some time -
252 * this happens with native Linux apps, too.
253 */
254 static void do_bind ( SOCKET s, struct sockaddr* addr, int addrlen )
255 {
256 cs130_tom 1.1 int err, wsaerr = 0, n_try = BIND_TRIES;
257
258 while ( ( err = bind ( s, addr, addrlen ) ) != 0 &&
259 ( wsaerr = WSAGetLastError () ) == WSAEADDRINUSE &&
260 n_try-- >= 0)
261 {
262 trace ( "address in use, waiting ...\n" );
263 Sleep ( 1000 * BIND_SLEEP );
264 }
265 ok ( err == 0, "failed to bind: %d\n", wsaerr );
266 }
267
268 static void server_start ( server_params *par )
269 {
270 int i;
271 test_params *gen = par->general;
272 server_memory *mem = (LPVOID) LocalAlloc ( LPTR, sizeof (server_memory));
273
274 TlsSetValue ( tls, mem );
275 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
276 NULL, 0, par->sock_flags );
277 cs130_tom 1.1 ok ( mem->s != INVALID_SOCKET, "Server: WSASocket failed\n" );
278
279 mem->addr.sin_family = AF_INET;
280 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
281 mem->addr.sin_port = htons ( gen->inet_port );
282
283 for (i = 0; i < MAX_CLIENTS; i++)
284 {
285 mem->sock[i].s = INVALID_SOCKET;
286 mem->sock[i].buf = (LPVOID) LocalAlloc ( LPTR, gen->n_chunks * gen->chunk_size );
287 mem->sock[i].nread = 0;
288 }
289
290 if ( gen->sock_type == SOCK_STREAM )
291 do_bind ( mem->s, (struct sockaddr*) &mem->addr, sizeof (mem->addr) );
292 }
293
294 static void server_stop (void)
295 {
296 int i;
297 server_memory *mem = TlsGetValue ( tls );
298 cs130_tom 1.1
299 for (i = 0; i < MAX_CLIENTS; i++ )
300 {
301 LocalFree ( (HANDLE) mem->sock[i].buf );
302 if ( mem->sock[i].s != INVALID_SOCKET )
303 closesocket ( mem->sock[i].s );
304 }
305 ok ( closesocket ( mem->s ) == 0, "closesocket failed\n" );
306 LocalFree ( (HANDLE) mem );
307 ExitThread ( GetCurrentThreadId () );
308 }
309
310 /**************** Client utilitiy functions ***************/
311
312 static void client_start ( client_params *par )
313 {
314 test_params *gen = par->general;
315 client_memory *mem = (LPVOID) LocalAlloc (LPTR, sizeof (client_memory));
316
317 TlsSetValue ( tls, mem );
318
319 cs130_tom 1.1 WaitForSingleObject ( server_ready, INFINITE );
320
321 mem->s = WSASocketA ( AF_INET, gen->sock_type, gen->sock_prot,
322 NULL, 0, par->sock_flags );
323
324 mem->addr.sin_family = AF_INET;
325 mem->addr.sin_addr.s_addr = inet_addr ( gen->inet_addr );
326 mem->addr.sin_port = htons ( gen->inet_port );
327
328 ok ( mem->s != INVALID_SOCKET, "Client: WSASocket failed\n" );
329
330 mem->send_buf = (LPVOID) LocalAlloc ( LPTR, 2 * gen->n_chunks * gen->chunk_size );
331 mem->recv_buf = mem->send_buf + gen->n_chunks * gen->chunk_size;
332 fill_buffer ( mem->send_buf, gen->chunk_size, gen->n_chunks );
333
334 SetEvent ( client_ready[client_id] );
335 /* Wait for the other clients to come up */
336 WaitForMultipleObjects ( min ( gen->n_clients, MAX_CLIENTS ), client_ready, TRUE, INFINITE );
337 }
338
339 static void client_stop (void)
340 cs130_tom 1.1 {
341 client_memory *mem = TlsGetValue ( tls );
342 wsa_ok ( closesocket ( mem->s ), 0 ==, "closesocket error (%lx): %d\n" );
343 LocalFree ( (HANDLE) mem->send_buf );
344 LocalFree ( (HANDLE) mem );
345 ExitThread(0);
346 }
347
348 /**************** Servers ***************/
349
350 /*
351 * simple_server: A very basic server doing synchronous IO.
352 */
353 static VOID WINAPI simple_server ( server_params *par )
354 {
355 test_params *gen = par->general;
356 server_memory *mem;
357 int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
358 id = GetCurrentThreadId();
359 char *p;
360
361 cs130_tom 1.1 trace ( "simple_server (%x) starting\n", id );
362
363 set_so_opentype ( FALSE ); /* non-overlapped */
364 server_start ( par );
365 mem = TlsGetValue ( tls );
366
367 wsa_ok ( set_blocking ( mem->s, TRUE ), 0 ==, "simple_server (%lx): failed to set blocking mode: %d\n");
368 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "simple_server (%lx): listen failed: %d\n");
369
370 trace ( "simple_server (%x) ready\n", id );
371 SetEvent ( server_ready ); /* notify clients */
372
373 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
374 {
375 trace ( "simple_server (%x): waiting for client\n", id );
376
377 /* accept a single connection */
378 tmp = sizeof ( mem->sock[0].peer );
379 mem->sock[0].s = accept ( mem->s, (struct sockaddr*) &mem->sock[0].peer, &tmp );
380 wsa_ok ( mem->sock[0].s, INVALID_SOCKET !=, "simple_server (%lx): accept failed: %d\n" );
381
382 cs130_tom 1.1 ok ( mem->sock[0].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
383 "simple_server (%x): strange peer address\n", id );
384
385 /* Receive data & check it */
386 n_recvd = do_synchronous_recv ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
387 ok ( n_recvd == n_expected,
388 "simple_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
389 p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
390 ok ( p == NULL, "simple_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
391
392 /* Echo data back */
393 n_sent = do_synchronous_send ( mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
394 ok ( n_sent == n_expected,
395 "simple_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
396
397 /* cleanup */
398 read_zero_bytes ( mem->sock[0].s );
399 wsa_ok ( closesocket ( mem->sock[0].s ), 0 ==, "simple_server (%lx): closesocket error: %d\n" );
400 mem->sock[0].s = INVALID_SOCKET;
401 }
402
403 cs130_tom 1.1 trace ( "simple_server (%x) exiting\n", id );
404 server_stop ();
405 }
406
|
407 cs130_tom 1.2 /*
408 * select_server: A non-blocking server.
409 */
410 static VOID WINAPI select_server ( server_params *par )
411 {
412 test_params *gen = par->general;
413 server_memory *mem;
414 int n_recvd, n_sent, n_expected = gen->n_chunks * gen->chunk_size, tmp, i,
415 id = GetCurrentThreadId(), n_connections = 0;
416 char *p;
417 struct timeval zerotime = {0,0};
418 fd_set fds_read, fds_write, fds_open;
419
420 trace ( "select_server (%x) starting\n", id );
421
422 set_so_opentype ( FALSE ); /* non-overlapped */
423 server_start ( par );
424 mem = TlsGetValue ( tls );
425
426 wsa_ok ( set_blocking ( mem->s, FALSE ), 0 ==, "select_server (%lx): failed to set blocking mode: %d\n");
427 wsa_ok ( listen ( mem->s, SOMAXCONN ), 0 ==, "select_server (%lx): listen failed: %d\n");
428 cs130_tom 1.2
429 trace ( "select_server (%x) ready\n", id );
430 SetEvent ( server_ready ); /* notify clients */
431
432 FD_ZERO ( &fds_openread );
433 FD_ZERO ( &fds_read );
434 FD_ZERO ( &fds_write );
435
436 FD_SET ( mem->s, &fds_open );
437
438 while(1)
439 {
440 fds_read = fds_write = fds_open;
441
442 select ( 0, &fds_read, &fds_write, NULL, &zerotime );
443
444 /* check for incoming requests */
445 if ( FD_ISSET ( mem->s, &fds_read ) ) {
446 trace ( "select_server (%x): accepting client connection\n", id );
447
448 /* accept a single connection */
449 cs130_tom 1.2 tmp = sizeof ( mem->sock[i].peer );
450 mem->sock[i].s = accept ( mem->s, (struct sockaddr*) &mem->sock[i].peer, &tmp );
451 wsa_ok ( mem->sock[i].s, INVALID_SOCKET !=, "select_server (%lx): accept failed: %d\n" );
452
453 ok ( mem->sock[i].peer.sin_addr.s_addr == inet_addr ( gen->inet_addr ),
454 "select_server (%x): strange peer address\n", id );
455
456 /* add to list of open connections */
457 FD_SET ( mem->sock[i].s, &fds_open );
458
459 n_connections += 1;
460 }
461
462 /* handle open requests */
463
464 for ( i = 0; i < n_connections; i++ )
465 {
466 if ( (mem->sock[i].nread < n_expected)
467 && FD_ISSET( mem->sock[i].s, &fds_read ) ) {
468 /* more data to receive */
469
470 cs130_tom 1.2 /* Receive data & check it */
471 n_recvd = do_nonblocking_recv ( i, mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
472 //ok ( n_recvd == n_expected,
473 // "select_server (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
474
475 if ( n_recvd == n_expected ) {
476 p = test_buffer ( mem->sock[0].buf, gen->chunk_size, gen->n_chunks );
477 ok ( p == NULL, "select_server (%x): test pattern error: %d\n", id, p - mem->sock[0].buf);
478 }
479
480 } else if ( mem->sock[i].nread == n_expected
481 && FD_ISSET ( mem->sock[i].s, &fds_write ) ) {
482 /* send data */
483
484 /* Echo data back */
485 n_sent = do_nonblocking_send ( i, mem->sock[0].s, mem->sock[0].buf, n_expected, par->buflen );
486 ok ( n_sent == n_expected,
487 "select_server (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
488
489
490
491 cs130_tom 1.2 } else {
492 ok ( 0, "select_server (%x): too many bytes read\n", id );
493 }
494 }
495 }
496
497 for ( i = 0; i < min ( gen->n_clients, MAX_CLIENTS ); i++ )
498 {
499 /* cleanup */
500 read_zero_bytes ( mem->sock[i].s );
501 wsa_ok ( closesocket ( mem->sock[i].s ), 0 ==, "select_server (%lx): closesocket error: %d\n" );
502 mem->sock[i].s = INVALID_SOCKET;
503 }
504
505 trace ( "select_server (%x) exiting\n", id );
506 server_stop ();
507 }
508
|
509 cs130_tom 1.1 /**************** Clients ***************/
510
511 /*
512 * simple_client: A very basic client doing synchronous IO.
513 */
514 static VOID WINAPI simple_client ( client_params *par )
515 {
516 test_params *gen = par->general;
517 client_memory *mem;
518 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
519 char *p;
520
521 id = GetCurrentThreadId();
522 trace ( "simple_client (%x): starting\n", id );
523 /* wait here because we want to call set_so_opentype before creating a socket */
524 WaitForSingleObject ( server_ready, INFINITE );
525 trace ( "simple_client (%x): server ready\n", id );
526
527 check_so_opentype ();
528 set_so_opentype ( FALSE ); /* non-overlapped */
529 client_start ( par );
530 cs130_tom 1.1 mem = TlsGetValue ( tls );
531
532 /* Connect */
533 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
534 0 ==, "simple_client (%lx): connect error: %d\n" );
535 ok ( set_blocking ( mem->s, TRUE ) == 0,
536 "simple_client (%x): failed to set blocking mode\n", id );
537 trace ( "simple_client (%x) connected\n", id );
538
539 /* send data to server */
540 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
541 ok ( n_sent == n_expected,
542 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
543
544 /* shutdown send direction */
545 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%lx): shutdown failed: %d\n" );
546
547 /* Receive data echoed back & check it */
548 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
549 ok ( n_recvd == n_expected,
550 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
551 cs130_tom 1.1
552 /* check data */
553 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
554 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
555
556 /* cleanup */
557 read_zero_bytes ( mem->s );
558 trace ( "simple_client (%x) exiting\n", id );
559 client_stop ();
560 }
561
562 /*
563 * event_client: An event-driven client
564 */
565 static void WINAPI event_client ( client_params *par )
566 {
567 test_params *gen = par->general;
568 client_memory *mem;
569 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
570 tmp, err, n;
571 HANDLE event;
572 cs130_tom 1.1 WSANETWORKEVENTS wsa_events;
573 char *send_last, *recv_last, *send_p, *recv_p;
574 long mask = FD_READ | FD_WRITE | FD_CLOSE;
575
576 trace ( "event_client (%x): starting\n", id );
577 client_start ( par );
578 trace ( "event_client (%x): server ready\n", id );
579
580 mem = TlsGetValue ( tls );
581
582 /* Prepare event notification for connect, makes socket nonblocking */
583 event = WSACreateEvent ();
584 WSAEventSelect ( mem->s, event, FD_CONNECT );
585 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
586 if ( tmp != 0 && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
587 ok ( 0, "event_client (%x): connect error: %d\n", id, err );
588
589 tmp = WaitForSingleObject ( event, INFINITE );
590 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
591 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
592 wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
593 cs130_tom 1.1
594 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
595 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
596 if ( err ) goto out;
597
598 trace ( "event_client (%x) connected\n", id );
599
600 WSAEventSelect ( mem->s, event, mask );
601
602 recv_p = mem->recv_buf;
603 recv_last = mem->recv_buf + n_expected;
604 send_p = mem->send_buf;
605 send_last = mem->send_buf + n_expected;
606
607 while ( TRUE )
608 {
609 err = WaitForSingleObject ( event, INFINITE );
610 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
611
612 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
613 wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
614 cs130_tom 1.1
615 if ( wsa_events.lNetworkEvents & FD_WRITE )
616 {
617 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
618 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
619
620 if ( err== 0 )
621 do
622 {
623 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
624 if ( n < 0 )
625 {
626 err = WSAGetLastError ();
627 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
628 }
629 else
630 send_p += n;
631 }
632 while ( n >= 0 && send_p < send_last );
633
634 if ( send_p == send_last )
635 cs130_tom 1.1 {
636 trace ( "event_client (%x): all data sent - shutdown\n", id );
637 shutdown ( mem->s, SD_SEND );
638 mask &= ~FD_WRITE;
639 WSAEventSelect ( mem->s, event, mask );
640 }
641 }
642 if ( wsa_events.lNetworkEvents & FD_READ )
643 {
644 err = wsa_events.iErrorCode[ FD_READ_BIT ];
645 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
646 if ( err != 0 ) break;
647
648 /* First read must succeed */
649 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
650 wsa_ok ( n, 0 <=, "event_client (%lx): recv error: %d\n" );
651
652 while ( n >= 0 ) {
653 recv_p += n;
654 if ( recv_p == recv_last )
655 {
656 cs130_tom 1.1 mask &= ~FD_READ;
657 trace ( "event_client (%x): all data received\n", id );
658 WSAEventSelect ( mem->s, event, mask );
659 break;
660 }
661 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
662 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
663 ok ( 0, "event_client (%x): read error: %d\n", id, err );
664
665 }
666 }
667 if ( wsa_events.lNetworkEvents & FD_CLOSE )
668 {
669 trace ( "event_client (%x): close event\n", id );
670 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
671 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
672 break;
673 }
674 }
675
676 ok ( send_p == send_last,
677 cs130_tom 1.1 "simple_client (%x): sent less data than expected: %d of %d\n",
678 id, send_p - mem->send_buf, n_expected );
679 ok ( recv_p == recv_last,
680 "simple_client (%x): received less data than expected: %d of %d\n",
681 id, recv_p - mem->recv_buf, n_expected );
682 recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
683 ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
684
685 out:
686 WSACloseEvent ( event );
687 trace ( "event_client (%x) exiting\n", id );
688 client_stop ();
689 }
690
691 /**************** Main program utility functions ***************/
692
693 static void Init (void)
694 {
695 WORD ver = MAKEWORD (2, 2);
696 WSADATA data;
697
698 cs130_tom 1.1 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
699 tls = TlsAlloc();
700 }
701
702 static void Exit (void)
703 {
704 TlsFree ( tls );
705 ok ( WSACleanup() == 0, "WSACleanup failed\n" );
706 }
707
708 static void StartServer (LPTHREAD_START_ROUTINE routine,
709 test_params *general, server_params *par)
710 {
711 par->general = general;
712 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
713 ok ( thread[0] != NULL, "Failed to create server thread\n" );
714 }
715
716 static void StartClients (LPTHREAD_START_ROUTINE routine,
717 test_params *general, client_params *par)
718 {
719 cs130_tom 1.1 int i;
720 par->general = general;
721 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
722 {
723 client_id = i - 1;
724 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
725 ok ( thread[i] != NULL, "Failed to create client thread\n" );
726 /* Make sure the client is up and running */
727 WaitForSingleObject ( client_ready[client_id], INFINITE );
728 };
729 }
730
731 static void do_test( test_setup *test )
732 {
733 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
734 DWORD wait;
735
736 server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
737 for (i = 0; i <= n; i++)
738 client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
739
740 cs130_tom 1.1 StartServer ( test->srv, &test->general, &test->srv_params );
741 StartClients ( test->clt, &test->general, &test->clt_params );
742 WaitForSingleObject ( server_ready, INFINITE );
743
744 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
745 ok ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ,
746 "some threads have not completed: %lx\n", wait );
747
748 if ( ! ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ) )
749 {
750 for (i = 0; i <= n; i++)
751 {
752 trace ("terminating thread %08lx\n", thread_id[i]);
753 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
754 TerminateThread ( thread [i], 0 );
755 }
756 }
757 CloseHandle ( server_ready );
758 for (i = 0; i <= n; i++)
759 CloseHandle ( client_ready[i] );
760 }
761 cs130_tom 1.1
762 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
763 /* optname = SO_LINGER */
764 LINGER linger_testvals[] = {
765 {0,0},
766 {0,73},
767 {1,0},
768 {5,189}
769 };
770
771 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
772 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
773 bug in the linux kernel (fixed in 2.6.8) */
774 #define SOCKTIMEOUT2 997000 /* 997 seconds */
775
776 static void test_set_getsockopt()
777 {
778 SOCKET s;
779 int i, err;
780 int timeout;
781 LINGER lingval;
782 cs130_tom 1.1 int size;
783
784 s = socket(AF_INET, SOCK_STREAM, 0);
785 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
786 if( s == INVALID_SOCKET) return;
787 /* SO_RCVTIMEO */
788 timeout = SOCKTIMEOUT1;
789 size = sizeof(timeout);
790 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
791 if( !err)
792 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
793 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
794 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
795 /* SO_SNDTIMEO */
796 timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */
797 size = sizeof(timeout);
798 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
799 if( !err)
800 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
801 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
802 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
803 cs130_tom 1.1 /* SO_LINGER */
804 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
805 size = sizeof(lingval);
806 lingval = linger_testvals[i];
807 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
808 if( !err)
809 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
810 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
811 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
812 (lingval.l_linger == linger_testvals[i].l_linger ||
813 (!lingval.l_linger && !linger_testvals[i].l_onoff))
814 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d \n", i,
815 lingval.l_onoff, lingval.l_linger,
816 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
817 }
818 closesocket(s);
819 }
820
821 static void test_so_reuseaddr()
822 {
823 struct sockaddr_in saddr;
824 cs130_tom 1.1 SOCKET s1,s2;
825 unsigned int rc,reuse,size;
826
827 saddr.sin_family = AF_INET;
828 saddr.sin_port = htons(9375);
829 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
830
831 s1=socket(AF_INET, SOCK_STREAM, 0);
832 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
833 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
834 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
835
836 s2=socket(AF_INET, SOCK_STREAM, 0);
837 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
838
839 reuse=0x1234;
840 size=sizeof(reuse);
841 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
842 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
843
844 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
845 cs130_tom 1.1 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
846
847 reuse = 1;
848 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
849 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
850
851 todo_wine {
852 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
853 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
854 }
855
856 closesocket(s2);
857 closesocket(s1);
858 }
859
860 /************* Array containing the tests to run **********/
861
862 #define STD_STREAM_SOCKET \
863 SOCK_STREAM, \
864 0, \
865 "127.0.0.1", \
866 cs130_tom 1.1 9374
867
868 static test_setup tests [NUM_TESTS] =
869 {
870 /* Test 0: synchronous client and server */
871 {
872 {
873 STD_STREAM_SOCKET,
874 2048,
875 16,
876 2
877 },
878 simple_server,
879 {
880 NULL,
881 0,
882 64
883 },
884 simple_client,
885 {
886 NULL,
887 cs130_tom 1.1 0,
888 128
889 }
890 },
891 /* Test 1: event-driven client, synchronous server */
892 {
893 {
894 STD_STREAM_SOCKET,
895 2048,
896 16,
897 2
898 },
899 simple_server,
|
921 cs130_tom 1.1 {
922 NULL,
923 0,
924 64
925 },
926 event_client,
927 {
928 NULL,
929 WSA_FLAG_OVERLAPPED,
930 128
931 }
932 }
933 };
934
935 static void test_WSAAddressToStringA()
936 {
937 INT ret;
938 DWORD len;
939 SOCKADDR_IN sockaddr;
940 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
941
942 cs130_tom 1.1 CHAR expect1[] = "0.0.0.0";
943 CHAR expect2[] = "255.255.255.255";
944 CHAR expect3[] = "0.0.0.0:65535";
945 CHAR expect4[] = "255.255.255.255:65535";
946
947 len = 0;
948
949 sockaddr.sin_family = AF_INET;
950 sockaddr.sin_port = 0;
951 sockaddr.sin_addr.s_addr = 0;
952
953 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
954 ok( ret == SOCKET_ERROR, "WSAAddressToStringA() succeeded unexpectedly: %d\n", WSAGetLastError() );
955
956 len = sizeof(address);
957
958 sockaddr.sin_family = AF_INET;
959 sockaddr.sin_port = 0;
960 sockaddr.sin_addr.s_addr = 0;
961
962 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
963 cs130_tom 1.1 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
964
965 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
966
967 len = sizeof(address);
968
969 sockaddr.sin_family = AF_INET;
970 sockaddr.sin_port = 0;
971 sockaddr.sin_addr.s_addr = 0xffffffff;
972
973 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
974 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
975
976 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
977
978 len = sizeof(address);
979
980 sockaddr.sin_family = AF_INET;
981 sockaddr.sin_port = 0xffff;
982 sockaddr.sin_addr.s_addr = 0;
983
984 cs130_tom 1.1 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
985 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
986
987 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
988
989 len = sizeof(address);
990
991 sockaddr.sin_family = AF_INET;
992 sockaddr.sin_port = 0xffff;
993 sockaddr.sin_addr.s_addr = 0xffffffff;
994
995 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
996 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
997
998 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
999 }
1000
1001 static void test_WSAAddressToStringW()
1002 {
1003 INT ret;
1004 DWORD len;
1005 cs130_tom 1.1 SOCKADDR_IN sockaddr;
1006 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1007
1008 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1009 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1010 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1011 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1012 '6', '5', '5', '3', '5', 0 };
1013
1014 len = 0;
1015
1016 sockaddr.sin_family = AF_INET;
1017 sockaddr.sin_port = 0;
1018 sockaddr.sin_addr.s_addr = 0;
1019
1020 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1021 ok( ret == SOCKET_ERROR, "WSAAddressToStringW() succeeded unexpectedly: %x\n", WSAGetLastError() );
1022
1023 len = sizeof(address);
1024
1025 sockaddr.sin_family = AF_INET;
1026 cs130_tom 1.1 sockaddr.sin_port = 0;
1027 sockaddr.sin_addr.s_addr = 0;
1028
1029 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1030 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1031
1032 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1033
1034 len = sizeof(address);
1035
1036 sockaddr.sin_family = AF_INET;
1037 sockaddr.sin_port = 0;
1038 sockaddr.sin_addr.s_addr = 0xffffffff;
1039
1040 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1041 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1042
1043 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1044
1045 len = sizeof(address);
1046
1047 cs130_tom 1.1 sockaddr.sin_family = AF_INET;
1048 sockaddr.sin_port = 0xffff;
1049 sockaddr.sin_addr.s_addr = 0;
1050
1051 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1052 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1053
1054 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1055
1056 len = sizeof(address);
1057
1058 sockaddr.sin_family = AF_INET;
1059 sockaddr.sin_port = 0xffff;
1060 sockaddr.sin_addr.s_addr = 0xffffffff;
1061
1062 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1063 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1064
1065 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1066 }
1067
1068 cs130_tom 1.1 static void test_WSAStringToAddressA()
1069 {
1070 INT ret, len;
1071 SOCKADDR_IN sockaddr;
1072
1073 CHAR address1[] = "0.0.0.0";
1074 CHAR address2[] = "127.127.127.127";
1075 CHAR address3[] = "255.255.255.255";
1076 CHAR address4[] = "127.127.127.127:65535";
1077 CHAR address5[] = "255.255.255.255:65535";
1078
1079 len = 0;
1080 sockaddr.sin_family = AF_INET;
1081
1082 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1083 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %x\n",
1084 WSAGetLastError() );
1085
1086 len = sizeof(sockaddr);
1087 sockaddr.sin_port = 0;
1088 sockaddr.sin_addr.s_addr = 0;
1089 cs130_tom 1.1
1090 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1091 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1092 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1093
1094 len = sizeof(sockaddr);
1095 sockaddr.sin_port = 0;
1096 sockaddr.sin_addr.s_addr = 0;
1097
1098 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1099 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1100 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1101
1102 len = sizeof(sockaddr);
1103
1104 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1105 ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1106
1107 len = sizeof(sockaddr);
1108 sockaddr.sin_port = 0;
1109 sockaddr.sin_addr.s_addr = 0;
1110 cs130_tom 1.1
1111 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1112 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1113 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1114
1115 len = sizeof(sockaddr);
1116
1117 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1118 ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1119 }
1120
1121 static void test_WSAStringToAddressW()
1122 {
1123 INT ret, len;
1124 SOCKADDR_IN sockaddr;
1125
1126 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1127 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1128 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1129 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1130 ':', '6', '5', '5', '3', '5', 0 };
1131 cs130_tom 1.1 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1132 '6', '5', '5', '3', '5', 0 };
1133
1134 len = 0;
1135 sockaddr.sin_family = AF_INET;
1136
1137 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1138 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1139 WSAGetLastError() );
1140
1141 len = sizeof(sockaddr);
1142 sockaddr.sin_port = 0;
1143 sockaddr.sin_addr.s_addr = 0;
1144
1145 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1146 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1147 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1148
1149 len = sizeof(sockaddr);
1150
1151 sockaddr.sin_port = 0;
1152 cs130_tom 1.1 sockaddr.sin_addr.s_addr = 0;
1153
1154 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1155 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1156 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1157
1158 len = sizeof(sockaddr);
1159
1160 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1161 ok( ret, "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1162
1163 len = sizeof(sockaddr);
1164 sockaddr.sin_port = 0;
1165 sockaddr.sin_addr.s_addr = 0;
1166
1167 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1168 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1169 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1170
1171 len = sizeof(sockaddr);
1172
1173 cs130_tom 1.1 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1174 ok( ret, "WSAStringToAddressW() succeeded unexpectedly: %d\n", WSAGetLastError() );
1175 }
1176
1177 /**************** Main program ***************/
1178
1179 START_TEST( sock )
1180 {
1181 int i;
1182 Init();
1183
1184 test_set_getsockopt();
1185 test_so_reuseaddr();
1186
1187 for (i = 0; i < NUM_TESTS; i++)
1188 {
1189 trace ( " **** STARTING TEST %d **** \n", i );
1190 do_test ( &tests[i] );
1191 trace ( " **** TEST %d COMPLETE **** \n", i );
1192 }
1193
1194 cs130_tom 1.1 test_WSAAddressToStringA();
1195 test_WSAAddressToStringW();
1196
1197 test_WSAStringToAddressA();
1198 test_WSAStringToAddressW();
1199
1200 Exit();
1201 }
|