nsprpub/pr/tests/sendzlf.c

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * Test: sendzlf.c
michael@0 8 *
michael@0 9 * Description: send a zero-length file with PR_SendFile and
michael@0 10 * PR_TransmitFile.
michael@0 11 */
michael@0 12
michael@0 13 #define ZERO_LEN_FILE_NAME "zerolen.tmp"
michael@0 14 #define HEADER_STR "Header"
michael@0 15 #define HEADER_LEN 6 /* length of HEADER_STR, not counting the null byte */
michael@0 16 #define TRAILER_STR "Trailer"
michael@0 17 #define TRAILER_LEN 7 /* length of TRAILER_STR, not counting the null byte */
michael@0 18
michael@0 19 #include "nspr.h"
michael@0 20
michael@0 21 #include <stdio.h>
michael@0 22 #include <stdlib.h>
michael@0 23 #include <string.h>
michael@0 24
michael@0 25 static void ClientThread(void *arg)
michael@0 26 {
michael@0 27 PRFileDesc *sock;
michael@0 28 PRNetAddr addr;
michael@0 29 PRUint16 port = (PRUint16) arg;
michael@0 30 char buf[1024];
michael@0 31 char *bufPtr;
michael@0 32 PRInt32 nbytes;
michael@0 33 PRInt32 ntotal;
michael@0 34 PRInt32 nexpected;
michael@0 35
michael@0 36 sock = PR_NewTCPSocket();
michael@0 37 if (NULL == sock) {
michael@0 38 fprintf(stderr, "PR_NewTCPSocket failed\n");
michael@0 39 exit(1);
michael@0 40 }
michael@0 41 if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) {
michael@0 42 fprintf(stderr, "PR_InitializeNetAddr failed\n");
michael@0 43 exit(1);
michael@0 44 }
michael@0 45 if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
michael@0 46 fprintf(stderr, "PR_Connect failed\n");
michael@0 47 exit(1);
michael@0 48 }
michael@0 49 ntotal = 0;
michael@0 50 bufPtr = buf;
michael@0 51 while ((nbytes = PR_Read(sock, bufPtr, sizeof(buf)-ntotal)) > 0) {
michael@0 52 ntotal += nbytes;
michael@0 53 bufPtr += nbytes;
michael@0 54 }
michael@0 55 if (-1 == nbytes) {
michael@0 56 fprintf(stderr, "PR_Read failed\n");
michael@0 57 exit(1);
michael@0 58 }
michael@0 59 nexpected = HEADER_LEN+TRAILER_LEN+TRAILER_LEN+HEADER_LEN+HEADER_LEN;
michael@0 60 if (ntotal != nexpected) {
michael@0 61 fprintf(stderr, "total bytes read should be %d but is %d\n",
michael@0 62 nexpected, ntotal);
michael@0 63 exit(1);
michael@0 64 }
michael@0 65 if (memcmp(buf, HEADER_STR TRAILER_STR TRAILER_STR HEADER_STR HEADER_STR,
michael@0 66 nexpected) != 0) {
michael@0 67 fprintf(stderr, "wrong data is received\n");
michael@0 68 exit(1);
michael@0 69 }
michael@0 70 if (PR_Close(sock) == PR_FAILURE) {
michael@0 71 fprintf(stderr, "PR_Close failed\n");
michael@0 72 exit(1);
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 static void ServerThread(void *arg)
michael@0 77 {
michael@0 78 PRFileDesc *listenSock = (PRFileDesc *) arg;
michael@0 79 PRFileDesc *acceptSock;
michael@0 80 PRFileDesc *file;
michael@0 81 PRSendFileData sfd;
michael@0 82 char header[1024], trailer[1024];
michael@0 83 PRInt32 nbytes;
michael@0 84
michael@0 85 /* Create a zero-length file */
michael@0 86 file = PR_Open(ZERO_LEN_FILE_NAME,
michael@0 87 PR_CREATE_FILE|PR_TRUNCATE|PR_RDWR, 0666);
michael@0 88 if (NULL == file) {
michael@0 89 fprintf(stderr, "PR_Open failed\n");
michael@0 90 exit(1);
michael@0 91 }
michael@0 92 sfd.fd = file;
michael@0 93 sfd.file_offset = 0;
michael@0 94 sfd.file_nbytes = 0;
michael@0 95 memcpy(header, HEADER_STR, HEADER_LEN);
michael@0 96 memcpy(trailer, TRAILER_STR, TRAILER_LEN);
michael@0 97 sfd.header = header;
michael@0 98 sfd.hlen = HEADER_LEN;
michael@0 99 sfd.trailer = trailer;
michael@0 100 sfd.tlen = TRAILER_LEN;
michael@0 101 acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
michael@0 102 if (NULL == acceptSock) {
michael@0 103 fprintf(stderr, "PR_Accept failed\n");
michael@0 104 exit(1);
michael@0 105 }
michael@0 106 /* Send both header and trailer */
michael@0 107 nbytes = PR_SendFile(acceptSock, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
michael@0 108 PR_INTERVAL_NO_TIMEOUT);
michael@0 109 if (HEADER_LEN+TRAILER_LEN != nbytes) {
michael@0 110 fprintf(stderr, "PR_SendFile should return %d but returned %d\n",
michael@0 111 HEADER_LEN+TRAILER_LEN, nbytes);
michael@0 112 exit(1);
michael@0 113 }
michael@0 114 /* Trailer only, no header */
michael@0 115 sfd.hlen = 0;
michael@0 116 nbytes = PR_SendFile(acceptSock, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
michael@0 117 PR_INTERVAL_NO_TIMEOUT);
michael@0 118 if (TRAILER_LEN != nbytes) {
michael@0 119 fprintf(stderr, "PR_SendFile should return %d but returned %d\n",
michael@0 120 TRAILER_LEN, nbytes);
michael@0 121 exit(1);
michael@0 122 }
michael@0 123 /* Header only, no trailer */
michael@0 124 sfd.hlen = HEADER_LEN;
michael@0 125 sfd.tlen = 0;
michael@0 126 nbytes = PR_SendFile(acceptSock, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
michael@0 127 PR_INTERVAL_NO_TIMEOUT);
michael@0 128 if (HEADER_LEN != nbytes) {
michael@0 129 fprintf(stderr, "PR_SendFile should return %d but returned %d\n",
michael@0 130 HEADER_LEN, nbytes);
michael@0 131 exit(1);
michael@0 132 }
michael@0 133 /* Try PR_TransmitFile */
michael@0 134 nbytes = PR_TransmitFile(acceptSock, file, header, HEADER_LEN,
michael@0 135 PR_TRANSMITFILE_KEEP_OPEN, PR_INTERVAL_NO_TIMEOUT);
michael@0 136 if (HEADER_LEN != nbytes) {
michael@0 137 fprintf(stderr, "PR_TransmitFile should return %d but returned %d\n",
michael@0 138 HEADER_LEN, nbytes);
michael@0 139 exit(1);
michael@0 140 }
michael@0 141 if (PR_Close(acceptSock) == PR_FAILURE) {
michael@0 142 fprintf(stderr, "PR_Close failed\n");
michael@0 143 exit(1);
michael@0 144 }
michael@0 145 if (PR_Close(file) == PR_FAILURE) {
michael@0 146 fprintf(stderr, "PR_Close failed\n");
michael@0 147 exit(1);
michael@0 148 }
michael@0 149 if (PR_Delete(ZERO_LEN_FILE_NAME) == PR_FAILURE) {
michael@0 150 fprintf(stderr, "PR_Delete failed\n");
michael@0 151 exit(1);
michael@0 152 }
michael@0 153 }
michael@0 154
michael@0 155 int main(int argc, char **argv)
michael@0 156 {
michael@0 157 PRFileDesc *listenSock;
michael@0 158 PRThread *clientThread;
michael@0 159 PRThread *serverThread;
michael@0 160 PRNetAddr addr;
michael@0 161 PRThreadScope scope = PR_GLOBAL_THREAD;
michael@0 162
michael@0 163 listenSock = PR_NewTCPSocket();
michael@0 164 if (NULL == listenSock) {
michael@0 165 fprintf(stderr, "PR_NewTCPSocket failed\n");
michael@0 166 exit(1);
michael@0 167 }
michael@0 168 if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
michael@0 169 fprintf(stderr, "PR_InitializeNetAddr failed\n");
michael@0 170 exit(1);
michael@0 171 }
michael@0 172 if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
michael@0 173 fprintf(stderr, "PR_Bind failed\n");
michael@0 174 exit(1);
michael@0 175 }
michael@0 176 /* Find out what port number we are bound to. */
michael@0 177 if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
michael@0 178 fprintf(stderr, "PR_GetSockName failed\n");
michael@0 179 exit(1);
michael@0 180 }
michael@0 181 if (PR_Listen(listenSock, 5) == PR_FAILURE) {
michael@0 182 fprintf(stderr, "PR_Listen failed\n");
michael@0 183 exit(1);
michael@0 184 }
michael@0 185
michael@0 186 clientThread = PR_CreateThread(PR_USER_THREAD,
michael@0 187 ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
michael@0 188 PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
michael@0 189 if (NULL == clientThread) {
michael@0 190 fprintf(stderr, "PR_CreateThread failed\n");
michael@0 191 exit(1);
michael@0 192 }
michael@0 193 serverThread = PR_CreateThread(PR_USER_THREAD,
michael@0 194 ServerThread, listenSock,
michael@0 195 PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
michael@0 196 if (NULL == serverThread) {
michael@0 197 fprintf(stderr, "PR_CreateThread failed\n");
michael@0 198 exit(1);
michael@0 199 }
michael@0 200 if (PR_JoinThread(clientThread) == PR_FAILURE) {
michael@0 201 fprintf(stderr, "PR_JoinThread failed\n");
michael@0 202 exit(1);
michael@0 203 }
michael@0 204 if (PR_JoinThread(serverThread) == PR_FAILURE) {
michael@0 205 fprintf(stderr, "PR_JoinThread failed\n");
michael@0 206 exit(1);
michael@0 207 }
michael@0 208 if (PR_Close(listenSock) == PR_FAILURE) {
michael@0 209 fprintf(stderr, "PR_Close failed\n");
michael@0 210 exit(1);
michael@0 211 }
michael@0 212 printf("PASS\n");
michael@0 213 return 0;
michael@0 214 }

mercurial