VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/poll_nm.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/***********************************************************************
39**
40** Name: prpoll_norm.c
41**
42** Description: This program tests PR_Poll with sockets.
43** Normal operation are tested
44**
45** Modification History:
46** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
47** The debug mode will print all of the printfs associated with this test.
48** The regress mode will be the default mode. Since the regress tool limits
49** the output to a one line status:PASS or FAIL,all of the printf statements
50** have been handled with an if (debug_mode) statement.
51** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
52** recognize the return code from tha main program.
53***********************************************************************/
54
55/***********************************************************************
56** Includes
57***********************************************************************/
58/* Used to get the command line option */
59#include "plgetopt.h"
60
61#include "prinit.h"
62#include "prio.h"
63#include "prlog.h"
64#include "prprf.h"
65#include "prnetdb.h"
66#ifndef XP_MAC
67#include "obsolete/probslet.h"
68#else
69#include "probslet.h"
70#endif
71
72#ifndef XP_MAC
73#include "private/pprio.h"
74#else
75#include "pprio.h"
76#endif
77
78#include <stdio.h>
79#include <string.h>
80#include <stdlib.h>
81PRIntn failed_already=0;
82PRIntn debug_mode;
83
84#define NUM_ITERATIONS 5
85
86#ifdef XP_MAC
87int fprintf(FILE *stream, const char *fmt, ...)
88{
89PR_LogPrint(fmt);
90return 0;
91}
92#define printf PR_LogPrint
93extern void SetupMacPrintfLog(char *logFile);
94#endif
95
96static void PR_CALLBACK
97clientThreadFunc(void *arg)
98{
99 PRUintn port = (PRUintn) arg;
100 PRFileDesc *sock;
101 PRNetAddr addr;
102 char buf[128];
103 int i;
104 PRStatus sts;
105 PRInt32 n;
106
107 addr.inet.family = PR_AF_INET;
108 addr.inet.port = PR_htons((PRUint16)port);
109 addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
110 memset(buf, 0, sizeof(buf));
111 PR_snprintf(buf, sizeof(buf), "%hu", port);
112
113 for (i = 0; i < NUM_ITERATIONS; i++) {
114 sock = PR_NewTCPSocket();
115 PR_ASSERT(sock != NULL);
116
117 sts = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
118 PR_ASSERT(sts == PR_SUCCESS);
119
120 n = PR_Write(sock, buf, sizeof(buf));
121 PR_ASSERT(n >= 0);
122
123 sts = PR_Close(sock);
124 PR_ASSERT(sts == PR_SUCCESS);
125 }
126}
127
128int main(int argc, char **argv)
129{
130 PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
131 PRUint16 listenPort1, listenPort2;
132 PRNetAddr addr;
133 char buf[128];
134 PRThread *clientThread;
135 PRPollDesc pds0[20], pds1[20], *pds, *other_pds;
136 PRIntn npds;
137 PRInt32 retVal;
138 PRIntn i, j;
139 PRSocketOptionData optval;
140
141 /* The command line argument: -d is used to determine if the test is being run
142 in debug mode. The regress tool requires only one line output:PASS or FAIL.
143 All of the printfs associated with this test has been handled with a if (debug_mode)
144 test.
145 Usage: test_name -d
146 */
147 PLOptStatus os;
148 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
149 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
150 {
151 if (PL_OPT_BAD == os) continue;
152 switch (opt->option)
153 {
154 case 'd': /* debug mode */
155 debug_mode = 1;
156 break;
157 default:
158 break;
159 }
160 }
161 PL_DestroyOptState(opt);
162
163 /* main test */
164
165 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
166 PR_STDIO_INIT();
167
168#ifdef XP_MAC
169 debug_mode = 1;
170 SetupMacPrintfLog("poll_nm.log");
171#endif
172
173 if (debug_mode) {
174 printf("This program tests PR_Poll with sockets.\n");
175 printf("Normal operation are tested.\n\n");
176 }
177
178 /* Create two listening sockets */
179 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
180 fprintf(stderr, "Can't create a new TCP socket\n");
181 failed_already=1;
182 goto exit_now;
183 }
184 memset(&addr, 0, sizeof(addr));
185 addr.inet.family = PR_AF_INET;
186 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
187 addr.inet.port = PR_htons(0);
188 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
189 fprintf(stderr, "Can't bind socket\n");
190 failed_already=1;
191 goto exit_now;
192 }
193 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
194 fprintf(stderr, "PR_GetSockName failed\n");
195 failed_already=1;
196 goto exit_now;
197 }
198 listenPort1 = PR_ntohs(addr.inet.port);
199 optval.option = PR_SockOpt_Nonblocking;
200 optval.value.non_blocking = PR_TRUE;
201 PR_SetSocketOption(listenSock1, &optval);
202 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
203 fprintf(stderr, "Can't listen on a socket\n");
204 failed_already=1;
205 goto exit_now;
206 }
207
208 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
209 fprintf(stderr, "Can't create a new TCP socket\n");
210 failed_already=1;
211 goto exit_now;
212 }
213 addr.inet.family = PR_AF_INET;
214 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
215 addr.inet.port = PR_htons(0);
216 if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
217 fprintf(stderr, "Can't bind socket\n");
218 failed_already=1;
219 goto exit_now;
220 }
221 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
222 fprintf(stderr, "PR_GetSockName failed\n");
223 failed_already=1;
224 goto exit_now;
225 }
226 listenPort2 = PR_ntohs(addr.inet.port);
227 PR_SetSocketOption(listenSock2, &optval);
228 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
229 fprintf(stderr, "Can't listen on a socket\n");
230 failed_already=1;
231 goto exit_now;
232 }
233 PR_snprintf(buf, sizeof(buf),
234 "The server thread is listening on ports %hu and %hu\n\n",
235 listenPort1, listenPort2);
236 if (debug_mode) printf("%s", buf);
237
238 /* Set up the poll descriptor array */
239 pds = pds0;
240 other_pds = pds1;
241 memset(pds, 0, sizeof(pds));
242 pds[0].fd = listenSock1;
243 pds[0].in_flags = PR_POLL_READ;
244 pds[1].fd = listenSock2;
245 pds[1].in_flags = PR_POLL_READ;
246 /* Add some unused entries to test if they are ignored by PR_Poll() */
247 memset(&pds[2], 0, sizeof(pds[2]));
248 memset(&pds[3], 0, sizeof(pds[3]));
249 memset(&pds[4], 0, sizeof(pds[4]));
250 npds = 5;
251
252 clientThread = PR_CreateThread(PR_USER_THREAD,
253 clientThreadFunc, (void *) listenPort1,
254 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
255 PR_UNJOINABLE_THREAD, 0);
256 if (clientThread == NULL) {
257 fprintf(stderr, "can't create thread\n");
258 failed_already=1;
259 goto exit_now;
260 }
261
262 clientThread = PR_CreateThread(PR_USER_THREAD,
263 clientThreadFunc, (void *) listenPort2,
264 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
265 PR_UNJOINABLE_THREAD, 0);
266 if (clientThread == NULL) {
267 fprintf(stderr, "can't create thread\n");
268 failed_already=1;
269 goto exit_now;
270 }
271
272 if (debug_mode) {
273 printf("Two client threads are created. Each of them will\n");
274 printf("send data to one of the two ports the server is listening on.\n");
275 printf("The data they send is the port number. Each of them send\n");
276 printf("the data five times, so you should see ten lines below,\n");
277 printf("interleaved in an arbitrary order.\n");
278 }
279
280 /* two clients, three events per iteration: accept, read, close */
281 i = 0;
282 while (i < 2 * 3 * NUM_ITERATIONS) {
283 PRPollDesc *tmp;
284 int nextIndex;
285 int nEvents = 0;
286
287 retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
288 PR_ASSERT(retVal != 0); /* no timeout */
289 if (retVal == -1) {
290 fprintf(stderr, "PR_Poll failed\n");
291 failed_already=1;
292 goto exit_now;
293 }
294
295 nextIndex = 2;
296 /* the two listening sockets */
297 for (j = 0; j < 2; j++) {
298 other_pds[j] = pds[j];
299 PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
300 && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
301 if (pds[j].out_flags & PR_POLL_READ) {
302 PRFileDesc *sock;
303
304 nEvents++;
305 sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
306 if (sock == NULL) {
307 fprintf(stderr, "PR_Accept() failed\n");
308 failed_already=1;
309 goto exit_now;
310 }
311 other_pds[nextIndex].fd = sock;
312 other_pds[nextIndex].in_flags = PR_POLL_READ;
313 nextIndex++;
314 } else if (pds[j].out_flags & PR_POLL_ERR) {
315 fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
316 failed_already=1;
317 goto exit_now;
318 } else if (pds[j].out_flags & PR_POLL_NVAL) {
319 fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
320 PR_FileDesc2NativeHandle(pds[j].fd));
321 failed_already=1;
322 goto exit_now;
323 }
324 }
325
326 for (j = 2; j < npds; j++) {
327 if (NULL == pds[j].fd) {
328 /*
329 * Keep the unused entries in the poll descriptor array
330 * for testing purposes.
331 */
332 other_pds[nextIndex] = pds[j];
333 nextIndex++;
334 continue;
335 }
336
337 PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
338 && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
339 if (pds[j].out_flags & PR_POLL_READ) {
340 PRInt32 nAvail;
341 PRInt32 nRead;
342
343 nEvents++;
344 nAvail = PR_Available(pds[j].fd);
345 nRead = PR_Read(pds[j].fd, buf, sizeof(buf));
346 PR_ASSERT(nAvail == nRead);
347 if (nRead == -1) {
348 fprintf(stderr, "PR_Read() failed\n");
349 failed_already=1;
350 goto exit_now;
351 } else if (nRead == 0) {
352 PR_Close(pds[j].fd);
353 continue;
354 } else {
355 /* Just to be safe */
356 buf[127] = '\0';
357 if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
358 }
359 } else if (pds[j].out_flags & PR_POLL_ERR) {
360 fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
361 failed_already=1;
362 goto exit_now;
363 } else if (pds[j].out_flags & PR_POLL_NVAL) {
364 fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
365 failed_already=1;
366 goto exit_now;
367 }
368 other_pds[nextIndex] = pds[j];
369 nextIndex++;
370 }
371
372 PR_ASSERT(retVal == nEvents);
373 /* swap */
374 tmp = pds;
375 pds = other_pds;
376 other_pds = tmp;
377 npds = nextIndex;
378 i += nEvents;
379 }
380
381 if (debug_mode) printf("Tests passed\n");
382
383exit_now:
384
385 if (listenSock1) {
386 PR_Close(listenSock1);
387 }
388 if (listenSock2) {
389 PR_Close(listenSock2);
390 }
391
392 PR_Cleanup();
393
394 if(failed_already)
395 return 1;
396 else
397 return 0;
398
399}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette