Bug 462294 - Add "View Video" to context menu for <video> elements. r=gavin, ui...
[wine-gecko.git] / nsprpub / pr / tests / accept.c
blob4fada1aabe3a90e301074abd9820f80d0d972363
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
13 * License.
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.
22 * Contributor(s):
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
41 ** Name: accept.c
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 /***********************************************************************
58 ** Includes
59 ***********************************************************************/
61 #include "nspr.h"
62 #include "prpriv.h"
64 #include <stdlib.h>
65 #include <string.h>
67 #include "plgetopt.h"
68 #include "plerror.h"
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
88 #else
89 #define TIMEOUTSECS 2
90 #endif
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;
99 static PRStatus rv;
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;
111 PRIntn debug_mode;
113 void Test_Assert(const char *msg, const char *file, PRIntn line)
115 failed_already=1;
116 if (debug_mode) {
117 PR_fprintf(output, "@%s:%d ", file, line);
118 PR_fprintf(output, msg);
120 } /* Test_Assert */
122 #define TEST_ASSERT(expr) \
123 if (!(expr)) Test_Assert(#expr, __FILE__, __LINE__)
125 #ifdef WINNT
126 #define CALLBACK_MAGIC 0x12345678
128 void timeout_callback(void *magic)
130 TEST_ASSERT(magic == (void *)CALLBACK_MAGIC);
131 if (debug_mode)
132 PR_fprintf(output, "timeout callback called okay\n");
134 #endif
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--;) {
149 PRInt32 rv;
150 char buf[CLIENT_DATA];
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;
162 if (action != CLIENT_TIMEOUT_ACCEPT) {
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;
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;
186 } else {
187 PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1));
189 } else {
190 PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1));
192 if (debug_mode)
193 PR_fprintf(output, ".");
194 PR_Close(sock);
195 sock = NULL;
197 if (debug_mode)
198 PR_fprintf(output, "\n");
200 ErrorExit:
201 if (sock != NULL)
202 PR_Close(sock);
206 static void
207 RunTest(PRInt32 acceptType, PRInt32 clientAction)
209 int i;
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;
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
225 * use
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;
234 failed_already=1;
235 if (debug_mode)
236 PR_fprintf(output,"accept: ERROR - PR_Bind failed\n");
237 return;
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;
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;
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;
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;
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;
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;
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;
371 break;
372 #endif
374 if (clientSock != NULL) {
375 PR_Close(clientSock);
376 clientSock = NULL;
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;
445 double d;
447 start = PR_IntervalNow();
448 (*func)();
449 stop = PR_IntervalNow();
451 d = (double)PR_IntervalToMicroseconds(stop - start);
452 if (debug_mode)
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)
463 test.
464 Usage: test_name [-d] [-c n]
466 PLOptStatus os;
467 PLOptState *opt = PL_CreateOptState(argc, argv, "Gdc:");
468 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
470 if (PL_OPT_BAD == os) continue;
471 switch (opt->option)
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;
486 PL_DestroyOptState(opt);
488 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
489 output = PR_STDERR;
490 PR_STDIO_INIT();
492 #ifdef XP_MAC
493 SetupMacPrintfLog("accept.log");
494 debug_mode = 1;
495 #endif
497 timeoutTime = PR_SecondsToInterval(TIMEOUTSECS);
498 if (debug_mode)
499 PR_fprintf(output, "\nRun accept() sucessful connection tests\n");
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;