acceptance.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <windows.h>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cwchar>
  6. #include <string>
  7. #include <stdexcept>
  8. #include <vector>
  9. // 原生验收同时可在 Windows 和 Wine 执行;Wine 结果不能代替 Windows 实机验收。
  10. static void check(bool ok, const char* message) {
  11. if (!ok) throw std::runtime_error(std::string(message) + ": " + std::to_string(GetLastError()));
  12. }
  13. static std::wstring self() {
  14. wchar_t path[32768];
  15. check(GetModuleFileNameW(nullptr, path, 32768) != 0, "self path");
  16. return path;
  17. }
  18. static PROCESS_INFORMATION launch(std::wstring command, HANDLE input = INVALID_HANDLE_VALUE, HANDLE output = INVALID_HANDLE_VALUE) {
  19. STARTUPINFOW si{};
  20. si.cb = sizeof(si);
  21. if (input != INVALID_HANDLE_VALUE) {
  22. si.dwFlags = STARTF_USESTDHANDLES;
  23. si.hStdInput = input;
  24. si.hStdOutput = output == INVALID_HANDLE_VALUE ? GetStdHandle(STD_OUTPUT_HANDLE) : output;
  25. si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
  26. }
  27. PROCESS_INFORMATION pi{};
  28. check(CreateProcessW(nullptr, command.data(), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi), "launch");
  29. CloseHandle(pi.hThread);
  30. return pi;
  31. }
  32. static std::string readLine(HANDLE pipe) {
  33. std::string result;
  34. for (;;) {
  35. char c; DWORD count;
  36. check(ReadFile(pipe, &c, 1, &count, nullptr) && count == 1, "control read");
  37. if (c == '\n') return result;
  38. result += c;
  39. check(result.size() < 1024, "bounded control frame");
  40. }
  41. }
  42. static void writeLine(HANDLE pipe, const char* line) {
  43. DWORD count;
  44. check(WriteFile(pipe, line, static_cast<DWORD>(strlen(line)), &count, nullptr), "control write");
  45. }
  46. int wmain(int argc, wchar_t** argv) {
  47. try {
  48. if (argc > 2 && std::wstring(argv[1]) == L"watch") {
  49. std::vector<HANDLE> processes;
  50. for (int i = 2; i < argc; ++i) {
  51. auto handle = OpenProcess(SYNCHRONIZE, FALSE, wcstoul(argv[i], nullptr, 10));
  52. check(handle != nullptr, "watch process identity"); processes.push_back(handle);
  53. }
  54. std::puts("watching"); std::fflush(stdout);
  55. for (auto handle : processes) {
  56. check(WaitForSingleObject(handle, 4000) == WAIT_OBJECT_0, "Host loss must terminate owned process");
  57. CloseHandle(handle);
  58. }
  59. return 0;
  60. }
  61. if (argc > 1 && std::wstring(argv[1]) == L"fixture") {
  62. const std::wstring mode = argv[2];
  63. if (mode == L"descendant") { Sleep(INFINITE); return 0; }
  64. if (mode == L"tree" || mode == L"root-exit") {
  65. auto pi = launch(L"\"" + self() + L"\" fixture descendant");
  66. // 独立进程句柄由验收父进程打开;检查 Job 清空不能只检查根退出。
  67. std::printf("descendant %lu\n", pi.dwProcessId); std::fflush(stdout);
  68. CloseHandle(pi.hProcess);
  69. if (mode == L"root-exit") {
  70. char c; DWORD count;
  71. while (ReadFile(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &count, nullptr) && count) {}
  72. return 0;
  73. }
  74. }
  75. if (mode == L"graceful") {
  76. char buffer[128]; DWORD count;
  77. while (ReadFile(GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &count, nullptr) && count) {}
  78. Sleep(1450);
  79. return 0;
  80. }
  81. Sleep(INFINITE); return 0;
  82. }
  83. check(argc == 2, "usage: acceptance.exe helper.exe");
  84. struct UnrelatedProcess {
  85. HANDLE handle;
  86. ~UnrelatedProcess() { TerminateProcess(handle, 0); WaitForSingleObject(handle, 1000); CloseHandle(handle); }
  87. } unrelated{launch(L"\"" + self() + L"\" fixture descendant").hProcess};
  88. for (const auto mode : {L"graceful", L"tree", L"root-exit", L"control-loss", L"helper-loss", L"cancel", L"ownership-failure"}) {
  89. const auto name = L"\\\\.\\pipe\\zcode-job-acceptance-" + std::to_wstring(GetCurrentProcessId()) + L"-" + mode;
  90. HANDLE control = CreateNamedPipeW(name.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_WAIT, 1, 4096, 4096, 0, nullptr);
  91. check(control != INVALID_HANDLE_VALUE, "create control");
  92. SECURITY_ATTRIBUTES sa{sizeof(sa), nullptr, TRUE};
  93. HANDLE input, writer, outputReader, output;
  94. check(CreatePipe(&outputReader, &output, &sa, 0), "stdout pipe");
  95. SetHandleInformation(outputReader, HANDLE_FLAG_INHERIT, 0);
  96. check(CreatePipe(&input, &writer, &sa, 0), "stdin pipe");
  97. SetHandleInformation(writer, HANDLE_FLAG_INHERIT, 0);
  98. const std::wstring fixture = std::wstring(mode) == L"graceful" ? L"graceful" : std::wstring(mode) == L"root-exit" ? L"root-exit" : L"tree";
  99. const auto jobName = L"Local\\zcode-job-test-" + std::to_wstring(GetCurrentProcessId()) + L"-" + mode;
  100. HANDLE conflictingJob = std::wstring(mode) == L"ownership-failure" ? CreateJobObjectW(nullptr, jobName.c_str()) : nullptr;
  101. auto pi = launch(L"\"" + std::wstring(argv[1]) + L"\" " + name + L" " + std::to_wstring(GetCurrentProcessId()) + L" " + jobName + L" \"" + self() + L"\" fixture " + fixture, input, output);
  102. CloseHandle(input);
  103. CloseHandle(output);
  104. check(ConnectNamedPipe(control, nullptr) || GetLastError() == ERROR_PIPE_CONNECTED, "connect control");
  105. if (conflictingJob) {
  106. check(WaitForSingleObject(pi.hProcess, 2000) == WAIT_OBJECT_0, "reject Job ownership failure");
  107. DWORD code; check(GetExitCodeProcess(pi.hProcess, &code) && code != 0, "Job failure must fail startup");
  108. DWORD available = 0;
  109. const BOOL readable = PeekNamedPipe(outputReader, nullptr, 0, nullptr, &available, nullptr);
  110. check((readable && available == 0) || (!readable && GetLastError() == ERROR_BROKEN_PIPE), "failed ownership must not execute CLI");
  111. CloseHandle(conflictingJob); CloseHandle(pi.hProcess); CloseHandle(control); CloseHandle(writer); CloseHandle(outputReader);
  112. std::puts("PASS ownership failure rejects CLI execution"); continue;
  113. }
  114. const auto ready = readLine(control);
  115. check(ready.rfind("ready ", 0) == 0, "ready");
  116. const DWORD rootPid = std::stoul(ready.substr(6));
  117. HANDLE root = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, rootPid);
  118. check(root != nullptr, "root handle");
  119. DWORD available = 0;
  120. check(PeekNamedPipe(outputReader, nullptr, 0, nullptr, &available, nullptr) && available == 0, "CLI must stay suspended before start");
  121. HANDLE descendant = nullptr;
  122. if (std::wstring(mode) != L"cancel") {
  123. writeLine(control, "start\n");
  124. check(readLine(control) == "started", "started");
  125. if (fixture != L"graceful") {
  126. const auto line = readLine(outputReader);
  127. check(line.rfind("descendant ", 0) == 0, "descendant identity");
  128. descendant = OpenProcess(SYNCHRONIZE, FALSE, std::stoul(line.substr(11)));
  129. check(descendant != nullptr, "capture descendant handle before cleanup");
  130. }
  131. }
  132. const auto begin = GetTickCount64();
  133. if (std::wstring(mode) == L"control-loss" || std::wstring(mode) == L"cancel") {
  134. CloseHandle(control); control = INVALID_HANDLE_VALUE;
  135. } else if (std::wstring(mode) == L"helper-loss") {
  136. check(TerminateProcess(pi.hProcess, 91), "kill helper");
  137. } else {
  138. CloseHandle(writer); writer = INVALID_HANDLE_VALUE;
  139. if (std::wstring(mode) != L"root-exit") writeLine(control, "close\n");
  140. std::string line;
  141. do {
  142. line = readLine(control);
  143. if (std::wstring(mode) == L"graceful" && line.rfind("root ", 0) == 0)
  144. check(line == "root 0", "graceful root was forcibly killed");
  145. } while (line != "empty");
  146. }
  147. if (descendant) { check(WaitForSingleObject(descendant, 4000) == WAIT_OBJECT_0, "descendant must exit"); CloseHandle(descendant); }
  148. check(WaitForSingleObject(root, 4000) == WAIT_OBJECT_0, "root must exit");
  149. check(WaitForSingleObject(pi.hProcess, 4000) == WAIT_OBJECT_0, "helper must exit");
  150. if (std::wstring(mode) == L"graceful") check(GetTickCount64() - begin >= 1400, "full graceful window");
  151. auto probe = launch(L"\"" + std::wstring(argv[1]) + L"\" --check-empty " + jobName);
  152. check(WaitForSingleObject(probe.hProcess, 4000) == WAIT_OBJECT_0, "Job probe exits");
  153. DWORD probeCode; check(GetExitCodeProcess(probe.hProcess, &probeCode) && probeCode == 0, "kernel Job empty probe");
  154. CloseHandle(probe.hProcess);
  155. check(WaitForSingleObject(unrelated.handle, 0) == WAIT_TIMEOUT, "unrelated process must survive Job cleanup");
  156. std::printf("PASS %ls elapsed=%llu ms\n", mode, GetTickCount64() - begin);
  157. if (writer != INVALID_HANDLE_VALUE) CloseHandle(writer);
  158. if (control != INVALID_HANDLE_VALUE) CloseHandle(control);
  159. CloseHandle(root); CloseHandle(pi.hProcess); CloseHandle(outputReader);
  160. }
  161. return 0;
  162. } catch (const std::exception& error) { std::fprintf(stderr, "FAIL %s\n", error.what()); return 1; }
  163. }