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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
38 /***********************************************************************
39 ** 1996 - Netscape Communications Corporation
43 ** Description: Run accept() sucessful connection tests.
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 ***********************************************************************/
57 /***********************************************************************
59 ***********************************************************************/
70 #define BASE_PORT 10000
72 #define CLIENT_DATA 128
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
80 #define CLIENT_NORMAL 0x1
81 #define CLIENT_TIMEOUT_ACCEPT 0x2
82 #define CLIENT_TIMEOUT_SEND 0x3
84 #define SERVER_MAX_BIND_COUNT 100
86 #if defined(XP_MAC) || defined(XP_OS2)
87 #define TIMEOUTSECS 10
91 PRIntervalTime timeoutTime
;
93 static PRInt32 count
= 1;
94 static PRFileDesc
*output
;
95 static PRNetAddr serverAddr
;
96 static PRThreadScope thread_scope
= PR_LOCAL_THREAD
;
97 static PRInt32 clientCommand
;
98 static PRInt32 iterations
;
100 static PRFileDesc
*listenSock
;
101 static PRFileDesc
*clientSock
= NULL
;
102 static PRNetAddr listenAddr
;
103 static PRNetAddr clientAddr
;
104 static PRThread
*clientThread
;
105 static PRNetAddr
*raddr
;
106 static char buf
[4096 + 2*sizeof(PRNetAddr
) + 32];
107 static PRInt32 status
;
108 static PRInt32 bytesRead
;
110 PRIntn failed_already
=0;
113 void Test_Assert(const char *msg
, const char *file
, PRIntn line
)
117 PR_fprintf(output
, "@%s:%d ", file
, line
);
118 PR_fprintf(output
, msg
);
122 #define TEST_ASSERT(expr) \
123 if (!(expr)) Test_Assert(#expr, __FILE__, __LINE__)
126 #define CALLBACK_MAGIC 0x12345678
128 void timeout_callback(void *magic
)
130 TEST_ASSERT(magic
== (void *)CALLBACK_MAGIC
);
132 PR_fprintf(output
, "timeout callback called okay\n");
137 static void PR_CALLBACK
138 ClientThread(void *_action
)
140 PRInt32 action
= * (PRInt32
*) _action
;
141 PRInt32 iterations
= count
;
142 PRFileDesc
*sock
= NULL
;
144 serverAddr
.inet
.family
= PR_AF_INET
;
145 serverAddr
.inet
.port
= listenAddr
.inet
.port
;
146 serverAddr
.inet
.ip
= PR_htonl(PR_INADDR_LOOPBACK
);
148 for (; iterations
--;) {
150 char buf
[CLIENT_DATA
];
152 memset(buf
, 0xaf, sizeof(buf
)); /* initialize with arbitrary data */
153 sock
= PR_NewTCPSocket();
158 PR_fprintf(output
, "client: unable to create socket\n");
162 if (action
!= CLIENT_TIMEOUT_ACCEPT
) {
164 if ((rv
= PR_Connect(sock
, &serverAddr
,
170 "client: unable to connect to server (%ld, %ld, %ld, %ld)\n",
171 iterations
, rv
, PR_GetError(), PR_GetOSError());
175 if (action
!= CLIENT_TIMEOUT_SEND
) {
176 if ((rv
= PR_Send(sock
, buf
, CLIENT_DATA
,
177 0, timeoutTime
))< 0) {
182 "client: unable to send to server (%d, %ld, %ld)\n",
183 CLIENT_DATA
, rv
, PR_GetError());
187 PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS
+ 1));
190 PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS
+ 1));
193 PR_fprintf(output
, ".");
198 PR_fprintf(output
, "\n");
207 RunTest(PRInt32 acceptType
, PRInt32 clientAction
)
211 /* First bind to the socket */
212 listenSock
= PR_NewTCPSocket();
216 PR_fprintf(output
, "unable to create listen socket\n");
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
);
224 * try a few times to bind server's address, if addresses are in
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
)
236 PR_fprintf(output
,"accept: ERROR - PR_Bind failed\n");
241 rv
= PR_Listen(listenSock
, 100);
242 if (rv
== PR_FAILURE
) {
245 PR_fprintf(output
, "unable to listen\n");
249 clientCommand
= clientAction
;
250 clientThread
= PR_CreateThread(PR_USER_THREAD
, ClientThread
,
251 (void *)&clientCommand
, PR_PRIORITY_NORMAL
, thread_scope
,
252 PR_JOINABLE_THREAD
, 0);
256 PR_fprintf(output
, "error creating client thread\n");
261 for (;iterations
--;) {
262 switch (acceptType
) {
264 clientSock
= PR_Accept(listenSock
, &clientAddr
,
266 switch(clientAction
) {
267 case CLIENT_TIMEOUT_ACCEPT
:
268 TEST_ASSERT(clientSock
== 0);
269 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR
);
272 TEST_ASSERT(clientSock
);
273 bytesRead
= PR_Recv(clientSock
,
274 buf
, CLIENT_DATA
, 0, timeoutTime
);
275 TEST_ASSERT(bytesRead
== CLIENT_DATA
);
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
);
287 status
= PR_AcceptRead(listenSock
, &clientSock
,
288 &raddr
, buf
, CLIENT_DATA
, timeoutTime
);
289 switch(clientAction
) {
290 case CLIENT_TIMEOUT_ACCEPT
:
291 /* Invalid test case */
295 TEST_ASSERT(clientSock
);
296 TEST_ASSERT(status
== CLIENT_DATA
);
298 case CLIENT_TIMEOUT_SEND
:
299 TEST_ASSERT(status
== -1);
300 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR
);
306 clientSock
= PR_NTFast_Accept(listenSock
,
307 &clientAddr
, timeoutTime
);
308 switch(clientAction
) {
309 case CLIENT_TIMEOUT_ACCEPT
:
310 TEST_ASSERT(clientSock
== 0);
312 PR_fprintf(output
, "PR_GetError is %ld\n", PR_GetError());
313 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR
);
316 TEST_ASSERT(clientSock
);
317 bytesRead
= PR_Recv(clientSock
,
318 buf
, CLIENT_DATA
, 0, timeoutTime
);
319 TEST_ASSERT(bytesRead
== CLIENT_DATA
);
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
);
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 */
340 TEST_ASSERT(clientSock
);
341 TEST_ASSERT(status
== CLIENT_DATA
);
343 case CLIENT_TIMEOUT_SEND
:
344 TEST_ASSERT(clientSock
== NULL
);
345 TEST_ASSERT(status
== -1);
346 TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR
);
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 */
360 TEST_ASSERT(clientSock
);
361 TEST_ASSERT(status
== CLIENT_DATA
);
363 case CLIENT_TIMEOUT_SEND
:
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
);
374 if (clientSock
!= NULL
) {
375 PR_Close(clientSock
);
379 PR_Close(listenSock
);
381 PR_JoinThread(clientThread
);
385 void AcceptUpdatedTest(void)
387 RunTest(ACCEPT_NORMAL
, CLIENT_NORMAL
);
389 void AcceptNotUpdatedTest(void)
391 RunTest(ACCEPT_FAST
, CLIENT_NORMAL
);
393 void AcceptReadTest(void)
395 RunTest(ACCEPT_READ
, CLIENT_NORMAL
);
397 void AcceptReadNotUpdatedTest(void)
399 RunTest(ACCEPT_READ_FAST
, CLIENT_NORMAL
);
401 void AcceptReadCallbackTest(void)
403 RunTest(ACCEPT_READ_FAST_CB
, CLIENT_NORMAL
);
406 void TimeoutAcceptUpdatedTest(void)
408 RunTest(ACCEPT_NORMAL
, CLIENT_TIMEOUT_ACCEPT
);
410 void TimeoutAcceptNotUpdatedTest(void)
412 RunTest(ACCEPT_FAST
, CLIENT_TIMEOUT_ACCEPT
);
414 void TimeoutAcceptReadCallbackTest(void)
416 RunTest(ACCEPT_READ_FAST_CB
, CLIENT_TIMEOUT_ACCEPT
);
419 void TimeoutReadUpdatedTest(void)
421 RunTest(ACCEPT_NORMAL
, CLIENT_TIMEOUT_SEND
);
423 void TimeoutReadNotUpdatedTest(void)
425 RunTest(ACCEPT_FAST
, CLIENT_TIMEOUT_SEND
);
427 void TimeoutReadReadTest(void)
429 RunTest(ACCEPT_READ
, CLIENT_TIMEOUT_SEND
);
431 void TimeoutReadReadNotUpdatedTest(void)
433 RunTest(ACCEPT_READ_FAST
, CLIENT_TIMEOUT_SEND
);
435 void TimeoutReadReadCallbackTest(void)
437 RunTest(ACCEPT_READ_FAST_CB
, CLIENT_TIMEOUT_SEND
);
440 /************************************************************************/
442 static void Measure(void (*func
)(void), const char *msg
)
444 PRIntervalTime start
, stop
;
447 start
= PR_IntervalNow();
449 stop
= PR_IntervalNow();
451 d
= (double)PR_IntervalToMicroseconds(stop
- start
);
453 PR_fprintf(output
, "%40s: %6.2f usec\n", msg
, d
/ count
);
457 int main(int argc
, char **argv
)
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)
464 Usage: test_name [-d] [-c n]
467 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "Gdc:");
468 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
470 if (PL_OPT_BAD
== os
) continue;
473 case 'G': /* global threads */
474 thread_scope
= PR_GLOBAL_THREAD
;
476 case 'd': /* debug mode */
479 case 'c': /* loop counter */
480 count
= atoi(opt
->value
);
486 PL_DestroyOptState(opt
);
488 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
493 SetupMacPrintfLog("accept.log");
497 timeoutTime
= PR_SecondsToInterval(TIMEOUTSECS
);
499 PR_fprintf(output
, "\nRun accept() sucessful connection tests\n");
501 Measure(AcceptUpdatedTest
, "PR_Accept()");
502 Measure(AcceptReadTest
, "PR_AcceptRead()");
504 Measure(AcceptNotUpdatedTest
, "PR_NTFast_Accept()");
505 Measure(AcceptReadNotUpdatedTest
, "PR_NTFast_AcceptRead()");
506 Measure(AcceptReadCallbackTest
, "PR_NTFast_AcceptRead_WithTimeoutCallback()");
509 PR_fprintf(output
, "\nRun accept() timeout in the accept tests\n");
511 Measure(TimeoutReadReadCallbackTest
, "PR_NTFast_AcceptRead_WithTimeoutCallback()");
513 Measure(TimeoutReadUpdatedTest
, "PR_Accept()");
515 PR_fprintf(output
, "\nRun accept() timeout in the read tests\n");
516 Measure(TimeoutReadReadTest
, "PR_AcceptRead()");
518 Measure(TimeoutReadNotUpdatedTest
, "PR_NTFast_Accept()");
519 Measure(TimeoutReadReadNotUpdatedTest
, "PR_NTFast_AcceptRead()");
520 Measure(TimeoutReadReadCallbackTest
, "PR_NTFast_AcceptRead_WithTimeoutCallback()");
522 PR_fprintf(output
, "%s\n", (failed_already
) ? "FAIL" : "PASS");
523 return failed_already
;