1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /***********************************************************************
7 ** 1997 - Netscape Communications Corporation
11 ** Description: tests PR_Select with sockets. Time out functions
13 ** Modification History:
14 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
15 ** The debug mode will print all of the printfs associated with this
17 ** The regress mode will be the default mode. Since the regress tool
19 ** the output to a one line status:PASS or FAIL,all of the printf
21 ** have been handled with an if (debug_mode) statement.
22 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been
24 ** recognize the return code from tha main program.
25 ***********************************************************************/
27 /***********************************************************************
29 ***********************************************************************/
30 /* Used to get the command line option */
39 #include "obsolete/probslet.h"
47 PRIntn failed_already
= 0;
50 int main(int argc
, char** argv
) {
51 PRFileDesc
*listenSock1
, *listenSock2
;
52 PRUint16 listenPort1
, listenPort2
;
58 /* The command line argument: -d is used to determine if the test is being run
59 in debug mode. The regress tool requires only one line output:PASS or FAIL.
60 All of the printfs associated with this test has been handled with a if
61 (debug_mode) test. Usage: test_name -d
64 PLOptState
* opt
= PL_CreateOptState(argc
, argv
, "d:");
65 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
))) {
66 if (PL_OPT_BAD
== os
) {
69 switch (opt
->option
) {
70 case 'd': /* debug mode */
77 PL_DestroyOptState(opt
);
81 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
85 printf("This program tests PR_Select with sockets. Timeout \n");
86 printf("operations are tested.\n\n");
89 /* Create two listening sockets */
90 if ((listenSock1
= PR_NewTCPSocket()) == NULL
) {
91 fprintf(stderr
, "Can't create a new TCP socket\n");
95 addr
.inet
.family
= PR_AF_INET
;
96 addr
.inet
.ip
= PR_htonl(PR_INADDR_ANY
);
97 addr
.inet
.port
= PR_htons(0);
98 if (PR_Bind(listenSock1
, &addr
) == PR_FAILURE
) {
99 fprintf(stderr
, "Can't bind socket\n");
103 if (PR_GetSockName(listenSock1
, &addr
) == PR_FAILURE
) {
104 fprintf(stderr
, "PR_GetSockName failed\n");
108 listenPort1
= PR_ntohs(addr
.inet
.port
);
109 if (PR_Listen(listenSock1
, 5) == PR_FAILURE
) {
110 fprintf(stderr
, "Can't listen on a socket\n");
115 if ((listenSock2
= PR_NewTCPSocket()) == NULL
) {
116 fprintf(stderr
, "Can't create a new TCP socket\n");
120 addr
.inet
.family
= PR_AF_INET
;
121 addr
.inet
.ip
= PR_htonl(PR_INADDR_ANY
);
122 addr
.inet
.port
= PR_htons(0);
123 if (PR_Bind(listenSock2
, &addr
) == PR_FAILURE
) {
124 fprintf(stderr
, "Can't bind socket\n");
128 if (PR_GetSockName(listenSock2
, &addr
) == PR_FAILURE
) {
129 fprintf(stderr
, "PR_GetSockName failed\n");
133 listenPort2
= PR_ntohs(addr
.inet
.port
);
134 if (PR_Listen(listenSock2
, 5) == PR_FAILURE
) {
135 fprintf(stderr
, "Can't listen on a socket\n");
139 PR_snprintf(buf
, sizeof(buf
),
140 "The server thread is listening on ports %hu and %hu\n\n",
141 listenPort1
, listenPort2
);
146 /* Set up the fd set */
147 PR_FD_ZERO(&readFdSet
);
148 PR_FD_SET(listenSock1
, &readFdSet
);
149 PR_FD_SET(listenSock2
, &readFdSet
);
151 /* Testing timeout */
153 printf("PR_Select should time out in 5 seconds\n");
155 retVal
= PR_Select(0 /* unused */, &readFdSet
, NULL
, NULL
,
156 PR_SecondsToInterval(5));
158 PR_snprintf(buf
, sizeof(buf
),
159 "PR_Select should time out and return 0, but it returns %ld\n",
161 fprintf(stderr
, "%s", buf
);
163 fprintf(stderr
, "Error %d, oserror %d\n", PR_GetError(), PR_GetOSError());
169 printf("PR_Select timed out. Test passed.\n\n");
175 if (failed_already
) {