1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is the Netscape Portable Runtime (NSPR).
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
22
* Alternatively, the contents of this file may be used under the
23
* terms of the GNU General Public License Version 2 or later (the
24
* "GPL"), in which case the provisions of the GPL are applicable
25
* instead of those above. If you wish to allow use of your
26
* version of this file only under the terms of the GPL and not to
27
* allow others to use your version of this file under the MPL,
28
* indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by
30
* the GPL. If you do not delete the provisions above, a recipient
31
* may use your version of this file under either the MPL or the
35
/***********************************************************************
37
** Name: prpoll_norm.c
39
** Description: This program tests PR_Poll with sockets.
40
** Normal operation are tested
42
** Modification History:
43
** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
44
** The debug mode will print all of the printfs associated with this test.
45
** The regress mode will be the default mode. Since the regress tool limits
46
** the output to a one line status:PASS or FAIL,all of the printf statements
47
** have been handled with an if (debug_mode) statement.
48
** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
49
** recognize the return code from tha main program.
50
***********************************************************************/
52
/***********************************************************************
54
***********************************************************************/
55
/* Used to get the command line option */
64
#include "obsolete/probslet.h"
70
#include "private/pprio.h"
78
PRIntn failed_already=0;
81
#define NUM_ITERATIONS 5
84
int fprintf(FILE *stream, const char *fmt, ...)
89
#define printf PR_LogPrint
90
extern void SetupMacPrintfLog(char *logFile);
93
static void PR_CALLBACK
94
clientThreadFunc(void *arg)
96
PRUintn port = (PRUintn) arg;
104
addr.inet.family = PR_AF_INET;
105
addr.inet.port = PR_htons((PRUint16)port);
106
addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
107
memset(buf, 0, sizeof(buf));
108
PR_snprintf(buf, sizeof(buf), "%hu", port);
110
for (i = 0; i < NUM_ITERATIONS; i++) {
111
sock = PR_NewTCPSocket();
112
PR_ASSERT(sock != NULL);
114
sts = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
115
PR_ASSERT(sts == PR_SUCCESS);
117
n = PR_Write(sock, buf, sizeof(buf));
120
sts = PR_Close(sock);
121
PR_ASSERT(sts == PR_SUCCESS);
125
int main(int argc, char **argv)
127
PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
128
PRUint16 listenPort1, listenPort2;
131
PRThread *clientThread;
132
PRPollDesc pds0[20], pds1[20], *pds, *other_pds;
136
PRSocketOptionData optval;
138
/* The command line argument: -d is used to determine if the test is being run
139
in debug mode. The regress tool requires only one line output:PASS or FAIL.
140
All of the printfs associated with this test has been handled with a if (debug_mode)
145
PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
146
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
148
if (PL_OPT_BAD == os) continue;
151
case 'd': /* debug mode */
158
PL_DestroyOptState(opt);
162
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
167
SetupMacPrintfLog("poll_nm.log");
171
printf("This program tests PR_Poll with sockets.\n");
172
printf("Normal operation are tested.\n\n");
175
/* Create two listening sockets */
176
if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
177
fprintf(stderr, "Can't create a new TCP socket\n");
181
memset(&addr, 0, sizeof(addr));
182
addr.inet.family = PR_AF_INET;
183
addr.inet.ip = PR_htonl(PR_INADDR_ANY);
184
addr.inet.port = PR_htons(0);
185
if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
186
fprintf(stderr, "Can't bind socket\n");
190
if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
191
fprintf(stderr, "PR_GetSockName failed\n");
195
listenPort1 = PR_ntohs(addr.inet.port);
196
optval.option = PR_SockOpt_Nonblocking;
197
optval.value.non_blocking = PR_TRUE;
198
PR_SetSocketOption(listenSock1, &optval);
199
if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
200
fprintf(stderr, "Can't listen on a socket\n");
205
if ((listenSock2 = PR_NewTCPSocket()) == NULL) {
206
fprintf(stderr, "Can't create a new TCP socket\n");
210
addr.inet.family = PR_AF_INET;
211
addr.inet.ip = PR_htonl(PR_INADDR_ANY);
212
addr.inet.port = PR_htons(0);
213
if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
214
fprintf(stderr, "Can't bind socket\n");
218
if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
219
fprintf(stderr, "PR_GetSockName failed\n");
223
listenPort2 = PR_ntohs(addr.inet.port);
224
PR_SetSocketOption(listenSock2, &optval);
225
if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
226
fprintf(stderr, "Can't listen on a socket\n");
230
PR_snprintf(buf, sizeof(buf),
231
"The server thread is listening on ports %hu and %hu\n\n",
232
listenPort1, listenPort2);
233
if (debug_mode) printf("%s", buf);
235
/* Set up the poll descriptor array */
238
memset(pds, 0, sizeof(pds));
239
pds[0].fd = listenSock1;
240
pds[0].in_flags = PR_POLL_READ;
241
pds[1].fd = listenSock2;
242
pds[1].in_flags = PR_POLL_READ;
243
/* Add some unused entries to test if they are ignored by PR_Poll() */
244
memset(&pds[2], 0, sizeof(pds[2]));
245
memset(&pds[3], 0, sizeof(pds[3]));
246
memset(&pds[4], 0, sizeof(pds[4]));
249
clientThread = PR_CreateThread(PR_USER_THREAD,
250
clientThreadFunc, (void *) listenPort1,
251
PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
252
PR_UNJOINABLE_THREAD, 0);
253
if (clientThread == NULL) {
254
fprintf(stderr, "can't create thread\n");
259
clientThread = PR_CreateThread(PR_USER_THREAD,
260
clientThreadFunc, (void *) listenPort2,
261
PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
262
PR_UNJOINABLE_THREAD, 0);
263
if (clientThread == NULL) {
264
fprintf(stderr, "can't create thread\n");
270
printf("Two client threads are created. Each of them will\n");
271
printf("send data to one of the two ports the server is listening on.\n");
272
printf("The data they send is the port number. Each of them send\n");
273
printf("the data five times, so you should see ten lines below,\n");
274
printf("interleaved in an arbitrary order.\n");
277
/* two clients, three events per iteration: accept, read, close */
279
while (i < 2 * 3 * NUM_ITERATIONS) {
284
retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
285
PR_ASSERT(retVal != 0); /* no timeout */
287
fprintf(stderr, "PR_Poll failed\n");
293
/* the two listening sockets */
294
for (j = 0; j < 2; j++) {
295
other_pds[j] = pds[j];
296
PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
297
&& (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
298
if (pds[j].out_flags & PR_POLL_READ) {
302
sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
304
fprintf(stderr, "PR_Accept() failed\n");
308
other_pds[nextIndex].fd = sock;
309
other_pds[nextIndex].in_flags = PR_POLL_READ;
311
} else if (pds[j].out_flags & PR_POLL_ERR) {
312
fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
315
} else if (pds[j].out_flags & PR_POLL_NVAL) {
316
fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
317
PR_FileDesc2NativeHandle(pds[j].fd));
323
for (j = 2; j < npds; j++) {
324
if (NULL == pds[j].fd) {
326
* Keep the unused entries in the poll descriptor array
327
* for testing purposes.
329
other_pds[nextIndex] = pds[j];
334
PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
335
&& (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
336
if (pds[j].out_flags & PR_POLL_READ) {
341
nAvail = PR_Available(pds[j].fd);
342
nRead = PR_Read(pds[j].fd, buf, sizeof(buf));
343
PR_ASSERT(nAvail == nRead);
345
fprintf(stderr, "PR_Read() failed\n");
348
} else if (nRead == 0) {
352
/* Just to be safe */
354
if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
356
} else if (pds[j].out_flags & PR_POLL_ERR) {
357
fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
360
} else if (pds[j].out_flags & PR_POLL_NVAL) {
361
fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
365
other_pds[nextIndex] = pds[j];
369
PR_ASSERT(retVal == nEvents);
378
if (debug_mode) printf("Tests passed\n");
383
PR_Close(listenSock1);
386
PR_Close(listenSock2);