1 rizwank 1.1 /*
|
2 rizwank 1.3 * Unit tests for 32-bit socket functions in Wine
|
3 rizwank 1.1 *
|
4 rizwank 1.3 * Copyright (c) 2005 Thomas Kho, Fredy Garcia, Douglas Rosenberg
|
5 rizwank 1.1 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdio.h>
22
|
23 rizwank 1.3 #include <windows.h>
|
24 cs130_tom 1.4 #include <winsock.h>
25 #include <wtypes.h>
26 #include <winerror.h>
|
27 rizwank 1.1
28 #ifndef STANDALONE
29 #include "wine/test.h"
30 #else
31 #include <assert.h>
32 #define START_TEST(name) main(int argc, char **argv)
33 #define ok(condition, msg) \
34 do { \
35 if(!(condition)) \
36 { \
37 fprintf(stderr,"failed at %d\n",__LINE__); \
38 exit(0); \
39 } \
40 } while(0)
41
42 #define todo_wine
43 #endif
44
|
45 cs130_tom 1.4 #define NUM_CLIENTS 5
|
46 cs130_tom 1.6 // amount of data to transfer from each client to server
47 #define TRANSFER_SIZE 1000000
|
48 cs130_tom 1.4
49 struct TestParams {
|
50 cs130_tom 1.5 int serverSock;
51 int serverType;
|
52 cs130_tom 1.4 int serverPort;
53 int clientPort[NUM_CLIENTS];
54 };
55
56 struct ClientParams {
57 struct TestParams *test;
58 int clientNum; // 1...NUM_CLIENTS
59 };
60
61 static void test_Startup(void);
|
62 cs130_tom 1.6 void BlockingClient(int *serverPort);
63 void BlockingServer(int *port);
|
64 cs130_tom 1.4 static void test_ClientServerBlocking_1(void);
65 static void test_Startup(void);
|
66 rizwank 1.1
|
67 cs130_tom 1.5 // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
68 void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
|
69 rizwank 1.1 {
|
70 cs130_tom 1.5 SOCKADDR_IN tmpAddr;
71 int tmpAddrSize;
|
72 cs130_tom 1.4
|
73 cs130_tom 1.5 *sock = socket(AF_INET, type, 0);
74 if (*sock == INVALID_SOCKET) {
|
75 cs130_tom 1.4 ok( 0 , "Error in socket()");
76 WSACleanup();
77 exit(0);
78 }
79 trace("socket() ok\n");
80
|
81 cs130_tom 1.5 addr->sin_family = AF_INET;
82 addr->sin_addr.s_addr = INADDR_ANY;
83 addr->sin_port = htons(0);
|
84 cs130_tom 1.4
|
85 cs130_tom 1.5 if( bind(*sock, (const SOCKADDR *) addr, sizeof(*addr)) ) {
86 ok( 0 , "Error binding client to socket");
87 WSACleanup();
88 exit(0);
89 }
|
90 cs130_tom 1.4
|
91 cs130_tom 1.5 // get port number
92 tmpAddrSize = sizeof(tmpAddr);
93 getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
94 addr->sin_port = tmpAddr.sin_port;
95 }
|
96 cs130_tom 1.4
|
97 cs130_tom 1.6 void BlockingClient(int *serverPort)
|
98 cs130_tom 1.5 {
99 SOCKET sock;
|
100 cs130_tom 1.6 SOCKADDR_IN client, server;
101 HOSTENT *hp;
|
102 cs130_tom 1.5 StartNetworkApp(SOCK_STREAM, &sock, &client);
103
|
104 cs130_tom 1.6 hp = gethostbyname("localhost");
105
106 while(*serverPort == 0) ;
107
|
108 cs130_tom 1.5 // network code here
|
109 cs130_tom 1.6 server.sin_family = AF_INET;
110 server.sin_addr = *(struct in_addr *) hp->h_addr;
111 server.sin_port = *serverPort;
|
112 cs130_tom 1.4
|
113 cs130_tom 1.5 trace("blocking client done\n");
|
114 rizwank 1.1 }
115
|
116 cs130_tom 1.6 void BlockingServer(int *port)
|
117 rizwank 1.3 {
|
118 cs130_tom 1.5 SOCKET sock;
119 SOCKADDR_IN server;
120 StartNetworkApp(SOCK_STREAM, &sock, &server);
|
121 cs130_tom 1.6 *port = server.sin_port;
|
122 cs130_tom 1.5
123 // network code here
124
125 trace("blocking server done\n");
|
126 rizwank 1.3 }
127
|
128 cs130_tom 1.4 static void test_ClientServerBlocking_1(void)
|
129 rizwank 1.3 {
|
130 cs130_tom 1.6 int serverPort = 0;
|
131 cs130_tom 1.4 HANDLE Thread1, Thread2;
132 DWORD ThreadId1, ThreadId2;
|
133 cs130_tom 1.6 Thread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, &serverPort, 0, &ThreadId1);
134 Thread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, &serverPort, 0, &ThreadId2);
|
135 cs130_tom 1.5 trace("test_ClientServerBlocking_1 done\n");
|
136 rizwank 1.3 }
137
|
138 cs130_tom 1.4 static void test_Startup(void)
|
139 rizwank 1.3 {
|
140 cs130_tom 1.4 // initialize application
141 WSADATA wsaData;
142 int wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
143 if ( (LOBYTE(wsaData.wVersion) != 1) && (HIBYTE(wsaData.wVersion) != 1) )
144 {
145 ok( 0 , "WSAStartup returns an incompatible sockets version");
146 WSACleanup();
147 exit(0);
148 }
149
150 ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
|
151 rizwank 1.3 }
152
|
153 cs130_tom 1.4
|
154 rizwank 1.1 START_TEST(wsock32_main)
155 {
|
156 cs130_tom 1.4 trace("test 1 of 2:\n");
157 test_Startup();
158 trace("test 2 of 2:\n");
159 test_ClientServerBlocking_1();
160 trace("all tests done\n");
|
161 rizwank 1.1 }
|