sockping.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * File: sockping.c
  7. *
  8. * Description:
  9. * This test runs in conjunction with the sockpong test.
  10. * This test creates a socket pair and passes one socket
  11. * to the sockpong test. Then this test writes "ping" to
  12. * to the sockpong test and the sockpong test writes "pong"
  13. * back. To run this pair of tests, just invoke sockping.
  14. *
  15. * Tested areas: process creation, socket pairs, file
  16. * descriptor inheritance.
  17. */
  18. #include "prerror.h"
  19. #include "prio.h"
  20. #include "prproces.h"
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #define NUM_ITERATIONS 10
  25. static char *child_argv[] = { "sockpong", NULL };
  26. int main(int argc, char **argv)
  27. {
  28. PRFileDesc *sock[2];
  29. PRStatus status;
  30. PRProcess *process;
  31. PRProcessAttr *attr;
  32. char buf[1024];
  33. PRInt32 nBytes;
  34. PRInt32 exitCode;
  35. int idx;
  36. status = PR_NewTCPSocketPair(sock);
  37. if (status == PR_FAILURE) {
  38. fprintf(stderr, "PR_NewTCPSocketPair failed\n");
  39. exit(1);
  40. }
  41. status = PR_SetFDInheritable(sock[0], PR_FALSE);
  42. if (status == PR_FAILURE) {
  43. fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n",
  44. PR_GetError(), PR_GetOSError());
  45. exit(1);
  46. }
  47. status = PR_SetFDInheritable(sock[1], PR_TRUE);
  48. if (status == PR_FAILURE) {
  49. fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n",
  50. PR_GetError(), PR_GetOSError());
  51. exit(1);
  52. }
  53. attr = PR_NewProcessAttr();
  54. if (attr == NULL) {
  55. fprintf(stderr, "PR_NewProcessAttr failed\n");
  56. exit(1);
  57. }
  58. status = PR_ProcessAttrSetInheritableFD(attr, sock[1], "SOCKET");
  59. if (status == PR_FAILURE) {
  60. fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n");
  61. exit(1);
  62. }
  63. process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr);
  64. if (process == NULL) {
  65. fprintf(stderr, "PR_CreateProcess failed\n");
  66. exit(1);
  67. }
  68. PR_DestroyProcessAttr(attr);
  69. status = PR_Close(sock[1]);
  70. if (status == PR_FAILURE) {
  71. fprintf(stderr, "PR_Close failed\n");
  72. exit(1);
  73. }
  74. for (idx = 0; idx < NUM_ITERATIONS; idx++) {
  75. strcpy(buf, "ping");
  76. printf("ping process: sending \"%s\"\n", buf);
  77. nBytes = PR_Write(sock[0], buf, 5);
  78. if (nBytes == -1) {
  79. fprintf(stderr, "PR_Write failed: (%d, %d)\n",
  80. PR_GetError(), PR_GetOSError());
  81. exit(1);
  82. }
  83. memset(buf, 0, sizeof(buf));
  84. nBytes = PR_Read(sock[0], buf, sizeof(buf));
  85. if (nBytes == -1) {
  86. fprintf(stderr, "PR_Read failed: (%d, %d)\n",
  87. PR_GetError(), PR_GetOSError());
  88. exit(1);
  89. }
  90. printf("ping process: received \"%s\"\n", buf);
  91. if (nBytes != 5) {
  92. fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n",
  93. nBytes);
  94. exit(1);
  95. }
  96. if (strcmp(buf, "pong") != 0) {
  97. fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n",
  98. buf);
  99. exit(1);
  100. }
  101. }
  102. status = PR_Close(sock[0]);
  103. if (status == PR_FAILURE) {
  104. fprintf(stderr, "PR_Close failed\n");
  105. exit(1);
  106. }
  107. status = PR_WaitProcess(process, &exitCode);
  108. if (status == PR_FAILURE) {
  109. fprintf(stderr, "PR_WaitProcess failed\n");
  110. exit(1);
  111. }
  112. if (exitCode == 0) {
  113. printf("PASS\n");
  114. return 0;
  115. } else {
  116. printf("FAIL\n");
  117. return 1;
  118. }
  119. }