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.7 #define NUM_CLIENTS 1
|
47 cs130_tom 1.6 // amount of data to transfer from each client to server
48 #define TRANSFER_SIZE 1000000
|
49 cs130_tom 1.4
|
50 cs130_tom 1.7 int clientsDone = 0;
|
51 cs130_tom 1.12 int sizeofSOCKADDR_IN = sizeof(SOCKADDR_IN);
|
52 cs130_tom 1.7
|
53 cs130_tom 1.4 struct TestParams {
|
54 cs130_tom 1.5 int serverSock;
55 int serverType;
|
56 cs130_tom 1.4 int serverPort;
57 int clientPort[NUM_CLIENTS];
58 };
59
60 struct ClientParams {
61 struct TestParams *test;
62 int clientNum; // 1...NUM_CLIENTS
63 };
64
|
65 cs130_tom 1.12 struct ServerThread {
66 HANDLE* ServerThread;
67 DWORD* ServerThreadID;
68 SOCKET Socket; // socket to communicate with client
69 SOCKADDR_IN Client; // client info
70 };
71
|
72 cs130_tom 1.4 static void test_Startup(void);
|
73 cs130_tom 1.6 void BlockingClient(int *serverPort);
74 void BlockingServer(int *port);
|
75 cs130_tom 1.4 static void test_ClientServerBlocking_1(void);
76 static void test_Startup(void);
|
77 rizwank 1.1
|
78 cs130_tom 1.5 // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
79 void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
|
80 rizwank 1.1 {
|
81 cs130_tom 1.5 SOCKADDR_IN tmpAddr;
82 int tmpAddrSize;
|
83 cs130_tom 1.4
|
84 cs130_tom 1.5 *sock = socket(AF_INET, type, 0);
85 if (*sock == INVALID_SOCKET) {
|
86 cs130_tom 1.4 ok( 0 , "Error in socket()");
87 WSACleanup();
88 exit(0);
89 }
90 trace("socket() ok\n");
91
|
92 cs130_tom 1.5 addr->sin_family = AF_INET;
93 addr->sin_addr.s_addr = INADDR_ANY;
94 addr->sin_port = htons(0);
|
95 cs130_tom 1.4
|
96 cs130_tom 1.5 if( bind(*sock, (const SOCKADDR *) addr, sizeof(*addr)) ) {
97 ok( 0 , "Error binding client to socket");
98 WSACleanup();
99 exit(0);
100 }
|
101 cs130_tom 1.4
|
102 cs130_tom 1.5 // get port number
103 tmpAddrSize = sizeof(tmpAddr);
104 getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
105 addr->sin_port = tmpAddr.sin_port;
106 }
|
107 cs130_tom 1.4
|
108 cs130_tom 1.6 void BlockingClient(int *serverPort)
|
109 cs130_tom 1.5 {
110 SOCKET sock;
|
111 cs130_tom 1.6 SOCKADDR_IN client, server;
112 HOSTENT *hp;
|
113 cs130_tom 1.12 char* msg="Some message to send to server";
114
|
115 cs130_tom 1.5 StartNetworkApp(SOCK_STREAM, &sock, &client);
116
|
117 cs130_tom 1.6 hp = gethostbyname("localhost");
118
119 while(*serverPort == 0) ;
120
|
121 cs130_tom 1.5 // network code here
|
122 cs130_tom 1.6 server.sin_family = AF_INET;
123 server.sin_addr = *(struct in_addr *) hp->h_addr;
124 server.sin_port = *serverPort;
|
125 cs130_tom 1.4
|
126 cs130_tom 1.12 if(connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr)) < 0){
127 ok( 0 , "Error connecting client to socket\n");
128 WSACleanup();
129 exit(0);
130 }
131
132 if((send(sock, msg, strlen(msg), 0))==-1){
133 ok( 0 , "Error sending message\n");
134 WSACleanup();
135 exit(0);
136 }
137
|
138 cs130_tom 1.5 trace("blocking client done\n");
|
139 cs130_tom 1.7 clientsDone++;
|
140 rizwank 1.1 }
141
|
142 cs130_doug 1.11 void ProcessConnection(SOCKET *ConnectedSocket)
|
143 cs130_doug 1.8 {
144 // this will handle all connections to the server, it's in its own function to allow for multithreading
|
145 cs130_tom 1.10 int bClosed;
146 bClosed = close(ConnectedSocket);
147 //ok(bClosed,"Error closing socket");
|
148 cs130_doug 1.8 }
149
|
150 cs130_doug 1.11 void BlockingServer(int *port) // listens for incoming connections and accepts up to NUM_CLIENTS connections at once
|
151 rizwank 1.3 {
|
152 cs130_tom 1.12 struct ServerThread *Threads;
|
153 cs130_doug 1.11 HANDLE* ServerThreads; // the handles for the threads that process connections
154 DWORD* ServerThreadIDs; // the thread ids for the threads that process connections
155 SOCKET* ConnectedSockets; // the threads created by accept() that get sent to the processing function
156 int ThreadIndex = 0;
157
158 SOCKET sock;
159 SOCKADDR_IN server;
|
160 cs130_tom 1.12 int ListenReturn;
|
161 cs130_tom 1.10
|
162 cs130_tom 1.5 StartNetworkApp(SOCK_STREAM, &sock, &server);
|
163 cs130_tom 1.6 *port = server.sin_port;
|
164 cs130_tom 1.9
|
165 cs130_tom 1.12 Threads = malloc(sizeof(struct ServerThread) * NUM_CLIENTS);
166 memset(Threads, 0, sizeof(struct ServerThread) * NUM_CLIENTS);
167
|
168 cs130_tom 1.10 ServerThreads = malloc(sizeof(HANDLE) * NUM_CLIENTS);
169 memset(ServerThreads, 0, sizeof(HANDLE) * NUM_CLIENTS);
170
|
171 cs130_doug 1.8 ServerThreadIDs = malloc(sizeof(DWORD) * NUM_CLIENTS);
172 memset(ServerThreadIDs, 0, sizeof(DWORD) * NUM_CLIENTS);
173
|
174 cs130_doug 1.11 ConnectedSockets = malloc(sizeof(DWORD) * NUM_CLIENTS);
175 memset(ConnectedSockets, 0, sizeof(DWORD) * NUM_CLIENTS);
176
|
177 cs130_doug 1.8 //SOCKADDR_IN RemoteAddress;
178
|
179 cs130_doug 1.11 ListenReturn = listen(sock, 5);
180 ok(ListenReturn != SOCKET_ERROR, "error listening on socket");
181
182 for (ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++)
|
183 cs130_doug 1.8 {
|
184 cs130_tom 1.12 ConnectedSockets[ThreadIndex] = accept(sock, (SOCKADDR *) &Threads[ThreadIndex].Client, &sizeofSOCKADDR_IN); // this can be modified to include the address of the remote socket
185 ok(ConnectedSockets[ThreadIndex] != INVALID_SOCKET, "error accepting socket");
|
186 cs130_doug 1.11
187 ServerThreads[ThreadIndex] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &ProcessConnection, &ConnectedSockets[ThreadIndex], 0, &ServerThreadIDs[ThreadIndex]);
|
188 cs130_doug 1.8 }
|
189 cs130_tom 1.5
190 // network code here
191
|
192 cs130_doug 1.8 free(ServerThreads);
193 free(ServerThreadIDs);
|
194 cs130_doug 1.11 free(ConnectedSockets);
|
195 cs130_tom 1.5 trace("blocking server done\n");
|
196 rizwank 1.3 }
197
|
198 cs130_tom 1.4 static void test_ClientServerBlocking_1(void)
|
199 rizwank 1.3 {
|
200 cs130_tom 1.9 int ThreadIndex = 0;
|
201 cs130_doug 1.8 int serverPort = 0; // I think the server port would work better as a #DEFINE rather than a variable that gets passed around everywhere
|
202 cs130_tom 1.10 HANDLE ServerThread;
203 DWORD ServerThreadId;
|
204 cs130_tom 1.9 DWORD *ClientThreadIds;
205 HANDLE *ClientThreads;
206
207 ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, &serverPort, 0, &ServerThreadId);
208
|
209 cs130_tom 1.10 ClientThreads = malloc(sizeof(HANDLE) * NUM_CLIENTS);
210 memset(ClientThreads, 0, sizeof(HANDLE) * NUM_CLIENTS);
|
211 cs130_tom 1.9
212 ClientThreadIds = malloc(sizeof(DWORD) * NUM_CLIENTS);
213 memset(ClientThreadIds, 0, sizeof(DWORD) * NUM_CLIENTS);
214
215 for(ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
216 ClientThreads[ThreadIndex] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, &serverPort, 0, &ClientThreadIds[ThreadIndex]);
217 }
218
|
219 cs130_tom 1.5 trace("test_ClientServerBlocking_1 done\n");
|
220 rizwank 1.3 }
221
|
222 cs130_tom 1.4 static void test_Startup(void)
|
223 rizwank 1.3 {
|
224 cs130_tom 1.4 // initialize application
225 WSADATA wsaData;
226 int wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
227 if ( (LOBYTE(wsaData.wVersion) != 1) && (HIBYTE(wsaData.wVersion) != 1) )
228 {
229 ok( 0 , "WSAStartup returns an incompatible sockets version");
230 WSACleanup();
231 exit(0);
232 }
233
234 ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
|
235 rizwank 1.3 }
236
|
237 cs130_tom 1.4
|
238 rizwank 1.1 START_TEST(wsock32_main)
239 {
|
240 cs130_tom 1.4 trace("test 1 of 2:\n");
241 test_Startup();
242 trace("test 2 of 2:\n");
243 test_ClientServerBlocking_1();
244 trace("all tests done\n");
|
245 cs130_tom 1.7 while (clientsDone != NUM_CLIENTS)
246 ;
|
247 rizwank 1.1 }
|