import fs from 'node:fs/promises' import path from 'node:path' import assert from 'node:assert/strict' import { createHash } from 'node:crypto' import { spawnSync } from 'node:child_process' const output = await fs.mkdtemp(path.resolve('.tmp/rea-extra-resources-')), root = path.join(output, 'fixture'), checks = [] await fs.mkdir(root) const hash = b => createHash('sha256').update(b).digest('hex') const names = ['Tool.cs', 'Tool.csproj', 'Tool.slnx', 'schema.xml', 'schema.xsd', 'setup.sh', 'setup.ps1', 'viewer.html', 'font.LICENSE', 'LICENSE', '.gitignore'] for (const name of names) await fs.writeFile(path.join(root, name), 'OWN_FIXTURE_UTF8_中文_' + name) const font = Buffer.alloc(32); font.writeUInt32BE(0x00010000); font.writeUInt16BE(1, 4); font.write('test', 12); font.writeUInt32BE(28, 20); font.writeUInt32BE(4, 24) await fs.writeFile(path.join(root, 'valid.ttf'), font) await fs.writeFile(path.join(root, 'invalid.ttf'), 'not an SFNT font') const badRange = Buffer.from(font); badRange.writeUInt32BE(1000, 20); await fs.writeFile(path.join(root, 'range.ttf'), badRange) const cli = (command, ...args) => spawnSync(process.execPath, ['.tmp/rea-css-runtime/scripts/rea.mjs', command, root, ...args], { encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 }) const inventory = cli('inspect-selected-directory-text-root'); assert.equal(inventory.status, 0, inventory.stderr) const pin = JSON.parse(inventory.stdout).normalized_result.root_sha256 const text = cli('export-selected-directory-text', pin, JSON.stringify(names)); assert.equal(text.status, 0, text.stderr) const files = JSON.parse(text.stdout).normalized_result.files for (const name of names) { const file = files.find(f => f.path === name); assert.equal(file.text, 'OWN_FIXTURE_UTF8_中文_' + name); assert.equal(file.sha256, hash(Buffer.from(file.text))); checks.push('exact UTF-8 export: ' + name) } const binary = (name, bytes, sha = hash(bytes)) => cli('export-selected-directory-binary', pin, JSON.stringify([{ path: name, sha256: sha }])) const valid = binary('valid.ttf', font); assert.equal(valid.status, 0, valid.stderr); const result = JSON.parse(valid.stdout).normalized_result.files[0] assert.equal(result.media_type, 'font/ttf'); assert.deepEqual(Buffer.from(result.data_base64, 'base64'), font); checks.push('SFNT font exact bytes/SHA/media') for (const [label, response, pattern] of [['invalid TTF signature', binary('invalid.ttf', Buffer.from('not an SFNT font')), /TTF signature/], ['TTF table bounds', binary('range.ttf', badRange), /table range/], ['selected SHA pin', binary('valid.ttf', font, '0'.repeat(64)), /SHA mismatch/], ['path traversal', cli('export-selected-directory-text', pin, JSON.stringify(['../Tool.cs'])), /canonical/]]) { assert.notEqual(response.status, 0, label); assert.match(response.stderr, pattern, label); checks.push(label) } await fs.writeFile(path.join(output, 'report.json'), JSON.stringify({ result: 'PASS', checks, ownFixtureOnly: true }, null, 2)) console.log(JSON.stringify({ result: 'PASS', checks: checks.length, directory: output }))