"use strict"; // Behavior + source pins for utils/session-epoch.js — the ONE home of // session-epoch generation (adpanel:credential-generation). // // Pinned here: // 1. Generation is CSPRNG (crypto.randomInt) in the established range // [1, 2147483647] — never 0 (0 is the column's "no generation" default), // never a predictable increment, never Math.random/Date.now. // 2. newSessionEpochDifferentFrom(current) never returns the excluded // current generation (bounded retry), including for legacy rows whose // current is 0/absent/malformed. // 3. The retry is a real loop, not a single redraw: deterministic draws via // the house vm-extraction technique prove a colliding candidate is // redrawn (and that the draw bounds are the established ones). const test = require("node:test"); const assert = require("node:assert"); const fs = require("node:fs"); const path = require("node:path"); const vm = require("node:vm"); const ROOT = path.join(__dirname, ".."); const MODULE_JS = path.join(ROOT, "utils", "session-epoch.js"); const { newSessionEpoch, newSessionEpochDifferentFrom, SESSION_EPOCH_MIN, SESSION_EPOCH_MAX } = require(MODULE_JS); const moduleSource = fs.readFileSync(MODULE_JS, "utf8"); const MIN = 1; const MAX = 2147483647; test("source: the generator is crypto.randomInt over the established range — no PRNG fallback, no 0, no increment", () => { assert.ok(moduleSource.includes("crypto.randomInt("), "generation must call crypto.randomInt (CSPRNG, CWE-338)"); assert.ok(!moduleSource.includes("Math.random"), "Math.random must never generate discriminator material"); assert.ok(!moduleSource.includes("Date.now"), "Date.now must never generate discriminator material"); assert.match(moduleSource, /SESSION_EPOCH_MIN\s*=\s*1\b/, "the range floor is 1 — never the 0 column default"); assert.match(moduleSource, /SESSION_EPOCH_MAX\s*=\s*2147483647\b/, "the range ceiling is the established INT-safe bound"); assert.ok(moduleSource.includes("// adpanel:credential-generation"), "the constraint comment (epoch bumps ride the credential's statement) lives here"); }); test("range: every draw is an integer in [1, 2147483647] and never 0", () => { assert.equal(SESSION_EPOCH_MIN, MIN); assert.equal(SESSION_EPOCH_MAX, MAX); for (let i = 0; i < 20000; i += 1) { const v = newSessionEpoch(); assert.ok(Number.isInteger(v), `draw ${i} must be an integer, got ${v}`); assert.ok(v >= MIN && v <= MAX, `draw ${i} out of range: ${v}`); assert.notEqual(v, 0, "a real generation must never be the 0 column default"); } }); test("CSPRNG: draws actually vary (no constant epoch)", () => { const seen = new Set(); for (let i = 0; i < 5000; i += 1) seen.add(newSessionEpoch()); assert.ok(seen.size > 4000, `5000 CSPRNG draws over a 2^31 range must produce thousands of distinct values, got ${seen.size}`); }); test("distinct-from: never returns the excluded current generation (statistical sweep over boundary currents)", () => { for (const current of [1, 2, MIN, MAX, MAX - 1, 42, 2147483646, 0]) { for (let i = 0; i < 5000; i += 1) { const v = newSessionEpochDifferentFrom(current); assert.notEqual(v, current, `a bump must never land on the row's current generation (${current})`); assert.ok(Number.isInteger(v) && v >= MIN && v <= MAX, `out-of-range draw for current ${current}: ${v}`); } } }); test("distinct-from: absent/NULL/malformed currents (legacy rows) still yield in-range generations", () => { for (const current of [undefined, null, "", "not-a-number", NaN]) { for (let i = 0; i < 1000; i += 1) { const v = newSessionEpochDifferentFrom(current); assert.ok(Number.isInteger(v) && v >= MIN && v <= MAX, `current ${String(current)} produced ${v}`); assert.notEqual(v, 0); } } }); // --------------------------------------------------------------------------- // Deterministic retry behavior — the house vm-extraction technique: the REAL // module source runs with a fake crypto whose randomInt emits a scripted // sequence, so the bounded retry is exercised exactly, not statistically. // --------------------------------------------------------------------------- function loadModuleWithScriptedRandomInt(sequence) { let cursor = 0; const draws = []; const fakeCrypto = { randomInt(min, max) { assert.equal(min, MIN, "the generator must draw from the established floor"); assert.equal(max, MAX, "the generator must draw from the established ceiling"); const value = cursor < sequence.length ? sequence[cursor] : sequence[sequence.length - 1]; cursor += 1; draws.push(value); return value; }, }; const module_ = { exports: {} }; const ctx = vm.createContext({ require: (name) => { assert.equal(name, "node:crypto", "the module must depend only on node:crypto"); return fakeCrypto; }, module: module_, exports: module_.exports, }); vm.runInContext(fs.readFileSync(MODULE_JS, "utf8"), ctx, { filename: "utils/session-epoch.js" }); return { exports: module_.exports, draws }; } test("retry behavior: a candidate colliding with the current generation is REDRAWN, not returned", () => { // Current generation 7; the CSPRNG script emits 7 twice (collisions) then 9. const { exports, draws } = loadModuleWithScriptedRandomInt([7, 7, 9]); const v = exports.newSessionEpochDifferentFrom(7); assert.equal(v, 9, "the colliding candidates must be redrawn until one differs"); assert.deepEqual(draws, [7, 7, 9], "exactly three draws were consumed (two collisions + the winner)"); }); test("retry behavior: a legacy-row current (0) never collides — the first draw wins", () => { const { exports, draws } = loadModuleWithScriptedRandomInt([5]); const v = exports.newSessionEpochDifferentFrom(0); assert.equal(v, 5); assert.deepEqual(draws, [5], "0 is the column default, never a real generation — no redraw needed"); }); test("retry behavior: newSessionEpoch itself is a single draw", () => { const { exports, draws } = loadModuleWithScriptedRandomInt([123456]); assert.equal(exports.newSessionEpoch(), 123456); assert.deepEqual(draws, [123456]); });