~ubuntu-branches/ubuntu/trusty/virtualbox-lts-xenial/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libs/xpcom18a4/nsprpub/pr/tests/peek.c

  • Committer: Package Import Robot
  • Author(s): Gianfranco Costamagna
  • Date: 2016-02-23 14:28:26 UTC
  • Revision ID: package-import@ubuntu.com-20160223142826-bdu69el2z6wa2a44
Tags: upstream-4.3.36-dfsg
ImportĀ upstreamĀ versionĀ 4.3.36-dfsg

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
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
16
 *
 
17
 * The Initial Developer of the Original Code is
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 1999-2000
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
/*
 
39
 * A test case for the PR_MSG_PEEK flag of PR_Recv().
 
40
 *
 
41
 * Test both blocking and non-blocking sockets.
 
42
 */
 
43
 
 
44
#include "nspr.h"
 
45
 
 
46
#include <stdio.h>
 
47
#include <stdlib.h>
 
48
#include <string.h>
 
49
 
 
50
#define BUFFER_SIZE 1024
 
51
 
 
52
static int iterations = 10;
 
53
 
 
54
/*
 
55
 * In iteration i, recv_amount[i] is the number of bytes we
 
56
 * wish to receive, and send_amount[i] is the number of bytes
 
57
 * we actually send.  Therefore, the number of elements in the
 
58
 * recv_amount or send_amount array should equal to 'iterations'.
 
59
 * For this test to pass we need to ensure that
 
60
 *     recv_amount[i] <= BUFFER_SIZE,
 
61
 *     send_amount[i] <= BUFFER_SIZE,
 
62
 *     send_amount[i] <= recv_amount[i].
 
63
 */
 
64
static PRInt32 recv_amount[10] = {
 
65
    16, 128, 256, 1024, 512, 512, 128, 256, 32, 32};
 
66
static PRInt32 send_amount[10] = {
 
67
    16,  64, 128, 1024, 512, 256, 128,  64, 16, 32};
 
68
 
 
69
/* Blocking I/O */
 
70
static void ServerB(void *arg)
 
