nsprpub/pr/tests/poll_er.c

branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
equal deleted inserted replaced
-1:000000000000 0:36663f7c3139
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 **
8 ** Name: prpoll_err.c
9 **
10 ** Description: This program tests PR_Poll with sockets.
11 ** error reporting operation is tested
12 **
13 ** Modification History:
14 ** 19-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 ***********************************************************************/
22
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
31
32 /***********************************************************************
33 ** Includes
34 ***********************************************************************/
35 /* Used to get the command line option */
36 #include "plgetopt.h"
37
38 #include "primpl.h"
39
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43
44 PRIntn failed_already=0;
45 PRIntn debug_mode;
46
47 static void
48 ClientThreadFunc(void *arg)
49 {
50 PRFileDesc *badFD = (PRFileDesc *) arg;
51 /*
52 * Make the fd invalid
53 */
54 #if defined(XP_UNIX)
55 close(PR_FileDesc2NativeHandle(badFD));
56 #elif defined(XP_OS2)
57 soclose(PR_FileDesc2NativeHandle(badFD));
58 #elif defined(WIN32) || defined(WIN16)
59 closesocket(PR_FileDesc2NativeHandle(badFD));
60 #else
61 #error "Unknown architecture"
62 #endif
63 }
64
65 int main(int argc, char **argv)
66 {
67 PRFileDesc *listenSock1, *listenSock2;
68 PRFileDesc *badFD;
69 PRUint16 listenPort1, listenPort2;
70 PRNetAddr addr;
71 char buf[128];
72 PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
73 PRIntn npds;
74 PRInt32 retVal;
75
76 /* The command line argument: -d is used to determine if the test is being run
77 in debug mode. The regress tool requires only one line output:PASS or FAIL.
78 All of the printfs associated with this test has been handled with a if (debug_mode)
79 test.
80 Usage: test_name -d
81 */
82 PLOptStatus os;
83 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
84 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
85 {
86 if (PL_OPT_BAD == os) continue;
87 switch (opt->option)
88 {
89 case 'd': /* debug mode */
90 debug_mode = 1;
91 break;
92 default:
93 break;
94 }
95 }
96 PL_DestroyOptState(opt);
97
98 /* main test */
99
100 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
101 PR_STDIO_INIT();
102
103 if (debug_mode) {
104 printf("This program tests PR_Poll with sockets.\n");
105 printf("error reporting is tested.\n\n");
106 }
107
108 /* Create two listening sockets */
109 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
110 fprintf(stderr, "Can't create a new TCP socket\n");
111 failed_already=1;
112 goto exit_now;
113 }
114 addr.inet.family = AF_INET;
115 addr.inet.ip = PR_htonl(INADDR_ANY);
116 addr.inet.port = PR_htons(0);
117 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
118 fprintf(stderr, "Can't bind socket\n");
119 failed_already=1;
120 goto exit_now;
121 }
122 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
123 fprintf(stderr, "PR_GetSockName failed\n");
124 failed_already=1;
125 goto exit_now;
126 }
127 listenPort1 = PR_ntohs(addr.inet.port);
128 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
129 fprintf(stderr, "Can't listen on a socket\n");
130 failed_already=1;
131 goto exit_now;
132 }
133
134 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
135 fprintf(stderr, "Can't create a new TCP socket\n");
136 failed_already=1;
137 goto exit_now;
138 }
139 addr.inet.family = AF_INET;
140 addr.inet.ip = PR_htonl(INADDR_ANY);
141 addr.inet.port = PR_htons(0);
142 if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
143 fprintf(stderr, "Can't bind socket\n");
144 failed_already=1;
145 goto exit_now;
146 }
147 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
148 fprintf(stderr, "PR_GetSockName failed\n");
149 failed_already=1;
150 goto exit_now;
151 }
152 listenPort2 = PR_ntohs(addr.inet.port);
153 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
154 fprintf(stderr, "Can't listen on a socket\n");
155 failed_already=1;
156 goto exit_now;
157 }
158 PR_snprintf(buf, sizeof(buf),
159 "The server thread is listening on ports %hu and %hu\n\n",
160 listenPort1, listenPort2);
161 if (debug_mode) printf("%s", buf);
162
163 /* Set up the poll descriptor array */
164 pds = pds0;
165 other_pds = pds1;
166 memset(pds, 0, sizeof(pds));
167 pds[0].fd = listenSock1;
168 pds[0].in_flags = PR_POLL_READ;
169 pds[1].fd = listenSock2;
170 pds[1].in_flags = PR_POLL_READ;
171 npds = 2;
172
173
174 /* Testing bad fd */
175 if (debug_mode) printf("PR_Poll should detect a bad file descriptor\n");
176 if ((badFD = PR_NewTCPSocket()) == NULL) {
177 fprintf(stderr, "Can't create a TCP socket\n");
178 goto exit_now;
179 }
180
181 pds[2].fd = badFD;
182 pds[2].in_flags = PR_POLL_READ;
183 npds = 3;
184
185 if (PR_CreateThread(PR_USER_THREAD, ClientThreadFunc,
186 badFD, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
187 PR_UNJOINABLE_THREAD, 0) == NULL) {
188 fprintf(stderr, "cannot create thread\n");
189 exit(1);
190 }
191
192 retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
193 if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) {
194 fprintf(stderr, "Failed to detect the bad fd: "
195 "PR_Poll returns %d, out_flags is 0x%hx\n",
196 retVal, pds[2].out_flags);
197 failed_already=1;
198 goto exit_now;
199 }
200 if (debug_mode) printf("PR_Poll detected the bad fd. Test passed.\n\n");
201 PR_Cleanup();
202 goto exit_now;
203 exit_now:
204 if(failed_already)
205 return 1;
206 else
207 return 0;
208 }
209
210 #endif /* XP_BEOS */

mercurial