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 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 /***********************************************************************
24 ** Includes
25 ***********************************************************************/
26 /* Used to get the command line option */
27 #include "plgetopt.h"
28
29 #include "prinit.h"
30 #include "prio.h"
31 #include "prlog.h"
32 #include "prprf.h"
33 #include "prnetdb.h"
34
35 #include "private/pprio.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
main(int argc,char ** argv)44 int main(int argc, char **argv)
45 {
46 PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
47 PRUint16 listenPort1, listenPort2;
48 PRNetAddr addr;
49 char buf[128];
50 PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
51 PRIntn npds;
52 PRInt32 retVal;
53
54 /* The command line argument: -d is used to determine if the test is being run
55 in debug mode. The regress tool requires only one line output:PASS or FAIL.
56 All of the printfs associated with this test has been handled with a if (debug_mode)
57 test.
58 Usage: test_name -d
59 */
60 PLOptStatus os;
61 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
62 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
63 {
64 if (PL_OPT_BAD == os) {
65 continue;
66 }
67 switch (opt->option)
68 {
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 }
205 else {
206 return 0;
207 }
208
209 }
210