michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /*********************************************************************** michael@0: ** 1996 - Netscape Communications Corporation michael@0: ** michael@0: ** Name: accept.c michael@0: ** michael@0: ** Description: Run accept() sucessful connection tests. michael@0: ** michael@0: ** Modification History: michael@0: ** 04-Jun-97 AGarcia - Reconvert test file to return a 0 for PASS and a 1 for FAIL michael@0: ** 13-May-97 AGarcia- Converted the test to accomodate the debug_mode michael@0: ** The debug mode will print all of the printfs associated with this test. michael@0: ** The regress mode will be the default mode. Since the regress tool limits michael@0: ** the output to a one line status:PASS or FAIL,all of the printf statements michael@0: ** have been handled with an if (debug_mode) statement. michael@0: ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to michael@0: ** recognize the return code from tha main program. michael@0: ** 12-June-97 Revert to return code 0 and 1. michael@0: ***********************************************************************/ michael@0: michael@0: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: michael@0: #include "nspr.h" michael@0: #include "prpriv.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "plgetopt.h" michael@0: #include "plerror.h" michael@0: michael@0: #define BASE_PORT 10000 michael@0: michael@0: #define CLIENT_DATA 128 michael@0: michael@0: #define ACCEPT_NORMAL 0x1 michael@0: #define ACCEPT_FAST 0x2 michael@0: #define ACCEPT_READ 0x3 michael@0: #define ACCEPT_READ_FAST 0x4 michael@0: #define ACCEPT_READ_FAST_CB 0x5 michael@0: michael@0: #define CLIENT_NORMAL 0x1 michael@0: #define CLIENT_TIMEOUT_ACCEPT 0x2 michael@0: #define CLIENT_TIMEOUT_SEND 0x3 michael@0: michael@0: #define SERVER_MAX_BIND_COUNT 100 michael@0: michael@0: #if defined(XP_OS2) || defined(SYMBIAN) michael@0: #define TIMEOUTSECS 10 michael@0: #else michael@0: #define TIMEOUTSECS 2 michael@0: #endif michael@0: PRIntervalTime timeoutTime; michael@0: michael@0: static PRInt32 count = 1; michael@0: static PRFileDesc *output; michael@0: static PRNetAddr serverAddr; michael@0: static PRThreadScope thread_scope = PR_LOCAL_THREAD; michael@0: static PRInt32 clientCommand; michael@0: static PRInt32 iterations; michael@0: static PRStatus rv; michael@0: static PRFileDesc *listenSock; michael@0: static PRFileDesc *clientSock = NULL; michael@0: static PRNetAddr listenAddr; michael@0: static PRNetAddr clientAddr; michael@0: static PRThread *clientThread; michael@0: static PRNetAddr *raddr; michael@0: static char buf[4096 + 2*sizeof(PRNetAddr) + 32]; michael@0: static PRInt32 status; michael@0: static PRInt32 bytesRead; michael@0: michael@0: PRIntn failed_already=0; michael@0: PRIntn debug_mode; michael@0: michael@0: void Test_Assert(const char *msg, const char *file, PRIntn line) michael@0: { michael@0: failed_already=1; michael@0: if (debug_mode) { michael@0: PR_fprintf(output, "@%s:%d ", file, line); michael@0: PR_fprintf(output, msg); michael@0: } michael@0: } /* Test_Assert */ michael@0: michael@0: #define TEST_ASSERT(expr) \ michael@0: if (!(expr)) Test_Assert(#expr, __FILE__, __LINE__) michael@0: michael@0: #ifdef WINNT michael@0: #define CALLBACK_MAGIC 0x12345678 michael@0: michael@0: void timeout_callback(void *magic) michael@0: { michael@0: TEST_ASSERT(magic == (void *)CALLBACK_MAGIC); michael@0: if (debug_mode) michael@0: PR_fprintf(output, "timeout callback called okay\n"); michael@0: } michael@0: #endif michael@0: michael@0: michael@0: static void PR_CALLBACK michael@0: ClientThread(void *_action) michael@0: { michael@0: PRInt32 action = * (PRInt32 *) _action; michael@0: PRInt32 iterations = count; michael@0: PRFileDesc *sock = NULL; michael@0: michael@0: serverAddr.inet.family = PR_AF_INET; michael@0: serverAddr.inet.port = listenAddr.inet.port; michael@0: serverAddr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); michael@0: michael@0: for (; iterations--;) { michael@0: PRInt32 rv; michael@0: char buf[CLIENT_DATA]; michael@0: michael@0: memset(buf, 0xaf, sizeof(buf)); /* initialize with arbitrary data */ michael@0: sock = PR_NewTCPSocket(); michael@0: if (!sock) { michael@0: if (!debug_mode) michael@0: failed_already=1; michael@0: else michael@0: PR_fprintf(output, "client: unable to create socket\n"); michael@0: return; michael@0: } michael@0: michael@0: if (action != CLIENT_TIMEOUT_ACCEPT) { michael@0: michael@0: if ((rv = PR_Connect(sock, &serverAddr, michael@0: timeoutTime)) < 0) { michael@0: if (!debug_mode) michael@0: failed_already=1; michael@0: else michael@0: PR_fprintf(output, michael@0: "client: unable to connect to server (%ld, %ld, %ld, %ld)\n", michael@0: iterations, rv, PR_GetError(), PR_GetOSError()); michael@0: goto ErrorExit; michael@0: } michael@0: michael@0: if (action != CLIENT_TIMEOUT_SEND) { michael@0: if ((rv = PR_Send(sock, buf, CLIENT_DATA, michael@0: 0, timeoutTime))< 0) { michael@0: if (!debug_mode) michael@0: failed_already=1; michael@0: else michael@0: PR_fprintf(output, michael@0: "client: unable to send to server (%d, %ld, %ld)\n", michael@0: CLIENT_DATA, rv, PR_GetError()); michael@0: goto ErrorExit; michael@0: } michael@0: } else { michael@0: PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1)); michael@0: } michael@0: } else { michael@0: PR_Sleep(PR_SecondsToInterval(TIMEOUTSECS + 1)); michael@0: } michael@0: if (debug_mode) michael@0: PR_fprintf(output, "."); michael@0: PR_Close(sock); michael@0: sock = NULL; michael@0: } michael@0: if (debug_mode) michael@0: PR_fprintf(output, "\n"); michael@0: michael@0: ErrorExit: michael@0: if (sock != NULL) michael@0: PR_Close(sock); michael@0: } michael@0: michael@0: michael@0: static void michael@0: RunTest(PRInt32 acceptType, PRInt32 clientAction) michael@0: { michael@0: int i; michael@0: michael@0: /* First bind to the socket */ michael@0: listenSock = PR_NewTCPSocket(); michael@0: if (!listenSock) { michael@0: failed_already=1; michael@0: if (debug_mode) michael@0: PR_fprintf(output, "unable to create listen socket\n"); michael@0: return; michael@0: } michael@0: memset(&listenAddr, 0 , sizeof(listenAddr)); michael@0: listenAddr.inet.family = PR_AF_INET; michael@0: listenAddr.inet.port = PR_htons(BASE_PORT); michael@0: listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY); michael@0: /* michael@0: * try a few times to bind server's address, if addresses are in michael@0: * use michael@0: */ michael@0: i = 0; michael@0: while (PR_Bind(listenSock, &listenAddr) == PR_FAILURE) { michael@0: if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) { michael@0: listenAddr.inet.port += 2; michael@0: if (i++ < SERVER_MAX_BIND_COUNT) michael@0: continue; michael@0: } michael@0: failed_already=1; michael@0: if (debug_mode) michael@0: PR_fprintf(output,"accept: ERROR - PR_Bind failed\n"); michael@0: return; michael@0: } michael@0: michael@0: michael@0: rv = PR_Listen(listenSock, 100); michael@0: if (rv == PR_FAILURE) { michael@0: failed_already=1; michael@0: if (debug_mode) michael@0: PR_fprintf(output, "unable to listen\n"); michael@0: return; michael@0: } michael@0: michael@0: clientCommand = clientAction; michael@0: clientThread = PR_CreateThread(PR_USER_THREAD, ClientThread, michael@0: (void *)&clientCommand, PR_PRIORITY_NORMAL, thread_scope, michael@0: PR_JOINABLE_THREAD, 0); michael@0: if (!clientThread) { michael@0: failed_already=1; michael@0: if (debug_mode) michael@0: PR_fprintf(output, "error creating client thread\n"); michael@0: return; michael@0: } michael@0: michael@0: iterations = count; michael@0: for (;iterations--;) { michael@0: switch (acceptType) { michael@0: case ACCEPT_NORMAL: michael@0: clientSock = PR_Accept(listenSock, &clientAddr, michael@0: timeoutTime); michael@0: switch(clientAction) { michael@0: case CLIENT_TIMEOUT_ACCEPT: michael@0: TEST_ASSERT(clientSock == 0); michael@0: TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR); michael@0: break; michael@0: case CLIENT_NORMAL: michael@0: TEST_ASSERT(clientSock); michael@0: bytesRead = PR_Recv(clientSock, michael@0: buf, CLIENT_DATA, 0, timeoutTime); michael@0: TEST_ASSERT(bytesRead == CLIENT_DATA); michael@0: break; michael@0: case CLIENT_TIMEOUT_SEND: michael@0: TEST_ASSERT(clientSock); michael@0: bytesRead = PR_Recv(clientSock, michael@0: buf, CLIENT_DATA, 0, timeoutTime); michael@0: TEST_ASSERT(bytesRead == -1); michael@0: TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR); michael@0: break; michael@0: } michael@0: break; michael@0: case ACCEPT_READ: michael@0: status = PR_AcceptRead(listenSock, &clientSock, michael@0: &raddr, buf, CLIENT_DATA, timeoutTime); michael@0: switch(clientAction) { michael@0: case CLIENT_TIMEOUT_ACCEPT: michael@0: /* Invalid test case */ michael@0: TEST_ASSERT(0); michael@0: break; michael@0: case CLIENT_NORMAL: michael@0: TEST_ASSERT(clientSock); michael@0: TEST_ASSERT(status == CLIENT_DATA); michael@0: break; michael@0: case CLIENT_TIMEOUT_SEND: michael@0: TEST_ASSERT(status == -1); michael@0: TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR); michael@0: break; michael@0: } michael@0: break; michael@0: #ifdef WINNT michael@0: case ACCEPT_FAST: michael@0: clientSock = PR_NTFast_Accept(listenSock, michael@0: &clientAddr, timeoutTime); michael@0: switch(clientAction) { michael@0: case CLIENT_TIMEOUT_ACCEPT: michael@0: TEST_ASSERT(clientSock == 0); michael@0: if (debug_mode) michael@0: PR_fprintf(output, "PR_GetError is %ld\n", PR_GetError()); michael@0: TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR); michael@0: break; michael@0: case CLIENT_NORMAL: michael@0: TEST_ASSERT(clientSock); michael@0: bytesRead = PR_Recv(clientSock, michael@0: buf, CLIENT_DATA, 0, timeoutTime); michael@0: TEST_ASSERT(bytesRead == CLIENT_DATA); michael@0: break; michael@0: case CLIENT_TIMEOUT_SEND: michael@0: TEST_ASSERT(clientSock); michael@0: bytesRead = PR_Recv(clientSock, michael@0: buf, CLIENT_DATA, 0, timeoutTime); michael@0: TEST_ASSERT(bytesRead == -1); michael@0: TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR); michael@0: break; michael@0: } michael@0: break; michael@0: break; michael@0: case ACCEPT_READ_FAST: michael@0: status = PR_NTFast_AcceptRead(listenSock, michael@0: &clientSock, &raddr, buf, 4096, timeoutTime); michael@0: switch(clientAction) { michael@0: case CLIENT_TIMEOUT_ACCEPT: michael@0: /* Invalid test case */ michael@0: TEST_ASSERT(0); michael@0: break; michael@0: case CLIENT_NORMAL: michael@0: TEST_ASSERT(clientSock); michael@0: TEST_ASSERT(status == CLIENT_DATA); michael@0: break; michael@0: case CLIENT_TIMEOUT_SEND: michael@0: TEST_ASSERT(clientSock == NULL); michael@0: TEST_ASSERT(status == -1); michael@0: TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR); michael@0: break; michael@0: } michael@0: break; michael@0: case ACCEPT_READ_FAST_CB: michael@0: status = PR_NTFast_AcceptRead_WithTimeoutCallback( michael@0: listenSock, &clientSock, &raddr, buf, 4096, michael@0: timeoutTime, timeout_callback, (void *)CALLBACK_MAGIC); michael@0: switch(clientAction) { michael@0: case CLIENT_TIMEOUT_ACCEPT: michael@0: /* Invalid test case */ michael@0: TEST_ASSERT(0); michael@0: break; michael@0: case CLIENT_NORMAL: michael@0: TEST_ASSERT(clientSock); michael@0: TEST_ASSERT(status == CLIENT_DATA); michael@0: break; michael@0: case CLIENT_TIMEOUT_SEND: michael@0: if (debug_mode) michael@0: PR_fprintf(output, "clientSock = 0x%8.8lx\n", clientSock); michael@0: TEST_ASSERT(clientSock == NULL); michael@0: TEST_ASSERT(status == -1); michael@0: TEST_ASSERT(PR_GetError() == PR_IO_TIMEOUT_ERROR); michael@0: break; michael@0: } michael@0: break; michael@0: #endif michael@0: } michael@0: if (clientSock != NULL) { michael@0: PR_Close(clientSock); michael@0: clientSock = NULL; michael@0: } michael@0: } michael@0: PR_Close(listenSock); michael@0: michael@0: PR_JoinThread(clientThread); michael@0: } michael@0: michael@0: michael@0: void AcceptUpdatedTest(void) michael@0: { michael@0: RunTest(ACCEPT_NORMAL, CLIENT_NORMAL); michael@0: } michael@0: void AcceptNotUpdatedTest(void) michael@0: { michael@0: RunTest(ACCEPT_FAST, CLIENT_NORMAL); michael@0: } michael@0: void AcceptReadTest(void) michael@0: { michael@0: RunTest(ACCEPT_READ, CLIENT_NORMAL); michael@0: } michael@0: void AcceptReadNotUpdatedTest(void) michael@0: { michael@0: RunTest(ACCEPT_READ_FAST, CLIENT_NORMAL); michael@0: } michael@0: void AcceptReadCallbackTest(void) michael@0: { michael@0: RunTest(ACCEPT_READ_FAST_CB, CLIENT_NORMAL); michael@0: } michael@0: michael@0: void TimeoutAcceptUpdatedTest(void) michael@0: { michael@0: RunTest(ACCEPT_NORMAL, CLIENT_TIMEOUT_ACCEPT); michael@0: } michael@0: void TimeoutAcceptNotUpdatedTest(void) michael@0: { michael@0: RunTest(ACCEPT_FAST, CLIENT_TIMEOUT_ACCEPT); michael@0: } michael@0: void TimeoutAcceptReadCallbackTest(void) michael@0: { michael@0: RunTest(ACCEPT_READ_FAST_CB, CLIENT_TIMEOUT_ACCEPT); michael@0: } michael@0: michael@0: void TimeoutReadUpdatedTest(void) michael@0: { michael@0: RunTest(ACCEPT_NORMAL, CLIENT_TIMEOUT_SEND); michael@0: } michael@0: void TimeoutReadNotUpdatedTest(void) michael@0: { michael@0: RunTest(ACCEPT_FAST, CLIENT_TIMEOUT_SEND); michael@0: } michael@0: void TimeoutReadReadTest(void) michael@0: { michael@0: RunTest(ACCEPT_READ, CLIENT_TIMEOUT_SEND); michael@0: } michael@0: void TimeoutReadReadNotUpdatedTest(void) michael@0: { michael@0: RunTest(ACCEPT_READ_FAST, CLIENT_TIMEOUT_SEND); michael@0: } michael@0: void TimeoutReadReadCallbackTest(void) michael@0: { michael@0: RunTest(ACCEPT_READ_FAST_CB, CLIENT_TIMEOUT_SEND); michael@0: } michael@0: michael@0: /************************************************************************/ michael@0: michael@0: static void Measure(void (*func)(void), const char *msg) michael@0: { michael@0: PRIntervalTime start, stop; michael@0: double d; michael@0: michael@0: start = PR_IntervalNow(); michael@0: (*func)(); michael@0: stop = PR_IntervalNow(); michael@0: michael@0: d = (double)PR_IntervalToMicroseconds(stop - start); michael@0: if (debug_mode) michael@0: PR_fprintf(output, "%40s: %6.2f usec\n", msg, d / count); michael@0: michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: michael@0: /* The command line argument: -d is used to determine if the test is being run michael@0: in debug mode. The regress tool requires only one line output:PASS or FAIL. michael@0: All of the printfs associated with this test has been handled with a if (debug_mode) michael@0: test. michael@0: Usage: test_name [-d] [-c n] michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "Gdc:"); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'G': /* global threads */ michael@0: thread_scope = PR_GLOBAL_THREAD; michael@0: break; michael@0: case 'd': /* debug mode */ michael@0: debug_mode = 1; michael@0: break; michael@0: case 'c': /* loop counter */ michael@0: count = atoi(opt->value); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: output = PR_STDERR; michael@0: PR_STDIO_INIT(); michael@0: michael@0: timeoutTime = PR_SecondsToInterval(TIMEOUTSECS); michael@0: if (debug_mode) michael@0: PR_fprintf(output, "\nRun accept() sucessful connection tests\n"); michael@0: michael@0: Measure(AcceptUpdatedTest, "PR_Accept()"); michael@0: Measure(AcceptReadTest, "PR_AcceptRead()"); michael@0: #ifdef WINNT michael@0: Measure(AcceptNotUpdatedTest, "PR_NTFast_Accept()"); michael@0: Measure(AcceptReadNotUpdatedTest, "PR_NTFast_AcceptRead()"); michael@0: Measure(AcceptReadCallbackTest, "PR_NTFast_AcceptRead_WithTimeoutCallback()"); michael@0: #endif michael@0: if (debug_mode) michael@0: PR_fprintf(output, "\nRun accept() timeout in the accept tests\n"); michael@0: #ifdef WINNT michael@0: Measure(TimeoutReadReadCallbackTest, "PR_NTFast_AcceptRead_WithTimeoutCallback()"); michael@0: #endif michael@0: Measure(TimeoutReadUpdatedTest, "PR_Accept()"); michael@0: if (debug_mode) michael@0: PR_fprintf(output, "\nRun accept() timeout in the read tests\n"); michael@0: Measure(TimeoutReadReadTest, "PR_AcceptRead()"); michael@0: #ifdef WINNT michael@0: Measure(TimeoutReadNotUpdatedTest, "PR_NTFast_Accept()"); michael@0: Measure(TimeoutReadReadNotUpdatedTest, "PR_NTFast_AcceptRead()"); michael@0: Measure(TimeoutReadReadCallbackTest, "PR_NTFast_AcceptRead_WithTimeoutCallback()"); michael@0: #endif michael@0: PR_fprintf(output, "%s\n", (failed_already) ? "FAIL" : "PASS"); michael@0: return failed_already; michael@0: }