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 | ** 1997 - Netscape Communications Corporation
|
---|
40 | **
|
---|
41 | ** Name: prselect_err.c
|
---|
42 | **
|
---|
43 | ** Description: tests PR_Select with sockets Error condition functions.
|
---|
44 | **
|
---|
45 | ** Modification History:
|
---|
46 | ** 14-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 | #ifdef XP_BEOS
|
---|
56 | #include <stdio.h>
|
---|
57 | int main()
|
---|
58 | {
|
---|
59 | printf( "This test is not ported to the BeOS\n" );
|
---|
60 | return 0;
|
---|
61 | }
|
---|
62 | #else
|
---|
63 |
|
---|
64 | /***********************************************************************
|
---|
65 | ** Includes
|
---|
66 | ***********************************************************************/
|
---|
67 | /* Used to get the command line option */
|
---|
68 | #include "plgetopt.h"
|
---|
69 |
|
---|
70 | #include "primpl.h"
|
---|
71 | #include "pprio.h"
|
---|
72 | #include "prnetdb.h"
|
---|
73 |
|
---|
74 | #include <stdio.h>
|
---|
75 | #include <string.h>
|
---|
76 | #include <stdlib.h>
|
---|
77 |
|
---|
78 |
|
---|
79 | PRIntn failed_already=0;
|
---|
80 | PRIntn debug_mode;
|
---|
81 |
|
---|
82 | int main(int argc, char **argv)
|
---|
83 | {
|
---|
84 | PRFileDesc *listenSock1, *listenSock2;
|
---|
85 | PRFileDesc *badFD;
|
---|
86 | PRUint16 listenPort1, listenPort2;
|
---|
87 | PRNetAddr addr;
|
---|
88 | PR_fd_set readFdSet;
|
---|
89 | char buf[128];
|
---|
90 | PRInt32 retVal;
|
---|
91 |
|
---|
92 | /* The command line argument: -d is used to determine if the test is being run
|
---|
93 | in debug mode. The regress tool requires only one line output:PASS or FAIL.
|
---|
94 | All of the printfs associated with this test has been handled with a if (debug_mode)
|
---|
95 | test.
|
---|
96 | 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 | {
|
---|
102 | if (PL_OPT_BAD == os) continue;
|
---|
103 | switch (opt->option)
|
---|
104 | {
|
---|
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_Select with sockets. Error\n");
|
---|
121 | printf("reporting operations 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 | addr.inet.family = AF_INET;
|
---|
131 | addr.inet.ip = PR_htonl(INADDR_ANY);
|
---|
132 | addr.inet.port = PR_htons(0);
|
---|
133 | if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
|
---|
134 | fprintf(stderr, "Can't bind socket\n");
|
---|
135 | failed_already=1;
|
---|
136 | goto exit_now;
|
---|
137 | }
|
---|
138 | if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
|
---|
139 | fprintf(stderr, "PR_GetSockName failed\n");
|
---|
140 | failed_already=1;
|
---|
141 | goto exit_now;
|
---|
142 | }
|
---|
143 | listenPort1 = PR_ntohs(addr.inet.port);
|
---|
144 | if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
|
---|
145 | fprintf(stderr, "Can't listen on a socket\n");
|
---|
146 | failed_already=1;
|
---|
147 | goto exit_now;
|
---|
148 | }
|
---|
149 |
|
---|
150 | if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
|
---|
151 | fprintf(stderr, "Can't create a new TCP socket\n");
|
---|
152 | failed_already=1;
|
---|
153 | goto exit_now;
|
---|
154 | }
|
---|
155 | addr.inet.family = AF_INET;
|
---|
156 | addr.inet.ip = PR_htonl(INADDR_ANY);
|
---|
157 | addr.inet.port = PR_htons(0);
|
---|
158 | if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
|
---|
159 | fprintf(stderr, "Can't bind socket\n");
|
---|
160 | failed_already=1;
|
---|
161 | goto exit_now;
|
---|
162 | }
|
---|
163 | if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
|
---|
164 | fprintf(stderr, "PR_GetSockName failed\n");
|
---|
165 | failed_already=1;
|
---|
166 | goto exit_now;
|
---|
167 | }
|
---|
168 | listenPort2 = PR_ntohs(addr.inet.port);
|
---|
169 | if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
|
---|
170 | fprintf(stderr, "Can't listen on a socket\n");
|
---|
171 | failed_already=1;
|
---|
172 | goto exit_now;
|
---|
173 | }
|
---|
174 | PR_snprintf(buf, sizeof(buf),
|
---|
175 | "The server thread is listening on ports %hu and %hu\n\n",
|
---|
176 | listenPort1, listenPort2);
|
---|
177 | if (debug_mode) printf("%s", buf);
|
---|
178 |
|
---|
179 | /* Set up the fd set */
|
---|
180 | PR_FD_ZERO(&readFdSet);
|
---|
181 | PR_FD_SET(listenSock1, &readFdSet);
|
---|
182 | PR_FD_SET(listenSock2, &readFdSet);
|
---|
183 |
|
---|
184 |
|
---|
185 | /* Testing bad fd */
|
---|
186 | if (debug_mode) printf("PR_Select should detect a bad file descriptor\n");
|
---|
187 | if ((badFD = PR_NewTCPSocket()) == NULL) {
|
---|
188 | fprintf(stderr, "Can't create a TCP socket\n");
|
---|
189 | failed_already=1;
|
---|
190 | goto exit_now;
|
---|
191 | }
|
---|
192 |
|
---|
193 | PR_FD_SET(badFD, &readFdSet);
|
---|
194 | /*
|
---|
195 | * Make the fd invalid
|
---|
196 | */
|
---|
197 | #if defined(XP_UNIX)
|
---|
198 | close(PR_FileDesc2NativeHandle(badFD));
|
---|
199 | #elif defined(XP_OS2)
|
---|
200 | soclose(PR_FileDesc2NativeHandle(badFD));
|
---|
201 | #elif defined(WIN32) || defined(WIN16)
|
---|
202 | closesocket(PR_FileDesc2NativeHandle(badFD));
|
---|
203 | #elif defined(XP_MAC)
|
---|
204 | _PR_MD_CLOSE_SOCKET(PR_FileDesc2NativeHandle(badFD));
|
---|
205 | #else
|
---|
206 | #error "Unknown architecture"
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
|
---|
210 | PR_INTERVAL_NO_TIMEOUT);
|
---|
211 | if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
|
---|
212 | fprintf(stderr, "Failed to detect the bad fd: "
|
---|
213 | "PR_Select returns %d\n", retVal);
|
---|
214 | if (retVal == -1) {
|
---|
215 | fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
|
---|
216 | PR_GetOSError());
|
---|
217 | failed_already=1;
|
---|
218 | }
|
---|
219 | goto exit_now;
|
---|
220 | }
|
---|
221 | if (debug_mode) printf("PR_Select detected a bad fd. Test passed.\n\n");
|
---|
222 | PR_FD_CLR(badFD, &readFdSet);
|
---|
223 |
|
---|
224 | PR_Cleanup();
|
---|
225 | goto exit_now;
|
---|
226 | exit_now:
|
---|
227 | if(failed_already)
|
---|
228 | return 1;
|
---|
229 | else
|
---|
230 | return 0;
|
---|
231 |
|
---|
232 | }
|
---|
233 |
|
---|
234 | #endif /* XP_BEOS */
|
---|