Files
scriptc/packages/runtime/test/sha-kernels.test.ts
T
Chris Tate db94b2d257 Add fs.cpSync, fs.cp and fs/promises cp
fs.cpSync, fs.cp and fs/promises cp compile with every option:
dereference, errorOnExist, filter, force, mode, preserveTimestamps,
recursive and verbatimSymlinks. They follow Node's two implementations.
cpSync follows cp-sync.js over the fs binding: copy errors are plain
Errors with a code, std::error_code failures throw an ErrnoException,
and links inside a copied directory point at their canonical targets.
fs.cp and fs/promises cp follow cp.js, whose copy errors are
SystemErrors with code, info, errno, syscall and path.

Options and paths validate in Node's order with Node's errors. filter
runs for the top-level paths and each entry in Node's order; an async
filter must return a promise that is already settled, and a pending one
throws SC2020. The callback and promise forms complete the copy before
the callback runs or the promise settles. Windows builds throw SC2020.

The copy lives in the new scr_fs_cp.c. Directory walks keep their frames
on the heap, so deep trees also copy on a fiber's stack. File contents
copy with sendfile on Linux, and COPYFILE_FICLONE uses FICLONE
where it exists. ScrError gains the SystemError context and the
ErrnoException key order, with matching fields in the compiler's Error
layout. The stored fs.cp callback uses the same implementation.

Corpus 5567-5571 cover the sync, callback and promise forms, errors,
symlinks and 1500-level trees.
2026-10-16 21:06:22 -05:00

73 lines
1.9 KiB
TypeScript

import { execFile } from "node:child_process";
import { mkdir } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { beforeAll, expect, test } from "vitest";
const execFileAsync = promisify(execFile);
const testDir = import.meta.dirname;
const bin = join(testDir, "build", "test_sha_kernels");
// test_sha_kernels.c includes scr_lib.c, so it is not in this list.
const runtimeSources = [
"scr_shared.c",
"scr_bytes.c",
"scr_bytes.test.c",
"scr_bytes_io.c",
"scr_fs_cp.c",
"scr_zlib.c",
"scr_hash.c",
"scr_string.c",
"scr_array.c",
"scr_map.c",
"scr_exception.c",
"scr_error.c",
"scr_number.c",
"scr_console.c",
"scr_closure.c",
"scr_ffi.c",
"scr_object.c",
"scr_union.c",
"scr_cycle.c",
"scr_alloc.c",
"scr_json.c",
"scr_inspect.c",
"scr_bigint.c",
"scr_async.c",
"scr_async_dyn.c",
"scr_child.c",
"scr_path.c",
"scr_url.c",
"scr_url_params.c",
];
// Compiled once with ASan, UBSan and the RC audit. The test runs the SHA-NI and portable
// SHA-1/SHA-256 blocks over the same inputs and checks known vectors.
beforeAll(async () => {
await mkdir(join(testDir, "build"), { recursive: true });
await execFileAsync("clang", [
"-std=c11",
"-O1",
"-Wall",
"-Wextra",
"-Wno-missing-field-initializers",
"-fsanitize=address,undefined",
"-DSCR_RC_AUDIT",
"-I",
join(testDir, "../src"),
"-o",
bin,
join(testDir, "test_sha_kernels.c"),
...runtimeSources.map((source) => join(testDir, "../src", source)),
"-lz",
...(process.platform === "linux" ? ["-D_GNU_SOURCE", "-lm"] : []),
]);
}, 300_000);
test("SHA-1 and SHA-256: the SHA-NI blocks match the portable blocks", async () => {
const { stderr } = await execFileAsync(bin, [], {
env: { ...process.env, UBSAN_OPTIONS: "halt_on_error=1" },
});
expect(stderr.trim().split("\n").pop()).toMatch(/^(\d+)\/\1 cases passed$/);
});