Files
AnyPS5/core/libs/tests/HostPerformanceProcess.cpp
T
MarcZX10 ad28df623d test(libkernel): check that exits release the process session and the environment is validated
Run the process session in a test executable linked with libc and leave
through LibcExit and LibcExitWithoutHandlers: the backend is released once
in both. Check that a refused request is never released, that off never
reaches the backend, that a second session is refused, that an unset
APS5_GAME_MODE is accepted and that APS5_GAME_MODE=bogus is rejected with
an error naming the variable.
2026-10-29 17:14:38 +00:00

88 lines
3.0 KiB
C++

#include "prx/libc/include/Shutdown.hpp"
#include "prx/libkernel/System/include/HostPerformance.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <stdexcept>
#include <string>
namespace {
using namespace HostPerformance;
class PrintingBackend final : public IBackend {
public:
explicit PrintingBackend(Outcome outcome) : outcome(outcome) {}
const char* Name() const override { return "printing"; }
BeginResult Begin() override {
std::puts("begin called");
return {outcome, "scripted"};
}
void End() override {
std::puts("release ran at exit");
std::fflush(stdout);
}
private:
Outcome outcome;
};
int Fail(const char* message) {
std::fprintf(stderr, "host performance process: %s\n", message);
return 1;
}
}
int main(int argc, char** argv) {
if (argc != 2) return Fail("usage: host_performance_process_tests <case>");
const std::string name = argv[1];
if (name == "exit" || name == "exit-without-handlers") {
if (StartProcessSession(Mode::On, std::make_unique<PrintingBackend>(Outcome::Started)) == nullptr) return Fail("an on session must exist");
std::fflush(stdout);
if (name == "exit") LibcExit_nid_no_patch(0);
LibcExitWithoutHandlers_nid_no_patch(0);
}
if (name == "refused") {
if (StartProcessSession(Mode::Auto, std::make_unique<PrintingBackend>(Outcome::Refused)) == nullptr) return Fail("an auto session must exist");
std::fflush(stdout);
LibcExit_nid_no_patch(0);
}
if (name == "off") {
if (StartProcessSession(Mode::Off, std::make_unique<PrintingBackend>(Outcome::Started)) != nullptr) return Fail("an off session must not exist");
std::puts("off left the backend alone");
std::fflush(stdout);
LibcExit_nid_no_patch(0);
}
if (name == "twice") {
StartProcessSession(Mode::On, std::make_unique<PrintingBackend>(Outcome::Started));
try {
StartProcessSession(Mode::On, std::make_unique<PrintingBackend>(Outcome::Started));
} catch (const std::logic_error&) {
std::puts("second session refused");
std::fflush(stdout);
LibcExit_nid_no_patch(0);
}
return Fail("a second session must be refused");
}
if (name == "environment") {
const char* value = std::getenv("APS5_GAME_MODE");
try {
StartFromEnvironment();
} catch (const std::invalid_argument& error) {
if (value != nullptr && std::strcmp(value, "bogus") == 0 && std::string(error.what()).find("APS5_GAME_MODE=bogus") != std::string::npos) {
std::puts("invalid value rejected by name");
return 0;
}
return Fail(error.what());
}
if (value != nullptr && std::strcmp(value, "bogus") == 0) return Fail("an invalid value must be rejected");
std::puts("environment accepted");
return 0;
}
return Fail("unknown case");
}