~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/poll_nm.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
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/
 
7
 * 
 
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.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
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
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
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
 
32
 * GPL.
 
33
 */
 
34
 
 
35
/***********************************************************************
 
36
**
 
37
** Name: prpoll_norm.c
 
38
**
 
39
** Description: This program tests PR_Poll with sockets.
 
40
**              Normal operation are tested
 
41
**
 
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
***********************************************************************/
 
51
 
 
52
/***********************************************************************
 
53
** Includes
 
54
***********************************************************************/
 
55
/* Used to get the command line option */
 
56
#include "plgetopt.h"
 
57
 
 
58
#include "prinit.h"
 
59
#include "prio.h"
 
60
#include "prlog.h"
 
61
#include "prprf.h"
 
62
#include "prnetdb.h"
 
63
#ifndef XP_MAC
 
64
#include "obsolete/probslet.h"
 
65
#else
 
66
#include "probslet.h"
 
67
#endif
 
68
 
 
69
#ifndef XP_MAC
 
70
#include "private/pprio.h"
 
71
#else
 
72
#include "pprio.h"
 
73
#endif
 
74
 
 
75
#include <stdio.h>
 
76
#include <string.h>
 
77
#include <stdlib.h>
 
78
PRIntn failed_already=0;
 
79
PRIntn debug_mode;
 
80
 
 
81
#define NUM_ITERATIONS 5
 
82
 
 
83
#ifdef XP_MAC
 
84
int fprintf(FILE *stream, const char *fmt, ...)
 
85
{
 
86
PR_LogPrint(fmt);
 
87
return 0;
 
88
}
 
89
#define printf PR_LogPrint
 
90
extern void SetupMacPrintfLog(char *logFile);
 
91
#endif
 
92
 
 
93
static void PR_CALLBACK
 
94
clientThreadFunc(void *arg)
 
95
{
 
96
    PRUintn port = (PRUintn) arg;
 
97
    PRFileDesc *sock;
 
98
    PRNetAddr addr;
 
99
    char buf[128];
 
100
    int i;
 
101
    PRStatus sts;
 
102
    PRInt32 n;
 
103
 
 
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);
 
109
 
 
110
    for (i = 0; i < NUM_ITERATIONS; i++) {
 
111
        sock = PR_NewTCPSocket();
 
112
        PR_ASSERT(sock != NULL);
 
113
        
 
114
    sts = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
 
115
        PR_ASSERT(sts == PR_SUCCESS);
 
116
 
 
117
        n = PR_Write(sock, buf, sizeof(buf));
 
118
        PR_ASSERT(n >= 0);
 
119
 
 
120
        sts = PR_Close(sock);
 
121
        PR_ASSERT(sts == PR_SUCCESS);
 
122
    }
 
123
}
 
124
 
 
125
int main(int argc, char **argv)
 
126
{
 
127
    PRFileDesc *listenSock1 = NULL, *listenSock2 = NULL;
 
128
    PRUint16 listenPort1, listenPort2;
 
129
    PRNetAddr addr;
 
130
    char buf[128];
 
131
    PRThread *clientThread;
 
132
    PRPollDesc pds0[20], pds1[20], *pds, *other_pds;
 
133
    PRIntn npds;
 
134
    PRInt32 retVal;
 
135
    PRIntn i, j;
 
136
    PRSocketOptionData optval;
 
137
 
 
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)
 
141
        test.
 
142
        Usage: test_name -d
 
143
        */
 
144
        PLOptStatus os;
 
145
        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
 
146
        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
 
147
    {
 
148
                if (PL_OPT_BAD == os) continue;
 
149
        switch (opt->option)
 
150
        {
 
151
        case 'd':  /* debug mode */
 
152
                        debug_mode = 1;
 
153
            break;
 
154
         default:
 
155
            break;
 
156
        }
 
157
    }
 
158
        PL_DestroyOptState(opt);
 
159
 
 
160
 /* main test */
 
161
        
 
162
    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
 
163
    PR_STDIO_INIT();
 
164
 
 
165
#ifdef XP_MAC
 
166
        debug_mode = 1;
 
167
        SetupMacPrintfLog("poll_nm.log");
 
168
#endif
 
169
 
 
170
    if (debug_mode) {
 
171
                printf("This program tests PR_Poll with sockets.\n");
 
172
                printf("Normal operation are tested.\n\n");
 
173
        }
 
174
 
 
175
    /* Create two listening sockets */
 
176
    if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
 
177
        fprintf(stderr, "Can't create a new TCP socket\n");
 
178
        failed_already=1;
 
179
        goto exit_now;
 
180
    }
 
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");
 
187
        failed_already=1;
 
188
        goto exit_now;
 
189
    }
 
190
    if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
 
191
        fprintf(stderr, "PR_GetSockName failed\n");
 
192
        failed_already=1;
 
193
        goto exit_now;
 
194
    }
 
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");
 
201
        failed_already=1;
 
202
        goto exit_now;
 
203
    }
 
204
 
 
205
    if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
 
