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.7 #define NUM_CLIENTS 1
|
46 cs130_tom 1.6 // amount of data to transfer from each client to server
47 #define TRANSFER_SIZE 1000000
|
48 cs130_tom 1.4
|
49 cs130_tom 1.7 int clientsDone = 0;
50
|
51 cs130_tom 1.4 struct TestParams {
|
52 cs130_tom 1.5 int serverSock;
53 int serverType;
|
54 cs130_tom 1.4 int serverPort;
55 int clientPort[NUM_CLIENTS];
56 };
57
58 struct ClientParams {
59 struct TestParams *test;
60 int clientNum; // 1...NUM_CLIENTS
61 };
62
63 static void test_Startup(void);
|
64 cs130_tom 1.6 void BlockingClient(int *serverPort);
65 void BlockingServer(int *port);
|
66 cs130_tom 1.4 static void test_ClientServerBlocking_1(void);
67 static void test_Startup(void);
|
68 rizwank 1.1
|
69 cs130_tom 1.5 // StartNetworkApp creates socket sock of type type and returns assigned port number in addr.
70 void StartNetworkApp(int type, SOCKET *sock, SOCKADDR_IN *addr)
|
71 rizwank 1.1 {
|
72 cs130_tom 1.5 SOCKADDR_IN tmpAddr;
73 int tmpAddrSize;
|
74 cs130_tom 1.4
|
75 cs130_tom 1.5 *sock = socket(AF_INET, type, 0);
76 if (*sock == INVALID_SOCKET) {
|
77 cs130_tom 1.4 ok( 0 , "Error in socket()");
78 WSACleanup();
79 exit(0);
80 }
81 trace("socket() ok\n");
82
|
83 cs130_tom 1.5 addr->sin_family = AF_INET;
84 addr->sin_addr.s_addr = INADDR_ANY;
85 addr->sin_port = htons(0);
|
86 cs130_tom 1.4
|
87 cs130_tom 1.5 if( bind(*sock, (const SOCKADDR *) addr, sizeof(*addr)) ) {
88 ok( 0 , "Error binding client to socket");
89 WSACleanup();
90 exit(0);
91 }
|
92 cs130_tom 1.4
|
93 cs130_tom 1.5 // get port number
94 tmpAddrSize = sizeof(tmpAddr);
95 getsockname(*sock, (SOCKADDR *) &tmpAddr, &tmpAddrSize);
96 addr->sin_port = tmpAddr.sin_port;
97 }
|
98 cs130_tom 1.4
|
99 cs130_tom 1.6 void BlockingClient(int *serverPort)
|
100 cs130_tom 1.5 {
101 SOCKET sock;
|
102 cs130_tom 1.6 SOCKADDR_IN client, server;
103 HOSTENT *hp;
|
104 cs130_tom 1.5 StartNetworkApp(SOCK_STREAM, &sock, &client);
105
|
106 cs130_tom 1.6 hp = gethostbyname("localhost");
107
108 while(*serverPort == 0) ;
109
|
110 cs130_tom 1.5 // network code here
|
111 cs130_tom 1.6 server.sin_family = AF_INET;
112 server.sin_addr = *(struct in_addr *) hp->h_addr;
113 server.sin_port = *serverPort;
|
114 cs130_tom 1.4
|
115 cs130_tom 1.5 trace("blocking client done\n");
|
116 cs130_tom 1.7 clientsDone++;
|
117 rizwank 1.1 }
118
|
119 cs130_tom 1.6 void BlockingServer(int *port)
|
120 rizwank 1.3 {
|
121 cs130_tom 1.5 SOCKET sock;
122 SOCKADDR_IN server;
123 StartNetworkApp(SOCK_STREAM, &sock, &server);
|
124 cs130_tom 1.6 *port = server.sin_port;
|
125 cs130_tom 1.5
126 // network code here
127
128 trace("blocking server done\n");
|
129 rizwank 1.3 }
130
|
131 cs130_tom 1.4 static void test_ClientServerBlocking_1(void)
|
132 rizwank 1.3 {
|
133 cs130_tom 1.6 int serverPort = 0;
|
134 cs130_tom 1.4 HANDLE Thread1, Thread2;
135 DWORD ThreadId1, ThreadId2;
|
136 cs130_tom 1.6 Thread1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingClient, &serverPort, 0, &ThreadId1);
137 Thread2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) &BlockingServer, &serverPort, 0, &ThreadId2);
|
138 cs130_tom 1.5 trace("test_ClientServerBlocking_1 done\n");
|
139 rizwank 1.3 }
140
|
141 cs130_tom 1.4 static void test_Startup(void)
|
142 rizwank 1.3 {
|
143 cs130_tom 1.4 // initialize application
144 WSADATA wsaData;
145 int wsastartup_result = WSAStartup(MAKEWORD(1,1), &wsaData);
146 if ( (LOBYTE(wsaData.wVersion) != 1) && (HIBYTE(wsaData.wVersion) != 1) )
147 {
148 ok( 0 , "WSAStartup returns an incompatible sockets version");
149 WSACleanup();
150 exit(0);
151 }
152
153 ok((wsastartup_result == NO_ERROR), "Error in WSAStartup()");
|
154 rizwank 1.3 }
155
|
156 cs130_tom 1.4
|
157 rizwank 1.1 START_TEST(wsock32_main)
158 {
|
159 cs130_tom 1.4 trace("test 1 of 2:\n");
160 test_Startup();
161 trace("test 2 of 2:\n");
162 test_ClientServerBlocking_1();
163 trace("all tests done\n");
|
164 cs130_tom 1.7 while (clientsDone != NUM_CLIENTS)
165 ;
|
166 rizwank 1.1 }
|