"use strict"; // Swapoff safety rule of the fixed remote swap helper, run locally with a fake /proc (no swap is touched). const test = require("node:test"); const assert = require("node:assert/strict"); const fs = require("node:fs"); const os = require("node:os"); const path = require("node:path"); const { spawnSync } = require("node:child_process"); const SCRIPT = path.join(__dirname, "../src/main/system/remoteSwap.sh"); const MiB = 1024; // The helper is a POSIX shell script; Windows hosts have no /bin/sh. const skip = process.platform === "win32" ? "POSIX sh is not available on Windows." : false; function check({ totalMiB, availableMiB, areas }) { const proc = fs.mkdtempSync(path.join(os.tmpdir(), "chj-swap-proc-")); try { fs.writeFileSync(path.join(proc, "meminfo"), `MemTotal: ${totalMiB * MiB} kB\nMemAvailable: ${availableMiB * MiB} kB\n`); fs.writeFileSync(path.join(proc, "swaps"), "Filename\tType\tSize\tUsed\tPriority\n" + areas.map(([name, sizeMiB, usedMiB]) => `${name}\tfile\t${sizeMiB * MiB}\t${usedMiB * MiB}\t-2`).join("\n") + "\n"); const result = spawnSync("sh", [SCRIPT, "safety-check", areas[0][0]], { encoding: "utf8", env: { ...process.env, CHJ_SWAP_TEST_PROC: proc } }); return result.stdout.includes("safe\n") ? "safe" : (result.stdout.match(/CHJ_ERR=(\w+)/) || [])[1]; } finally { fs.rmSync(proc, { recursive: true, force: true }); } } test("swapoff is refused when the used swap cannot fit into RAM minus the reserve", { skip }, () => { assert.equal(check({ totalMiB: 4096, availableMiB: 1024, areas: [["/swapfile", 2048, 900]] }), "SWAPOFF_UNSAFE"); assert.equal(check({ totalMiB: 4096, availableMiB: 1024, areas: [["/swapfile", 2048, 400]] }), "safe"); }); test("free space in other active swap areas counts towards the room", { skip }, () => { assert.equal(check({ totalMiB: 4096, availableMiB: 1024, areas: [["/swapfile", 2048, 900], ["/swap/b", 1024, 0]] }), "safe"); }); test("an unused swap area can always be deactivated, even on a small server", { skip }, () => { assert.equal(check({ totalMiB: 468, availableMiB: 408, areas: [["/swapfile", 128, 0]] }), "safe"); }); test("the reserve never exceeds a quarter of RAM on small servers", { skip }, () => { // 468 MiB RAM: reserve 117 MiB instead of 512 MiB. assert.equal(check({ totalMiB: 468, availableMiB: 400, areas: [["/swapfile", 256, 200]] }), "safe"); assert.equal(check({ totalMiB: 468, availableMiB: 300, areas: [["/swapfile", 256, 200]] }), "SWAPOFF_UNSAFE"); }); test("the test-only operation is unavailable without the test /proc override", { skip }, () => { const result = spawnSync("sh", [SCRIPT, "safety-check", "/swapfile"], { encoding: "utf8", env: { PATH: process.env.PATH } }); assert.match(result.stdout, /CHJ_ERR=ACTION_INVALID/); }); // fstab priority handling of the helper on a temporary fstab copy (test-only operation; nothing on the host changes). function fstabRun(content, args, { swaps = "", canon = "" } = {}) { const proc = fs.mkdtempSync(path.join(os.tmpdir(), "chj-swap-fstab-")); try { const fstab = path.join(proc, "fstab"); fs.writeFileSync(fstab, content); fs.writeFileSync(path.join(proc, "swaps"), "Filename\tType\tSize\tUsed\tPriority\n" + swaps); fs.writeFileSync(path.join(proc, "canon"), canon); fs.writeFileSync(path.join(proc, "meminfo"), "MemTotal: 4194304 kB\nMemAvailable: 2097152 kB\n"); const result = spawnSync("sh", [SCRIPT, "fstab-test", ...args], { encoding: "utf8", env: { PATH: process.env.PATH, CHJ_SWAP_TEST_PROC: proc, CHJ_SWAP_TEST_FSTAB: fstab } }); return { out: result.stdout, error: (result.stdout.match(/CHJ_ERR=(\w+)/) || [])[1], fstab: fs.readFileSync(fstab, "utf8"), backups: fs.readdirSync(proc).filter((name) => name.startsWith("fstab.chj-backup-")).length }; } finally { fs.rmSync(proc, { recursive: true, force: true }); } } const UBUNTU = "# /etc/fstab\nUUID=abc / ext4 errors=remount-ro 0 1\n/swapfile-chj none swap sw 0 0 # CH-J chj.system-monitor\n/swapfile-chj2 none swap sw 0 0 # CH-J chj.system-monitor\n"; test("explicit priorities 100/10 update the existing entries in place, keep options, comments and order", { skip }, () => { let result = fstabRun(UBUNTU, ["priority", "/swapfile-chj", "100"]); assert.equal(result.error, undefined); assert.equal(result.backups, 1); assert.equal(fstabRun(UBUNTU, ["priority", "/swapfile-chj", "100"]).backups, 1); result = fstabRun(result.fstab, ["priority", "/swapfile-chj2", "10"]); assert.equal(result.fstab, "# /etc/fstab\nUUID=abc / ext4 errors=remount-ro 0 1\n/swapfile-chj none swap sw,pri=100 0 0 # CH-J chj.system-monitor\n/swapfile-chj2 none swap sw,pri=10 0 0 # CH-J chj.system-monitor\n"); // Changing an existing pri= replaces it; repeating the same value creates no duplicates. result = fstabRun(result.fstab, ["priority", "/swapfile-chj", "200"]); result = fstabRun(result.fstab, ["priority", "/swapfile-chj", "200"]); assert.match(result.fstab, /^\/swapfile-chj none swap sw,pri=200 0 0 # CH-J chj\.system-monitor$/m); assert.equal((result.fstab.match(/swapfile-chj /g) || []).length, 1); // Back to automatic removes pri= only. result = fstabRun(result.fstab, ["priority", "/swapfile-chj", "-1"]); assert.match(result.fstab, /^\/swapfile-chj none swap sw 0 0 # CH-J chj\.system-monitor$/m); }); test("other options, foreign comments, tabs and unrelated lines survive; reversed order is matched by identity", { skip }, () => { const source = "/swapfile-chj2\tnone\tswap\tdefaults,nofail,pri=3\t0\t0\n# comment line\nproc /proc proc defaults 0 0\n/swapfile-chj none swap sw,discard 0 0\n"; const result = fstabRun(source, ["priority", "/swapfile-chj2", "10"]); assert.equal(result.fstab, "/swapfile-chj2 none swap defaults,nofail,pri=10 0 0\n# comment line\nproc /proc proc defaults 0 0\n/swapfile-chj none swap sw,discard 0 0\n"); }); test("UUID= partition entries are matched through their canonical device", { skip }, () => { const source = "UUID=1234-ab none swap sw 0 0\n"; const result = fstabRun(source, ["priority", "/dev/sda2", "50"], { canon: "UUID=1234-ab|/dev/sda2\n" }); assert.equal(result.fstab, "UUID=1234-ab none swap sw,pri=50 0 0\n"); }); test("swap only active (no fstab entry) is not written by priority; persist appends once with the priority", { skip }, () => { let result = fstabRun("proc /proc proc defaults 0 0\n", ["priority", "/swap/a", "5"]); assert.equal(result.fstab, "proc /proc proc defaults 0 0\n"); assert.equal(result.backups, 0); result = fstabRun(result.fstab, ["persist", "/swap/a", "5"]); result = fstabRun(result.fstab, ["persist", "/swap/a", "keep"]); assert.equal(result.fstab, "proc /proc proc defaults 0 0\n/swap/a none swap sw,pri=5 0 0 # CH-J chj.system-monitor\n"); result = fstabRun(result.fstab, ["remove", "/swap/a", "keep"]); assert.equal(result.fstab, "proc /proc proc defaults 0 0\n"); }); test("duplicate entries for one swap area are refused instead of guessed", { skip }, () => { const source = "/swapfile none swap sw 0 0\n/swapfile none swap sw,pri=4 0 0\n"; const result = fstabRun(source, ["priority", "/swapfile", "9"]); assert.equal(result.error, "FSTAB_AMBIGUOUS"); assert.equal(result.fstab, source); }); test("keep resolves fstab pri=, then explicit runtime priority, else automatic (auto negatives are never reused)", { skip }, () => { assert.equal(fstabRun("/swapfile none swap sw,pri=100 0 0\n", ["resolve", "/swapfile", "keep"]).out.trim().split("\n").at(-1), "100"); assert.equal(fstabRun("", ["resolve", "/swapfile", "keep"], { swaps: "/swapfile\tfile\t1024\t0\t7\n" }).out.trim().split("\n").at(-1), "7"); assert.equal(fstabRun("", ["resolve", "/swapfile", "keep"], { swaps: "/swapfile\tfile\t1024\t0\t-3\n" }).out.trim().split("\n").at(-1), "-1"); }); test("a non-writable fstab is reported and left unchanged", { skip: skip || (process.getuid?.() === 0 && "root ignores permissions") }, () => { const proc = fs.mkdtempSync(path.join(os.tmpdir(), "chj-swap-ro-")); try { const fstab = path.join(proc, "fstab"); fs.writeFileSync(fstab, UBUNTU); fs.writeFileSync(path.join(proc, "swaps"), "Filename\n"); fs.chmodSync(proc, 0o500); const result = spawnSync("sh", [SCRIPT, "fstab-test", "priority", "/swapfile-chj", "100"], { encoding: "utf8", env: { PATH: process.env.PATH, CHJ_SWAP_TEST_PROC: proc, CHJ_SWAP_TEST_FSTAB: fstab } }); assert.match(result.stdout, /CHJ_ERR=FSTAB_(BACKUP|WRITE)_FAILED/); assert.equal(fs.readFileSync(fstab, "utf8"), UBUNTU); } finally { fs.chmodSync(proc, 0o700); fs.rmSync(proc, { recursive: true, force: true }); } });