/** * @license * Copyright 2022-2026 Matter.js Authors * SPDX-License-Identifier: Apache-2.0 */ import { Fabric } from "#fabric/Fabric.js"; import { FabricManager } from "#fabric/FabricManager.js"; import { SessionParameters } from "#index.js"; import { PeerAddress } from "#peer/PeerAddress.js"; import { SessionClosedError } from "#protocol/errors.js"; import type { MessageExchange } from "#protocol/MessageExchange.js"; import { DuplicateMessageError } from "#protocol/MessageReceptionState.js"; import { NodeSession } from "#session/NodeSession.js"; import { SessionManager } from "#session/SessionManager.js"; import { b$, Bytes, Environment, Hours, ImplementationError, InternalError, Key, Lifetime, Logger, LogLevel, MemoryStorageDriver, Millis, NetworkError, PrivateKey, Seconds, StandardCrypto, StorageContext, StorageManager, SupportedStorageTypes, Time, Timestamp, } from "@matter/general"; import { CaseAuthenticatedTag, FabricId, FabricIndex, GlobalFabricId, NodeId, VendorId } from "@matter/types"; const DUMMY_BYTEARRAY = new Uint8Array(); const TEST_ROOT_PUBLIC_KEY = Bytes.fromHex( "044a9f42b1ca4840d37292bbc7f6a7e11e22200c976fc900dbc98a7a383a641cb8254a2e56d4e295a847943b4e3897c4a773e930277b4d9fbede8a052686bfacfa", ); const TEST_IDENTITY_PROTECTION_KEY = Bytes.fromHex("9bc61cd9c62a2df6d64dfcaa9dc472d4"); const SEC1_KEY = Bytes.fromHex( "30770201010420aef3484116e9481ec57be0472df41bf499064e5024ad869eca5e889802d48075a00a06082a8648ce3d030107a144034200043c398922452b55caf389c25bd1bca4656952ccb90e8869249ad8474653014cbf95d687965e036b521c51037e6b8cedefca1eb44046694fa08882eed6519decba", ); function testFabric(crypto: StandardCrypto, fabricStorage: StorageContext, fabricIndex = FabricIndex(1)) { const fabric = new Fabric(crypto, { fabricIndex, fabricId: FabricId(BigInt("0x456789ABCDEF1234") + BigInt(fabricIndex - 1)), nodeId: NodeId(1), rootNodeId: NodeId(1), globalId: GlobalFabricId(fabricIndex - 1), keyPair: Key({ sec1: SEC1_KEY }) as PrivateKey, rootPublicKey: TEST_ROOT_PUBLIC_KEY, rootVendorId: VendorId(0), rootCert: new Uint8Array(), identityProtectionKey: new Uint8Array(), operationalIdentityProtectionKey: TEST_IDENTITY_PROTECTION_KEY, intermediateCACert: new Uint8Array(), operationalCert: new Uint8Array(), label: "", }); fabric.storage = fabricStorage; return fabric; } describe("SessionManager", () => { describe("getNextAvailableSessionId", () => { let storage: MemoryStorageDriver; let storageContext: StorageContext; let sessionManager: SessionManager; beforeEach(async () => { storage = new MemoryStorageDriver(); storage.initialize(); storageContext = new StorageContext(storage, ["context"]); sessionManager = new SessionManager({ parameters: {} as SessionParameters, fabrics: new FabricManager(new StandardCrypto()), storage: storageContext, }); await sessionManager.construction.ready; }); it("next number is increasing", async () => { let first = await sessionManager.getNextAvailableSessionId(); if (first === 0xffff) { // Keep test simple and just ignore the special case and let it overflow first = await sessionManager.getNextAvailableSessionId(); } const second = await sessionManager.getNextAvailableSessionId(); expect(first + 1).to.equal(second); }); it("verify that id is 1 after being 0xffff", async () => { const first = await sessionManager.getNextAvailableSessionId(); if (first === 0xffff) { expect(await sessionManager.getNextAvailableSessionId()).to.equal(1); } else { for (let i = first; i < 0xfffe; i++) { // read over until one before overrun await sessionManager.getNextAvailableSessionId(); } expect(await sessionManager.getNextAvailableSessionId()).to.equal(0xffff); expect(await sessionManager.getNextAvailableSessionId()).to.equal(1); } }); it("verify that existing session ids are skipped", async () => { let first = await sessionManager.getNextAvailableSessionId(); if (first === 0xfffe) { // Keep test simple and just ignore the special case and let it overflow first = await sessionManager.getNextAvailableSessionId(); } if (first === 0xffff) { // Keep test simple and just ignore the special case and let it overflow first = await sessionManager.getNextAvailableSessionId(); } // Create a session with "next expected number" await sessionManager.createSecureSession({ id: first + 1, fabric: undefined, peerNodeId: NodeId.UNSPECIFIED_NODE_ID, peerSessionId: 0x8d4b, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: false, isResumption: false, }); expect(await sessionManager.getNextAvailableSessionId()).to.equal(first + 2); }); it("verify that oldest session gets closed when no more ids are available", async () => { // Reduce ID space range so this test takes a reasonable amount of time. Otherwise it takes 4x the time of // all other nodejs tests combined sessionManager.compressIdRange(0xff); const first = await sessionManager.getNextAvailableSessionId(); let firstClosed = false; sessionManager.sessions.deleted.on(() => { firstClosed = true; }); await sessionManager.createSecureSession({ id: first, fabric: undefined, peerNodeId: NodeId.UNSPECIFIED_NODE_ID, peerSessionId: 0x8d4b, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: false, isResumption: false, }); await MockTime.advance(1000); for (let i = 0; i < 0xfe; i++) { const sessionId = await sessionManager.getNextAvailableSessionId(); await sessionManager.createSecureSession({ id: sessionId, fabric: undefined, peerNodeId: NodeId.UNSPECIFIED_NODE_ID, peerSessionId: 0x8d4b, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: false, isResumption: false, }); } expect(await sessionManager.getNextAvailableSessionId()).to.equal(first); expect(firstClosed).to.be.true; }); describe("with a fixed random seed", () => { class FixedSeedCrypto extends StandardCrypto { constructor(readonly seed: number) { super(); } override get randomUint16() { return this.seed; } override get randomUint32() { return this.seed; } } async function managerWithSeed(seed: number) { const manager = new SessionManager({ parameters: {} as SessionParameters, fabrics: new FabricManager(new FixedSeedCrypto(seed)), storage: storageContext, }); await manager.construction.ready; return manager; } async function occupy(manager: SessionManager, id: number) { await manager.createSecureSession({ id, fabric: undefined, peerNodeId: NodeId.UNSPECIFIED_NODE_ID, peerSessionId: 0x8d4b, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: false, isResumption: false, }); } it("never allocates 0 when the random seed is 0", async () => { const manager = await managerWithSeed(0); expect(await manager.getNextAvailableSessionId()).to.equal(1); expect(await manager.getNextAvailableSessionId()).to.equal(2); }); it("stays within the ID range after reusing the session with the highest ID", async () => { const manager = await managerWithSeed(0); manager.compressIdRange(3); const [first, second, third] = [ await manager.getNextAvailableSessionId(), await manager.getNextAvailableSessionId(), await manager.getNextAvailableSessionId(), ]; expect([first, second, third]).to.deep.equal([1, 2, 3]); await occupy(manager, third); await MockTime.advance(1000); await occupy(manager, first); await occupy(manager, second); const reused = await manager.getNextAvailableSessionId(); expect(reused).to.equal(3); await occupy(manager, reused); expect(await manager.getNextAvailableSessionId()).to.be.within(1, 3); }); }); }); describe("maybeSessionFor", () => { let storage: MemoryStorageDriver; let storageContext: StorageContext; let sessionManager: SessionManager; beforeEach(async () => { storage = new MemoryStorageDriver(); storage.initialize(); storageContext = new StorageContext(storage, ["context"]); sessionManager = new SessionManager({ parameters: {} as SessionParameters, fabrics: new FabricManager(new StandardCrypto()), storage: storageContext, }); await sessionManager.construction.ready; }); it("returns session with most recent activeTimestamp, not timestamp", async () => { const PEER_NODE_ID = NodeId(0x1234n); const PEER_ADDRESS = { fabricIndex: FabricIndex(0), nodeId: PEER_NODE_ID }; // Session A: recently active on sends (high timestamp) but peer hasn't talked to us recently const sessionA = await sessionManager.createSecureSession({ id: 0x0100, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: 0x0001, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: true, isResumption: false, }); // Session B: peer has more recently communicated with us (higher activeTimestamp) const sessionB = await sessionManager.createSecureSession({ id: 0x0200, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: 0x0002, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: true, isResumption: false, }); // Manipulate timestamps: sessionA has higher timestamp (from sends) but lower activeTimestamp sessionA.timestamp = Timestamp(2000); sessionA.activeTimestamp = Timestamp(100); // sessionB has lower timestamp but higher activeTimestamp (peer was recently heard from) sessionB.timestamp = Timestamp(1000); sessionB.activeTimestamp = Timestamp(200); const result = sessionManager.maybeSessionFor(PEER_ADDRESS); expect(result).to.equal(sessionB); }); it("closes the peer's least recently used session beyond the per-peer cap", async () => { const PEER_NODE_ID = NodeId(0x1234n); const sessions = new Array(); for (let i = 0; i < 6; i++) { sessions.push( await sessionManager.createSecureSession({ id: 0x0100 + i, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: 0x0001 + i, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: false, // the peer established these; we are the responder isResumption: false, }), ); // Least-recently-used ordering decides who goes sessions[i].timestamp = Timestamp(1000 + i); } await MockTime.yield3(); expect(sessions[0].isClosing).is.true; expect(sessions.slice(1).filter(session => session.isClosing)).is.empty; }); it("never evicts a session we initiated, even when it is the least recently used", async () => { const PEER_NODE_ID = NodeId(0x9abcn); // Ours, and the oldest -- a naive least-recently-used sweep would take this one first const ours = await sessionManager.createSecureSession({ id: 0x0300, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: 0x0021, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: true, isResumption: false, }); ours.timestamp = Timestamp(1); const theirs = new Array(); for (let i = 0; i < 6; i++) { theirs.push( await sessionManager.createSecureSession({ id: 0x0301 + i, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: 0x0031 + i, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: false, isResumption: false, }), ); theirs[i].timestamp = Timestamp(1000 + i); } await MockTime.yield3(); expect(ours.isClosing).is.false; expect(theirs[0].isClosing).is.true; expect(theirs.slice(1).filter(session => session.isClosing)).is.empty; }); it("leaves sessions we initiated alone; peer loss reclaims those", async () => { const PEER_NODE_ID = NodeId(0x5678n); const sessions = new Array(); for (let i = 0; i < 6; i++) { sessions.push( await sessionManager.createSecureSession({ id: 0x0200 + i, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: 0x0011 + i, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: true, isResumption: false, }), ); sessions[i].timestamp = Timestamp(1000 + i); } await MockTime.yield3(); expect(sessions.filter(session => session.isClosing)).is.empty; }); it("conveys the initiating exchange when reporting peer loss", async () => { const PEER_NODE_ID = NodeId(0x1234n); const PEER_ADDRESS = { fabricIndex: FabricIndex(0), nodeId: PEER_NODE_ID }; const session = await sessionManager.createSecureSession({ id: 0x0100, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: 0x0001, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: true, isResumption: false, }); const currentExchange = {} as MessageExchange; const received = new Array(); session.subscriptions.add({ subscriptionId: 1, isTerminated: false, async handlePeerCancel() {}, async close(_flushViaSession, exchange) { received.push(exchange); }, async closeForShutdown() {}, }); await sessionManager.handlePeerLoss(PEER_ADDRESS, { cause: new Error("unresponsive"), currentExchange, }); expect(received[0]).equals(currentExchange); }); }); describe("session supersession", () => { const PEER_NODE_ID = NodeId(0x1234n); const PEER_ADDRESS = PeerAddress({ fabricIndex: FabricIndex.NO_FABRIC, nodeId: PEER_NODE_ID }); const [CAT_A, CAT_B] = [CaseAuthenticatedTag(0x0001_0001), CaseAuthenticatedTag(0x0002_0001)]; let sessionManager: SessionManager; let nextId: number; beforeEach(async () => { const storage = new MemoryStorageDriver(); storage.initialize(); sessionManager = new SessionManager({ fabrics: new FabricManager(new StandardCrypto()), storage: new StorageContext(storage, ["context"]), }); await sessionManager.construction.ready; nextId = 0x0100; }); function session( options: { peerNodeId?: NodeId; caseAuthenticatedTags?: CaseAuthenticatedTag[]; isInitiator?: boolean; establishing?: true; } = {}, ) { const id = nextId++; return sessionManager.createSecureSession({ id, fabric: undefined, peerNodeId: options.peerNodeId ?? PEER_NODE_ID, peerSessionId: id, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: options.isInitiator ?? false, isResumption: false, caseAuthenticatedTags: options.caseAuthenticatedTags, establishing: options.establishing, }); } /** The sessions that superseded {@link older}, in order. */ function supersessionsOf(older: NodeSession) { const newer = new Array(); older.superseded.on(session => { newer.push(session); }); return newer; } /** Start closing {@link session} and keep it in the closing state until the returned function runs. */ function holdClosing(session: NodeSession) { let release!: () => void; const closed = session.initiateClose(() => new Promise(resolve => (release = resolve))); return async () => { release(); await closed; }; } it("supersedes an older session with the same peer and CATs in any order", async () => { const older = await session({ caseAuthenticatedTags: [CAT_A, CAT_B] }); const superseded = supersessionsOf(older); const newer = await session({ caseAuthenticatedTags: [CAT_B, CAT_A, CAT_B] }); expect(superseded).deep.equals([newer]); }); it("does not supersede a session with other CATs", async () => { const superseded = supersessionsOf(await session({ caseAuthenticatedTags: [CAT_A] })); await session({ caseAuthenticatedTags: [CAT_A, CAT_B] }); await session({ caseAuthenticatedTags: [CAT_B] }); await session(); expect(superseded).deep.equals([]); }); it("supersedes a session of the other role with equal CATs", async () => { const initiated = await session({ isInitiator: true, caseAuthenticatedTags: [CAT_A] }); const ofInitiated = supersessionsOf(initiated); const responded = await session({ caseAuthenticatedTags: [CAT_A] }); const ofResponded = supersessionsOf(responded); const initiatedAgain = await session({ isInitiator: true, caseAuthenticatedTags: [CAT_A] }); expect(ofInitiated).deep.equals([responded, initiatedAgain]); expect(ofResponded).deep.equals([initiatedAgain]); }); it("does not supersede a session of the other role with other CATs", async () => { const ofInitiated = supersessionsOf(await session({ isInitiator: true, caseAuthenticatedTags: [CAT_A] })); const ofResponded = supersessionsOf(await session({ caseAuthenticatedTags: [CAT_B] })); await session({ isInitiator: true, caseAuthenticatedTags: [CAT_A, CAT_B] }); expect(ofInitiated).deep.equals([]); expect(ofResponded).deep.equals([]); }); it("does not supersede a session with another peer", async () => { const superseded = supersessionsOf(await session()); await session({ peerNodeId: NodeId(0x5678n) }); expect(superseded).deep.equals([]); }); it("does not supersede a PASE session", async () => { const superseded = supersessionsOf(await session({ peerNodeId: NodeId.UNSPECIFIED_NODE_ID })); await session({ peerNodeId: NodeId.UNSPECIFIED_NODE_ID }); expect(superseded).deep.equals([]); }); it("does not supersede a closing session", async () => { const older = await session(); const superseded = supersessionsOf(older); const release = holdClosing(older); await session(); expect(superseded).deep.equals([]); await release(); }); it("does not supersede a session that is still establishing", async () => { const establishing = await session({ establishing: true }); const superseded = supersessionsOf(establishing); await session(); expect(superseded).deep.equals([]); }); it("supersedes nothing while establishing and the older sessions once established", async () => { const older = await session(); const superseded = supersessionsOf(older); const newer = await session({ establishing: true }); const ofNewer = supersessionsOf(newer); expect(superseded).deep.equals([]); newer.completeEstablishment(); expect(superseded).deep.equals([newer]); expect(ofNewer).deep.equals([]); }); it("orders supersession by completion, not by registration", async () => { const older = await session(); const ofOlder = supersessionsOf(older); const resumed = await session({ establishing: true }); const ofResumed = supersessionsOf(resumed); const newer = await session(); const ofNewer = supersessionsOf(newer); resumed.completeEstablishment(); expect(ofOlder).deep.equals([newer, resumed]); expect(ofNewer).deep.equals([resumed]); expect(ofResumed).deep.equals([]); }); it("does not establish a session that is closing", async () => { const superseded = supersessionsOf(await session()); const establishing = await session({ establishing: true }); const release = holdClosing(establishing); expect(establishing.completeEstablishment()).false; expect(establishing.isEstablishing).true; expect(superseded).deep.equals([]); await release(); }); it("completes establishment once and shows observers an established session", async () => { const superseded = supersessionsOf(await session()); const establishing = await session({ establishing: true }); const seenEstablishing = new Array(); establishing.established.on(() => { seenEstablishing.push(establishing.isEstablishing); }); expect(establishing.completeEstablishment()).true; expect(establishing.completeEstablishment()).true; expect(seenEstablishing).deep.equals([false]); expect(superseded).deep.equals([establishing]); }); it("finds an establishing session only once it is established", async () => { const older = await session(); const establishing = await session({ establishing: true }); establishing.activeTimestamp = Timestamp(older.activeTimestamp + 1); expect(sessionManager.maybeSessionFor(PEER_ADDRESS)).equals(older); establishing.completeEstablishment(); expect(sessionManager.maybeSessionFor(PEER_ADDRESS)).equals(establishing); }); it("supersedes the least recently used session before it evicts it", async () => { const older = await session(); older.timestamp = Timestamp(0); const events = new Array(); older.superseded.on(() => { events.push("superseded"); }); older.closing.on(() => { events.push("closing"); }); for (let i = 0; i < 4; i++) { await session({ caseAuthenticatedTags: [CAT_A] }); } const newer = await session({ establishing: true }); await MockTime.yield3(); expect(older.isClosing).false; newer.completeEstablishment(); await MockTime.yield3(); expect(events).deep.equals(["superseded", "closing"]); }); it("lists an establishing session in sessions only once it is established", async () => { const added = new Array(); sessionManager.sessions.added.on(session => { added.push(session); }); const establishing = await session({ establishing: true }); expect(sessionManager.sessions.has(establishing)).false; expect(sessionManager.registeredSessions.has(establishing)).true; expect(sessionManager.getSession(establishing.id)).equals(establishing); expect(added).deep.equals([]); establishing.completeEstablishment(); expect(sessionManager.sessions.has(establishing)).true; expect(added).deep.equals([establishing]); }); it("lists a session in sessions only after every observer of registeredSessions saw it", async () => { const events = new Array(); sessionManager.sessions.added.on(() => { events.push("sessions"); }); sessionManager.registeredSessions.added.on(() => { events.push("registeredSessions"); }); await session(); expect(events).deep.equals(["registeredSessions", "sessions"]); }); it("never lists a session whose handshake fails", async () => { const events = new Array(); sessionManager.sessions.added.on(() => { events.push("added"); }); sessionManager.sessions.deleted.on(() => { events.push("deleted"); }); const establishing = await session({ establishing: true }); await establishing.initiateForceClose({ cause: new SessionClosedError("test") }); expect(establishing.completeEstablishment()).false; expect(sessionManager.registeredSessions.has(establishing)).false; expect(sessionManager.sessions.has(establishing)).false; expect(events).deep.equals([]); }); it("characterization: does not list a session that establishes after it was disposed", async () => { const establishing = await session({ establishing: true }); await establishing[Symbol.asyncDispose](); expect(establishing.completeEstablishment()).true; expect(sessionManager.registeredSessions.has(establishing)).false; expect(sessionManager.sessions.has(establishing)).false; }); it("reports an established session that is closing as established", async () => { const established = await session(); const release = holdClosing(established); expect(established.completeEstablishment()).true; await release(); }); it("completes establishment when an observer of sessions throws", async () => { sessionManager.sessions.added.on(() => { throw new InternalError("test: observer fails"); }); const establishing = await session({ establishing: true }); expect(establishing.completeEstablishment()).true; expect(establishing.isEstablishing).false; expect(sessionManager.sessions.has(establishing)).true; }); it("lists a session in sessions when an observer of its establishment throws", async () => { sessionManager.registeredSessions.added.on(added => { added.established.on(() => { throw new InternalError("test: observer fails"); }); }); const establishing = await session({ establishing: true }); expect(establishing.completeEstablishment()).true; expect(sessionManager.sessions.has(establishing)).true; }); it("notifies every observer of supersession when one throws", async () => { const older = await session(); older.superseded.on(() => { throw new InternalError("test: observer fails"); }); const superseded = supersessionsOf(older); const newer = await session(); expect(superseded).deep.equals([newer]); }); it("neither counts nor evicts an establishing session for the per-peer cap", async () => { const sessions = new Array(); for (let i = 0; i < 3; i++) { sessions.push(await session()); } const establishing = await session({ establishing: true }); establishing.timestamp = Timestamp(0); sessions.push(await session()); sessions.push(await session()); await MockTime.yield3(); expect([...sessions, establishing].filter(session => session.isClosing)).deep.equals([]); }); }); describe("log attribution", () => { // Session eviction runs from a timer or transport callback, so nothing on the call stack says which node // it belongs to. A process running several nodes reads these lines only if the message names its owner. it("names the originating environment on the lines an eviction produces", async () => { const storage = new MemoryStorageDriver(); storage.initialize(); const storageManager = new StorageManager(storage); await storageManager.initialize(); const environment = new Environment("test", Environment.default); environment.set(StorageManager, storageManager); environment.set(FabricManager, new FabricManager(new StandardCrypto())); // Through the environment rather than by construction, so the logger the environment installs is the // one under test const sessionManager = environment.get(SessionManager); await sessionManager.construction.ready; const dest = Logger.destinations.default; const original = { ...dest }; const origins = new Array(); // The level is process-global and other suites move it; the line under test is INFO dest.level = LogLevel.INFO; dest.add = message => { if (String(message.values[1]).startsWith("Closing least recently used session")) { origins.push(message.origin); } }; try { const PEER_NODE_ID = NodeId(0x4321n); for (let i = 0; i < 6; i++) { const session = await sessionManager.createSecureSession({ id: 0x0200 + i, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: 0x0001 + i, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: false, isResumption: false, }); session.timestamp = Timestamp(1000 + i); } await MockTime.yield3(); } finally { Object.assign(Logger.destinations.default, original); await sessionManager.close(); } // Identity, not deep equality: an origin is a plain name-and-parent record, so deep equality holds // between any two environments named alike and would accept attribution to the wrong node expect(origins.length).equals(1); expect(origins[0]).equals(environment.logOrigin); }); }); describe("group data message counter", () => { it("exposes one node-global counter shared across group sessions", async () => { const storage = new MemoryStorageDriver(); storage.initialize(); const sessionManager = new SessionManager({ parameters: {} as SessionParameters, fabrics: new FabricManager(new StandardCrypto()), storage: new StorageContext(storage, ["sessions"]), }); await sessionManager.construction.ready; const first = await sessionManager.groupDataMessageCounter.getIncrementedCounter(); const second = await sessionManager.groupDataMessageCounter.getIncrementedCounter(); expect(second).equal(first + 1); }); it("closes the counter it replaces when cleared", async () => { const storage = new MemoryStorageDriver(); storage.initialize(); const sessionManager = new SessionManager({ parameters: {} as SessionParameters, fabrics: new FabricManager(new StandardCrypto()), storage: new StorageContext(storage, ["sessions"]), }); await sessionManager.construction.ready; const counterLifetimes = () => { using probe = Lifetime.process.join("probe"); return [...(probe.owner?.spans ?? [])].filter(({ name }) => name === "persisted message counter") .length; }; const before = counterLifetimes(); await sessionManager.clear(); expect(counterLifetimes()).equal(before); await sessionManager.close(); expect(counterLifetimes()).equal(before - 1); }); it("seeds the global counter above legacy per-key counters and clears them (Q-02 migration)", async () => { const crypto = new StandardCrypto(); const storage = new MemoryStorageDriver(); storage.initialize(); const fabricManager = new FabricManager(crypto); await fabricManager.construction.ready; const fabricStorage = new StorageContext(storage, ["fabric"]); const fabric = testFabric(crypto, fabricStorage); fabricManager.addFabric(fabric); // Simulate a legacy per-operational-key data counter persisted by the old model. const legacyKey = `${"a".repeat(32)}-data`; await fabricStorage.set(legacyKey, 4242); const sessionManager = new SessionManager({ parameters: {} as SessionParameters, fabrics: fabricManager, storage: new StorageContext(storage, ["sessions"]), }); await sessionManager.construction.ready; expect(await sessionManager.groupDataMessageCounter.getIncrementedCounter()).greaterThan(4242); expect(await fabricStorage.has(legacyKey)).equal(false); }); it("does not roll the counter back when a group key set is removed (Q-02)", async () => { const crypto = new StandardCrypto(); const storage = new MemoryStorageDriver(); storage.initialize(); const fabricManager = new FabricManager(crypto); await fabricManager.construction.ready; const fabric = testFabric(crypto, new StorageContext(storage, ["fabric"])); fabricManager.addFabric(fabric); await fabric.groups.setFromGroupKeySet({ groupKeySetId: 1, groupKeySecurityPolicy: 0, epochKey0: b$`000102030405060708090a0b0c0d0e0f`, epochStartTime0: 1, epochKey1: null, epochStartTime1: null, epochKey2: null, epochStartTime2: null, groupKeyMulticastPolicy: 0, }); const sessionManager = new SessionManager({ parameters: {} as SessionParameters, fabrics: fabricManager, storage: new StorageContext(storage, ["sessions"]), }); await sessionManager.construction.ready; const before = await sessionManager.groupDataMessageCounter.getIncrementedCounter(); fabric.groups.removeGroupKeySet(1); const after = await sessionManager.groupDataMessageCounter.getIncrementedCounter(); expect(after).greaterThan(before); }); const keySet = (groupKeySetId: number, epochKey0: Bytes) => ({ groupKeySetId, groupKeySecurityPolicy: 0, epochKey0, epochStartTime0: 1, epochKey1: null, epochStartTime1: null, epochKey2: null, epochStartTime2: null, groupKeyMulticastPolicy: 0, }); it("forgets group message reception state when its key set is removed so a reused key re-syncs", async () => { const crypto = new StandardCrypto(); const storage = new MemoryStorageDriver(); storage.initialize(); const fabricManager = new FabricManager(crypto); await fabricManager.construction.ready; const fabric = testFabric(crypto, new StorageContext(storage, ["fabric"])); fabricManager.addFabric(fabric); const epochKey = b$`000102030405060708090a0b0c0d0e0f`; await fabric.groups.setFromGroupKeySet(keySet(1, epochKey)); const source = NodeId(2); const operationalKey = fabric.groups.keySets.get("groupKeySetId", 1)!.operationalEpochKey0; // Advance the replay window; a lower counter on the retained state is then a replay. fabric.groups.messaging.receptionStateFor(source, operationalKey).updateMessageCounter(1000); expect(() => fabric.groups.messaging.receptionStateFor(source, operationalKey).updateMessageCounter(500), ).throws(DuplicateMessageError); // Remove and re-provision the identical epoch key (same derived operational key). fabric.groups.removeGroupKeySet(1); await fabric.groups.setFromGroupKeySet(keySet(1, epochKey)); const reusedKey = fabric.groups.keySets.get("groupKeySetId", 1)!.operationalEpochKey0; expect(Bytes.toHex(reusedKey)).equals(Bytes.toHex(operationalKey)); // The fresh window syncs on the first (lower) counter instead of rejecting it as a replay. expect(() => fabric.groups.messaging.receptionStateFor(source, reusedKey).updateMessageCounter(500), ).not.throws(); }); it("keeps reception state until the last key set sharing an operational key is removed", async () => { const crypto = new StandardCrypto(); const storage = new MemoryStorageDriver(); storage.initialize(); const fabricManager = new FabricManager(crypto); await fabricManager.construction.ready; const fabric = testFabric(crypto, new StorageContext(storage, ["fabric"])); fabricManager.addFabric(fabric); const epochKey = b$`000102030405060708090a0b0c0d0e0f`; await fabric.groups.setFromGroupKeySet(keySet(1, epochKey)); await fabric.groups.setFromGroupKeySet(keySet(2, epochKey)); const source = NodeId(2); const operationalKey = fabric.groups.keySets.get("groupKeySetId", 1)!.operationalEpochKey0; fabric.groups.messaging.receptionStateFor(source, operationalKey).updateMessageCounter(1000); // One of two key sets shares the derived key, so its reception window must survive this removal. fabric.groups.removeGroupKeySet(1); expect(() => fabric.groups.messaging.receptionStateFor(source, operationalKey).updateMessageCounter(500), ).throws(DuplicateMessageError); // Removing the last referencing key set forgets it. fabric.groups.removeGroupKeySet(2); expect(() => fabric.groups.messaging.receptionStateFor(source, operationalKey).updateMessageCounter(500), ).not.throws(); }); it("forgets the previous operational key when a key set is rewritten with a new epoch key", async () => { const crypto = new StandardCrypto(); const storage = new MemoryStorageDriver(); storage.initialize(); const fabricManager = new FabricManager(crypto); await fabricManager.construction.ready; const fabric = testFabric(crypto, new StorageContext(storage, ["fabric"])); fabricManager.addFabric(fabric); const epochA = b$`000102030405060708090a0b0c0d0e0f`; const epochB = b$`0f0e0d0c0b0a09080706050403020100`; await fabric.groups.setFromGroupKeySet(keySet(1, epochA)); const source = NodeId(2); const operationalA = fabric.groups.keySets.get("groupKeySetId", 1)!.operationalEpochKey0; fabric.groups.messaging.receptionStateFor(source, operationalA).updateMessageCounter(1000); // In-place rewrite with a different epoch key drops key A, so its window must be forgotten. await fabric.groups.setFromGroupKeySet(keySet(1, epochB)); expect(() => fabric.groups.messaging.receptionStateFor(source, operationalA).updateMessageCounter(500), ).not.throws(); }); }); describe("active threshold validation", () => { function newManager(parameters: Partial) { const storage = new MemoryStorageDriver(); storage.initialize(); return new SessionManager({ parameters: parameters as SessionParameters, fabrics: new FabricManager(new StandardCrypto()), storage: new StorageContext(storage, ["context"]), }); } it("rejects a local active threshold over 65535 milliseconds on construction", () => { expect(() => newManager({ activeThreshold: Millis(65536) })).throws( ImplementationError, "Active Threshold", ); }); it("rejects a local active threshold over 65535 milliseconds via the setter", async () => { const sessionManager = newManager({}); await sessionManager.construction.ready; expect(() => (sessionManager.sessionParameters = { activeThreshold: Millis(65536) })).throws( ImplementationError, "Active Threshold", ); }); it("accepts a local active threshold of exactly 65535 milliseconds", async () => { const sessionManager = newManager({ activeThreshold: Millis(65535) }); await sessionManager.construction.ready; expect(sessionManager.sessionParameters.activeThreshold).equal(Millis(65535)); }); }); describe("max paths per invoke validation", () => { function newManager(parameters: Partial) { const storage = new MemoryStorageDriver(); storage.initialize(); return new SessionManager({ parameters: parameters as SessionParameters, fabrics: new FabricManager(new StandardCrypto()), storage: new StorageContext(storage, ["context"]), }); } it("rejects a local max paths per invoke of zero on construction", () => { expect(() => newManager({ maxPathsPerInvoke: 0 })).throws(ImplementationError, "Max Paths Per Invoke"); }); it("rejects a local max paths per invoke of zero via the setter", async () => { const sessionManager = newManager({}); await sessionManager.construction.ready; expect(() => (sessionManager.sessionParameters = { maxPathsPerInvoke: 0 })).throws( ImplementationError, "Max Paths Per Invoke", ); }); it("rejects a local max paths per invoke that is not a whole count", async () => { const sessionManager = newManager({}); await sessionManager.construction.ready; expect(() => (sessionManager.sessionParameters = { maxPathsPerInvoke: 2.5 })).throws( ImplementationError, "Max Paths Per Invoke", ); }); it("accepts a local max paths per invoke of one", async () => { const sessionManager = newManager({ maxPathsPerInvoke: 1 }); await sessionManager.construction.ready; expect(sessionManager.sessionParameters.maxPathsPerInvoke).equals(1); }); }); describe("session parameter setter", () => { function newManager(parameters: Partial) { const storage = new MemoryStorageDriver(); storage.initialize(); return new SessionManager({ parameters: parameters as SessionParameters, fabrics: new FabricManager(new StandardCrypto()), storage: new StorageContext(storage, ["context"]), }); } it("ignores undefined values and retains current values", async () => { const sessionManager = newManager({ idleInterval: Millis(1000), activeInterval: Millis(700) }); await sessionManager.construction.ready; sessionManager.sessionParameters = { idleInterval: undefined, activeInterval: Millis(600) }; expect(sessionManager.sessionParameters.idleInterval).equal(Millis(1000)); expect(sessionManager.sessionParameters.activeInterval).equal(Millis(600)); }); }); describe("peer loss by registration order", () => { const PEER_NODE_ID = NodeId(0x1234n); const PEER_ADDRESS = PeerAddress({ fabricIndex: FabricIndex(0), nodeId: PEER_NODE_ID }); let sessionManager: SessionManager; beforeEach(async () => { MockTime.reset(); const storage = new MemoryStorageDriver(); storage.initialize(); sessionManager = new SessionManager({ parameters: {} as SessionParameters, fabrics: new FabricManager(new StandardCrypto()), storage: new StorageContext(storage, ["context"]), }); await sessionManager.construction.ready; }); function aSession(id: number, delayManagerRegistration?: true) { return sessionManager.createSecureSession({ id, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: id, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: delayManagerRegistration === true, isResumption: false, delayManagerRegistration, }); } it("keeps the session given to peer shutdown and every session registered after it", async () => { const older = await aSession(0x0100); const kept = await aSession(0x0200); const newer = await aSession(0x0300); expect(older.createdAt).equals(kept.createdAt); await sessionManager.handlePeerShutdown(PEER_ADDRESS, kept); expect(older.isPeerLost).true; expect(kept.isPeerLost).false; expect(newer.isPeerLost).false; }); it("keeps a session that registers after the given registration count although it was created before", async () => { const lost = await aSession(0x0100); const establishing = await aSession(0x0200, true); const registeredBefore = sessionManager.registrationCount; await MockTime.advance(Seconds(1)); sessionManager.registeredSessions.add(establishing); await sessionManager.handlePeerLoss(PEER_ADDRESS, { cause: new NetworkError("unresponsive"), registeredBefore, }); expect(lost.isPeerLost).true; expect(establishing.isPeerLost).false; }); it("keeps a session registered while the peer loss runs", async () => { const lost = await aSession(0x0100); let registeredDuringLoss: NodeSession | undefined; lost.subscriptions.add({ subscriptionId: 1, isTerminated: false, async handlePeerCancel() {}, async close() { registeredDuringLoss = await aSession(0x0200); }, async closeForShutdown() {}, }); await sessionManager.handlePeerLoss(PEER_ADDRESS, { cause: new NetworkError("unresponsive") }); expect(lost.isPeerLost).true; expect(registeredDuringLoss?.isPeerLost).equals(false); }); it("keeps the sessions created at or after a deprecated timestamp given to peer shutdown", async () => { const older = await aSession(0x0100); await MockTime.advance(Seconds(1)); const kept = await aSession(0x0200); await sessionManager.handlePeerShutdown(PEER_ADDRESS, kept.createdAt); expect(older.isPeerLost).true; expect(kept.isPeerLost).false; }); it("returns the sessions of the peer registered at or after a session", async () => { const older = await aSession(0x0100); const since = await aSession(0x0200); const establishing = await aSession(0x0300, true); const newer = await aSession(0x0400); sessionManager.registeredSessions.add(establishing); expect(sessionManager.sessionsRegisteredSince(since)).have.members([since, newer, establishing]); expect(sessionManager.sessionsRegisteredSince(establishing)).have.members([establishing]); expect(sessionManager.sessionsRegisteredSince(older)).have.members([older, since, newer, establishing]); }); it("treats a session that never registered as keeping nothing", async () => { const registered = await aSession(0x0100); const unregistered = await aSession(0x0200, true); expect(sessionManager.sessionsRegisteredSince(unregistered)).deep.equals([]); await sessionManager.handlePeerShutdown(PEER_ADDRESS, unregistered); expect(registered.isPeerLost).true; }); }); describe("wall-clock steps", () => { const PEER_NODE_ID = NodeId(0x1234n); const PEER_ADDRESS = PeerAddress({ fabricIndex: FabricIndex(0), nodeId: PEER_NODE_ID }); let sessionManager: SessionManager; beforeEach(async () => { MockTime.reset(); const storage = new MemoryStorageDriver(); storage.initialize(); sessionManager = new SessionManager({ parameters: {} as SessionParameters, fabrics: new FabricManager(new StandardCrypto()), storage: new StorageContext(storage, ["context"]), }); await sessionManager.construction.ready; }); function aSession(id = 0x0100) { return sessionManager.createSecureSession({ id, fabric: undefined, peerNodeId: PEER_NODE_ID, peerSessionId: id, sharedSecret: DUMMY_BYTEARRAY, salt: DUMMY_BYTEARRAY, isInitiator: false, isResumption: false, }); } it("keeps a peer heard from just now active across a forward step", async () => { const session = await aSession(); expect(session.isPeerActive).true; MockTime.stepWallClock(Hours(1)); expect(session.isPeerActive).true; }); it("keeps a peer silent beyond the active threshold idle across a backward step", async () => { const session = await aSession(); await MockTime.advance(session.parameters.activeThreshold + Seconds(1)); expect(session.isPeerActive).false; MockTime.stepWallClock(-3_600_000); expect(session.isPeerActive).false; }); it("prefers the session heard from last when the clock stepped back between them", async () => { const first = await aSession(0x0100); const second = await aSession(0x0200); first.notifyActivity(true); MockTime.stepWallClock(-3_600_000); await MockTime.advance(Seconds(1)); second.notifyActivity(true); expect(sessionManager.maybeSessionFor(PEER_ADDRESS)).equals(second); }); it("evicts the least recently used session when the clock stepped back since its last traffic", async () => { const sessions = new Array(); for (let i = 0; i < 5; i++) { sessions.push(await aSession(0x0100 + i)); } MockTime.stepWallClock(-3_600_000); await MockTime.advance(Seconds(1)); sessions[0].notifyActivity(false); await aSession(0x0200); await MockTime.yield3(); expect(sessions[0].isClosing).false; expect(sessions[1].isClosing).true; }); it("closes a session on peer loss after the clock stepped back", async () => { const session = await aSession(); MockTime.stepWallClock(-3_600_000); await MockTime.advance(Seconds(1)); await sessionManager.handlePeerLoss(PEER_ADDRESS, { cause: new NetworkError("unresponsive") }); expect(session.isPeerLost).true; }); it("closes a session on peer loss when the clock stepped forward before it was created", async () => { MockTime.stepWallClock(Hours(1)); const session = await aSession(); await MockTime.advance(Seconds(1)); await sessionManager.handlePeerLoss(PEER_ADDRESS, { cause: new NetworkError("unresponsive") }); expect(session.isPeerLost).true; }); it("closes a session on peer shutdown after the clock stepped back", async () => { const session = await aSession(); MockTime.stepWallClock(-3_600_000); await MockTime.advance(Seconds(1)); await sessionManager.handlePeerShutdown(PEER_ADDRESS); expect(session.isPeerLost).true; }); it("reports session activity on the wall clock as it reads now", async () => { await MockTime.advance(0.4); await aSession(); await MockTime.advance(Seconds(5)); MockTime.stepWallClock(Hours(1)); const [info] = sessionManager.getActiveSessionInformation(); const expected = Math.round(Time.nowMs - Seconds(5)); expect(info.lastInteractionTimestamp).equals(expected); expect(info.lastActiveTimestamp).equals(expected); }); it("reports a peer never heard from with a last active time of 0", async () => { const session = await aSession(); session.activeTimestamp = 0; MockTime.stepWallClock(Hours(1)); const [info] = sessionManager.getActiveSessionInformation(); expect(info.lastActiveTimestamp).equals(0); }); }); describe("resumption records", () => { class WriteRecordingStorage extends MemoryStorageDriver { writes = new Array(); override set( contexts: string[], keyOrValues: string | Record, value?: SupportedStorageTypes, ) { const keys = typeof keyOrValues === "string" ? [keyOrValues] : Object.keys(keyOrValues); for (const key of keys) { this.writes.push([...contexts, key].join(".")); } if (typeof keyOrValues === "string") { return super.set(contexts, keyOrValues, value); } return super.set(contexts, keyOrValues); } } const crypto = new StandardCrypto(); let storage: WriteRecordingStorage; let fabrics: FabricManager; let fabric: Fabric; beforeEach(async () => { storage = new WriteRecordingStorage(); storage.initialize(); fabrics = new FabricManager(crypto); await fabrics.construction.ready; fabric = testFabric(crypto, new StorageContext(storage, ["fabric"])); fabrics.addFabric(fabric); }); async function startSessionManager() { const sessionManager = new SessionManager({ fabrics, storage: new StorageContext(storage, ["sessions"]), }); await sessionManager.construction.ready; return sessionManager; } function recordFor(peerNodeId: NodeId, resumptionId: Bytes) { return { sharedSecret: b$`00112233445566778899aabbccddeeff`, resumptionId, fabric, peerNodeId, sessionParameters: SessionParameters(), }; } function storedKeys(...contexts: string[]) { return storage.keys(["sessions", ...contexts]); } it("writes only the changed peer's key", async () => { const sessionManager = await startSessionManager(); await sessionManager.saveResumptionRecord(recordFor(NodeId(0x10), b$`01`)); await sessionManager.saveResumptionRecord(recordFor(NodeId(0x20), b$`02`)); storage.writes.length = 0; await sessionManager.saveResumptionRecord(recordFor(NodeId(0x20), b$`03`)); expect(storage.writes).deep.equal(["sessions.resumption.1.20"]); expect(storedKeys("resumption", "1")).deep.equal(["10", "20"]); expect(storedKeys()).not.include("resumptionRecords"); }); it("restores records after a restart and finds them by their current resumption ID", async () => { const first = await startSessionManager(); await first.saveResumptionRecord(recordFor(NodeId(0x10), b$`01`)); await first.saveResumptionRecord(recordFor(NodeId(0x10), b$`02`)); await first.close(); const sessionManager = await startSessionManager(); expect(sessionManager.findResumptionRecordById(b$`01`)).undefined; expect(sessionManager.findResumptionRecordById(b$`02`)?.peerNodeId).equal(NodeId(0x10)); expect( sessionManager.findResumptionRecordByAddress(fabric.addressOf(NodeId(0x10)))?.resumptionId, ).deep.equal(b$`02`); }); it("forgets a deleted record by address and by resumption ID", async () => { const sessionManager = await startSessionManager(); await sessionManager.saveResumptionRecord(recordFor(NodeId(0x10), b$`01`)); expect(await sessionManager.deleteResumptionRecord(fabric.addressOf(NodeId(0x10)))).equal(true); expect(sessionManager.findResumptionRecordById(b$`01`)).undefined; expect(storedKeys("resumption", "1")).deep.equal([]); }); it("deletes the records of a removed fabric", async () => { const sessionManager = await startSessionManager(); await sessionManager.saveResumptionRecord(recordFor(NodeId(0x10), b$`01`)); await sessionManager.saveResumptionRecord(recordFor(NodeId(0x20), b$`02`)); expect(await sessionManager.deleteResumptionRecordsForFabric(fabric)).equal(true); expect(sessionManager.findResumptionRecordById(b$`02`)).undefined; expect(storedKeys("resumption", "1")).deep.equal([]); }); it("moves the legacy array to per-peer keys and drops records of unknown fabrics", async () => { const { sessionParameters } = recordFor(NodeId(0x10), b$`01`); const legacyRecord = { sharedSecret: b$`00112233445566778899aabbccddeeff`, resumptionId: b$`01`, fabricId: fabric.fabricId, fabricIndex: fabric.fabricIndex, nodeId: NodeId(0x10), peerNodeId: NodeId(0x10), sessionParameters: { ...sessionParameters, supportedTransports: undefined }, }; storage.set(["sessions"], "resumptionRecords", [ legacyRecord, { ...legacyRecord, resumptionId: b$`02`, fabricId: FabricId(0x999), fabricIndex: FabricIndex(9) }, { ...legacyRecord, resumptionId: b$`04`, nodeId: NodeId(0x40), peerNodeId: NodeId(0x40), caseAuthenticatedTags: [CaseAuthenticatedTag(0x00010001)], }, ]); // Written by an earlier per-peer run before a downgrade; the array written since replaces it storage.set(["sessions", "resumption", "1"], "30", { ...legacyRecord, peerNodeId: NodeId(0x30) }); const sessionManager = await startSessionManager(); expect(sessionManager.findResumptionRecordById(b$`01`)?.peerNodeId).equal(NodeId(0x10)); expect(sessionManager.findResumptionRecordById(b$`02`)).undefined; expect(sessionManager.findResumptionRecordByAddress(fabric.addressOf(NodeId(0x30)))).undefined; expect(storedKeys()).not.include("resumptionRecords"); expect(storedKeys("resumption", "1")).deep.equal(["10"]); expect(sessionManager.findResumptionRecordById(b$`04`)).undefined; expect(storage.get(["sessions", "resumption", "1"], "10")).not.have.property("nodeId"); expect(storage.get(["sessions", "resumption", "1"], "10")).have.property("version", 1); }); it("deletes a stored record whose fabric no longer exists", async () => { const first = await startSessionManager(); await first.saveResumptionRecord(recordFor(NodeId(0x10), b$`01`)); await first.close(); storage.set(["sessions", "resumption", "2"], "10", storage.get(["sessions", "resumption", "1"], "10")); await startSessionManager(); expect(storedKeys("resumption", "1")).deep.equal(["10"]); expect(storedKeys("resumption", "2")).deep.equal([]); }); describe("format version", () => { const CATS = [CaseAuthenticatedTag(0x00010001)]; function store( peerNodeId: NodeId, fields: { version?: number; caseAuthenticatedTags?: CaseAuthenticatedTag[] }, ) { storage.set(["sessions", "resumption", "1"], peerNodeId.toString(16), { sharedSecret: b$`00112233445566778899aabbccddeeff`, resumptionId: Bytes.fromHex(peerNodeId.toString(16)), fabricId: fabric.fabricId, fabricIndex: fabric.fabricIndex, peerNodeId, sessionParameters: { ...SessionParameters(), supportedTransports: undefined }, ...fields, }); } function storedVersionOf(peerNodeId: NodeId) { const stored = storage.get(["sessions", "resumption", "1"], peerNodeId.toString(16)); return stored !== null && typeof stored === "object" && "version" in stored ? stored.version : undefined; } function restored(sessionManager: SessionManager, peerNodeId: NodeId) { return sessionManager.findResumptionRecordByAddress(fabric.addressOf(peerNodeId)); } it("deletes records without a version that carry CATs", async () => { store(NodeId(0x10), { caseAuthenticatedTags: CATS }); const sessionManager = await startSessionManager(); expect(restored(sessionManager, NodeId(0x10))).undefined; expect(storedKeys("resumption", "1")).deep.equal([]); }); it("uses records without a version and without CATs and stores them with the version", async () => { store(NodeId(0x10), { caseAuthenticatedTags: [] }); store(NodeId(0x20), {}); const sessionManager = await startSessionManager(); expect(restored(sessionManager, NodeId(0x10))?.caseAuthenticatedTags).deep.equal([]); expect(restored(sessionManager, NodeId(0x20))).not.undefined; expect(storedVersionOf(NodeId(0x10))).equal(1); expect(storedVersionOf(NodeId(0x20))).equal(1); }); it("uses records of the current version with CATs without storing them again", async () => { store(NodeId(0x10), { version: 1, caseAuthenticatedTags: CATS }); storage.writes.length = 0; const sessionManager = await startSessionManager(); expect(restored(sessionManager, NodeId(0x10))?.caseAuthenticatedTags).deep.equal(CATS); expect(storage.writes.filter(key => key.startsWith("sessions.resumption."))).deep.equal([]); }); it("deletes records of another version", async () => { store(NodeId(0x10), { version: 2 }); const sessionManager = await startSessionManager(); expect(restored(sessionManager, NodeId(0x10))).undefined; expect(storedKeys("resumption", "1")).deep.equal([]); }); it("stores saved records with the version", async () => { const sessionManager = await startSessionManager(); await sessionManager.saveResumptionRecord(recordFor(NodeId(0x10), b$`01`)); expect(storedVersionOf(NodeId(0x10))).equal(1); }); }); }); });