Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
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 **
8 ** Name: prpoll_to.c
9 **
10 ** Description: This program tests PR_Poll with sockets.
11 ** Timeout operation is tested
12 **
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 /***********************************************************************
24 ** Includes
25 ***********************************************************************/
26 /* Used to get the command line option */
27 #include "plgetopt.h"
29 #include "prinit.h"
30 #include "prio.h"
31 #include "prlog.h"
32 #include "prprf.h"
33 #include "prnetdb.h"
35 #include "private/pprio.h"
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
41 PRIntn failed_already=0;
42 PRIntn debug_mode;
44 int main(int argc, char **argv)
45 {
46 PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
47 PRUint16 listenPort1, listenPort2;
48 PRNetAddr addr;
49 char buf[128];
50 PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
51 PRIntn npds;
52 PRInt32 retVal;
54 /* The command line argument: -d is used to determine if the test is being run
55 in debug mode. The regress tool requires only one line output:PASS or FAIL.
56 All of the printfs associated with this test has been handled with a if (debug_mode)
57 test.
58 Usage: test_name -d
59 */
60 PLOptStatus os;
61 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
62 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
63 {
64 if (PL_OPT_BAD == os) continue;
65 switch (opt->option)
66 {
67 case 'd': /* debug mode */
68 debug_mode = 1;
69 break;
70 default:
71 break;
72 }
73 }
74 PL_DestroyOptState(opt);
76 /* main test */
78 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
79 PR_STDIO_INIT();
81 if (debug_mode) {
82 printf("This program tests PR_Poll with sockets.\n");
83 printf("Timeout is tested.\n\n");
84 }
86 /* Create two listening sockets */
87 if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
88 fprintf(stderr, "Can't create a new TCP socket\n");
89 if (!debug_mode) failed_already=1;
90 goto exit_now;
91 }
92 memset(&addr, 0, sizeof(addr));
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");
98 if (!debug_mode) failed_already=1;
99 goto exit_now;
100 }
101 if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
102 fprintf(stderr, "PR_GetSockName failed\n");
103 if (!debug_mode) failed_already=1;
104 goto exit_now;
105 }
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");
109 if (!debug_mode) failed_already=1;
110 goto exit_now;
111 }
113 if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
114 fprintf(stderr, "Can't create a new TCP socket\n");
115 if (!debug_mode) failed_already=1;
116 goto exit_now;
117 }
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");
123 if (!debug_mode) failed_already=1;
124 goto exit_now;
125 }
126 if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
127 fprintf(stderr, "PR_GetSockName failed\n");
128 if (!debug_mode) failed_already=1;
129 goto exit_now;
130 }
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");
134 if (!debug_mode) failed_already=1;
135 goto exit_now;
136 }
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 poll descriptor array */
143 pds = pds0;
144 other_pds = pds1;
145 memset(pds, 0, sizeof(pds));
146 pds[0].fd = listenSock1;
147 pds[0].in_flags = PR_POLL_READ;
148 pds[1].fd = listenSock2;
149 pds[1].in_flags = PR_POLL_READ;
150 npds = 2;
152 /* Testing timeout */
153 if (debug_mode) printf("PR_Poll should time out in 5 seconds\n");
154 retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
155 if (retVal != 0) {
156 PR_snprintf(buf, sizeof(buf),
157 "PR_Poll should time out and return 0, but it returns %ld\n",
158 retVal);
159 fprintf(stderr, "%s", buf);
160 if (!debug_mode) failed_already=1;
161 goto exit_now;
162 }
163 if (debug_mode) printf("PR_Poll timed out. Test passed.\n\n");
165 exit_now:
167 if (listenSock1) {
168 PR_Close(listenSock1);
169 }
170 if (listenSock2) {
171 PR_Close(listenSock2);
172 }
174 PR_Cleanup();
176 if(failed_already)
177 return 1;
178 else
179 return 0;
181 }