Back to index

lightning-sunbird  0.9+nobinonly
sendzlf.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Test: sendzlf.c 
00040  *
00041  * Description: send a zero-length file with PR_SendFile and
00042  * PR_TransmitFile.
00043  */
00044 
00045 #define ZERO_LEN_FILE_NAME "zerolen.tmp"
00046 #define HEADER_STR "Header"
00047 #define HEADER_LEN 6 /* length of HEADER_STR, not counting the null byte */
00048 #define TRAILER_STR "Trailer"
00049 #define TRAILER_LEN 7 /* length of TRAILER_STR, not counting the null byte */
00050 
00051 #include "nspr.h"
00052 
00053 #include <stdio.h>
00054 #include <stdlib.h>
00055 #include <string.h>
00056 
00057 static void ClientThread(void *arg)
00058 {
00059     PRFileDesc *sock;
00060     PRNetAddr addr;
00061     PRUint16 port = (PRUint16) arg;
00062     char buf[1024];
00063     char *bufPtr;
00064     PRInt32 nbytes;
00065     PRInt32 ntotal;
00066     PRInt32 nexpected;
00067 
00068     sock = PR_NewTCPSocket();
00069     if (NULL == sock) {
00070         fprintf(stderr, "PR_NewTCPSocket failed\n");
00071         exit(1);
00072     }
00073     if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) {
00074         fprintf(stderr, "PR_InitializeNetAddr failed\n");
00075         exit(1);
00076     }
00077     if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
00078         fprintf(stderr, "PR_Connect failed\n");
00079         exit(1);
00080     }
00081     ntotal = 0;
00082     bufPtr = buf;
00083     while ((nbytes = PR_Read(sock, bufPtr, sizeof(buf)-ntotal)) > 0) {
00084         ntotal += nbytes;
00085         bufPtr += nbytes;
00086     }
00087     if (-1 == nbytes) {
00088         fprintf(stderr, "PR_Read failed\n");
00089         exit(1);
00090     }
00091     nexpected = HEADER_LEN+TRAILER_LEN+TRAILER_LEN+HEADER_LEN+HEADER_LEN;
00092     if (ntotal != nexpected) {
00093         fprintf(stderr, "total bytes read should be %d but is %d\n",
00094                 nexpected, ntotal);
00095         exit(1);
00096     }
00097     if (memcmp(buf, HEADER_STR TRAILER_STR TRAILER_STR HEADER_STR HEADER_STR,
00098             nexpected) != 0) {
00099         fprintf(stderr, "wrong data is received\n");
00100         exit(1);
00101     }
00102     if (PR_Close(sock) == PR_FAILURE) {
00103         fprintf(stderr, "PR_Close failed\n");
00104         exit(1);
00105     }
00106 }
00107 
00108 static void ServerThread(void *arg)
00109 {
00110     PRFileDesc *listenSock = (PRFileDesc *) arg;
00111     PRFileDesc *acceptSock;
00112     PRFileDesc *file;
00113     PRSendFileData sfd;
00114     char header[1024], trailer[1024];
00115     PRInt32 nbytes;
00116 
00117     /* Create a zero-length file */
00118     file = PR_Open(ZERO_LEN_FILE_NAME,
00119             PR_CREATE_FILE|PR_TRUNCATE|PR_RDWR, 0666);
00120     if (NULL == file) {
00121         fprintf(stderr, "PR_Open failed\n");
00122         exit(1);
00123     }
00124     sfd.fd = file;
00125     sfd.file_offset = 0;
00126     sfd.file_nbytes = 0;
00127     memcpy(header, HEADER_STR, HEADER_LEN);
00128     memcpy(trailer, TRAILER_STR, TRAILER_LEN);
00129     sfd.header = header;
00130     sfd.hlen = HEADER_LEN;
00131     sfd.trailer = trailer;
00132     sfd.tlen = TRAILER_LEN;
00133     acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
00134     if (NULL == acceptSock) {
00135         fprintf(stderr, "PR_Accept failed\n");
00136         exit(1);
00137     }
00138     /* Send both header and trailer */
00139     nbytes = PR_SendFile(acceptSock, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
00140             PR_INTERVAL_NO_TIMEOUT);
00141     if (HEADER_LEN+TRAILER_LEN != nbytes) {
00142         fprintf(stderr, "PR_SendFile should return %d but returned %d\n",
00143                 HEADER_LEN+TRAILER_LEN, nbytes);
00144         exit(1);
00145     }
00146     /* Trailer only, no header */
00147     sfd.hlen = 0;
00148     nbytes = PR_SendFile(acceptSock, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
00149             PR_INTERVAL_NO_TIMEOUT);
00150     if (TRAILER_LEN != nbytes) {
00151         fprintf(stderr, "PR_SendFile should return %d but returned %d\n",
00152                 TRAILER_LEN, nbytes);
00153         exit(1);
00154     }
00155     /* Header only, no trailer */
00156     sfd.hlen = HEADER_LEN;
00157     sfd.tlen = 0;
00158     nbytes = PR_SendFile(acceptSock, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
00159             PR_INTERVAL_NO_TIMEOUT);
00160     if (HEADER_LEN != nbytes) {
00161         fprintf(stderr, "PR_SendFile should return %d but returned %d\n",
00162                 HEADER_LEN, nbytes);
00163         exit(1);
00164     }
00165     /* Try PR_TransmitFile */
00166     nbytes = PR_TransmitFile(acceptSock, file, header, HEADER_LEN,
00167             PR_TRANSMITFILE_KEEP_OPEN, PR_INTERVAL_NO_TIMEOUT);
00168     if (HEADER_LEN != nbytes) {
00169         fprintf(stderr, "PR_TransmitFile should return %d but returned %d\n",
00170                 HEADER_LEN, nbytes);
00171         exit(1);
00172     }
00173     if (PR_Close(acceptSock) == PR_FAILURE) {
00174         fprintf(stderr, "PR_Close failed\n");
00175         exit(1);
00176     }
00177     if (PR_Close(file) == PR_FAILURE) {
00178         fprintf(stderr, "PR_Close failed\n");
00179         exit(1);
00180     }
00181     if (PR_Delete(ZERO_LEN_FILE_NAME) == PR_FAILURE) {
00182         fprintf(stderr, "PR_Delete failed\n");
00183         exit(1);
00184     }
00185 }
00186 
00187 int main()
00188 {
00189     PRFileDesc *listenSock;
00190     PRThread *clientThread;
00191     PRThread *serverThread;
00192     PRNetAddr addr;
00193     PRThreadScope scope = PR_GLOBAL_THREAD;
00194 
00195     listenSock = PR_NewTCPSocket();
00196     if (NULL == listenSock) {
00197         fprintf(stderr, "PR_NewTCPSocket failed\n");
00198         exit(1);
00199     }
00200     if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
00201         fprintf(stderr, "PR_InitializeNetAddr failed\n");
00202         exit(1);
00203     }
00204     if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
00205         fprintf(stderr, "PR_Bind failed\n");
00206         exit(1);
00207     }
00208     /* Find out what port number we are bound to. */
00209     if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
00210         fprintf(stderr, "PR_GetSockName failed\n");
00211         exit(1);
00212     }
00213     if (PR_Listen(listenSock, 5) == PR_FAILURE) {
00214         fprintf(stderr, "PR_Listen failed\n");
00215         exit(1);
00216     }
00217 
00218     clientThread = PR_CreateThread(PR_USER_THREAD,
00219             ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
00220             PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
00221     if (NULL == clientThread) {
00222         fprintf(stderr, "PR_CreateThread failed\n");
00223         exit(1);
00224     }
00225     serverThread = PR_CreateThread(PR_USER_THREAD,
00226             ServerThread, listenSock,
00227             PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
00228     if (NULL == serverThread) {
00229         fprintf(stderr, "PR_CreateThread failed\n");
00230         exit(1);
00231     }
00232     if (PR_JoinThread(clientThread) == PR_FAILURE) {
00233         fprintf(stderr, "PR_JoinThread failed\n");
00234         exit(1);
00235     }
00236     if (PR_JoinThread(serverThread) == PR_FAILURE) {
00237         fprintf(stderr, "PR_JoinThread failed\n");
00238         exit(1);
00239     }
00240     if (PR_Close(listenSock) == PR_FAILURE) {
00241         fprintf(stderr, "PR_Close failed\n");
00242         exit(1);
00243     }
00244     printf("PASS\n");
00245     return 0;
00246 }