Debian Codegraph @

nsprpub/pr/tests/selct_er.c

raw
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/. */
5
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
16 *test.
17 ** The regress mode will be the default mode. Since the regress tool
18 *limits
19 ** the output to a one line status:PASS or FAIL,all of the printf
20 *statements
21 ** have been handled with an if (debug_mode) statement.
22 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been
23 *updated to
24 ** recognize the return code from tha main program.
25 ***********************************************************************/
26
27 /***********************************************************************
28 ** Includes
29 ***********************************************************************/
30 /* Used to get the command line option */
31 #include "plgetopt.h"
32
33 #include "primpl.h"
34 #include "pprio.h"
35 #include "prnetdb.h"
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40
41 PRIntn failed_already = 0;
42 PRIntn debug_mode;
43
44 int main(int argc, char** argv) {
45 PRFileDesc *listenSock1, *listenSock2;
46 PRFileDesc* badFD;
47 PRUint16 listenPort1, listenPort2;
48 PRNetAddr addr;
49 PR_fd_set readFdSet;
50 char buf[128];
51 PRInt32 retVal;
52
53 /* The command line argument: -d is used to determine if the test is being run
54 in debug mode. The regress tool requires only one line output:PASS or FAIL.
55 All of the printfs associated with this test has been handled with a if
56 (debug_mode) test. Usage: test_name -d
57 */
58 PLOptStatus os;
59 PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
60 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
61 if (PL_OPT_BAD == os) {
62 continue;
63 }
64 switch (opt->option) {
65 case 'd': /* debug mode */
66 debug_mode = 1;
67 break;
68 default:
69 break;
70 }
71 }
72 PL_DestroyOptState(opt);
73
74 /* main test */
75
76 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
77 PR_STDIO_INIT();
78
79 if (debug_mode) {
80 printf("This program tests PR_Select with sockets. Error\n");
81 printf("reporting operations are tested.\n\n");
82 }
83
84 /* Create two listening sockets */
85 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
86 fprintf(stderr, "Can't create a new TCP socket\n");
87 failed_already = 1;
88 goto exit_now;
89 }
90 addr.inet.family = AF_INET;
91 addr.inet.ip = PR_htonl(INADDR_ANY);
92 addr.inet.port = PR_htons(0);
93 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
94 fprintf(stderr, "Can't bind socket\n");
95 failed_already = 1;
96 goto exit_now;
97 }
98 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
99 fprintf(stderr, "PR_GetSockName failed\n");
100 failed_already = 1;
101 goto exit_now;
102 }
103 listenPort1 = PR_ntohs(addr.inet.port);
104 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
105 fprintf(stderr, "Can't listen on a socket\n");
106 failed_already = 1;
107 goto exit_now;
108 }
109
110 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
111 fprintf(stderr, "Can't create a new TCP socket\n");
112 failed_already = 1;
113 goto exit_now;
114 }
115 addr.inet.family = AF_INET;
116 addr.inet.ip = PR_htonl(INADDR_ANY);
117 addr.inet.port = PR_htons(0);
118 if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
119 fprintf(stderr, "Can't bind socket\n");
120 failed_already = 1;
121 goto exit_now;
122 }
123 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
124 fprintf(stderr, "PR_GetSockName failed\n");
125 failed_already = 1;
126 goto exit_now;
127 }
128 listenPort2 = PR_ntohs(addr.inet.port);
129 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
130 fprintf(stderr, "Can't listen on a socket\n");
131 failed_already = 1;
132 goto exit_now;
133 }
134 PR_snprintf(buf, sizeof(buf),
135 "The server thread is listening on ports %hu and %hu\n\n",
136 listenPort1, listenPort2);
137 if (debug_mode) {
138 printf("%s", buf);
139 }
140
141 /* Set up the fd set */
142 PR_FD_ZERO(&readFdSet);
143 PR_FD_SET(listenSock1, &readFdSet);
144 PR_FD_SET(listenSock2, &readFdSet);
145
146 /* Testing bad fd */
147 if (debug_mode) {
148 printf("PR_Select should detect a bad file descriptor\n");
149 }
150 if ((badFD = PR_NewTCPSocket()) == NULL) {
151 fprintf(stderr, "Can't create a TCP socket\n");
152 failed_already = 1;
153 goto exit_now;
154 }
155
156 PR_FD_SET(badFD, &readFdSet);
157 /*
158 * Make the fd invalid
159 */
160 #if defined(XP_UNIX)
161 close(PR_FileDesc2NativeHandle(badFD));
162 #elif defined(WIN32)
163 closesocket(PR_FileDesc2NativeHandle(badFD));
164 #else
165 # error "Unknown architecture"
166 #endif
167
168 retVal =
169 PR_Select(0 /* unused */, &readFdSet, NULL, NULL, PR_INTERVAL_NO_TIMEOUT);
170 if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) {
171 fprintf(stderr,
172 "Failed to detect the bad fd: "
173 "PR_Select returns %d\n",
174 retVal);
175 if (retVal == -1) {
176 fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(), PR_GetOSError());
177 failed_already = 1;
178 }
179 goto exit_now;
180 }
181 if (debug_mode) {
182 printf("PR_Select detected a bad fd. Test passed.\n\n");
183 }
184 PR_FD_CLR(badFD, &readFdSet);
185
186 PR_Cleanup();
187 goto exit_now;
188 exit_now:
189 if (failed_already) {
190 return 1;
191 } else {
192 return 0;
193 }
194 }