Files
order-notification-automation/tests/bpm/vault-retry.test.cjs
T

8 lines
2.4 KiB
JavaScript

'use strict';
const test=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path'),cp=require('node:child_process');
function fixture(t,protection){const dir=fs.mkdtempSync(path.join(os.tmpdir(),'vault-retry-')),previous=process.env.LOCALAPPDATA;process.env.LOCALAPPDATA=dir;const logs=[];t.mock.method(console,'error',(...args)=>logs.push(args.join(' ')));t.mock.method(cp,'execFileSync',(_,args,options)=>{const script=args.at(-1);if(script.includes('::Protect('))return protection(options);if(script.includes('::Unprotect('))return options.input;return '';});delete require.cache[require.resolve('../../src/platform/security/vault.cjs')];const {BpmVault}=require('../../src/platform/security/vault.cjs');let vault;t.after(()=>{vault?.destroy();if(previous===undefined)delete process.env.LOCALAPPDATA;else process.env.LOCALAPPDATA=previous;fs.rmSync(dir,{recursive:true,force:true});});return {dir,logs,create:()=>vault=new BpmVault({stateDir:path.join(dir,'state')}),keys:()=>fs.readdirSync(path.join(dir,'OrderNotificationAutomation','bpm-keys'))};}
test('one timed-out protection attempt retries the same key and commits one readable encrypted identity',{skip:process.platform!=='win32'},t=>{
const inputs=[];const f=fixture(t,options=>{inputs.push(options.input);if(inputs.length===1)throw Object.assign(Error('synthetic-secret timeout'),{code:'ETIMEDOUT'});return options.input;});const vault=f.create(),value={fixture:'private synthetic data'},bytes=vault.encode('sample',value);assert.deepEqual(vault.decode('sample',bytes),value);assert.equal(inputs.length,2);assert.equal(inputs[0],inputs[1]);assert.equal(f.keys().length,1);assert.equal(f.logs.length,1);assert.doesNotMatch(f.logs.join(''),/synthetic-secret|private|ProtectedData|base64/i);assert.equal(bytes.includes(Buffer.from(value.fixture)),false);
});
for(const code of ['ETIMEDOUT','EACCES'])test('protection '+code+' stays bounded and never publishes a partial key',{skip:process.platform!=='win32'},t=>{let calls=0;const f=fixture(t,()=>{calls++;throw Object.assign(Error('synthetic-secret '+code),{code});}),vault=f.create();assert.throws(()=>vault.encode('sample',{fixture:'private'}),e=>e.status===503&&e.stage==='key_protect'&&e.cause.code===code);assert.equal(calls,code==='ETIMEDOUT'?2:1);assert.equal(f.keys().length,0);assert.equal(f.logs.length,code==='ETIMEDOUT'?1:0);assert.doesNotMatch(f.logs.join(''),/synthetic-secret|private/);});