فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

test(a02): cover Holoo ZIP probe with synthetic valid and hostile archives

davoodmehraban89 committed Oct 30, 2026 at 16:55 UTC 738b10c03495346c46a767e2b4a5ff9a2ec7406a
1 file changed +94
tests/a02-holoo-backup-probe.test.mjs new
+94
@@ -0,0 +1,94 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { probeHolooBackupZip } from '../src/core/HolooBackupProbe.mjs';
4 +
5 +function zip({ name = 'Holoo1', flag = 0, method = 0, entries = 1, comment = '', payload = 'FAKE', corruptCentralName = false } = {}) {
6 + const bytes = new TextEncoder();
7 + const filename = bytes.encode(name);
8 + const data = bytes.encode(payload);
9 + const commentBytes = bytes.encode(comment);
10 + const local = new Uint8Array(30 + filename.length + data.length);
11 + const l = new DataView(local.buffer);
12 + l.setUint32(0, 0x04034b50, true);
13 + l.setUint16(4, 20, true);
14 + l.setUint16(6, flag, true);
15 + l.setUint16(8, method, true);
16 + l.setUint32(18, data.length, true);
17 + l.setUint32(22, data.length, true);
18 + l.setUint16(26, filename.length, true);
19 + local.set(filename, 30);
20 + local.set(data, 30 + filename.length);
21 + const central = new Uint8Array(46 + filename.length);
22 + const c = new DataView(central.buffer);
23 + c.setUint32(0, 0x02014b50, true);
24 + c.setUint16(4, 20, true);
25 + c.setUint16(6, 20, true);
26 + c.setUint16(8, flag, true);
27 + c.setUint16(10, method, true);
28 + c.setUint32(20, data.length, true);
29 + c.setUint32(24, data.length, true);
30 + c.setUint16(28, filename.length, true);
31 + central.set(filename, 46);
32 + if (corruptCentralName) central[46] ^= 1;
33 + const end = new Uint8Array(22 + commentBytes.length);
34 + const e = new DataView(end.buffer);
35 + e.setUint32(0, 0x06054b50, true);
36 + e.setUint16(8, entries, true);
37 + e.setUint16(10, entries, true);
38 + e.setUint32(12, central.length, true);
39 + e.setUint32(16, local.length, true);
40 + e.setUint16(20, commentBytes.length, true);
41 + end.set(commentBytes, 22);
42 + return new Blob([local, central, end]);
43 +}
44 +
45 +test('recognized one-entry ZIP without unpacking or importing records', async () => {
46 + const blob = zip();
47 + const result = await probeHolooBackupZip(blob);
48 + assert.equal(result.entryName, 'Holoo1');
49 + assert.equal(result.zipEnvelopeIdentified, true);
50 + assert.equal(result.uncompressedSize, 4);
51 + assert.equal(result.sqlBackupVerified, false);
52 + assert.equal(result.crcVerified, false);
53 + assert.equal(result.migrationSupported, false);
54 + assert.equal(result.canCommit, false);
55 + assert.equal(result.writePerformed, false);
56 + assert.equal(blob.size, 114);
57 +});
58 +
59 +test('supports trailing ZIP comments without false negative', async () => {
60 + assert.equal((await probeHolooBackupZip(zip({ comment: 'zip-note' }))).entryName, 'Holoo1');
61 +});
62 +
63 +test('rejects invalid source, magic, encryption, multiple entries and unsafe names', async () => {
64 + const invalid = [null, {}, new Blob(), new Blob(['BADZIP'.repeat(12)]),
65 + zip({ flag: 1 }), zip({ entries: 2 }), zip({ name: '../secret' }),
66 + zip({ name: 'C:evil' }), zip({ corruptCentralName: true }), zip({ method: 9 })];
67 + for (const candidate of invalid) {
68 + await assert.rejects(probeHolooBackupZip(candidate));
69 + }
70 +});
71 +
72 +test('rejects damaged end-of-archive, unsupported sizes, and malicious oversized inputs', async () => {
73 + const original = new Uint8Array(await zip().arrayBuffer());
74 + const copy = new Uint8Array(original);
75 + copy[copy.length - 22] ^= 1;
76 + await assert.rejects(probeHolooBackupZip(new Blob([copy])), /HOLOO_ZIP_MISSING_END/);
77 + const oversized = { size: 3 * 1024 ** 3, slice() { throw Error('should not read'); } };
78 + await assert.rejects(probeHolooBackupZip(oversized), /HOLOO_ZIP_TOO_LARGE/);
79 +});
80 +
81 +test('reads only bounded ZIP headers and never reads a payload', async () => {
82 + const blob = zip({payload:'PRIVATE'});
83 + const accesses = [];
84 + const bounded = {
85 + size: blob.size,
86 + slice(start, end) {
87 + accesses.push([start, end ?? blob.size]);
88 + return blob.slice(start, end);
89 + }
90 + };
91 + await probeHolooBackupZip(bounded);
92 + assert.ok(accesses.every(([start, end]) => end - start <= 65557));
93 + assert.equal(accesses.length, 5);
94 +});