'use strict'; const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const { verifyProtectedTree, readManifest } = require('../tools/check-protected-lib'); const repositoryRoot = path.resolve(__dirname, '..'); const manifest = readManifest(); function temporaryRepository(t) { // Keep temporary writes within the checkout; no source fixture is modified. const directory = fs.mkdtempSync(path.join(repositoryRoot, '.protected-test-')); t.after(() => fs.rmSync(directory, { recursive: true, force: true })); fs.mkdirSync(path.join(directory, 'scripts')); fs.cpSync(path.join(repositoryRoot, 'scripts/lib'), path.join(directory, 'scripts/lib'), { recursive: true }); return directory; } test('the actual protected library matches the reviewed baseline', () => { const result = verifyProtectedTree(repositoryRoot, manifest); assert.equal(result.ok, true, result.errors.join('\n')); assert.equal(result.fileCount, 7); }); test('an unchanged complete copy passes', t => { const result = verifyProtectedTree(temporaryRepository(t), manifest); assert.equal(result.ok, true, result.errors.join('\n')); }); test('changed protected source is detected', t => { const directory = temporaryRepository(t); fs.appendFileSync(path.join(directory, 'scripts/lib/proxer_utils.py'), '\n# test change\n'); const result = verifyProtectedTree(directory, manifest); assert.equal(result.ok, false); assert.match(result.errors.join('\n'), /content changed: scripts\/lib\/proxer_utils\.py/); }); test('missing protected bytecode is detected', t => { const directory = temporaryRepository(t); fs.unlinkSync(path.join(directory, 'scripts/lib/__pycache__/proxer_utils.cpython-311.pyc')); const result = verifyProtectedTree(directory, manifest); assert.equal(result.ok, false); assert.match(result.errors.join('\n'), /Missing protected entry/); }); test('new bytecode and hidden files are detected even if git would ignore them', t => { const directory = temporaryRepository(t); fs.writeFileSync(path.join(directory, 'scripts/lib/__pycache__/proxer_utils.cpython-314.pyc'), 'new bytecode'); fs.writeFileSync(path.join(directory, 'scripts/lib/.generated'), 'new hidden file'); const result = verifyProtectedTree(directory, manifest); assert.equal(result.ok, false); assert.match(result.errors.join('\n'), /Unexpected protected entry: scripts\/lib\/__pycache__\/proxer_utils\.cpython-314\.pyc/); assert.match(result.errors.join('\n'), /Unexpected protected entry: scripts\/lib\/\.generated/); }); test('an added empty directory is detected', t => { const directory = temporaryRepository(t); fs.mkdirSync(path.join(directory, 'scripts/lib/generated')); const result = verifyProtectedTree(directory, manifest); assert.equal(result.ok, false); assert.match(result.errors.join('\n'), /Unexpected protected entry: scripts\/lib\/generated/); }); test('a protected file replaced by a symlink is detected without following it', t => { const directory = temporaryRepository(t); const file = path.join(directory, 'scripts/lib/proxer_utils.py'); fs.unlinkSync(file); fs.symlinkSync('/nonexistent/protected-test-target', file); const result = verifyProtectedTree(directory, manifest); assert.equal(result.ok, false); assert.match(result.errors.join('\n'), /entry type changed: scripts\/lib\/proxer_utils\.py/); }); test('a symlinked scripts directory is rejected', t => { const directory = temporaryRepository(t); fs.renameSync(path.join(directory, 'scripts'), path.join(directory, 'moved-scripts')); fs.symlinkSync('moved-scripts', path.join(directory, 'scripts')); const result = verifyProtectedTree(directory, manifest); assert.equal(result.ok, false); assert.match(result.errors.join('\n'), /scripts must be a real directory/); }); test('executable-bit changes are detected', t => { const directory = temporaryRepository(t); fs.chmodSync(path.join(directory, 'scripts/lib/proxer_utils.py'), 0o644); const result = verifyProtectedTree(directory, manifest); assert.equal(result.ok, false); assert.match(result.errors.join('\n'), /executable bit changed/); }); test('an invalid manifest cannot redirect the checked path', t => { const directory = temporaryRepository(t); const result = verifyProtectedTree(directory, { ...manifest, protectedRoot: '../outside' }); assert.equal(result.ok, false); assert.match(result.errors.join('\n'), /Invalid protected-library manifest/); });