1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is the Netscape Portable Runtime (NSPR).
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
22
* Alternatively, the contents of this file may be used under the
23
* terms of the GNU General Public License Version 2 or later (the
24
* "GPL"), in which case the provisions of the GPL are applicable
25
* instead of those above. If you wish to allow use of your
26
* version of this file only under the terms of the GPL and not to
27
* allow others to use your version of this file under the MPL,
28
* indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by
30
* the GPL. If you do not delete the provisions above, a recipient
31
* may use your version of this file under either the MPL or the
35
/***********************************************************************
36
** 1997 - Netscape Communications Corporation
38
** Name: prselect_err.c
40
** Description: tests PR_Select with sockets Error condition functions.
42
** Modification History:
43
** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
44
** The debug mode will print all of the printfs associated with this test.
45
** The regress mode will be the default mode. Since the regress tool limits
46
** the output to a one line status:PASS or FAIL,all of the printf statements
47
** have been handled with an if (debug_mode) statement.
48
** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
49
** recognize the return code from tha main program.
50
***********************************************************************/
56
printf( "This test is not ported to the BeOS\n" );
61
/***********************************************************************
63
***********************************************************************/
64
/* Used to get the command line option */
76
PRIntn failed_already=0;
79
int main(int argc, char **argv)
81
PRFileDesc *listenSock1, *listenSock2;
83
PRUint16 listenPort1, listenPort2;
89
/* The command line argument: -d is used to determine if the test is being run
90
in debug mode. The regress tool requires only one line output:PASS or FAIL.
91
All of the printfs associated with this test has been handled with a if (debug_mode)
96
PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
97
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
99
if (PL_OPT_BAD == os) continue;
102
case 'd': /* debug mode */
109
PL_DestroyOptState(opt);
113
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
117
printf("This program tests PR_Select with sockets. Error\n");
118
printf("reporting operations are tested.\n\n");
121
/* Create two listening sockets */
122
if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
123
fprintf(stderr, "Can't create a new TCP socket\n");
127
addr.inet.family = AF_INET;
128
addr.inet.ip = PR_htonl(INADDR_ANY);
129
addr.inet.port = PR_htons(0);
130
if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
131
fprintf(stderr, "Can't bind socket\n");
135
if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
136
fprintf(stderr, "PR_GetSockName failed\n");
140
listenPort1 = PR_ntohs(addr.inet.port);
141
if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
142
fprintf(stderr, "Can't listen on a socket\n");
147
if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
148
fprintf(stderr, "Can't create a new TCP socket\n");
152
addr.inet.family = AF_INET;
153
addr.inet.ip = PR_htonl(INADDR_ANY);
154
addr.inet.port = PR_htons(0);
155
if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
156
fprintf(stderr, "Can't bind socket\n");
160
if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
161
fprintf(stderr, "PR_GetSockName failed\n");
165
listenPort2 = PR_ntohs(addr.inet.port);
166
if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
167
fprintf(stderr, "Can't listen on a socket\n");
171
PR_snprintf(buf, sizeof(buf),
172
"The server thread is listening on ports %hu and %hu\n\n",
173
listenPort1, listenPort2);
174
if (debug_mode) printf("%s", buf);
176
/* Set up the fd set */
177
PR_FD_ZERO(&readFdSet);
178
PR_FD_SET(listenSock1, &readFdSet);
179
PR_FD_SET(listenSock2, &readFdSet);
183
if (debug_mode) printf("PR_Select should detect a bad file descriptor\n");
184
if ((badFD = PR_NewTCPSocket()) == NULL) {
185
fprintf(stderr, "Can't create a TCP socket\n");
190
PR_FD_SET(badFD, &readFdSet);
192
* Make the fd invalid
195
close(PR_FileDesc2NativeHandle(badFD));
196
#elif defined(XP_OS2)
197
soclose(PR_FileDesc2NativeHandle(badFD));
198
#elif defined(WIN32) || defined(WIN16)
199
closesocket(PR_FileDesc2NativeHandle(badFD));
200
#elif defined(XP_MAC)
201
_PR_MD_CLOSE_SOCKET(PR_FileDesc2NativeHandle(badFD));
203
#error "Unknown architecture"
206
retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
207
PR_INTERVAL_NO_TIMEOUT);
208
if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
209
fprintf(stderr, "Failed to detect the bad fd: "
210
"PR_Select returns %d\n", retVal);
212
fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
218
if (debug_mode) printf("PR_Select detected a bad fd. Test passed.\n\n");
219
PR_FD_CLR(badFD, &readFdSet);