testProcess.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <kwsys/Process.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #if defined(_WIN32)
  5. # include <windows.h>
  6. #else
  7. # include <unistd.h>
  8. #endif
  9. int test1()
  10. {
  11. fprintf(stdout, "Output on stdout from test returning 0.\n");
  12. fprintf(stderr, "Output on stderr from test returning 0.\n");
  13. return 0;
  14. }
  15. int test2()
  16. {
  17. fprintf(stdout, "Output on stdout from test returning 123.\n");
  18. fprintf(stderr, "Output on stderr from test returning 123.\n");
  19. return 123;
  20. }
  21. int test3()
  22. {
  23. fprintf(stdout, "Output before sleep on stdout from timeout test.\n");
  24. fprintf(stderr, "Output before sleep on stderr from timeout test.\n");
  25. fflush(stdout);
  26. fflush(stderr);
  27. #if defined(_WIN32)
  28. Sleep(5000);
  29. #else
  30. sleep(5);
  31. #endif
  32. fprintf(stdout, "Output after sleep on stdout from timeout test.\n");
  33. fprintf(stderr, "Output after sleep on stderr from timeout test.\n");
  34. return 0;
  35. }
  36. int test4()
  37. {
  38. fprintf(stdout, "Output before crash on stdout from crash test.\n");
  39. fprintf(stderr, "Output before crash on stderr from crash test.\n");
  40. fflush(stdout);
  41. fflush(stderr);
  42. *(int*)0 = 0;
  43. fprintf(stdout, "Output after crash on stdout from crash test.\n");
  44. fprintf(stderr, "Output after crash on stderr from crash test.\n");
  45. return 0;
  46. }
  47. int runChild(const char* cmd[], int state, int exception, int value)
  48. {
  49. int result = 0;
  50. char* data = 0;
  51. int length = 0;
  52. kwsysProcess* kp = kwsysProcess_New();
  53. if(!kp)
  54. {
  55. fprintf(stderr, "kwsysProcess_New returned NULL!\n");
  56. return 1;
  57. }
  58. kwsysProcess_SetCommand(kp, cmd);
  59. kwsysProcess_SetTimeout(kp, 3);
  60. kwsysProcess_Execute(kp);
  61. while(kwsysProcess_WaitForData(kp, (kwsysProcess_Pipe_STDOUT |
  62. kwsysProcess_Pipe_STDERR),
  63. &data, &length, 0))
  64. {
  65. fwrite(data, 1, length, stdout);
  66. fflush(stdout);
  67. }
  68. kwsysProcess_WaitForExit(kp, 0);
  69. switch (kwsysProcess_GetState(kp))
  70. {
  71. case kwsysProcess_State_Starting:
  72. printf("No process has been executed.\n"); break;
  73. case kwsysProcess_State_Executing:
  74. printf("The process is still executing.\n"); break;
  75. case kwsysProcess_State_Expired:
  76. printf("Child was killed when timeout expired.\n"); break;
  77. case kwsysProcess_State_Exited:
  78. printf("Child exited with value = %d\n",
  79. kwsysProcess_GetExitValue(kp));
  80. result = ((exception != kwsysProcess_GetExitException(kp)) ||
  81. (value != kwsysProcess_GetExitValue(kp))); break;
  82. case kwsysProcess_State_Killed:
  83. printf("Child was killed by parent.\n"); break;
  84. case kwsysProcess_State_Exception:
  85. printf("Child terminated abnormally.\n");
  86. result = ((exception != kwsysProcess_GetExitException(kp)) ||
  87. (value != kwsysProcess_GetExitValue(kp))); break;
  88. case kwsysProcess_State_Error:
  89. printf("Error in administrating child process: [%s]\n",
  90. kwsysProcess_GetErrorString(kp)); break;
  91. };
  92. if(result)
  93. {
  94. if(exception != kwsysProcess_GetExitException(kp))
  95. {
  96. fprintf(stderr, "Mismatch in exit exception. Should have been %d.\n",
  97. exception);
  98. }
  99. if(value != kwsysProcess_GetExitValue(kp))
  100. {
  101. fprintf(stderr, "Mismatch in exit value. Should have been %d.\n",
  102. value);
  103. }
  104. }
  105. if(kwsysProcess_GetState(kp) != state)
  106. {
  107. fprintf(stderr, "Mismatch in state. Should have been %d.\n", state);
  108. result = 1;
  109. }
  110. kwsysProcess_Delete(kp);
  111. return result;
  112. }
  113. int main(int argc, const char* argv[])
  114. {
  115. int n = 0;
  116. if(argc == 2)
  117. {
  118. n = atoi(argv[1]);
  119. }
  120. else if(argc == 3)
  121. {
  122. n = atoi(argv[2]);
  123. }
  124. /* Check arguments. */
  125. if(n < 1 || n > 5 || (argc == 3 && strcmp(argv[1], "run") != 0))
  126. {
  127. fprintf(stdout, "Usage: %s <test number>\n", argv[0]);
  128. return 1;
  129. }
  130. if(argc == 3)
  131. {
  132. switch (n)
  133. {
  134. case 1: return test1();
  135. case 2: return test2();
  136. case 3: return test3();
  137. case 4: return test4();
  138. }
  139. fprintf(stderr, "Invalid test number %d.\n", n);
  140. return 1;
  141. }
  142. if(n <= 4)
  143. {
  144. int states[4] =
  145. {
  146. kwsysProcess_State_Exited,
  147. kwsysProcess_State_Exited,
  148. kwsysProcess_State_Expired,
  149. kwsysProcess_State_Exception
  150. };
  151. int exceptions[4] = {kwsysProcess_Exception_None, kwsysProcess_Exception_None,
  152. kwsysProcess_Exception_None, kwsysProcess_Exception_Fault};
  153. int values[4] = {0, 123, 1, 1};
  154. const char* cmd[4];
  155. cmd[0] = argv[0];
  156. cmd[1] = "run";
  157. cmd[2] = argv[1];
  158. cmd[3] = 0;
  159. return runChild(cmd, states[n-1], exceptions[n-1], values[n-1]);
  160. }
  161. else
  162. {
  163. return 0;
  164. }
  165. }