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 cs130_doug 1.8 #include <string.h>
|
28 rizwank 1.1
29 #ifndef STANDALONE
30 #include "wine/test.h"
31 #else
32 #include <assert.h>
33 #define START_TEST(name) main(int argc, char **argv)
34 #define ok(condition, msg) \
35 do { \
36 if(!(condition)) \
37 { \
38 fprintf(stderr,"failed at %d\n",__LINE__); \
39 exit(0); \
40 } \
41 } while(0)
42
43 #define todo_wine
44 #endif
45
|
46 cs130_tom 1.19 // clients threads to create
|
47 cs130_tom 1.22 #define NUM_CLIENTS 64
|
48 cs130_tom 1.19
|
49 cs130_tom 1.6 // amount of data to transfer from each client to server
|
50 cs130_tom 1.19 #define TEST_DATA_SIZE 145243
|
51 cs130_tom 1.4
|
52 cs130_tom 1.24 // max time (seconds) to run test
53 #define TEST_TIMEOUT 10
|
54 cs130_tom 1.19
55 // we often pass this size by reference
|
56 cs130_tom 1.12 int sizeofSOCKADDR_IN = sizeof(SOCKADDR_IN);
|
57 cs130_tom 1.19
58 // global test data; server sends it to client, then client verifies it
|
59 cs130_tom 1.24 char *gTestData;
|
60 cs130_tom 1.4
|
61 cs130_tom 1.20 struct MyThread {
|
62 cs130_tom 1.16 HANDLE Handle;
63 DWORD ID;
64 };
65
|
66 cs130_tom 1.12 struct ServerThread {
|
67 cs130_doug 1.13 HANDLE ServerThread;
68 DWORD ServerThreadID;
69 SOCKET ConnectedSocket; // socket to communicate with client
|
70 cs130_tom 1.12 SOCKADDR_IN Client; // client info
71 };
72
|
73 cs130_tom 1.4 static void test_Startup(void);
|
74 cs130_tom 1.23 static void test_ClientServerBlocking_1(void);
|
75 cs130_tom 1.22 static void test_Cleanup(void);
|
76 cs130_tom 1.23
|
77 cs130_tom 1.24 static void StartBlockingClients(int *serverPort);
78 static void BlockingClient(int *serverPort);
79 static void BlockingServer();
|
80 cs130_tom 1.23
|
81 rizwank 1.1
|
82 cs130_tom 1.5 // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
83 void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
|
84 rizwank 1.1 {
|
85 cs130_tom 1.5 SOCKADDR_IN tmpAddr;
86 int tmpAddrSize;
|
87 cs130_tom 1.19 int bindOK;
|
88 cs130_tom 1.4
|
89 cs130_tom 1.19 // create socket
|
90 cs130_tom 1.5 *sock = socket(AF_INET, type, 0);
|
91 cs130_tom 1.24 ok( *sock != INVALID_SOCKET , "Error in socket()\n");
|
92 cs130_tom 1.5 if (*sock == INVALID_SOCKET) {
|
93 cs130_tom 1.4 WSACleanup();
94 exit(0);
95 }
96
|
97 cs130_tom 1.5 addr->sin_family = AF_INET;
98 addr->sin_addr.s_addr = INADDR_ANY;
99 addr->sin_port = htons(0);
|
100 cs130_tom 1.4
|
101 cs130_tom 1.19 // bind socket to port
102 bindOK = !bind(*sock, (const SOCKADDR *) addr, sizeof(*addr));
|
103 cs130_tom 1.24 ok( bindOK , "Error binding client to socket\n");
|
104 cs130_tom 1.19 if( !bindOK ) {
|
105 cs130_tom 1.5 WSACleanup();
106 exit(0);
107 }
|
108 cs130_tom 1.4
|
109 cs130_tom 1.5 // get port number
110 tmpAddrSize = sizeof(tmpAddr);
111 getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
112 addr->sin_port = tmpAddr.sin_port;
113 }
|
114 cs130_tom 1.4
|
115 cs130_tom 1.24 void BlockingClient(int *serverPort)
|
116 cs130_tom 1.5 {
117 SOCKET sock;
|
118 cs130_tom 1.6 SOCKADDR_IN client, server;
119 HOSTENT *hp;
|
120 cs130_tom 1.16 int connectError;
121 int totCharsReceived = 0;
122 int numCharsReceived;
123 int memSame;
|
124 cs130_tom 1.19 int yieldCounter = 0;
|
125 cs130_tom 1.16 char buf[1001];
|
126 cs130_tom 1.12
|
127 cs130_tom 1.5 StartNetworkApp(SOCK_STREAM, &sock, &client);
128
|
129 cs130_tom 1.20 //trace("client port %d\n",ntohs(client.sin_port));
130
|
131 cs130_tom 1.6 hp = gethostbyname("localhost");
132
133 server.sin_family = AF_INET;
134 server.sin_addr = *(struct in_addr *) hp->h_addr;
135 server.sin_port = *serverPort;
|
136 cs130_tom 1.4
|
137 cs130_tom 1.19 // connect to server
|
138 cs130_tom 1.16 connectError = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
139 ok( !connectError , "client cannot connect to host\n");
140 if(connectError) {
|
141 cs130_tom 1.12 WSACleanup();
142 exit(0);
143 }
|
144 cs130_tom 1.19
145 // start receiving data from server
|
146 cs130_tom 1.16 while( totCharsReceived < TEST_DATA_SIZE ) {
147 numCharsReceived = recv(sock, buf, 1000, 0);
|
148 cs130_tom 1.24 ok( numCharsReceived > 0, "socket was closed unexpectedly\n" );
|
149 cs130_tom 1.19
150 // check received data againt global test data
|
151 cs130_tom 1.24 memSame = ! memcmp(buf,gTestData+totCharsReceived,numCharsReceived);
152 ok( memSame, "data integrity lost during transfer\n" );
|
153 cs130_tom 1.19 totCharsReceived += numCharsReceived;
|
154 cs130_tom 1.18
|
155 cs130_tom 1.19 // yield to our other threads
156 if(yieldCounter % 20 == 0) {
|
157 cs130_tom 1.23 //Sleep(100);
|
158 cs130_tom 1.18 }
|
159 cs130_tom 1.19 yieldCounter++;
|
160 cs130_tom 1.12 }
161
|
162 cs130_tom 1.22 //trace("client done\n");
|
163 rizwank 1.1 }
164
|
165 cs130_tom 1.24 void BlockingServer_ProcessConnection(struct ServerThread *t)
|
166 cs130_doug 1.8 {
167 // this will handle all connections to the server, it's in its own function to allow for multithreading
|
168 cs130_tom 1.10 int bClosed;
|
169 cs130_tom 1.17 int totCharsSent = 0;
170 int numCharsSent;
|
171 cs130_tom 1.19 int yieldCounter = 0;
|
172 cs130_tom 1.17 const int charsPerSend = 2000;
173
|
174 cs130_tom 1.19 // loop and send data
|
175 cs130_tom 1.17 while( totCharsSent < TEST_DATA_SIZE ) {
|
176 cs130_tom 1.24 numCharsSent = send(t->ConnectedSocket, gTestData+totCharsSent, (totCharsSent + charsPerSend <= TEST_DATA_SIZE) ? charsPerSend : TEST_DATA_SIZE - totCharsSent, 0);
177 ok( numCharsSent != SOCKET_ERROR, "socket error\n" );
|
178 cs130_tom 1.17 if(numCharsSent == SOCKET_ERROR) {
179 printf("error code: %d",WSAGetLastError());
180 }
181 totCharsSent += numCharsSent;
|
182 cs130_tom 1.19
183 // yield to our other threads
184 if(yieldCounter % 20 == 0) {
|
185 cs130_tom 1.23 //Sleep(100);
|
186 cs130_tom 1.19 }
187 yieldCounter++;
|
188 cs130_tom 1.17 }
189
|
190 cs130_tom 1.19 bClosed = !closesocket(t->ConnectedSocket);
|
191 cs130_tom 1.24 ok(bClosed,"Error closing socket\n");
|
192 cs130_doug 1.8 }
193
|
194 cs130_tom 1.24 void BlockingServer() // listens for incoming connections and accepts up to NUM_CLIENTS connections at once
|
195 rizwank 1.3 {
|
196 cs130_tom 1.12 struct ServerThread *Threads;
|
197 cs130_doug 1.11 int ThreadIndex = 0;
|
198 cs130_tom 1.24 int serverPort = 0;
|
199 cs130_doug 1.11
200 SOCKET sock;
201 SOCKADDR_IN server;
|
202 cs130_tom 1.12 int ListenReturn;
|
203 cs130_tom 1.10
|
204 cs130_tom 1.5 StartNetworkApp(SOCK_STREAM, &sock, &server);
|
205 cs130_tom 1.9
|
206 cs130_tom 1.19 // allocate enough space to keep track of NUM_CLIENTS connections
|
207 cs130_tom 1.12 Threads = malloc(sizeof(struct ServerThread) * NUM_CLIENTS);
208 memset(Threads, 0, sizeof(struct ServerThread) * NUM_CLIENTS);
209
|
210 cs130_tom 1.19 // listen on port
|
211 cs130_tom 1.17 ListenReturn = listen(sock, NUM_CLIENTS);
|
212 cs130_tom 1.24 ok(ListenReturn != SOCKET_ERROR, "error listening on socket\n");
|
213 cs130_tom 1.17
|
214 cs130_tom 1.19 // set the port parameter; clients now know we're ready to accept connections
|
215 cs130_tom 1.24 serverPort = server.sin_port;
|
216 cs130_tom 1.17
|
217 cs130_tom 1.23 // bound to port; now we can start clients
|
218 cs130_tom 1.24 CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &StartBlockingClients, &serverPort, 0, NULL);
|
219 cs130_tom 1.23
|
220 cs130_tom 1.19 // we require one connection from each client thread
|
221 cs130_tom 1.17 for (ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
|
222 cs130_tom 1.19 // accept connection
|
223 cs130_tom 1.14 Threads[ThreadIndex].ConnectedSocket = accept(sock, (SOCKADDR *) &Threads[ThreadIndex].Client, &sizeofSOCKADDR_IN); // this can be modified to include the address of the remote socket
|
224 cs130_tom 1.24 ok(Threads[ThreadIndex].ConnectedSocket != INVALID_SOCKET, "error accepting socket\n");
|
225 cs130_tom 1.19
226 // spawn thread to handle sending data
|
227 cs130_tom 1.24 Threads[ThreadIndex].ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer_ProcessConnection, &Threads[ThreadIndex], 0, &Threads[ThreadIndex].ServerThreadID);
|
228 cs130_doug 1.8 }
|
229 cs130_tom 1.5
|
230 cs130_tom 1.19 // wait for all clients to receive data before cleaning up
|
231 cs130_tom 1.24 for(ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
232 WaitForSingleObject(Threads[ThreadIndex].ServerThread, INFINITE);
233 }
|
234 cs130_tom 1.5
|
235 cs130_doug 1.13 free(Threads);
|
236 rizwank 1.3 }
237
|
238 cs130_tom 1.24 static void StartBlockingClients(int *serverPort)
|
239 rizwank 1.3 {
|
240 cs130_tom 1.9 int ThreadIndex = 0;
|
241 cs130_tom 1.20 struct MyThread *ClientThreads;
|
242 cs130_tom 1.9
|
243 cs130_tom 1.20 ClientThreads = malloc(sizeof(struct MyThread) * NUM_CLIENTS);
244 memset(ClientThreads, 0, sizeof(struct MyThread) * NUM_CLIENTS);
|
245 cs130_tom 1.18
|
246 cs130_tom 1.9 for(ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
|
247 cs130_tom 1.23 ClientThreads[ThreadIndex].Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, (void *) serverPort, 0, &ClientThreads[ThreadIndex].ID);
|
248 cs130_tom 1.9 }
|
249 cs130_tom 1.20 trace("%d clients started\n", NUM_CLIENTS);
|
250 cs130_tom 1.9
|
251 cs130_tom 1.20 // wait for all clients to receive data before cleaning up
|
252 cs130_tom 1.24 for(ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
253 WaitForSingleObject(ClientThreads[ThreadIndex].Handle, INFINITE);
254 }
|
255 cs130_tom 1.19
256 free(ClientThreads);
|
257 cs130_tom 1.23 }
258
259 static void test_ClientServerBlocking_1(void)
260 {
261 struct MyThread ServerThread;
|
262 cs130_tom 1.24 DWORD waitStatus;
|
263 cs130_tom 1.23
264 // start server thread
265 // server starts client threads after it binds to a port.
266 trace("starting main server thread\n");
|
267 cs130_tom 1.24 ServerThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, NULL, 0, &ServerThread.ID);
|
268 cs130_tom 1.23
|
269 cs130_tom 1.24 // server thread needs to end before cleaning up
270 waitStatus = WaitForSingleObject(ServerThread.Handle, TEST_TIMEOUT * 1000);
271 ok( waitStatus != WAIT_TIMEOUT, "test did not complete in time\n" );
|
272 cs130_tom 1.19
|
273 cs130_tom 1.5 trace("test_ClientServerBlocking_1 done\n");
|
274 rizwank 1.3 }
275
|
276 cs130_tom 1.4 static void test_Startup(void)
|
277 rizwank 1.3 {
|
278 cs130_tom 1.4 // initialize application
279 WSADATA wsaData;
|
280 cs130_tom 1.22 int wsastartup_result;
|
281 cs130_tom 1.14 int versionOK;
282
|
283 cs130_tom 1.19 // check for compatible winsock version
|
284 cs130_tom 1.14 wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
285 versionOK = (LOBYTE(wsaData.wVersion) == 1) && (HIBYTE(wsaData.wVersion) == 1);
286
|
287 cs130_tom 1.24 ok( versionOK , "WSAStartup returns an incompatible sockets version\n");
|
288 cs130_tom 1.14 if ( !versionOK ) {
|
289 cs130_tom 1.4 WSACleanup();
290 exit(0);
291 }
292
|
293 cs130_tom 1.24 ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()\n");
|
294 cs130_tom 1.22 trace("startup ok\n");
295 }
296
297 static void test_Cleanup(void)
298 {
299 int cleanupOK;
300
301 cleanupOK = ! WSACleanup();
302
|
303 cs130_tom 1.24 ok( cleanupOK , "error in WSACleanup()\n");
|
304 cs130_tom 1.22 trace("cleanup ok\n");
|
305 rizwank 1.3 }
|
306 cs130_tom 1.4
|
307 rizwank 1.1 START_TEST(wsock32_main)
308 {
|
309 cs130_tom 1.22 const int numTests = 3;
|
310 cs130_tom 1.24 gTestData = malloc(TEST_DATA_SIZE);
|
311 cs130_tom 1.22
312 trace("test 1 of %d:\n", numTests);
313 test_Startup();
314
315 trace("test 2 of %d:\n", numTests);
316 test_ClientServerBlocking_1();
317
318 trace("test 3 of %d:\n", numTests);
319 test_Cleanup();
320
|
321 cs130_tom 1.4 trace("all tests done\n");
|
322 cs130_tom 1.20
|
323 cs130_tom 1.24 free(gTestData);
|
324 rizwank 1.1 }
|