Bug 1825055 [wpt PR 39247] - Add support for allowing elements with display:contents...
[gecko.git] / nsprpub / pr / tests / zerolen.c
bloba84bd432285cc0f7d00a8dcabdb8d0db74229f47
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /*
7 * Test: zerolen.c
9 * Description: a test for Bugzilla bug #17699. We perform
10 * the same test for PR_Writev, PR_Write, and PR_Send. In
11 * each test the server thread first fills up the connection
12 * to the client so that the next write operation will fail
13 * with EAGAIN. Then it calls PR_Writev, PR_Write, or PR_Send
14 * with a zero-length buffer. The client thread initially
15 * does not read so that the connection can be filled up.
16 * Then it empties the connection so that the server thread's
17 * PR_Writev, PR_Write, or PR_Send call can succeed.
19 * Bug #17699 is specific to the pthreads version on Unix,
20 * so on other platforms this test does nothing.
23 #ifndef XP_UNIX
25 #include <stdio.h>
27 int main(int argc, char **argv)
29 printf("PASS\n");
30 return 0;
33 #else /* XP_UNIX */
35 #include "nspr.h"
36 #include "private/pprio.h"
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <unistd.h>
44 static void ClientThread(void *arg)
46 PRFileDesc *sock;
47 PRNetAddr addr;
48 PRUint16 port = (PRUint16) arg;
49 char buf[1024];
50 PRInt32 nbytes;
52 sock = PR_NewTCPSocket();
53 if (NULL == sock) {
54 fprintf(stderr, "PR_NewTCPSocket failed\n");
55 exit(1);
57 if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) {
58 fprintf(stderr, "PR_InitializeNetAddr failed\n");
59 exit(1);
61 if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
62 fprintf(stderr, "PR_Connect failed\n");
63 exit(1);
66 * Sleep 5 seconds to force the server thread to get EAGAIN.
68 if (PR_Sleep(PR_SecondsToInterval(5)) == PR_FAILURE) {
69 fprintf(stderr, "PR_Sleep failed\n");
70 exit(1);
73 * Then start reading.
75 while (nbytes = PR_Read(sock, buf, sizeof(buf)) > 0) {
76 /* empty loop body */
78 if (-1 == nbytes) {
79 fprintf(stderr, "PR_Read failed\n");
80 exit(1);
82 if (PR_Close(sock) == PR_FAILURE) {
83 fprintf(stderr, "PR_Close failed\n");
84 exit(1);
88 int main()
90 PRFileDesc *listenSock;
91 PRFileDesc *acceptSock;
92 int osfd;
93 PRThread *clientThread;
94 PRNetAddr addr;
95 char buf[1024];
96 PRInt32 nbytes;
97 PRIOVec iov;
99 memset(buf, 0, sizeof(buf)); /* Initialize the buffer. */
100 listenSock = PR_NewTCPSocket();
101 if (NULL == listenSock) {
102 fprintf(stderr, "PR_NewTCPSocket failed\n");
103 exit(1);
105 if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
106 fprintf(stderr, "PR_InitializeNetAddr failed\n");
107 exit(1);
109 if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
110 fprintf(stderr, "PR_Bind failed\n");
111 exit(1);
113 /* Find out what port number we are bound to. */
114 if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
115 fprintf(stderr, "PR_GetSockName failed\n");
116 exit(1);
118 if (PR_Listen(listenSock, 5) == PR_FAILURE) {
119 fprintf(stderr, "PR_Listen failed\n");
120 exit(1);
124 * First test PR_Writev.
126 clientThread = PR_CreateThread(PR_USER_THREAD,
127 ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
128 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
129 if (NULL == clientThread) {
130 fprintf(stderr, "PR_CreateThread failed\n");
131 exit(1);
133 acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
134 if (NULL == acceptSock) {
135 fprintf(stderr, "PR_Accept failed\n");
136 exit(1);
138 osfd = PR_FileDesc2NativeHandle(acceptSock);
139 while (write(osfd, buf, sizeof(buf)) != -1) {
140 /* empty loop body */
142 if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
143 fprintf(stderr, "write failed\n");
144 exit(1);
146 iov.iov_base = buf;
147 iov.iov_len = 0;
148 printf("calling PR_Writev with a zero-length buffer\n");
149 fflush(stdout);
150 nbytes = PR_Writev(acceptSock, &iov, 1, PR_INTERVAL_NO_TIMEOUT);
151 if (nbytes != 0) {
152 fprintf(stderr, "PR_Writev should return 0 but returns %d\n", nbytes);
153 exit(1);
155 if (PR_Close(acceptSock) == PR_FAILURE) {
156 fprintf(stderr, "PR_Close failed\n");
157 exit(1);
159 if (PR_JoinThread(clientThread) == PR_FAILURE) {
160 fprintf(stderr, "PR_JoinThread failed\n");
161 exit(1);
165 * Then test PR_Write.
167 clientThread = PR_CreateThread(PR_USER_THREAD,
168 ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
169 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
170 if (NULL == clientThread) {
171 fprintf(stderr, "PR_CreateThread failed\n");
172 exit(1);
174 acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
175 if (NULL == acceptSock) {
176 fprintf(stderr, "PR_Accept failed\n");
177 exit(1);
179 osfd = PR_FileDesc2NativeHandle(acceptSock);
180 while (write(osfd, buf, sizeof(buf)) != -1) {
181 /* empty loop body */
183 if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
184 fprintf(stderr, "write failed\n");
185 exit(1);
187 printf("calling PR_Write with a zero-length buffer\n");
188 fflush(stdout);
189 nbytes = PR_Write(acceptSock, buf, 0);
190 if (nbytes != 0) {
191 fprintf(stderr, "PR_Write should return 0 but returns %d\n", nbytes);
192 exit(1);
194 if (PR_Close(acceptSock) == PR_FAILURE) {
195 fprintf(stderr, "PR_Close failed\n");
196 exit(1);
198 if (PR_JoinThread(clientThread) == PR_FAILURE) {
199 fprintf(stderr, "PR_JoinThread failed\n");
200 exit(1);
204 * Finally test PR_Send.
206 clientThread = PR_CreateThread(PR_USER_THREAD,
207 ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
208 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
209 if (NULL == clientThread) {
210 fprintf(stderr, "PR_CreateThread failed\n");
211 exit(1);
213 acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
214 if (NULL == acceptSock) {
215 fprintf(stderr, "PR_Accept failed\n");
216 exit(1);
218 osfd = PR_FileDesc2NativeHandle(acceptSock);
219 while (write(osfd, buf, sizeof(buf)) != -1) {
220 /* empty loop body */
222 if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
223 fprintf(stderr, "write failed\n");
224 exit(1);
226 printf("calling PR_Send with a zero-length buffer\n");
227 fflush(stdout);
228 nbytes = PR_Send(acceptSock, buf, 0, 0, PR_INTERVAL_NO_TIMEOUT);
229 if (nbytes != 0) {
230 fprintf(stderr, "PR_Send should return 0 but returns %d\n", nbytes);
231 exit(1);
233 if (PR_Close(acceptSock) == PR_FAILURE) {
234 fprintf(stderr, "PR_Close failed\n");
235 exit(1);
237 if (PR_JoinThread(clientThread) == PR_FAILURE) {
238 fprintf(stderr, "PR_JoinThread failed\n");
239 exit(1);
242 if (PR_Close(listenSock) == PR_FAILURE) {
243 fprintf(stderr, "PR_Close failed\n");
244 exit(1);
246 printf("PASS\n");
247 return 0;
250 #endif /* XP_UNIX */