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

test(A03): SHA256 legacy migration and four tamper cases

davoodmehraban89 committed Oct 20, 2026 at 08:38 UTC b60cd19f0e364f72bae2e7639580c5cf6840e5ad
1 file changed +53
tests/a03-audit-chain.test.mjs new
+53
@@ -0,0 +1,53 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { sha256, auditHash, verifyChain, normalizeLegacyAudit } from '../src/core/AuditChain.js';
4 +
5 +function sample() {
6 + let previous = null;
7 + return ['first','second','third'].map((id, index) => {
8 + const row = { id, companyId: 'company-1', chainIndex: index,
9 + prevHash: previous, eventType: 'journal_posted' };
10 + row.eventHash = auditHash(row);
11 + previous = row.eventHash;
12 + return row;
13 + });
14 +}
15 +test('A03 SHA-256 matches published abc vector', () => {
16 + assert.equal(sha256('abc'),
17 + 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad');
18 +});
19 +test('A03 valid chain and anchor', () => {
20 + const rows = sample();
21 + assert.equal(verifyChain(rows, { count: 3, headHash: rows[2].eventHash }).valid, true);
22 +});
23 +test('A03 changed event payload is detected', () => {
24 + const rows = sample();
25 + rows[1].eventType = 'period_closed';
26 + assert.equal(verifyChain(rows).valid, false);
27 +});
28 +test('A03 middle deletion is detected', () => {
29 + const rows = sample();
30 + rows.splice(1, 1);
31 + assert.equal(verifyChain(rows).valid, false);
32 +});
33 +test('A03 reordering is detected', () => {
34 + const rows = sample();
35 + [rows[0], rows[1]] = [rows[1], rows[0]];
36 + assert.equal(verifyChain(rows).valid, false);
37 +});
38 +test('A03 final event deletion needs a retained anchor', () => {
39 + const rows = sample();
40 + const anchor = { count: 3, headHash: rows[2].eventHash };
41 + rows.pop();
42 + assert.equal(verifyChain(rows, anchor).valid, false);
43 + assert.equal(verifyChain(rows).valid, true);
44 +});
45 +test('A03 old backup audit metadata receives deterministic defaults', () => {
46 + const old = { id: 'legacy-1', companyId: 'company-1', eventType: 'journal_posted' };
47 + const migrated = normalizeLegacyAudit(old, 0, null);
48 + assert.equal(migrated.actorId, 'legacy-unknown');
49 + assert.equal(migrated.sourceId, 'legacy-1');
50 + assert.equal(migrated.businessDate, null);
51 + assert.equal(verifyChain([migrated]).valid, true);
52 + assert.deepEqual(normalizeLegacyAudit(old, 0, null), migrated);
53 +});