app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

source-archive.test.mjs (10629B)


      1 import assert from 'node:assert/strict';
      2 import { execFileSync } from 'node:child_process';
      3 import { readFile } from 'node:fs/promises';
      4 import { test } from 'node:test';
      5 import { fileURLToPath } from 'node:url';
      6 
      7 const gitEnv = Object.fromEntries(
      8   Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_'))
      9 );
     10 const root = fileURLToPath(new URL('../../../', import.meta.url));
     11 /** @param {string[]} args */
     12 const git = (...args) =>
     13   execFileSync('git', ['-C', root, ...args], {
     14     env: gitEnv,
     15     maxBuffer: 64 * 1024 * 1024
     16   });
     17 const revision = git('rev-parse', 'HEAD').toString().trim();
     18 const epoch = Number(
     19   git('show', '-s', '--format=%ct', revision).toString().trim()
     20 );
     21 const tree = git('ls-tree', '-rz', '--full-tree', revision)
     22   .toString()
     23   .split('\0')
     24   .filter(Boolean)
     25   .map((entry) => {
     26     const match = /^(100644|100755) blob ([a-f0-9]{40})\t(.+)$/.exec(entry);
     27     assert.ok(match, 'Full source tree must contain regular Git blobs only');
     28     return {
     29       mode: match[1] === '100755' ? 0o755 : 0o644,
     30       blob: match[2],
     31       name: match[3]
     32     };
     33   })
     34   .sort((a, b) => Buffer.compare(Buffer.from(a.name), Buffer.from(b.name)));
     35 // One actual Git blob batch, independent from filesystem/generated outputs.
     36 const raw = execFileSync('git', ['-C', root, 'cat-file', '--batch'], {
     37   input: tree.map((entry) => entry.blob).join('\n') + '\n',
     38   env: gitEnv,
     39   maxBuffer: 64 * 1024 * 1024
     40 });
     41 let cursor = 0;
     42 const inputs = tree.map((entry) => {
     43   const end = raw.indexOf(10, cursor);
     44   const header = raw.subarray(cursor, end).toString().split(' ');
     45   assert.equal(header[0], entry.blob);
     46   assert.equal(header[1], 'blob');
     47   const size = Number(header[2]);
     48   const bytes = raw.subarray(end + 1, end + 1 + size);
     49   assert.equal(bytes.length, size);
     50   cursor = end + size + 2;
     51   return { ...entry, bytes };
     52 });
     53 assert.equal(cursor, raw.length);
     54 /** @param {Buffer} header @param {number} offset @param {number} width @param {number} value */
     55 function octal(header, offset, width, value) {
     56   const field = value.toString(8).padStart(width - 1, '0') + '\0';
     57   assert.equal(field.length, width);
     58   header.write(field, offset, width, 'ascii');
     59 }
     60 /** @param {typeof inputs} entries */
     61 function encodeFixture(entries) {
     62   // Test-only canonical materialization: never a release/distribution producer.
     63   const blocks = [];
     64   for (const entry of entries) {
     65     const header = Buffer.alloc(512);
     66     let name = entry.name;
     67     let prefix = '';
     68     if (Buffer.byteLength(name) > 100) {
     69       const split = name.lastIndexOf('/');
     70       prefix = name.slice(0, split);
     71       name = name.slice(split + 1);
     72     }
     73     assert.ok(
     74       Buffer.byteLength(name) <= 100 && Buffer.byteLength(prefix) <= 155
     75     );
     76     header.write(name, 0, 100);
     77     octal(header, 100, 8, entry.mode);
     78     octal(header, 108, 8, 0);
     79     octal(header, 116, 8, 0);
     80     octal(header, 124, 12, entry.bytes.length);
     81     octal(header, 136, 12, epoch);
     82     header.fill(32, 148, 156);
     83     header[156] = 48;
     84     header.write('ustar\0', 257, 6);
     85     header.write('00', 263, 2);
     86     header.write(prefix, 345, 155);
     87     const checksum = header.reduce((sum, value) => sum + value, 0);
     88     header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148, 8);
     89     blocks.push(
     90       header,
     91       entry.bytes,
     92       Buffer.alloc((512 - (entry.bytes.length % 512)) % 512)
     93     );
     94   }
     95   return Buffer.concat([...blocks, Buffer.alloc(1024)]);
     96 }
     97 /** @param {Buffer} bytes */
     98 function decodeAndCompare(bytes) {
     99   let offset = 0;
    100   let count = 0;
    101   let previous = Buffer.alloc(0);
    102   while (
    103     offset < bytes.length &&
    104     !bytes.subarray(offset, offset + 512).equals(Buffer.alloc(512))
    105   ) {
    106     const header = bytes.subarray(offset, offset + 512);
    107     assert.equal(header.length, 512, 'Truncated header');
    108     /** @param {number} start @param {number} width */
    109     const field = (start, width) => {
    110       const part = header.subarray(start, start + width);
    111       const zero = part.indexOf(0);
    112       if (zero !== -1)
    113         assert.ok(
    114           part.subarray(zero).every((value) => value === 0),
    115           'Noncanonical field padding'
    116         );
    117       return new TextDecoder('utf-8', { fatal: true }).decode(
    118         zero === -1 ? part : part.subarray(0, zero)
    119       );
    120     };
    121     /** @param {number} start @param {number} width */
    122     const number = (start, width) => {
    123       const value = field(start, width);
    124       assert.match(value, /^[0-7]+$/);
    125       return Number.parseInt(value, 8);
    126     };
    127     const prefix = field(345, 155);
    128     const name = (prefix ? prefix + '/' : '') + field(0, 100);
    129     assert.ok(
    130       name &&
    131         !name.startsWith('/') &&
    132         !name.split('/').some((part) => !part || part === '.' || part === '..'),
    133       'Unsafe archive path'
    134     );
    135     const ordered = Buffer.from(name);
    136     assert.ok(
    137       Buffer.compare(previous, ordered) < 0,
    138       'Bytewise order/duplicate violation'
    139     );
    140     previous = ordered;
    141     assert.equal(
    142       header[156],
    143       48,
    144       'Only regular files, no PAX/directory/link/submodule'
    145     );
    146     assert.equal(field(157, 100), '', 'No link target');
    147     assert.equal(header.subarray(257, 263).toString(), 'ustar\0');
    148     assert.equal(header.subarray(263, 265).toString(), '00');
    149     for (const [start, width] of [
    150       [265, 32],
    151       [297, 32]
    152     ])
    153       assert.equal(field(start, width), '', 'No owner names');
    154     assert.equal(number(108, 8), 0);
    155     assert.equal(number(116, 8), 0);
    156     assert.ok(
    157       header.subarray(329, 345).every((value) => value === 0),
    158       'No device metadata'
    159     );
    160     assert.ok(
    161       header.subarray(500).every((value) => value === 0),
    162       'No header extension'
    163     );
    164     assert.equal(number(136, 12), epoch, 'Actual candidate epoch');
    165     const expected = inputs[count++];
    166     assert.ok(expected, 'Extra archive entry');
    167     assert.equal(name, expected.name, 'Exact unprefixed full-tree path');
    168     assert.equal(number(100, 8), expected.mode, 'Exact Git executable mode');
    169     const size = number(124, 12);
    170     assert.equal(size, expected.bytes.length);
    171     const checksum = header.reduce(
    172       (sum, value, index) => sum + (index >= 148 && index < 156 ? 32 : value),
    173       0
    174     );
    175     assert.match(header.subarray(148, 156).toString(), /^[0-7]{6}\0 $/);
    176     assert.equal(
    177       Number.parseInt(header.subarray(148, 154).toString(), 8),
    178       checksum
    179     );
    180     offset += 512;
    181     assert.deepEqual(
    182       bytes.subarray(offset, offset + size),
    183       expected.bytes,
    184       'Actual Git blob bytes'
    185     );
    186     offset += size;
    187     const padding = (512 - (size % 512)) % 512;
    188     assert.ok(
    189       bytes.subarray(offset, offset + padding).every((value) => value === 0),
    190       'Zero content padding'
    191     );
    192     offset += padding;
    193   }
    194   assert.equal(
    195     count,
    196     inputs.length,
    197     'Complete tracked tree, never runtime-filtered'
    198   );
    199   assert.equal(bytes.length - offset, 1024, 'Exactly two zero trailer blocks');
    200   assert.ok(bytes.subarray(offset).every((value) => value === 0));
    201 }
    202 const archive = encodeFixture(inputs);
    203 await test('native v3 JSON and Kotlin canonical authority remain exact and separate from web provenance', async () => {
    204   const contractBytes = await readFile(
    205     new URL(
    206       '../../../contracts/release/harvestcircle-artifact-contract.v3.json',
    207       import.meta.url
    208     )
    209   );
    210   const kotlin = await readFile(
    211     new URL(
    212       '../../../build-logic/contracts/src/main/kotlin/org/harvestcircle/buildlogic/contracts/HarvestCircleArtifactContract.kt',
    213       import.meta.url
    214     ),
    215     'utf8'
    216   );
    217   const literal = /CANONICAL_JSON: String =\s*("(?:[^"\\]|\\.)*")/.exec(kotlin);
    218   assert.ok(literal);
    219   assert.equal(contractBytes.toString(), JSON.parse(literal[1]));
    220   const contract = JSON.parse(contractBytes.toString());
    221   assert.equal(contract.contract_version, 3);
    222   assert.equal(contract.package_contract.filename, 'HarvestCircle-1.0.0.dmg');
    223   assert.equal(contract.producer.source_task, 'packageDmg');
    224   assert.equal(contract.source_archive.format, 'ustar');
    225   assert.equal(contract.source_archive.content, 'exact_source_revision_tree');
    226   assert.equal(contract.source_archive.pax_headers, 'forbidden');
    227   assert.equal(contract.source_archive.trailer, 'two_zero_blocks');
    228   assert.equal(contract.delivery.publication, 'unauthorized');
    229   assert.equal(contract.sqlite.high_level_authority, 'sqlx_only');
    230 });
    231 await test('actual complete HEAD Git tree fixture preserves web, licenses, corpus, modes, epoch and blobs', () => {
    232   decodeAndCompare(archive);
    233   assert.ok(inputs.some((entry) => entry.name === 'web/package.json'));
    234   assert.ok(inputs.some((entry) => entry.name === 'LICENSE'));
    235   assert.ok(inputs.some((entry) => entry.name.startsWith('LICENSES/')));
    236   assert.ok(
    237     inputs.some((entry) =>
    238       entry.name.startsWith('core/crates/harvestcircle_nostr/tests/fixtures/')
    239     )
    240   );
    241   assert.ok(inputs.some((entry) => entry.mode === 0o755));
    242 });
    243 for (const kind of [
    244   'filtered',
    245   'truncated',
    246   'trailer',
    247   'content',
    248   'mode',
    249   'epoch',
    250   'uid',
    251   'prefix',
    252   'pax',
    253   'directory',
    254   'symlink',
    255   'hardlink',
    256   'order',
    257   'duplicate',
    258   'padding'
    259 ]) {
    260   await test(`independent full-tree archive comparison rejects ${kind}`, () => {
    261     let candidate = Buffer.from(archive);
    262     if (kind === 'filtered')
    263       candidate = encodeFixture(
    264         inputs.filter((entry) => !entry.name.startsWith('web/'))
    265       );
    266     else if (kind === 'order')
    267       candidate = encodeFixture([inputs[1], inputs[0], ...inputs.slice(2)]);
    268     else if (kind === 'duplicate')
    269       candidate = encodeFixture([inputs[0], ...inputs]);
    270     else if (kind === 'truncated') candidate = candidate.subarray(0, 200);
    271     else if (kind === 'trailer')
    272       candidate = Buffer.concat([candidate, Buffer.alloc(512)]);
    273     else if (kind === 'content') candidate[512] ^= 1;
    274     else if (kind === 'mode')
    275       octal(candidate, 100, 8, inputs[0].mode === 0o644 ? 0o755 : 0o644);
    276     else if (kind === 'epoch') octal(candidate, 136, 12, epoch + 1);
    277     else if (kind === 'uid') octal(candidate, 108, 8, 1);
    278     else if (kind === 'prefix') candidate.write('runtime/', 0);
    279     else if (kind === 'padding') candidate[512 + inputs[0].bytes.length] = 1;
    280     else {
    281       const type = { pax: 120, directory: 53, symlink: 50, hardlink: 49 }[kind];
    282       if (type === undefined)
    283         throw new Error('Unknown negative archive fixture');
    284       candidate[156] = type;
    285     }
    286     assert.throws(() => decodeAndCompare(candidate));
    287   });
    288 }
    289 await test('ordinary git archive PAX/directory output is not the native v3 canonical fixture', () => {
    290   assert.throws(() =>
    291     decodeAndCompare(git('archive', '--format=tar', revision))
    292   );
    293 });