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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
38 /***********************************************************************
40 ** Name: prpoll_norm.c
42 ** Description: This program tests PR_Poll with sockets.
43 ** Normal operation are tested
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 ***********************************************************************/
55 /***********************************************************************
57 ***********************************************************************/
58 /* Used to get the command line option */
67 #include "obsolete/probslet.h"
73 #include "private/pprio.h"
81 PRIntn failed_already
=0;
84 #define NUM_ITERATIONS 5
87 int fprintf(FILE *stream
, const char *fmt
, ...)
92 #define printf PR_LogPrint
93 extern void SetupMacPrintfLog(char *logFile
);
96 static void PR_CALLBACK
97 clientThreadFunc(void *arg
)
99 PRUintn port
= (PRUintn
) arg
;
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
);
113 for (i
= 0; i
< NUM_ITERATIONS
; i
++) {
114 sock
= PR_NewTCPSocket();
115 PR_ASSERT(sock
!= NULL
);
117 sts
= PR_Connect(sock
, &addr
, PR_INTERVAL_NO_TIMEOUT
);
118 PR_ASSERT(sts
== PR_SUCCESS
);
120 n
= PR_Write(sock
, buf
, sizeof(buf
));
123 sts
= PR_Close(sock
);
124 PR_ASSERT(sts
== PR_SUCCESS
);
128 int main(int argc
, char **argv
)
130 PRFileDesc
*listenSock1
= NULL
, *listenSock2
= NULL
;
131 PRUint16 listenPort1
, listenPort2
;
134 PRThread
*clientThread
;
135 PRPollDesc pds0
[20], pds1
[20], *pds
, *other_pds
;
139 PRSocketOptionData optval
;
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)
148 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "d:");
149 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
151 if (PL_OPT_BAD
== os
) continue;
154 case 'd': /* debug mode */
161 PL_DestroyOptState(opt
);
165 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
170 SetupMacPrintfLog("poll_nm.log");
174 printf("This program tests PR_Poll with sockets.\n");
175 printf("Normal operation are tested.\n\n");
178 /* Create two listening sockets */
179 if ((listenSock1
= PR_NewTCPSocket()) == NULL
) {
180 fprintf(stderr
, "Can't create a new TCP socket\n");
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");
193 if (PR_GetSockName(listenSock1
, &addr
) == PR_FAILURE
) {
194 fprintf(stderr
, "PR_GetSockName failed\n");
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");
208 if ((listenSock2
= PR_NewTCPSocket()) == NULL
) {
209 fprintf(stderr
, "Can't create a new TCP socket\n");
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");
221 if (PR_GetSockName(listenSock2
, &addr
) == PR_FAILURE
) {
222 fprintf(stderr
, "PR_GetSockName failed\n");
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");
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
);
238 /* Set up the poll descriptor array */
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]));
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");
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");
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");
280 /* two clients, three events per iteration: accept, read, close */
282 while (i
< 2 * 3 * NUM_ITERATIONS
) {
287 retVal
= PR_Poll(pds
, npds
, PR_INTERVAL_NO_TIMEOUT
);
288 PR_ASSERT(retVal
!= 0); /* no timeout */
290 fprintf(stderr
, "PR_Poll failed\n");
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
) {
305 sock
= PR_Accept(pds
[j
].fd
, NULL
, PR_INTERVAL_NO_TIMEOUT
);
307 fprintf(stderr
, "PR_Accept() failed\n");
311 other_pds
[nextIndex
].fd
= sock
;
312 other_pds
[nextIndex
].in_flags
= PR_POLL_READ
;
314 } else if (pds
[j
].out_flags
& PR_POLL_ERR
) {
315 fprintf(stderr
, "PR_Poll() indicates that an fd has error\n");
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
));
326 for (j
= 2; j
< npds
; j
++) {
327 if (NULL
== pds
[j
].fd
) {
329 * Keep the unused entries in the poll descriptor array
330 * for testing purposes.
332 other_pds
[nextIndex
] = pds
[j
];
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
) {
344 nAvail
= PR_Available(pds
[j
].fd
);
345 nRead
= PR_Read(pds
[j
].fd
, buf
, sizeof(buf
));
346 PR_ASSERT(nAvail
== nRead
);
348 fprintf(stderr
, "PR_Read() failed\n");
351 } else if (nRead
== 0) {
355 /* Just to be safe */
357 if (debug_mode
) printf("The server received \"%s\" from a client\n", buf
);
359 } else if (pds
[j
].out_flags
& PR_POLL_ERR
) {
360 fprintf(stderr
, "PR_Poll() indicates that an fd has error\n");
363 } else if (pds
[j
].out_flags
& PR_POLL_NVAL
) {
364 fprintf(stderr
, "PR_Poll() indicates that an fd is invalid\n");
368 other_pds
[nextIndex
] = pds
[j
];
372 PR_ASSERT(retVal
== nEvents
);
381 if (debug_mode
) printf("Tests passed\n");
386 PR_Close(listenSock1
);
389 PR_Close(listenSock2
);