testUVProcessChain.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include <algorithm>
  2. #include <csignal>
  3. #include <functional>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <string>
  7. #include <vector>
  8. #include <cm/memory>
  9. #include <cm3p/uv.h>
  10. #include "cmGetPipes.h"
  11. #include "cmUVHandlePtr.h"
  12. #include "cmUVProcessChain.h"
  13. #include "cmUVStreambuf.h"
  14. struct ExpectedStatus
  15. {
  16. bool Finished;
  17. bool MatchExitStatus;
  18. bool MatchTermSignal;
  19. cmUVProcessChain::Status Status;
  20. };
  21. static const std::vector<ExpectedStatus> status1 = {
  22. { false, false, false, { 0, 0 } },
  23. { false, false, false, { 0, 0 } },
  24. { false, false, false, { 0, 0 } },
  25. };
  26. static const std::vector<ExpectedStatus> status2 = {
  27. { true, true, true, { 0, 0 } },
  28. { false, false, false, { 0, 0 } },
  29. { false, false, false, { 0, 0 } },
  30. };
  31. static const std::vector<ExpectedStatus> status3 = {
  32. { true, true, true, { 0, 0 } },
  33. { true, true, true, { 1, 0 } },
  34. #ifdef _WIN32
  35. { true, true, true, { 2, 0 } },
  36. #else
  37. { true, false, true, { 0, SIGABRT } },
  38. #endif
  39. };
  40. bool operator==(const cmUVProcessChain::Status* actual,
  41. const ExpectedStatus& expected)
  42. {
  43. if (!expected.Finished) {
  44. return !actual;
  45. } else if (!actual) {
  46. return false;
  47. }
  48. if (expected.MatchExitStatus &&
  49. expected.Status.ExitStatus != actual->ExitStatus) {
  50. return false;
  51. }
  52. if (expected.MatchTermSignal &&
  53. expected.Status.TermSignal != actual->TermSignal) {
  54. return false;
  55. }
  56. return true;
  57. }
  58. static bool resultsMatch(
  59. const std::vector<const cmUVProcessChain::Status*>& actual,
  60. const std::vector<ExpectedStatus>& expected)
  61. {
  62. return actual.size() == expected.size() &&
  63. std::equal(actual.begin(), actual.end(), expected.begin());
  64. }
  65. static std::string getInput(std::istream& input)
  66. {
  67. char buffer[1024];
  68. std::ostringstream str;
  69. do {
  70. input.read(buffer, 1024);
  71. str.write(buffer, input.gcount());
  72. } while (input.gcount() > 0);
  73. return str.str();
  74. }
  75. template <typename T>
  76. std::function<std::ostream&(std::ostream&)> printExpected(bool match,
  77. const T& value)
  78. {
  79. return [match, value](std::ostream& stream) -> std::ostream& {
  80. if (match) {
  81. stream << value;
  82. } else {
  83. stream << "*";
  84. }
  85. return stream;
  86. };
  87. }
  88. std::ostream& operator<<(
  89. std::ostream& stream,
  90. const std::function<std::ostream&(std::ostream&)>& func)
  91. {
  92. return func(stream);
  93. }
  94. static void printResults(
  95. const std::vector<const cmUVProcessChain::Status*>& actual,
  96. const std::vector<ExpectedStatus>& expected)
  97. {
  98. std::cout << "Expected: " << std::endl;
  99. for (auto const& e : expected) {
  100. if (e.Finished) {
  101. std::cout << " ExitStatus: "
  102. << printExpected(e.MatchExitStatus, e.Status.ExitStatus)
  103. << ", TermSignal: "
  104. << printExpected(e.MatchTermSignal, e.Status.TermSignal)
  105. << std::endl;
  106. } else {
  107. std::cout << " null" << std::endl;
  108. }
  109. }
  110. std::cout << "Actual:" << std::endl;
  111. for (auto const& a : actual) {
  112. if (a) {
  113. std::cout << " ExitStatus: " << a->ExitStatus
  114. << ", TermSignal: " << a->TermSignal << std::endl;
  115. } else {
  116. std::cout << " null" << std::endl;
  117. }
  118. }
  119. }
  120. static bool checkExecution(cmUVProcessChainBuilder& builder,
  121. std::unique_ptr<cmUVProcessChain>& chain)
  122. {
  123. std::vector<const cmUVProcessChain::Status*> status;
  124. chain = cm::make_unique<cmUVProcessChain>(builder.Start());
  125. if (!chain->Valid()) {
  126. std::cout << "Valid() returned false, should be true" << std::endl;
  127. return false;
  128. }
  129. status = chain->GetStatus();
  130. if (!resultsMatch(status, status1)) {
  131. std::cout << "GetStatus() did not produce expected output" << std::endl;
  132. printResults(status, status1);
  133. return false;
  134. }
  135. if (chain->Wait(6000)) {
  136. std::cout << "Wait() returned true, should be false" << std::endl;
  137. return false;
  138. }
  139. status = chain->GetStatus();
  140. if (!resultsMatch(status, status2)) {
  141. std::cout << "GetStatus() did not produce expected output" << std::endl;
  142. printResults(status, status2);
  143. return false;
  144. }
  145. if (!chain->Wait()) {
  146. std::cout << "Wait() returned false, should be true" << std::endl;
  147. return false;
  148. }
  149. status = chain->GetStatus();
  150. if (!resultsMatch(status, status3)) {
  151. std::cout << "GetStatus() did not produce expected output" << std::endl;
  152. printResults(status, status3);
  153. return false;
  154. }
  155. return true;
  156. }
  157. static bool checkOutput(std::istream& outputStream, std::istream& errorStream)
  158. {
  159. std::string output = getInput(outputStream);
  160. if (output != "HELO WRD!") {
  161. std::cout << "Output was \"" << output << "\", expected \"HELO WRD!\""
  162. << std::endl;
  163. return false;
  164. }
  165. std::string error = getInput(errorStream);
  166. auto qemu_error_pos = error.find("qemu:");
  167. if (qemu_error_pos != std::string::npos) {
  168. error.resize(qemu_error_pos);
  169. }
  170. if (error.length() != 3 || error.find('1') == std::string::npos ||
  171. error.find('2') == std::string::npos ||
  172. error.find('3') == std::string::npos) {
  173. std::cout << "Error was \"" << error << "\", expected \"123\""
  174. << std::endl;
  175. return false;
  176. }
  177. return true;
  178. }
  179. bool testUVProcessChainBuiltin(const char* helperCommand)
  180. {
  181. cmUVProcessChainBuilder builder;
  182. std::unique_ptr<cmUVProcessChain> chain;
  183. builder.AddCommand({ helperCommand, "echo" })
  184. .AddCommand({ helperCommand, "capitalize" })
  185. .AddCommand({ helperCommand, "dedup" })
  186. .SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
  187. .SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
  188. if (!checkExecution(builder, chain)) {
  189. return false;
  190. }
  191. if (!chain->OutputStream()) {
  192. std::cout << "OutputStream() was null, expecting not null" << std::endl;
  193. return false;
  194. }
  195. if (!chain->ErrorStream()) {
  196. std::cout << "ErrorStream() was null, expecting not null" << std::endl;
  197. return false;
  198. }
  199. if (!checkOutput(*chain->OutputStream(), *chain->ErrorStream())) {
  200. return false;
  201. }
  202. return true;
  203. }
  204. bool testUVProcessChainExternal(const char* helperCommand)
  205. {
  206. cmUVProcessChainBuilder builder;
  207. std::unique_ptr<cmUVProcessChain> chain;
  208. int outputPipe[2], errorPipe[2];
  209. cm::uv_pipe_ptr outputInPipe, outputOutPipe, errorInPipe, errorOutPipe;
  210. if (cmGetPipes(outputPipe) < 0) {
  211. std::cout << "Error creating pipes" << std::endl;
  212. return false;
  213. }
  214. if (cmGetPipes(errorPipe) < 0) {
  215. std::cout << "Error creating pipes" << std::endl;
  216. return false;
  217. }
  218. builder.AddCommand({ helperCommand, "echo" })
  219. .AddCommand({ helperCommand, "capitalize" })
  220. .AddCommand({ helperCommand, "dedup" })
  221. .SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, outputPipe[1])
  222. .SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, errorPipe[1]);
  223. if (!checkExecution(builder, chain)) {
  224. return false;
  225. }
  226. if (chain->OutputStream()) {
  227. std::cout << "OutputStream() was not null, expecting null" << std::endl;
  228. return false;
  229. }
  230. if (chain->ErrorStream()) {
  231. std::cout << "ErrorStream() was not null, expecting null" << std::endl;
  232. return false;
  233. }
  234. outputOutPipe.init(chain->GetLoop(), 0);
  235. uv_pipe_open(outputOutPipe, outputPipe[1]);
  236. outputOutPipe.reset();
  237. errorOutPipe.init(chain->GetLoop(), 0);
  238. uv_pipe_open(errorOutPipe, errorPipe[1]);
  239. errorOutPipe.reset();
  240. outputInPipe.init(chain->GetLoop(), 0);
  241. uv_pipe_open(outputInPipe, outputPipe[0]);
  242. cmUVStreambuf outputBuf;
  243. outputBuf.open(outputInPipe);
  244. std::istream outputStream(&outputBuf);
  245. errorInPipe.init(chain->GetLoop(), 0);
  246. uv_pipe_open(errorInPipe, errorPipe[0]);
  247. cmUVStreambuf errorBuf;
  248. errorBuf.open(errorInPipe);
  249. std::istream errorStream(&errorBuf);
  250. if (!checkOutput(outputStream, errorStream)) {
  251. return false;
  252. }
  253. return true;
  254. }
  255. bool testUVProcessChainNone(const char* helperCommand)
  256. {
  257. cmUVProcessChainBuilder builder;
  258. std::unique_ptr<cmUVProcessChain> chain;
  259. builder.AddCommand({ helperCommand, "echo" })
  260. .AddCommand({ helperCommand, "capitalize" })
  261. .AddCommand({ helperCommand, "dedup" });
  262. if (!checkExecution(builder, chain)) {
  263. return false;
  264. }
  265. if (chain->OutputStream()) {
  266. std::cout << "OutputStream() was not null, expecting null" << std::endl;
  267. return false;
  268. }
  269. if (chain->ErrorStream()) {
  270. std::cout << "ErrorStream() was not null, expecting null" << std::endl;
  271. return false;
  272. }
  273. return true;
  274. }
  275. int testUVProcessChain(int argc, char** const argv)
  276. {
  277. if (argc < 2) {
  278. std::cout << "Invalid arguments.\n";
  279. return -1;
  280. }
  281. if (!testUVProcessChainBuiltin(argv[1])) {
  282. std::cout << "While executing testUVProcessChainBuiltin().\n";
  283. return -1;
  284. }
  285. if (!testUVProcessChainExternal(argv[1])) {
  286. std::cout << "While executing testUVProcessChainExternal().\n";
  287. return -1;
  288. }
  289. if (!testUVProcessChainNone(argv[1])) {
  290. std::cout << "While executing testUVProcessChainNone().\n";
  291. return -1;
  292. }
  293. return 0;
  294. }