entry.test.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Keyless entry-package tests — run on every host, no kernel or binary
  3. * required. Cover the JS seam's pure surface: grant-argv construction, the
  4. * resolution contract (platform package → fallback), and probe verdicts over
  5. * fake launchers. Requires built `lib/` (`pnpm build:ts`).
  6. */
  7. import assert from 'node:assert/strict';
  8. import fs from 'node:fs';
  9. import os from 'node:os';
  10. import path from 'node:path';
  11. import {
  12. LAUNCHER_BIN,
  13. LAUNCHER_FAILURE_EXIT,
  14. grantArgs,
  15. launcherPath,
  16. probe,
  17. } from 'node-addon-landlock-run';
  18. // --- constants are part of the CLI contract ---
  19. assert.equal(LAUNCHER_BIN, 'landlock-run');
  20. assert.equal(LAUNCHER_FAILURE_EXIT, 125);
  21. // --- grantArgs: flag spelling, ordering, and empty grants ---
  22. assert.deepEqual(grantArgs({}), []);
  23. assert.deepEqual(grantArgs({ readOnly: ['/'] }), ['--ro', '/']);
  24. assert.deepEqual(
  25. grantArgs({ readOnly: ['/', '/opt'], readWrite: ['/tmp/work'] }),
  26. ['--ro', '/', '--ro', '/opt', '--rw', '/tmp/work'],
  27. );
  28. assert.deepEqual(grantArgs({ readWrite: ['/a'], readOnly: ['/b'] }), ['--ro', '/b', '--rw', '/a']);
  29. // --- launcherPath: resolves the platform package next to its package.json ---
  30. const platformPackage = `node-addon-landlock-run-${process.platform}-${process.arch}`;
  31. const resolvedViaSeam = launcherPath((specifier) => {
  32. assert.equal(specifier, `${platformPackage}/package.json`);
  33. return path.join('/fake-install', specifier);
  34. });
  35. assert.equal(resolvedViaSeam, path.join('/fake-install', platformPackage, 'bin', LAUNCHER_BIN));
  36. // --- launcherPath: unresolvable package falls back to an absolute, package-boundary path ---
  37. const fallback = launcherPath(() => {
  38. throw new Error('not installed');
  39. });
  40. assert.ok(path.isAbsolute(fallback), 'fallback path must be absolute');
  41. assert.ok(
  42. fallback.includes(path.join('node_modules', ...platformPackage.split('/'), 'bin', LAUNCHER_BIN)),
  43. `fallback must point at the platform package layout: ${fallback}`,
  44. );
  45. // --- launcherPath: default resolution agrees with this workspace's layout ---
  46. const defaultPath = launcherPath();
  47. assert.ok(path.isAbsolute(defaultPath));
  48. assert.ok(defaultPath.endsWith(path.join('bin', LAUNCHER_BIN)), defaultPath);
  49. // --- probe: a missing launcher is unusable, indistinguishable from an unenforcing kernel ---
  50. assert.equal(probe(path.join(os.tmpdir(), 'nalr-no-such-launcher')), 'unusable');
  51. // --- probe: verdict parsing over fake launchers (POSIX shells only) ---
  52. if (process.platform !== 'win32') {
  53. const fakeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nalr-fake-launcher-'));
  54. const fake = (name, script) => {
  55. const file = path.join(fakeDir, name);
  56. fs.writeFileSync(file, `#!/bin/sh\n${script}\n`, { mode: 0o755 });
  57. return file;
  58. };
  59. assert.equal(probe(fake('full', 'echo "landlock: fully enforced"; exit 0')), 'full');
  60. assert.equal(probe(fake('partial', 'echo "landlock: partially enforced (older ABI)"; exit 0')), 'partial');
  61. assert.equal(probe(fake('failing', `exit ${LAUNCHER_FAILURE_EXIT}`)), 'unusable');
  62. assert.equal(probe(fake('hanging', 'sleep 10'), { timeoutMs: 200 }), 'unusable');
  63. fs.rmSync(fakeDir, { recursive: true, force: true });
  64. }
  65. console.log('entry.test: ok');