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: ** 1997 - Netscape Communications Corporation michael@0: ** michael@0: ** Name: prselect_err.c michael@0: ** michael@0: ** Description: tests PR_Select with sockets Error condition functions. michael@0: ** michael@0: ** Modification History: michael@0: ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. 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: ***********************************************************************/ michael@0: michael@0: #ifdef XP_BEOS michael@0: #include michael@0: int main() michael@0: { michael@0: printf( "This test is not ported to the BeOS\n" ); michael@0: return 0; michael@0: } michael@0: #else michael@0: michael@0: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: /* Used to get the command line option */ michael@0: #include "plgetopt.h" michael@0: michael@0: #include "primpl.h" michael@0: #include "pprio.h" michael@0: #include "prnetdb.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: michael@0: PRIntn failed_already=0; michael@0: PRIntn debug_mode; michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRFileDesc *listenSock1, *listenSock2; michael@0: PRFileDesc *badFD; michael@0: PRUint16 listenPort1, listenPort2; michael@0: PRNetAddr addr; michael@0: PR_fd_set readFdSet; michael@0: char buf[128]; michael@0: PRInt32 retVal; 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 michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); 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 'd': /* debug mode */ michael@0: debug_mode = 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: /* main test */ michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: PR_STDIO_INIT(); michael@0: michael@0: if (debug_mode) { michael@0: printf("This program tests PR_Select with sockets. Error\n"); michael@0: printf("reporting operations are tested.\n\n"); michael@0: } michael@0: michael@0: /* Create two listening sockets */ michael@0: if ((listenSock1 = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr, "Can't create a new TCP socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: addr.inet.family = AF_INET; michael@0: addr.inet.ip = PR_htonl(INADDR_ANY); michael@0: addr.inet.port = PR_htons(0); michael@0: if (PR_Bind(listenSock1, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "Can't bind socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetSockName failed\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: listenPort1 = PR_ntohs(addr.inet.port); michael@0: if (PR_Listen(listenSock1, 5) == PR_FAILURE) { michael@0: fprintf(stderr, "Can't listen on a socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: michael@0: if ((listenSock2 = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr, "Can't create a new TCP socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: addr.inet.family = AF_INET; michael@0: addr.inet.ip = PR_htonl(INADDR_ANY); michael@0: addr.inet.port = PR_htons(0); michael@0: if (PR_Bind(listenSock2, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "Can't bind socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) { michael@0: fprintf(stderr, "PR_GetSockName failed\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: listenPort2 = PR_ntohs(addr.inet.port); michael@0: if (PR_Listen(listenSock2, 5) == PR_FAILURE) { michael@0: fprintf(stderr, "Can't listen on a socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: PR_snprintf(buf, sizeof(buf), michael@0: "The server thread is listening on ports %hu and %hu\n\n", michael@0: listenPort1, listenPort2); michael@0: if (debug_mode) printf("%s", buf); michael@0: michael@0: /* Set up the fd set */ michael@0: PR_FD_ZERO(&readFdSet); michael@0: PR_FD_SET(listenSock1, &readFdSet); michael@0: PR_FD_SET(listenSock2, &readFdSet); michael@0: michael@0: michael@0: /* Testing bad fd */ michael@0: if (debug_mode) printf("PR_Select should detect a bad file descriptor\n"); michael@0: if ((badFD = PR_NewTCPSocket()) == NULL) { michael@0: fprintf(stderr, "Can't create a TCP socket\n"); michael@0: failed_already=1; michael@0: goto exit_now; michael@0: } michael@0: michael@0: PR_FD_SET(badFD, &readFdSet); michael@0: /* michael@0: * Make the fd invalid michael@0: */ michael@0: #if defined(XP_UNIX) michael@0: close(PR_FileDesc2NativeHandle(badFD)); michael@0: #elif defined(XP_OS2) michael@0: soclose(PR_FileDesc2NativeHandle(badFD)); michael@0: #elif defined(WIN32) || defined(WIN16) michael@0: closesocket(PR_FileDesc2NativeHandle(badFD)); michael@0: #else michael@0: #error "Unknown architecture" michael@0: #endif michael@0: michael@0: retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL, michael@0: PR_INTERVAL_NO_TIMEOUT); michael@0: if (retVal != -1 || PR_GetError() != PR_BAD_DESCRIPTOR_ERROR) { michael@0: fprintf(stderr, "Failed to detect the bad fd: " michael@0: "PR_Select returns %d\n", retVal); michael@0: if (retVal == -1) { michael@0: fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(), michael@0: PR_GetOSError()); michael@0: failed_already=1; michael@0: } michael@0: goto exit_now; michael@0: } michael@0: if (debug_mode) printf("PR_Select detected a bad fd. Test passed.\n\n"); michael@0: PR_FD_CLR(badFD, &readFdSet); michael@0: michael@0: PR_Cleanup(); michael@0: goto exit_now; michael@0: exit_now: michael@0: if(failed_already) michael@0: return 1; michael@0: else michael@0: return 0; michael@0: michael@0: } michael@0: michael@0: #endif /* XP_BEOS */