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
|
52 cs130_tom 1.4 struct TestParams {
|
53 cs130_tom 1.5 int serverSock;
54 int serverType;
|
55 cs130_tom 1.4 int serverPort;
56 int clientPort[NUM_CLIENTS];
57 };
58
59 struct ClientParams {
60 struct TestParams *test;
61 int clientNum; // 1...NUM_CLIENTS
62 };
63
64 static void test_Startup(void);
|
65 cs130_tom 1.6 void BlockingClient(int *serverPort);
66 void BlockingServer(int *port);
|
67 cs130_tom 1.4 static void test_ClientServerBlocking_1(void);
68 static void test_Startup(void);
|
69 rizwank 1.1
|
70 cs130_tom 1.5 // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
71 void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
|
72 rizwank 1.1 {
|
73 cs130_tom 1.5 SOCKADDR_IN tmpAddr;
74 int tmpAddrSize;
|
75 cs130_tom 1.4
|
76 cs130_tom 1.5 *sock = socket(AF_INET, type, 0);
77 if (*sock == INVALID_SOCKET) {
|
78 cs130_tom 1.4 ok( 0 , "Error in socket()");
79 WSACleanup();
80 exit(0);
81 }
82 trace("socket() ok\n");
83
|
84 cs130_tom 1.5 addr->sin_family = AF_INET;
85 addr->sin_addr.s_addr = INADDR_ANY;
86 addr->sin_port = htons(0);
|
87 cs130_tom 1.4
|
88 cs130_tom 1.5 if( bind(*sock, (const SOCKADDR *) addr, sizeof(*addr)) ) {
89 ok( 0 , "Error binding client to socket");
90 WSACleanup();
91 exit(0);
92 }
|
93 cs130_tom 1.4
|
94 cs130_tom 1.5 // get port number
95 tmpAddrSize = sizeof(tmpAddr);
96 getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
97 addr->sin_port = tmpAddr.sin_port;
98 }
|
99 cs130_tom 1.4
|
100 cs130_tom 1.6 void BlockingClient(int *serverPort)
|
101 cs130_tom 1.5 {
102 SOCKET sock;
|
103 cs130_tom 1.6 SOCKADDR_IN client, server;
104 HOSTENT *hp;
|
105 cs130_tom 1.5 StartNetworkApp(SOCK_STREAM, &sock, &client);
106
|
107 cs130_tom 1.6 hp = gethostbyname("localhost");
108
109 while(*serverPort == 0) ;
110
|
111 cs130_tom 1.5 // network code here
|
112 cs130_tom 1.6 server.sin_family = AF_INET;
113 server.sin_addr = *(struct in_addr *) hp->h_addr;
114 server.sin_port = *serverPort;
|
115 cs130_tom 1.4
|
116 cs130_tom 1.5 trace("blocking client done\n");
|
117 cs130_tom 1.7 clientsDone++;
|
118 rizwank 1.1 }
119
|
120 cs130_doug 1.8 void ProcessConnection(SOCKET ConnectedSocket)
121 {
122 // this will handle all connections to the server, it's in its own function to allow for multithreading
|
123 cs130_tom 1.10 int bClosed;
124 bClosed = close(ConnectedSocket);
125 //ok(bClosed,"Error closing socket");
|
126 cs130_doug 1.8 }
127
|
128 cs130_tom 1.6 void BlockingServer(int *port)
|
129 rizwank 1.3 {
|
130 cs130_tom 1.10 HANDLE* ServerThreads;
131 DWORD* ServerThreadIDs;
132 int ThreadIndex = 0;
133
134 SOCKET sock, ConnectedSocket;
|
135 cs130_tom 1.5 SOCKADDR_IN server;
136 StartNetworkApp(SOCK_STREAM, &sock, &server);
|
137 cs130_tom 1.6 *port = server.sin_port;
|
138 cs130_tom 1.9
|
139 cs130_tom 1.10 ServerThreads = malloc(sizeof(HANDLE) * NUM_CLIENTS);
140 memset(ServerThreads, 0, sizeof(HANDLE) * NUM_CLIENTS);
141
|
142 cs130_doug 1.8 ServerThreadIDs = malloc(sizeof(DWORD) * NUM_CLIENTS);
143 memset(ServerThreadIDs, 0, sizeof(DWORD) * NUM_CLIENTS);
144
145 //SOCKADDR_IN RemoteAddress;
146
147 // condition for how long we want the test to run goes here
148 {
149 ok(listen(sock, 5) != SOCKET_ERROR, "error listening on socket");
|
150 cs130_tom 1.10 //ok(INVALID_SOCKET != (ConnectedSocket = accept(sock)), "error accepting socket"); // this can be modified to include the address of the remote socket
151 ServerThreads[ThreadIndex] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &ProcessConnection, &ConnectedSocket, 0, &ServerThreadIDs[ThreadIndex]);
|
152 cs130_doug 1.8 // the line above needs to be cleaned up, it currently copies the connected socket into the called function
153 // and will then overwrite the local variable. I'm thinking maybe it needs an array of sockets to handle this
154 // and then it can pass the address of the socket.
155 }
|
156 cs130_tom 1.5
157 // network code here
158
|
159 cs130_doug 1.8 free(ServerThreads);
160 free(ServerThreadIDs);
|
161 cs130_tom 1.5 trace("blocking server done\n");
|
162 rizwank 1.3 }
163
|
164 cs130_tom 1.4 static void test_ClientServerBlocking_1(void)
|
165 rizwank 1.3 {
|
166 cs130_tom 1.9 int ThreadIndex = 0;
|
167 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
|
168 cs130_tom 1.10 HANDLE ServerThread;
169 DWORD ServerThreadId;
|
170 cs130_tom 1.9 DWORD *ClientThreadIds;
171 HANDLE *ClientThreads;
172
173 ServerThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, &serverPort, 0, &ServerThreadId);
174
|
175 cs130_tom 1.10 ClientThreads = malloc(sizeof(HANDLE) * NUM_CLIENTS);
176 memset(ClientThreads, 0, sizeof(HANDLE) * NUM_CLIENTS);
|
177 cs130_tom 1.9
178 ClientThreadIds = malloc(sizeof(DWORD) * NUM_CLIENTS);
179 memset(ClientThreadIds, 0, sizeof(DWORD) * NUM_CLIENTS);
180
181 for(ThreadIndex = 0; ThreadIndex < NUM_CLIENTS; ThreadIndex++) {
182 ClientThreads[ThreadIndex] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, &serverPort, 0, &ClientThreadIds[ThreadIndex]);
183 }
184
|
185 cs130_tom 1.5 trace("test_ClientServerBlocking_1 done\n");
|
186 rizwank 1.3 }
187
|
188 cs130_tom 1.4 static void test_Startup(void)
|
189 rizwank 1.3 {
|
190 cs130_tom 1.4 // initialize application
191 WSADATA wsaData;
192 int wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
193 if ( (LOBYTE(wsaData.wVersion) != 1) && (HIBYTE(wsaData.wVersion) != 1) )
194 {
195 ok( 0 , "WSAStartup returns an incompatible sockets version");
196 WSACleanup();
197 exit(0);
198 }
199
200 ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
|
201 rizwank 1.3 }
202
|
203 cs130_tom 1.4
|
204 rizwank 1.1 START_TEST(wsock32_main)
205 {
|
206 cs130_tom 1.4 trace("test 1 of 2:\n");
207 test_Startup();
208 trace("test 2 of 2:\n");
209 test_ClientServerBlocking_1();
210 trace("all tests done\n");
|
211 cs130_tom 1.7 while (clientsDone != NUM_CLIENTS)
212 ;
|
213 rizwank 1.1 }
|