1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /***********************************************************************
|
---|
39 | **
|
---|
40 | ** Name: prpoll_to.c
|
---|
41 | **
|
---|
42 | ** Description: This program tests PR_Poll with sockets.
|
---|
43 | ** Timeout operation is tested
|
---|
44 | **
|
---|
45 | ** Modification History:
|
---|
46 | ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
|
---|
47 | ** The debug mode will print all of the printfs associated with this test.
|
---|
48 | ** The regress mode will be the default mode. Since the regress tool limits
|
---|
49 | ** the output to a one line status:PASS or FAIL,all of the printf statements
|
---|
50 | ** have been handled with an if (debug_mode) statement.
|
---|
51 | ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
|
---|
52 | ** recognize the return code from tha main program.
|
---|
53 | ***********************************************************************/
|
---|
54 |
|
---|
55 | /***********************************************************************
|
---|
56 | ** Includes
|
---|
57 | ***********************************************************************/
|
---|
58 | /* Used to get the command line option */
|
---|
59 | #include "plgetopt.h"
|
---|
60 |
|
---|
61 | #include "prinit.h"
|
---|
62 | #include "prio.h"
|
---|
63 | #include "prlog.h"
|
---|
64 | #include "prprf.h"
|
---|
65 | #include "prnetdb.h"
|
---|
66 |
|
---|
67 | #ifndef XP_MAC
|
---|
68 | #include "private/pprio.h"
|
---|
69 | #else
|
---|
70 | #include "pprio.h"
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | #include <stdio.h>
|
---|
74 | #include <string.h>
|
---|
75 | #include <stdlib.h>
|
---|
76 | PRIntn failed_already=0;
|
---|
77 | PRIntn debug_mode;
|
---|
78 |
|
---|
79 | int main(int argc, char **argv)
|
---|
80 | {
|
---|
81 | PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
|
---|
82 | PRUint16 listenPort1, listenPort2;
|
---|
83 | PRNetAddr addr;
|
---|
84 | char buf[128];
|
---|
85 | PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
|
---|
86 | PRIntn npds;
|
---|
87 | PRInt32 retVal;
|
---|
88 |
|
---|
89 | /* The command line argument: -d is used to determine if the test is being run
|
---|
90 | in debug mode. The regress tool requires only one line output:PASS or FAIL.
|
---|
91 | All of the printfs associated with this test has been handled with a if (debug_mode)
|
---|
92 | test.
|
---|
93 | Usage: test_name -d
|
---|
94 | */
|
---|
95 | PLOptStatus os;
|
---|
96 | PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
|
---|
97 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
98 | {
|
---|
99 | if (PL_OPT_BAD == os) continue;
|
---|
100 | switch (opt->option)
|
---|
101 | {
|
---|
102 | case 'd': /* debug mode */
|
---|
103 | debug_mode = 1;
|
---|
104 | break;
|
---|
105 | default:
|
---|
106 | break;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | PL_DestroyOptState(opt);
|
---|
110 |
|
---|
111 | /* main test */
|
---|
112 |
|
---|
113 | PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
|
---|
114 | PR_STDIO_INIT();
|
---|
115 |
|
---|
116 | if (debug_mode) {
|
---|
117 | printf("This program tests PR_Poll with sockets.\n");
|
---|
118 | printf("Timeout is tested.\n\n");
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* Create two listening sockets */
|
---|
122 | if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
|
---|
123 | fprintf(stderr, "Can't create a new TCP socket\n");
|
---|
124 | if (!debug_mode) failed_already=1;
|
---|
125 | goto exit_now;
|
---|
126 | }
|
---|
127 | memset(&addr, 0, sizeof(addr));
|
---|
128 | addr.inet.family = PR_AF_INET;
|
---|
129 | addr.inet.ip = PR_htonl(PR_INADDR_ANY);
|
---|
130 | addr.inet.port = PR_htons(0);
|
---|
131 | if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
|
---|
132 | fprintf(stderr, "Can't bind socket\n");
|
---|
133 | if (!debug_mode) failed_already=1;
|
---|
134 | goto exit_now;
|
---|
135 | }
|
---|
136 | if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
|
---|
137 | fprintf(stderr, "PR_GetSockName failed\n");
|
---|
138 | if (!debug_mode) failed_already=1;
|
---|
139 | goto exit_now;
|
---|
140 | }
|
---|
141 | listenPort1 = PR_ntohs(addr.inet.port);
|
---|
142 | if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
|
---|
143 | fprintf(stderr, "Can't listen on a socket\n");
|
---|
144 | if (!debug_mode) failed_already=1;
|
---|
145 | goto exit_now;
|
---|
146 | }
|
---|
147 |
|
---|
148 | if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
|
---|
149 | fprintf(stderr, "Can't create a new TCP socket\n");
|
---|
150 | if (!debug_mode) failed_already=1;
|
---|
151 | goto exit_now;
|
---|
152 | }
|
---|
153 | addr.inet.family = PR_AF_INET;
|
---|
154 | addr.inet.ip = PR_htonl(PR_INADDR_ANY);
|
---|
155 | addr.inet.port = PR_htons(0);
|
---|
156 | if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
|
---|
157 | fprintf(stderr, "Can't bind socket\n");
|
---|
158 | if (!debug_mode) failed_already=1;
|
---|
159 | goto exit_now;
|
---|
160 | }
|
---|
161 | if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
|
---|
162 | fprintf(stderr, "PR_GetSockName failed\n");
|
---|
163 | if (!debug_mode) failed_already=1;
|
---|
164 | goto exit_now;
|
---|
165 | }
|
---|
166 | listenPort2 = PR_ntohs(addr.inet.port);
|
---|
167 | if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
|
---|
168 | fprintf(stderr, "Can't listen on a socket\n");
|
---|
169 | if (!debug_mode) failed_already=1;
|
---|
170 | goto exit_now;
|
---|
171 | }
|
---|
172 | PR_snprintf(buf, sizeof(buf),
|
---|
173 | "The server thread is listening on ports %hu and %hu\n\n",
|
---|
174 | listenPort1, listenPort2);
|
---|
175 | if (debug_mode) printf("%s", buf);
|
---|
176 |
|
---|
177 | /* Set up the poll descriptor array */
|
---|
178 | pds = pds0;
|
---|
179 | other_pds = pds1;
|
---|
180 | memset(pds, 0, sizeof(pds));
|
---|
181 | pds[0].fd = listenSock1;
|
---|
182 | pds[0].in_flags = PR_POLL_READ;
|
---|
183 | pds[1].fd = listenSock2;
|
---|
184 | pds[1].in_flags = PR_POLL_READ;
|
---|
185 | npds = 2;
|
---|
186 |
|
---|
187 | /* Testing timeout */
|
---|
188 | if (debug_mode) printf("PR_Poll should time out in 5 seconds\n");
|
---|
189 | retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
|
---|
190 | if (retVal != 0) {
|
---|
191 | PR_snprintf(buf, sizeof(buf),
|
---|
192 | "PR_Poll should time out and return 0, but it returns %ld\n",
|
---|
193 | retVal);
|
---|
194 | fprintf(stderr, "%s", buf);
|
---|
195 | if (!debug_mode) failed_already=1;
|
---|
196 | goto exit_now;
|
---|
197 | }
|
---|
198 | if (debug_mode) printf("PR_Poll timed out. Test passed.\n\n");
|
---|
199 |
|
---|
200 | exit_now:
|
---|
201 |
|
---|
202 | if (listenSock1) {
|
---|
203 | PR_Close(listenSock1);
|
---|
204 | }
|
---|
205 | if (listenSock2) {
|
---|
206 | PR_Close(listenSock2);
|
---|
207 | }
|
---|
208 |
|
---|
209 | PR_Cleanup();
|
---|
210 |
|
---|
211 | if(failed_already)
|
---|
212 | return 1;
|
---|
213 | else
|
---|
214 | return 0;
|
---|
215 |
|
---|
216 | }
|
---|