206
        fprintf(stderr, "Can't create a new TCP socket\n");
 
207
        failed_already=1;       
 
208
        goto exit_now;
 
209
    }
 
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");
 
215
        failed_already=1;       
 
216
        goto exit_now;
 
217
    }
 
218
    if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
 
219
        fprintf(stderr, "PR_GetSockName failed\n");
 
220
        failed_already=1;       
 
221
        goto exit_now;
 
222
    }
 
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");
 
227
        failed_already=1;       
 
228
        goto exit_now;
 
229
    }
 
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);
 
234
 
 
235
    /* Set up the poll descriptor array */
 
236
    pds = pds0;
 
237
    other_pds = pds1;
 
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]));
 
247
    npds = 5;
 
248
 
 
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");
 
255
        failed_already=1;       
 
256
        goto exit_now;
 
257
    }
 
258
 
 
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");
 
265
        failed_already=1;               
 
266
        goto exit_now;
 
267
    }
 
268
 
 
269
    if (debug_mode) {
 
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");
 
275
        }
 
276
 
 
277
    /* two clients, three events per iteration: accept, read, close */
 
278
    i = 0;
 
279
    while (i < 2 * 3 * NUM_ITERATIONS) {
 
280
        PRPollDesc *tmp;
 
281
        int nextIndex;
 
282
        int nEvents = 0;
 
283
 
 
284
        retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
 
285
        PR_ASSERT(retVal != 0);  /* no timeout */
 
286
        if (retVal == -1) {
 
287
            fprintf(stderr, "PR_Poll failed\n");
 
288
                failed_already=1;                       
 
289
            goto exit_now;
 
290
        }
 
291
 
 
292
        nextIndex = 2;
 
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) {
 
299
                PRFileDesc *sock;
 
300
 
 
301
                nEvents++;
 
302
                sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
 
303
                if (sock == NULL) {
 
304
                    fprintf(stderr, "PR_Accept() failed\n");
 
305
                        failed_already=1;       
 
306
                    goto exit_now;
 
307
                }
 
308
                other_pds[nextIndex].fd = sock;
 
309
                other_pds[nextIndex].in_flags = PR_POLL_READ;
 
310
                nextIndex++;
 
311
            } else if (pds[j].out_flags & PR_POLL_ERR) {
 
312
                fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
 
313
                failed_already=1;       
 
314
                goto exit_now;
 
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));
 
318
                failed_already=1;       
 
319
                goto exit_now;
 
320
            }
 
321
        }
 
322
 
 
323
        for (j = 2; j < npds; j++) {
 
324
            if (NULL == pds[j].fd) {
 
325
                /*
 
326
                 * Keep the unused entries in the poll descriptor array
 
327
                 * for testing purposes.
 
328
                 */
 
329
                other_pds[nextIndex] = pds[j];
 
330
                nextIndex++;
 
331
                continue;
 
332
            }
 
333
 
 
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) {
 
337
                PRInt32 nAvail;
 
338
                PRInt32 nRead;
 
339
 
 
340
                nEvents++;
 
341
                nAvail = PR_Available(pds[j].fd);
 
342
                nRead = PR_Read(pds[j].fd, buf, sizeof(buf));
 
343
                PR_ASSERT(nAvail == nRead);
 
344
                if (nRead == -1) {
 
345
                    fprintf(stderr, "PR_Read() failed\n");
 
346
                        failed_already=1;       
 
347
                    goto exit_now;
 
348
                } else if (nRead == 0) {
 
349
                    PR_Close(pds[j].fd);
 
350
                    continue;
 
351
                } else {
 
352
                    /* Just to be safe */
 
353
                    buf[127] = '\0';
 
354
                    if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
 
355
                }
 
356
            } else if (pds[j].out_flags & PR_POLL_ERR) {
 
357
                fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
 
358
                failed_already=1;                       
 
359
                goto exit_now;
 
360
            } else if (pds[j].out_flags & PR_POLL_NVAL) {
 
361
                fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
 
362
                failed_already=1;                       
 
363
                goto exit_now;
 
364
            }
 
365
            other_pds[nextIndex] = pds[j];
 
366
            nextIndex++;
 
367
        }
 
368
 
 
369
        PR_ASSERT(retVal == nEvents);
 
370
        /* swap */
 
371
        tmp = pds;
 
372
        pds = other_pds;
 
373
        other_pds = tmp;
 
374
        npds = nextIndex;
 
375
        i += nEvents;
 
376
    }
 
377
 
 
378
    if (debug_mode) printf("Tests passed\n");
 
379
 
 
380
exit_now:
 
381
 
 
382
    if (listenSock1) {
 
383
        PR_Close(listenSock1);
 
384
    }
 
385
    if (listenSock2) {
 
386
        PR_Close(listenSock2);
 
387
    }
 
388
 
 
389
    PR_Cleanup();
 
390
        
 
391
        if(failed_already)      
 
392
                return 1;
 
393
        else
 
394
                return 0;
 
395
 
 
396
}