test_process.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <iostream>
  2. #include<thread>
  3. #include <cstring>
  4. #include <gtest/gtest.h>
  5. #include "process_monitor.h"
  6. #include "toolkit.h"
  7. #include "list.h"
  8. #ifndef _WIN32
  9. #include <winpr/thread.h>
  10. #include <winpr/file.h>
  11. #include <winpr/error.h>
  12. #include <winpr/library.h>
  13. #endif
  14. static const int process_create_max_count = 10;
  15. static const int process_parent_wait_max_time = 15;
  16. static int process_exit_count = 0;
  17. static char module_name[256] = { 0 };
  18. #ifdef _WIN32
  19. static char process_name[] = "single_proc.exe";
  20. #else
  21. static char process_name[] = "single_proc";
  22. #endif // _WIN32
  23. static int on_detect_process_end(process_monitor_t* monitor, tk_process_t* dead_process, void* user_data)
  24. {
  25. std::cout << "on_detect_process_end invoked! " << ++process_exit_count << std::endl;
  26. //here must return TRUE, or the handle would not be removed.
  27. return 1;
  28. }
  29. static int create_process_test(tk_process_t* new_process)
  30. {
  31. STARTUPINFOA si = { sizeof(STARTUPINFOA) };
  32. PROCESS_INFORMATION pi;
  33. HANDLE process = NULL;
  34. /*
  35. *The string can specify the full path and file name of the module to execute
  36. *or it can specify a partial name. In the case of a partial name, the function
  37. *uses the current drive and current directory to complete the specification.
  38. *The function will not use the search path. This parameter must include the file name extension;
  39. * no default extension is assumed.
  40. *
  41. * The lpApplicationName parameter can be NULL. In that case,
  42. * the module name must be the first white space¨Cdelimited token in the lpCommandLine string.
  43. */
  44. auto ret = CreateProcessA(module_name, NULL,
  45. NULL, NULL, FALSE, 0,
  46. NULL, NULL, &si, &pi);
  47. if (ret) {
  48. process = pi.hProcess;
  49. if (process == INVALID_HANDLE_VALUE)
  50. process = NULL;
  51. CloseHandle(pi.hThread);
  52. new_process->handle = process;
  53. new_process->pid = pi.dwProcessId;
  54. }
  55. if (process == NULL) {
  56. std::cerr << "create process failed, error: " << GetLastError() << std::endl;
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. //TEST(TestProcessCreate, CreateProcessTest)
  62. //{
  63. // char path[MAX_PATH];
  64. // char app[MAX_PATH] = { '\0' };
  65. // char tmp[32];
  66. // tk_process_t* process1 = NULL;
  67. // tk_process_option_t option;
  68. //#ifdef _WIN32
  69. // sprintf(app, ".\\bin\\sphost.exe 254");
  70. //#else
  71. // sprintf(app, "./bin/sphost 254");
  72. //#endif //_WIN32
  73. // option.exit_cb = NULL;
  74. // option.file = NULL;
  75. // option.flags = 0;
  76. // option.params = app;
  77. // EXPECT_TRUE(process_spawn(&option, &process1) == 0);
  78. // EXPECT_TRUE(process1 != NULL);
  79. //}
  80. TEST(TestProcessMonitor, TestProcess)
  81. {
  82. char* pt = NULL;
  83. const auto args = ::testing::internal::GetArgvs();
  84. if (args.size() > 1) {
  85. std::string test_dir("");
  86. auto dir = args[1].find_last_of('/');
  87. if (dir == std::string::npos)
  88. dir = args[1].find_last_of('\\');
  89. if (dir != std::string::npos) {
  90. test_dir = args[1].substr(0, dir + 1);
  91. }
  92. else {
  93. test_dir = args[1];
  94. }
  95. strcpy(module_name, test_dir.c_str());
  96. }
  97. else {
  98. DWORD len = GetModuleFileNameA(NULL, module_name, sizeof(module_name));
  99. if ((pt = strrchr(module_name, '/')) == NULL) {
  100. pt = strrchr(module_name, '\\');
  101. }
  102. pt[1] = '\0';
  103. }
  104. strcat(module_name, process_name);
  105. std::cout << "single proc full path: " << module_name << std::endl;
  106. process_monitor_t* pm = NULL;
  107. ASSERT_TRUE(process_monitor_create(&pm) == 0);
  108. process_monitor_set_cb(pm, on_detect_process_end, NULL);
  109. ASSERT_TRUE(process_monitor_start(pm) == 0);
  110. /**/
  111. for (int i = 0; i < process_create_max_count; ++i) {
  112. std::cout << "create test process[" << i << "]" << std::endl;
  113. tk_process_t process;
  114. EXPECT_TRUE(create_process_test(&process) == 0);
  115. EXPECT_TRUE(0 == process_monitor_add(pm, &process));
  116. }
  117. const int elapse_sec = 1;
  118. const int count = process_parent_wait_max_time / elapse_sec + 1;
  119. int n = 0;
  120. while (process_exit_count < process_create_max_count && n++ < count) {
  121. std::this_thread::sleep_for(std::chrono::seconds(elapse_sec));
  122. }
  123. ASSERT_TRUE(process_create_max_count == process_exit_count);
  124. ASSERT_TRUE(process_monitor_stop(pm) == 0);
  125. process_monitor_destroy(pm);
  126. }