cmvr-wms/mobile-app/tests/ota.test.mjs

70 lines
6.6 KiB
JavaScript
Raw Normal View History

import test from 'node:test';import assert from 'node:assert/strict';
import { mkdtemp, mkdir, writeFile, readFile } from 'node:fs/promises';import path from 'node:path';import os from 'node:os';
import { createHash, randomUUID, generateKeyPairSync, verify } from 'node:crypto';
import { createOtaServer } from '../scripts/ota-server.mjs';
test('internal OTA feed isolates dev/prod, platform/runtime and only serves immutable hashed assets',async t=>{
const root=await mkdtemp(path.join(os.tmpdir(),'cmvr-ota-'));const server=createOtaServer({root});
await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(()=>new Promise(r=>server.close(r)));
const base=`http://127.0.0.1:${server.address().port}`,runtime='test-native-1',id=randomUUID();
const {privateKey,publicKey}=generateKeyPairSync('rsa',{modulusLength:2048});
const manifest=JSON.stringify({id,runtimeVersion:runtime,metadata:{environment:'dev'}});
const {sign}=await import('node:crypto');const signature=sign('RSA-SHA256',Buffer.from(manifest),privateKey).toString('base64');
for(const dir of [path.join(root,'dev/android',runtime),path.join(root,'releases',id),path.join(root,'assets')])await mkdir(dir,{recursive:true});
await writeFile(path.join(root,'dev/android',runtime,'current.json'),JSON.stringify({id}));
await writeFile(path.join(root,'releases',id,'manifest.json'),manifest);await writeFile(path.join(root,'releases',id,'signature.txt'),`sig="${signature}", keyid="dev"`);
const headers={'expo-platform':'android','expo-runtime-version':runtime};const response=await fetch(base+'/updates/dev',{headers});
assert.equal(response.status,200);assert.equal(response.headers.get('expo-protocol-version'),'1');assert.match(response.headers.get('expo-signature'),/keyid="dev"/);
assert.equal(verify('RSA-SHA256',Buffer.from(await response.text()),publicKey,Buffer.from(signature,'base64')),true);
for(const [channel,runtimeValue] of [['prod',runtime],['dev','other-native']]){
const empty=await fetch(base+`/updates/${channel}`,{headers:{...headers,'expo-runtime-version':runtimeValue}});assert.equal(empty.status,200);assert.match(empty.headers.get('content-type'),/multipart\/mixed/);assert.equal(await empty.text(),'--empty--\r\n');
}
assert.equal((await fetch(base+'/updates/dev',{headers:{...headers,'expo-platform':'ios'}})).status,400);
assert.equal((await fetch(base+'/updates/dev',{method:'POST'})).status,405);
const content=Buffer.from('test-code'),hash=createHash('sha256').update(content).digest('hex');await writeFile(path.join(root,'assets',hash),content);await writeFile(path.join(root,'assets',hash+'.type'),'application/javascript');
const asset=await fetch(base+'/assets/'+hash);assert.equal(await asset.text(),'test-code');assert.match(asset.headers.get('cache-control'),/immutable/);
assert.equal((await fetch(base+'/assets/not-a-hash')).status,404);
assert.equal((await fetch(base+'/keys/private-key.pem')).status,404);
});
test('production service rejects the developer channel, unsupported protocol and invalid configuration',async t=>{
const root=await mkdtemp(path.join(os.tmpdir(),'cmvr-prod-ota-'));
assert.throws(()=>createOtaServer({root,environments:['other']}),/allowlist/);
const server=createOtaServer({root,environments:['prod']});
await new Promise(resolve=>server.listen(0,'127.0.0.1',resolve));t.after(()=>new Promise(resolve=>server.close(resolve)));
const base=`http://127.0.0.1:${server.address().port}`;
const headers={'expo-platform':'android','expo-runtime-version':'native-prod'};
assert.equal((await fetch(base+'/updates/dev',{headers})).status,404);
assert.equal((await fetch(base+'/updates/prod',{headers:{...headers,'expo-protocol-version':'2'}})).status,406);
const empty=await fetch(base+'/updates/prod',{headers});assert.equal(empty.status,200);
assert.equal(empty.headers.get('expo-server-defined-headers'),'');
assert.equal(await empty.text(),'--empty--\r\n');
assert.equal((await fetch(base+'/health')).status,200);
});
test('development update service never serves production manifests, even when public assets are shared',async t=>{
const root=await mkdtemp(path.join(os.tmpdir(),'cmvr-dev-ota-')),runtime='native-dev',id=randomUUID();
const dir=path.join(root,'releases',id);await mkdir(dir,{recursive:true});
await mkdir(path.join(root,'dev/android',runtime),{recursive:true});
await writeFile(path.join(root,'dev/android',runtime,'current.json'),JSON.stringify({id}));
await writeFile(path.join(dir,'manifest.json'),JSON.stringify({id,runtimeVersion:runtime,metadata:{environment:'dev'}}));
await writeFile(path.join(dir,'signature.txt'),'dev-test-signature');
const server=createOtaServer({root,environments:['dev']});await new Promise(r=>server.listen(0,'127.0.0.1',r));
t.after(()=>new Promise(r=>server.close(r)));
const base=`http://127.0.0.1:${server.address().port}`,headers={'expo-platform':'android','expo-runtime-version':runtime};
const dev=await fetch(base+'/updates/dev',{headers});assert.equal(dev.status,200);assert.equal((await dev.json()).metadata.environment,'dev');
assert.equal((await fetch(base+'/updates/prod',{headers})).status,404);
const incompatible=await fetch(base+'/updates/dev',{headers:{...headers,'expo-runtime-version':'native-prod'}});
assert.match(incompatible.headers.get('content-type'),/multipart\/mixed/);
});
test('optional development APK downloads serve only named APKs; secrets and production downloads stay unavailable',async t=>{
const root=await mkdtemp(path.join(os.tmpdir(),'cmvr-dev-download-')),downloads=path.join(root,'downloads');
await mkdir(downloads);await writeFile(path.join(downloads,'cmvr-wms-dev-test-1.apk'),'test APK');
await writeFile(path.join(downloads,'private-key.pem'),'must not be served');
assert.throws(()=>createOtaServer({root,environments:['prod'],downloadDirectory:downloads}),/development-only/);
const server=createOtaServer({root,environments:['dev'],downloadDirectory:downloads});
await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(()=>new Promise(r=>server.close(r)));
const base=`http://127.0.0.1:${server.address().port}`;
const apk=await fetch(base+'/downloads/cmvr-wms-dev-test-1.apk');assert.equal(await apk.text(),'test APK');
assert.match(apk.headers.get('content-type'),/android.package-archive/);assert.equal(apk.headers.get('cache-control'),'no-store');
for(const route of ['/downloads/private-key.pem','/downloads/cmvr-wms-prod-test.apk','/downloads/cmvr-wms-dev-missing.apk','/downloads/../private-key.pem'])assert.equal((await fetch(base+route)).status,404);
assert.equal((await fetch(base+'/downloads/cmvr-wms-dev-test-1.apk',{method:'POST'})).status,405);
});