// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. // QC phase 4, M1 (tests first): a release may publish only a pin made from the code it releases. // node --test scripts/ // Each case builds a small git repository in the temp folder and runs release-pin.mjs on it as the // release pipeline does (IANITOR_REPO points it there). import { test } from "node:test"; import assert from "node:assert/strict"; import { execFileSync, spawnSync } from "node:child_process"; import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const script = join(dirname(fileURLToPath(import.meta.url)), "release-pin.mjs"); const HASH = "sha256-" + "A".repeat(43) + "="; function repo() { const dir = mkdtempSync(join(tmpdir(), "ianitor-pin-test-")); const git = (...args) => execFileSync("git", ["-c", "user.name=t", "-c", "user.email=t@t.test", "-c", "core.hooksPath=", ...args], { cwd: dir, encoding: "utf8", }).trim(); git("init", "-q", "-b", "main"); const write = (p, text) => { mkdirSync(dirname(join(dir, p)), { recursive: true }); writeFileSync(join(dir, p), text); }; const commit = (msg) => { git("add", "-A"); git("commit", "-q", "-m", msg); return git("rev-parse", "HEAD"); }; return { dir, git, write, commit, done: () => rmSync(dir, { recursive: true, force: true }) }; } /// The pin nix-pin.sh writes for the commit `pinned` (its source tree from release-pin itself). function pinFor(r, pinned) { const tree = execFileSync(process.execPath, [script, "--source-tree", pinned], { env: { ...process.env, IANITOR_REPO: r.dir }, encoding: "utf8", }).trim(); return JSON.stringify({ version: "1.0.0", system: "x86_64-linux", commit: pinned, sourceTree: tree, nixpkgs: "0123456789abcdef0123456789abcdef01234567", outputs: { ianitor: { storePath: "/nix/store/" + "a".repeat(32) + "-ianitor-1.0.0", narHash: HASH, narSize: 1 }, "ianitor-web": { storePath: "/nix/store/" + "b".repeat(32) + "-ianitor-web-1.0.0", narHash: HASH, narSize: 1 }, }, }); } function run(r, tagCommit) { return spawnSync(process.execPath, [script, "1.0.0", "v1.0.0", tagCommit], { env: { ...process.env, IANITOR_REPO: r.dir }, encoding: "utf8", }); } test("no pin: refused", () => { const r = repo(); try { r.write("crates/a.rs", "fn a() {}\n"); const c = r.commit("code"); const out = run(r, c); assert.equal(out.status, 1, out.stderr); assert.match(out.stderr, /pin\.json/); } finally { r.done(); } }); test("a pin from an older commit, with code changed since: refused", () => { const r = repo(); try { r.write("crates/a.rs", "fn a() {}\n"); const a = r.commit("code at A"); r.write(".fabrica/nix/pin.json", pinFor(r, a)); r.commit("pin A"); r.write("crates/a.rs", "fn a() { fixed() }\n"); const b = r.commit("fix at B"); const out = run(r, b); assert.equal(out.status, 1, out.stdout + out.stderr); assert.match(out.stderr, /not the code of/); } finally { r.done(); } }); test("a pin from the parent where only pin.json changed: accepted; the tagged commit is the build reference", () => { const r = repo(); try { r.write("crates/a.rs", "fn a() {}\n"); const a = r.commit("code at A"); r.write(".fabrica/nix/pin.json", pinFor(r, a)); const b = r.commit("[repo] Release: the Nix pin"); const out = run(r, b); assert.equal(out.status, 0, out.stdout + out.stderr); const published = JSON.parse(readFileSync(join(r.dir, "target/nix/ianitor-nix.json"), "utf8")); assert.equal(published.commit, a, "the commit the pin was made at, for the record"); assert.equal(published.commitChecked, true, "its objects are here, so its tree was checked"); assert.equal(published.taggedCommit, b); assert.equal(published.tag, "v1.0.0"); // Verifiers build the tagged commit: the same source by construction, and it surely exists. assert.equal(published.flake, `git+https://gitea.marek-novak.cz/mnovak/Ianitor.git?rev=${b}`); } finally { r.done(); } }); test("a forged commit (present, but another source tree): refused", () => { const r = repo(); try { r.write("crates/a.rs", "fn other() {}\n"); const x = r.commit("other code at X"); r.write("crates/a.rs", "fn a() {}\n"); const a = r.commit("code at A"); const pin = JSON.parse(pinFor(r, a)); pin.commit = x; // the tree is A's, the commit named is X r.write(".fabrica/nix/pin.json", JSON.stringify(pin)); const b = r.commit("pin"); const out = run(r, b); assert.equal(out.status, 1, out.stdout + out.stderr); assert.match(out.stderr, /does not hold the source tree/); } finally { r.done(); } }); test("a commit rebased away (absent): accepted, the tagged commit is the build reference", () => { const r = repo(); try { r.write("crates/a.rs", "fn a() {}\n"); const a = r.commit("code at A"); const pin = JSON.parse(pinFor(r, a)); pin.commit = "f".repeat(40); // no such object here (rebased away, or a shallow clone) r.write(".fabrica/nix/pin.json", JSON.stringify(pin)); const b = r.commit("pin"); const out = run(r, b); assert.equal(out.status, 0, out.stdout + out.stderr); const published = JSON.parse(readFileSync(join(r.dir, "target/nix/ianitor-nix.json"), "utf8")); assert.equal(published.taggedCommit, b); assert.equal(published.commit, "f".repeat(40)); assert.equal(published.commitChecked, false); assert.equal(published.flake, `git+https://gitea.marek-novak.cz/mnovak/Ianitor.git?rev=${b}`); } finally { r.done(); } }); test("a pin whose recorded tree was edited by hand: refused", () => { const r = repo(); try { r.write("crates/a.rs", "fn a() {}\n"); const a = r.commit("code"); const pin = JSON.parse(pinFor(r, a)); pin.sourceTree = "0".repeat(64); r.write(".fabrica/nix/pin.json", JSON.stringify(pin)); const b = r.commit("pin"); assert.equal(run(r, b).status, 1); } finally { r.done(); } }); test("the repository is the script's own unless IANITOR_REPO says otherwise", () => { assert.equal(resolve(dirname(script), ".."), resolve(dirname(fileURLToPath(import.meta.url)), "..")); });