VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/accept.c@ 22683

最後變更 在這個檔案從22683是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 16.3 KB
 
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** 1996 - Netscape Communications Corporation
40**
41** Name: accept.c
42**
43** Description: Run accept() sucessful connection tests.
44**
45** Modification History:
46** 04-Jun-97 AGarcia - Reconvert test file to return a 0 for PASS and a 1 for FAIL
47** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode
48** The debug mode will print all of the printfs associated with this test.
49** The regress mode will be the default mode. Since the regress tool limits
50** the output to a one line status:PASS or FAIL,all of the printf statements
51** have been handled with an if (debug_mode) statement.
52** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
53** recognize the return code from tha main program.
54** 12-June-97 Revert to return code 0 and 1.
55***********************************************************************/
56
57/***********************************************************************
58** Includes
59***********************************************************************/
60
61#include "nspr.h"
62#include "prpriv.h"
63
64#include <stdlib.h>
65#include <string.h>
66
67#include "plgetopt.h"
68#include "plerror.h"
69
70#define BASE_PORT 10000
71
72#define CLIENT_DATA 128
73
74#define ACCEPT_NORMAL 0x1
75#define ACCEPT_FAST 0x2
76#define ACCEPT_READ 0x3
77#define ACCEPT_READ_FAST 0x4
78#define ACCEPT_READ_FAST_CB 0x5
79
80#define CLIENT_NORMAL 0x1
81#define CLIENT_TIMEOUT_ACCEPT 0x2
82#define CLIENT_TIMEOUT_SEND 0x3
83
84#define SERVER_MAX_BIND_COUNT 100
85
86#if defined(XP_MAC) || defined(XP_OS2)
87#define TIMEOUTSECS 10
88#else
89#define TIMEOUTSECS 2
90#endif
91PRIntervalTime timeoutTime;
92
93static PRInt32 count = 1;
94static PRFileDesc *output;
95static PRNetAddr serverAddr;
96static PRThreadScope thread_scope = PR_LOCAL_THREAD;
97static PRInt32 clientCommand;
98static PRInt32 iterations;
99static PRStatus rv;
100static PRFileDesc *listenSock;
101static PRFileDesc *clientSock = NULL;
102static PRNetAddr listenAddr;
103static PRNetAddr clientAddr;
104static PRThread *clientThread;
105static PRNetAddr *raddr;
106static char buf[4096 + 2*sizeof(PRNetAddr) + 32];
107static PRInt32 status;
108static PRInt32 bytesRead;
109
110PRIntn failed_already=0;
111PRIntn debug_mode;
112
113void Test_Assert(const char *msg, const char *file, PRIntn line)
114{
115 failed_already=1;
116 if (debug_mode) {
117 PR_fprintf(output, "@%s:%d ", file, line);
118 PR_fprintf(output, msg);
119 }
120} /* Test_Assert */
121
122#define TEST_ASSERT(expr) \
123 if (!(expr)) Test_Assert(#expr, __FILE__, __LINE__)
124
125#ifdef WINNT
126#define CALLBACK_MAGIC 0x12345678
127
128void timeout_callback(void *magic)
129{
130 TEST_ASSERT(magic == (void *)CALLBACK_MAGIC);
131 if (debug_mode)
132 PR_fprintf(output, "timeout callback called okay\n");
133}
134#endif
135
136
137static void PR_CALLBACK
138ClientThread(void *_action)
139{
140 PRInt32 action = * (PRInt32 *) _action;
141 PRInt32 iterations = count;
142 PRFileDesc *sock = NULL;
143
144 serverAddr.inet.family = PR_AF_INET;
145 serverAddr.inet.port = listenAddr.inet.port;
146 serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
147
148 for (; iterations--;) {
149 PRInt32 rv;
150 char buf[CLIENT_DATA];
151
152 memset(buf, 0xaf, sizeof(buf)); /* initialize with arbitrary data */
153 sock = PR_NewTCPSocket();
154 if (!sock) {
155 if (!debug_mode)
156 failed_already=1;
157 else
158 PR_fprintf(output, "client: unable to create socket\n");
159 return;
160 }
161
162 if (action != CLIENT_TIMEOUT_ACCEPT) {
163
164 if ((rv = PR_Connect(sock, &serverAddr,
165 timeoutTime)) < 0) {
166 if (!debug_mode)
167 failed_already=1;
168 else
169 PR_fprintf(output,
170 "client: unable to connect to server (%ld, %ld, %ld, %ld)\n",
171 iterations, rv, PR_GetError(), PR_GetOSError());
172 goto ErrorExit;
173 }
174
175 if (action != CLIENT_TIMEOUT_SEND) {
176 if ((rv = PR_Send(sock, buf, CLIENT_DATA,
177 0, timeoutTime))< 0) {
178 if (!debug_mode)
179 failed_already=1;
180 else
181 PR_fprintf(output,
182 "client: unable to send to server (%d, %ld, %ld)\n",
183 CLIENT_DATA, rv, PR_GetError());
184 goto ErrorExit;
185 }
186 } else {
187 PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1));
188 }
189 } else {
190 PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1));
191 }
192 if (debug_mode)
193 PR_fprintf(output, ".");
194 PR_Close(sock);
195 sock = NULL;
196 }
197 if (debug_mode)
198 PR_fprintf(output, "\n");
199
200ErrorExit:
201 if (sock != NULL)
202 PR_Close(sock);
203}
204
205
206static void
207RunTest(PRInt32 acceptType, PRInt32 clientAction)
208{
209int i;
210
211 /* First bind to the socket */
212 listenSock = PR_NewTCPSocket();
213 if (!listenSock) {
214 failed_already=1;
215 if (debug_mode)
216 PR_fprintf(output, "unable to create listen socket\n");
217 return;
218 }
219 memset(&listenAddr, 0 , sizeof(listenAddr));
220 listenAddr.inet.family = PR_AF_INET;
221 listenAddr.inet.port = PR_htons(BASE_PORT);
222 listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
223 /*
224 * try a few times to bind server's address, if addresses are in
225 * use
226 */
227 i = 0;
228 while (PR_Bind(listenSock, &listenAddr) == PR_FAILURE) {
229 if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) {
230 listenAddr.inet.port += 2;
231 if (i++ < SERVER_MAX_BIND_COUNT)
232 continue;
233 }
234 failed_already=1;
235 if (debug_mode)
236 PR_fprintf(output,"accept: ERROR - PR_Bind failed\n");
237 return;
238 }
239
240
241 rv = PR_Listen(listenSock, 100);
242 if (rv == PR_FAILURE) {
243 failed_already=1;
244 if (debug_mode)
245 PR_fprintf(output, "unable to listen\n");
246 return;
247 }
248
249 clientCommand = clientAction;
250 clientThread = PR_CreateThread(PR_USER_THREAD, ClientThread,
251 (void *)&clientCommand, PR_PRIORITY_NORMAL, thread_scope,
252 PR_JOINABLE_THREAD, 0);
253 if (!clientThread) {
254 failed_already=1;
255 if (debug_mode)
256 PR_fprintf(output, "error creating client thread\n");
257 return;
258 }
259
260 iterations = count;
261 for (;iterations--;) {
262 switch (acceptType) {
263 case ACCEPT_NORMAL:
264 clientSock = PR_Accept(listenSock, &clientAddr,
265 timeoutTime);
266 switch(clientAction) {
267 case CLIENT_TIMEOUT_ACCEPT:
268 TEST_ASSERT(clientSock == 0);
269 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
270 break;
271 case CLIENT_NORMAL:
272 TEST_ASSERT(clientSock);
273 bytesRead = PR_Recv(clientSock,
274 buf, CLIENT_DATA, 0, timeoutTime);
275 TEST_ASSERT(bytesRead == CLIENT_DATA);
276 break;
277 case CLIENT_TIMEOUT_SEND:
278 TEST_ASSERT(clientSock);
279 bytesRead = PR_Recv(clientSock,
280 buf, CLIENT_DATA, 0, timeoutTime);
281 TEST_ASSERT(bytesRead == -1);
282 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
283 break;
284 }
285 break;
286 case ACCEPT_READ:
287 status = PR_AcceptRead(listenSock, &clientSock,
288 &raddr, buf, CLIENT_DATA, timeoutTime);
289 switch(clientAction) {
290 case CLIENT_TIMEOUT_ACCEPT:
291 /* Invalid test case */
292 TEST_ASSERT(0);
293 break;
294 case CLIENT_NORMAL:
295 TEST_ASSERT(clientSock);
296 TEST_ASSERT(status == CLIENT_DATA);
297 break;
298 case CLIENT_TIMEOUT_SEND:
299 TEST_ASSERT(status == -1);
300 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
301 break;
302 }
303 break;
304#ifdef WINNT
305 case ACCEPT_FAST:
306 clientSock = PR_NTFast_Accept(listenSock,
307 &clientAddr, timeoutTime);
308 switch(clientAction) {
309 case CLIENT_TIMEOUT_ACCEPT:
310 TEST_ASSERT(clientSock == 0);
311 if (debug_mode)
312 PR_fprintf(output, "PR_GetError is %ld\n", PR_GetError());
313 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
314 break;
315 case CLIENT_NORMAL:
316 TEST_ASSERT(clientSock);
317 bytesRead = PR_Recv(clientSock,
318 buf, CLIENT_DATA, 0, timeoutTime);
319 TEST_ASSERT(bytesRead == CLIENT_DATA);
320 break;
321 case CLIENT_TIMEOUT_SEND:
322 TEST_ASSERT(clientSock);
323 bytesRead = PR_Recv(clientSock,
324 buf, CLIENT_DATA, 0, timeoutTime);
325 TEST_ASSERT(bytesRead == -1);
326 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
327 break;
328 }
329 break;
330 break;
331 case ACCEPT_READ_FAST:
332 status = PR_NTFast_AcceptRead(listenSock,
333 &clientSock, &raddr, buf, 4096, timeoutTime);
334 switch(clientAction) {
335 case CLIENT_TIMEOUT_ACCEPT:
336 /* Invalid test case */
337 TEST_ASSERT(0);
338 break;
339 case CLIENT_NORMAL:
340 TEST_ASSERT(clientSock);
341 TEST_ASSERT(status == CLIENT_DATA);
342 break;
343 case CLIENT_TIMEOUT_SEND:
344 TEST_ASSERT(clientSock == NULL);
345 TEST_ASSERT(status == -1);
346 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
347 break;
348 }
349 break;
350 case ACCEPT_READ_FAST_CB:
351 status = PR_NTFast_AcceptRead_WithTimeoutCallback(
352 listenSock, &clientSock, &raddr, buf, 4096,
353 timeoutTime, timeout_callback, (void *)CALLBACK_MAGIC);
354 switch(clientAction) {
355 case CLIENT_TIMEOUT_ACCEPT:
356 /* Invalid test case */
357 TEST_ASSERT(0);
358 break;
359 case CLIENT_NORMAL:
360 TEST_ASSERT(clientSock);
361 TEST_ASSERT(status == CLIENT_DATA);
362 break;
363 case CLIENT_TIMEOUT_SEND:
364 if (debug_mode)
365 PR_fprintf(output, "clientSock = 0x%8.8lx\n", clientSock);
366 TEST_ASSERT(clientSock == NULL);
367 TEST_ASSERT(status == -1);
368 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR);
369 break;
370 }
371 break;
372#endif
373 }
374 if (clientSock != NULL) {
375 PR_Close(clientSock);
376 clientSock = NULL;
377 }
378 }
379 PR_Close(listenSock);
380
381 PR_JoinThread(clientThread);
382}
383
384
385void AcceptUpdatedTest(void)
386{
387 RunTest(ACCEPT_NORMAL, CLIENT_NORMAL);
388}
389void AcceptNotUpdatedTest(void)
390{
391 RunTest(ACCEPT_FAST, CLIENT_NORMAL);
392}
393void AcceptReadTest(void)
394{
395 RunTest(ACCEPT_READ, CLIENT_NORMAL);
396}
397void AcceptReadNotUpdatedTest(void)
398{
399 RunTest(ACCEPT_READ_FAST, CLIENT_NORMAL);
400}
401void AcceptReadCallbackTest(void)
402{
403 RunTest(ACCEPT_READ_FAST_CB, CLIENT_NORMAL);
404}
405
406void TimeoutAcceptUpdatedTest(void)
407{
408 RunTest(ACCEPT_NORMAL, CLIENT_TIMEOUT_ACCEPT);
409}
410void TimeoutAcceptNotUpdatedTest(void)
411{
412 RunTest(ACCEPT_FAST, CLIENT_TIMEOUT_ACCEPT);
413}
414void TimeoutAcceptReadCallbackTest(void)
415{
416 RunTest(ACCEPT_READ_FAST_CB, CLIENT_TIMEOUT_ACCEPT);
417}
418
419void TimeoutReadUpdatedTest(void)
420{
421 RunTest(ACCEPT_NORMAL, CLIENT_TIMEOUT_SEND);
422}
423void TimeoutReadNotUpdatedTest(void)
424{
425 RunTest(ACCEPT_FAST, CLIENT_TIMEOUT_SEND);
426}
427void TimeoutReadReadTest(void)
428{
429 RunTest(ACCEPT_READ, CLIENT_TIMEOUT_SEND);
430}
431void TimeoutReadReadNotUpdatedTest(void)
432{
433 RunTest(ACCEPT_READ_FAST, CLIENT_TIMEOUT_SEND);
434}
435void TimeoutReadReadCallbackTest(void)
436{
437 RunTest(ACCEPT_READ_FAST_CB, CLIENT_TIMEOUT_SEND);
438}
439
440/************************************************************************/
441
442static void Measure(void (*func)(void), const char *msg)
443{
444 PRIntervalTime start, stop;
445 double d;
446
447 start = PR_IntervalNow();
448 (*func)();
449 stop = PR_IntervalNow();
450
451 d = (double)PR_IntervalToMicroseconds(stop - start);
452 if (debug_mode)
453 PR_fprintf(output, "%40s: %6.2f usec\n", msg, d / count);
454
455}
456
457int main(int argc, char **argv)
458{
459
460 /* The command line argument: -d is used to determine if the test is being run
461 in debug mode. The regress tool requires only one line output:PASS or FAIL.
462 All of the printfs associated with this test has been handled with a if (debug_mode)
463 test.
464 Usage: test_name [-d] [-c n]
465 */
466 PLOptStatus os;
467 PLOptState *opt = PL_CreateOptState(argc, argv, "Gdc:");
468 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
469 {
470 if (PL_OPT_BAD == os) continue;
471 switch (opt->option)
472 {
473 case 'G': /* global threads */
474 thread_scope = PR_GLOBAL_THREAD;
475 break;
476 case 'd': /* debug mode */
477 debug_mode = 1;
478 break;
479 case 'c': /* loop counter */
480 count = atoi(opt->value);
481 break;
482 default:
483 break;
484 }
485 }
486 PL_DestroyOptState(opt);
487
488 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
489 output = PR_STDERR;
490 PR_STDIO_INIT();
491
492#ifdef XP_MAC
493 SetupMacPrintfLog("accept.log");
494 debug_mode = 1;
495#endif
496
497 timeoutTime = PR_SecondsToInterval(TIMEOUTSECS);
498 if (debug_mode)
499 PR_fprintf(output, "\nRun accept() sucessful connection tests\n");
500
501 Measure(AcceptUpdatedTest, "PR_Accept()");
502 Measure(AcceptReadTest, "PR_AcceptRead()");
503#ifdef WINNT
504 Measure(AcceptNotUpdatedTest, "PR_NTFast_Accept()");
505 Measure(AcceptReadNotUpdatedTest, "PR_NTFast_AcceptRead()");
506 Measure(AcceptReadCallbackTest, "PR_NTFast_AcceptRead_WithTimeoutCallback()");
507#endif
508 if (debug_mode)
509 PR_fprintf(output, "\nRun accept() timeout in the accept tests\n");
510#ifdef WINNT
511 Measure(TimeoutReadReadCallbackTest, "PR_NTFast_AcceptRead_WithTimeoutCallback()");
512#endif
513 Measure(TimeoutReadUpdatedTest, "PR_Accept()");
514 if (debug_mode)
515 PR_fprintf(output, "\nRun accept() timeout in the read tests\n");
516 Measure(TimeoutReadReadTest, "PR_AcceptRead()");
517#ifdef WINNT
518 Measure(TimeoutReadNotUpdatedTest, "PR_NTFast_Accept()");
519 Measure(TimeoutReadReadNotUpdatedTest, "PR_NTFast_AcceptRead()");
520 Measure(TimeoutReadReadCallbackTest, "PR_NTFast_AcceptRead_WithTimeoutCallback()");
521#endif
522 PR_fprintf(output, "%s\n", (failed_already) ? "FAIL" : "PASS");
523 return failed_already;
524}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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