Debian Codegraph @

nsprpub/pr/tests/poll_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 **
8 ** Name: prpoll_norm.c
9 **
10 ** Description: This program tests PR_Poll with sockets.
11 ** Normal operation are tested
12 **
13 ** Modification History:
14 ** 19-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 "prnetdb.h"
38 #include "obsolete/probslet.h"
39
40 #include "private/pprio.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 #define NUM_ITERATIONS 5
50
51 static void PR_CALLBACK clientThreadFunc(void* arg) {
52 PRUintn port = (PRUintn)arg;
53 PRFileDesc* sock;
54 PRNetAddr addr;
55 char buf[128];
56 int i;
57 PRStatus sts;
58 PRInt32 n;
59
60 addr.inet.family = PR_AF_INET;
61 addr.inet.port = PR_htons((PRUint16)port);
62 addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
63 memset(buf, 0, sizeof(buf));
64 PR_snprintf(buf, sizeof(buf), "%hu", port);
65
66 for (i = 0; i < NUM_ITERATIONS; i++) {
67 sock = PR_NewTCPSocket();
68 PR_ASSERT(sock != NULL);
69
70 sts = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
71 PR_ASSERT(sts == PR_SUCCESS);
72
73 n = PR_Write(sock, buf, sizeof(buf));
74 PR_ASSERT(n >= 0);
75
76 sts = PR_Close(sock);
77 PR_ASSERT(sts == PR_SUCCESS);
78 }
79 }
80
81 int main(int argc, char** argv) {
82 PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
83 PRUint16 listenPort1, listenPort2;
84 PRNetAddr addr;
85 char buf[128];
86 PRThread* clientThread;
87 PRPollDesc pds0[20], pds1[20], *pds, *other_pds;
88 PRIntn npds;
89 PRInt32 retVal;
90 PRIntn i, j;
91 PRSocketOptionData optval;
92
93 /* The command line argument: -d is used to determine if the test is being run
94 in debug mode. The regress tool requires only one line output:PASS or FAIL.
95 All of the printfs associated with this test has been handled with a if
96 (debug_mode) test. Usage: test_name -d
97 */
98 PLOptStatus os;
99 PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
100 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
101 if (PL_OPT_BAD == os) {
102 continue;
103 }
104 switch (opt->option) {
105 case 'd': /* debug mode */
106 debug_mode = 1;
107 break;
108 default:
109 break;
110 }
111 }
112 PL_DestroyOptState(opt);
113
114 /* main test */
115
116 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
117 PR_STDIO_INIT();
118
119 if (debug_mode) {
120 printf("This program tests PR_Poll with sockets.\n");
121 printf("Normal operation are tested.\n\n");
122 }
123
124 /* Create two listening sockets */
125 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
126 fprintf(stderr, "Can't create a new TCP socket\n");
127 failed_already = 1;
128 goto exit_now;
129 }
130 memset(&addr, 0, sizeof(addr));
131 addr.inet.family = PR_AF_INET;
132 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
133 addr.inet.port = PR_htons(0);
134 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
135 fprintf(stderr, "Can't bind socket\n");
136 failed_already = 1;
137 goto exit_now;
138 }
139 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
140 fprintf(stderr, "PR_GetSockName failed\n");
141 failed_already = 1;
142 goto exit_now;
143 }
144 listenPort1 = PR_ntohs(addr.inet.port);
145 optval.option = PR_SockOpt_Nonblocking;
146 optval.value.non_blocking = PR_TRUE;
147 PR_SetSocketOption(listenSock1, &optval);
148 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
149 fprintf(stderr, "Can't listen on a socket\n");
150 failed_already = 1;
151 goto exit_now;
152 }
153
154 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
155 fprintf(stderr, "Can't create a new TCP socket\n");
156 failed_already = 1;
157 goto exit_now;
158 }
159 addr.inet.family = PR_AF_INET;
160 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
161 addr.inet.port = PR_htons(0);
162 if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
163 fprintf(stderr, "Can't bind socket\n");
164 failed_already = 1;
165 goto exit_now;
166 }
167 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
168 fprintf(stderr, "PR_GetSockName failed\n");
169 failed_already = 1;
170 goto exit_now;
171 }
172 listenPort2 = PR_ntohs(addr.inet.port);
173 PR_SetSocketOption(listenSock2, &optval);
174 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
175 fprintf(stderr, "Can't listen on a socket\n");
176 failed_already = 1;
177 goto exit_now;
178 }
179 PR_snprintf(buf, sizeof(buf),
180 "The server thread is listening on ports %hu and %hu\n\n",
181 listenPort1, listenPort2);
182 if (debug_mode) {
183 printf("%s", buf);
184 }
185
186 /* Set up the poll descriptor array */
187 pds = pds0;
188 other_pds = pds1;
189 memset(pds, 0, sizeof(pds));
190 pds[0].fd = listenSock1;
191 pds[0].in_flags = PR_POLL_READ;
192 pds[1].fd = listenSock2;
193 pds[1].in_flags = PR_POLL_READ;
194 /* Add some unused entries to test if they are ignored by PR_Poll() */
195 memset(&pds[2], 0, sizeof(pds[2]));
196 memset(&pds[3], 0, sizeof(pds[3]));
197 memset(&pds[4], 0, sizeof(pds[4]));
198 npds = 5;
199
200 clientThread = PR_CreateThread(PR_USER_THREAD, clientThreadFunc,
201 (void*)listenPort1, PR_PRIORITY_NORMAL,
202 PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0);
203 if (clientThread == NULL) {
204 fprintf(stderr, "can't create thread\n");
205 failed_already = 1;
206 goto exit_now;
207 }
208
209 clientThread = PR_CreateThread(PR_USER_THREAD, clientThreadFunc,
210 (void*)listenPort2, PR_PRIORITY_NORMAL,
211 PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0);
212 if (clientThread == NULL) {
213 fprintf(stderr, "can't create thread\n");
214 failed_already = 1;
215 goto exit_now;
216 }
217
218 if (debug_mode) {
219 printf("Two client threads are created. Each of them will\n");
220 printf("send data to one of the two ports the server is listening on.\n");
221 printf("The data they send is the port number. Each of them send\n");
222 printf("the data five times, so you should see ten lines below,\n");
223 printf("interleaved in an arbitrary order.\n");
224 }
225
226 /* two clients, three events per iteration: accept, read, close */
227 i = 0;
228 while (i < 2 * 3 * NUM_ITERATIONS) {
229 PRPollDesc* tmp;
230 int nextIndex;
231 int nEvents = 0;
232
233 retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
234 PR_ASSERT(retVal != 0); /* no timeout */
235 if (retVal == -1) {
236 fprintf(stderr, "PR_Poll failed\n");
237 failed_already = 1;
238 goto exit_now;
239 }
240
241 nextIndex = 2;
242 /* the two listening sockets */
243 for (j = 0; j < 2; j++) {
244 other_pds[j] = pds[j];
245 PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0 &&
246 (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
247 if (pds[j].out_flags & PR_POLL_READ) {
248 PRFileDesc* sock;
249
250 nEvents++;
251 sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
252 if (sock == NULL) {
253 fprintf(stderr, "PR_Accept() failed\n");
254 failed_already = 1;
255 goto exit_now;
256 }
257 other_pds[nextIndex].fd = sock;
258 other_pds[nextIndex].in_flags = PR_POLL_READ;
259 nextIndex++;
260 } else if (pds[j].out_flags & PR_POLL_ERR) {
261 fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
262 failed_already = 1;
263 goto exit_now;
264 } else if (pds[j].out_flags & PR_POLL_NVAL) {
265 fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
266 PR_FileDesc2NativeHandle(pds[j].fd));
267 failed_already = 1;
268 goto exit_now;
269 }
270 }
271
272 for (j = 2; j < npds; j++) {
273 if (NULL == pds[j].fd) {
274 /*
275 * Keep the unused entries in the poll descriptor array
276 * for testing purposes.
277 */
278 other_pds[nextIndex] = pds[j];
279 nextIndex++;
280 continue;
281 }
282
283 PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0 &&
284 (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
285 if (pds[j].out_flags & PR_POLL_READ) {
286 PRInt32 nAvail;
287 PRInt32 nRead;
288
289 nEvents++;
290 nAvail = PR_Available(pds[j].fd);
291 nRead = PR_Read(pds[j].fd, buf, sizeof(buf));
292 PR_ASSERT(nAvail == nRead);
293 if (nRead == -1) {
294 fprintf(stderr, "PR_Read() failed\n");
295 failed_already = 1;
296 goto exit_now;
297 } else if (nRead == 0) {
298 PR_Close(pds[j].fd);
299 continue;
300 } else {
301 /* Just to be safe */
302 buf[127] = '\0';
303 if (debug_mode) {
304 printf("The server received \"%s\" from a client\n", buf);
305 }
306 }
307 } else if (pds[j].out_flags & PR_POLL_ERR) {
308 fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
309 failed_already = 1;
310 goto exit_now;
311 } else if (pds[j].out_flags & PR_POLL_NVAL) {
312 fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
313 failed_already = 1;
314 goto exit_now;
315 }
316 other_pds[nextIndex] = pds[j];
317 nextIndex++;
318 }
319
320 PR_ASSERT(retVal == nEvents);
321 /* swap */
322 tmp = pds;
323 pds = other_pds;
324 other_pds = tmp;
325 npds = nextIndex;
326 i += nEvents;
327 }
328
329 if (debug_mode) {
330 printf("Tests passed\n");
331 }
332
333 exit_now:
334
335 if (listenSock1) {
336 PR_Close(listenSock1);
337 }
338 if (listenSock2) {
339 PR_Close(listenSock2);
340 }
341
342 PR_Cleanup();
343
344 if (failed_already) {
345 return 1;
346 } else {
347 return 0;
348 }
349 }