// Copyright (C) 2026 Zaher Salman // SPDX-License-Identifier: GPL-3.0-or-later // Tests for CGI parsing of the server-owned musrfit_startup.xml and the // scoped resolve_run_name existence check backing portable RUN naming. 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-startup-")); const perlLibraryPath = [path.join(repoRoot, "tests", "perl")] .filter(Boolean).join(path.delimiter); const dataRoot = path.join(testRoot, "data"); // A portable file matching the startup-XML LTF template // d%yyyy%/tdc/deltat_tdc_ltf_%rrrr%.bin for year 2026, run 1234. const portableFile = path.join(dataRoot, "d2026", "tdc", "deltat_tdc_ltf_1234.bin"); fs.mkdirSync(path.dirname(portableFile), { recursive: true }); fs.writeFileSync(portableFile, "dummy\n"); const startupXmlPath = path.join(testRoot, "musrfit_startup.xml"); const startupTemplateRoot = path.join(testRoot, "remote-root"); const startupTemplateFile = path.join(startupTemplateRoot, "d2026", "tdc", "deltat_tdc_ltf_1234.bin"); fs.mkdirSync(path.dirname(startupTemplateFile), { recursive: true }); fs.writeFileSync(startupTemplateFile, "dummy\n"); fs.writeFileSync(startupXmlPath, ` ${startupTemplateRoot} d%yyyy%/tdc/deltat_tdc_ltf_%rrrr%.bin `); // A startup XML whose leading comment contains prose with literal tag-like text // must still parse cleanly (comments are skipped, never parsed as elements). const commentedXmlPath = path.join(testRoot, "musrfit_startup_commented.xml"); fs.writeFileSync(commentedXmlPath, ` ${startupTemplateRoot} d%yyyy%/tdc/deltat_tdc_ltf_%rrrr%.bin `); function runCgi(action, params, startupOverride) { const body = new URLSearchParams(Object.assign({ action }, params)).toString(); const env = Object.assign({}, process.env, { REQUEST_METHOD: "POST", PERL5LIB: perlLibraryPath, CONTENT_TYPE: "application/x-www-form-urlencoded", CONTENT_LENGTH: String(Buffer.byteLength(body)), MUSRFIT_WEBROOT: path.join(testRoot, "web"), MUSRFIT_TMPROOT: path.join(testRoot, "tmp"), MUSRFIT_TMP_URL: "/tmp", MUSRFIT_BIN: "perl", MUSRVIEW_BIN: "perl", MSR2DATA_BIN: "perl", MUSRFIT_RUNTIME_HOME: testRoot, MUSRFIT_DATA_ROOT: dataRoot }); env.MUSRFIT_STARTUP_XML = startupOverride || startupXmlPath; const result = childProcess.spawnSync( "perl", [path.join(repoRoot, "cgi", "single_musrfit.cgi")], { cwd: repoRoot, encoding: "utf8", input: body, env: env } ); assert.strictEqual(result.status, 0, result.stderr); const responseBody = result.stdout.split(/\r?\n\r?\n/).slice(1).join("\n\n"); return JSON.parse(responseBody); } let health; try { health = runCgi("health"); } catch (e) { console.error(e); process.exit(1); } // Health must report the parsed startup XML: ok, its data paths, and templates. const sx = health.environment && health.environment.startup_xml; assert.ok(sx, "health reports environment.startup_xml"); assert.deepStrictEqual(sx.data_paths, [startupTemplateRoot]); assert.deepStrictEqual(sx.templates, { ltf: ["d%yyyy%/tdc/deltat_tdc_ltf_%rrrr%.bin"] }); // Comments (even with literal tag-like prose) are skipped, not parsed as // elements, so health reports the real data_path/template only. const commentedSx = runCgi("health", {}, commentedXmlPath).environment.startup_xml; assert.deepStrictEqual(commentedSx.data_paths, [startupTemplateRoot]); assert.deepStrictEqual(commentedSx.templates, { ltf: ["d%yyyy%/tdc/deltat_tdc_ltf_%rrrr%.bin"] }); // MUSRFULLDATAPATH must point at a that actually exists so relative // portable RUN paths (e.g. 2026/lem26_his_0514.root) resolve against the // instrument root rather than the shared data root. When the admin leaves // MUSRFULLDATAPATH unset, the CGI derives it from the first on-disk // in the startup XML. assert.strictEqual( health.environment.musr_full_data_path, startupTemplateRoot, "MUSRFULLDATAPATH defaults to the first on-disk startup-XML " ); // An explicit MUSRFULLDATAPATH always wins over XML-derived defaults. function healthWithFullDataPath(full) { const body = "action=health"; const env = Object.assign({}, process.env, { REQUEST_METHOD: "POST", PERL5LIB: perlLibraryPath, CONTENT_TYPE: "application/x-www-form-urlencoded", CONTENT_LENGTH: String(Buffer.byteLength(body)), MUSRFIT_WEBROOT: path.join(testRoot, "web"), MUSRFIT_TMPROOT: path.join(testRoot, "tmp"), MUSRFIT_TMP_URL: "/tmp", MUSRFIT_BIN: "perl", MUSRVIEW_BIN: "perl", MSR2DATA_BIN: "perl", MUSRFIT_RUNTIME_HOME: testRoot, MUSRFIT_DATA_ROOT: dataRoot, MUSRFIT_STARTUP_XML: startupXmlPath, MUSRFULLDATAPATH: full }); const result = childProcess.spawnSync( "perl", [path.join(repoRoot, "cgi", "single_musrfit.cgi")], { cwd: repoRoot, encoding: "utf8", input: body, env: env } ); const responseBody = result.stdout.split(/\r?\n\r?\n/).slice(1).join("\n\n"); return JSON.parse(responseBody); } assert.strictEqual( healthWithFullDataPath("/opt/override").environment.musr_full_data_path, "/opt/override", "an explicit MUSRFULLDATAPATH overrides the XML-derived default" ); // resolve_run_name confirms an existing candidate under a configured data path. let res = runCgi("resolve_run_name", { sub_path: "d2026/tdc/deltat_tdc_ltf_1234.bin", data_path: startupTemplateRoot }); assert.strictEqual(res.ok, true, JSON.stringify(res)); assert.strictEqual(res.sub_path, "d2026/tdc/deltat_tdc_ltf_1234.bin"); assert.strictEqual(res.data_path, startupTemplateRoot); // ... and reports not-found for a non-existent candidate in a configured root. res = runCgi("resolve_run_name", { sub_path: "d2026/tdc/deltat_tdc_ltf_9999.bin", data_path: startupTemplateRoot }); assert.strictEqual(res.ok, false); assert.strictEqual(res.code, 404, JSON.stringify(res)); // A data path not configured in the startup XML is rejected regardless of what // actually lives there. res = runCgi("resolve_run_name", { sub_path: "d2026/tdc/deltat_tdc_ltf_1234.bin", data_path: dataRoot }); assert.strictEqual(res.ok, false); assert.strictEqual(res.code, 403, JSON.stringify(res)); // Traversal is rejected even within a configured root. res = runCgi("resolve_run_name", { sub_path: "../escape.txt", data_path: startupTemplateRoot }); assert.strictEqual(res.ok, false); assert.strictEqual(res.code, 400, JSON.stringify(res)); console.log("CGI startup-XML tests passed");