#include "stdafx.h" #include #include "UnitTestConfig.h" std::uint16_t Daemon::_iter = 0; Daemon::Daemon() { _i = _iter++; Cleanup(); } Daemon::~Daemon() { Stop(); Cleanup(); } BOOL Daemon::DirectoryExists(const String& path) { DWORD dwAttrib = GetFileAttributes(path.c_str()); return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); } void Daemon::Cleanup() { BOOL failed = TRUE; String path; path.resize(MAX_PATH + 1); GetFullPathName((L"\"..\\..\\..\\..\\3rd-party\\Sia-v1.1.0-windows-amd64\\data" + std::to_wstring(_i)).c_str(), MAX_PATH, &path[0], nullptr); while (failed) { PROCESS_INFORMATION pi2; STARTUPINFO si2; ZeroMemory(&si2, sizeof(si2)); si2.cb = sizeof(si2); ZeroMemory(&pi2, sizeof(pi2)); String szCmdline = L"cmd.exe /c del /s /q \"" + path + L"\""; CreateProcess(nullptr, &szCmdline[0], nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si2, &pi2); if (!(failed = (pi2.hProcess == 0))) { WaitForSingleObject(pi2.hProcess, INFINITE); CloseHandle(pi2.hProcess); CloseHandle(pi2.hThread); } ZeroMemory(&si2, sizeof(si2)); si2.cb = sizeof(si2); ZeroMemory(&pi2, sizeof(pi2)); szCmdline = L"cmd.exe /c rd /s /q \"" + path + L"\""; CreateProcess(nullptr, &szCmdline[0], nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si2, &pi2); if (!(failed = (pi2.hProcess == 0))) { WaitForSingleObject(pi2.hProcess, INFINITE); CloseHandle(pi2.hProcess); CloseHandle(pi2.hThread); } Sleep(100); } } void Daemon::Start() { String path; path.resize(MAX_PATH + 1); GetFullPathName(L"\"..\\..\\..\\..\\3rd-party\\Sia-v1.1.0-windows-amd64\\", MAX_PATH, &path[0], nullptr); String exec; exec.resize(MAX_PATH + 1); GetFullPathName(L"\"..\\..\\..\\..\\3rd-party\\Sia-v1.1.0-windows-amd64\\siad.exe", MAX_PATH, &exec[0], nullptr); String szCmdline = L" -d .\\data" + std::to_wstring(_i) + L" --api-addr " + String(TEST_SERVER_HOST) + L":" + std::to_wstring(TEST_SERVER_PORT) + L" --no-bootstrap"; CreateProcess(exec.c_str(), &szCmdline[0], nullptr, nullptr, FALSE, 0, nullptr, path.c_str(), &si, &pi); } void Daemon::Stop() { if (pi.hProcess) { TerminateProcess(pi.hProcess, -1); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); } }