1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/selct_to.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,172 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/*********************************************************************** 1.10 +** 1997 - Netscape Communications Corporation 1.11 +** 1.12 +** Name: prselect_to.c 1.13 +** 1.14 +** Description: tests PR_Select with sockets. Time out functions 1.15 +** 1.16 +** Modification History: 1.17 +** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. 1.18 +** The debug mode will print all of the printfs associated with this test. 1.19 +** The regress mode will be the default mode. Since the regress tool limits 1.20 +** the output to a one line status:PASS or FAIL,all of the printf statements 1.21 +** have been handled with an if (debug_mode) statement. 1.22 +** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to 1.23 +** recognize the return code from tha main program. 1.24 +***********************************************************************/ 1.25 + 1.26 +/*********************************************************************** 1.27 +** Includes 1.28 +***********************************************************************/ 1.29 +/* Used to get the command line option */ 1.30 +#include "plgetopt.h" 1.31 + 1.32 +#include "prinit.h" 1.33 +#include "prio.h" 1.34 +#include "prlog.h" 1.35 +#include "prprf.h" 1.36 +#include "prnetdb.h" 1.37 + 1.38 +#include "obsolete/probslet.h" 1.39 + 1.40 +#include "prerror.h" 1.41 + 1.42 +#include <stdio.h> 1.43 +#include <string.h> 1.44 +#include <stdlib.h> 1.45 + 1.46 +PRIntn failed_already=0; 1.47 +PRIntn debug_mode; 1.48 + 1.49 +int main(int argc, char **argv) 1.50 +{ 1.51 + PRFileDesc *listenSock1, *listenSock2; 1.52 + PRUint16 listenPort1, listenPort2; 1.53 + PRNetAddr addr; 1.54 + PR_fd_set readFdSet; 1.55 + char buf[128]; 1.56 + PRInt32 retVal; 1.57 + 1.58 + /* The command line argument: -d is used to determine if the test is being run 1.59 + in debug mode. The regress tool requires only one line output:PASS or FAIL. 1.60 + All of the printfs associated with this test has been handled with a if (debug_mode) 1.61 + test. 1.62 + Usage: test_name -d 1.63 + */ 1.64 + PLOptStatus os; 1.65 + PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); 1.66 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.67 + { 1.68 + if (PL_OPT_BAD == os) continue; 1.69 + switch (opt->option) 1.70 + { 1.71 + case 'd': /* debug mode */ 1.72 + debug_mode = 1; 1.73 + break; 1.74 + default: 1.75 + break; 1.76 + } 1.77 + } 1.78 + PL_DestroyOptState(opt); 1.79 + 1.80 + /* main test */ 1.81 + 1.82 + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 1.83 + PR_STDIO_INIT(); 1.84 + 1.85 + if (debug_mode) { 1.86 + printf("This program tests PR_Select with sockets. Timeout \n"); 1.87 + printf("operations are tested.\n\n"); 1.88 + } 1.89 + 1.90 + /* Create two listening sockets */ 1.91 + if ((listenSock1 = PR_NewTCPSocket()) == NULL) { 1.92 + fprintf(stderr, "Can't create a new TCP socket\n"); 1.93 + failed_already=1; 1.94 + goto exit_now; 1.95 + } 1.96 + addr.inet.family = PR_AF_INET; 1.97 + addr.inet.ip = PR_htonl(PR_INADDR_ANY); 1.98 + addr.inet.port = PR_htons(0); 1.99 + if (PR_Bind(listenSock1, &addr) == PR_FAILURE) { 1.100 + fprintf(stderr, "Can't bind socket\n"); 1.101 + failed_already=1; 1.102 + goto exit_now; 1.103 + } 1.104 + if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) { 1.105 + fprintf(stderr, "PR_GetSockName failed\n"); 1.106 + failed_already=1; 1.107 + goto exit_now; 1.108 + } 1.109 + listenPort1 = PR_ntohs(addr.inet.port); 1.110 + if (PR_Listen(listenSock1, 5) == PR_FAILURE) { 1.111 + fprintf(stderr, "Can't listen on a socket\n"); 1.112 + failed_already=1; 1.113 + goto exit_now; 1.114 + } 1.115 + 1.116 + if ((listenSock2 = PR_NewTCPSocket()) == NULL) { 1.117 + fprintf(stderr, "Can't create a new TCP socket\n"); 1.118 + failed_already=1; 1.119 + goto exit_now; 1.120 + } 1.121 + addr.inet.family = PR_AF_INET; 1.122 + addr.inet.ip = PR_htonl(PR_INADDR_ANY); 1.123 + addr.inet.port = PR_htons(0); 1.124 + if (PR_Bind(listenSock2, &addr) == PR_FAILURE) { 1.125 + fprintf(stderr, "Can't bind socket\n"); 1.126 + failed_already=1; 1.127 + goto exit_now; 1.128 + } 1.129 + if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) { 1.130 + fprintf(stderr, "PR_GetSockName failed\n"); 1.131 + failed_already=1; 1.132 + goto exit_now; 1.133 + } 1.134 + listenPort2 = PR_ntohs(addr.inet.port); 1.135 + if (PR_Listen(listenSock2, 5) == PR_FAILURE) { 1.136 + fprintf(stderr, "Can't listen on a socket\n"); 1.137 + failed_already=1; 1.138 + goto exit_now; 1.139 + } 1.140 + PR_snprintf(buf, sizeof(buf), 1.141 + "The server thread is listening on ports %hu and %hu\n\n", 1.142 + listenPort1, listenPort2); 1.143 + if (debug_mode) printf("%s", buf); 1.144 + 1.145 + /* Set up the fd set */ 1.146 + PR_FD_ZERO(&readFdSet); 1.147 + PR_FD_SET(listenSock1, &readFdSet); 1.148 + PR_FD_SET(listenSock2, &readFdSet); 1.149 + 1.150 + /* Testing timeout */ 1.151 + if (debug_mode) printf("PR_Select should time out in 5 seconds\n"); 1.152 + retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL, 1.153 + PR_SecondsToInterval(5)); 1.154 + if (retVal != 0) { 1.155 + PR_snprintf(buf, sizeof(buf), 1.156 + "PR_Select should time out and return 0, but it returns %ld\n", 1.157 + retVal); 1.158 + fprintf(stderr, "%s", buf); 1.159 + if (retVal == -1) { 1.160 + fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(), 1.161 + PR_GetOSError()); 1.162 + failed_already=1; 1.163 + } 1.164 + goto exit_now; 1.165 + } 1.166 + if (debug_mode) printf("PR_Select timed out. Test passed.\n\n"); 1.167 + 1.168 + PR_Cleanup(); 1.169 + 1.170 +exit_now: 1.171 + if(failed_already) 1.172 + return 1; 1.173 + else 1.174 + return 0; 1.175 +}