VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/poll_er.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: 7.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**
40** Name: prpoll_err.c
41**
42** Description: This program tests PR_Poll with sockets.
43** error reporting operation is tested
44**
45** Modification History:
46** 19-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#ifdef XP_BEOS
56#include <stdio.h>
57int main()
58{
59 printf( "This test is not ported to the BeOS\n" );
60 return 0;
61}
62#else
63
64/***********************************************************************
65** Includes
66***********************************************************************/
67/* Used to get the command line option */
68#include "plgetopt.h"
69
70#include "primpl.h"
71
72#include <stdio.h>
73#include <string.h>
74#include <stdlib.h>
75
76PRIntn failed_already=0;
77PRIntn debug_mode;
78
79static void
80ClientThreadFunc(void *arg)
81{
82 PRFileDesc *badFD = (PRFileDesc *) arg;
83 /*
84 * Make the fd invalid
85 */
86#if defined(XP_UNIX)
87 close(PR_FileDesc2NativeHandle(badFD));
88#elif defined(XP_OS2)
89 soclose(PR_FileDesc2NativeHandle(badFD));
90#elif defined(WIN32) || defined(WIN16)
91 closesocket(PR_FileDesc2NativeHandle(badFD));
92#elif defined(XP_MAC)
93 _PR_MD_CLOSE_SOCKET(PR_FileDesc2NativeHandle(badFD));
94#else
95#error "Unknown architecture"
96#endif
97}
98
99int main(int argc, char **argv)
100{
101 PRFileDesc *listenSock1, *listenSock2;
102 PRFileDesc *badFD;
103 PRUint16 listenPort1, listenPort2;
104 PRNetAddr addr;
105 char buf[128];
106 PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
107 PRIntn npds;
108 PRInt32 retVal;
109
110 /* The command line argument: -d is used to determine if the test is being run
111 in debug mode. The regress tool requires only one line output:PASS or FAIL.
112 All of the printfs associated with this test has been handled with a if (debug_mode)
113 test.
114 Usage: test_name -d
115 */
116 PLOptStatus os;
117 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
118 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
119 {
120 if (PL_OPT_BAD == os) continue;
121 switch (opt->option)
122 {
123 case 'd': /* debug mode */
124 debug_mode = 1;
125 break;
126 default:
127 break;
128 }
129 }
130 PL_DestroyOptState(opt);
131
132 /* main test */
133
134 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
135 PR_STDIO_INIT();
136
137 if (debug_mode) {
138 printf("This program tests PR_Poll with sockets.\n");
139 printf("error reporting is tested.\n\n");
140 }
141
142 /* Create two listening sockets */
143 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
144 fprintf(stderr, "Can't create a new TCP socket\n");
145 failed_already=1;
146 goto exit_now;
147 }
148 addr.inet.family = AF_INET;
149 addr.inet.ip = PR_htonl(INADDR_ANY);
150 addr.inet.port = PR_htons(0);
151 if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
152 fprintf(stderr, "Can't bind socket\n");
153 failed_already=1;
154 goto exit_now;
155 }
156 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
157 fprintf(stderr, "PR_GetSockName failed\n");
158 failed_already=1;
159 goto exit_now;
160 }
161 listenPort1 = PR_ntohs(addr.inet.port);
162 if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
163 fprintf(stderr, "Can't listen on a socket\n");
164 failed_already=1;
165 goto exit_now;
166 }
167
168 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
169 fprintf(stderr, "Can't create a new TCP socket\n");
170 failed_already=1;
171 goto exit_now;
172 }
173 addr.inet.family = AF_INET;
174 addr.inet.ip = PR_htonl(INADDR_ANY);
175 addr.inet.port = PR_htons(0);
176 if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
177 fprintf(stderr, "Can't bind socket\n");
178 failed_already=1;
179 goto exit_now;
180 }
181 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
182 fprintf(stderr, "PR_GetSockName failed\n");
183 failed_already=1;
184 goto exit_now;
185 }
186 listenPort2 = PR_ntohs(addr.inet.port);
187 if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
188 fprintf(stderr, "Can't listen on a socket\n");
189 failed_already=1;
190 goto exit_now;
191 }
192 PR_snprintf(buf, sizeof(buf),
193 "The server thread is listening on ports %hu and %hu\n\n",
194 listenPort1, listenPort2);
195 if (debug_mode) printf("%s", buf);
196
197 /* Set up the poll descriptor array */
198 pds = pds0;
199 other_pds = pds1;
200 memset(pds, 0, sizeof(pds));
201 pds[0].fd = listenSock1;
202 pds[0].in_flags = PR_POLL_READ;
203 pds[1].fd = listenSock2;
204 pds[1].in_flags = PR_POLL_READ;
205 npds = 2;
206
207
208 /* Testing bad fd */
209 if (debug_mode) printf("PR_Poll should detect a bad file descriptor\n");
210 if ((badFD = PR_NewTCPSocket()) == NULL) {
211 fprintf(stderr, "Can't create a TCP socket\n");
212 goto exit_now;
213 }
214
215 pds[2].fd = badFD;
216 pds[2].in_flags = PR_POLL_READ;
217 npds = 3;
218
219 if (PR_CreateThread(PR_USER_THREAD, ClientThreadFunc,
220 badFD, PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
221 PR_UNJOINABLE_THREAD, 0) == NULL) {
222 fprintf(stderr, "cannot create thread\n");
223 exit(1);
224 }
225
226 retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
227 if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) {
228 fprintf(stderr, "Failed to detect the bad fd: "
229 "PR_Poll returns %d, out_flags is 0x%hx\n",
230 retVal, pds[2].out_flags);
231 failed_already=1;
232 goto exit_now;
233 }
234 if (debug_mode) printf("PR_Poll detected the bad fd. Test passed.\n\n");
235 PR_Cleanup();
236 goto exit_now;
237exit_now:
238 if(failed_already)
239 return 1;
240 else
241 return 0;
242}
243
244#endif /* XP_BEOS */
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