123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #include <iostream>
- #include<thread>
- #include <cstring>
- #include <gtest/gtest.h>
- #include "process_monitor.h"
- #include "toolkit.h"
- #include "list.h"
- #ifndef _WIN32
- #include <winpr/thread.h>
- #include <winpr/file.h>
- #include <winpr/error.h>
- #include <winpr/library.h>
- #endif
- static const int process_create_max_count = 10;
- static const int process_parent_wait_max_time = 15;
- static int process_exit_count = 0;
- static char module_name[256] = { 0 };
- #ifdef _WIN32
- static char process_name[] = "single_proc.exe";
- #else
- static char process_name[] = "single_proc";
- #endif // _WIN32
- static int on_detect_process_end(process_monitor_t* monitor, tk_process_t* dead_process, void* user_data)
- {
- std::cout << "on_detect_process_end invoked! " << ++process_exit_count << std::endl;
- //here must return TRUE, or the handle would not be removed.
- return 1;
- }
- static int create_process_test(tk_process_t* new_process)
- {
- STARTUPINFOA si = { sizeof(STARTUPINFOA) };
- PROCESS_INFORMATION pi;
- HANDLE process = NULL;
- /*
- *The string can specify the full path and file name of the module to execute
- *or it can specify a partial name. In the case of a partial name, the function
- *uses the current drive and current directory to complete the specification.
- *The function will not use the search path. This parameter must include the file name extension;
- * no default extension is assumed.
- *
- * The lpApplicationName parameter can be NULL. In that case,
- * the module name must be the first white space¨Cdelimited token in the lpCommandLine string.
- */
- auto ret = CreateProcessA(module_name, NULL,
- NULL, NULL, FALSE, 0,
- NULL, NULL, &si, &pi);
- if (ret) {
- process = pi.hProcess;
- if (process == INVALID_HANDLE_VALUE)
- process = NULL;
- CloseHandle(pi.hThread);
- new_process->handle = process;
- new_process->pid = pi.dwProcessId;
- }
- if (process == NULL) {
- std::cerr << "create process failed, error: " << GetLastError() << std::endl;
- return -1;
- }
- return 0;
- }
- //TEST(TestProcessCreate, CreateProcessTest)
- //{
- // char path[MAX_PATH];
- // char app[MAX_PATH] = { '\0' };
- // char tmp[32];
- // tk_process_t* process1 = NULL;
- // tk_process_option_t option;
- //#ifdef _WIN32
- // sprintf(app, ".\\bin\\sphost.exe 254");
- //#else
- // sprintf(app, "./bin/sphost 254");
- //#endif //_WIN32
- // option.exit_cb = NULL;
- // option.file = NULL;
- // option.flags = 0;
- // option.params = app;
- // EXPECT_TRUE(process_spawn(&option, &process1) == 0);
- // EXPECT_TRUE(process1 != NULL);
- //}
- TEST(TestProcessMonitor, TestProcess)
- {
- char* pt = NULL;
- const auto args = ::testing::internal::GetArgvs();
- if (args.size() > 1) {
- std::string test_dir("");
- auto dir = args[1].find_last_of('/');
- if (dir == std::string::npos)
- dir = args[1].find_last_of('\\');
- if (dir != std::string::npos) {
- test_dir = args[1].substr(0, dir + 1);
- }
- else {
- test_dir = args[1];
- }
- strcpy(module_name, test_dir.c_str());
- }
- else {
- DWORD len = GetModuleFileNameA(NULL, module_name, sizeof(module_name));
- if ((pt = strrchr(module_name, '/')) == NULL) {
- pt = strrchr(module_name, '\\');
- }
- pt[1] = '\0';
- }
- strcat(module_name, process_name);
- std::cout << "single proc full path: " << module_name << std::endl;
- process_monitor_t* pm = NULL;
- ASSERT_TRUE(process_monitor_create(&pm) == 0);
- process_monitor_set_cb(pm, on_detect_process_end, NULL);
- ASSERT_TRUE(process_monitor_start(pm) == 0);
- /**/
- for (int i = 0; i < process_create_max_count; ++i) {
- std::cout << "create test process[" << i << "]" << std::endl;
- tk_process_t process;
- EXPECT_TRUE(create_process_test(&process) == 0);
- EXPECT_TRUE(0 == process_monitor_add(pm, &process));
- }
- const int elapse_sec = 1;
- const int count = process_parent_wait_max_time / elapse_sec + 1;
- int n = 0;
- while (process_exit_count < process_create_max_count && n++ < count) {
- std::this_thread::sleep_for(std::chrono::seconds(elapse_sec));
- }
- ASSERT_TRUE(process_create_max_count == process_exit_count);
-
- ASSERT_TRUE(process_monitor_stop(pm) == 0);
- process_monitor_destroy(pm);
- }
|