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.19 // clients threads to create
|
46 cs130_tom 1.22 #define NUM_CLIENTS 64
|
47 cs130_tom 1.19
|
48 cs130_tom 1.6 // amount of data to transfer from each client to server
|
49 cs130_tom 1.19 #define TEST_DATA_SIZE 145243
|
50 cs130_tom 1.4
|
51 cs130_tom 1.24 // max time (seconds) to run test
52 #define TEST_TIMEOUT 10
|
53 cs130_tom 1.19
54 // we often pass this size by reference
|
55 cs130_tom 1.12 int sizeofSOCKADDR_IN = sizeof(SOCKADDR_IN);
|
56 cs130_tom 1.19
57 // global test data; server sends it to client, then client verifies it
|
58 cs130_tom 1.24 char *gTestData;
|
59 cs130_tom 1.4
|
60 cs130_tom 1.25 struct ThreadInfo {
|
61 cs130_tom 1.16 HANDLE Handle;
62 DWORD ID;
63 };
64
|
65 cs130_tom 1.25 struct ServerInfo {
66 HANDLE threadHandle;
67 DWORD threadID;
68 SOCKET connectedSocket; // socket to communicate with client
69 SOCKADDR_IN clientAddr; // client info
|
70 cs130_tom 1.12 };
71
|
72 cs130_tom 1.4 static void test_Startup(void);
|
73 cs130_tom 1.23 static void test_ClientServerBlocking_1(void);
|
74 cs130_tom 1.22 static void test_Cleanup(void);
|
75 cs130_tom 1.23
|
76 cs130_tom 1.25 static void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr);
77 static void BlockingServer_ProcessConnection(struct ServerInfo *t);
|
78 cs130_tom 1.24 static void StartBlockingClients(int *serverPort);
79 static void BlockingClient(int *serverPort);
80 static void BlockingServer();
|
81 cs130_tom 1.23
|
82 cs130_tom 1.5 // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
|
83 cs130_tom 1.25 static 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.25 static 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 char buf[1001];
|
125 cs130_tom 1.12
|
126 cs130_tom 1.5 StartNetworkApp(SOCK_STREAM, &sock, &client);
127
|
128 cs130_tom 1.6 hp = gethostbyname("localhost");
129
130 server.sin_family = AF_INET;
131 server.sin_addr = *(struct in_addr *) hp->h_addr;
132 server.sin_port = *serverPort;
|
133 cs130_tom 1.4
|
134 cs130_tom 1.19 // connect to server
|
135 cs130_tom 1.16 connectError = connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr));
136 ok( !connectError , "client cannot connect to host\n");
137 if(connectError) {
|
138 cs130_tom 1.12 WSACleanup();
139 exit(0);
140 }
|
141 cs130_tom 1.19
142 // start receiving data from server
|
143 cs130_tom 1.16 while( totCharsReceived < TEST_DATA_SIZE ) {
144 numCharsReceived = recv(sock, buf, 1000, 0);
|
145 cs130_tom 1.24 ok( numCharsReceived > 0, "socket was closed unexpectedly\n" );
|
146 cs130_tom 1.19
147 // check received data againt global test data
|
148 cs130_tom 1.24 memSame = ! memcmp(buf,gTestData+totCharsReceived,numCharsReceived);
149 ok( memSame, "data integrity lost during transfer\n" );
|
150 cs130_tom 1.19 totCharsReceived += numCharsReceived;
|
151 cs130_tom 1.12 }
|
152 rizwank 1.1 }
153
|
154 cs130_tom 1.25 static void BlockingServer_ProcessConnection(struct ServerInfo *t)
|
155 cs130_doug 1.8 {
156 // this will handle all connections to the server, it's in its own function to allow for multithreading
|
157 cs130_tom 1.10 int bClosed;
|
158 cs130_tom 1.17 int totCharsSent = 0;
159 int numCharsSent;
160 const int charsPerSend = 2000;
161
|
162 cs130_tom 1.19 // loop and send data
|
163 cs130_tom 1.17 while( totCharsSent < TEST_DATA_SIZE ) {
|
164 cs130_tom 1.25 numCharsSent = send(t->connectedSocket, gTestData+totCharsSent, (totCharsSent + charsPerSend <= TEST_DATA_SIZE) ? charsPerSend : TEST_DATA_SIZE - totCharsSent, 0);
|
165 cs130_tom 1.24 ok( numCharsSent != SOCKET_ERROR, "socket error\n" );
|
166 cs130_tom 1.25
167 // pass if send buffer is full
168 if(numCharsSent == 0) {
169 Sleep(100);
|
170 cs130_tom 1.17 }
|
171 cs130_tom 1.25
|
172 cs130_tom 1.17 totCharsSent += numCharsSent;
173 }
174
|
175 cs130_tom 1.25 bClosed = !closesocket(t->connectedSocket);
|
176 cs130_tom 1.24 ok(bClosed,"Error closing socket\n");
|
177 cs130_doug 1.8 }
178
|
179 cs130_tom 1.25 static void BlockingServer() // listens for incoming connections and accepts up to NUM_CLIENTS connections at once
|
180 rizwank 1.3 {
|
181 cs130_tom 1.25 struct ServerInfo *threads;
182 int threadIndex = 0;
|
183 cs130_tom 1.24 int serverPort = 0;
|
184 cs130_doug 1.11
185 SOCKET sock;
186 SOCKADDR_IN server;
|
187 cs130_tom 1.25 int listenReturn;
|
188 cs130_tom 1.10
|
189 cs130_tom 1.5 StartNetworkApp(SOCK_STREAM, &sock, &server);
|
190 cs130_tom 1.9
|
191 cs130_tom 1.19 // allocate enough space to keep track of NUM_CLIENTS connections
|
192 cs130_tom 1.25 threads = malloc(sizeof(struct ServerInfo) * NUM_CLIENTS);
193 memset(threads, 0, sizeof(struct ServerInfo) * NUM_CLIENTS);
|
194 cs130_tom 1.12
|
195 cs130_tom 1.19 // listen on port
|
196 cs130_tom 1.25 listenReturn = listen(sock, NUM_CLIENTS);
197 ok(listenReturn != SOCKET_ERROR, "error listening on socket\n");
|
198 cs130_tom 1.17
|
199 cs130_tom 1.19 // set the port parameter; clients now know we're ready to accept connections
|
200 cs130_tom 1.24 serverPort = server.sin_port;
|
201 cs130_tom 1.17
|
202 cs130_tom 1.23 // bound to port; now we can start clients
|
203 cs130_tom 1.24 CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &StartBlockingClients, &serverPort, 0, NULL);
|
204 cs130_tom 1.23
|
205 cs130_tom 1.19 // we require one connection from each client thread
|
206 cs130_tom 1.25 for (threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
|
207 cs130_tom 1.19 // accept connection
|
208 cs130_tom 1.25 threads[threadIndex].connectedSocket = accept(sock, (SOCKADDR *) &threads[threadIndex].clientAddr, &sizeofSOCKADDR_IN); // this can be modified to include the address of the remote socket
209 ok(threads[threadIndex].connectedSocket != INVALID_SOCKET, "error accepting socket\n");
|
210 cs130_tom 1.19
211 // spawn thread to handle sending data
|
212 cs130_tom 1.25 threads[threadIndex].threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer_ProcessConnection, &threads[threadIndex], 0, &threads[threadIndex].threadID);
|
213 cs130_doug 1.8 }
|
214 cs130_tom 1.5
|
215 cs130_tom 1.19 // wait for all clients to receive data before cleaning up
|
216 cs130_tom 1.25 for(threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
217 WaitForSingleObject(threads[threadIndex].threadHandle, INFINITE);
|
218 cs130_tom 1.24 }
|
219 cs130_tom 1.5
|
220 cs130_tom 1.25 free(threads);
|
221 rizwank 1.3 }
222
|
223 cs130_tom 1.24 static void StartBlockingClients(int *serverPort)
|
224 rizwank 1.3 {
|
225 cs130_tom 1.25 int threadIndex = 0;
226 struct ThreadInfo *clientThreads;
|
227 cs130_tom 1.9
|
228 cs130_tom 1.25 clientThreads = malloc(sizeof(struct ThreadInfo) * NUM_CLIENTS);
229 memset(clientThreads, 0, sizeof(struct ThreadInfo) * NUM_CLIENTS);
|
230 cs130_tom 1.18
|
231 cs130_tom 1.25 for(threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
232 clientThreads[threadIndex].Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, (void *) serverPort, 0, &clientThreads[threadIndex].ID);
|
233 cs130_tom 1.9 }
|
234 cs130_tom 1.20 trace("%d clients started\n", NUM_CLIENTS);
|
235 cs130_tom 1.9
|
236 cs130_tom 1.20 // wait for all clients to receive data before cleaning up
|
237 cs130_tom 1.25 for(threadIndex = 0; threadIndex < NUM_CLIENTS; threadIndex++) {
238 WaitForSingleObject(clientThreads[threadIndex].Handle, INFINITE);
|
239 cs130_tom 1.24 }
|
240 cs130_tom 1.19
|
241 cs130_tom 1.25 free(clientThreads);
|
242 cs130_tom 1.23 }
243
244 static void test_ClientServerBlocking_1(void)
245 {
|
246 cs130_tom 1.25 struct ThreadInfo serverThread;
|
247 cs130_tom 1.24 DWORD waitStatus;
|
248 cs130_tom 1.23
249 // start server thread
250 // server starts client threads after it binds to a port.
251 trace("starting main server thread\n");
|
252 cs130_tom 1.25 serverThread.Handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, NULL, 0, &serverThread.ID);
|
253 cs130_tom 1.23
|
254 cs130_tom 1.24 // server thread needs to end before cleaning up
|
255 cs130_tom 1.25 waitStatus = WaitForSingleObject(serverThread.Handle, TEST_TIMEOUT * 1000);
|
256 cs130_tom 1.24 ok( waitStatus != WAIT_TIMEOUT, "test did not complete in time\n" );
|
257 rizwank 1.3 }
258
|
259 cs130_tom 1.4 static void test_Startup(void)
|
260 rizwank 1.3 {
|
261 cs130_tom 1.4 // initialize application
262 WSADATA wsaData;
|
263 cs130_tom 1.22 int wsastartup_result;
|
264 cs130_tom 1.14 int versionOK;
265
|
266 cs130_tom 1.19 // check for compatible winsock version
|
267 cs130_tom 1.14 wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
268 versionOK = (LOBYTE(wsaData.wVersion) == 1) && (HIBYTE(wsaData.wVersion) == 1);
269
|
270 cs130_tom 1.24 ok( versionOK , "WSAStartup returns an incompatible sockets version\n");
|
271 cs130_tom 1.14 if ( !versionOK ) {
|
272 cs130_tom 1.4 WSACleanup();
273 exit(0);
274 }
275
|
276 cs130_tom 1.24 ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()\n");
|
277 cs130_tom 1.22 trace("startup ok\n");
278 }
279
280 static void test_Cleanup(void)
281 {
282 int cleanupOK;
283
284 cleanupOK = ! WSACleanup();
285
|
286 cs130_tom 1.24 ok( cleanupOK , "error in WSACleanup()\n");
|
287 cs130_tom 1.22 trace("cleanup ok\n");
|
288 rizwank 1.3 }
|
289 cs130_tom 1.4
|
290 rizwank 1.1 START_TEST(wsock32_main)
291 {
|
292 cs130_tom 1.22 const int numTests = 3;
|
293 cs130_tom 1.24 gTestData = malloc(TEST_DATA_SIZE);
|
294 cs130_tom 1.22
295 trace("test 1 of %d:\n", numTests);
296 test_Startup();
297
298 trace("test 2 of %d:\n", numTests);
299 test_ClientServerBlocking_1();
300
301 trace("test 3 of %d:\n", numTests);
302 test_Cleanup();
303
|
304 cs130_tom 1.4 trace("all tests done\n");
|
305 cs130_tom 1.20
|
306 cs130_tom 1.24 free(gTestData);
|
307 rizwank 1.1 }
|