pipepong.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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: pipepong.c
  7. *
  8. * Description:
  9. * This test runs in conjunction with the pipeping test.
  10. * The pipeping test creates two pipes and redirects the
  11. * stdin and stdout of this test to the pipes. Then the
  12. * pipeping test writes "ping" to this test and this test
  13. * writes "pong" back. Note that this test does not depend
  14. * on NSPR at all. To run this pair of tests, just invoke
  15. * pipeping.
  16. *
  17. * Tested areas: process creation, pipes, file descriptor
  18. * inheritance, standard I/O redirection.
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #define NUM_ITERATIONS 10
  24. int main(int argc, char **argv)
  25. {
  26. char buf[1024];
  27. size_t nBytes;
  28. int idx;
  29. for (idx = 0; idx < NUM_ITERATIONS; idx++) {
  30. memset(buf, 0, sizeof(buf));
  31. nBytes = fread(buf, 1, 5, stdin);
  32. fprintf(stderr, "pong process: received \"%s\"\n", buf);
  33. if (nBytes != 5) {
  34. fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
  35. nBytes);
  36. exit(1);
  37. }
  38. if (strcmp(buf, "ping") != 0) {
  39. fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n",
  40. buf);
  41. exit(1);
  42. }
  43. strcpy(buf, "pong");
  44. fprintf(stderr, "pong process: sending \"%s\"\n", buf);
  45. nBytes = fwrite(buf, 1, 5, stdout);
  46. if (nBytes != 5) {
  47. fprintf(stderr, "pong process: fwrite failed\n");
  48. exit(1);
  49. }
  50. fflush(stdout);
  51. }
  52. return 0;
  53. }