Files
onionHEN/source/util/tests/test_settings.cpp
T
Hexwell 56a4530a02 remove: drop legacy TCP 9028 jailbreak command server
Delete the util hijacker Command protocol, config toggle, and Toolbox
UI. Homebrew jailbreak remains on the daemon sandbox FIFO path only.
2026-07-16 23:16:55 +08:00

213 lines
7.2 KiB
C++

/* Host unit tests for libonion_settings schema (no PS5 SDK). */
#include "test_harness.h"
#include <onion/settings.hpp>
#include <cstdio>
#include <cstring>
#include <string>
#include <unistd.h>
static std::string temp_ini_path() {
char tmpl[] = "/tmp/onion-settings-XXXXXX.ini";
/* mkstemps needs suffix length 4 for ".ini" */
int fd = mkstemps(tmpl, 4);
if (fd < 0) {
return {};
}
close(fd);
unlink(tmpl); /* write fresh via settings_save_file */
return std::string(tmpl);
}
static int test_defaults_and_serialize_keys(void) {
onion::Settings s{};
std::string text = onion::settings_serialize(s);
TEST_ASSERT_TRUE(text.find("[Settings]") != std::string::npos);
TEST_ASSERT_TRUE(text.find("schema_version=1") != std::string::npos);
TEST_ASSERT_TRUE(text.find("fan_threshold=77") != std::string::npos);
TEST_ASSERT_TRUE(text.find("Util_rest_kill=0") != std::string::npos);
TEST_ASSERT_TRUE(text.find("Overlay_pos=0") != std::string::npos);
TEST_ASSERT_TRUE(text.find("ui_lang=0") != std::string::npos);
return 0;
}
static int test_roundtrip_file(void) {
std::string path = temp_ini_path();
TEST_ASSERT_TRUE(!path.empty());
onion::Settings in{};
in.fan_threshold = 55;
in.cheats_shortcut_opt = 2;
in.rest_mode_delay_seconds = 7;
in.ui_lang = 1;
TEST_ASSERT_TRUE(onion::settings_save_file(path.c_str(), in));
onion::Settings out{};
TEST_ASSERT_TRUE(onion::settings_load_file(path.c_str(), &out));
TEST_ASSERT_EQ_INT(55, out.fan_threshold);
TEST_ASSERT_EQ_INT(2, out.cheats_shortcut_opt);
TEST_ASSERT_EQ_U64(7, out.rest_mode_delay_seconds);
TEST_ASSERT_EQ_INT(1, out.ui_lang);
TEST_ASSERT_EQ_INT(onion::kSettingsSchemaVersion, out.schema_version);
unlink(path.c_str());
return 0;
}
static int test_missing_file_defaults(void) {
onion::Settings out{};
out.fan_threshold = 1;
TEST_ASSERT_TRUE(
!onion::settings_load_file("/tmp/onion-settings-does-not-exist-xyz.ini",
&out));
/* load_file resets to defaults even on failure */
TEST_ASSERT_EQ_INT(77, out.fan_threshold);
return 0;
}
static int test_full_schema_roundtrip(void) {
std::string path = temp_ini_path();
TEST_ASSERT_TRUE(!path.empty());
onion::Settings in{};
in.util_rest_kill = true;
in.game_rest_kill = true;
in.rest_mode_delay_seconds = 42;
in.libhijacker_cheats = true;
in.debug_app_jb_msg = true;
in.display_tids = true;
in.onionhen_game_opts = false;
in.enable_fan_speed = true;
in.fan_threshold = 90;
in.overlay_ram = false;
in.overlay_cpu = false;
in.overlay_gpu = false;
in.overlay_ip = true;
in.overlay_pos = 3;
in.cheats_shortcut_opt = 4;
in.toolbox_shortcut_opt = 2;
TEST_ASSERT_TRUE(onion::settings_save_file(path.c_str(), in));
onion::Settings out{};
TEST_ASSERT_TRUE(onion::settings_load_file(path.c_str(), &out));
TEST_ASSERT_TRUE(out.util_rest_kill == in.util_rest_kill);
TEST_ASSERT_TRUE(out.game_rest_kill == in.game_rest_kill);
TEST_ASSERT_EQ_U64(in.rest_mode_delay_seconds, out.rest_mode_delay_seconds);
TEST_ASSERT_TRUE(out.libhijacker_cheats == in.libhijacker_cheats);
TEST_ASSERT_TRUE(out.debug_app_jb_msg == in.debug_app_jb_msg);
TEST_ASSERT_TRUE(out.display_tids == in.display_tids);
TEST_ASSERT_TRUE(out.onionhen_game_opts == in.onionhen_game_opts);
TEST_ASSERT_TRUE(out.enable_fan_speed == in.enable_fan_speed);
TEST_ASSERT_EQ_INT(in.fan_threshold, out.fan_threshold);
TEST_ASSERT_TRUE(out.overlay_ram == in.overlay_ram);
TEST_ASSERT_TRUE(out.overlay_cpu == in.overlay_cpu);
TEST_ASSERT_TRUE(out.overlay_gpu == in.overlay_gpu);
TEST_ASSERT_TRUE(out.overlay_ip == in.overlay_ip);
TEST_ASSERT_EQ_INT(in.overlay_pos, out.overlay_pos);
TEST_ASSERT_EQ_INT(in.cheats_shortcut_opt, out.cheats_shortcut_opt);
TEST_ASSERT_EQ_INT(in.toolbox_shortcut_opt, out.toolbox_shortcut_opt);
unlink(path.c_str());
return 0;
}
static int test_partial_ini_keeps_defaults(void) {
std::string path = temp_ini_path();
TEST_ASSERT_TRUE(!path.empty());
FILE *f = fopen(path.c_str(), "w");
TEST_ASSERT_TRUE(f != nullptr);
fputs("[Settings]\nUtil_rest_kill=1\n", f);
fclose(f);
onion::Settings out{};
TEST_ASSERT_TRUE(onion::settings_load_file(path.c_str(), &out));
/* specified key applied; unspecified keys stay at defaults */
TEST_ASSERT_TRUE(out.util_rest_kill == true);
TEST_ASSERT_EQ_INT(77, out.fan_threshold);
unlink(path.c_str());
return 0;
}
static int test_serialize_contains_overlay_keys(void) {
onion::Settings s{};
s.overlay_ip = true;
s.overlay_pos = 2;
std::string text = onion::settings_serialize(s);
TEST_ASSERT_TRUE(text.find("overlay_fps=") == std::string::npos);
TEST_ASSERT_TRUE(text.find("overlay_ip=1") != std::string::npos);
TEST_ASSERT_TRUE(text.find("Overlay_pos=2") != std::string::npos);
return 0;
}
static int test_empty_file_loads_defaults(void) {
std::string path = temp_ini_path();
TEST_ASSERT_TRUE(!path.empty());
FILE *f = fopen(path.c_str(), "w");
TEST_ASSERT_TRUE(f != nullptr);
fclose(f);
onion::Settings out{};
/* empty file: load may succeed or fall back — either way defaults applied */
(void)onion::settings_load_file(path.c_str(), &out);
TEST_ASSERT_EQ_INT(77, out.fan_threshold);
unlink(path.c_str());
return 0;
}
static int test_settings_store_snapshot_update(void) {
onion::SettingsStore store;
onion::Settings a = store.snapshot();
TEST_ASSERT_EQ_INT(77, a.fan_threshold);
store.update([](onion::Settings &s) {
s.fan_threshold = 60;
s.enable_fan_speed = true;
});
onion::Settings b = store.snapshot();
TEST_ASSERT_EQ_INT(60, b.fan_threshold);
TEST_ASSERT_TRUE(b.enable_fan_speed);
onion::Settings c{};
c.overlay_ip = true;
store.store(c);
onion::Settings d = store.snapshot();
TEST_ASSERT_TRUE(d.overlay_ip);
TEST_ASSERT_TRUE(d.enable_fan_speed == false); /* full replace */
return 0;
}
static int test_config_mtime_helpers(void) {
/* No twin paths on host test machine → newest is 0, not newer. */
TEST_ASSERT_TRUE(onion::settings_config_newest_mtime() == 0 ||
onion::settings_config_newest_mtime() >= 0);
const time_t n = onion::settings_config_newest_mtime();
if (n == 0) {
TEST_ASSERT_TRUE(!onion::settings_config_is_newer_than(0));
} else {
TEST_ASSERT_TRUE(onion::settings_config_is_newer_than(n - 1));
TEST_ASSERT_TRUE(!onion::settings_config_is_newer_than(n));
}
return 0;
}
extern "C" int test_settings_suite(void) {
int failures = 0;
failures += onion_test_run("settings_defaults_serialize", test_defaults_and_serialize_keys);
failures += onion_test_run("settings_roundtrip_file", test_roundtrip_file);
failures += onion_test_run("settings_missing_file_defaults", test_missing_file_defaults);
failures += onion_test_run("settings_full_schema_roundtrip", test_full_schema_roundtrip);
failures += onion_test_run("settings_partial_ini_defaults", test_partial_ini_keeps_defaults);
failures += onion_test_run("settings_serialize_overlay_keys", test_serialize_contains_overlay_keys);
failures += onion_test_run("settings_empty_file_defaults", test_empty_file_loads_defaults);
failures += onion_test_run("settings_store_snapshot_update",
test_settings_store_snapshot_update);
failures += onion_test_run("settings_config_mtime_helpers", test_config_mtime_helpers);
return failures;
}