Debian Codegraph @

nsprpub/pr/tests/selct_nm.c

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 /***********************************************************************
7 ** 1997 - Netscape Communications Corporation
8 **
9 ** Name: prselect_norm.c
10 **
11 ** Description: tests PR_Select with sockets - Normal operations.
12 **
13 ** Modification History:
14 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
15 ** The debug mode will print all of the printfs associated with this
16 *test.
17 ** The regress mode will be the default mode. Since the regress tool
18 *limits
19 ** the output to a one line status:PASS or FAIL,all of the printf
20 *statements
21 ** have been handled with an if (debug_mode) statement.
22 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been
23 *updated to
24 ** recognize the return code from tha main program.
25 ***********************************************************************/
26
27 /***********************************************************************
28 ** Includes
29 ***********************************************************************/
30 /* Used to get the command line option */
31 #include "plgetopt.h"
32
33 #include "prinit.h"
34 #include "prio.h"
35 #include "prlog.h"
36 #include "prprf.h"
37 #include "prerror.h"
38 #include "prnetdb.h"
39
40 #include "obsolete/probslet.h"
41
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45
46 PRIntn failed_already = 0;
47 PRIntn debug_mode;
48
49 static void clientThreadFunc(void* arg) {
50 PRUintn port = (PRUintn)arg;
51 PRFileDesc* sock;
52 PRNetAddr addr;
53 char buf[128];
54 int i;
55
56 addr.inet.family = PR_AF_INET;
57 addr.inet.port = PR_htons((PRUint16)port);
58 addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
59 PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.port);
60
61 for (i = 0; i < 5; i++) {
62 sock = PR_NewTCPSocket();
63 PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
64 PR_Write(sock, buf, sizeof(buf));
65 PR_Close(sock);
66 }
67 }
68
69 int main(int argc, char** argv) {
70 PRFileDesc *listenSock1, *listenSock2;
71 PRFileDesc *fds0[10], *fds1[10], **fds, **other_fds;
72 PRIntn nfds;
73 PRUint16 listenPort1, listenPort2;
74 PRNetAddr addr;
75 PR_fd_set readFdSet;
76 char buf[128];
77 PRThread* clientThread;
78 PRInt32 retVal;
79 PRIntn i, j;
80
81 /* The command line argument: -d is used to determine if the test is being run
82 in debug mode. The regress tool requires only one line output:PASS or FAIL.
83 All of the printfs associated with this test has been handled with a if
84 (debug_mode) test. Usage: test_name -d
85 */
86 PLOptStatus os;
87 PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
88 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
89 if (PL_OPT_BAD == os) {
90 continue;
91 }
92 switch (opt->option) {
93 case 'd': /* debug mode */
94 debug_mode = 1;
95 break;
96 default:
97 break;
98 }
99 }
100 PL_DestroyOptState(opt);
101
102 /* main test */
103
104 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
105 PR_STDIO_INIT();
106
107 if (debug_mode) {
108 printf("This program tests PR_Select with sockets. \n");
109 printf(" Normal operation are tested.\n\n");
110 }
111
112 /* Create two listening sockets */
113 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
114 fprintf(stderr, "Can't create a new TCP socket\n");
115 failed_already = 1;
116 goto exit_now;
117 }
118 addr.inet.family = PR_AF_INET;
119 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
120 addr.inet.port = PR_htons(0);
121 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
122 fprintf(stderr, "Can't bind socket\n");
123 failed_already = 1;
124 goto exit_now;
125 }
126 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
127 fprintf(stderr, "PR_GetSockName failed\n");
128 failed_already = 1;
129 goto exit_now;
130 }
131 listenPort1 = PR_ntohs(addr.inet.port);
132 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
133 fprintf(stderr, "Can't listen on a socket\n");
134 failed_already = 1;
135 goto exit_now;
136 }
137
138 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
139 fprintf(stderr, "Can't create a new TCP socket\n");
140 failed_already = 1;
141 goto exit_now;
142 }
143 addr.inet.family = PR_AF_INET;
144 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
145 addr.inet.port = PR_htons(0);
146 if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
147 fprintf(stderr, "Can't bind socket\n");
148 failed_already = 1;
149 goto exit_now;
150 }
151 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
152 fprintf(stderr, "PR_GetSockName failed\n");
153 failed_already = 1;
154 goto exit_now;
155 }
156 listenPort2 = PR_ntohs(addr.inet.port);
157 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
158 fprintf(stderr, "Can't listen on a socket\n");
159 failed_already = 1;
160 goto exit_now;
161 }
162 PR_snprintf(buf, sizeof(buf),
163 "The server thread is listening on ports %hu and %hu\n\n",
164 listenPort1, listenPort2);
165 if (debug_mode) {
166 printf("%s", buf);
167 }
168
169 clientThread = PR_CreateThread(PR_USER_THREAD, clientThreadFunc,
170 (void*)listenPort1, PR_PRIORITY_NORMAL,
171 PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0);
172 if (clientThread == NULL) {
173 fprintf(stderr, "can't create thread\n");
174 failed_already = 1;
175 goto exit_now;
176 }
177
178 clientThread = PR_CreateThread(PR_USER_THREAD, clientThreadFunc,
179 (void*)listenPort2, PR_PRIORITY_NORMAL,
180 PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0);
181 if (clientThread == NULL) {
182 fprintf(stderr, "can't create thread\n");
183 failed_already = 1;
184 goto exit_now;
185 }
186
187 if (debug_mode) {
188 printf("Two client threads are created. Each of them will\n");
189 printf("send data to one of the two ports the server is listening on.\n");
190 printf("The data they send is the port number. Each of them send\n");
191 printf("the data five times, so you should see ten lines below,\n");
192 printf("interleaved in an arbitrary order.\n");
193 }
194 /* set up the fd array */
195 fds = fds0;
196 other_fds = fds1;
197 fds[0] = listenSock1;
198 fds[1] = listenSock2;
199 nfds = 2;
200 /* Set up the fd set */
201 PR_FD_ZERO(&readFdSet);
202 PR_FD_SET(listenSock1, &readFdSet);
203 PR_FD_SET(listenSock2, &readFdSet);
204
205 /* 20 events total */
206 i = 0;
207 while (i < 20) {
208 PRFileDesc** tmp;
209 int nextIndex;
210 int nEvents = 0;
211
212 retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
213 PR_INTERVAL_NO_TIMEOUT);
214 PR_ASSERT(retVal != 0); /* no timeout */
215 if (retVal == -1) {
216 fprintf(stderr, "PR_Select failed (%d, %d)\n", PR_GetError(),
217 PR_GetOSError());
218 failed_already = 1;
219 goto exit_now;
220 }
221
222 nextIndex = 2;
223 /* the two listening sockets */
224 for (j = 0; j < 2; j++) {
225 other_fds[j] = fds[j];
226 if (PR_FD_ISSET(fds[j], &readFdSet)) {
227 PRFileDesc* sock;
228
229 nEvents++;
230 sock = PR_Accept(fds[j], NULL, PR_INTERVAL_NO_TIMEOUT);
231 if (sock == NULL) {
232 fprintf(stderr, "PR_Accept() failed\n");
233 failed_already = 1;
234 goto exit_now;
235 }
236 other_fds[nextIndex] = sock;
237 PR_FD_SET(sock, &readFdSet);
238 nextIndex++;
239 }
240 PR_FD_SET(fds[j], &readFdSet);
241 }
242
243 for (j = 2; j < nfds; j++) {
244 if (PR_FD_ISSET(fds[j], &readFdSet)) {
245 PRInt32 nBytes;
246
247 PR_FD_CLR(fds[j], &readFdSet);
248 nEvents++;
249 nBytes = PR_Read(fds[j], buf, sizeof(buf));
250 if (nBytes == -1) {
251 fprintf(stderr, "PR_Read() failed\n");
252 failed_already = 1;
253 goto exit_now;
254 }
255 /* Just to be safe */
256 buf[127] = '\0';
257 PR_Close(fds[j]);
258 if (debug_mode) {
259 printf("The server received \"%s\" from a client\n", buf);
260 }
261 } else {
262 PR_FD_SET(fds[j], &readFdSet);
263 other_fds[nextIndex] = fds[j];
264 nextIndex++;
265 }
266 }
267
268 PR_ASSERT(retVal == nEvents);
269 /* swap */
270 tmp = fds;
271 fds = other_fds;
272 other_fds = tmp;
273 nfds = nextIndex;
274 i += nEvents;
275 }
276
277 if (debug_mode) {
278 printf("Test passed\n");
279 }
280
281 PR_Cleanup();
282 goto exit_now;
283 exit_now:
284 if (failed_already) {
285 return 1;
286 } else {
287 return 0;
288 }
289 }