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

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/prpoll.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
#ifdef WIN32
 
36
#include <windows.h>
 
37
#endif
 
38
 
 
39
#ifdef XP_OS2_VACPP
 
40
#include <io.h>      /* for close() */
 
41
#endif
 
42
 
 
43
#include "prinit.h"
 
44
#include "prio.h"
 
45
#include "prlog.h"
 
46
#include "prprf.h"
 
47
#include "prnetdb.h"
 
48
 
 
49
#ifndef XP_MAC
 
50
#include "private/pprio.h"
 
51
#else
 
52
#include "pprio.h"
 
53
#endif
 
54
 
 
55
#define CLIENT_LOOPS    5
 
56
#define BUF_SIZE                128
 
57
 
 
58
#include <stdio.h>
 
59
#include <string.h>
 
60
#include <stdlib.h>
 
61
 
 
62
static void
 
63
clientThreadFunc(void *arg)
 
64
{
 
65
    PRUint16 port = (PRUint16) arg;
 
66
    PRFileDesc *sock;
 
67
    PRNetAddr addr;
 
68
    char buf[BUF_SIZE];
 
69
    int i;
 
70
 
 
71
    addr.inet.family = PR_AF_INET;
 
72
    addr.inet.port = PR_htons(port);
 
73
    addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
 
74
    PR_snprintf(buf, sizeof(buf), "%hu", port);
 
75
 
 
76
    for (i = 0; i < 5; i++) {
 
77
        sock = PR_NewTCPSocket();
 
78
        PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
 
79
 
 
80
        PR_Write(sock, buf, sizeof(buf));
 
81
        PR_Close(sock);
 
82
    }
 
83
}
 
84
 
 
85
int main(int argc, char **argv)
 
86
{
 
87
    PRFileDesc *listenSock1, *listenSock2;
 
88
    PRFileDesc *badFD;
 
89
    PRUint16 listenPort1, listenPort2;
 
90
    PRNetAddr addr;
 
91
    char buf[BUF_SIZE];
 
92
    PRThread *clientThread;
 
93
    PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
 
94
    PRIntn npds;
 
95
    PRInt32 retVal;
 
96
    PRInt32 sd, rv;
 
97
        struct sockaddr_in saddr;
 
98
    PRIntn saddr_len;
 
99
    PRUint16 listenPort3;
 
100
    PRFileDesc *socket_poll_fd;
 
101
    PRIntn i, j;
 
102
 
 
103
    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
 
104
    PR_STDIO_INIT();
 
105
 
 
106
    printf("This program tests PR_Poll with sockets.\n");
 
107
    printf("Timeout, error reporting, and normal operation are tested.\n\n");
 
108
 
 
109
    /* Create two listening sockets */
 
110
    if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
 
111
        fprintf(stderr, "Can't create a new TCP socket\n");
 
112
        exit(1);
 
113
    }
 
114
    addr.inet.family = PR_AF_INET;
 
115
    addr.inet.ip = PR_htonl(PR_INADDR_ANY);
 
116
    addr.inet.port = PR_htons(0);
 
117
    if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
 
118
        fprintf(stderr, "Can't bind socket\n");
 
119
        exit(1);
 
120
    }
 
121
    if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
 
122
        fprintf(stderr, "PR_GetSockName failed\n");
 
123
        exit(1);
 
124
    }
 
125
    listenPort1 = PR_ntohs(addr.inet.port);
 
126
    if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
 
127
        fprintf(stderr, "Can't listen on a socket\n");
 
128
        exit(1);
 
129
    }
 
130
 
 
131
    if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
 
132
        fprintf(stderr, "Can't create a new TCP socket\n");
 
133
        exit(1);
 
134
    }
 
135
    addr.inet.family = PR_AF_INET;
 
136
    addr.inet.ip = PR_htonl(PR_INADDR_ANY);
 
137
    addr.inet.port = PR_htons(0);
 
138
    if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
 
139
        fprintf(stderr, "Can't bind socket\n");
 
140
        exit(1);
 
141
    }
 
142
    if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
 
143
        fprintf(stderr, "PR_GetSockName failed\n");
 
144
        exit(1);
 
145
    }
 
146
    listenPort2 = PR_ntohs(addr.inet.port);
 
147
    if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
 
148
        fprintf(stderr, "Can't listen on a socket\n");
 
149
        exit(1);
 
150
    }
 
151
    /* Set up the poll descriptor array */
 
152
    pds = pds0;
 
153
    other_pds = pds1;
 
154
    memset(pds, 0, sizeof(pds));
 
155
        npds = 0;
 
156
    pds[npds].fd = listenSock1;
 
157
    pds[npds].in_flags = PR_POLL_READ;
 
158
        npds++;
 
159
    pds[npds].fd = listenSock2;
 
160
    pds[npds].in_flags = PR_POLL_READ;
 
161
        npds++;
 
