No Bug, mozilla-central repo-update HSTS HPKP remote-settings tld-suffixes ct-logs...
[gecko.git] / nsprpub / pr / tests / selct_to.c
blob7460a270f0412c23cbbe7dfbb609e2769b6f7e11
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
8 **
9 ** Name: prselect_to.c
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
16 *test.
17 ** The regress mode will be the default mode. Since the regress tool
18 *limits
19 ** the output to a one line status:PASS or FAIL,all of the printf
20 *statements
21 ** have been handled with an if (debug_mode) statement.
22 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been
23 *updated to
24 ** recognize the return code from tha main program.
25 ***********************************************************************/
27 /***********************************************************************
28 ** Includes
29 ***********************************************************************/
30 /* Used to get the command line option */
31 #include "plgetopt.h"
33 #include "prinit.h"
34 #include "prio.h"
35 #include "prlog.h"
36 #include "prprf.h"
37 #include "prnetdb.h"
39 #include "obsolete/probslet.h"
41 #include "prerror.h"
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
47 PRIntn failed_already = 0;
48 PRIntn debug_mode;
50 int main(int argc, char** argv) {
51 PRFileDesc *listenSock1, *listenSock2;
52 PRUint16 listenPort1, listenPort2;
53 PRNetAddr addr;
54 PR_fd_set readFdSet;
55 char buf[128];
56 PRInt32 retVal;
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
63 PLOptStatus os;
64 PLOptState* opt = PL_CreateOptState(argc, argv, "d:");
65 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
66 if (PL_OPT_BAD == os) {
67 continue;
69 switch (opt->option) {
70 case 'd': /* debug mode */
71 debug_mode = 1;
72 break;
73 default:
74 break;
77 PL_DestroyOptState(opt);
79 /* main test */
81 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
82 PR_STDIO_INIT();
84 if (debug_mode) {
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");
92 failed_already = 1;
93 goto exit_now;
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");
100 failed_already = 1;
101 goto exit_now;
103 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
104 fprintf(stderr, "PR_GetSockName failed\n");
105 failed_already = 1;
106 goto exit_now;
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");
111 failed_already = 1;
112 goto exit_now;
115 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
116 fprintf(stderr, "Can't create a new TCP socket\n");
117 failed_already = 1;
118 goto exit_now;
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");
125 failed_already = 1;
126 goto exit_now;
128 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
129 fprintf(stderr, "PR_GetSockName failed\n");
130 failed_already = 1;
131 goto exit_now;
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");
136 failed_already = 1;
137 goto exit_now;
139 PR_snprintf(buf, sizeof(buf),
140 "The server thread is listening on ports %hu and %hu\n\n",
141 listenPort1, listenPort2);
142 if (debug_mode) {
143 printf("%s", buf);
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 */
152 if (debug_mode) {
153 printf("PR_Select should time out in 5 seconds\n");
155 retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
156 PR_SecondsToInterval(5));
157 if (retVal != 0) {
158 PR_snprintf(buf, sizeof(buf),
159 "PR_Select should time out and return 0, but it returns %ld\n",
160 retVal);
161 fprintf(stderr, "%s", buf);
162 if (retVal == -1) {
163 fprintf(stderr, "Error %d, oserror %d\n", PR_GetError(), PR_GetOSError());
164 failed_already = 1;
166 goto exit_now;
168 if (debug_mode) {
169 printf("PR_Select timed out. Test passed.\n\n");
172 PR_Cleanup();
174 exit_now:
175 if (failed_already) {
176 return 1;
177 } else {
178 return 0;