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 /***********************************************************************
42 ** Description: This program tests PR_Poll with sockets.
43 ** error reporting operation is 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 ***********************************************************************/
59 printf( "This test is not ported to the BeOS\n" );
64 /***********************************************************************
66 ***********************************************************************/
67 /* Used to get the command line option */
76 PRIntn failed_already
=0;
80 ClientThreadFunc(void *arg
)
82 PRFileDesc
*badFD
= (PRFileDesc
*) arg
;
87 close(PR_FileDesc2NativeHandle(badFD
));
89 soclose(PR_FileDesc2NativeHandle(badFD
));
90 #elif defined(WIN32) || defined(WIN16)
91 closesocket(PR_FileDesc2NativeHandle(badFD
));
93 #error "Unknown architecture"
97 int main(int argc
, char **argv
)
99 PRFileDesc
*listenSock1
, *listenSock2
;
101 PRUint16 listenPort1
, listenPort2
;
104 PRPollDesc pds0
[10], pds1
[10], *pds
, *other_pds
;
108 /* The command line argument: -d is used to determine if the test is being run
109 in debug mode. The regress tool requires only one line output:PASS or FAIL.
110 All of the printfs associated with this test has been handled with a if (debug_mode)
115 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "d:");
116 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
118 if (PL_OPT_BAD
== os
) continue;
121 case 'd': /* debug mode */
128 PL_DestroyOptState(opt
);
132 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
136 printf("This program tests PR_Poll with sockets.\n");
137 printf("error reporting is tested.\n\n");
140 /* Create two listening sockets */
141 if ((listenSock1
= PR_NewTCPSocket()) == NULL
) {
142 fprintf(stderr
, "Can't create a new TCP socket\n");
146 addr
.inet
.family
= AF_INET
;
147 addr
.inet
.ip
= PR_htonl(INADDR_ANY
);
148 addr
.inet
.port
= PR_htons(0);
149 if (PR_Bind(listenSock1
, &addr
) == PR_FAILURE
) {
150 fprintf(stderr
, "Can't bind socket\n");
154 if (PR_GetSockName(listenSock1
, &addr
) == PR_FAILURE
) {
155 fprintf(stderr
, "PR_GetSockName failed\n");
159 listenPort1
= PR_ntohs(addr
.inet
.port
);
160 if (PR_Listen(listenSock1
, 5) == PR_FAILURE
) {
161 fprintf(stderr
, "Can't listen on a socket\n");
166 if ((listenSock2
= PR_NewTCPSocket()) == NULL
) {
167 fprintf(stderr
, "Can't create a new TCP socket\n");
171 addr
.inet
.family
= AF_INET
;
172 addr
.inet
.ip
= PR_htonl(INADDR_ANY
);
173 addr
.inet
.port
= PR_htons(0);
174 if (PR_Bind(listenSock2
, &addr
) == PR_FAILURE
) {
175 fprintf(stderr
, "Can't bind socket\n");
179 if (PR_GetSockName(listenSock2
, &addr
) == PR_FAILURE
) {
180 fprintf(stderr
, "PR_GetSockName failed\n");
184 listenPort2
= PR_ntohs(addr
.inet
.port
);
185 if (PR_Listen(listenSock2
, 5) == PR_FAILURE
) {
186 fprintf(stderr
, "Can't listen on a socket\n");
190 PR_snprintf(buf
, sizeof(buf
),
191 "The server thread is listening on ports %hu and %hu\n\n",
192 listenPort1
, listenPort2
);
193 if (debug_mode
) printf("%s", buf
);
195 /* Set up the poll descriptor array */
198 memset(pds
, 0, sizeof(pds
));
199 pds
[0].fd
= listenSock1
;
200 pds
[0].in_flags
= PR_POLL_READ
;
201 pds
[1].fd
= listenSock2
;
202 pds
[1].in_flags
= PR_POLL_READ
;
207 if (debug_mode
) printf("PR_Poll should detect a bad file descriptor\n");
208 if ((badFD
= PR_NewTCPSocket()) == NULL
) {
209 fprintf(stderr
, "Can't create a TCP socket\n");
214 pds
[2].in_flags
= PR_POLL_READ
;
217 if (PR_CreateThread(PR_USER_THREAD
, ClientThreadFunc
,
218 badFD
, PR_PRIORITY_NORMAL
, PR_LOCAL_THREAD
,
219 PR_UNJOINABLE_THREAD
, 0) == NULL
) {
220 fprintf(stderr
, "cannot create thread\n");
224 retVal
= PR_Poll(pds
, npds
, PR_INTERVAL_NO_TIMEOUT
);
225 if (retVal
!= 1 || (unsigned short) pds
[2].out_flags
!= PR_POLL_NVAL
) {
226 fprintf(stderr
, "Failed to detect the bad fd: "
227 "PR_Poll returns %d, out_flags is 0x%hx\n",
228 retVal
, pds
[2].out_flags
);
232 if (debug_mode
) printf("PR_Poll detected the bad fd. Test passed.\n\n");