162
 
 
163
        sd = socket(AF_INET, SOCK_STREAM, 0);
 
164
        PR_ASSERT(sd >= 0);
 
165
        memset((char *) &saddr, 0, sizeof(saddr));
 
166
        saddr.sin_family = AF_INET;
 
167
        saddr.sin_addr.s_addr = htonl(INADDR_ANY);
 
168
        saddr.sin_port = htons(0);
 
169
 
 
170
        rv = bind(sd, (struct sockaddr *)&saddr, sizeof(saddr));
 
171
        PR_ASSERT(rv == 0);
 
172
        saddr_len = sizeof(saddr);
 
173
        rv = getsockname(sd, (struct sockaddr *) &saddr, &saddr_len);
 
174
        PR_ASSERT(rv == 0);
 
175
    listenPort3 = ntohs(saddr.sin_port);
 
176
 
 
177
        rv = listen(sd, 5);
 
178
        PR_ASSERT(rv == 0);
 
179
    pds[npds].fd = socket_poll_fd = PR_CreateSocketPollFd(sd);
 
180
        PR_ASSERT(pds[npds].fd);
 
181
    pds[npds].in_flags = PR_POLL_READ;
 
182
    npds++;
 
183
    PR_snprintf(buf, sizeof(buf),
 
184
            "The server thread is listening on ports %hu, %hu and %hu\n\n",
 
185
            listenPort1, listenPort2, listenPort3);
 
186
    printf("%s", buf);
 
187
 
 
188
    /* Testing timeout */
 
189
    printf("PR_Poll should time out in 5 seconds\n");
 
190
    retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
 
191
    if (retVal != 0) {
 
192
        PR_snprintf(buf, sizeof(buf),
 
193
                "PR_Poll should time out and return 0, but it returns %ld\n",
 
194
                retVal);
 
195
        fprintf(stderr, "%s", buf);
 
196
        exit(1);
 
197
    }
 
198
    printf("PR_Poll timed out.  Test passed.\n\n");
 
199
 
 
200
    /* Testing bad fd */
 
201
    printf("PR_Poll should detect a bad file descriptor\n");
 
202
    if ((badFD = PR_NewTCPSocket()) == NULL) {
 
203
        fprintf(stderr, "Can't create a TCP socket\n");
 
204
        exit(1);
 
205
    }
 
206
 
 
207
    pds[npds].fd = badFD;
 
208
    pds[npds].in_flags = PR_POLL_READ;
 
209
    npds++;
 
210
    PR_Close(badFD);  /* make the fd bad */
 
211
#if 0
 
212
    retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
 
213
    if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) {
 
214
        fprintf(stderr, "Failed to detect the bad fd: "
 
215
                "PR_Poll returns %d, out_flags is 0x%hx\n",
 
216
                retVal, pds[npds - 1].out_flags);
 
217
        exit(1);
 
218
    }
 
219
    printf("PR_Poll detected the bad fd.  Test passed.\n\n");
 
220
#endif
 
221
    npds--;
 
222
 
 
223
    clientThread = PR_CreateThread(PR_USER_THREAD,
 
224
            clientThreadFunc, (void *) listenPort1,
 
225
            PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
 
226
            PR_UNJOINABLE_THREAD, 0);
 
227
    if (clientThread == NULL) {
 
228
        fprintf(stderr, "can't create thread\n");
 
229
        exit(1);
 
230
    }
 
231
 
 
232
    clientThread = PR_CreateThread(PR_USER_THREAD,
 
233
            clientThreadFunc, (void *) listenPort2,
 
234
            PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
 
235
            PR_UNJOINABLE_THREAD, 0);
 
236
    if (clientThread == NULL) {
 
237
        fprintf(stderr, "can't create thread\n");
 
238
        exit(1);
 
239
    }
 
240
 
 
241
    clientThread = PR_CreateThread(PR_USER_THREAD,
 
242
            clientThreadFunc, (void *) listenPort3,
 
243
            PR_PRIORITY_NORMAL, PR_GLOBAL_BOUND_THREAD,
 
244
            PR_UNJOINABLE_THREAD, 0);
 
245
    if (clientThread == NULL) {
 
246
        fprintf(stderr, "can't create thread\n");
 
247
        exit(1);
 
248
    }
 
249
 
 
250
 
 
251
    printf("Three client threads are created.  Each of them will\n");
 
252
    printf("send data to one of the three ports the server is listening on.\n");
 
253
    printf("The data they send is the port number.  Each of them send\n");
 
254
    printf("the data five times, so you should see ten lines below,\n");
 
255
    printf("interleaved in an arbitrary order.\n");
 
256
 
 
257
    /* 30 events total */
 
258
    i = 0;
 
