sendzlf.c 6.6 KB

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