test-process.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Copyright (c) 2011-2019, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #include <stdio.h>
  5. #ifdef _WIN32
  6. #define WINDOWS_LEAN_AND_MEAN
  7. #include <windows.h>
  8. #else
  9. #include <unistd.h>
  10. #endif /* defined(_WIN32) */
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #ifdef _WIN32
  14. #define SLEEP(sec) Sleep((sec)*1000)
  15. #else
  16. #define SLEEP(sec) sleep(sec)
  17. #endif
  18. /* Trivial test program to test process_t. */
  19. int
  20. main(int argc, char **argv)
  21. {
  22. /* Does our process get the right arguments? */
  23. for (int i = 0; i < argc; ++i) {
  24. fprintf(stdout, "argv[%d] = '%s'\n", i, argv[i]);
  25. fflush(stdout);
  26. }
  27. /* Make sure our process got our environment variable. */
  28. fprintf(stdout, "Environment variable TOR_TEST_ENV = '%s'\n",
  29. getenv("TOR_TEST_ENV"));
  30. fflush(stdout);
  31. /* Test line handling on stdout and stderr. */
  32. fprintf(stdout, "Output on stdout\nThis is a new line\n");
  33. fflush(stdout);
  34. fprintf(stderr, "Output on stderr\nThis is a new line\n");
  35. fflush(stderr);
  36. fprintf(stdout, "Partial line on stdout ...");
  37. fflush(stdout);
  38. fprintf(stderr, "Partial line on stderr ...");
  39. fflush(stderr);
  40. SLEEP(2);
  41. fprintf(stdout, "end of partial line on stdout\n");
  42. fflush(stdout);
  43. fprintf(stderr, "end of partial line on stderr\n");
  44. fflush(stderr);
  45. /* Echo input from stdin. */
  46. char buffer[1024];
  47. int count = 0;
  48. while (fgets(buffer, sizeof(buffer), stdin)) {
  49. /* Strip the newline. */
  50. size_t size = strlen(buffer);
  51. if (size >= 1 && buffer[size - 1] == '\n') {
  52. buffer[size - 1] = '\0';
  53. --size;
  54. }
  55. if (size >= 1 && buffer[size - 1] == '\r') {
  56. buffer[size - 1] = '\0';
  57. --size;
  58. }
  59. fprintf(stdout, "Read line from stdin: '%s'\n", buffer);
  60. fflush(stdout);
  61. if (++count == 3)
  62. break;
  63. }
  64. fprintf(stdout, "We are done for here, thank you!\n");
  65. return 0;
  66. }