okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge commit 'b8d604d28' into f-clean-1ab0fc432ed

main
3.1 KiB62 linesraw
1import test from 'node:test';
2import assert from 'node:assert/strict';
3import { createHash } from 'node:crypto';
4import { mkdtemp, readdir, rm } from 'node:fs/promises';
5import { tmpdir } from 'node:os';
6import { join } from 'node:path';
7import { connectorAsset, createConnectorInstaller, verifyConnector } from '../server/cloudflared-install.mjs';
8
9test('automatic setup selects native executables and rejects unsupported platforms', () => {
10  assert.equal(connectorAsset('darwin','arm64'),'cloudflared-darwin-arm64.tgz');
11  assert.equal(connectorAsset('linux','x64'),'cloudflared-linux-amd64');
12  assert.equal(connectorAsset('win32','x64'),'cloudflared-windows-amd64.exe');
13  assert.throws(()=>connectorAsset('win32','arm64'),/not available/);
14  assert.throws(()=>connectorAsset('unknown','x64'),/not available/);
15});
16
17test('corrupt or unverified downloads cannot become executable', () => {
18  const bytes=Buffer.from('official connector fixture');
19  const digest=`sha256:${createHash('sha256').update(bytes).digest('hex')}`;
20  assert.doesNotThrow(()=>verifyConnector(bytes,digest));
21  assert.throws(()=>verifyConnector(Buffer.from('tampered'),digest),/could not be verified/);
22  assert.throws(()=>verifyConnector(bytes,null),/could not be verified/);
23});
24
25/** Waits until the fake download service has been asked; fails (instead of spinning for ever) when it never is. */
26async function untilAsked(isAsked){
27  const deadline=Date.now()+10000;
28  while(!isAsked()){
29    if(Date.now()>deadline)throw new Error('the installer never asked the download service (it found a connector, or stopped before the request)');
30    await new Promise(resolve=>setTimeout(resolve,5));
31  }
32}
33
34// The installer first looks for a connector that is already on the computer. The look is pinned to Strata's own empty folder
35// (search:false, an empty environment): a cloudflared installed on the machine that runs the test (PATH, /usr/local/bin, ~/.local/bin)
36// would otherwise be used, nothing would be downloaded, and the test would wait for ever for a request that never comes.
37test('failed concurrent setup shares one download, cleans up and can be retried', {timeout:30000}, async () => {
38  const directory=await mkdtemp(join(tmpdir(),'strata-connector-test-'));
39  let requests=0, release;
40  const installer=createConnectorInstaller({directory,platform:'linux',arch:'x64',env:{},home:directory,search:false,fetchImpl:async()=>{
41    requests++;
42    await new Promise(resolve=>{release=resolve;});
43    return {ok:false};
44  }});
45  try {
46    const first=installer.ensure(),second=installer.ensure();
47    assert.equal(first,second);
48    await untilAsked(()=>release);
49    release();
50    const results=await Promise.allSettled([first,second]);
51    assert.ok(results.every(result=>result.status==='rejected'));
52    assert.equal(requests,1);
53    assert.deepEqual(await readdir(directory),[]);
54    release=null;
55    const retry=installer.ensure();
56    await untilAsked(()=>release);
57    release();
58    await assert.rejects(retry,/download service/);
59    assert.equal(requests,2);
60    assert.deepEqual(await readdir(directory),[]);
61  } finally {await rm(directory,{recursive:true,force:true});}
62});