fix: make client integration kit self-contained and clarify capabilities
Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash, generateKeyPairSync, sign } from 'node:crypto';
|
||||
import { mkdtemp, readdir, rm } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { Readable } from 'node:stream';
|
||||
import { UpdateClient, UpdateError, verifyCheck } from './update-sdk.mjs';
|
||||
|
||||
const { privateKey, publicKey } = generateKeyPairSync('ec', { namedCurve: 'prime256v1' });
|
||||
const now = Date.now();
|
||||
const payload = {version:'update-v1',instance_id:'i1',product_id:1,customer_id:2,current_version:'1.0.0',platform:'linux',arch:'x64',channel:'stable',server_time:new Date(now).toISOString().replace('.000Z','Z'),expires_at:new Date(now+600000).toISOString().replace('.000Z','Z'),policy_revision:3,policy_enabled:true,recommended_version:'2.0.0',minimum_supported_version:'1.0.0',enforce_after:'',mandatory:false,required_now:false,update_available:true,download_allowed:true,release_id:7,sha256:createHash('sha256').update('good').digest('hex'),size_bytes:4,message:''};
|
||||
function envelope(p = payload) { const raw = Buffer.from(JSON.stringify(p)); return {payload:raw.toString('base64').replace(/=+$/,''),signature:sign('sha256',raw,privateKey).toString('base64').replace(/=+$/,''),key_id:'k1'}; }
|
||||
const keys = {k1:publicKey.export({type:'spki',format:'pem'})};
|
||||
const signed = envelope();
|
||||
verifyCheck(signed, keys, 'i1', {now,expectedPlatform:'linux',expectedArch:'x64',expectedChannel:'stable',expectedCurrentVersion:'1.0.0'});
|
||||
const tampered = {...signed, payload:Buffer.from('{"version":"update-v1"}').toString('base64').replace(/=+$/,'')};
|
||||
assert.throws(() => verifyCheck(tampered, keys, 'i1', {now}), UpdateError);
|
||||
assert.throws(() => verifyCheck(signed, keys, 'wrong', {now}), UpdateError);
|
||||
assert.throws(() => verifyCheck(signed, keys, 'i1', {now:now+600001}), UpdateError);
|
||||
assert.throws(() => verifyCheck(signed, keys, 'i1', {now,previous:{policy_revision:4}}), UpdateError);
|
||||
assert.throws(() => verifyCheck(signed, keys, 'i1', {now,expectedCurrentVersion:'0.9'}), UpdateError);
|
||||
|
||||
for (const [status, chunks, expected] of [[200,['good-extra'],'package_too_large'],[206,['good'],'download_http_206'],[302,[],'download_http_302'],[500,[],'download_http_500'],[200,['evil'],'package_integrity_failed']]) {
|
||||
const dir = await mkdtemp(join(tmpdir(),'update-sdk-')); const target = join(dir,'pkg.bin');
|
||||
const fetchImpl = async () => new Response(Readable.toWeb(Readable.from(chunks.map(x => Buffer.from(x)))), {status,headers: status===302?{location:'https://evil.invalid'}:{}});
|
||||
const client = new UpdateClient({baseUrl:'https://license.example',instanceId:'i1',secret:'secret',publicKeys:keys,fetchImpl});
|
||||
await assert.rejects(client.download({payload},target), e => e instanceof UpdateError && e.message.includes(expected));
|
||||
assert.deepEqual(await readdir(dir), []); await rm(dir,{recursive:true,force:true});
|
||||
}
|
||||
|
||||
let persisted;
|
||||
const state = {load:()=>persisted,saveAtomic:value=>{persisted=value;}};
|
||||
const fetchImpl = async () => Response.json({data:signed});
|
||||
const client = new UpdateClient({baseUrl:'https://license.example',instanceId:'i1',secret:'secret',publicKeys:keys,state,fetchImpl,clock:()=>now});
|
||||
persisted={scope:JSON.stringify(['i1','windows','x64','stable']),policy_revision:99};
|
||||
assert.equal((await client.check('1.0.0','linux','x64','stable')).payload.policy_revision,3);
|
||||
console.log('Node update SDK negative tests passed');
|
||||
Reference in New Issue
Block a user