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 test.
16 ** The regress mode will be the default mode. Since the regress tool limits
17 ** the output to a one line status:PASS or FAIL,all of the printf statements
18 ** have been handled with an if (debug_mode) statement.
19 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
20 ** recognize the return code from tha main program.
21 ***********************************************************************/
23 /***********************************************************************
25 ***********************************************************************/
26 /* Used to get the command line option */
35 #include "obsolete/probslet.h"
43 PRIntn failed_already
=0;
46 int main(int argc
, char **argv
)
48 PRFileDesc
*listenSock1
, *listenSock2
;
49 PRUint16 listenPort1
, listenPort2
;
55 /* The command line argument: -d is used to determine if the test is being run
56 in debug mode. The regress tool requires only one line output:PASS or FAIL.
57 All of the printfs associated with this test has been handled with a if (debug_mode)
62 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "d:");
63 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
65 if (PL_OPT_BAD
== os
) continue;
68 case 'd': /* debug mode */
75 PL_DestroyOptState(opt
);
79 PR_Init(PR_USER_THREAD
, PR_PRIORITY_NORMAL
, 0);
83 printf("This program tests PR_Select with sockets. Timeout \n");
84 printf("operations are tested.\n\n");
87 /* Create two listening sockets */
88 if ((listenSock1
= PR_NewTCPSocket()) == NULL
) {
89 fprintf(stderr
, "Can't create a new TCP socket\n");
93 addr
.inet
.family
= PR_AF_INET
;
94 addr
.inet
.ip
= PR_htonl(PR_INADDR_ANY
);
95 addr
.inet
.port
= PR_htons(0);
96 if (PR_Bind(listenSock1
, &addr
) == PR_FAILURE
) {
97 fprintf(stderr
, "Can't bind socket\n");
101 if (PR_GetSockName(listenSock1
, &addr
) == PR_FAILURE
) {
102 fprintf(stderr
, "PR_GetSockName failed\n");
106 listenPort1
= PR_ntohs(addr
.inet
.port
);
107 if (PR_Listen(listenSock1
, 5) == PR_FAILURE
) {
108 fprintf(stderr
, "Can't listen on a socket\n");
113 if ((listenSock2
= PR_NewTCPSocket()) == NULL
) {
114 fprintf(stderr
, "Can't create a new TCP socket\n");
118 addr
.inet
.family
= PR_AF_INET
;
119 addr
.inet
.ip
= PR_htonl(PR_INADDR_ANY
);
120 addr
.inet
.port
= PR_htons(0);
121 if (PR_Bind(listenSock2
, &addr
) == PR_FAILURE
) {
122 fprintf(stderr
, "Can't bind socket\n");
126 if (PR_GetSockName(listenSock2
, &addr
) == PR_FAILURE
) {
127 fprintf(stderr
, "PR_GetSockName failed\n");
131 listenPort2
= PR_ntohs(addr
.inet
.port
);
132 if (PR_Listen(listenSock2
, 5) == PR_FAILURE
) {
133 fprintf(stderr
, "Can't listen on a socket\n");
137 PR_snprintf(buf
, sizeof(buf
),
138 "The server thread is listening on ports %hu and %hu\n\n",
139 listenPort1
, listenPort2
);
140 if (debug_mode
) printf("%s", buf
);
142 /* Set up the fd set */
143 PR_FD_ZERO(&readFdSet
);
144 PR_FD_SET(listenSock1
, &readFdSet
);
145 PR_FD_SET(listenSock2
, &readFdSet
);
147 /* Testing timeout */
148 if (debug_mode
) printf("PR_Select should time out in 5 seconds\n");
149 retVal
= PR_Select(0 /* unused */, &readFdSet
, NULL
, NULL
,
150 PR_SecondsToInterval(5));
152 PR_snprintf(buf
, sizeof(buf
),
153 "PR_Select should time out and return 0, but it returns %ld\n",
155 fprintf(stderr
, "%s", buf
);
157 fprintf(stderr
, "Error %d, oserror %d\n", PR_GetError(),
163 if (debug_mode
) printf("PR_Select timed out. Test passed.\n\n");