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

test(A04): checked restore blocks tampered data while accepting supported legacy files

davoodmehraban89 committed Oct 14, 2026 at 23:38 UTC 12fd07a833e9b14f83d86686ea8a9d2227b5615e
1 file changed +33
tests/backup-restore-integration.test.mjs
+33
@@ -136,3 +136,36 @@ test('A04: exported appVersion, package and desktop installer versions agree', a
136 assert.equal(desktop.version, appPackage.version);
137 assert.equal(backup.schemaVersion, (await StorageService.exportAll()).version);
138 });
139 +
140 +test('A04: checked backup restores successfully and rejects a corrupted payload before writes', async () => {
141 + await setup();
142 + const backup = await SettingsController.exportBackup();
143 + backup.data.contacts = [{ id: 'checksum-customer', companyId: 'c1', name: 'Original' }];
144 + const hashed = async data => {
145 + const bytes = await crypto.subtle.digest('SHA-256',
146 + new TextEncoder().encode(JSON.stringify(data)));
147 + return Array.from(new Uint8Array(bytes), x => x.toString(16).padStart(2, '0')).join('');
148 + };
149 + backup.integrity = { algorithm: 'SHA-256', digest: await hashed(backup.data) };
150 + await SettingsController.importBackup(backup);
151 + assert.equal((await StorageService.get('contacts', 'checksum-customer')).name, 'Original');
152 +
153 + await StorageService.delete('contacts', 'checksum-customer');
154 + const corrupted = structuredClone(backup);
155 + corrupted.data.contacts[0].name = 'Tampered';
156 + await assert.rejects(SettingsController.importBackup(corrupted), /تغییر کرده|آسیب/);
157 + assert.equal(await StorageService.get('contacts', 'checksum-customer'), undefined);
158 + await assert.rejects(SettingsController.importBackup({
159 + ...backup, integrity: { algorithm: 'MD5', digest: 'bad' }
160 + }), /نامعتبر/);
161 + assert.equal(await StorageService.get('contacts', 'checksum-customer'), undefined);
162 +});
163 +
164 +test('A04: older backup envelopes without optional checksum retain supported compatibility', async () => {
165 + await setup();
166 + const backup = await SettingsController.exportBackup();
167 + assert.equal(backup.integrity, undefined);
168 + backup.data.contacts = [{ id: 'legacy-contact', companyId: 'c1', name: 'Legacy' }];
169 + await SettingsController.importBackup(backup);
170 + assert.equal((await StorageService.get('contacts', 'legacy-contact')).name, 'Legacy');
171 +});