Resolve instrument, year, and run identities using server-owned startup templates and data roots. Return exact filenames, relative destination paths, formats, sizes, and version tokens for desktop data-directory downloads. Stream binary files with explicit lengths and HTTP error responses. Cover current and historical layouts, HAL/HIFI naming, changed metadata, and path containment with integration tests, and document the API.
200 lines
11 KiB
JavaScript
200 lines
11 KiB
JavaScript
// Copyright (C) 2026 Zaher Salman
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Exercise the real Perl endpoint with temporary data trees. Inspect binary
|
|
// stdout as bytes so NULs, newlines, and invalid UTF-8 cannot hide corruption.
|
|
const assert = require("assert");
|
|
const childProcess = require("child_process");
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const path = require("path");
|
|
|
|
const repoRoot = path.resolve(__dirname, "..");
|
|
const testRoot = fs.mkdtempSync(path.join(os.tmpdir(), "musrflow-download-"));
|
|
const dataRoot = path.join(testRoot, "data");
|
|
const firstRoot = path.join(testRoot, "first");
|
|
const secondRoot = path.join(testRoot, "second");
|
|
const tmpRoot = path.join(testRoot, "unused-web-tmp");
|
|
const startupPath = path.join(testRoot, "musrfit_startup.xml");
|
|
const binary = Buffer.alloc(180003);
|
|
for (let index = 0; index < binary.length; index++) {
|
|
binary[index] = index % 256;
|
|
}
|
|
|
|
function writeData(root, relativePath, content) {
|
|
const target = path.join(root, relativePath);
|
|
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
fs.writeFileSync(target, content === undefined ? binary : content);
|
|
return target;
|
|
}
|
|
|
|
function request(action, params, extraEnv) {
|
|
const body = new URLSearchParams(Object.assign({
|
|
action: action, instrument: "LEM", year: "2026", run: "514"
|
|
}, params)).toString();
|
|
const result = childProcess.spawnSync("perl", [path.join(repoRoot, "cgi", "single_musrfit.cgi")], {
|
|
cwd: repoRoot,
|
|
input: body,
|
|
timeout: 10000,
|
|
maxBuffer: 2 * 1024 * 1024,
|
|
env: Object.assign({}, process.env, {
|
|
REQUEST_METHOD: "POST",
|
|
CONTENT_TYPE: "application/x-www-form-urlencoded",
|
|
CONTENT_LENGTH: String(Buffer.byteLength(body)),
|
|
PERL5LIB: path.join(repoRoot, "tests", "perl"),
|
|
MUSRFIT_WEBROOT: path.join(testRoot, "unused-web-root"),
|
|
MUSRFIT_TMPROOT: tmpRoot,
|
|
MUSRFIT_DATA_ROOT: dataRoot,
|
|
MUSRFIT_STARTUP_XML: startupPath,
|
|
// Downloads must work without any musrfit installation.
|
|
MUSRFIT_BIN: "/no/musrfit", MUSRVIEW_BIN: "/no/musrview",
|
|
MSR2DATA_BIN: "/no/msr2data", DUMP_HEADER_BIN: "/no/dump_header"
|
|
}, extraEnv)
|
|
});
|
|
assert.ifError(result.error);
|
|
assert.strictEqual(result.status, 0, result.stderr.toString());
|
|
const separator = result.stdout.indexOf("\n\n");
|
|
assert.ok(separator > 0, "CGI headers precede the response body");
|
|
const headers = Object.fromEntries(result.stdout.subarray(0, separator).toString("ascii")
|
|
.split("\n").map(function(line) {
|
|
const colon = line.indexOf(":");
|
|
return [line.slice(0, colon).toLowerCase(), line.slice(colon + 1).trim()];
|
|
}));
|
|
const responseBody = result.stdout.subarray(separator + 2);
|
|
return {
|
|
headers: headers, body: responseBody,
|
|
json: headers["content-type"] === "application/json"
|
|
? JSON.parse(responseBody.toString("utf8")) : null
|
|
};
|
|
}
|
|
|
|
try {
|
|
const lemFile = writeData(firstRoot, "2026/lem26_his_0514.root");
|
|
writeData(secondRoot, "d2026/tdc/lem26_his_0600.root");
|
|
writeData(secondRoot, "2026/lem26_his_00601.root");
|
|
writeData(dataRoot, "gps/his/2007/deltat_ccr_0007.bin");
|
|
writeData(secondRoot, "d2024/tdc/root/deltatbc_tdc_gps_2024_0008.root");
|
|
writeData(secondRoot, "d2015/tdc/tdc_hifi_2015_00009.bin");
|
|
fs.writeFileSync(startupPath, `<musrfit>
|
|
<data_path>${firstRoot}/</data_path>
|
|
<data_path>${secondRoot}</data_path>
|
|
<run_name_template inst="lem">%yyyy%/lem%yy%_his_%rrrr%.root</run_name_template>
|
|
<run_name_template inst="lem">d%yyyy%/tdc/lem%yy%_his_%rrrr%.root</run_name_template>
|
|
<run_name_template inst="lem">%yyyy%/lem%yy%_his_%rrrrr%.root</run_name_template>
|
|
<run_name_template inst="gps">d%yyyy%/tdc/root/deltatbc_tdc_gps_%yyyy%_%rrrr%.root</run_name_template>
|
|
<run_name_template inst="gps">d%yyyy%/deltat_ccr_%rrrr%.bin</run_name_template>
|
|
<run_name_template inst="hifi">d%yyyy%/tdc/tdc_hifi_%yyyy%_%rrrrr%.bin</run_name_template>
|
|
</musrfit>`);
|
|
|
|
const info = request("run_file_info").json;
|
|
assert.strictEqual(info.ok, true);
|
|
assert.strictEqual(info.code, 0);
|
|
assert.strictEqual(info.instrument, "LEM");
|
|
assert.strictEqual(info.year, 2026);
|
|
assert.strictEqual(info.run, 514);
|
|
assert.strictEqual(info.filename, "lem26_his_0514.root");
|
|
assert.strictEqual(info.relative_path, "lem/his/2026/lem26_his_0514.root");
|
|
assert.strictEqual(info.format, "MUSR-ROOT");
|
|
assert.strictEqual(info.size, binary.length);
|
|
assert.strictEqual(info.mtime, Math.floor(fs.statSync(lemFile).mtimeMs / 1000));
|
|
assert.match(info.version, /^[0-9a-f]{64}$/);
|
|
assert.ok(!JSON.stringify(info).includes(testRoot), "metadata does not expose server paths");
|
|
assert.strictEqual(request("run_file_info", { instrument: "lem", run: "000514" }).json.version,
|
|
info.version, "equivalent run identities have the same version");
|
|
|
|
const download = request("download_run_file", { version: info.version });
|
|
assert.strictEqual(download.headers["content-type"], "application/octet-stream");
|
|
assert.strictEqual(download.headers["content-disposition"], 'attachment; filename="lem26_his_0514.root"');
|
|
assert.strictEqual(download.headers["content-length"], String(binary.length));
|
|
assert.strictEqual(download.headers["cache-control"], "no-store");
|
|
assert.strictEqual(download.headers["x-content-type-options"], "nosniff");
|
|
assert.deepStrictEqual(download.body, binary, "multi-chunk download preserves every byte");
|
|
|
|
// Template and root priority are the same for both actions, including
|
|
// older campaign filenames and the HAL/HIFI format exception.
|
|
const cases = [
|
|
[{ run: "600" }, "LEM", "lem/his/d2026/tdc/lem26_his_0600.root", "MUSR-ROOT"],
|
|
[{ run: "601" }, "LEM", "lem/his/2026/lem26_his_00601.root", "MUSR-ROOT"],
|
|
[{ instrument: "GPS", year: "2007", run: "7" }, "GPS", "gps/his/2007/deltat_ccr_0007.bin", "PSI-BIN"],
|
|
[{ instrument: "gps", year: "2024", run: "8" }, "GPS", "gps/his/d2024/tdc/root/deltatbc_tdc_gps_2024_0008.root", "MUSR-ROOT"],
|
|
[{ instrument: "HAL", year: "2015", run: "9" }, "HAL", "hal/his/d2015/tdc/tdc_hifi_2015_00009.bin", "PSI-MDU"],
|
|
[{ instrument: "hifi", year: "2015", run: "9" }, "HAL", "hal/his/d2015/tdc/tdc_hifi_2015_00009.bin", "PSI-MDU"]
|
|
];
|
|
for (const [params, instrument, relativePath, format] of cases) {
|
|
const metadata = request("run_file_info", params).json;
|
|
assert.strictEqual(metadata.ok, true, JSON.stringify(metadata));
|
|
assert.strictEqual(metadata.instrument, instrument);
|
|
assert.strictEqual(metadata.relative_path, relativePath);
|
|
assert.strictEqual(metadata.format, format);
|
|
assert.deepStrictEqual(request("download_run_file", params).body, binary);
|
|
}
|
|
writeData(firstRoot, "2026/lem26_his_0600.root", "preferred template");
|
|
assert.strictEqual(request("download_run_file", { run: "600" }).body.toString(),
|
|
"preferred template", "an earlier matching template takes precedence");
|
|
|
|
const shippedXmlPath = path.join(testRoot, "shipped-startup.xml");
|
|
const shippedXml = fs.readFileSync(path.join(repoRoot, "cgi", "musrfit_startup.xml"), "utf8")
|
|
.replace(/<data_path>[^<]*<\/data_path>/g,
|
|
"<data_path>" + path.join(testRoot, "unused-shipped-root") + "</data_path>");
|
|
fs.writeFileSync(shippedXmlPath, shippedXml);
|
|
const flame = request("run_file_info", { instrument: "FLAME", run: "1285" }, {
|
|
MUSRFIT_STARTUP_XML: shippedXmlPath,
|
|
MUSRFIT_DATA_ROOT: path.join(testRoot, "shipped-layout")
|
|
});
|
|
assert.strictEqual(flame.json.code, 404, "shipped templates recognize the modern FLAME layout");
|
|
writeData(dataRoot, "flame/his/2026/flame26_his_01285.root");
|
|
const shippedFlame = request("run_file_info", { instrument: "FLAME", run: "1285" }, {
|
|
MUSRFIT_STARTUP_XML: shippedXmlPath
|
|
}).json;
|
|
assert.strictEqual(shippedFlame.relative_path, "flame/his/2026/flame26_his_01285.root");
|
|
|
|
// Stale metadata returns JSON with an HTTP error, never a binary response
|
|
// carrying bytes different from the file the desktop agreed to download.
|
|
fs.appendFileSync(lemFile, "new counts");
|
|
const changed = request("download_run_file", { version: info.version });
|
|
assert.strictEqual(changed.headers.status, "409 Conflict");
|
|
assert.strictEqual(changed.json.reason, "run-file-changed");
|
|
assert.strictEqual(request("download_run_file", { version: "bogus" }).json.code, 400);
|
|
const refreshed = request("run_file_info").json;
|
|
assert.notStrictEqual(refreshed.version, info.version);
|
|
assert.strictEqual(request("download_run_file", { version: refreshed.version }).body.length,
|
|
refreshed.size);
|
|
|
|
for (const action of ["run_file_info", "download_run_file"]) {
|
|
for (const params of [
|
|
{ instrument: "../lem" }, { instrument: "unknown" },
|
|
{ year: "26" }, { year: "2026/.." }, { run: "0" },
|
|
{ run: "-1" }, { run: "1.5" }, { run: "1e3" },
|
|
{ run: "514\n" }, { run: "1/../../etc/passwd" }, { run: "1234567890" }
|
|
]) {
|
|
const response = request(action, params);
|
|
assert.strictEqual(response.headers.status, "400 Bad Request", JSON.stringify(params));
|
|
assert.strictEqual(response.json.ok, false);
|
|
}
|
|
const missing = request(action, { run: "9999", fn: lemFile });
|
|
assert.strictEqual(missing.headers.status, "404 Not Found");
|
|
assert.strictEqual(missing.json.outcome, "missing-data");
|
|
assert.strictEqual(request(action, {}, {
|
|
MUSRFIT_STARTUP_XML: path.join(testRoot, "missing.xml")
|
|
}).json.reason, "run-file-unconfigured");
|
|
}
|
|
writeData(firstRoot, "2026/lem26_his_0602.root", "");
|
|
assert.strictEqual(request("download_run_file", { run: "602" }).json.reason, "run-file-empty");
|
|
|
|
// Resolve legitimate mount symlinks, but reject a data-file or directory
|
|
// symlink that escapes the configured roots.
|
|
const outside = writeData(testRoot, "private/outside.root");
|
|
fs.symlinkSync(outside, path.join(firstRoot, "2026/lem26_his_0603.root"));
|
|
assert.strictEqual(request("download_run_file", { run: "603" }).json.code, 404);
|
|
writeData(testRoot, "private/lem27_his_0604.root");
|
|
fs.symlinkSync(path.join(testRoot, "private"), path.join(firstRoot, "2027"));
|
|
assert.strictEqual(request("download_run_file", { run: "604", year: "2027" }).json.code, 404);
|
|
fs.symlinkSync(lemFile, path.join(firstRoot, "2026/lem26_his_0605.root"));
|
|
assert.strictEqual(request("run_file_info", { run: "605" }).json.ok, true);
|
|
|
|
assert.strictEqual(fs.existsSync(tmpRoot), false, "read-only actions never create web jobs");
|
|
console.log("CGI run metadata and binary download tests passed");
|
|
} finally {
|
|
fs.rmSync(testRoot, { recursive: true, force: true });
|
|
}
|