VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/selct_to.c@ 65823

Last change on this file since 65823 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: 6.5 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_to.c
42**
43** Description: tests PR_Select with sockets. Time out functions
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#ifdef XP_MAC
68#include "probslet.h"
69#else
70#include "obsolete/probslet.h"
71#endif
72
73#include "prerror.h"
74
75#include <stdio.h>
76#include <string.h>
77#include <stdlib.h>
78
79PRIntn failed_already=0;
80PRIntn debug_mode;
81
82int main(int argc, char **argv)
83{
84 PRFileDesc *listenSock1, *listenSock2;
85 PRUint16 listenPort1, listenPort2;
86 PRNetAddr addr;
87 PR_fd_set readFdSet;
88 char buf[128];
89 PRInt32 retVal;
90
91 /* The command line argument: -d is used to determine if the test is being run
92 in debug mode. The regress tool requires only one line output:PASS or FAIL.
93 All of the printfs associated with this test has been handled with a if (debug_mode)
94 test.
95 Usage: test_name -d
96 */
97 PLOptStatus os;
98 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
99 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
100 {
101 if (PL_OPT_BAD == os) continue;
102 switch (opt->option)
103 {
104 case 'd': /* debug mode */
105 debug_mode = 1;
106 break;
107 default:
108 break;
109 }
110 }
111 PL_DestroyOptState(opt);
112
113 /* main test */
114
115 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
116 PR_STDIO_INIT();
117
118 if (debug_mode) {
119 printf("This program tests PR_Select with sockets. Timeout \n");
120 printf("operations are tested.\n\n");
121 }
122
123 /* Create two listening sockets */
124 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
125 fprintf(stderr, "Can't create a new TCP socket\n");
126 failed_already=1;
127 goto exit_now;
128 }
129 addr.inet.family = PR_AF_INET;
130 addr.inet.ip = PR_htonl(PR_INADDR_ANY);
131 addr.inet.port = PR_htons(0);
132 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
133 fprintf(stderr, "Can't bind socket\n");
134 failed_already=1;
135 goto exit_now;
136 }
137 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
138 fprintf(stderr, "PR_GetSockName failed\n");
139 failed_already=1;
140 goto exit_now;
141 }
142 listenPort1 = PR_ntohs(addr.inet.port);
143 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
144 fprintf(stderr, "Can't listen on a socket\n");
145 failed_already=1;
146 goto exit_now;
147 }
148
149 if ((listenSock2 = 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(listenSock2, &addr) == PR_FAILURE) {
158 fprintf(stderr, "Can't bind socket\n");
159 failed_already=1;
160 goto exit_now;
161 }
162 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
163 fprintf(stderr, "PR_GetSockName failed\n");
164 failed_already=1;
165 goto exit_now;
166 }
167 listenPort2 = PR_ntohs(addr.inet.port);
168 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
169 fprintf(stderr, "Can't listen on a socket\n");
170 failed_already=1;
171 goto exit_now;
172 }
173 PR_snprintf(buf, sizeof(buf),
174 "The server thread is listening on ports %hu and %hu\n\n",
175 listenPort1, listenPort2);
176 if (debug_mode) printf("%s", buf);
177
178 /* Set up the fd set */
179 PR_FD_ZERO(&readFdSet);
180 PR_FD_SET(listenSock1, &readFdSet);
181 PR_FD_SET(listenSock2, &readFdSet);
182
183 /* Testing timeout */
184 if (debug_mode) printf("PR_Select should time out in 5 seconds\n");
185 retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
186 PR_SecondsToInterval(5));
187 if (retVal != 0) {
188 PR_snprintf(buf, sizeof(buf),
189 "PR_Select should time out and return 0, but it returns %ld\n",
190 retVal);
191 fprintf(stderr, "%s", buf);
192 if (retVal == -1) {
193 fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(),
194 PR_GetOSError());
195 failed_already=1;
196 }
197 goto exit_now;
198 }
199 if (debug_mode) printf("PR_Select timed out. Test passed.\n\n");
200
201 PR_Cleanup();
202
203exit_now:
204 if(failed_already)
205 return 1;
206 else
207 return 0;
208}
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