testProcess.c 5.1 KB

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