22 lines
1.7 KiB
JavaScript
22 lines
1.7 KiB
JavaScript
const fs=require('fs'),vm=require('vm'),assert=require('assert/strict'),crypto=require('crypto');
|
|
const img=new Uint8Array([1,2,3,4]),sha=crypto.createHash('sha256').update(img).digest('hex');
|
|
let fail=true,mode='server',calls=[],downloads=[];
|
|
const ctx={window:{},location:{protocol:'https:'},crypto:crypto.webcrypto,Uint8Array,DataView,AbortSignal,fetch:async(path,opts={})=>{
|
|
calls.push(path);
|
|
if(path==='/api/backup-status')return{ok:true,json:async()=>({mode,retentionHours:24})};
|
|
if(path==='/api/device-backups'){if(fail)throw Error('test network failure');return{ok:true,json:async()=>({sha256:sha,size:4,fileUrl:'/api/device-backups/'+'a'.repeat(32)})};}
|
|
if(path.startsWith('/api/device-backups/'))return{ok:true,arrayBuffer:async()=>img.slice().buffer};
|
|
throw Error(path);
|
|
}};
|
|
vm.runInNewContext(fs.readFileSync(__dirname+'/../src/archive-client.js','utf8'),ctx);
|
|
const a=ctx.window.TamaArchive;
|
|
(async()=>{
|
|
await assert.rejects(()=>a.deliver(img,'save.tamasave',(b,n)=>downloads.push([b,n]),()=>{},'save'),/failure/);
|
|
assert(a.hasPending());assert.equal(downloads.length,0);
|
|
fail=false;await a.retry();assert(!a.hasPending());assert.equal(downloads.length,1);
|
|
assert.notEqual(downloads[0][0],img,'Download must use verified server bytes');assert.deepEqual(downloads[0][0],img);
|
|
assert(calls.indexOf('/api/device-backups')<calls.indexOf('/api/device-backups/'+'a'.repeat(32)));
|
|
mode='local';calls=[];await a.deliver(img,'save.tamasave',()=>{},()=>{},'save');assert.deepEqual(calls,['/api/backup-status']);
|
|
console.log('PASS: server first, no failed-upload download, retained retry, verified server bytes and admin local policy');
|
|
})().catch(e=>{console.error(e);process.exit(1);});
|