Debian Codegraph @

nsprpub/pr/tests/poll_to.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 **
8 ** Name: prpoll_to.c
9 **
10 ** Description: This program tests PR_Poll with sockets.
11 ** Timeout operation is tested
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 "prinit.h"
34 #include "prio.h"
35 #include "prlog.h"
36 #include "prprf.h"
37 #include "prnetdb.h"
38
39 #include "private/pprio.h"
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44
45 PRIntn failed_already = 0;
46 PRIntn debug_mode;
47
48 int main(int argc, char** argv) {
49 PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
50 PRUint16 listenPort1, listenPort2;
51 PRNetAddr addr;
52 char buf[128];
53 PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
54 PRIntn npds;
55 PRInt32 retVal;
56
57 /* The command line argument: -d is used to determine if the test is being run
58 in debug mode. The regress tool requires only one line output:PASS or FAIL.
59 All of the printfs associated with this test has been handled with a if
60 (debug_mode) test. Usage: test_name -d
61 */
62 PLOptStatus os;
63 PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
64 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
65 if (PL_OPT_BAD == os) {
66 continue;
67 }
68 switch (opt->option) {
69 case 'd': /* debug mode */
70 debug_mode = 1;
71 break;
72 default:
73 break;
74 }
75 }
76 PL_DestroyOptState(opt);
77
78 /* main test */
79
80 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
81 PR_STDIO_INIT();
82
83 if (debug_mode) {
84 printf("This program tests PR_Poll with sockets.\n");
85 printf("Timeout is tested.\n\n");
86 }
87
88 /* Create two listening sockets */
89 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
90 fprintf(stderr, "Can't create a new TCP socket\n");
91 if (!debug_mode) {
92 failed_already = 1;
93 }
94 goto exit_now;
95 }
96 memset(&addr, 0, sizeof(addr));
97 addr.inet.family = PR_AF_INET;
98 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
99 addr.inet.port = PR_htons(0);
100 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
101 fprintf(stderr, "Can't bind socket\n");
102 if (!debug_mode) {
103 failed_already = 1;
104 }
105 goto exit_now;
106 }
107 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
108 fprintf(stderr, "PR_GetSockName failed\n");
109 if (!debug_mode) {
110 failed_already = 1;
111 }
112 goto exit_now;
113 }
114 listenPort1 = PR_ntohs(addr.inet.port);
115 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
116 fprintf(stderr, "Can't listen on a socket\n");
117 if (!debug_mode) {
118 failed_already = 1;
119 }
120 goto exit_now;
121 }
122
123 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
124 fprintf(stderr, "Can't create a new TCP socket\n");
125 if (!debug_mode) {
126 failed_already = 1;
127 }
128 goto exit_now;
129 }
130 addr.inet.family = PR_AF_INET;
131 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
132 addr.inet.port = PR_htons(0);
133 if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
134 fprintf(stderr, "Can't bind socket\n");
135 if (!debug_mode) {
136 failed_already = 1;
137 }
138 goto exit_now;
139 }
140 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
141 fprintf(stderr, "PR_GetSockName failed\n");
142 if (!debug_mode) {
143 failed_already = 1;
144 }
145 goto exit_now;
146 }
147 listenPort2 = PR_ntohs(addr.inet.port);
148 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
149 fprintf(stderr, "Can't listen on a socket\n");
150 if (!debug_mode) {
151 failed_already = 1;
152 }
153 goto exit_now;
154 }
155 PR_snprintf(buf, sizeof(buf),
156 "The server thread is listening on ports %hu and %hu\n\n",
157 listenPort1, listenPort2);
158 if (debug_mode) {
159 printf("%s", buf);
160 }
161
162 /* Set up the poll descriptor array */
163 pds = pds0;
164 other_pds = pds1;
165 memset(pds, 0, sizeof(pds));
166 pds[0].fd = listenSock1;
167 pds[0].in_flags = PR_POLL_READ;
168 pds[1].fd = listenSock2;
169 pds[1].in_flags = PR_POLL_READ;
170 npds = 2;
171
172 /* Testing timeout */
173 if (debug_mode) {
174 printf("PR_Poll should time out in 5 seconds\n");
175 }
176 retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
177 if (retVal != 0) {
178 PR_snprintf(buf, sizeof(buf),
179 "PR_Poll should time out and return 0, but it returns %ld\n",
180 retVal);
181 fprintf(stderr, "%s", buf);
182 if (!debug_mode) {
183 failed_already = 1;
184 }
185 goto exit_now;
186 }
187 if (debug_mode) {
188 printf("PR_Poll timed out. Test passed.\n\n");
189 }
190
191 exit_now:
192
193 if (listenSock1) {
194 PR_Close(listenSock1);
195 }
196 if (listenSock2) {
197 PR_Close(listenSock2);
198 }
199
200 PR_Cleanup();
201
202 if (failed_already) {
203 return 1;
204 } else {
205 return 0;
206 }
207 }