71
{
 
72
    PRFileDesc *listenSock = (PRFileDesc *) arg;
 
73
    PRFileDesc *sock;
 
74
    char buf[BUFFER_SIZE];
 
75
    PRInt32 nbytes;
 
76
    int i;
 
77
    int j;
 
78
 
 
79
    sock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
 
80
    if (NULL == sock) {
 
81
        fprintf(stderr, "PR_Accept failed\n");
 
82
        exit(1);
 
83
    }
 
84
 
 
85
    for (i = 0; i < iterations; i++) {
 
86
        memset(buf, 0, sizeof(buf));
 
87
        nbytes = PR_Recv(sock, buf, recv_amount[i],
 
88
                PR_MSG_PEEK, PR_INTERVAL_NO_TIMEOUT);
 
89
        if (-1 == nbytes) {
 
90
            fprintf(stderr, "PR_Recv failed\n");
 
91
            exit(1);
 
92
        }
 
93
        if (send_amount[i] != nbytes) {
 
94
            fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
 
95
            exit(1);
 
96
        }
 
97
        for (j = 0; j < nbytes; j++) {
 
98
            if (buf[j] != 2*i) {
 
99
                fprintf(stderr, "byte %d should be %d but is %d\n",
 
100
                        j, 2*i, buf[j]);
 
101
                exit(1);
 
102
            }
 
103
        }
 
104
        fprintf(stderr, "server: peeked expected data\n");
 
105
 
 
106
        memset(buf, 0, sizeof(buf));
 
107
        nbytes = PR_Recv(sock, buf, recv_amount[i],
 
108
                PR_MSG_PEEK, PR_INTERVAL_NO_TIMEOUT);
 
109
        if (-1 == nbytes) {
 
110
            fprintf(stderr, "PR_Recv failed\n");
 
111
            exit(1);
 
112
        }
 
113
        if (send_amount[i] != nbytes) {
 
114
            fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
 
115
            exit(1);
 
116
        }
 
117
        for (j = 0; j < nbytes; j++) {
 
118
            if (buf[j] != 2*i) {
 
119
                fprintf(stderr, "byte %d should be %d but is %d\n",
 
120
                        j, 2*i, buf[j]);
 
121
                exit(1);
 
122
            }
 
123
        }
 
124
        fprintf(stderr, "server: peeked expected data\n");
 
125
 
 
126
        memset(buf, 0, sizeof(buf));
 
127
        nbytes = PR_Recv(sock, buf, recv_amount[i],
 
128
                0, PR_INTERVAL_NO_TIMEOUT);
 
129
        if (-1 == nbytes) {
 
130
            fprintf(stderr, "PR_Recv failed\n");
 
131
            exit(1);
 
132
        }
 
133
        if (send_amount[i] != nbytes) {
 
134
            fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
 
135
            exit(1);
 
136
        }
 
137
        for (j = 0; j < nbytes; j++) {
 
138
            if (buf[j] != 2*i) {
 
139
                fprintf(stderr, "byte %d should be %d but is %d\n",
 
140
                        j, 2*i, buf[j]);
 
141
                exit(1);
 
142
            }
 
143
        }
 
144
        fprintf(stderr, "server: received expected data\n");
 
145
 
 
146
        PR_Sleep(PR_SecondsToInterval(1));
 
147
        memset(buf, 2*i+1, send_amount[i]);
 
148
        nbytes = PR_Send(sock, buf, send_amount[i],
 
149
                0, PR_INTERVAL_NO_TIMEOUT);
 
150
        if (-1 == nbytes) {
 
151
            fprintf(stderr, "PR_Send failed\n");
 
152
            exit(1);
 
153
        }
 
154
        if (send_amount[i] != nbytes) {
 
155
            fprintf(stderr, "PR_Send returned %d, absurd!\n", nbytes);
 
156
            exit(1);
 
157
        }
 
158
    }
 
159
    if (PR_Close(sock) == PR_FAILURE) {
 
160
        fprintf(stderr, "PR_Close failed\n");
 
161
        exit(1);
 
162
    }
 
163
}
 
164
 
 
165
/* Non-blocking I/O */
 
166
static void ClientNB(void *arg)
 
167
{
 
168
    PRFileDesc *sock;
 
169
    PRSocketOptionData opt;
 
170
    PRUint16 port = (PRUint16) arg;
 
171
    PRNetAddr addr;
 
172
    char buf[BUFFER_SIZE];
 
173
    PRPollDesc pd;
 
174
    PRInt32 npds;
 
175
    PRInt32 nbytes;
 
176
    int i;
 
177
    int j;
 
178
 
 
179
    sock = PR_OpenTCPSocket(PR_AF_INET6);
 
180
    if (NULL == sock) {
 
181
        fprintf(stderr, "PR_OpenTCPSocket failed\n");
 
182
        exit(1);
 
183
    }
 
184
    opt.option = PR_SockOpt_Nonblocking;
 
185
    opt.value.non_blocking = PR_TRUE;
 
186
    if (PR_SetSocketOption(sock, &opt) == PR_FAILURE) {
 
187
        fprintf(stderr, "PR_SetSocketOption failed\n");
 
188
        exit(1);
 
189
    }
 
190
    memset(&addr, 0, sizeof(addr));
 
191
    if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, port, &addr)
 
192
            == PR_FAILURE) {
 
193
        fprintf(stderr, "PR_SetNetAddr failed\n");
 
194
        exit(1);
 
195
    }
 
196
    if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
 
197
        if (PR_GetError() != PR_IN_PROGRESS_ERROR) {
 
198
            fprintf(stderr, "PR_Connect failed\n");
 
199
            exit(1);
 
200
        }
 
201
        pd.fd = sock;
 
202
        pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;
 
203
        npds = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
 
204
        if (-1 == npds) {
 
205
            fprintf(stderr, "PR_Poll failed\n");
 
206
            exit(1);
 
207
        }
 
208
        if (1 != npds) {
 
209
            fprintf(stderr, "PR_Poll returned %d, absurd!\n", npds);
 
210
            exit(1);
 
211
        }
 
