1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/poll_to.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,181 @@ 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 +** 1.11 +** Name: prpoll_to.c 1.12 +** 1.13 +** Description: This program tests PR_Poll with sockets. 1.14 +** Timeout operation is tested 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 "private/pprio.h" 1.39 + 1.40 +#include <stdio.h> 1.41 +#include <string.h> 1.42 +#include <stdlib.h> 1.43 + 1.44 +PRIntn failed_already=0; 1.45 +PRIntn debug_mode; 1.46 + 1.47 +int main(int argc, char **argv) 1.48 +{ 1.49 + PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL; 1.50 + PRUint16 listenPort1, listenPort2; 1.51 + PRNetAddr addr; 1.52 + char buf[128]; 1.53 + PRPollDesc pds0[10], pds1[10], *pds, *other_pds; 1.54 + PRIntn npds; 1.55 + PRInt32 retVal; 1.56 + 1.57 + /* The command line argument: -d is used to determine if the test is being run 1.58 + in debug mode. The regress tool requires only one line output:PASS or FAIL. 1.59 + All of the printfs associated with this test has been handled with a if (debug_mode) 1.60 + test. 1.61 + Usage: test_name -d 1.62 + */ 1.63 + PLOptStatus os; 1.64 + PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); 1.65 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.66 + { 1.67 + if (PL_OPT_BAD == os) continue; 1.68 + switch (opt->option) 1.69 + { 1.70 + case 'd': /* debug mode */ 1.71 + debug_mode = 1; 1.72 + break; 1.73 + default: 1.74 + break; 1.75 + } 1.76 + } 1.77 + PL_DestroyOptState(opt); 1.78 + 1.79 + /* main test */ 1.80 + 1.81 + PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 1.82 + PR_STDIO_INIT(); 1.83 + 1.84 + if (debug_mode) { 1.85 + printf("This program tests PR_Poll with sockets.\n"); 1.86 + printf("Timeout is tested.\n\n"); 1.87 + } 1.88 + 1.89 + /* Create two listening sockets */ 1.90 + if ((listenSock1 = PR_NewTCPSocket()) == NULL) { 1.91 + fprintf(stderr, "Can't create a new TCP socket\n"); 1.92 + if (!debug_mode) failed_already=1; 1.93 + goto exit_now; 1.94 + } 1.95 + memset(&addr, 0, sizeof(addr)); 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 + if (!debug_mode) 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 + if (!debug_mode) 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 + if (!debug_mode) 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 + if (!debug_mode) 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 + if (!debug_mode) 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 + if (!debug_mode) 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 + if (!debug_mode) 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 poll descriptor array */ 1.146 + pds = pds0; 1.147 + other_pds = pds1; 1.148 + memset(pds, 0, sizeof(pds)); 1.149 + pds[0].fd = listenSock1; 1.150 + pds[0].in_flags = PR_POLL_READ; 1.151 + pds[1].fd = listenSock2; 1.152 + pds[1].in_flags = PR_POLL_READ; 1.153 + npds = 2; 1.154 + 1.155 + /* Testing timeout */ 1.156 + if (debug_mode) printf("PR_Poll should time out in 5 seconds\n"); 1.157 + retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5)); 1.158 + if (retVal != 0) { 1.159 + PR_snprintf(buf, sizeof(buf), 1.160 + "PR_Poll should time out and return 0, but it returns %ld\n", 1.161 + retVal); 1.162 + fprintf(stderr, "%s", buf); 1.163 + if (!debug_mode) failed_already=1; 1.164 + goto exit_now; 1.165 + } 1.166 + if (debug_mode) printf("PR_Poll timed out. Test passed.\n\n"); 1.167 + 1.168 +exit_now: 1.169 + 1.170 + if (listenSock1) { 1.171 + PR_Close(listenSock1); 1.172 + } 1.173 + if (listenSock2) { 1.174 + PR_Close(listenSock2); 1.175 + } 1.176 + 1.177 + PR_Cleanup(); 1.178 + 1.179 + if(failed_already) 1.180 + return 1; 1.181 + else 1.182 + return 0; 1.183 + 1.184 +}