Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /***********************************************************************
7 ** 1997 - Netscape Communications Corporation
8 **
9 ** Name: prselect_err.c
10 **
11 ** Description: tests PR_Select with sockets Error condition functions.
12 **
13 ** Modification History:
14 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
15 ** The debug mode will print all of the printfs associated with this test.
16 ** The regress mode will be the default mode. Since the regress tool limits
17 ** the output to a one line status:PASS or FAIL,all of the printf statements
18 ** have been handled with an if (debug_mode) statement.
19 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
20 ** recognize the return code from tha main program.
21 ***********************************************************************/
23 #ifdef XP_BEOS
24 #include <stdio.h>
25 int main()
26 {
27 printf( "This test is not ported to the BeOS\n" );
28 return 0;
29 }
30 #else
32 /***********************************************************************
33 ** Includes
34 ***********************************************************************/
35 /* Used to get the command line option */
36 #include "plgetopt.h"
38 #include "primpl.h"
39 #include "pprio.h"
40 #include "prnetdb.h"
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
47 PRIntn failed_already=0;
48 PRIntn debug_mode;
50 int main(int argc, char **argv)
51 {
52 PRFileDesc *listenSock1, *listenSock2;
53 PRFileDesc *badFD;
54 PRUint16 listenPort1, listenPort2;
55 PRNetAddr addr;
56 PR_fd_set readFdSet;
57 char buf[128];
58 PRInt32 retVal;
60 /* The command line argument: -d is used to determine if the test is being run
61 in debug mode. The regress tool requires only one line output:PASS or FAIL.
62 All of the printfs associated with this test has been handled with a if (debug_mode)
63 test.
64 Usage: test_name -d
65 */
66 PLOptStatus os;
67 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
68 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
69 {
70 if (PL_OPT_BAD == os) continue;
71 switch (opt->option)
72 {
73 case 'd': /* debug mode */
74 debug_mode = 1;
75 break;
76 default:
77 break;
78 }
79 }
80 PL_DestroyOptState(opt);
82 /* main test */
84 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
85 PR_STDIO_INIT();
87 if (debug_mode) {
88 printf("This program tests PR_Select with sockets. Error\n");
89 printf("reporting operations are tested.\n\n");
90 }
92 /* Create two listening sockets */
93 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
94 fprintf(stderr, "Can't create a new TCP socket\n");
95 failed_already=1;
96 goto exit_now;
97 }
98 addr.inet.family = AF_INET;
99 addr.inet.ip = PR_htonl(INADDR_ANY);
100 addr.inet.port = PR_htons(0);
101 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
102 fprintf(stderr, "Can't bind socket\n");
103 failed_already=1;
104 goto exit_now;
105 }
106 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
107 fprintf(stderr, "PR_GetSockName failed\n");
108 failed_already=1;
109 goto exit_now;
110 }
111 listenPort1 = PR_ntohs(addr.inet.port);
112 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
113 fprintf(stderr, "Can't listen on a socket\n");
114 failed_already=1;
115 goto exit_now;
116 }
118 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
119 fprintf(stderr, "Can't create a new TCP socket\n");
120 failed_already=1;
121 goto exit_now;
122 }
123 addr.inet.family = AF_INET;
124 addr.inet.ip = PR_htonl(INADDR_ANY);
125 addr.inet.port = PR_htons(0);
126 if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
127 fprintf(stderr, "Can't bind socket\n");
128 failed_already=1;
129 goto exit_now;
130 }
131 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
132 fprintf(stderr, "PR_GetSockName failed\n");
133 failed_already=1;
134 goto exit_now;
135 }
136 listenPort2 = PR_ntohs(addr.inet.port);
137 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
138 fprintf(stderr, "Can't listen on a socket\n");
139 failed_already=1;
140 goto exit_now;
141 }
142 PR_snprintf(buf, sizeof(buf),
143 "The server thread is listening on ports %hu and %hu\n\n",
144 listenPort1, listenPort2);
145 if (debug_mode) printf("%s", buf);
147 /* Set up the fd set */
148 PR_FD_ZERO(&readFdSet);
149 PR_FD_SET(listenSock1, &readFdSet);
150 PR_FD_SET(listenSock2, &readFdSet);
153 /* Testing bad fd */
154 if (debug_mode) printf("PR_Select should detect a bad file descriptor\n");
155 if ((badFD = PR_NewTCPSocket()) == NULL) {
156 fprintf(stderr, "Can't create a TCP socket\n");
157 failed_already=1;
158 goto exit_now;
159 }
161 PR_FD_SET(badFD, &readFdSet);
162 /*
163 * Make the fd invalid
164 */
165 #if defined(XP_UNIX)
166 close(PR_FileDesc2NativeHandle(badFD));
167 #elif defined(XP_OS2)
168 soclose(PR_FileDesc2NativeHandle(badFD));
169 #elif defined(WIN32) || defined(WIN16)
170 closesocket(PR_FileDesc2NativeHandle(badFD));
171 #else
172 #error "Unknown architecture"
173 #endif
175 retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
176 PR_INTERVAL_NO_TIMEOUT);
177 if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
178 fprintf(stderr, "Failed to detect the bad fd: "
179 "PR_Select returns %d\n", retVal);
180 if (retVal == -1) {
181 fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
182 PR_GetOSError());
183 failed_already=1;
184 }
185 goto exit_now;
186 }
187 if (debug_mode) printf("PR_Select detected a bad fd. Test passed.\n\n");
188 PR_FD_CLR(badFD, &readFdSet);
190 PR_Cleanup();
191 goto exit_now;
192 exit_now:
193 if(failed_already)
194 return 1;
195 else
196 return 0;
198 }
200 #endif /* XP_BEOS */