Files
bugpass/build/test-bg.mjs
T
2026-10-18 02:15:25 -05:00

33 lines
2.2 KiB
JavaScript

// background.js message guard + lock/fill flow, with a mocked `browser`.
import assert from "node:assert/strict";
globalThis.window = globalThis;
const sent = [];
const ROOT = "moz-extension://uuid/";
globalThis.browser = {
runtime: { id: "bugpass@local", getURL: (p) => ROOT + p, onMessage: { addListener() {} }, sendNativeMessage: async () => ({ profiles: [] }) },
tabs: { get: async (id) => ({ id, url: id === 1 ? "https://login.example.org/x" : "https://evil.test/" }), sendMessage: async (...a) => { sent.push(a); } },
};
const { onMessage } = await import("./background.js");
const ext = { id: "bugpass@local", url: ROOT + "popup.html" };
const page = { id: "bugpass@local", url: "https://example.org/", tab: { id: 1 } };
const profile = { site: "example.org", login: "[email protected]", lowercase: true, uppercase: true, digits: true, symbols: true, length: 16, counter: 1 };
for (const type of ["status", "unlock", "reveal", "fill", "copy", "list", "relay"])
await assert.rejects(onMessage({ type, master: "x", profile, tabId: 1 }, page), /forbidden/);
assert.deepEqual(await onMessage({ type: "status" }, ext), { unlocked: false });
assert.deepEqual(await onMessage({ type: "reveal", profile }, ext), { locked: true });
assert.deepEqual(await onMessage({ type: "fill", tabId: 1, profile }, ext), { locked: true });
await assert.rejects(onMessage({ type: "unlock", master: "" }, ext));
assert.equal((await onMessage({ type: "unlock", master: "password" }, ext)).fingerprint.length, 3);
assert.deepEqual(await onMessage({ type: "reveal", profile }, ext), { password: "WHLpUL)e00[iHR+w" });
await assert.rejects(onMessage({ type: "fill", tabId: 2, profile }, ext), /not on/);
assert.equal(sent.length, 0);
await onMessage({ type: "fill", tabId: 1, profile }, ext);
assert.deepEqual(sent[0], [1, { type: "fill", username: profile.login, password: "WHLpUL)e00[iHR+w", toast: false }, { frameId: 0 }]);
await onMessage({ type: "relay", msg: { type: "fill", password: "x" } }, { ...ext, tab: { id: 1 } });
assert.equal(sent.length, 1, "relay must not forward fill");
await onMessage({ type: "lock" }, ext);
assert.deepEqual(await onMessage({ type: "reveal", profile }, ext), { locked: true });
console.log("bugpass: background guard ok");
process.exit(0); // idle auto-lock timer