nsprpub/pr/tests/sendzlf.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/sendzlf.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,214 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * Test: sendzlf.c 
    1.11 + *
    1.12 + * Description: send a zero-length file with PR_SendFile and
    1.13 + * PR_TransmitFile.
    1.14 + */
    1.15 +
    1.16 +#define ZERO_LEN_FILE_NAME "zerolen.tmp"
    1.17 +#define HEADER_STR "Header"
    1.18 +#define HEADER_LEN 6 /* length of HEADER_STR, not counting the null byte */
    1.19 +#define TRAILER_STR "Trailer"
    1.20 +#define TRAILER_LEN 7 /* length of TRAILER_STR, not counting the null byte */
    1.21 +
    1.22 +#include "nspr.h"
    1.23 +
    1.24 +#include <stdio.h>
    1.25 +#include <stdlib.h>
    1.26 +#include <string.h>
    1.27 +
    1.28 +static void ClientThread(void *arg)
    1.29 +{
    1.30 +    PRFileDesc *sock;
    1.31 +    PRNetAddr addr;
    1.32 +    PRUint16 port = (PRUint16) arg;
    1.33 +    char buf[1024];
    1.34 +    char *bufPtr;
    1.35 +    PRInt32 nbytes;
    1.36 +    PRInt32 ntotal;
    1.37 +    PRInt32 nexpected;
    1.38 +
    1.39 +    sock = PR_NewTCPSocket();
    1.40 +    if (NULL == sock) {
    1.41 +        fprintf(stderr, "PR_NewTCPSocket failed\n");
    1.42 +        exit(1);
    1.43 +    }
    1.44 +    if (PR_InitializeNetAddr(PR_IpAddrLoopback, port, &addr) == PR_FAILURE) {
    1.45 +        fprintf(stderr, "PR_InitializeNetAddr failed\n");
    1.46 +        exit(1);
    1.47 +    }
    1.48 +    if (PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE) {
    1.49 +        fprintf(stderr, "PR_Connect failed\n");
    1.50 +        exit(1);
    1.51 +    }
    1.52 +    ntotal = 0;
    1.53 +    bufPtr = buf;
    1.54 +    while ((nbytes = PR_Read(sock, bufPtr, sizeof(buf)-ntotal)) > 0) {
    1.55 +        ntotal += nbytes;
    1.56 +        bufPtr += nbytes;
    1.57 +    }
    1.58 +    if (-1 == nbytes) {
    1.59 +        fprintf(stderr, "PR_Read failed\n");
    1.60 +        exit(1);
    1.61 +    }
    1.62 +    nexpected = HEADER_LEN+TRAILER_LEN+TRAILER_LEN+HEADER_LEN+HEADER_LEN;
    1.63 +    if (ntotal != nexpected) {
    1.64 +        fprintf(stderr, "total bytes read should be %d but is %d\n",
    1.65 +                nexpected, ntotal);
    1.66 +        exit(1);
    1.67 +    }
    1.68 +    if (memcmp(buf, HEADER_STR TRAILER_STR TRAILER_STR HEADER_STR HEADER_STR,
    1.69 +            nexpected) != 0) {
    1.70 +        fprintf(stderr, "wrong data is received\n");
    1.71 +        exit(1);
    1.72 +    }
    1.73 +    if (PR_Close(sock) == PR_FAILURE) {
    1.74 +        fprintf(stderr, "PR_Close failed\n");
    1.75 +        exit(1);
    1.76 +    }
    1.77 +}
    1.78 +
    1.79 +static void ServerThread(void *arg)
    1.80 +{
    1.81 +    PRFileDesc *listenSock = (PRFileDesc *) arg;
    1.82 +    PRFileDesc *acceptSock;
    1.83 +    PRFileDesc *file;
    1.84 +    PRSendFileData sfd;
    1.85 +    char header[1024], trailer[1024];
    1.86 +    PRInt32 nbytes;
    1.87 +
    1.88 +    /* Create a zero-length file */
    1.89 +    file = PR_Open(ZERO_LEN_FILE_NAME,
    1.90 +            PR_CREATE_FILE|PR_TRUNCATE|PR_RDWR, 0666);
    1.91 +    if (NULL == file) {
    1.92 +        fprintf(stderr, "PR_Open failed\n");
    1.93 +        exit(1);
    1.94 +    }
    1.95 +    sfd.fd = file;
    1.96 +    sfd.file_offset = 0;
    1.97 +    sfd.file_nbytes = 0;
    1.98 +    memcpy(header, HEADER_STR, HEADER_LEN);
    1.99 +    memcpy(trailer, TRAILER_STR, TRAILER_LEN);
   1.100 +    sfd.header = header;
   1.101 +    sfd.hlen = HEADER_LEN;
   1.102 +    sfd.trailer = trailer;
   1.103 +    sfd.tlen = TRAILER_LEN;
   1.104 +    acceptSock = PR_Accept(listenSock, NULL, PR_INTERVAL_NO_TIMEOUT);
   1.105 +    if (NULL == acceptSock) {
   1.106 +        fprintf(stderr, "PR_Accept failed\n");
   1.107 +        exit(1);
   1.108 +    }
   1.109 +    /* Send both header and trailer */
   1.110 +    nbytes = PR_SendFile(acceptSock, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
   1.111 +            PR_INTERVAL_NO_TIMEOUT);
   1.112 +    if (HEADER_LEN+TRAILER_LEN != nbytes) {
   1.113 +        fprintf(stderr, "PR_SendFile should return %d but returned %d\n",
   1.114 +                HEADER_LEN+TRAILER_LEN, nbytes);
   1.115 +        exit(1);
   1.116 +    }
   1.117 +    /* Trailer only, no header */
   1.118 +    sfd.hlen = 0;
   1.119 +    nbytes = PR_SendFile(acceptSock, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
   1.120 +            PR_INTERVAL_NO_TIMEOUT);
   1.121 +    if (TRAILER_LEN != nbytes) {
   1.122 +        fprintf(stderr, "PR_SendFile should return %d but returned %d\n",
   1.123 +                TRAILER_LEN, nbytes);
   1.124 +        exit(1);
   1.125 +    }
   1.126 +    /* Header only, no trailer */
   1.127 +    sfd.hlen = HEADER_LEN;
   1.128 +    sfd.tlen = 0;
   1.129 +    nbytes = PR_SendFile(acceptSock, &sfd, PR_TRANSMITFILE_KEEP_OPEN,
   1.130 +            PR_INTERVAL_NO_TIMEOUT);
   1.131 +    if (HEADER_LEN != nbytes) {
   1.132 +        fprintf(stderr, "PR_SendFile should return %d but returned %d\n",
   1.133 +                HEADER_LEN, nbytes);
   1.134 +        exit(1);
   1.135 +    }
   1.136 +    /* Try PR_TransmitFile */
   1.137 +    nbytes = PR_TransmitFile(acceptSock, file, header, HEADER_LEN,
   1.138 +            PR_TRANSMITFILE_KEEP_OPEN, PR_INTERVAL_NO_TIMEOUT);
   1.139 +    if (HEADER_LEN != nbytes) {
   1.140 +        fprintf(stderr, "PR_TransmitFile should return %d but returned %d\n",
   1.141 +                HEADER_LEN, nbytes);
   1.142 +        exit(1);
   1.143 +    }
   1.144 +    if (PR_Close(acceptSock) == PR_FAILURE) {
   1.145 +        fprintf(stderr, "PR_Close failed\n");
   1.146 +        exit(1);
   1.147 +    }
   1.148 +    if (PR_Close(file) == PR_FAILURE) {
   1.149 +        fprintf(stderr, "PR_Close failed\n");
   1.150 +        exit(1);
   1.151 +    }
   1.152 +    if (PR_Delete(ZERO_LEN_FILE_NAME) == PR_FAILURE) {
   1.153 +        fprintf(stderr, "PR_Delete failed\n");
   1.154 +        exit(1);
   1.155 +    }
   1.156 +}
   1.157 +
   1.158 +int main(int argc, char **argv)
   1.159 +{
   1.160 +    PRFileDesc *listenSock;
   1.161 +    PRThread *clientThread;
   1.162 +    PRThread *serverThread;
   1.163 +    PRNetAddr addr;
   1.164 +    PRThreadScope scope = PR_GLOBAL_THREAD;
   1.165 +
   1.166 +    listenSock = PR_NewTCPSocket();
   1.167 +    if (NULL == listenSock) {
   1.168 +        fprintf(stderr, "PR_NewTCPSocket failed\n");
   1.169 +        exit(1);
   1.170 +    }
   1.171 +    if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
   1.172 +        fprintf(stderr, "PR_InitializeNetAddr failed\n");
   1.173 +        exit(1);
   1.174 +    }
   1.175 +    if (PR_Bind(listenSock, &addr) == PR_FAILURE) {
   1.176 +        fprintf(stderr, "PR_Bind failed\n");
   1.177 +        exit(1);
   1.178 +    }
   1.179 +    /* Find out what port number we are bound to. */
   1.180 +    if (PR_GetSockName(listenSock, &addr) == PR_FAILURE) {
   1.181 +        fprintf(stderr, "PR_GetSockName failed\n");
   1.182 +        exit(1);
   1.183 +    }
   1.184 +    if (PR_Listen(listenSock, 5) == PR_FAILURE) {
   1.185 +        fprintf(stderr, "PR_Listen failed\n");
   1.186 +        exit(1);
   1.187 +    }
   1.188 +
   1.189 +    clientThread = PR_CreateThread(PR_USER_THREAD,
   1.190 +            ClientThread, (void *) PR_ntohs(PR_NetAddrInetPort(&addr)),
   1.191 +            PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
   1.192 +    if (NULL == clientThread) {
   1.193 +        fprintf(stderr, "PR_CreateThread failed\n");
   1.194 +        exit(1);
   1.195 +    }
   1.196 +    serverThread = PR_CreateThread(PR_USER_THREAD,
   1.197 +            ServerThread, listenSock,
   1.198 +            PR_PRIORITY_NORMAL, scope, PR_JOINABLE_THREAD, 0);
   1.199 +    if (NULL == serverThread) {
   1.200 +        fprintf(stderr, "PR_CreateThread failed\n");
   1.201 +        exit(1);
   1.202 +    }
   1.203 +    if (PR_JoinThread(clientThread) == PR_FAILURE) {
   1.204 +        fprintf(stderr, "PR_JoinThread failed\n");
   1.205 +        exit(1);
   1.206 +    }
   1.207 +    if (PR_JoinThread(serverThread) == PR_FAILURE) {
   1.208 +        fprintf(stderr, "PR_JoinThread failed\n");
   1.209 +        exit(1);
   1.210 +    }
   1.211 +    if (PR_Close(listenSock) == PR_FAILURE) {
   1.212 +        fprintf(stderr, "PR_Close failed\n");
   1.213 +        exit(1);
   1.214 +    }
   1.215 +    printf("PASS\n");
   1.216 +    return 0;
   1.217 +}

mercurial