Files
nt-lite/code/user/tests/realpath_smoke/realpath_smoke.c
T

78 lines
2.8 KiB
C

// Userspace realpath smoke test.
// Verifies native resource-path canonicalization and size reporting.
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <ntlite/syscall.h>
#include <ntlite/path.h>
static void print(const char* s) {
SYS_WRITE_CONSOLE_REQUEST req = { sizeof(req), SysSvFromCstr(s) };
SysWriteConsole(&req);
}
static void exit_now(int code) {
while (true) SysExitProcess(code);
}
static void fail_status(const char* what, KSTATUS status) {
(void)status;
print("realpath_smoke: "); print(what); print("\n");
exit_now(1);
}
static void fail(const char* what) {
print("realpath_smoke: "); print(what); print("\n");
exit_now(1);
}
static void expect_realpath(const char* input, const char* expected) {
char out[SYS_PATH_MAX + 1u];
size_t required = 0;
KSTATUS status = NtlRealPath(SysSvFromCstr(input), out, sizeof(out), &required);
if (status != NTLSTATUS_SUCCESS) fail_status("realpath", status);
if (required != strlen(expected) + 1u) fail("required size mismatch");
if (strcmp(out, expected) != 0) fail("resolved path mismatch");
}
int main(void) {
print("realpath_smoke: start\n");
expect_realpath("/SystemRoot//foo///bar/./", "/SystemRoot/foo/bar");
char cwd[SYS_PATH_MAX + 1u];
size_t cwdRequired = 0;
KSTATUS status = NtlGetCurrentDirectory(cwd, sizeof(cwd), &cwdRequired);
if (status != NTLSTATUS_SUCCESS) fail_status("get cwd", status);
char expected[SYS_PATH_MAX + 1u];
size_t cwdLength = strlen(cwd);
if (cwdLength + sizeof("/missing_file") > sizeof(expected)) fail("expected buffer too small");
memcpy(expected, cwd, cwdLength);
memcpy(expected + cwdLength, "/missing_file", sizeof("/missing_file"));
expect_realpath("missing_realpath_dir/../missing_file", expected);
size_t required = 0;
status = NtlRealPath(SYS_SV_LITERAL("/SystemRoot/system/init.exe"), (char*)0, 0, &required);
if (status != NTLSTATUS_SUCCESS || required != sizeof("/SystemRoot/system/init.exe"))
fail_status("size query", status);
char small[4];
size_t smallRequired = 0;
status = NtlRealPath(SYS_SV_LITERAL("/SystemRoot/system/init.exe"), small, sizeof(small), &smallRequired);
if (status != NTLSTATUS_BUFFER_TOO_SMALL || smallRequired != required)
fail_status("small buffer", status);
expect_realpath("/SystemRoot/foo/../bar", "/SystemRoot/bar");
status = NtlRealPath(SYS_SV_LITERAL("/SystemRoot/../../escape"), expected, sizeof(expected), (size_t*)0);
if (status != NTLSTATUS_ACCESS_DENIED) fail_status("resource-root traversal", status);
status = NtlRealPath(SYS_SV_LITERAL("/SystemRoot/foo\\bar"), expected, sizeof(expected), (size_t*)0);
if (status != NTLSTATUS_INVALID_PARAMETER) fail_status("native backslash", status);
print("realpath_smoke: PASS\n");
exit_now(0);
return 0;
}