VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/selct_nm.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
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** 1997 - Netscape Communications Corporation
40**
41** Name: prselect_norm.c
42**
43** Description: tests PR_Select with sockets - Normal operations.
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 "prerror.h"
66#include "prnetdb.h"
67
68#ifdef XP_MAC
69#include "probslet.h"
70#else
71#include "obsolete/probslet.h"
72#endif
73
74#include <stdio.h>
75#include <string.h>
76#include <stdlib.h>
77
78PRIntn failed_already=0;
79PRIntn debug_mode;
80
81static void
82clientThreadFunc(void *arg)
83{
84 PRUintn port = (PRUintn) arg;
85 PRFileDesc *sock;
86 PRNetAddr addr;
87 char buf[128];
88 int i;
89
90 addr.inet.family = PR_AF_INET;
91 addr.inet.port = PR_htons((PRUint16)port);
92 addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
93 PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.port);
94
95 for (i = 0; i < 5; i++) {
96 sock = PR_NewTCPSocket();
97 PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
98 PR_Write(sock, buf, sizeof(buf));
99 PR_Close(sock);
100 }
101}
102
103int main(int argc, char **argv)
104{
105 PRFileDesc *listenSock1, *listenSock2;
106 PRFileDesc *fds0[10], *fds1[10], **fds, **other_fds;
107 PRIntn nfds;
108 PRUint16 listenPort1, listenPort2;
109 PRNetAddr addr;
110 PR_fd_set readFdSet;
111 char buf[128];
112 PRThread *clientThread;
113 PRInt32 retVal;
114 PRIntn i, j;
115
116 /* The command line argument: -d is used to determine if the test is being run
117 in debug mode. The regress tool requires only one line output:PASS or FAIL.
118 All of the printfs associated with this test has been handled with a if (debug_mode)
119 test.
120 Usage: test_name -d
121 */
122 PLOptStatus os;
123 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
124 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
125 {
126 if (PL_OPT_BAD == os) continue;
127 switch (opt->option)
128 {
129 case 'd': /* debug mode */
130 debug_mode = 1;
131 break;
132 default:
133 break;
134 }
135 }
136 PL_DestroyOptState(opt);
137
138 /* main test */
139
140 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
141 PR_STDIO_INIT();
142
143 if (debug_mode) {
144 printf("This program tests PR_Select with sockets. \n");
145 printf(" Normal operation are tested.\n\n");
146 }
147
148 /* Create two listening sockets */
149 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
150 fprintf(stderr, "Can't create a new TCP socket\n");
151 failed_already=1;
152 goto exit_now;
153 }
154 addr.inet.family = PR_AF_INET;
155 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
156 addr.inet.port = PR_htons(0);
157 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
158 fprintf(stderr, "Can't bind socket\n");
159 failed_already=1;
160 goto exit_now;
161 }
162 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
163 fprintf(stderr, "PR_GetSockName failed\n");
164 failed_already=1;
165 goto exit_now;
166 }
167 listenPort1 = PR_ntohs(addr.inet.port);
168 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
169 fprintf(stderr, "Can't listen on a socket\n");
170 failed_already=1;
171 goto exit_now;
172 }
173
174 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
175 fprintf(stderr, "Can't create a new TCP socket\n");
176 failed_already=1;
177 goto exit_now;
178 }
179 addr.inet.family = PR_AF_INET;
180 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
181 addr.inet.port = PR_htons(0);
182 if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
183 fprintf(stderr, "Can't bind socket\n");
184 failed_already=1;
185 goto exit_now;
186 }
187 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
188 fprintf(stderr, "PR_GetSockName failed\n");
189 failed_already=1;
190 goto exit_now;
191 }
192 listenPort2 = PR_ntohs(addr.inet.port);
193 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
194 fprintf(stderr, "Can't listen on a socket\n");
195failed_already=1;
196 goto exit_now;
197 }
198 PR_snprintf(buf, sizeof(buf),
199 "The server thread is listening on ports %hu and %hu\n\n",
200 listenPort1, listenPort2);
201 if (debug_mode) printf("%s", buf);
202
203 clientThread = PR_CreateThread(PR_USER_THREAD,
204 clientThreadFunc, (void *) listenPort1,
205 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
206 PR_UNJOINABLE_THREAD, 0);
207 if (clientThread == NULL) {
208 fprintf(stderr, "can't create thread\n");
209 failed_already=1;
210 goto exit_now;
211 }
212
213 clientThread = PR_CreateThread(PR_USER_THREAD,
214 clientThreadFunc, (void *) listenPort2,
215 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
216 PR_UNJOINABLE_THREAD, 0);
217 if (clientThread == NULL) {
218 fprintf(stderr, "can't create thread\n");
219 failed_already=1;
220 goto exit_now;
221 }
222
223 if (debug_mode) {
224 printf("Two client threads are created. Each of them will\n");
225 printf("send data to one of the two ports the server is listening on.\n");
226 printf("The data they send is the port number. Each of them send\n");
227 printf("the data five times, so you should see ten lines below,\n");
228 printf("interleaved in an arbitrary order.\n");
229 }
230 /* set up the fd array */
231 fds = fds0;
232 other_fds = fds1;
233 fds[0] = listenSock1;
234 fds[1] = listenSock2;
235 nfds = 2;
236 /* Set up the fd set */
237 PR_FD_ZERO(&readFdSet);
238 PR_FD_SET(listenSock1, &readFdSet);
239 PR_FD_SET(listenSock2, &readFdSet);
240
241 /* 20 events total */
242 i = 0;
243 while (i < 20) {
244 PRFileDesc **tmp;
245 int nextIndex;
246 int nEvents = 0;
247
248 retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
249 PR_INTERVAL_NO_TIMEOUT);
250 PR_ASSERT(retVal != 0); /* no timeout */
251 if (retVal == -1) {
252 fprintf(stderr, "PR_Select failed (%d, %d)\n", PR_GetError(),
253 PR_GetOSError());
254 failed_already=1;
255 goto exit_now;
256 }
257
258 nextIndex = 2;
259 /* the two listening sockets */
260 for (j = 0; j < 2; j++) {
261 other_fds[j] = fds[j];
262 if (PR_FD_ISSET(fds[j], &readFdSet)) {
263 PRFileDesc *sock;
264
265 nEvents++;
266 sock = PR_Accept(fds[j], NULL, PR_INTERVAL_NO_TIMEOUT);
267 if (sock == NULL) {
268 fprintf(stderr, "PR_Accept() failed\n");
269 failed_already=1;
270 goto exit_now;
271 }
272 other_fds[nextIndex] = sock;
273 PR_FD_SET(sock, &readFdSet);
274 nextIndex++;
275 }
276 PR_FD_SET(fds[j], &readFdSet);
277 }
278
279 for (j = 2; j < nfds; j++) {
280 if (PR_FD_ISSET(fds[j], &readFdSet)) {
281 PRInt32 nBytes;
282
283 PR_FD_CLR(fds[j], &readFdSet);
284 nEvents++;
285 nBytes = PR_Read(fds[j], buf, sizeof(buf));
286 if (nBytes == -1) {
287 fprintf(stderr, "PR_Read() failed\n");
288 failed_already=1;
289 goto exit_now;
290 }
291 /* Just to be safe */
292 buf[127] = '\0';
293 PR_Close(fds[j]);
294 if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
295 } else {
296 PR_FD_SET(fds[j], &readFdSet);
297 other_fds[nextIndex] = fds[j];
298 nextIndex++;
299 }
300 }
301
302 PR_ASSERT(retVal == nEvents);
303 /* swap */
304 tmp = fds;
305 fds = other_fds;
306 other_fds = tmp;
307 nfds = nextIndex;
308 i += nEvents;
309 }
310
311 if (debug_mode) printf("Test passed\n");
312
313 PR_Cleanup();
314 goto exit_now;
315exit_now:
316 if(failed_already)
317 return 1;
318 else
319 return 0;
320}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette