VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/zerolen.c@ 32258

Last change on this file since 32258 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
Line 
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 * Test: zerolen.c
40 *
41 * Description: a test for Bugzilla bug #17699. We perform
42 * the same test for PR_Writev, PR_Write, and PR_Send. In
43 * each test the server thread first fills up the connection
44 * to the client so that the next write operation will fail
45 * with EAGAIN. Then it calls PR_Writev, PR_Write, or PR_Send
46 * with a zero-length buffer. The client thread initially
47 * does not read so that the connection can be filled up.
48 * Then it empties the connection so that the server thread's
49 * PR_Writev, PR_Write, or PR_Send call can succeed.
50 *
51 * Bug #17699 is specific to the pthreads version on Unix,
52 * so on other platforms this test does nothing.
53 */
54
55#ifndef XP_UNIX
56
57#include <stdio.h>
58
59int main()
60{
61 printf("PASS\n");
62 return 0;
63}
64
65#else /* XP_UNIX */
66
67#include "nspr.h"
68#include "private/pprio.h"
69
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#include <errno.h>
74#include <unistd.h>
75
76static void ClientThread(void *arg)
77{
78 PRFileDesc *sock;
79 PRNetAddr addr;
80 PRUint16 port = (PRUint16) arg;
81 char buf[1024];
82 PRInt32 nbytes;
83
84 sock = PR_NewTCPSocket();
85 if (NULL == sock) {
86 fprintf(stderr, "PR_NewTCPSocket failed\n");
87 exit(1);
88 }
89 if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) {
90 fprintf(stderr, "PR_InitializeNetAddr failed\n");
91 exit(1);
92 }
93 if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
94 fprintf(stderr, "PR_Connect failed\n");
95 exit(1);
96 }
97 /*
98 * Sleep 5 seconds to force the server thread to get EAGAIN.
99 */
100 if (PR_Sleep(PR_SecondsToInterval(5)) == PR_FAILURE) {
101 fprintf(stderr, "PR_Sleep failed\n");
102 exit(1);
103 }
104 /*
105 * Then start reading.
106 */
107 while ((nbytes = PR_Read(sock, buf, sizeof(buf))) > 0) {
108 /* empty loop body */
109 }
110 if (-1 == nbytes) {
111 fprintf(stderr, "PR_Read failed\n");
112 exit(1);
113 }
114 if (PR_Close(sock) == PR_FAILURE) {
115 fprintf(stderr, "PR_Close failed\n");
116 exit(1);
117 }
118}
119
120int main()
121{
122 PRFileDesc *listenSock;
123 PRFileDesc *acceptSock;
124 int osfd;
125 PRThread *clientThread;
126 PRNetAddr addr;
127 char buf[1024];
128 PRInt32 nbytes;
129 PRIOVec iov;
130
131 memset(buf, 0, sizeof(buf)); /* Initialize the buffer. */
132 listenSock = PR_NewTCPSocket();
133 if (NULL == listenSock) {
134 fprintf(stderr, "PR_NewTCPSocket failed\n");
135 exit(1);
136 }
137 if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
138 fprintf(stderr, "PR_InitializeNetAddr failed\n");
139 exit(1);
140 }
141 if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
142 fprintf(stderr, "PR_Bind failed\n");
143 exit(1);
144 }
145 /* Find out what port number we are bound to. */
146 if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
147 fprintf(stderr, "PR_GetSockName failed\n");
148 exit(1);
149 }
150 if (PR_Listen(listenSock, 5) == PR_FAILURE) {
151 fprintf(stderr, "PR_Listen failed\n");
152 exit(1);
153 }
154
155 /*
156 * First test PR_Writev.
157 */
158 clientThread = PR_CreateThread(PR_USER_THREAD,
159 ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
160 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
161 if (NULL == clientThread) {
162 fprintf(stderr, "PR_CreateThread failed\n");
163 exit(1);
164 }
165 acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
166 if (NULL == acceptSock) {
167 fprintf(stderr, "PR_Accept failed\n");
168 exit(1);
169 }
170 osfd = PR_FileDesc2NativeHandle(acceptSock);
171 while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) {
172 /* empty loop body */
173 }
174 if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
175 fprintf(stderr, "write failed\n");
176 exit(1);
177 }
178 iov.iov_base = buf;
179 iov.iov_len = 0;
180 printf("calling PR_Writev with a zero-length buffer\n");
181 fflush(stdout);
182 nbytes = PR_Writev(acceptSock, &iov, 1, PR_INTERVAL_NO_TIMEOUT);
183 if (nbytes != 0) {
184 fprintf(stderr, "PR_Writev should return 0 but returns %d\n", nbytes);
185 exit(1);
186 }
187 if (PR_Close(acceptSock) == PR_FAILURE) {
188 fprintf(stderr, "PR_Close failed\n");
189 exit(1);
190 }
191 if (PR_JoinThread(clientThread) == PR_FAILURE) {
192 fprintf(stderr, "PR_JoinThread failed\n");
193 exit(1);
194 }
195
196 /*
197 * Then test PR_Write.
198 */
199 clientThread = PR_CreateThread(PR_USER_THREAD,
200 ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
201 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
202 if (NULL == clientThread) {
203 fprintf(stderr, "PR_CreateThread failed\n");
204 exit(1);
205 }
206 acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
207 if (NULL == acceptSock) {
208 fprintf(stderr, "PR_Accept failed\n");
209 exit(1);
210 }
211 osfd = PR_FileDesc2NativeHandle(acceptSock);
212 while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) {
213 /* empty loop body */
214 }
215 if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
216 fprintf(stderr, "write failed\n");
217 exit(1);
218 }
219 printf("calling PR_Write with a zero-length buffer\n");
220 fflush(stdout);
221 nbytes = PR_Write(acceptSock, buf, 0);
222 if (nbytes != 0) {
223 fprintf(stderr, "PR_Write should return 0 but returns %d\n", nbytes);
224 exit(1);
225 }
226 if (PR_Close(acceptSock) == PR_FAILURE) {
227 fprintf(stderr, "PR_Close failed\n");
228 exit(1);
229 }
230 if (PR_JoinThread(clientThread) == PR_FAILURE) {
231 fprintf(stderr, "PR_JoinThread failed\n");
232 exit(1);
233 }
234
235 /*
236 * Finally test PR_Send.
237 */
238 clientThread = PR_CreateThread(PR_USER_THREAD,
239 ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
240 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
241 if (NULL == clientThread) {
242 fprintf(stderr, "PR_CreateThread failed\n");
243 exit(1);
244 }
245 acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
246 if (NULL == acceptSock) {
247 fprintf(stderr, "PR_Accept failed\n");
248 exit(1);
249 }
250 osfd = PR_FileDesc2NativeHandle(acceptSock);
251 while ((nbytes = write(osfd, buf, sizeof(buf))) != -1) {
252 /* empty loop body */
253 }
254 if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
255 fprintf(stderr, "write failed\n");
256 exit(1);
257 }
258 printf("calling PR_Send with a zero-length buffer\n");
259 fflush(stdout);
260 nbytes = PR_Send(acceptSock, buf, 0, 0, PR_INTERVAL_NO_TIMEOUT);
261 if (nbytes != 0) {
262 fprintf(stderr, "PR_Send should return 0 but returns %d\n", nbytes);
263 exit(1);
264 }
265 if (PR_Close(acceptSock) == PR_FAILURE) {
266 fprintf(stderr, "PR_Close failed\n");
267 exit(1);
268 }
269 if (PR_JoinThread(clientThread) == PR_FAILURE) {
270 fprintf(stderr, "PR_JoinThread failed\n");
271 exit(1);
272 }
273
274 if (PR_Close(listenSock) == PR_FAILURE) {
275 fprintf(stderr, "PR_Close failed\n");
276 exit(1);
277 }
278 printf("PASS\n");
279 return 0;
280}
281
282#endif /* XP_UNIX */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette