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/. */
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.
27 int main(int argc
, char **argv
)
36 #include "private/pprio.h"
44 static void ClientThread(void *arg
)
48 PRUint16 port
= (PRUint16
) arg
;
52 sock
= PR_NewTCPSocket();
54 fprintf(stderr
, "PR_NewTCPSocket failed\n");
57 if (PR_InitializeNetAddr(PR_IpAddrLoopback
, port
, &addr
) == PR_FAILURE
) {
58 fprintf(stderr
, "PR_InitializeNetAddr failed\n");
61 if (PR_Connect(sock
, &addr
, PR_INTERVAL_NO_TIMEOUT
) == PR_FAILURE
) {
62 fprintf(stderr
, "PR_Connect failed\n");
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");
75 while (nbytes
= PR_Read(sock
, buf
, sizeof(buf
)) > 0) {
79 fprintf(stderr
, "PR_Read failed\n");
82 if (PR_Close(sock
) == PR_FAILURE
) {
83 fprintf(stderr
, "PR_Close failed\n");
90 PRFileDesc
*listenSock
;
91 PRFileDesc
*acceptSock
;
93 PRThread
*clientThread
;
99 memset(buf
, 0, sizeof(buf
)); /* Initialize the buffer. */
100 listenSock
= PR_NewTCPSocket();
101 if (NULL
== listenSock
) {
102 fprintf(stderr
, "PR_NewTCPSocket failed\n");
105 if (PR_InitializeNetAddr(PR_IpAddrAny
, 0, &addr
) == PR_FAILURE
) {
106 fprintf(stderr
, "PR_InitializeNetAddr failed\n");
109 if (PR_Bind(listenSock
, &addr
) == PR_FAILURE
) {
110 fprintf(stderr
, "PR_Bind failed\n");
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");
118 if (PR_Listen(listenSock
, 5) == PR_FAILURE
) {
119 fprintf(stderr
, "PR_Listen failed\n");
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");
133 acceptSock
= PR_Accept(listenSock
, NULL
, PR_INTERVAL_NO_TIMEOUT
);
134 if (NULL
== acceptSock
) {
135 fprintf(stderr
, "PR_Accept failed\n");
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");
148 printf("calling PR_Writev with a zero-length buffer\n");
150 nbytes
= PR_Writev(acceptSock
, &iov
, 1, PR_INTERVAL_NO_TIMEOUT
);
152 fprintf(stderr
, "PR_Writev should return 0 but returns %d\n", nbytes
);
155 if (PR_Close(acceptSock
) == PR_FAILURE
) {
156 fprintf(stderr
, "PR_Close failed\n");
159 if (PR_JoinThread(clientThread
) == PR_FAILURE
) {
160 fprintf(stderr
, "PR_JoinThread failed\n");
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");
174 acceptSock
= PR_Accept(listenSock
, NULL
, PR_INTERVAL_NO_TIMEOUT
);
175 if (NULL
== acceptSock
) {
176 fprintf(stderr
, "PR_Accept failed\n");
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");
187 printf("calling PR_Write with a zero-length buffer\n");
189 nbytes
= PR_Write(acceptSock
, buf
, 0);
191 fprintf(stderr
, "PR_Write should return 0 but returns %d\n", nbytes
);
194 if (PR_Close(acceptSock
) == PR_FAILURE
) {
195 fprintf(stderr
, "PR_Close failed\n");
198 if (PR_JoinThread(clientThread
) == PR_FAILURE
) {
199 fprintf(stderr
, "PR_JoinThread failed\n");
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");
213 acceptSock
= PR_Accept(listenSock
, NULL
, PR_INTERVAL_NO_TIMEOUT
);
214 if (NULL
== acceptSock
) {
215 fprintf(stderr
, "PR_Accept failed\n");
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");
226 printf("calling PR_Send with a zero-length buffer\n");
228 nbytes
= PR_Send(acceptSock
, buf
, 0, 0, PR_INTERVAL_NO_TIMEOUT
);
230 fprintf(stderr
, "PR_Send should return 0 but returns %d\n", nbytes
);
233 if (PR_Close(acceptSock
) == PR_FAILURE
) {
234 fprintf(stderr
, "PR_Close failed\n");
237 if (PR_JoinThread(clientThread
) == PR_FAILURE
) {
238 fprintf(stderr
, "PR_JoinThread failed\n");
242 if (PR_Close(listenSock
) == PR_FAILURE
) {
243 fprintf(stderr
, "PR_Close failed\n");