| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- #include <Windows.h>
- #include <array>
- #include <crtdbg.h>
- #include <cstdint>
- #include <cstdio>
- #include <string_view>
- #include "core/settings/settings.h"
- #include "state/account/inventory/item_state.h"
- namespace sunrise::core::log {
- Settings defaults() noexcept {
- return {};
- }
- void early(std::string_view) noexcept {}
- void write(Channel, Level, std::string_view) noexcept {}
- } // namespace sunrise::core::log
- namespace sunrise::state::activity::defaults {
- ActivityDefaults authored() noexcept {
- return {};
- }
- bool valid(const DefaultDestination&) noexcept {
- return true;
- }
- bool valid(const ActivityDefaults&) noexcept {
- return true;
- }
- } // namespace sunrise::state::activity::defaults
- namespace sunrise::state::entitlements {
- Table authored() noexcept {
- return {};
- }
- bool valid(const Entitlement&) noexcept {
- return true;
- }
- bool valid(const Table&) noexcept {
- return true;
- }
- } // namespace sunrise::state::entitlements
- namespace sunrise::state::account {
- bool valid_authored(const AccountState&) noexcept {
- return true;
- }
- } // namespace sunrise::state::account
- namespace sunrise::state::account::settings {
- bool valid(const AccountSettings&) noexcept {
- return true;
- }
- } // namespace sunrise::state::account::settings
- int failures{};
- namespace {
- void expect(bool value, const char* label) noexcept {
- if (!value) {
- std::fprintf(stderr, "FAIL %s\n", label);
- ++failures;
- }
- }
- bool parse_catalyst_policy(std::string_view json, bool& output) noexcept {
- sunrise::core::settings::Settings settings{};
- if (!sunrise::core::settings::parse(json, settings)) {
- return false;
- }
- output = settings.completeExoticCatalysts;
- return true;
- }
- void test_item_state_contract() noexcept {
- for (std::uint32_t flags = 0; flags <= 0x7U; ++flags) {
- expect(sunrise::state::account::inventory::valid_item_state(flags),
- "known item-state bits are valid");
- }
- expect(!sunrise::state::account::inventory::valid_item_state(0x8U),
- "unknown item-state bits are invalid");
- }
- void test_optional_catalyst_policy() noexcept {
- bool enabled = false;
- expect(sunrise::core::settings::kSettingsVersion == 8,
- "optional catalyst key keeps settings version 8");
- expect(parse_catalyst_policy(R"({"version":8})", enabled) && enabled,
- "version 8 without catalyst key keeps the true default");
- expect(parse_catalyst_policy(R"({"version":8,"complete_exotic_catalysts":false})", enabled)
- && !enabled,
- "version 8 accepts an explicit false catalyst policy");
- expect(
- !parse_catalyst_policy(
- R"({"version":8,"complete_exotic_catalysts":true,"complete_exotic_catalysts":false})",
- enabled),
- "duplicate catalyst policy is rejected");
- expect(!parse_catalyst_policy(R"({"version":8,"complete_exotic_catalysts":1})", enabled),
- "non-boolean catalyst policy is rejected");
- }
- void test_settings_file_round_trip() noexcept {
- constexpr std::string_view document = R"({"version":8,"complete_exotic_catalysts":false})";
- std::array<char, MAX_PATH + 1> directory{};
- std::array<char, MAX_PATH + 1> path{};
- const DWORD directoryLength =
- GetTempPathA(static_cast<DWORD>(directory.size()), directory.data());
- if (directoryLength == 0 || directoryLength >= directory.size()
- || GetTempFileNameA(directory.data(), "sun", 0, path.data()) == 0) {
- expect(false, "settings round trip creates a temporary file");
- return;
- }
- HANDLE file = CreateFileA(
- path.data(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, nullptr);
- DWORD written = 0;
- const bool saved =
- file != INVALID_HANDLE_VALUE
- && WriteFile(file, document.data(), static_cast<DWORD>(document.size()), &written, nullptr)
- && written == document.size();
- if (file != INVALID_HANDLE_VALUE) {
- CloseHandle(file);
- }
- std::array<char, 128> reloaded{};
- file = CreateFileA(path.data(),
- GENERIC_READ,
- FILE_SHARE_READ,
- nullptr,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- nullptr);
- DWORD read = 0;
- const bool loaded =
- file != INVALID_HANDLE_VALUE
- && ReadFile(file, reloaded.data(), static_cast<DWORD>(reloaded.size()), &read, nullptr)
- && read == document.size();
- if (file != INVALID_HANDLE_VALUE) {
- CloseHandle(file);
- }
- DeleteFileA(path.data());
- bool enabled = true;
- expect(saved && loaded
- && parse_catalyst_policy(std::string_view{reloaded.data(), read}, enabled)
- && !enabled,
- "settings file round trip preserves the catalyst policy");
- }
- } // namespace
- void test_exotic_catalysts() noexcept;
- void test_resolved_catalyst_output() noexcept;
- void test_opcode406_item_state() noexcept;
- int main() {
- #if defined(_DEBUG)
- _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
- _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
- #endif
- test_item_state_contract();
- test_optional_catalyst_policy();
- test_settings_file_round_trip();
- test_opcode406_item_state();
- test_exotic_catalysts();
- test_resolved_catalyst_output();
- if (failures != 0) {
- std::fprintf(stderr, "%d regression test(s) failed\n", failures);
- return 1;
- }
- std::puts("All regression tests passed");
- return 0;
- }
|