259
    while (i < 30) {
 
260
                PRPollDesc *tmp;
 
261
                int nextIndex;
 
262
                int nEvents = 0;
 
263
 
 
264
                retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
 
265
                PR_ASSERT(retVal != 0);  /* no timeout */
 
266
                if (retVal == -1) {
 
267
                        fprintf(stderr, "PR_Poll failed\n");
 
268
                        exit(1);
 
269
                }
 
270
 
 
271
                nextIndex = 3;
 
272
                /* the three listening sockets */
 
273
                for (j = 0; j < 3; j++) {
 
274
                        other_pds[j] = pds[j];
 
275
                        PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
 
276
                                && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
 
277
                        if (pds[j].out_flags & PR_POLL_READ) {
 
278
                                PRFileDesc *sock;
 
279
 
 
280
                                nEvents++;
 
281
                                if (j == 2) {
 
282
                                        int newsd;
 
283
                                        newsd = accept(PR_FileDesc2NativeHandle(pds[j].fd), NULL, 0);
 
284
                                        if (newsd == -1) {
 
285
                                                fprintf(stderr, "accept() failed\n");
 
286
                                                exit(1);
 
287
                                        }
 
288
                                        other_pds[nextIndex].fd  = PR_CreateSocketPollFd(newsd);
 
289
                                        PR_ASSERT(other_pds[nextIndex].fd);
 
290
                                        other_pds[nextIndex].in_flags = PR_POLL_READ;
 
291
                                } else {
 
292
                                        sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
 
293
                                        if (sock == NULL) {
 
294
                                                fprintf(stderr, "PR_Accept() failed\n");
 
295
                                                exit(1);
 
296
                                        }
 
297
                                        other_pds[nextIndex].fd = sock;
 
298
                                        other_pds[nextIndex].in_flags = PR_POLL_READ;
 
299
                                }
 
300
                                nextIndex++;
 
301
                        } else if (pds[j].out_flags & PR_POLL_ERR) {
 
302
                                fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
 
303
                                exit(1);
 
304
                        } else if (pds[j].out_flags & PR_POLL_NVAL) {
 
305
                                fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
 
306
                                        PR_FileDesc2NativeHandle(pds[j].fd));
 
307
                                exit(1);
 
308
                        }
 
309
                }
 
310
 
 
311
                for (j = 3; j < npds; j++) {
 
312
                        PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
 
313
                                && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
 
314
                        if (pds[j].out_flags & PR_POLL_READ) {
 
315
                                PRInt32 nBytes;
 
316
 
 
317
                                nEvents++;
 
318
                                /* XXX: This call is a hack and should be fixed */
 
319
                                if (PR_GetDescType(pds[j].fd) == (PRDescType) 0) {
 
320
                                        nBytes = recv(PR_FileDesc2NativeHandle(pds[j].fd), buf,
 
321
                                                                                sizeof(buf), 0);
 
322
                                        if (nBytes == -1) {
 
323
                                                fprintf(stderr, "recv() failed\n");
 
324
                                                exit(1);
 
325
                                        }
 
326
                                        printf("Server read %d bytes from native fd %d\n",nBytes,
 
327
                                                                                PR_FileDesc2NativeHandle(pds[j].fd));
 
328
#ifdef WIN32
 
329
                                        closesocket((SOCKET)PR_FileDesc2NativeHandle(pds[j].fd));
 
330
#else
 
331
                                        close(PR_FileDesc2NativeHandle(pds[j].fd));
 
332
#endif
 
333
                                        PR_DestroySocketPollFd(pds[j].fd);
 
334
                                } else {
 
335
                                        nBytes = PR_Read(pds[j].fd, buf, sizeof(buf));
 
336
                                        if (nBytes == -1) {
 
337
                                                fprintf(stderr, "PR_Read() failed\n");
 
338
                                                exit(1);
 
339
                                        }
 
340
                                        PR_Close(pds[j].fd);
 
341
                                }
 
342
                                /* Just to be safe */
 
343
                                buf[BUF_SIZE - 1] = '\0';
 
344
                                printf("The server received \"%s\" from a client\n", buf);
 
345
                        } else if (pds[j].out_flags & PR_POLL_ERR) {
 
346
                                fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
 
347
                                exit(1);
 
348
                        } else if (pds[j].out_flags & PR_POLL_NVAL) {
 
349
                                fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
 
350
                                exit(1);
 
351
                        } else {
 
352
                                other_pds[nextIndex] = pds[j];
 
353
                                nextIndex++;
 
354
                        }
 
355
                }
 
356
 
 
357
                PR_ASSERT(retVal == nEvents);
 
358
                /* swap */
 
359
                tmp = pds;
 
360
                pds = other_pds;
 
361
                other_pds = tmp;
 
362
                npds = nextIndex;
 
363
                i += nEvents;
 
364
    }
 
365
    PR_DestroySocketPollFd(socket_poll_fd);
 
366
 
 
367
    printf("All tests finished\n");
 
368
    PR_Cleanup();
 
369
    return 0;
 
370
}