ProcessTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // ProcessTest.cpp
  3. //
  4. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  5. // and Contributors.
  6. //
  7. // SPDX-License-Identifier: BSL-1.0
  8. //
  9. #include "ProcessTest.h"
  10. #include "CppUnit/TestCaller.h"
  11. #include "CppUnit/TestSuite.h"
  12. #include "Poco/Process.h"
  13. #include "Poco/Pipe.h"
  14. #include "Poco/PipeStream.h"
  15. using Poco::Process;
  16. using Poco::ProcessHandle;
  17. using Poco::Pipe;
  18. using Poco::PipeInputStream;
  19. using Poco::PipeOutputStream;
  20. ProcessTest::ProcessTest(const std::string& name): CppUnit::TestCase(name)
  21. {
  22. }
  23. ProcessTest::~ProcessTest()
  24. {
  25. }
  26. void ProcessTest::testLaunch()
  27. {
  28. std::string name("TestApp");
  29. std::string cmd;
  30. #if defined(_DEBUG) && (POCO_OS != POCO_OS_ANDROID)
  31. name += "d";
  32. #endif
  33. #if defined(POCO_OS_FAMILY_UNIX)
  34. cmd = "./";
  35. cmd += name;
  36. #elif defined(_WIN32_WCE)
  37. cmd = "\\";
  38. cmd += name;
  39. cmd += ".EXE";
  40. #else
  41. cmd = name;
  42. #endif
  43. std::vector<std::string> args;
  44. args.push_back("arg1");
  45. args.push_back("arg2");
  46. args.push_back("arg3");
  47. ProcessHandle ph = Process::launch(cmd, args);
  48. int rc = ph.wait();
  49. assertTrue (rc == 3);
  50. }
  51. void ProcessTest::testLaunchRedirectIn()
  52. {
  53. #if !defined(_WIN32_WCE)
  54. std::string name("TestApp");
  55. std::string cmd;
  56. #if defined(_DEBUG) && (POCO_OS != POCO_OS_ANDROID)
  57. name += "d";
  58. #endif
  59. #if defined(POCO_OS_FAMILY_UNIX)
  60. cmd = "./";
  61. cmd += name;
  62. #else
  63. cmd = name;
  64. #endif
  65. std::vector<std::string> args;
  66. args.push_back("-count");
  67. Pipe inPipe;
  68. ProcessHandle ph = Process::launch(cmd, args, &inPipe, 0, 0);
  69. PipeOutputStream ostr(inPipe);
  70. ostr << std::string(100, 'x');
  71. ostr.close();
  72. int rc = ph.wait();
  73. assertTrue (rc == 100);
  74. #endif // !defined(_WIN32_WCE)
  75. }
  76. void ProcessTest::testLaunchRedirectOut()
  77. {
  78. #if !defined(_WIN32_WCE)
  79. std::string name("TestApp");
  80. std::string cmd;
  81. #if defined(_DEBUG) && (POCO_OS != POCO_OS_ANDROID)
  82. name += "d";
  83. #endif
  84. #if defined(POCO_OS_FAMILY_UNIX)
  85. cmd = "./";
  86. cmd += name;
  87. #else
  88. cmd = name;
  89. #endif
  90. std::vector<std::string> args;
  91. args.push_back("-hello");
  92. Pipe outPipe;
  93. ProcessHandle ph = Process::launch(cmd, args, 0, &outPipe, 0);
  94. PipeInputStream istr(outPipe);
  95. std::string s;
  96. int c = istr.get();
  97. while (c != -1) { s += (char) c; c = istr.get(); }
  98. assertTrue (s == "Hello, world!");
  99. int rc = ph.wait();
  100. assertTrue (rc == 1);
  101. #endif // !defined(_WIN32_WCE)
  102. }
  103. void ProcessTest::testLaunchEnv()
  104. {
  105. #if !defined(_WIN32_WCE)
  106. std::string name("TestApp");
  107. std::string cmd;
  108. #if defined(_DEBUG) && (POCO_OS != POCO_OS_ANDROID)
  109. name += "d";
  110. #endif
  111. #if defined(POCO_OS_FAMILY_UNIX)
  112. cmd = "./";
  113. cmd += name;
  114. #else
  115. cmd = name;
  116. #endif
  117. std::vector<std::string> args;
  118. args.push_back("-env");
  119. Pipe outPipe;
  120. Process::Env env;
  121. env["TESTENV"] = "test";
  122. ProcessHandle ph = Process::launch(cmd, args, 0, &outPipe, 0, env);
  123. PipeInputStream istr(outPipe);
  124. std::string s;
  125. int c = istr.get();
  126. while (c != -1) { s += (char) c; c = istr.get(); }
  127. assertTrue (s == "test");
  128. int rc = ph.wait();
  129. assertTrue (rc == 0);
  130. #endif // !defined(_WIN32_WCE)
  131. }
  132. void ProcessTest::testLaunchArgs()
  133. {
  134. #if defined (_WIN32) && !defined(_WIN32_WCE)
  135. std::string name("TestApp");
  136. std::string cmd = name;
  137. std::vector<std::string> args;
  138. args.push_back("-echo-args");
  139. args.push_back("simple");
  140. args.push_back("with space");
  141. args.push_back("with\ttab");
  142. args.push_back("with\vverticaltab");
  143. // can't test newline here because TestApp -echo-args uses newline to separate the echoed args
  144. //args.push_back("with\nnewline");
  145. args.push_back("with \" quotes");
  146. args.push_back("ends with \"quotes\"");
  147. args.push_back("\"starts\" with quotes");
  148. args.push_back("\"");
  149. args.push_back("\\");
  150. args.push_back("c:\\program files\\ends with backslash\\");
  151. args.push_back("\"already quoted \\\" \\\\\"");
  152. Pipe outPipe;
  153. ProcessHandle ph = Process::launch(cmd, args, 0, &outPipe, 0);
  154. PipeInputStream istr(outPipe);
  155. std::string receivedArg;
  156. int c = istr.get();
  157. int argNumber = 1;
  158. while (c != -1)
  159. {
  160. if ('\n' == c)
  161. {
  162. assertTrue (argNumber < args.size());
  163. std::string expectedArg = args[argNumber];
  164. if (expectedArg.npos != expectedArg.find("already quoted")) {
  165. expectedArg = "already quoted \" \\";
  166. }
  167. assertTrue (receivedArg == expectedArg);
  168. ++argNumber;
  169. receivedArg = "";
  170. }
  171. else if ('\r' != c)
  172. {
  173. receivedArg += (char)c;
  174. }
  175. c = istr.get();
  176. }
  177. assertTrue (argNumber == args.size());
  178. int rc = ph.wait();
  179. assertTrue (rc == args.size());
  180. #endif // !defined(_WIN32_WCE)
  181. }
  182. void ProcessTest::testIsRunning()
  183. {
  184. #if !defined(_WIN32_WCE)
  185. std::string name("TestApp");
  186. std::string cmd;
  187. #if defined(_DEBUG) && (POCO_OS != POCO_OS_ANDROID)
  188. name += "d";
  189. #endif
  190. #if defined(POCO_OS_FAMILY_UNIX)
  191. cmd = "./";
  192. cmd += name;
  193. #else
  194. cmd = name;
  195. #endif
  196. std::vector<std::string> args;
  197. args.push_back("-count");
  198. Pipe inPipe;
  199. ProcessHandle ph = Process::launch(cmd, args, &inPipe, 0, 0);
  200. Process::PID id = ph.id();
  201. assertTrue (Process::isRunning(ph));
  202. assertTrue (Process::isRunning(id));
  203. PipeOutputStream ostr(inPipe);
  204. ostr << std::string(100, 'x');
  205. ostr.close();
  206. int POCO_UNUSED rc = ph.wait();
  207. assertTrue (!Process::isRunning(ph));
  208. assertTrue (!Process::isRunning(id));
  209. #endif // !defined(_WIN32_WCE)
  210. }
  211. void ProcessTest::setUp()
  212. {
  213. }
  214. void ProcessTest::tearDown()
  215. {
  216. }
  217. CppUnit::Test* ProcessTest::suite()
  218. {
  219. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ProcessTest");
  220. CppUnit_addTest(pSuite, ProcessTest, testLaunch);
  221. CppUnit_addTest(pSuite, ProcessTest, testLaunchRedirectIn);
  222. CppUnit_addTest(pSuite, ProcessTest, testLaunchRedirectOut);
  223. CppUnit_addTest(pSuite, ProcessTest, testLaunchEnv);
  224. CppUnit_addTest(pSuite, ProcessTest, testLaunchArgs);
  225. CppUnit_addTest(pSuite, ProcessTest, testIsRunning);
  226. return pSuite;
  227. }