178 lines
5.2 KiB
C
178 lines
5.2 KiB
C
// Realpath ABI smoke test.
|
|
// Verifies lexical canonicalization, size reporting, and root clamping.
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <ntlite/syscall.h>
|
|
|
|
// Prints one NUL-terminated string to the kernel console.
|
|
static void print(const char* s) {
|
|
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromCstr(s) };
|
|
SysWriteConsole(&req);
|
|
}
|
|
|
|
// Prints one unsigned integer to the kernel console.
|
|
static void print_u64(uint64_t v) {
|
|
char buf[32];
|
|
int i = 0;
|
|
if (v == 0) {
|
|
print("0");
|
|
return;
|
|
}
|
|
while (v && i < (int)sizeof(buf)) {
|
|
buf[i++] = (char)('0' + (v % 10));
|
|
v /= 10;
|
|
}
|
|
while (i--) {
|
|
char c = buf[i];
|
|
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromParts(&c, 1) };
|
|
SysWriteConsole(&req);
|
|
}
|
|
}
|
|
|
|
// Terminates the current test process.
|
|
static void exit_now(int code) {
|
|
while (true) SysExitProcess(code);
|
|
}
|
|
|
|
// Fails the test with one status value.
|
|
static void fail_status(const char* what, uint32_t status) {
|
|
print("realpath_smoke: ");
|
|
print(what);
|
|
print(" status=");
|
|
print_u64(status);
|
|
print("\n");
|
|
exit_now(1);
|
|
}
|
|
|
|
// Fails the test with one plain message.
|
|
static void fail(const char* what) {
|
|
print("realpath_smoke: ");
|
|
print(what);
|
|
print("\n");
|
|
exit_now(1);
|
|
}
|
|
|
|
// Appends one string to a fixed test buffer.
|
|
static void append(char* out, size_t cap, const char* suffix) {
|
|
size_t len = strlen(out);
|
|
size_t add = strlen(suffix);
|
|
if (len + add + 1 > cap) fail("test buffer too small");
|
|
memcpy(out + len, suffix, add + 1);
|
|
}
|
|
|
|
// Builds the current drive root path.
|
|
static void get_current_drive_root(char* out, size_t cap) {
|
|
char current[SYS_CWD_MAX];
|
|
size_t required = 0;
|
|
SYS_REAL_PATH_REQUEST req = {
|
|
sizeof(req),
|
|
SYS_REAL_PATH_FLAG_NONE,
|
|
SysSvFromCstr("."),
|
|
current,
|
|
sizeof(current),
|
|
&required,
|
|
};
|
|
uint32_t status = SysRealPath(&req);
|
|
if (status != NTLSTATUS_SUCCESS) fail_status("query current path", status);
|
|
if (required < 4 || current[0] < '0' || current[0] > '7' || current[1] != ':' || current[2] != '/')
|
|
fail("current path is not native drive-rooted");
|
|
if (cap < 4) fail("drive root output too small");
|
|
out[0] = current[0];
|
|
out[1] = ':';
|
|
out[2] = '/';
|
|
out[3] = '\0';
|
|
}
|
|
|
|
// Resolves a path and compares the exact native string.
|
|
static void expect_realpath(const char* input, const char* expected) {
|
|
char out[SYS_CWD_MAX];
|
|
size_t required = 0;
|
|
SYS_REAL_PATH_REQUEST req = {
|
|
sizeof(req),
|
|
SYS_REAL_PATH_FLAG_NONE,
|
|
SysSvFromCstr(input),
|
|
out,
|
|
sizeof(out),
|
|
&required,
|
|
};
|
|
uint32_t status = SysRealPath(&req);
|
|
if (status != NTLSTATUS_SUCCESS) fail_status("realpath", status);
|
|
if (required != strlen(expected) + 1) fail("required size mismatch");
|
|
if (strcmp(out, expected) != 0) fail("resolved path mismatch");
|
|
}
|
|
|
|
// Verifies size-only and too-small buffer behavior.
|
|
static void check_size_reporting(const char* input, const char* expected) {
|
|
size_t required = 0;
|
|
SYS_REAL_PATH_REQUEST query = {
|
|
sizeof(query),
|
|
SYS_REAL_PATH_FLAG_NONE,
|
|
SysSvFromCstr(input),
|
|
(char*)0,
|
|
0,
|
|
&required,
|
|
};
|
|
uint32_t status = SysRealPath(&query);
|
|
if (status != NTLSTATUS_SUCCESS) fail_status("size query", status);
|
|
if (required != strlen(expected) + 1) fail("size query required mismatch");
|
|
|
|
char small[4];
|
|
size_t smallRequired = 0;
|
|
SYS_REAL_PATH_REQUEST smallReq = {
|
|
sizeof(smallReq),
|
|
SYS_REAL_PATH_FLAG_NONE,
|
|
SysSvFromCstr(input),
|
|
small,
|
|
sizeof(small),
|
|
&smallRequired,
|
|
};
|
|
status = SysRealPath(&smallReq);
|
|
if (status != NTLSTATUS_BUFFER_TOO_SMALL) fail_status("small buffer", status);
|
|
if (smallRequired != required) fail("small required mismatch");
|
|
}
|
|
|
|
// Program entry point.
|
|
int main(void) {
|
|
print("realpath_smoke: start\n");
|
|
|
|
char root[4];
|
|
get_current_drive_root(root, sizeof(root));
|
|
|
|
char input[SYS_CWD_MAX];
|
|
char expected[SYS_CWD_MAX];
|
|
|
|
memcpy(input, root, sizeof(root));
|
|
append(input, sizeof(input), "//foo///bar\\baz/.");
|
|
memcpy(expected, root, sizeof(root));
|
|
append(expected, sizeof(expected), "foo/bar/baz");
|
|
expect_realpath(input, expected);
|
|
check_size_reporting(input, expected);
|
|
|
|
memcpy(input, root, sizeof(root));
|
|
append(input, sizeof(input), "../../root_escape");
|
|
memcpy(expected, root, sizeof(root));
|
|
append(expected, sizeof(expected), "root_escape");
|
|
expect_realpath(input, expected);
|
|
|
|
SYS_REAL_PATH_REQUEST currentReq = {
|
|
sizeof(currentReq),
|
|
SYS_REAL_PATH_FLAG_NONE,
|
|
SysSvFromCstr("."),
|
|
expected,
|
|
sizeof(expected),
|
|
(size_t*)0,
|
|
};
|
|
uint32_t status = SysRealPath(¤tReq);
|
|
if (status != NTLSTATUS_SUCCESS) fail_status("current realpath", status);
|
|
|
|
memcpy(input, "missing_realpath_dir/../missing_file", sizeof("missing_realpath_dir/../missing_file"));
|
|
append(expected, sizeof(expected), expected[strlen(expected) - 1] == '/' ? "missing_file" : "/missing_file");
|
|
expect_realpath(input, expected);
|
|
|
|
print("realpath_smoke: PASS\n");
|
|
exit_now(0);
|
|
return 0;
|
|
}
|