212
        if (PR_GetConnectStatus(&pd) == PR_FAILURE) {
 
213
            fprintf(stderr, "PR_GetConnectStatus failed\n");
 
214
            exit(1);
 
215
        }
 
216
    }
 
217
 
 
218
    for (i = 0; i < iterations; i++) {
 
219
        PR_Sleep(PR_SecondsToInterval(1));
 
220
        memset(buf, 2*i, send_amount[i]);
 
221
        while ((nbytes = PR_Send(sock, buf, send_amount[i],
 
222
                0, PR_INTERVAL_NO_TIMEOUT)) == -1) {
 
223
            if (PR_GetError() != PR_WOULD_BLOCK_ERROR) {
 
224
                fprintf(stderr, "PR_Send failed\n");
 
225
                exit(1);
 
226
            }
 
227
            pd.fd = sock;
 
228
            pd.in_flags = PR_POLL_WRITE;
 
229
            npds = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
 
230
            if (-1 == npds) {
 
231
                fprintf(stderr, "PR_Poll failed\n");
 
232
                exit(1);
 
233
            }
 
234
            if (1 != npds) {
 
235
                fprintf(stderr, "PR_Poll returned %d, absurd!\n", npds);
 
236
                exit(1);
 
237
            }
 
238
        }
 
239
        if (send_amount[i] != nbytes) {
 
240
            fprintf(stderr, "PR_Send returned %d, absurd!\n", nbytes);
 
241
            exit(1);
 
242
        }
 
243
 
 
244
        memset(buf, 0, sizeof(buf));
 
245
        while ((nbytes = PR_Recv(sock, buf, recv_amount[i],
 
246
                PR_MSG_PEEK, PR_INTERVAL_NO_TIMEOUT)) == -1) {
 
247
            if (PR_GetError() != PR_WOULD_BLOCK_ERROR) {
 
248
                fprintf(stderr, "PR_Recv failed\n");
 
249
                exit(1);
 
250
            }
 
251
            pd.fd = sock;
 
252
            pd.in_flags = PR_POLL_READ;
 
253
            npds = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
 
254
            if (-1 == npds) {
 
255
                fprintf(stderr, "PR_Poll failed\n");
 
256
                exit(1);
 
257
            }
 
258
            if (1 != npds) {
 
259
                fprintf(stderr, "PR_Poll returned %d, absurd!\n", npds);
 
260
                exit(1);
 
261
            }
 
262
        }
 
263
        if (send_amount[i] != nbytes) {
 
264
            fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
 
265
            exit(1);
 
266
        }
 
267
        for (j = 0; j < nbytes; j++) {
 
268
            if (buf[j] != 2*i+1) {
 
269
                fprintf(stderr, "byte %d should be %d but is %d\n",
 
270
                        j, 2*i+1, buf[j]);
 
271
                exit(1);
 
272
            }
 
273
        }
 
274
        fprintf(stderr, "client: peeked expected data\n");
 
275
 
 
276
        memset(buf, 0, sizeof(buf));
 
277
        nbytes = PR_Recv(sock, buf, recv_amount[i],
 
278
                PR_MSG_PEEK, PR_INTERVAL_NO_TIMEOUT);
 
279
        if (-1 == nbytes) {
 
280
            fprintf(stderr, "PR_Recv failed\n");
 
281
            exit(1);
 
282
        }
 
283
        if (send_amount[i] != nbytes) {
 
284
            fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
 
285
            exit(1);
 
286
        }
 
287
        for (j = 0; j < nbytes; j++) {
 
288
            if (buf[j] != 2*i+1) {
 
289
                fprintf(stderr, "byte %d should be %d but is %d\n",
 
290
                        j, 2*i+1, buf[j]);
 
291
                exit(1);
 
292
            }
 
293
        }
 
294
        fprintf(stderr, "client: peeked expected data\n");
 
295
 
 
296
        memset(buf, 0, sizeof(buf));
 
297
        nbytes = PR_Recv(sock, buf, recv_amount[i],
 
298
                0, PR_INTERVAL_NO_TIMEOUT);
 
299
        if (-1 == nbytes) {
 
300
            fprintf(stderr, "PR_Recv failed\n");
 
301
            exit(1);
 
302
        }
 
303
        if (send_amount[i] != nbytes) {
 
304
            fprintf(stderr, "PR_Recv returned %d, absurd!\n", nbytes);
 
305
            exit(1);
 
306
        }
 
307
        for (j = 0; j < nbytes; j++) {
 
308
            if (buf[j] != 2*i+1) {
 
309
                fprintf(stderr, "byte %d should be %d but is %d\n",
 
310
                        j, 2*i+1, buf[j]);
 
311
                exit(1);
 
312
            }
 
313
        }
 
314
        fprintf(stderr, "client: received expected data\n");
 
315
    }
 
