Files
wine-access/tests/hook_exclusions_test.cpp
2026-11-03 00:04:28 -05:00

78 lines
3.9 KiB
C++

// SPDX-License-Identifier: LGPL-2.1-or-later
#include "../hook_exclusions.h"
#include <cstdio>
#include <vector>
static bool check(bool value, const char *message) {
if (!value) std::fprintf(stderr, "FAIL: %s\n", message);
return value;
}
int main() {
bool ok = true;
static WineAccessHookExclusions exclusions;
ok &= check(!wine_access_hook_exclusions_match(&exclusions, L"Game.exe"),
"empty list excludes nothing");
ok &= check(wine_access_hook_exclusions_add(&exclusions, L"Diablo IV.exe"),
"valid exclusion is stored");
ok &= check(wine_access_hook_exclusions_add(&exclusions, L"diablo iv.EXE"),
"duplicate exclusion is accepted");
ok &= check(exclusions.count == 1, "duplicate exclusion is stored once");
ok &= check(wine_access_hook_exclusions_match(&exclusions, L"Diablo IV.exe"),
"exclusion matches its file name");
ok &= check(wine_access_hook_exclusions_match(
&exclusions, L"C:\\Program Files\\Diablo IV\\Diablo IV.exe"),
"exclusion matches a Windows path by file name");
ok &= check(wine_access_hook_exclusions_match(
&exclusions, L"Z:/games/diablo iv.exe"),
"exclusion matches a forward-slash path by file name");
ok &= check(!wine_access_hook_exclusions_match(&exclusions, L"Diablo IV Launcher.exe"),
"exclusion does not match a different program");
ok &= check(!wine_access_hook_exclusions_match(&exclusions, nullptr),
"exclusion does not match a missing program");
ok &= check(!wine_access_hook_exclusions_add(&exclusions, L""),
"empty exclusion is rejected");
ok &= check(!wine_access_hook_exclusions_add(&exclusions, L"../Game.exe"),
"exclusion with a forward slash is rejected");
ok &= check(!wine_access_hook_exclusions_add(&exclusions, L"C:\\Game.exe"),
"exclusion with a backslash is rejected");
ok &= check(!wine_access_hook_exclusions_add(&exclusions, L" Game.exe"),
"exclusion with leading whitespace is rejected");
ok &= check(!wine_access_hook_exclusions_add(&exclusions, L"Game.exe "),
"exclusion with trailing whitespace is rejected");
ok &= check(!wine_access_hook_exclusions_add(&exclusions, L"Game\nEdition.exe"),
"exclusion containing the transport separator is rejected");
std::vector<WCHAR> oversized(WINE_ACCESS_MAX_HOOK_EXCLUSION_LENGTH + 2, L'a');
oversized.back() = 0;
ok &= check(!wine_access_hook_exclusions_add(&exclusions, oversized.data()),
"oversized exclusion is rejected");
oversized[WINE_ACCESS_MAX_HOOK_EXCLUSION_LENGTH] = 0;
ok &= check(wine_access_hook_exclusions_add(&exclusions, oversized.data()),
"maximum-size exclusion is accepted");
ok &= check(exclusions.count == 2,
"rejected exclusions do not change the stored count");
static WineAccessHookExclusions parsed;
const int added = wine_access_hook_exclusions_parse(
&parsed, L"One.exe\ntwo;edition.exe\nC:\\bad.exe\n\nTwo;Edition.EXE");
ok &= check(added == 2 && parsed.count == 2,
"newline list keeps valid unique exclusions");
ok &= check(wine_access_hook_exclusions_match(&parsed, L"ONE.EXE"),
"parsed exclusion matches its program");
ok &= check(wine_access_hook_exclusions_match(&parsed, L"TWO;EDITION.EXE"),
"parsed exclusion preserves a semicolon in its program name");
static WineAccessHookExclusions full;
WCHAR name[32];
bool filled = true;
for (int index = 0; index < WINE_ACCESS_MAX_HOOK_EXCLUSIONS; ++index) {
wsprintfW(name, L"game%d.exe", index);
filled &= wine_access_hook_exclusions_add(&full, name);
}
ok &= check(filled && full.count == WINE_ACCESS_MAX_HOOK_EXCLUSIONS,
"exclusion list fills to its bound");
ok &= check(!wine_access_hook_exclusions_add(&full, L"overflow.exe"),
"exclusion past the bound is rejected");
return ok ? 0 : 1;
}