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