316
    if (PR_Close(sock) == PR_FAILURE) {
 
317
        fprintf(stderr, "PR_Close failed\n");
 
318
        exit(1);
 
319
    }
 
320
}
 
321
 
 
322
static void
 
323
RunTest(PRThreadScope scope, PRFileDesc *listenSock, PRUint16 port)
 
324
{
 
325
    PRThread *server, *client;
 
326
 
 
327
    server = PR_CreateThread(PR_USER_THREAD, ServerB, listenSock,
 
328
            PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
 
329
    if (NULL == server) {
 
330
        fprintf(stderr, "PR_CreateThread failed\n");
 
331
        exit(1);
 
332
    }
 
333
    client = PR_CreateThread(
 
334
            PR_USER_THREAD, ClientNB, (void *) port,
 
335
            PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
 
336
    if (NULL == client) {
 
337
        fprintf(stderr, "PR_CreateThread failed\n");
 
338
        exit(1);
 
339
    }
 
340
 
 
341
    if (PR_JoinThread(server) == PR_FAILURE) {
 
342
        fprintf(stderr, "PR_JoinThread failed\n");
 
343
        exit(1);
 
344
    }
 
345
    if (PR_JoinThread(client) == PR_FAILURE) {
 
346
        fprintf(stderr, "PR_JoinThread failed\n");
 
347
        exit(1);
 
348
    }
 
349
}
 
350
 
 
351
int main(int argc, char **argv)
 
352
{
 
353
    PRFileDesc *listenSock;
 
354
    PRNetAddr addr;
 
355
    PRUint16 port;
 
356
 
 
357
    listenSock = PR_OpenTCPSocket(PR_AF_INET6);
 
358
    if (NULL == listenSock) {
 
359
        fprintf(stderr, "PR_OpenTCPSocket failed\n");
 
360
        exit(1);
 
361
    }
 
362
    memset(&addr, 0, sizeof(addr));
 
363
    if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
 
364
        fprintf(stderr, "PR_SetNetAddr failed\n");
 
365
        exit(1);
 
366
    }
 
367
    if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
 
368
        fprintf(stderr, "PR_Bind failed\n");
 
369
        exit(1);
 
370
    }
 
371
    if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
 
372
        fprintf(stderr, "PR_GetSockName failed\n");
 
373
        exit(1);
 
374
    }
 
375
    port = PR_ntohs(addr.ipv6.port);
 
376
    if (PR_Listen(listenSock, 5) == PR_FAILURE) {
 
377
        fprintf(stderr, "PR_Listen failed\n");
 
378
        exit(1);
 
379
    }
 
380
 
 
381
    fprintf(stderr, "Running the test with local threads\n");
 
382
    RunTest(PR_LOCAL_THREAD, listenSock, port);
 
383
    fprintf(stderr, "Running the test with global threads\n");
 
384
    RunTest(PR_GLOBAL_THREAD, listenSock, port);
 
385
 
 
386
    if (PR_Close(listenSock) == PR_FAILURE) {
 
387
        fprintf(stderr, "PR_Close failed\n");
 
388
        exit(1);
 
389
    }
 
390
    printf("PASS\n");
 
391
    return 0;
 
392
}